Mi2dRPGamEng/tests/test_inventory.py

50 lines
1.6 KiB
Python

from engine.inventory import Inventory
from engine.item import ItemDef, ItemInstance
STAFF = ItemDef(id="staff_oak", name="Oak Staff", width=1, height=5)
SANDWICH = ItemDef(id="sandwich", name="Sandwich", width=2, height=2, stackable=True, max_stack=4)
def test_place_non_overlapping_items_succeeds():
inv = Inventory(6, 5)
staff = ItemInstance("item-1", "staff_oak")
sandwich = ItemInstance("item-2", "sandwich")
assert inv.place(staff, STAFF, 0, 0) is True
assert inv.place(sandwich, SANDWICH, 1, 0) is True
assert {p.item.item_id for p in inv.items()} == {"item-1", "item-2"}
def test_overlapping_placement_rejected():
inv = Inventory(6, 5)
staff = ItemInstance("item-1", "staff_oak")
other = ItemInstance("item-2", "sandwich")
assert inv.place(staff, STAFF, 0, 0) is True
assert inv.can_place(SANDWICH, 0, 2) is False # overlaps rows 2-3 of the staff at x=0
assert inv.place(other, SANDWICH, 0, 2) is False
def test_out_of_bounds_placement_rejected():
inv = Inventory(6, 4)
assert inv.can_place(STAFF, 0, 0) is False # height 5 doesn't fit in a 4-tall grid at all
def test_remove_frees_footprint_for_reuse():
inv = Inventory(6, 5)
staff = ItemInstance("item-1", "staff_oak")
inv.place(staff, STAFF, 0, 0)
removed = inv.remove("item-1", STAFF)
assert removed is staff
assert inv.can_place(STAFF, 0, 0) is True
assert inv.place(staff, STAFF, 0, 0) is True
def test_find_first_fit_returns_leftmost_topmost_slot():
inv = Inventory(6, 5)
inv.place(ItemInstance("item-1", "staff_oak"), STAFF, 0, 0)
slot = inv.find_first_fit(SANDWICH)
assert slot == (1, 0)