diff --git a/dungeonbattle/entities/items.py b/dungeonbattle/entities/items.py index 217514d..8811905 100644 --- a/dungeonbattle/entities/items.py +++ b/dungeonbattle/entities/items.py @@ -63,6 +63,14 @@ class Heart(Item): player.health = min(player.maxhealth, player.health + self.healing) self.map.remove_entity(self) + def save_state(self) -> dict: + """ + Saves the state of the header into a dictionary + """ + d = super().save_state() + d["healing"] = self.healing + return d + class Bomb(Item): """ @@ -90,3 +98,12 @@ class Bomb(Item): if abs(e.x - self.x) + abs(e.y - self.y) <= 1 and \ isinstance(e, FightingEntity): e.take_damage(self, self.damage) + + def save_state(self) -> dict: + """ + Saves the state of the bomb into a dictionary + """ + d = super().save_state() + d["exploding"] = self.exploding + d["damage"] = self.damage + return d diff --git a/dungeonbattle/interfaces.py b/dungeonbattle/interfaces.py index 3b83725..873678a 100644 --- a/dungeonbattle/interfaces.py +++ b/dungeonbattle/interfaces.py @@ -135,8 +135,6 @@ class Map: d["currenty"] = self.currenty d["entities"] = [] for enti in self.entities: - if enti.save_state() is None: - raise Exception(enti) d["entities"].append(enti.save_state()) d["map"] = self.draw_string(TexturePack.ASCII_PACK) return d diff --git a/dungeonbattle/menus.py b/dungeonbattle/menus.py index 27680ac..68b7740 100644 --- a/dungeonbattle/menus.py +++ b/dungeonbattle/menus.py @@ -120,9 +120,3 @@ class SettingsMenu(Menu): game.settings.write_settings() self.waiting_for_key = False self.update_values(game.settings) - - -class ArbitraryMenu(Menu): - def __init__(self, values: list): - super().__init__() - self.values = values diff --git a/dungeonbattle/tests/entities_test.py b/dungeonbattle/tests/entities_test.py index d2c8171..b272541 100644 --- a/dungeonbattle/tests/entities_test.py +++ b/dungeonbattle/tests/entities_test.py @@ -1,7 +1,7 @@ import unittest from dungeonbattle.entities.items import Bomb, Heart, Item -from dungeonbattle.entities.monsters import Hedgehog +from dungeonbattle.entities.monsters import Beaver, Hedgehog, Rabbit, TeddyBear from dungeonbattle.entities.player import Player from dungeonbattle.interfaces import Entity, Map @@ -35,21 +35,18 @@ class TestEntities(unittest.TestCase): """ Test some random stuff with fighting entities. """ - entity = Hedgehog() + entity = Beaver() self.map.add_entity(entity) - self.assertEqual(entity.maxhealth, 10) + self.assertEqual(entity.maxhealth, 20) self.assertEqual(entity.maxhealth, entity.health) - self.assertEqual(entity.strength, 3) - self.assertIsNone(entity.hit(entity)) - self.assertFalse(entity.dead) - self.assertIsNone(entity.hit(entity)) - self.assertFalse(entity.dead) - self.assertIsNone(entity.hit(entity)) - self.assertFalse(entity.dead) + self.assertEqual(entity.strength, 2) + for _ in range(9): + self.assertIsNone(entity.hit(entity)) + self.assertFalse(entity.dead) self.assertIsNone(entity.hit(entity)) self.assertTrue(entity.dead) - entity = Hedgehog() + entity = Rabbit() self.map.add_entity(entity) entity.move(15, 44) # Move randomly @@ -61,13 +58,17 @@ class TestEntities(unittest.TestCase): self.map.tick() self.assertTrue(entity.y == 2 and entity.x == 6) - # Hedgehog should fight + # Rabbit should fight old_health = self.player.health self.map.tick() self.assertTrue(entity.y == 2 and entity.x == 6) self.assertEqual(old_health - entity.strength, self.player.health) - # Fight the hedgehog + # Fight the rabbit + old_health = entity.health + self.player.move_down() + self.assertEqual(entity.health, old_health - self.player.strength) + self.assertFalse(entity.dead) old_health = entity.health self.player.move_down() self.assertEqual(entity.health, old_health - self.player.strength) @@ -104,17 +105,25 @@ class TestEntities(unittest.TestCase): """ item = Bomb() hedgehog = Hedgehog() + teddy_bear = TeddyBear() self.map.add_entity(item) self.map.add_entity(hedgehog) + self.map.add_entity(teddy_bear) hedgehog.health = 2 + teddy_bear.health = 2 hedgehog.move(41, 42) + teddy_bear.move(42, 41) item.act(self.map) self.assertFalse(hedgehog.dead) + self.assertFalse(teddy_bear.dead) item.drop(42, 42) self.assertEqual(item.y, 42) self.assertEqual(item.x, 42) item.act(self.map) self.assertTrue(hedgehog.dead) + self.assertTrue(teddy_bear.dead) + bomb_state = item.save_state() + self.assertEqual(bomb_state["damage"], item.damage) def test_hearts(self) -> None: """ @@ -128,6 +137,8 @@ class TestEntities(unittest.TestCase): self.assertNotIn(item, self.map.entities) self.assertEqual(self.player.health, self.player.maxhealth - item.healing) + heart_state = item.save_state() + self.assertEqual(heart_state["healing"], item.healing) def test_players(self) -> None: """ @@ -158,3 +169,6 @@ class TestEntities(unittest.TestCase): self.assertEqual(player.current_xp, 10) self.assertEqual(player.max_xp, 40) self.assertEqual(player.level, 4) + + player_state = player.save_state() + self.assertEqual(player_state["current_xp"], 10) diff --git a/dungeonbattle/tests/game_test.py b/dungeonbattle/tests/game_test.py index 80720ee..6784dd2 100644 --- a/dungeonbattle/tests/game_test.py +++ b/dungeonbattle/tests/game_test.py @@ -21,8 +21,20 @@ class TestGame(unittest.TestCase): self.game.display_actions = display.handle_display_action def test_load_game(self) -> None: - self.assertRaises(NotImplementedError, Game.load_game, "game.save") - self.assertRaises(NotImplementedError, Display(None).display) + """ + Save a game and reload it. + """ + old_state = self.game.save_state() + + self.game.handle_key_pressed(KeyValues.DOWN) + self.assertEqual(self.game.main_menu.validate(), MainMenuValues.SAVE) + self.game.handle_key_pressed(KeyValues.ENTER) # Save game + self.game.handle_key_pressed(KeyValues.DOWN) + self.assertEqual(self.game.main_menu.validate(), MainMenuValues.LOAD) + self.game.handle_key_pressed(KeyValues.ENTER) # Load game + + new_state = self.game.save_state() + self.assertEqual(old_state, new_state) def test_bootstrap_fail(self) -> None: """ @@ -82,6 +94,12 @@ class TestGame(unittest.TestCase): self.assertEqual(self.game.main_menu.validate(), MainMenuValues.START) self.game.handle_key_pressed(KeyValues.DOWN) + self.assertEqual(self.game.main_menu.validate(), + MainMenuValues.SAVE) + self.game.handle_key_pressed(KeyValues.DOWN) + self.assertEqual(self.game.main_menu.validate(), + MainMenuValues.LOAD) + self.game.handle_key_pressed(KeyValues.DOWN) self.assertEqual(self.game.main_menu.validate(), MainMenuValues.SETTINGS) self.game.handle_key_pressed(KeyValues.ENTER) @@ -100,6 +118,12 @@ class TestGame(unittest.TestCase): self.assertEqual(self.game.main_menu.validate(), MainMenuValues.SETTINGS) self.game.handle_key_pressed(KeyValues.UP) + self.assertEqual(self.game.main_menu.validate(), + MainMenuValues.LOAD) + self.game.handle_key_pressed(KeyValues.UP) + self.assertEqual(self.game.main_menu.validate(), + MainMenuValues.SAVE) + self.game.handle_key_pressed(KeyValues.UP) self.assertEqual(self.game.main_menu.validate(), MainMenuValues.START) @@ -146,6 +170,8 @@ class TestGame(unittest.TestCase): # Open settings menu 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.ENTER) self.assertEqual(self.game.state, GameMode.SETTINGS) @@ -214,3 +240,9 @@ class TestGame(unittest.TestCase): new_y, new_x = self.game.player.y, self.game.player.x self.assertEqual(new_y, y) self.assertEqual(new_x, x) + + def test_not_implemented(self) -> None: + """ + Check that some functions are not implemented, only for coverage. + """ + self.assertRaises(NotImplementedError, Display.display, None) diff --git a/dungeonbattle/tests/menus_test.py b/dungeonbattle/tests/menus_test.py deleted file mode 100644 index 6ad9df7..0000000 --- a/dungeonbattle/tests/menus_test.py +++ /dev/null @@ -1,24 +0,0 @@ -import unittest - -from dungeonbattle.menus import ArbitraryMenu, MainMenu, MainMenuValues - - -class TestMenus(unittest.TestCase): - def test_scroll_menu(self) -> None: - """ - Test to scroll the menu. - """ - arbitrary_menu = ArbitraryMenu([]) - self.assertEqual(arbitrary_menu.position, 0) - - main_menu = MainMenu() - self.assertEqual(main_menu.position, 0) - self.assertEqual(main_menu.validate(), MainMenuValues.START) - main_menu.go_up() - self.assertEqual(main_menu.validate(), MainMenuValues.START) - main_menu.go_down() - self.assertEqual(main_menu.validate(), MainMenuValues.SETTINGS) - main_menu.go_down() - self.assertEqual(main_menu.validate(), MainMenuValues.EXIT) - main_menu.go_down() - self.assertEqual(main_menu.validate(), MainMenuValues.EXIT)