From dc2e581537ef6784c5d62633b3df5fe3f5b6894f Mon Sep 17 00:00:00 2001 From: bal7hazar Date: Wed, 21 Aug 2024 10:48:54 -0400 Subject: [PATCH] =?UTF-8?q?=E2=9C=85=20Fix=20and=20add=20unit=20tests?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- contracts/src/models/dungeon.cairo | 45 +++++++++++++++++++ contracts/src/models/player.cairo | 70 ++++++++++++++++++++++++++++++ contracts/src/tests/setup.cairo | 2 +- 3 files changed, 116 insertions(+), 1 deletion(-) diff --git a/contracts/src/models/dungeon.cairo b/contracts/src/models/dungeon.cairo index daf557d..400866d 100644 --- a/contracts/src/models/dungeon.cairo +++ b/contracts/src/models/dungeon.cairo @@ -69,3 +69,48 @@ impl DungeonAssert of AssertTrait { } } +#[cfg(test)] +mod tests { + // Local imports + + use super::{ + Dungeon, DungeonTrait, AssertTrait, Role, RoleTrait, Monster, MonsterTrait, Mode, ModeTrait + }; + + // Constants + + const ID: felt252 = 'ID'; + const MONSTER: Monster = Monster::Common; + const ROLE: Role = Role::Fire; + + #[test] + fn test_dungeon_new() { + let dungeon: Dungeon = DungeonTrait::new(ID, MONSTER, ROLE); + assert_eq!(dungeon.id, ID); + assert_eq!(dungeon.monster, MONSTER.into()); + assert_eq!(dungeon.role, ROLE.into()); + assert_eq!(dungeon.damage, MONSTER.damage()); + assert_eq!(dungeon.health, MONSTER.health()); + assert_eq!(dungeon.reward, MONSTER.reward()); + } + + #[test] + fn test_dungeon_is_done() { + let dungeon: Dungeon = DungeonTrait::new(ID, Monster::None, Role::None); + assert!(dungeon.is_done()); + let mut dungeon: Dungeon = DungeonTrait::new(ID, MONSTER, ROLE); + assert!(!dungeon.is_done()); + dungeon.take_damage(Role::Water, dungeon.health); + assert!(dungeon.is_done()); + } + + #[test] + fn test_dungeon_take_damage() { + let mut dungeon: Dungeon = DungeonTrait::new(ID, MONSTER, ROLE); + let damage: u8 = 10; + let player_role: Role = Role::Water; + dungeon.take_damage(player_role, damage); + assert_eq!(dungeon.health < MONSTER.health(), true); + } +} + diff --git a/contracts/src/models/player.cairo b/contracts/src/models/player.cairo index 374108b..20c0bda 100644 --- a/contracts/src/models/player.cairo +++ b/contracts/src/models/player.cairo @@ -128,3 +128,73 @@ impl PlayerAssert of AssertTrait { } } +#[cfg(test)] +mod tests { + // Local imports + + use super::{ + Player, PlayerTrait, Role, Mode, ModeTrait, Direction, Monster, MonsterTrait, + DEFAULT_PLAYER_DAMAGE, DEFAULT_PLAYER_HEALTH, DEFAULT_PLAYER_GOLD, DEFAULT_POTION_COST, + DEFAULT_POTION_HEAL + }; + + // Constants + + const ID: felt252 = 'ID'; + const PLAYER_NAME: felt252 = 'Alice'; + const TIME: u64 = 0; + const MODE: Mode = Mode::Easy; + + #[test] + fn test_player_new() { + let player = PlayerTrait::new(ID, PLAYER_NAME, TIME, MODE); + assert_eq!(player.id, ID); + assert_eq!(player.name, PLAYER_NAME); + assert_eq!(player.mode, MODE.into()); + assert_eq!(player.role, Role::None.into()); + assert_eq!(player.damage, DEFAULT_PLAYER_DAMAGE); + assert_eq!(player.health, DEFAULT_PLAYER_HEALTH); + assert_eq!(player.gold, DEFAULT_PLAYER_GOLD); + assert_eq!(player.score, 0); + } + + #[test] + fn test_player_enrole() { + let mut player = PlayerTrait::new(ID, PLAYER_NAME, TIME, MODE); + player.enrole(Role::Fire); + assert_eq!(player.role, Role::Fire.into()); + } + + #[test] + fn test_player_move() { + let mut player = PlayerTrait::new(ID, PLAYER_NAME, TIME, MODE); + let seed = player.seed; + player.move(Direction::Up); + assert_eq!(seed, player.seed); + } + + #[test] + fn test_player_take_damage() { + let mut player = PlayerTrait::new(ID, PLAYER_NAME, TIME, MODE); + player.take_damage(Role::Fire, 10); + assert_eq!(player.health < DEFAULT_PLAYER_HEALTH, true); + } + + #[test] + fn test_player_reward() { + let mut player = PlayerTrait::new(ID, PLAYER_NAME, TIME, MODE); + player.reward(10); + assert_eq!(player.gold, DEFAULT_PLAYER_GOLD + 10); + assert_eq!(player.score, 1); + } + + #[test] + fn test_player_heal() { + let mut player = PlayerTrait::new(ID, PLAYER_NAME, TIME, MODE); + player.gold = DEFAULT_POTION_COST; + player.heal(1); + assert_eq!(player.gold, 0); + assert_eq!(player.health, DEFAULT_PLAYER_HEALTH + DEFAULT_POTION_HEAL); + } +} + diff --git a/contracts/src/tests/setup.cairo b/contracts/src/tests/setup.cairo index 17af8c2..994e212 100644 --- a/contracts/src/tests/setup.cairo +++ b/contracts/src/tests/setup.cairo @@ -45,7 +45,7 @@ mod setup { fn spawn_game() -> (IWorldDispatcher, Systems, Context) { // [Setup] World let models = array![index::player::TEST_CLASS_HASH, index::dungeon::TEST_CLASS_HASH,]; - let world = spawn_test_world("dojo_starter_rpg", models); + let world = spawn_test_world(array!["dojo_starter_rpg"].span(), models.span()); // [Setup] Systems let actions_address = world