# HG changeset patch # User KarstenBock@gmx.net # Date 1314971795 -7200 # Node ID c017773e844318e990b31cdfcb6d09ae67e2455d # Parent 5a0b7b1f023022d85b86fd5bd5dfe5dc4677ced3 Added unit tests for the container functions. diff -r 5a0b7b1f0230 -r c017773e8443 tests/test_container.py --- a/tests/test_container.py Fri Sep 02 15:54:56 2011 +0200 +++ b/tests/test_container.py Fri Sep 02 15:56:35 2011 +0200 @@ -16,79 +16,125 @@ # along with PARPG. If not, see . import unittest -from parpg.objects.base import GameObject, Container, Scriptable, Carryable + +from parpg.grease.world import BaseWorld +from parpg.grease.entity import Entity +from parpg.components import containable, container class TestContainer(unittest.TestCase): - class ScriptableContainer (GameObject, Container, Scriptable): - def __init__ (self, ID, **kwargs): - GameObject.__init__(self, ID, **kwargs) - Container.__init__(self, **kwargs) - Scriptable.__init__(self, **kwargs) + class GameWorld(BaseWorld): + """GameWorld""" - class NonScriptableContainer (GameObject, Container): - def __init__ (self, ID, **kwargs): - GameObject.__init__(self, ID, **kwargs) - Container.__init__(self, **kwargs) - - class CarryableObject (GameObject, Carryable): - def __init__ (self, ID, **kwargs): - GameObject.__init__(self, ID, **kwargs) - Carryable.__init__(self, **kwargs) - + def configure(self): + """Set up the world""" + self.components.containable = containable.Containable() + self.components.container = container.Container() + class Inventory(Entity): + """Enity representing an Iventory""" + + def __init__(self, world, max_bulk, slots): + """Constructor""" + self.container.children = slots + self.container.max_bulk = max_bulk + + + class Item(Entity): + def __init__(self, world, bulk): + """Constructor""" + self.containable.bulk = bulk + def setUp(self): - self.ranOnPlaceItem = False - self.ranOnTakeItem = False - self.item = self.CarryableObject(6) - self.item2 = self.CarryableObject(7) - - def tearDown(self): - self.item = None - self.item2 = None - - - def onPlaceItem(self): - self.ranOnPlaceItem = True - - def onTakeItem(self): - self.ranOnTakeItem = True - + unittest.TestCase.setUp(self) + self.world = self.GameWorld() + slots_15 = list() + slots_25 = list() + for i in xrange(3): + slots_15.append(None) + + for i in xrange(10): + slots_25.append(None) - def testPlaceTake(self): - """ Test Container mixin Place/Take functions """ - container = self.NonScriptableContainer(5) - self.assertEqual(container.items,{}) - self.assertEqual(self.item.in_container,None) - self.assertEqual(container.count(), 0) - container.placeItem(self.item) - self.assertEqual(container.items,{0:self.item}) - self.assertEqual(self.item.in_container, container) - self.assertEqual(container.count(), 1) - self.assertRaises(Container.SlotBusy, container.placeItem, self.item2, 0) - self.assertRaises(ValueError, container.takeItem, self.item2) - container.takeItem(self.item) - self.assertEqual(container.items, {}) - self.assertEqual(container.count(), 0) + self.inv_15 = self.Inventory(self.world, 15, slots_15) + self.inv_25 = self.Inventory(self.world, 25, slots_25) + self.dagger_1 = self.Item(self.world, 2) + self.sword_1 = self.Item(self.world, 4) + self.axe_1 = self.Item(self.world, 4) + self.mace_1 = self.Item(self.world, 5) + self.spear_1 = self.Item(self.world, 8) + self.dagger_2 = self.Item(self.world, 2) + self.sword_2 = self.Item(self.world, 4) + self.axe_2 = self.Item(self.world, 4) + self.mace_2 = self.Item(self.world, 5) + self.spear_2 = self.Item(self.world, 8) + + def tearDown(self): + self.dagger_1 = None + self.sword_1 = None + self.axe_1 = None + self.mace_1 = None + self.spear_1 = None + self.dagger_2 = None + self.sword_2 = None + self.axe_2 = None + self.mace_2 = None + self.spear_2 = None + self.inv_15 = None + self.inv_25 = None + self.world = None + + def test_State(self): + for child in self.inv_15.container.children: + self.assertIsNone(child) - def testBulk(self): - container = self.NonScriptableContainer(5, capacity=15) - self.item.bulk=10 - self.item2.bulk=7 - container.placeItem(self.item) - self.assertRaises(Container.TooBig, container.placeItem, self.item2) + for child in self.inv_25.container.children: + self.assertIsNone(child) + + def test_PutTake(self): + self.assertIsNone(container.get_item(self.inv_15.container, 0)) + + container.put_item(self.inv_15.container, self.sword_1.containable, 0) + self.assertIsNotNone(container.get_item(self.inv_15.container, 0)) + self.assertListEqual(self.inv_15.container.children, self.sword_1.containable.container.children) + self.assertEqual(self.inv_15.container.children[0].container, self.sword_1.containable.container) + self.assertEqual(self.inv_15.container.children[0].slot, self.sword_1.containable.slot) + + container.take_item(self.inv_15.container, 0) + self.assertIsNone(self.inv_15.container.children[0]) + + def test_Swap(self): + self.assertIsNone(self.inv_15.container.children[0]) + + container.put_item(self.inv_15.container, self.sword_1.containable, 0) + sword1 = container.get_item(self.inv_15.container, 0) + self.assertEqual(sword1.container, self.sword_1.containable.container) + self.assertEqual(sword1.slot, self.sword_1.containable.slot) + + sword2 = container.put_item(self.inv_15.container, self.dagger_1.containable, 0) + self.assertEqual(sword2.container, sword1.container) + self.assertEqual(sword2.slot, sword1.slot) - def testScripting(self): - container = self.ScriptableContainer(5,parpg={'onPlaceItem':(self.onPlaceItem,(),{}),'onTakeItem':(self.onTakeItem,(),{})}) - self.assertFalse(self.ranOnPlaceItem) - self.assertFalse(self.ranOnTakeItem) - container.placeItem(self.item) - self.assertTrue(self.ranOnPlaceItem) - self.assertFalse(self.ranOnTakeItem) - self.ranOnPlaceItem = False - container.takeItem(self.item) - self.assertFalse(self.ranOnPlaceItem) - self.assertTrue(self.ranOnTakeItem) + self.assertIsNotNone(container.get_item(self.inv_15.container, 0)) + self.assertListEqual(self.inv_15.container.children, self.dagger_1.containable.container.children) + self.assertEqual(self.inv_15.container.children[0].container, self.dagger_1.containable.container) + self.assertEqual(self.inv_15.container.children[0].slot, self.dagger_1.containable.slot) + + def test_BulkSlots(self): + container.put_item(self.inv_15.container, self.sword_1.containable) + container.put_item(self.inv_25.container, self.sword_2.containable) + self.assertEqual(container.get_total_bulk(self.inv_15.container), self.sword_1.containable.bulk) + self.assertEqual(container.get_total_bulk(self.inv_25.container), self.sword_2.containable.bulk) + + container.put_item(self.inv_15.container, self.axe_1.containable) + container.put_item(self.inv_25.container, self.axe_2.containable) + self.assertEqual(container.get_total_bulk(self.inv_15.container), container.get_total_bulk(self.inv_25.container)) + + self.assertRaises(container.BulkLimitError, container.put_item, self.inv_15.container, self.spear_1.containable) + container.put_item(self.inv_25.container, self.spear_2.containable) -if __name__ == '__main__': - unittest.main() + container.put_item(self.inv_15.container, self.dagger_1.containable) + container.put_item(self.inv_25.container, self.dagger_2.containable) + self.assertNotEqual(container.get_total_bulk(self.inv_15.container), container.get_total_bulk(self.inv_25.container)) + self.assertRaises(container.NoFreeSlotError, container.put_item, self.inv_15.container, self.mace_1.containable) + container.put_item(self.inv_25.container, self.mace_2.containable)