|
| 1 | +import unittest |
| 2 | +from unittest.mock import patch |
| 3 | + |
| 4 | + |
| 5 | +from mastermind.ui.menu.resume_game_menu import ResumeGameMenu, _is_option_valid |
| 6 | + |
| 7 | + |
| 8 | +class TestResumeGameMenu(unittest.TestCase): |
| 9 | + """Unit tests for the ResumeGameMenu class""" |
| 10 | + |
| 11 | + def setUp(self): |
| 12 | + """Set up the test environment""" |
| 13 | + self.example_continuable_games = [ |
| 14 | + { |
| 15 | + "game_mode": "HvH", |
| 16 | + "number_of_dots": 4, |
| 17 | + "number_of_colors": 6, |
| 18 | + "amount_attempted": 8, |
| 19 | + "amount_allowed": 10, |
| 20 | + "win_status": None, |
| 21 | + "guesses": ["1234", "4561"], |
| 22 | + "feedback": [(4, 0), (3, 1)], |
| 23 | + }, |
| 24 | + { |
| 25 | + "game_mode": "HvAI", |
| 26 | + "number_of_dots": 5, |
| 27 | + "number_of_colors": 8, |
| 28 | + "amount_attempted": 10, |
| 29 | + "amount_allowed": 12, |
| 30 | + "win_status": None, |
| 31 | + "guesses": ["1234", "4561", "2312"], |
| 32 | + "feedback": [(4, 0), (3, 1), (2, 2)], |
| 33 | + }, |
| 34 | + ] |
| 35 | + |
| 36 | + def test_init(self): |
| 37 | + """Test the initialization of the ResumeGameMenu""" |
| 38 | + menu = ResumeGameMenu() |
| 39 | + self.assertEqual(menu.name, "Resume Game") |
| 40 | + self.assertEqual(menu.width, 27) |
| 41 | + self.assertEqual(menu._empty_message, "No continuable game found.") |
| 42 | + self.assertEqual(menu.menu, {"0": "Return to Main Menu"}) |
| 43 | + |
| 44 | + @patch("mastermind.main.game_history.list_continuable_games") |
| 45 | + def test_fetch_data_with_games(self, mock_continuable_games): |
| 46 | + """Test the _fetch_data method with continuable games""" |
| 47 | + mock_continuable_games.return_value = self.example_continuable_games |
| 48 | + menu = ResumeGameMenu() |
| 49 | + data = menu._fetch_data() |
| 50 | + self.assertEqual(len(data), 2) |
| 51 | + self.assertEqual(menu.menu_length, 2) |
| 52 | + |
| 53 | + @patch("mastermind.main.game_history.GameHistoryManager.retrieve_continuable_games") |
| 54 | + def test_fetch_data_without_games(self, mock_retrieve_games): |
| 55 | + """Test the _fetch_data method with no continuable games""" |
| 56 | + mock_retrieve_games.return_value = None |
| 57 | + menu = ResumeGameMenu() |
| 58 | + data = menu._fetch_data() |
| 59 | + self.assertIsNone(data) |
| 60 | + self.assertEqual(menu.menu_length, 0) |
| 61 | + |
| 62 | + @patch("builtins.print") |
| 63 | + @patch("mastermind.main.game_history.list_continuable_games") |
| 64 | + def test_render_data(self, mock_continuable_games, mock_print): |
| 65 | + """Test the _render_data method""" |
| 66 | + mock_continuable_games.return_value = self.example_continuable_games |
| 67 | + menu = ResumeGameMenu() |
| 68 | + data = menu._fetch_data() |
| 69 | + menu._render_data(data) |
| 70 | + |
| 71 | + mock_print.assert_any_call(" Mode Dimension Attempts") |
| 72 | + mock_print.assert_any_call("(1) HvH 6x4 8/10 ") |
| 73 | + mock_print.assert_any_call("(2) HvAI 8x5 10/12 ") |
| 74 | + mock_print.assert_any_call("\n(0) Return to Main Menu") |
| 75 | + |
| 76 | + def test_process_option_return(self): # sourcery skip: class-extract-method |
| 77 | + """Test the _process_option method when the user selects 'Return to Main Menu'""" |
| 78 | + menu = ResumeGameMenu() |
| 79 | + result = menu._process_option("0") |
| 80 | + self.assertEqual(result, "return") |
| 81 | + |
| 82 | + def test_process_option_select_game(self): |
| 83 | + """Test the _process_option method when the user selects a game to resume""" |
| 84 | + menu = ResumeGameMenu() |
| 85 | + result = menu._process_option("2") |
| 86 | + self.assertEqual(result, 1) |
| 87 | + |
| 88 | + def test_is_option_valid_valid_input(self): |
| 89 | + """Test the _is_option_valid function with valid input""" |
| 90 | + self.assertTrue(_is_option_valid("2", 3)) |
| 91 | + |
| 92 | + def test_is_option_valid_invalid_input(self): |
| 93 | + """Test the _is_option_valid function with invalid input""" |
| 94 | + self.assertFalse(_is_option_valid("a", 3)) |
| 95 | + self.assertFalse(_is_option_valid("4", 3)) |
| 96 | + |
| 97 | + @patch("builtins.input", side_effect=[""]) |
| 98 | + @patch("mastermind.main.game_history.GameHistoryManager.retrieve_continuable_games") |
| 99 | + @patch("builtins.print") |
| 100 | + def test_get_option_without_games( |
| 101 | + self, mock_print, mock_retrieve_games, mock_input |
| 102 | + ): |
| 103 | + """Test the get_option method with no continuable games""" |
| 104 | + mock_retrieve_games.return_value = None |
| 105 | + menu = ResumeGameMenu() |
| 106 | + result = menu.get_option() |
| 107 | + self.assertEqual(result, 0) |
| 108 | + self.assertEqual(mock_print.call_count, 3) |
| 109 | + mock_print.assert_any_call('No continuable game found.') |
| 110 | + |
| 111 | + @patch("builtins.input", side_effect=["9", "a", "2"]) |
| 112 | + @patch("mastermind.main.game_history.list_continuable_games") |
| 113 | + @patch("mastermind.ui.menu.resume_game_menu.ResumeGameMenu._render_data") |
| 114 | + def test_get_option_with_games( |
| 115 | + self, mock_render_data, mock_continuable_games, mock_input |
| 116 | + ): |
| 117 | + """Test the get_option method with continuable games""" |
| 118 | + mock_continuable_games.return_value = self.example_continuable_games |
| 119 | + menu = ResumeGameMenu() |
| 120 | + result = menu.get_option() |
| 121 | + self.assertEqual(result, 2) |
| 122 | + self.assertEqual(mock_input.call_count, 3) |
| 123 | + self.assertEqual(mock_render_data.call_count, 3) |
| 124 | + |
| 125 | + |
| 126 | +if __name__ == "__main__": |
| 127 | + unittest.main() |
0 commit comments