From caef8dc9b21bfed65b83a8a8f6da1f32712823df Mon Sep 17 00:00:00 2001 From: Yohann D'ANELLO Date: Wed, 11 Nov 2020 22:45:15 +0100 Subject: [PATCH] Cover settings --- dungeonbattle/game.py | 5 +++-- dungeonbattle/menus.py | 6 +++--- dungeonbattle/tests/game_test.py | 34 ++++++++++++++++++++++++++++++++ 3 files changed, 40 insertions(+), 5 deletions(-) diff --git a/dungeonbattle/game.py b/dungeonbattle/game.py index 138c161..14e3206 100644 --- a/dungeonbattle/game.py +++ b/dungeonbattle/game.py @@ -1,5 +1,5 @@ from random import randint -from typing import Any +from typing import Any, Optional from .entities.player import Player from .enums import GameMode, KeyValues @@ -56,7 +56,8 @@ class Game: self.handle_key_pressed( *KeyValues.translate_key(key, self.settings)) - def handle_key_pressed(self, key: KeyValues, raw_key: str = '') -> None: + def handle_key_pressed(self, key: Optional[KeyValues], raw_key: str = '')\ + -> None: """ Indicates what should be done when the given key is pressed, according to the current game state. diff --git a/dungeonbattle/menus.py b/dungeonbattle/menus.py index fb22aef..3713900 100644 --- a/dungeonbattle/menus.py +++ b/dungeonbattle/menus.py @@ -1,6 +1,6 @@ import sys from enum import Enum -from typing import Any +from typing import Any, Optional from .enums import GameMode, KeyValues from .settings import Settings @@ -59,8 +59,8 @@ class SettingsMenu(Menu): s = settings.dumps_to_string() self.values = s[6:-2].replace("\"", "").split(",\n ") - def handle_key_pressed(self, key: KeyValues, raw_key: str, game: Any) \ - -> None: + def handle_key_pressed(self, key: Optional[KeyValues], raw_key: str, + game: Any) -> None: """ For now, in the settings mode, we can only go backwards. """ diff --git a/dungeonbattle/tests/game_test.py b/dungeonbattle/tests/game_test.py index ec6ffd6..0894060 100644 --- a/dungeonbattle/tests/game_test.py +++ b/dungeonbattle/tests/game_test.py @@ -138,6 +138,40 @@ class TestGame(unittest.TestCase): self.game.handle_key_pressed(KeyValues.SPACE) self.assertEqual(self.game.state, GameMode.MAINMENU) + def test_settings_menu(self) -> None: + """ + Ensure that the settings menu is working properly. + """ + self.game.settings = Settings() + + # Open settings menu + self.game.handle_key_pressed(KeyValues.DOWN) + self.game.handle_key_pressed(KeyValues.ENTER) + self.assertEqual(self.game.state, GameMode.SETTINGS) + + # Define the "move up" key to 'w' + self.assertFalse(self.game.settings_menu.waiting_for_key) + self.game.handle_key_pressed(KeyValues.ENTER) + self.assertTrue(self.game.settings_menu.waiting_for_key) + self.game.handle_key_pressed(None, 'w') + self.assertFalse(self.game.settings_menu.waiting_for_key) + self.assertEqual(self.game.settings.KEY_UP_PRIMARY, 'w') + + # Navigate to "move left" + self.game.handle_key_pressed(KeyValues.DOWN) + self.game.handle_key_pressed(KeyValues.DOWN) + self.game.handle_key_pressed(KeyValues.DOWN) + self.game.handle_key_pressed(KeyValues.UP) + self.game.handle_key_pressed(KeyValues.DOWN) + self.game.handle_key_pressed(KeyValues.DOWN) + + # Define the "move up" key to 'a' + self.game.handle_key_pressed(KeyValues.ENTER) + self.assertTrue(self.game.settings_menu.waiting_for_key) + self.game.handle_key_pressed(None, 'a') + self.assertFalse(self.game.settings_menu.waiting_for_key) + self.assertEqual(self.game.settings.KEY_LEFT_PRIMARY, 'a') + def test_dead_screen(self) -> None: """ Kill player and render dead screen.