source: trunk/game/tests/test_inventory.py @ 388

Revision 346, 3.0 KB checked in by b0rland_parpg, 10 years ago (diff)

Ticket #107: patch by b0rland

  • Wrote initial version of the inventory storage class
  • Added some useful utility functions into container class and its derivatives
  • Created several combined classes for carryable containers
  • Extended tests and of course made new ones
  • Renamed old Inventory class to be InventoryGUI

Note: InventoryGUI still works the old way and doesn't use Inventory

Line 
1#!/usr/bin/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
18import unittest
19from scripts.inventory import Inventory
20from scripts.objects.base import GameObject, Carryable
21
22class  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)
30        self.item.weight = 12
31        self.item2 = self.CarryableObject(13)
32        self.item2.weight = 13
33        self.inventory = Inventory(ID=11)
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"))
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"))
46        self.assertTrue(self.item in self.inventory.getItemsInSlot("groin"))
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"))
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"))
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"))
70        self.assertFalse(self.item in self.inventory.getItemsInSlot("neck"))
71       
72if __name__ == '__main__':
73    unittest.main()
74
Note: See TracBrowser for help on using the repository browser.