Skip to content

Commit

Permalink
✅ Fix and add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bal7hazar committed Aug 21, 2024
1 parent c9f35c3 commit dc2e581
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 1 deletion.
45 changes: 45 additions & 0 deletions contracts/src/models/dungeon.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

70 changes: 70 additions & 0 deletions contracts/src/models/player.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

2 changes: 1 addition & 1 deletion contracts/src/tests/setup.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit dc2e581

Please sign in to comment.