• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

FlysonBot / Mastermind / 12154602358

04 Dec 2024 06:56AM UTC coverage: 97.093% (+1.0%) from 96.123%
12154602358

push

github

FlysonBot
test: add test for main package

212 of 216 new or added lines in 4 files covered. (98.15%)

2 existing lines in 2 files now uncovered.

2472 of 2546 relevant lines covered (97.09%)

0.97 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

97.67
/tests/main/test_game_controller.py
1
import unittest
1✔
2
from io import StringIO
1✔
3
from unittest.mock import call, create_autospec, patch
1✔
4

5
from mastermind.game.game import Game
1✔
6
from mastermind.main.game_controller import GameController
1✔
7
from mastermind.storage.user_data import UserDataManager
1✔
8

9

10
class TestGameController(unittest.TestCase):
1✔
11
    """Unit tests for the GameController class"""
12

13
    @patch("builtins.input", side_effect=["6", "4", "10", "q"])
1✔
14
    @patch("mastermind.main.game_controller.GameHistoryManager.save_game")
1✔
15
    def test_start_new_game_classic(self, mock_save_game, mock_input):
1✔
16
        """Test starting a new Classic game"""
17
        with patch("sys.stdout", new=StringIO()):
1✔
18
            GameController.start_new_game("HvAI")
1✔
19

20
        mock_input.assert_has_calls(
1✔
21
            [
22
                call("\nEnter the number of colors (2-10): "),
23
                call("\nEnter the number of dots (2-10): "),
24
                call("\nEnter the maximum number of attempts: "),
25
            ]
26
        )
27
        mock_save_game.assert_called_once()
1✔
28

29
    @patch("builtins.input", side_effect=["8", "5", "15", "q"])
1✔
30
    @patch("mastermind.main.game_controller.GameHistoryManager.save_game")
1✔
31
    def test_start_new_game_custom(self, mock_save_game, mock_input):
1✔
32
        """Test starting a new Custom game"""
33
        with patch("sys.stdout", new=StringIO()):
1✔
34
            GameController.start_new_game("HvAI")
1✔
35

36
        mock_input.assert_has_calls(
1✔
37
            [
38
                call("\nEnter the number of colors (2-10): "),
39
                call("\nEnter the number of dots (2-10): "),
40
                call("\nEnter the maximum number of attempts: "),
41
            ]
42
        )
43
        mock_save_game.assert_called_once()
1✔
44

45
    def test_resume_game_discard(self):
1✔
46
        """Test resuming a game and discarding it"""
47
        game_mock = create_autospec(Game, instance=True)
1✔
48
        game_mock.resume_game.return_value = "d"
1✔
49
        mock_user_data_manager = create_autospec(UserDataManager, instance=True)
1✔
50
        mock_user_data_manager.saved_games = [
1✔
51
            {
52
                "game": game_mock,
53
                "game_mode": "HvH",
54
                "number_of_dots": 4,
55
                "number_of_colors": 6,
56
                "amount_attempted": 8,
57
                "amount_allowed": 10,
58
                "win_status": None,
59
                "guesses": ["1234", "4561", "2312"],
60
                "feedback": [(4, 0), (3, 1), (2, 2)],
61
            }
62
        ]
63

64
        with patch(
1✔
65
            "mastermind.main.game_controller.userdata",
66
            new=mock_user_data_manager,
67
        ):
68
            GameController.resume_game(0)
1✔
69

70
        self.assertEqual(mock_user_data_manager.saved_games, [])
1✔
71

72
    @patch("builtins.input", side_effect=["q"])
1✔
73
    def test_resume_game_update_saved(self, mock_input):
1✔
74
        """Test resuming a game and updating the saved game"""
75
        game = Game(6, 4, 10, "HvAI")
1✔
76
        game._state.game_started = True
1✔
77
        game._player_logic.initialize_players()
1✔
78

79
        mock_user_data_manager = create_autospec(UserDataManager, instance=True)
1✔
80
        mock_user_data_manager.saved_games = [
1✔
81
            {
82
                "game": game,
83
                "game_mode": "HvAI",
84
                "number_of_dots": 4,
85
                "number_of_colors": 6,
86
                "amount_attempted": 3,
87
                "amount_allowed": 10,
88
                "win_status": None,
89
                "guesses": ["1234", "4561", "2312"],
90
                "feedback": [(4, 0), (3, 1), (2, 2)],
91
            }
92
        ]
93

94
        with patch(
1✔
95
            "mastermind.main.game_controller.userdata", new=mock_user_data_manager
96
        ):
97
            with patch("sys.stdout", new=StringIO()):
1✔
98
                GameController.resume_game(0)
1✔
99

100
            self.assertEqual(len(mock_user_data_manager.saved_games), 1)
1✔
101
            self.assertIsNone(mock_user_data_manager.saved_games[0]["win_status"])
1✔
102

103

104
if __name__ == "__main__":
1✔
NEW
105
    unittest.main()
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc