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
"""
held: bool
held_by: Optional[Player]
held_by: Optional[InventoryHolder]
price: int
def __init__(self, held: bool = False, held_by: Optional[Player] = None,
price: int = 2, *args, **kwargs):
def __init__(self, held: bool = False,
held_by: Optional[InventoryHolder] = None,
price: int = 2, *args, **kwargs):
super().__init__(*args, **kwargs)
self.held = held
self.held_by = held_by
@ -45,7 +46,7 @@ class Item(Entity):
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
"""
@ -66,15 +67,14 @@ class Item(Entity):
def get_all_items() -> list:
return [BodySnatchPotion, Bomb, Heart, Sword]
def be_sold(self, buyer: InventoryHolder, seller: InventoryHolder,
game: Any) -> bool:
def be_sold(self, buyer: InventoryHolder, seller: InventoryHolder) -> 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:
buyer.add_to_inventory(self)
self.hold(buyer)
seller.remove_from_inventory(self)
buyer.change_hazel_balance(-self.price)
seller.change_hazel_balance(self.price)
@ -109,22 +109,6 @@ class Heart(Item):
d["healing"] = self.healing
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):
"""

View File

@ -194,7 +194,7 @@ class Game:
if self.store_menu.values and not self.player.dead:
if key == KeyValues.ENTER:
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:
self.message = _("You do not have enough money")
self.display_actions(DisplayActions.UPDATE)

View File

@ -528,6 +528,15 @@ class TestGame(unittest.TestCase):
self.assertEqual(self.game.player.health,
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
self.game.handle_key_pressed(KeyValues.SPACE)
self.assertEqual(self.game.state, GameMode.PLAY)