Skip to content

Commit

Permalink
Add Tests for create_bot_player() Function with Successful and Existi…
Browse files Browse the repository at this point in the history
…ng Player Scenarios (#190)

* Updated game_actions.cairo

* Updated test_game.cairo
  • Loading branch information
Jagadeeshftw authored Jan 10, 2025
1 parent 356c5b0 commit a918ed8
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 2 deletions.
2 changes: 1 addition & 1 deletion onchain/src/systems/game_actions.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ pub mod GameActions {
player_address = contract_address_const::<'blue_bot'>();
},
PlayerColor::Red => {
username = 'red_bot ';
username = 'red_bot';
player_address = contract_address_const::<'red_bot'>();
}
};
Expand Down
61 changes: 60 additions & 1 deletion onchain/src/tests/test_game.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ mod tests {
};

// Models import
use starkludo::models::game::{Game, m_Game, GameCounter, m_GameCounter};
use starkludo::models::game::{Game, m_Game, GameCounter, m_GameCounter, PlayerColor};
use starkludo::models::player::{
Player, m_Player, AddressToUsername, UsernameToAddress, m_AddressToUsername,
m_UsernameToAddress
Expand Down Expand Up @@ -185,6 +185,65 @@ mod tests {
assert_eq!(address_to_username.username, username);
}

#[test]
fn test_create_bot_player_is_successful() {
let (_, game_action_system) = setup_world();

let blue_color = PlayerColor::Blue;
let green_color = PlayerColor::Green;
let red_color = PlayerColor::Red;
let yellow_color = PlayerColor::Yellow;

let created_blue_bot_player: Player = game_action_system.create_bot_player(blue_color);
let expected_blue_username = 'blue_bot';
let expected_blue_address = starknet::contract_address_const::<'blue_bot'>();
assert_eq!(created_blue_bot_player.username, expected_blue_username);
assert_eq!(created_blue_bot_player.owner, expected_blue_address);
assert_eq!(created_blue_bot_player.is_bot, true);

let created_green_bot_player: Player = game_action_system.create_bot_player(green_color);
let expected_green_username = 'green_bot';
let expected_green_address = starknet::contract_address_const::<'green_bot'>();
assert_eq!(created_green_bot_player.username, expected_green_username);
assert_eq!(created_green_bot_player.owner, expected_green_address);
assert_eq!(created_green_bot_player.is_bot, true);

let created_red_bot_player: Player = game_action_system.create_bot_player(red_color);
let expected_red_username = 'red_bot';
let expected_red_address = starknet::contract_address_const::<'red_bot'>();
assert_eq!(created_red_bot_player.username, expected_red_username);
assert_eq!(created_red_bot_player.owner, expected_red_address);
assert_eq!(created_red_bot_player.is_bot, true);

let created_yellow_bot_player: Player = game_action_system.create_bot_player(yellow_color);
let expected_yellow_username = 'yellow_bot';
let expected_yellow_address = starknet::contract_address_const::<'yellow_bot'>();
assert_eq!(created_yellow_bot_player.username, expected_yellow_username);
assert_eq!(created_yellow_bot_player.owner, expected_yellow_address);
assert_eq!(created_yellow_bot_player.is_bot, true);
}

#[test]
fn test_create_existing_bot_player() {
let (_, game_action_system) = setup_world();

let blue_color = PlayerColor::Blue;
let created_blue_bot_player: Player = game_action_system.create_bot_player(blue_color);

let expected_blue_username = 'blue_bot';
let expected_blue_address = starknet::contract_address_const::<'blue_bot'>();
assert_eq!(created_blue_bot_player.username, expected_blue_username);
assert_eq!(created_blue_bot_player.owner, expected_blue_address);
assert_eq!(created_blue_bot_player.is_bot, true);

let existing_blue_bot_player: Player = game_action_system.create_bot_player(blue_color);

assert_eq!(existing_blue_bot_player.username, expected_blue_username);
assert_eq!(existing_blue_bot_player.owner, expected_blue_address);
assert_eq!(existing_blue_bot_player.is_bot, true);
assert_eq!(created_blue_bot_player.owner, existing_blue_bot_player.owner);
}

#[test]
fn test_get_username_from_address() {
let ndef = namespace_def();
Expand Down

0 comments on commit a918ed8

Please sign in to comment.