def test_move_multiple_goose(self): player = 'Pippo' dice = [2, 2] start_pos = 10 sum_dice = sum(dice) end_pos = sum_dice + start_pos goose = [5, 9, 14, 18, 23, 27] goose_rule = GooseMoveRule(set(goose)) self.game.get_space_aliases.return_value = {0: 'Start'} renderer = MovePlayerOutputRenderer(self.game) res = renderer.render(player, dice, False, [ Move(self.player, start_pos, end_pos, BasicMoveRule()), Move(self.player, goose[2], goose[2] + sum_dice, goose_rule), Move(self.player, goose[3], goose[3] + sum_dice, goose_rule), ]) self.assertEqual( res, f'{player} rolls {dice[0]}, {dice[1]}. ' f'{player} moves from ' f'{start_pos} to {end_pos}, The Goose. ' f'{player} moves again and goes to ' f'{goose[2] + sum_dice}, The Goose. ' f'{player} moves again and goes to' f' {goose[3] + sum_dice}')
def test_goose(self): player = 'Pippo' rule = GooseMoveRule({5, 9, 14, 18, 23, 27}) current_pos = 5 step = 2 self.assertEqual(rule.apply(player, current_pos, current_pos, step), Move(player, current_pos, current_pos + step, rule))
def test_bridge(self): player = 'Pippo' rule = BridgeMoveRule(6, 12) current_pos = 6 step = 1 self.assertEqual(rule.apply(player, current_pos, current_pos, step), Move(player, current_pos, rule.bridge_end, rule))
def test_basic(self): player1 = 'Pippo' rule = BasicMoveRule() current_pos = 1 step = 4 self.assertEqual(rule.apply(player1, current_pos, current_pos, step), Move(player1, current_pos, current_pos + step, rule))
def test_bounce(self): player = 'Pippo' n_spaces = 63 rule = BounceMoveRule(n_spaces) current_pos = 65 step = 5 self.assertEqual(rule.apply(player, current_pos, current_pos, step), Move(player, current_pos, 61, rule))
def test_move_player_with_prank(self): current_pos = 8 new_pos = 10 player_2 = 'Pluto' self.repo.get_players_in_position.return_value = player_2 moves = [ Move(self.player_1, current_pos, new_pos, BasicMoveRule()), Move(player_2, new_pos, current_pos, PrankMoveRule(self.repo)) ] self.rule_engine.apply_rules.return_value = moves self.validator.validate.return_value = True dice = (1, 1) self.assertEqual(self.game.move_player(self.player_1, *dice), (False, moves)) calls = [(player_2, current_pos), (self.player_1, new_pos)] self.repo.update_position.has_calls(calls, any_order=True)
def test_game_over(self): new_pos = self.game._n_spaces moves = [Move(self.player_1, new_pos - 2, new_pos, BasicMoveRule())] self.rule_engine.apply_rules.return_value = moves self.validator.validate.return_value = True dice = (1, 1) self.game.move_player(self.player_1, *dice) self.assertRaises(GameOver, self.game.move_player, self.player_1, *dice) self.assertRaises(GameOver, self.game.add_player, self.player_1)
def test_win(self): new_pos = self.game._n_spaces moves = [Move(self.player_1, new_pos - 2, new_pos, BasicMoveRule())] self.rule_engine.apply_rules.return_value = moves self.validator.validate.return_value = True dice = (1, 1) win, _ = self.game.move_player(self.player_1, *dice) self.assertTrue(win) self.assertEqual(self.game.winner, self.player_1)
def test_move_and_bounce(self): dice = [3, 2] start_pos = 60 sum_dice = sum(dice) end_pos = sum_dice + start_pos final_pos = N_SPACES - (end_pos - N_SPACES) renderer = MovePlayerOutputRenderer(self.game) res = renderer.render(self.player, dice, False, [ Move(self.player, start_pos, end_pos, BasicMoveRule()), Move(self.player, end_pos, final_pos, BounceMoveRule(N_SPACES)) ]) self.assertEqual( res, f'{self.player} rolls {dice[0]}, {dice[1]}. ' f'{self.player} moves from {start_pos}' f' to {N_SPACES}. ' f'{self.player} bounces! ' f'{self.player} returns to {final_pos}')
def test_prank(self): player1, player2 = 'Pippo', 'Pluto' self.mock_repo.get_players_in_position.return_value = [player2] initial_pos = 8 current_pos = 10 step = 2 rule = PrankMoveRule(self.mock_repo) self.assertEqual(rule.apply(player1, initial_pos, current_pos, step), Move(player2, current_pos, initial_pos, rule)) self.mock_repo.get_players_in_position.assert_called_once_with( current_pos)
def test_move_player_no_dice(self, mock_roll_dice): old_pos = 8 new_pos = 10 called_rules = [Move(self.player_1, old_pos, new_pos, BasicMoveRule())] self.rule_engine.apply_rules.return_value = called_rules self.validator.validate.return_value = True self.assertEqual(self.game.move_player(self.player_1), (False, called_rules)) mock_roll_dice.assert_called_once_with(2, 6) self.repo.update_position.assert_called_once_with( self.player_1, new_pos)
def test_move_player(self): new_pos = 10 moves = [Move(self.player_1, 8, new_pos, BasicMoveRule())] self.rule_engine.apply_rules.return_value = moves self.validator.validate.return_value = True dice = (1, 1) self.assertEqual(self.game.move_player(self.player_1, *dice), (False, moves)) self.repo.update_position.assert_called_once_with( self.player_1, new_pos)
def test_move_simple_goose(self): dice = [1, 1] start_pos = 3 sum_dice = sum(dice) end_pos = sum_dice + start_pos goose = 5 self.game.get_space_aliases.return_value = {0: 'Start'} renderer = MovePlayerOutputRenderer(self.game) res = renderer.render(self.player, dice, False, [ Move(self.player, start_pos, end_pos, BasicMoveRule()), Move(self.player, end_pos, end_pos + sum_dice, GooseMoveRule({goose})) ]) self.assertEqual( res, f'{self.player} rolls {dice[0]}, {dice[1]}. ' f'{self.player} moves from ' f'{start_pos} to {end_pos}, The Goose. ' f'{self.player} moves again and goes ' f'to {goose + sum_dice}')
def test_move_bridge(self): dice = [1, 2] start_pos = 3 bridge_start = 6 bridge_end = 12 end__pos = sum(dice) + start_pos self.game.get_space_aliases.return_value = { 0: 'Start', bridge_start: 'The Bridge' } renderer = MovePlayerOutputRenderer(self.game) res = renderer.render(self.player, dice, False, [ Move(self.player, start_pos, end__pos, BasicMoveRule()), Move(self.player, bridge_start, bridge_end, BridgeMoveRule(bridge_start, bridge_end)) ]) self.assertEqual( res, f'{self.player} rolls {dice[0]}, {dice[1]}. ' f'{self.player} moves from ' f'{start_pos} to The Bridge. ' f'{self.player} jumps to {bridge_end}')
def test_move_dice_start(self): dice = [4, 2] start_pos = 0 end__pos = sum(dice) self.game.get_space_aliases.return_value = {0: 'Start'} renderer = MovePlayerOutputRenderer(self.game) res = renderer.render( self.player, dice, False, [Move(self.player, start_pos, end__pos, BasicMoveRule())]) self.assertEqual( res, f'{self.player} rolls {dice[0]}, {dice[1]}. ' f'{self.player} moves from Start to {end__pos}')
def test_move_win(self): dice = [1, 2] start_pos = 60 end__pos = sum(dice) + start_pos self.game.get_space_aliases.return_value = {0: 'Start'} renderer = MovePlayerOutputRenderer(self.game) res = renderer.render( self.player, dice, True, [Move(self.player, start_pos, end__pos, BasicMoveRule())]) self.assertEqual( res, f'{self.player} rolls {dice[0]}, {dice[1]}. ' f'{self.player} moves from {start_pos} ' f'to {end__pos}. ' f'{self.player} Wins!!')