|
| 1 | +from collections import deque |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | +from mastermind.core.controllers.players import PlayerRole |
| 6 | +from mastermind.core.models.game import Game |
| 7 | +from mastermind.core.models.game_configuration import GameConfiguration |
| 8 | +from mastermind.core.models.game_mode import GameMode |
| 9 | +from mastermind.core.models.game_round import GameRound |
| 10 | +from mastermind.core.services.game_service import ( |
| 11 | + GameEndedException, |
| 12 | + GameNotStartedException, |
| 13 | + GameService, |
| 14 | +) |
| 15 | +from mastermind.core.services.gameboard_service import NoRedoAvailableException |
| 16 | + |
| 17 | + |
| 18 | +@pytest.fixture |
| 19 | +def game(): |
| 20 | + return Game( |
| 21 | + game_configuration=GameConfiguration( |
| 22 | + NUMBER_OF_COLORS=3, |
| 23 | + NUMBER_OF_DOTS=4, |
| 24 | + ATTEMPTS_ALLOWED=5, |
| 25 | + GAME_MODE=GameMode.PVP, |
| 26 | + ) |
| 27 | + ) |
| 28 | + |
| 29 | + |
| 30 | +@pytest.fixture |
| 31 | +def game_service(game: Game): |
| 32 | + return GameService(game) |
| 33 | + |
| 34 | + |
| 35 | +def test_add_round(game_service: GameService, game: Game): |
| 36 | + game_service.add_round((1, 2, 3, 4), (1, 0)) |
| 37 | + assert game.game_board.game_rounds == deque( |
| 38 | + [GameRound(GUESS=(1, 2, 3, 4), FEEDBACK=(1, 0))] |
| 39 | + ) |
| 40 | + |
| 41 | + |
| 42 | +def test_undo(game_service: GameService, game: Game): |
| 43 | + game_service.add_round((1, 2, 3, 4), (1, 0)) |
| 44 | + game_service.undo() |
| 45 | + assert game.game_board.game_rounds == deque([]) |
| 46 | + |
| 47 | + |
| 48 | +def test_add_round_with_redo(game_service: GameService, game: Game): |
| 49 | + game_service.add_round((1, 2, 3, 4), (1, 0)) |
| 50 | + game_service.undo() |
| 51 | + game_service.redo() |
| 52 | + assert game.game_board.game_rounds == deque( |
| 53 | + [GameRound(GUESS=(1, 2, 3, 4), FEEDBACK=(1, 0))] |
| 54 | + ) |
| 55 | + |
| 56 | + |
| 57 | +def test_add_round_after_end(game_service: GameService, game: Game): |
| 58 | + game.game_state.game_started = True |
| 59 | + game.game_state.winner = PlayerRole.CODE_SETTER |
| 60 | + with pytest.raises(GameEndedException): |
| 61 | + game_service.add_round((1, 2, 3, 4), (1, 0)) |
| 62 | + |
| 63 | + |
| 64 | +def test_undo_after_end(game_service: GameService, game: Game): |
| 65 | + game.game_state.game_started = True |
| 66 | + game.game_state.winner = PlayerRole.CODE_SETTER |
| 67 | + with pytest.raises(GameEndedException): |
| 68 | + game_service.undo() |
| 69 | + |
| 70 | + |
| 71 | +def test_undo_before_start(game_service: GameService, game: Game): |
| 72 | + with pytest.raises(GameNotStartedException): |
| 73 | + game_service.undo() |
| 74 | + |
| 75 | + |
| 76 | +def test_redo_before_start(game_service: GameService, game: Game): |
| 77 | + with pytest.raises(GameNotStartedException): |
| 78 | + game_service.redo() |
| 79 | + |
| 80 | + |
| 81 | +def test_redo_without_undo(game_service: GameService, game: Game): |
| 82 | + game_service.add_round((1, 2, 3, 4), (1, 0)) |
| 83 | + with pytest.raises(NoRedoAvailableException): |
| 84 | + game_service.redo() |
0 commit comments