Add comprehensive tests for Player model functionality#195
Add comprehensive tests for Player model functionality#195Birdmannn merged 8 commits intoPrometheus-A:v1from
Conversation
|
Ready for review @Birdmannn |
| fn PLAYER_1() -> ContractAddress { | ||
| starknet::contract_address_const::<'PLAYER_1'>() | ||
| } | ||
|
|
||
| fn PLAYER_2() -> ContractAddress { | ||
| starknet::contract_address_const::<'PLAYER_2'>() | ||
| } | ||
|
|
||
| fn PLAYER_3() -> ContractAddress { | ||
| starknet::contract_address_const::<'PLAYER_3'>() | ||
| } | ||
|
|
||
| fn PLAYER_4() -> ContractAddress { | ||
| starknet::contract_address_const::<'PLAYER_4'>() | ||
| } | ||
|
|
||
| fn mock_poker_game(ref world: WorldStorage) { | ||
| let game = Game { | ||
| id: 1, | ||
| in_progress: true, | ||
| has_ended: false, | ||
| current_round: 1, | ||
| round_in_progress: true, | ||
| current_player_count: 2, | ||
| players: array![PLAYER_1(), PLAYER_2(), PLAYER_3()], |
There was a problem hiding this comment.
This code is also in test_actions.cairo. make all functions public there, then import from there please
| deck: array![], | ||
| next_player: Option::Some(PLAYER_1()), | ||
| community_cards: array![], | ||
| pots: array![0], | ||
| current_bet: 0, | ||
| params: get_default_game_params(), | ||
| reshuffled: 0, | ||
| should_end: false, | ||
| deck_root: 0, | ||
| dealt_cards_root: 0, | ||
| nonce: 0, | ||
| community_dealing: false, | ||
| showdown: false, | ||
| round_count: 0, | ||
| highest_staker: Option::None, | ||
| previous_offset: 0, | ||
| }; | ||
|
|
||
| let player_1 = Player { | ||
| id: PLAYER_1(), | ||
| alias: 'dub_zn', | ||
| chips: 2000, | ||
| current_bet: 0, | ||
| total_rounds: 1, | ||
| locked: (true, 1), | ||
| is_dealer: false, | ||
| in_round: true, | ||
| out: (0, 0), | ||
| pub_key: 0x1, | ||
| locked_chips: 0, | ||
| is_blacklisted: false, | ||
| eligible_pots: 1, |
There was a problem hiding this comment.
This too.. and it goes down
| }; | ||
|
|
||
| let player_2 = Player { | ||
| id: PLAYER_2(), | ||
| alias: 'Birdmannn', | ||
| chips: 5000, | ||
| current_bet: 0, | ||
| total_rounds: 1, | ||
| locked: (true, 2), | ||
| is_dealer: false, | ||
| in_round: true, | ||
| out: (0, 0), | ||
| pub_key: 0x2, | ||
| locked_chips: 0, | ||
| is_blacklisted: false, | ||
| eligible_pots: 1, | ||
| }; | ||
|
|
||
| let player_3 = Player { | ||
| id: PLAYER_3(), | ||
| alias: 'chiscookeke11', | ||
| chips: 5000, | ||
| current_bet: 0, | ||
| total_rounds: 1, | ||
| locked: (false, 1), | ||
| is_dealer: false, | ||
| in_round: true, | ||
| out: (0, 0), |
There was a problem hiding this comment.
this too.. it still goes down
| pub_key: 0x3, | ||
| locked_chips: 0, | ||
| is_blacklisted: false, | ||
| eligible_pots: 1, | ||
| }; | ||
|
|
||
| world.write_model(@game); | ||
| world.write_models(array![@player_1, @player_2, @player_3].span()); | ||
| } |
There was a problem hiding this comment.
Yes, I think for the first part of your refactor, it'll end here 💯
| world.write_models(array![@player_1, @player_2, @player_3].span()); | ||
| } | ||
|
|
||
| fn mock_allowable_game(ref world: WorldStorage) { |
There was a problem hiding this comment.
from here Line167 to Line322, all new mocks would be to refactor the original mock function to take in parameters that target specific fields, and reuse this function.
SO basically all functions mocking the player state must be merged into one player function that takes in the specific parameters you wish to mock.
For all functions mocking the game state, then you must refactor the original function in test_actions.cairo to take in the specific mock parameters and adjust the tests there to pass.
Please, I hope you understand.
| }; | ||
|
|
||
| world.write_model(@game); | ||
| } |
…ed readability and maintainability
|
all done ser @Birdmannn |
Just to ask.... you normally use copilot to assist you, right? @No-bodyq |
Yes ser, why? @Birdmannn |
@No-bodyq Your code is bloated. Too much lines of code for little matters. And it causes an overhead during review when I want to optimize those lines but you and copilot are not helping matters. I'll review this code one more time, and I'll show you something... |
|
Okay ser, let me know what I should change and I'll do it manually |
| false, | ||
| true, | ||
| (0, 0), | ||
| 0x3, | ||
| 0, | ||
| false, | ||
| 1, |
There was a problem hiding this comment.
For example, something like this is repeated. including Line87 and Line88. Don't bother about the public key, you never tested it. This is what I wanted to show you. In the mock_player function you created, what should enter the params are the variables that are actually tested and that they vary for each player
So your function would resemble something like this
fn mock_player(id: ContractAddress, alias: felt252, chips: u256, out: (bool, u16)) -> Player {
let mut player: Player = Default::default();
player.id = id;
player.chips = chips;
player.alias = alias;
player.out = out;
player
}Implement this type of stuff too when mocking the game.
You see how neat and easy to read that is? That's how it should probably look... I didn't say that is the exact function... I'm not sure if I omitted some fields or parameters
| mock_poker_game_flex( | ||
| ref world, | ||
| 2, | ||
| true, | ||
| false, | ||
| 1, | ||
| false, | ||
| 2, | ||
| array![PLAYER_1(), PLAYER_2(), PLAYER_3()], | ||
| array![], | ||
| Option::Some(PLAYER_1()), | ||
| array![], | ||
| array![0], | ||
| 0, | ||
| get_default_game_params(), | ||
| 0, | ||
| false, | ||
| 0, | ||
| 0, | ||
| 0, | ||
| false, | ||
| false, | ||
| 0, | ||
| Option::None, | ||
| 0, | ||
| array![player_1, player_2, player_3], | ||
| ); | ||
| } |
There was a problem hiding this comment.
do the same with this function too. It takes in too many default fields... and yes, the pots might not be passed in as a parameter, you can just set the game.pots = array![0] since that is always the case for the pots, and the game.pots must be set like this inside the function because Default::default() makes it an empty array which is not a valid pot.
|
|
||
|
|
||
| #[test] | ||
| fn test_exit_with_out_true_succeeds() { |
| } | ||
|
|
||
| #[test] | ||
| fn test_exit_with_out_false_succeeds() { |
| let (mut world, _) = deploy_contracts(array![CoreContract::Actions]); | ||
| mock_poker_game_flex( | ||
| ref world, | ||
| 3, |
There was a problem hiding this comment.
by the way, if the id is not useful in mocking, you can default it to 1 in the mock function, and remove it as a parameter in the function's signature
|
I think we finally have it ser @Birdmannn |
|
Trés bien, Mister Asher @No-bodyq. Thanks for keeping up with this kind of review, and yes, thank you for your contribution.🫡. Ohayo 🧘🏻♂️ |
|
Thank you ser @Birdmannn |
Description
This PR introduces a comprehensive suite of unit tests for the playerImplementation
Related Issues
closes #98
Changes Made
mock_poker_gamefor simulating an active game statemock_allowable_gamefor simulating a joinable game statetest_exit_with_out_true_succeedstest_exit_with_out_false_succeedstest_exit_without_lock_failstest_exit_with_splitted_showdown_returns_locked_chipstest_exit_with_zero_player_count_failstest_exit_with_invalid_player_failstest_enter_succeeds_new_playerHow to Test
Run the tests using dojo:
sozo testChecklist