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

FlysonBot / Mastermind / 12171198948

05 Dec 2024 12:58AM UTC coverage: 97.438% (+0.002%) from 97.436%
12171198948

push

github

FlysonBot
ci: add codacy coverage report beside coveralls

2472 of 2537 relevant lines covered (97.44%)

0.97 hits per line

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

97.78
/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 = [self._sample_game_data(game_mock)]
1✔
51

52
        with patch(
1✔
53
            "mastermind.main.game_controller.userdata",
54
            new=mock_user_data_manager,
55
        ):
56
            GameController.resume_game(0)
1✔
57

58
        self.assertEqual(mock_user_data_manager.saved_games, [])
1✔
59

60
    @patch("builtins.input", side_effect=["q"])
1✔
61
    def test_resume_game_update_saved(self, mock_input):
1✔
62
        """Test resuming a game and updating the saved game"""
63
        game = Game(6, 4, 10, "HvAI")
1✔
64
        game._state.game_started = True
1✔
65
        game._player_logic.initialize_players()
1✔
66

67
        mock_user_data_manager = create_autospec(UserDataManager, instance=True)
1✔
68
        mock_user_data_manager.saved_games = [self._sample_game_data(game)]
1✔
69

70
        with patch(
1✔
71
            "mastermind.main.game_controller.userdata", new=mock_user_data_manager
72
        ):
73
            with patch("sys.stdout", new=StringIO()):
1✔
74
                GameController.resume_game(0)
1✔
75

76
            self.assertEqual(len(mock_user_data_manager.saved_games), 1)
1✔
77
            self.assertIsNone(mock_user_data_manager.saved_games[0]["win_status"])
1✔
78

79
    def _sample_game_data(self, game):
1✔
80
        """Helper method to create sample game data"""
81
        return {
1✔
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
if __name__ == "__main__":
1✔
95
    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