Test buying an item when we don't have enough of money

This commit is contained in:
Yohann D'ANELLO 2020-12-11 17:28:16 +01:00
parent 7179346e2b
commit 99352bc1d5
3 changed files with 17 additions and 24 deletions

View File

@ -14,11 +14,12 @@ class Item(Entity):
A class for items A class for items
""" """
held: bool held: bool
held_by: Optional[Player] held_by: Optional[InventoryHolder]
price: int price: int
def __init__(self, held: bool = False, held_by: Optional[Player] = None, def __init__(self, held: bool = False,
price: int = 2, *args, **kwargs): held_by: Optional[InventoryHolder] = None,
price: int = 2, *args, **kwargs):
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
self.held = held self.held = held
self.held_by = held_by self.held_by = held_by
@ -45,7 +46,7 @@ class Item(Entity):
Indicates what should be done when the item is equipped. Indicates what should be done when the item is equipped.
""" """
def hold(self, player: "Player") -> None: def hold(self, player: InventoryHolder) -> None:
""" """
The item is taken from the floor and put into the inventory The item is taken from the floor and put into the inventory
""" """
@ -66,15 +67,14 @@ class Item(Entity):
def get_all_items() -> list: def get_all_items() -> list:
return [BodySnatchPotion, Bomb, Heart, Sword] return [BodySnatchPotion, Bomb, Heart, Sword]
def be_sold(self, buyer: InventoryHolder, seller: InventoryHolder, def be_sold(self, buyer: InventoryHolder, seller: InventoryHolder) -> bool:
game: Any) -> bool:
""" """
Does all necessary actions when an object is to be sold. Does all necessary actions when an object is to be sold.
Is overwritten by some classes that cannot exist in the player's Is overwritten by some classes that cannot exist in the player's
inventory inventory
""" """
if buyer.hazel >= self.price: if buyer.hazel >= self.price:
buyer.add_to_inventory(self) self.hold(buyer)
seller.remove_from_inventory(self) seller.remove_from_inventory(self)
buyer.change_hazel_balance(-self.price) buyer.change_hazel_balance(-self.price)
seller.change_hazel_balance(self.price) seller.change_hazel_balance(self.price)
@ -109,22 +109,6 @@ class Heart(Item):
d["healing"] = self.healing d["healing"] = self.healing
return d return d
def be_sold(self, buyer: InventoryHolder, seller: InventoryHolder,
game: Any) -> bool:
"""
Does all necessary actions when an object is to be sold.
Is overwritten by some classes that cannot exist in the player's
inventory
"""
if buyer.hazel >= self.price:
self.hold(buyer)
seller.remove_from_inventory(self)
buyer.change_hazel_balance(-self.price)
seller.change_hazel_balance(self.price)
return True
else:
return False
class Bomb(Item): class Bomb(Item):
""" """

View File

@ -194,7 +194,7 @@ class Game:
if self.store_menu.values and not self.player.dead: if self.store_menu.values and not self.player.dead:
if key == KeyValues.ENTER: if key == KeyValues.ENTER:
item = self.store_menu.validate() item = self.store_menu.validate()
flag = item.be_sold(self.player, self.store_menu.merchant, self) flag = item.be_sold(self.player, self.store_menu.merchant)
if not flag: if not flag:
self.message = _("You do not have enough money") self.message = _("You do not have enough money")
self.display_actions(DisplayActions.UPDATE) self.display_actions(DisplayActions.UPDATE)

View File

@ -528,6 +528,15 @@ class TestGame(unittest.TestCase):
self.assertEqual(self.game.player.health, self.assertEqual(self.game.player.health,
self.game.player.maxhealth - 1) self.game.player.maxhealth - 1)
# We don't have enough of money
self.game.player.hazel = 0
item = self.game.store_menu.validate()
self.game.handle_key_pressed(KeyValues.ENTER)
self.assertNotIn(item, self.game.player.inventory)
self.assertIn(item, merchant.inventory)
self.assertEqual(self.game.message, _("You do not have enough money"))
self.game.handle_key_pressed(KeyValues.ENTER)
# 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)