Skip to content

Commit 0d411a9

Browse files
committed
refactor: split concrete menus into multiple files
1 parent beda5f8 commit 0d411a9

File tree

9 files changed

+187
-239
lines changed

9 files changed

+187
-239
lines changed

src/mastermind/ui/menu/__init__.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
from mastermind.ui.menu.concrete_menus import (
2-
GameHistoryMenu,
3-
MainMenu,
4-
NewGameMenu,
5-
ResumeGameMenu,
6-
)
1+
from mastermind.ui.menu.game_history_menu import GameHistoryMenu
2+
from mastermind.ui.menu.main_menu import MainMenu
3+
from mastermind.ui.menu.new_game_menu import NewGameMenu
4+
from mastermind.ui.menu.resume_game_menu import ResumeGameMenu
75

86
__all__ = [
97
"MainMenu",
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
from typing import Optional
2+
3+
import pandas as pd
4+
5+
from mastermind.main.game_history import GameHistoryManager
6+
from mastermind.ui.menu.data_menu import DataDisplayMenu
7+
from mastermind.utils.render_dataframe import render_dataframe
8+
9+
10+
class GameHistoryMenu(DataDisplayMenu):
11+
"""
12+
The menu for displaying the game history.
13+
"""
14+
15+
name = "Game History"
16+
width = 25
17+
18+
def _fetch_data(self) -> Optional[pd.DataFrame]:
19+
"""
20+
Retrieves the game history data.
21+
"""
22+
return GameHistoryManager.retrieve_game_history()
23+
24+
def _render_data(self, data: pd.DataFrame) -> None:
25+
"""
26+
Renders the game history data.
27+
"""
28+
render_dataframe(data)
29+
30+
def _empty_message(self) -> str:
31+
"""
32+
Returns the message to display when there is no game history.
33+
"""
34+
return "No game history found."
35+
36+
def display(self) -> None:
37+
"""
38+
Displays the game history menu and waits for user input to continue.
39+
"""
40+
super().display()
41+
input("\nPress Enter to continue...")
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from mastermind.ui.menu.option_menu import OptionMenu
2+
3+
4+
class MainMenu(OptionMenu):
5+
"""
6+
The main menu of the application.
7+
"""
8+
9+
name = "Main Menu"
10+
menu = {
11+
"1": "Start New Game",
12+
"2": "Load Saved Game",
13+
"3": "Game History",
14+
"0": "Save and Exit",
15+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from mastermind.ui.menu.option_menu import OptionMenu
2+
3+
4+
class NewGameMenu(OptionMenu):
5+
"""
6+
The menu for starting a new game.
7+
"""
8+
9+
name = "New Game Menu"
10+
menu = {
11+
"1": "You vs Someone Else",
12+
"2": "You vs AI",
13+
"3": "AI vs You",
14+
"4": "Solve External Game",
15+
"0": "Return to Main Menu",
16+
}

src/mastermind/ui/menu/concrete_menus.py renamed to src/mastermind/ui/menu/resume_game_menu.py

Lines changed: 1 addition & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,74 +1,9 @@
11
from typing import Optional, Union
22

33
import pandas as pd
4-
54
from mastermind.main.game_history import GameHistoryManager
65
from mastermind.ui.menu.data_menu import DataDisplayMenu
7-
from mastermind.ui.menu.option_menu import OptionMenu
8-
from mastermind.utils import render_dataframe
9-
10-
11-
class MainMenu(OptionMenu):
12-
"""
13-
The main menu of the application.
14-
"""
15-
16-
name = "Main Menu"
17-
menu = {
18-
"1": "Start New Game",
19-
"2": "Load Saved Game",
20-
"3": "Game History",
21-
"0": "Save and Exit",
22-
}
23-
24-
25-
class NewGameMenu(OptionMenu):
26-
"""
27-
The menu for starting a new game.
28-
"""
29-
30-
name = "New Game Menu"
31-
menu = {
32-
"1": "You vs Someone Else",
33-
"2": "You vs AI",
34-
"3": "AI vs You",
35-
"4": "Solve External Game",
36-
"0": "Return to Main Menu",
37-
}
38-
39-
40-
class GameHistoryMenu(DataDisplayMenu):
41-
"""
42-
The menu for displaying the game history.
43-
"""
44-
45-
name = "Game History"
46-
width = 25
47-
48-
def _fetch_data(self) -> Optional[pd.DataFrame]:
49-
"""
50-
Retrieves the game history data.
51-
"""
52-
return GameHistoryManager.retrieve_game_history()
53-
54-
def _render_data(self, data: pd.DataFrame) -> None:
55-
"""
56-
Renders the game history data.
57-
"""
58-
render_dataframe(data)
59-
60-
def _empty_message(self) -> str:
61-
"""
62-
Returns the message to display when there is no game history.
63-
"""
64-
return "No game history found."
65-
66-
def display(self) -> None:
67-
"""
68-
Displays the game history menu and waits for user input to continue.
69-
"""
70-
super().display()
71-
input("\nPress Enter to continue...")
6+
from mastermind.utils.render_dataframe import render_dataframe
727

738

749
class ResumeGameMenu(DataDisplayMenu):

tests/ui/menu/test_concrete_menus.py

Lines changed: 0 additions & 167 deletions
This file was deleted.
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import unittest
2+
from unittest.mock import MagicMock, patch
3+
4+
import pandas as pd
5+
6+
from mastermind.ui.menu.game_history_menu import GameHistoryMenu
7+
8+
9+
class TestGameHistoryMenu(unittest.TestCase):
10+
def setUp(self):
11+
self.menu = GameHistoryMenu()
12+
13+
@patch("mastermind.main.game_history.GameHistoryManager.retrieve_game_history")
14+
def test_print_content_with_data(self, mock_retrieve_game_history):
15+
mock_retrieve_game_history.return_value = pd.DataFrame(
16+
{"Game": ["Game 1", "Game 2"]}
17+
)
18+
self.menu._render_data = MagicMock()
19+
with patch("builtins.print") as mock_print:
20+
self.menu._print_content()
21+
mock_retrieve_game_history.assert_called()
22+
self.menu._render_data.assert_called_with(
23+
mock_retrieve_game_history.return_value
24+
)
25+
mock_print.assert_not_called()
26+
27+
@patch("mastermind.main.game_history.GameHistoryManager.retrieve_game_history")
28+
@patch("builtins.print")
29+
def test_print_content_without_data(self, mock_print, mock_retrieve_game_history):
30+
mock_retrieve_game_history.return_value = None
31+
self.menu._print_content()
32+
mock_retrieve_game_history.assert_called()
33+
mock_print.assert_called_with("No game history found.")
34+
35+
def test_fetch_data(self):
36+
with patch(
37+
"mastermind.main.game_history.GameHistoryManager.retrieve_game_history"
38+
) as mock_retrieve_game_history:
39+
mock_retrieve_game_history.return_value = pd.DataFrame(
40+
{"Game": ["Game 1", "Game 2"]}
41+
)
42+
self.assertEqual(
43+
self.menu._fetch_data().to_dict(),
44+
pd.DataFrame({"Game": ["Game 1", "Game 2"]}).to_dict(),
45+
)
46+
47+
@patch("builtins.input", return_value="")
48+
def test_display(self, mock_input):
49+
with patch.object(GameHistoryMenu, "_print_content") as mock_print_content:
50+
self.menu.display()
51+
mock_print_content.assert_called()
52+
mock_input.assert_called_once_with("\nPress Enter to continue...")

0 commit comments

Comments
 (0)