Mercurial > parpg-core
comparison tests/test_container.py @ 48:c017773e8443
Added unit tests for the container functions.
author | KarstenBock@gmx.net |
---|---|
date | Fri, 02 Sep 2011 15:56:35 +0200 |
parents | 1fd2201f5c36 |
children | 0a2cc748d16b |
comparison
equal
deleted
inserted
replaced
47:5a0b7b1f0230 | 48:c017773e8443 |
---|---|
14 # | 14 # |
15 # You should have received a copy of the GNU General Public License | 15 # You should have received a copy of the GNU General Public License |
16 # along with PARPG. If not, see <http://www.gnu.org/licenses/>. | 16 # along with PARPG. If not, see <http://www.gnu.org/licenses/>. |
17 | 17 |
18 import unittest | 18 import unittest |
19 from parpg.objects.base import GameObject, Container, Scriptable, Carryable | 19 |
20 from parpg.grease.world import BaseWorld | |
21 from parpg.grease.entity import Entity | |
22 from parpg.components import containable, container | |
20 | 23 |
21 class TestContainer(unittest.TestCase): | 24 class TestContainer(unittest.TestCase): |
22 class ScriptableContainer (GameObject, Container, Scriptable): | 25 class GameWorld(BaseWorld): |
23 def __init__ (self, ID, **kwargs): | 26 """GameWorld""" |
24 GameObject.__init__(self, ID, **kwargs) | |
25 Container.__init__(self, **kwargs) | |
26 Scriptable.__init__(self, **kwargs) | |
27 | 27 |
28 class NonScriptableContainer (GameObject, Container): | 28 def configure(self): |
29 def __init__ (self, ID, **kwargs): | 29 """Set up the world""" |
30 GameObject.__init__(self, ID, **kwargs) | 30 self.components.containable = containable.Containable() |
31 Container.__init__(self, **kwargs) | 31 self.components.container = container.Container() |
32 | 32 |
33 class CarryableObject (GameObject, Carryable): | 33 class Inventory(Entity): |
34 def __init__ (self, ID, **kwargs): | 34 """Enity representing an Iventory""" |
35 GameObject.__init__(self, ID, **kwargs) | 35 |
36 Carryable.__init__(self, **kwargs) | 36 def __init__(self, world, max_bulk, slots): |
37 """Constructor""" | |
38 self.container.children = slots | |
39 self.container.max_bulk = max_bulk | |
40 | |
41 | |
42 class Item(Entity): | |
43 def __init__(self, world, bulk): | |
44 """Constructor""" | |
45 self.containable.bulk = bulk | |
46 | |
47 def setUp(self): | |
48 unittest.TestCase.setUp(self) | |
49 self.world = self.GameWorld() | |
50 slots_15 = list() | |
51 slots_25 = list() | |
52 for i in xrange(3): | |
53 slots_15.append(None) | |
54 | |
55 for i in xrange(10): | |
56 slots_25.append(None) | |
37 | 57 |
58 self.inv_15 = self.Inventory(self.world, 15, slots_15) | |
59 self.inv_25 = self.Inventory(self.world, 25, slots_25) | |
60 self.dagger_1 = self.Item(self.world, 2) | |
61 self.sword_1 = self.Item(self.world, 4) | |
62 self.axe_1 = self.Item(self.world, 4) | |
63 self.mace_1 = self.Item(self.world, 5) | |
64 self.spear_1 = self.Item(self.world, 8) | |
65 self.dagger_2 = self.Item(self.world, 2) | |
66 self.sword_2 = self.Item(self.world, 4) | |
67 self.axe_2 = self.Item(self.world, 4) | |
68 self.mace_2 = self.Item(self.world, 5) | |
69 self.spear_2 = self.Item(self.world, 8) | |
70 | |
71 def tearDown(self): | |
72 self.dagger_1 = None | |
73 self.sword_1 = None | |
74 self.axe_1 = None | |
75 self.mace_1 = None | |
76 self.spear_1 = None | |
77 self.dagger_2 = None | |
78 self.sword_2 = None | |
79 self.axe_2 = None | |
80 self.mace_2 = None | |
81 self.spear_2 = None | |
82 self.inv_15 = None | |
83 self.inv_25 = None | |
84 self.world = None | |
38 | 85 |
39 def setUp(self): | 86 def test_State(self): |
40 self.ranOnPlaceItem = False | 87 for child in self.inv_15.container.children: |
41 self.ranOnTakeItem = False | 88 self.assertIsNone(child) |
42 self.item = self.CarryableObject(6) | |
43 self.item2 = self.CarryableObject(7) | |
44 | 89 |
45 def tearDown(self): | 90 for child in self.inv_25.container.children: |
46 self.item = None | 91 self.assertIsNone(child) |
47 self.item2 = None | 92 |
93 def test_PutTake(self): | |
94 self.assertIsNone(container.get_item(self.inv_15.container, 0)) | |
95 | |
96 container.put_item(self.inv_15.container, self.sword_1.containable, 0) | |
97 self.assertIsNotNone(container.get_item(self.inv_15.container, 0)) | |
98 self.assertListEqual(self.inv_15.container.children, self.sword_1.containable.container.children) | |
99 self.assertEqual(self.inv_15.container.children[0].container, self.sword_1.containable.container) | |
100 self.assertEqual(self.inv_15.container.children[0].slot, self.sword_1.containable.slot) | |
101 | |
102 container.take_item(self.inv_15.container, 0) | |
103 self.assertIsNone(self.inv_15.container.children[0]) | |
104 | |
105 def test_Swap(self): | |
106 self.assertIsNone(self.inv_15.container.children[0]) | |
107 | |
108 container.put_item(self.inv_15.container, self.sword_1.containable, 0) | |
109 sword1 = container.get_item(self.inv_15.container, 0) | |
110 self.assertEqual(sword1.container, self.sword_1.containable.container) | |
111 self.assertEqual(sword1.slot, self.sword_1.containable.slot) | |
112 | |
113 sword2 = container.put_item(self.inv_15.container, self.dagger_1.containable, 0) | |
114 self.assertEqual(sword2.container, sword1.container) | |
115 self.assertEqual(sword2.slot, sword1.slot) | |
48 | 116 |
117 self.assertIsNotNone(container.get_item(self.inv_15.container, 0)) | |
118 self.assertListEqual(self.inv_15.container.children, self.dagger_1.containable.container.children) | |
119 self.assertEqual(self.inv_15.container.children[0].container, self.dagger_1.containable.container) | |
120 self.assertEqual(self.inv_15.container.children[0].slot, self.dagger_1.containable.slot) | |
121 | |
122 def test_BulkSlots(self): | |
123 container.put_item(self.inv_15.container, self.sword_1.containable) | |
124 container.put_item(self.inv_25.container, self.sword_2.containable) | |
125 self.assertEqual(container.get_total_bulk(self.inv_15.container), self.sword_1.containable.bulk) | |
126 self.assertEqual(container.get_total_bulk(self.inv_25.container), self.sword_2.containable.bulk) | |
127 | |
128 container.put_item(self.inv_15.container, self.axe_1.containable) | |
129 container.put_item(self.inv_25.container, self.axe_2.containable) | |
130 self.assertEqual(container.get_total_bulk(self.inv_15.container), container.get_total_bulk(self.inv_25.container)) | |
131 | |
132 self.assertRaises(container.BulkLimitError, container.put_item, self.inv_15.container, self.spear_1.containable) | |
133 container.put_item(self.inv_25.container, self.spear_2.containable) | |
49 | 134 |
50 def onPlaceItem(self): | 135 container.put_item(self.inv_15.container, self.dagger_1.containable) |
51 self.ranOnPlaceItem = True | 136 container.put_item(self.inv_25.container, self.dagger_2.containable) |
137 self.assertNotEqual(container.get_total_bulk(self.inv_15.container), container.get_total_bulk(self.inv_25.container)) | |
52 | 138 |
53 def onTakeItem(self): | 139 self.assertRaises(container.NoFreeSlotError, container.put_item, self.inv_15.container, self.mace_1.containable) |
54 self.ranOnTakeItem = True | 140 container.put_item(self.inv_25.container, self.mace_2.containable) |
55 | |
56 | |
57 def testPlaceTake(self): | |
58 """ Test Container mixin Place/Take functions """ | |
59 container = self.NonScriptableContainer(5) | |
60 self.assertEqual(container.items,{}) | |
61 self.assertEqual(self.item.in_container,None) | |
62 self.assertEqual(container.count(), 0) | |
63 container.placeItem(self.item) | |
64 self.assertEqual(container.items,{0:self.item}) | |
65 self.assertEqual(self.item.in_container, container) | |
66 self.assertEqual(container.count(), 1) | |
67 self.assertRaises(Container.SlotBusy, container.placeItem, self.item2, 0) | |
68 self.assertRaises(ValueError, container.takeItem, self.item2) | |
69 container.takeItem(self.item) | |
70 self.assertEqual(container.items, {}) | |
71 self.assertEqual(container.count(), 0) | |
72 | |
73 def testBulk(self): | |
74 container = self.NonScriptableContainer(5, capacity=15) | |
75 self.item.bulk=10 | |
76 self.item2.bulk=7 | |
77 container.placeItem(self.item) | |
78 self.assertRaises(Container.TooBig, container.placeItem, self.item2) | |
79 | |
80 def testScripting(self): | |
81 container = self.ScriptableContainer(5,parpg={'onPlaceItem':(self.onPlaceItem,(),{}),'onTakeItem':(self.onTakeItem,(),{})}) | |
82 self.assertFalse(self.ranOnPlaceItem) | |
83 self.assertFalse(self.ranOnTakeItem) | |
84 container.placeItem(self.item) | |
85 self.assertTrue(self.ranOnPlaceItem) | |
86 self.assertFalse(self.ranOnTakeItem) | |
87 self.ranOnPlaceItem = False | |
88 container.takeItem(self.item) | |
89 self.assertFalse(self.ranOnPlaceItem) | |
90 self.assertTrue(self.ranOnTakeItem) | |
91 | |
92 if __name__ == '__main__': | |
93 unittest.main() | |
94 |