Mercurial > parpg-core
comparison tests/test_container.py @ 0:1fd2201f5c36
Initial commit of parpg-core.
author | M. George Hansen <technopolitica@gmail.com> |
---|---|
date | Sat, 14 May 2011 01:12:35 -0700 |
parents | |
children | c017773e8443 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:1fd2201f5c36 |
---|---|
1 #!/usr/bin/env python | |
2 | |
3 # This file is part of PARPG. | |
4 # | |
5 # PARPG is free software: you can redistribute it and/or modify | |
6 # it under the terms of the GNU General Public License as published by | |
7 # the Free Software Foundation, either version 3 of the License, or | |
8 # (at your option) any later version. | |
9 # | |
10 # PARPG is distributed in the hope that it will be useful, | |
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 # GNU General Public License for more details. | |
14 # | |
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/>. | |
17 | |
18 import unittest | |
19 from parpg.objects.base import GameObject, Container, Scriptable, Carryable | |
20 | |
21 class TestContainer(unittest.TestCase): | |
22 class ScriptableContainer (GameObject, Container, Scriptable): | |
23 def __init__ (self, ID, **kwargs): | |
24 GameObject.__init__(self, ID, **kwargs) | |
25 Container.__init__(self, **kwargs) | |
26 Scriptable.__init__(self, **kwargs) | |
27 | |
28 class NonScriptableContainer (GameObject, Container): | |
29 def __init__ (self, ID, **kwargs): | |
30 GameObject.__init__(self, ID, **kwargs) | |
31 Container.__init__(self, **kwargs) | |
32 | |
33 class CarryableObject (GameObject, Carryable): | |
34 def __init__ (self, ID, **kwargs): | |
35 GameObject.__init__(self, ID, **kwargs) | |
36 Carryable.__init__(self, **kwargs) | |
37 | |
38 | |
39 def setUp(self): | |
40 self.ranOnPlaceItem = False | |
41 self.ranOnTakeItem = False | |
42 self.item = self.CarryableObject(6) | |
43 self.item2 = self.CarryableObject(7) | |
44 | |
45 def tearDown(self): | |
46 self.item = None | |
47 self.item2 = None | |
48 | |
49 | |
50 def onPlaceItem(self): | |
51 self.ranOnPlaceItem = True | |
52 | |
53 def onTakeItem(self): | |
54 self.ranOnTakeItem = True | |
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 |