From b8d32b29c8b3d500b696a2d77f123756e7a1d9e8 Mon Sep 17 00:00:00 2001 From: Yohann D'ANELLO Date: Fri, 18 Dec 2020 02:17:06 +0100 Subject: [PATCH] Test selling items --- squirrelbattle/display/display_manager.py | 1 - squirrelbattle/menus.py | 4 ++-- squirrelbattle/tests/game_test.py | 22 ++++++++++++++++++++-- 3 files changed, 22 insertions(+), 5 deletions(-) diff --git a/squirrelbattle/display/display_manager.py b/squirrelbattle/display/display_manager.py index 4e2381a..83df543 100644 --- a/squirrelbattle/display/display_manager.py +++ b/squirrelbattle/display/display_manager.py @@ -54,7 +54,6 @@ class DisplayManager: self.mapdisplay.update_map(self.game.map) self.statsdisplay.update_player(self.game.player) self.game.inventory_menu.update_player(self.game.player) - self.game.store_menu.update_merchant(self.game.player) self.playerinventorydisplay.update_menu(self.game.inventory_menu) self.storeinventorydisplay.update_menu(self.game.store_menu) self.settingsmenudisplay.update_menu(self.game.settings_menu) diff --git a/squirrelbattle/menus.py b/squirrelbattle/menus.py index e0087a3..458c7b4 100644 --- a/squirrelbattle/menus.py +++ b/squirrelbattle/menus.py @@ -132,11 +132,11 @@ class InventoryMenu(Menu): class StoreMenu(Menu): - merchant: Merchant + merchant: Merchant = None def update_merchant(self, merchant: Merchant) -> None: self.merchant = merchant @property def values(self) -> list: - return self.merchant.inventory + return self.merchant.inventory if self.merchant else [] diff --git a/squirrelbattle/tests/game_test.py b/squirrelbattle/tests/game_test.py index 2b604be..89d6ed6 100644 --- a/squirrelbattle/tests/game_test.py +++ b/squirrelbattle/tests/game_test.py @@ -12,6 +12,7 @@ from ..entities.items import Bomb, Heart, Sword, Explosion from ..entities.player import Player from ..enums import DisplayActions from ..game import Game, KeyValues, GameMode +from ..interfaces import Map from ..menus import MainMenuValues from ..resources import ResourceManager from ..settings import Settings @@ -25,6 +26,10 @@ class TestGame(unittest.TestCase): """ self.game = Game() self.game.new_game() + self.game.map = Map.load( + ResourceManager.get_asset_path("example_map.txt")) + self.game.map.add_entity(self.game.player) + self.game.player.move(self.game.map.start_y, self.game.map.start_x) self.game.logs.add_message("Hello World !") display = DisplayManager(None, self.game) self.game.display_actions = display.handle_display_action @@ -556,12 +561,11 @@ class TestGame(unittest.TestCase): self.game.player.hazel = 0x7ffff42ff # The second item is not a heart - merchant.inventory[1] = Sword() + merchant.inventory[1] = sword = Sword() # Buy the second item by clicking on it item = self.game.store_menu.validate() self.assertIn(item, merchant.inventory) self.game.display_actions(DisplayActions.MOUSE, 7, 25) - self.game.handle_key_pressed(KeyValues.ENTER) self.assertIn(item, self.game.player.inventory) self.assertNotIn(item, merchant.inventory) @@ -587,6 +591,20 @@ class TestGame(unittest.TestCase): _("The buyer does not have enough money")) self.game.handle_key_pressed(KeyValues.ENTER) + # Sell an item + self.game.inventory_menu.position = len(self.game.player.inventory) - 1 + self.game.handle_key_pressed(KeyValues.LEFT) + self.assertFalse(self.game.is_in_store_menu) + self.assertIn(sword, self.game.player.inventory) + self.assertEqual(self.game.inventory_menu.validate(), sword) + old_player_money, old_merchant_money = self.game.player.hazel,\ + merchant.hazel + self.game.handle_key_pressed(KeyValues.ENTER) + self.assertNotIn(sword, self.game.player.inventory) + self.assertIn(sword, merchant.inventory) + self.assertEqual(self.game.player.hazel, old_player_money + sword.price) + self.assertEqual(merchant.hazel, old_merchant_money - sword.price) + # Exit the menu self.game.handle_key_pressed(KeyValues.SPACE) self.assertEqual(self.game.state, GameMode.PLAY)