def test_remove_player__with_only_one_player__no_callback_called(): turns = Turns() player = Mock() turns.add_player(player, turn_callback=player.callback) turns.start() player.callback.reset_mock() turns.remove_player(player) assert turns.next_player_action() is None player.callback.assert_not_called()
def test_remove_first_player_next_player_action_should_be_remaining_player(): """ Tests after removing the first of two players, next_player_action() should not return None """ turns = Turns() player1, player2 = Mock(), Mock() turns.add_player(player1, turn_callback=player1.callback) turns.add_player(player2, turn_callback=player2.callback) turns.start() turns.remove_player(player1) assert turns.next_player_action() is not None assert turns.next_player_action().player is player2 player2.callback.assert_called_once()
def test_removed_player_no_callback_called(): """ Tests remove_player """ turns = Turns() player1 = Mock() player2 = Mock() turns.add_player(player1, turn_callback=player1.callback) turns.add_player(player2, turn_callback=player2.callback) turns.start() turns.remove_player(player2) turns.perform_action(player1, PlayerAction.SHIFT_ACTION) turns.perform_action(player1, PlayerAction.MOVE_ACTION) player2.callback.assert_not_called()
def test_add_player__with_no_remaining_player__sets_next_player_action_and_calls_callback( ): turns = Turns() player = Player(0) turns.add_player(player) turns.start() turns.remove_player(player) player = Mock() turns.add_player(player, turn_callback=player.callback) assert turns.next_player_action() is not None assert turns.next_player_action().player is player player.callback.assert_called_once()
def test_remove_second_player_next_player_action_should_be_remaining_player(): """ Tests after removing the second of two players, next_player_action() should not return None """ turns = Turns() player1, player2 = Mock(), Mock() turns.add_player(player1, turn_callback=player1.callback) turns.add_player(player2, turn_callback=player2.callback) turns.start() turns.perform_action(player1, PlayerAction.SHIFT_ACTION) turns.perform_action(player1, PlayerAction.MOVE_ACTION) player1.callback.reset_mock() turns.remove_player(player2) assert turns.next_player_action() is not None assert turns.next_player_action().player is player1 player1.callback.assert_called_once()
def test_given_delay_with_two_players__when_player1_leaves_during_wait__player1_callback_is_not_called( ): turns = Turns(prepare_delay=timedelta(milliseconds=30)) player1, player2 = Mock(), Mock() turns.add_player(player1, turn_callback=player1.callback) turns.add_player(player2, turn_callback=player2.callback) turns.start() time.sleep(timedelta(milliseconds=50).total_seconds()) turns.perform_action(player1, PlayerAction.SHIFT_ACTION) time.sleep(timedelta(milliseconds=5).total_seconds()) player1.callback.reset_mock() turns.remove_player(player1) time.sleep(timedelta(milliseconds=50).total_seconds()) player1.callback.assert_not_called()
def test_given_delay_with_two_players__when_player1_leaves_during_wait__has_prepare_player2_state( ): turns = Turns(prepare_delay=timedelta(milliseconds=30)) player1, player2 = Mock(), Mock() turns.add_player(player1, turn_callback=player1.callback) turns.add_player(player2, turn_callback=player2.callback) turns.start() time.sleep(timedelta(milliseconds=50).total_seconds()) turns.perform_action(player1, PlayerAction.SHIFT_ACTION) time.sleep(timedelta(milliseconds=2).total_seconds()) turns.remove_player(player1) assert player2.callback.call_count == 1 assert turns.next_player_action().player == player2 assert turns.next_player_action().action == PlayerAction.PREPARE_SHIFT
def test_given_delay_with_two_players__when_player1_leaves_during_wait__another_wait_leads_to_player2_shift( ): turns = Turns(prepare_delay=timedelta(milliseconds=30)) player1, player2 = Player(1), Player(2) callback1, callback2 = Mock(), Mock() turns.add_player(player1, turn_callback=callback1) turns.add_player(player2, turn_callback=callback2) turns.start() time.sleep(timedelta(milliseconds=50).total_seconds()) turns.perform_action(player1, PlayerAction.SHIFT_ACTION) time.sleep(timedelta(milliseconds=5).total_seconds()) turns.remove_player(player1) callback2.reset_mock() time.sleep(timedelta(milliseconds=50).total_seconds()) assert callback2.call_count == 1 assert turns.next_player_action().player == player2 assert turns.next_player_action().action == PlayerAction.SHIFT_ACTION