Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add custome errors in test_game #97

Merged
merged 9 commits into from
Oct 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 15 additions & 7 deletions onchain/src/systems/player_actions.cairo
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
use starknet::{ContractAddress};
use starkludo::models::{player::{Player, PlayerTrait}};

pub mod Errors {
pub const ADDRESS_ZERO: felt252 = 'Cannot create from zero address';
pub const USERNAME_TAKEN: felt252 = 'username already taken';
pub const USERNAME_NOT_FOUND: felt252 = 'player with username not found';
pub const USERNAME_EXIST: felt252 = 'username already exist';
pub const ONLY_OWNER_USERNAME: felt252 = 'only user can udpate username';
}

#[dojo::interface]
trait IPlayerActions {
// Create a new player
Expand All @@ -18,6 +26,7 @@ trait IPlayerActions {

#[dojo::contract]
mod PlayerActions {
use super::Errors;
use super::{IPlayerActions, Player, PlayerTrait};
use starknet::{ContractAddress, get_caller_address, contract_address_const};

Expand All @@ -27,14 +36,14 @@ mod PlayerActions {
let caller: ContractAddress = get_caller_address();
let zero_address: ContractAddress = contract_address_const::<0x0>();

assert(caller != zero_address, 'Cannot create from zero address');
assert(caller != zero_address, Errors::ADDRESS_ZERO);

let new_player: Player = PlayerTrait::new(username, caller);

// Ensure player username is unique
let mut existing_player = get!(world, username, (Player));

assert(existing_player.owner == 0.try_into().unwrap(), 'username already taken');
assert(existing_player.owner == 0.try_into().unwrap(), Errors::USERNAME_TAKEN);

set!(world, (new_player));
}
Expand All @@ -45,17 +54,16 @@ mod PlayerActions {
let mut player: Player = get!(world, username, (Player));

// Validate username
assert(player.owner != 0.try_into().unwrap(), 'player with username not found');
assert(player.owner != 0.try_into().unwrap(), Errors::USERNAME_NOT_FOUND);

player.owner
}


fn get_player_stats(
ref world: IWorldDispatcher, username: felt252
) -> (u256, u256, u256, u256) {
let player: Player = get!(world, username, (Player));
assert(player.owner != 0.try_into().unwrap(), 'player with username not found');
assert(player.owner != 0.try_into().unwrap(), Errors::USERNAME_NOT_FOUND);

let total_points = player.total_games_won;

Expand All @@ -73,11 +81,11 @@ mod PlayerActions {
let mut player: Player = get!(world, old_username, (Player));

// Only allow the player to update their own username
assert(player.owner == caller, 'only user can udpate username');
assert(player.owner == caller, Errors::ONLY_OWNER_USERNAME);

// Check if the new username is already taken
let mut existing_player = get!(world, new_username, (Player));
assert(existing_player.owner == 0.try_into().unwrap(), 'username already exist');
assert(existing_player.owner == 0.try_into().unwrap(), Errors::USERNAME_EXIST);

// Update the player's username
player.username = new_username;
Expand Down
21 changes: 14 additions & 7 deletions onchain/src/tests/test_game.cairo
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
pub mod Errors {
pub const WRONG_DICE_VALUE: felt252 = 'Wrong dice value';
pub const WRONG_DICE_NONCE: felt252 = 'Wrong dice nonce';
}

#[cfg(test)]
mod tests {
use super::Errors;
// Import world dispatcher
use dojo::world::{IWorldDispatcher, IWorldDispatcherTrait};
// Import test utils
Expand All @@ -17,6 +23,7 @@ mod tests {
// Import Starknet utils
use starknet::{testing, contract_address_const, get_caller_address, ContractAddress};


fn create_and_setup_game(
game_mode: GameMode,
number_of_players: u8,
Expand Down Expand Up @@ -122,20 +129,20 @@ mod tests {
#[test]
fn test_dice_new_roll() {
let mut dice = DiceTrait::new(DICE_FACE_COUNT, DICE_SEED);
assert(dice.roll() == 1, 'Wrong dice value');
assert(dice.roll() == 6, 'Wrong dice value');
assert(dice.roll() == 3, 'Wrong dice value');
assert(dice.roll() == 1, 'Wrong dice value');
assert(dice.roll() == 4, 'Wrong dice value');
assert(dice.roll() == 3, 'Wrong dice value');
assert(dice.roll() == 1, Errors::WRONG_DICE_VALUE);
assert(dice.roll() == 6, Errors::WRONG_DICE_VALUE);
assert(dice.roll() == 3, Errors::WRONG_DICE_VALUE);
assert(dice.roll() == 1, Errors::WRONG_DICE_VALUE);
assert(dice.roll() == 4, Errors::WRONG_DICE_VALUE);
assert(dice.roll() == 3, Errors::WRONG_DICE_VALUE);
}

#[test]
fn test_dice_new_roll_overflow() {
let mut dice = DiceTrait::new(DICE_FACE_COUNT, DICE_SEED);
dice.nonce = 0x800000000000011000000000000000000000000000000000000000000000000;
dice.roll();
assert(dice.nonce == 0, 'Wrong dice nonce');
assert(dice.nonce == 0, Errors::WRONG_DICE_NONCE);
}

#[test]
Expand Down
19 changes: 14 additions & 5 deletions onchain/src/tests/test_player.cairo
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
pub mod Errors {
pub const INCORRECT_GAME_PLAYED: felt252 = 'Incorrect games played';
pub const INCORRECT_GAME_WON: felt252 = 'Incorrect games won';
pub const INCORRECT_TOTAL_POINTS: felt252 = 'Incorrect total points';
pub const INCORRECT_LEADBOARD_POSITION: felt252 = 'Incorrect leaderboard position';
}

#[cfg(test)]
mod tests {
use super::Errors;
// import world dispatcher
use dojo::world::{IWorldDispatcher, IWorldDispatcherTrait};
// import test utils
Expand All @@ -13,6 +21,7 @@ mod tests {
};
use starknet::{testing, contract_address_const, get_caller_address, ContractAddress};


fn create_and_setup_player(
username: felt252
) -> (IPlayerActionsDispatcher, IWorldDispatcher, ContractAddress) {
Expand Down Expand Up @@ -99,11 +108,11 @@ mod tests {
// Get the player's stats
let (games_played, games_won, total_points, leaderboard_position) = player_actions
.get_player_stats(username);

assert_eq!(games_played, 10, "Incorrect games played");
assert_eq!(games_won, 5, "Incorrect games won");
assert_eq!(total_points, 5, "Incorrect total points");
assert_eq!(leaderboard_position, 0, "Incorrect leaderboard position");
assert_eq!(games_played, 10, "{}", Errors::INCORRECT_GAME_PLAYED);
assert_eq!(games_won, 5, "{}", Errors::INCORRECT_GAME_WON);
assert_eq!(total_points, 5, "{}", Errors::INCORRECT_TOTAL_POINTS);
assert_eq!(leaderboard_position, 0, "{}", Errors::INCORRECT_LEADBOARD_POSITION);
}


Expand Down
Loading