source: trunk/game/tests/test_container.py @ 290

Revision 286, 2.9 KB checked in by b0rland_parpg, 10 years ago (diff)

Ticket #67: patch by b0rland

  • changed default Lockable state to unlocked
  • fixed Lockable constructor logic to correctly process locked/is_open combination
  • fixed typo in Container
  • fixed tests to accomodate the above changes
  • created tests for Openable, Lockable, Container
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.objects.base import GameObject, Container, Scriptable, Carryable
20
21class  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        container.placeItem(self.item)
63        self.assertEqual(container.items,[self.item])
64        self.assertEqual(self.item.in_container, container)
65        self.assertRaises(ValueError, container.takeItem, self.item2)
66        container.takeItem(self.item)
67        self.assertEqual(container.items, [])
68
69    def testScripting(self):
70        container = self.ScriptableContainer(5,scripts={'onPlaceItem':(self.onPlaceItem,(),{}),'onTakeItem':(self.onTakeItem,(),{})})
71        self.assertFalse(self.ranOnPlaceItem)
72        self.assertFalse(self.ranOnTakeItem)
73        container.placeItem(self.item)
74        self.assertTrue(self.ranOnPlaceItem)
75        self.assertFalse(self.ranOnTakeItem)
76        self.ranOnPlaceItem = False
77        container.takeItem(self.item)
78        self.assertFalse(self.ranOnPlaceItem)
79        self.assertTrue(self.ranOnTakeItem)
80
81if __name__ == '__main__':
82    unittest.main()
83
Note: See TracBrowser for help on using the repository browser.