comparison tests/test_inventory.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
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.inventory import Inventory
20 from parpg.objects.base import GameObject, Carryable
21
22 class TestInventory(unittest.TestCase):
23 class CarryableObject (GameObject, Carryable):
24 def __init__ (self, ID, **kwargs):
25 GameObject.__init__(self, ID, **kwargs)
26 Carryable.__init__(self, **kwargs)
27
28 def setUp(self):
29 self.item = self.CarryableObject(12, name="TestItem1")
30 self.item.weight = 12
31 self.item2 = self.CarryableObject(13)
32 self.item2.weight = 13
33 self.inventory = Inventory()
34
35 def testPlaceTakeMove(self):
36 """Test Inventory Place/Take/Move functions"""
37 self.assertTrue(self.inventory.isSlotEmpty("backpack"))
38 self.inventory.placeItem(self.item)
39 self.assertTrue(self.item in self.inventory.getItemsInSlot("backpack").values())
40 self.assertEqual(self.inventory.weight, 12)
41 self.assertEqual(self.inventory.count(), 1)
42 self.assertFalse(self.inventory.isSlotEmpty("backpack"))
43
44 self.inventory.moveItemToSlot(self.item, "groin")
45 self.assertFalse(self.item in self.inventory.getItemsInSlot("backpack").values())
46 self.assertTrue(self.item in self.inventory.getItemsInSlot("groin").values())
47 self.assertEqual(self.inventory.count(), 1)
48
49 self.assertRaises(ValueError, self.inventory.moveItemToSlot, self.item2, "somewhere")
50
51 self.inventory.moveItemToSlot(self.item2, "chest")
52 self.assertEqual(self.inventory.count(),2)
53 self.assertEqual(self.inventory.weight, 12+13)
54 self.assertTrue(self.item2 in self.inventory.getItemsInSlot("chest").values())
55
56 self.inventory.takeItem(self.item)
57 self.assertEqual(self.inventory.count(),1)
58 self.assertEqual(self.inventory.weight, 13)
59
60 def testReplace(self):
61 """Test Inventory items replace each other in single-item slots"""
62 self.inventory.placeItem(self.item)
63 self.inventory.moveItemToSlot(self.item,"neck")
64 self.assertFalse(self.inventory.isSlotEmpty("neck"))
65 self.assertTrue(self.item in self.inventory.getItemsInSlot("neck").values())
66
67 self.inventory.moveItemToSlot(self.item2, "neck")
68 self.assertFalse(self.inventory.isSlotEmpty("neck"))
69 self.assertTrue(self.item2 in self.inventory.getItemsInSlot("neck").values())
70 self.assertFalse(self.item in self.inventory.getItemsInSlot("neck").values())
71
72 def testFind(self):
73 self.inventory.placeItem(self.item)
74 self.assertEqual(self.inventory.findItemByID(12), self.item)
75 self.assertEqual(self.inventory.findItemByID(13), None)
76 self.assertEqual(self.inventory.findItem(name="TestItem1"), self.item)
77 self.assertEqual(self.inventory.findItem(name="RandomName1"), None)
78 self.assertEqual(self.inventory.findItem(kind="carryable"), self.item)
79 self.assertEqual(self.inventory.findItem(kind="weapon"), None)
80
81
82 if __name__ == '__main__':
83 unittest.main()
84