Skip to content

Commit

Permalink
fix: failing test
Browse files Browse the repository at this point in the history
  • Loading branch information
0xibs committed Feb 8, 2025
1 parent 6c69e72 commit 168eaee
Showing 1 changed file with 22 additions and 12 deletions.
34 changes: 22 additions & 12 deletions onchain/src/tests/test_game.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -734,7 +734,7 @@ mod tests {
#[test]
#[should_panic(expected: ('GAME NOT INITIALISED', 'ENTRYPOINT_FAILED'))]
fn test_join_game_not_initialized() {
let ( mut world, game_action_system) = setup_world();
let (mut world, game_action_system) = setup_world();
let caller = contract_address_const::<'test_gamer'>();
let username = 'gamer';
let no_of_players: u8 = 3;
Expand Down Expand Up @@ -783,7 +783,7 @@ mod tests {

let game_id = game_action_system
.create_new_game(GameMode::MultiPlayer, PlayerColor::Red, 2);

game_action_system.join(PlayerColor::Red, game_id);
}

Expand All @@ -806,8 +806,13 @@ mod tests {
let (mut world, game_action_system) = setup_world();
let caller = contract_address_const::<'first_gamer'>();
let username = 'gamer';
let second_player_address = contract_address_const::<'second_player'>();
let second_player_username = 'second_player';
let no_of_players: u8 = 3;

testing::set_contract_address(second_player_address);
game_action_system.create_new_player(second_player_username, false);

testing::set_contract_address(caller);
game_action_system.create_new_player(username, false);

Expand All @@ -821,12 +826,12 @@ mod tests {
world.write_model(@game);

// Join game with a different color than used in creating game
testing::set_contract_address(caller);
testing::set_contract_address(second_player_address);
game_action_system.join(PlayerColor::Blue, game_id);

// Verify join was successful
let joined_game: Game = world.read_model(game_id);
assert(joined_game.player_blue == username, 'Player should be blue');
assert(joined_game.player_blue == second_player_username, 'Player should be blue');
}

#[test]
Expand Down Expand Up @@ -894,39 +899,44 @@ mod tests {
let (mut world, game_action_system) = setup_world();
let caller = contract_address_const::<'test_gamer'>();
let username = 'gamer';
let second_player_address = contract_address_const::<'second_player'>();
let second_player_username = 'second_player';
let given_players: u8 = 3;

testing::set_contract_address(second_player_address);
game_action_system.create_new_player(second_player_username, false);

testing::set_contract_address(caller);
game_action_system.create_new_player(username, false);

let game_id = game_action_system
.create_new_game(GameMode::MultiPlayer, PlayerColor::Red, given_players);

// Set number of players to unaccepted value
// Set number of players to unaccepted value
testing::set_contract_address(game_action_system.contract_address);
let mut game: Game = world.read_model(game_id);
game.status = GameStatus::Pending;
game.number_of_players = 1;
world.write_model(@game);

testing::set_contract_address(caller);
testing::set_contract_address(second_player_address);
game_action_system.join(PlayerColor::Yellow, game_id);
}

#[test]
fn test_three_player_game_incomplete() {
let (mut world, game_action_system) = setup_world();

let caller1 = contract_address_const::<'test_gamer1'>();
let username1 = 'gamer1';
let caller2 = contract_address_const::<'test_gamer2'>();
let username2 = 'gamer2';

testing::set_contract_address(caller1);
game_action_system.create_new_player(username1, false);
testing::set_contract_address(caller2);
game_action_system.create_new_player(username2, false);

// Create 3-player game
testing::set_contract_address(caller1);
let game_id = game_action_system
Expand All @@ -937,12 +947,12 @@ mod tests {
let mut game: Game = world.read_model(game_id);
game.status = GameStatus::Pending;
world.write_model(@game);

testing::set_contract_address(caller2);
game_action_system.join(PlayerColor::Blue, game_id);

let game: Game = world.read_model(game_id);

// Assert game is still pending with only two players
assert(game.status == GameStatus::Pending, 'Game should be pending');
assert(game.player_red == username1, 'Red player should be set');
Expand Down

0 comments on commit 168eaee

Please sign in to comment.