Test selling items

This commit is contained in:
Yohann D'ANELLO 2020-12-18 02:17:06 +01:00
parent c55a7451e7
commit b8d32b29c8
3 changed files with 22 additions and 5 deletions

View File

@ -54,7 +54,6 @@ class DisplayManager:
self.mapdisplay.update_map(self.game.map) self.mapdisplay.update_map(self.game.map)
self.statsdisplay.update_player(self.game.player) self.statsdisplay.update_player(self.game.player)
self.game.inventory_menu.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.playerinventorydisplay.update_menu(self.game.inventory_menu)
self.storeinventorydisplay.update_menu(self.game.store_menu) self.storeinventorydisplay.update_menu(self.game.store_menu)
self.settingsmenudisplay.update_menu(self.game.settings_menu) self.settingsmenudisplay.update_menu(self.game.settings_menu)

View File

@ -132,11 +132,11 @@ class InventoryMenu(Menu):
class StoreMenu(Menu): class StoreMenu(Menu):
merchant: Merchant merchant: Merchant = None
def update_merchant(self, merchant: Merchant) -> None: def update_merchant(self, merchant: Merchant) -> None:
self.merchant = merchant self.merchant = merchant
@property @property
def values(self) -> list: def values(self) -> list:
return self.merchant.inventory return self.merchant.inventory if self.merchant else []

View File

@ -12,6 +12,7 @@ from ..entities.items import Bomb, Heart, Sword, Explosion
from ..entities.player import Player from ..entities.player import Player
from ..enums import DisplayActions from ..enums import DisplayActions
from ..game import Game, KeyValues, GameMode from ..game import Game, KeyValues, GameMode
from ..interfaces import Map
from ..menus import MainMenuValues from ..menus import MainMenuValues
from ..resources import ResourceManager from ..resources import ResourceManager
from ..settings import Settings from ..settings import Settings
@ -25,6 +26,10 @@ class TestGame(unittest.TestCase):
""" """
self.game = Game() self.game = Game()
self.game.new_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 !") self.game.logs.add_message("Hello World !")
display = DisplayManager(None, self.game) display = DisplayManager(None, self.game)
self.game.display_actions = display.handle_display_action self.game.display_actions = display.handle_display_action
@ -556,12 +561,11 @@ class TestGame(unittest.TestCase):
self.game.player.hazel = 0x7ffff42ff self.game.player.hazel = 0x7ffff42ff
# The second item is not a heart # The second item is not a heart
merchant.inventory[1] = Sword() merchant.inventory[1] = sword = Sword()
# Buy the second item by clicking on it # Buy the second item by clicking on it
item = self.game.store_menu.validate() item = self.game.store_menu.validate()
self.assertIn(item, merchant.inventory) self.assertIn(item, merchant.inventory)
self.game.display_actions(DisplayActions.MOUSE, 7, 25) self.game.display_actions(DisplayActions.MOUSE, 7, 25)
self.game.handle_key_pressed(KeyValues.ENTER)
self.assertIn(item, self.game.player.inventory) self.assertIn(item, self.game.player.inventory)
self.assertNotIn(item, merchant.inventory) self.assertNotIn(item, merchant.inventory)
@ -587,6 +591,20 @@ class TestGame(unittest.TestCase):
_("The buyer does not have enough money")) _("The buyer does not have enough money"))
self.game.handle_key_pressed(KeyValues.ENTER) 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 # Exit the menu
self.game.handle_key_pressed(KeyValues.SPACE) self.game.handle_key_pressed(KeyValues.SPACE)
self.assertEqual(self.game.state, GameMode.PLAY) self.assertEqual(self.game.state, GameMode.PLAY)