diff tests/test_lockable.py @ 50:79cb7a659414

Added unit tests for the Lockable functions
author KarstenBock@gmx.net
date Fri, 02 Sep 2011 15:57:59 +0200
parents 1fd2201f5c36
children 0a2cc748d16b
line wrap: on
line diff
--- a/tests/test_lockable.py	Fri Sep 02 15:57:27 2011 +0200
+++ b/tests/test_lockable.py	Fri Sep 02 15:57:59 2011 +0200
@@ -15,42 +15,49 @@
 # You should have received a copy of the GNU General Public License
 # along with PARPG.  If not, see <http://www.gnu.org/licenses/>.
 
+from parpg.grease.world import BaseWorld
+from parpg.grease.entity import Entity
+from parpg.components import lockable
+
 import unittest
-from parpg.objects.base import Lockable, GameObject
-
 
-class  TestLockable(unittest.TestCase):
+class TestLockable(unittest.TestCase):
+    class Lock(Entity):
+        """Enity representing an Lock"""
+        
+        def __init__(self, world, locked, closed):
+            """Constructor"""
+            self.lockable.locked = locked
+            self.lockable.closed = closed
 
-    class LockableObject (GameObject, Lockable):
-        def __init__ (self, ID, **kwargs):
-            GameObject.__init__(self, ID, **kwargs)
-            Lockable.__init__(self, **kwargs)
+    class GameWorld(BaseWorld):
+        """GameWorld"""
+
+        def configure(self):
+            """Set up the world"""
+            self.components.lockable = lockable.Lockable()
 
-    def testConstructor(self):
-        """ Test Lockable mixin constructor """
-        lockable = self.LockableObject(4)
-        self.assertEqual(lockable.locked,False)
-        self.assertEqual(lockable.is_open,True)
-        lockable = self.LockableObject(4,locked=False,is_open=False)
-        self.assertEqual(lockable.locked, False)
-        self.assertEqual(lockable.is_open, False)
-        lockable = self.LockableObject(4,locked=True)
-        self.assertEqual(lockable.locked, True)
-        self.assertEqual(lockable.is_open, False)
-
+    def setUp(self):
+        unittest.TestCase.setUp(self)
+        self.world = self.GameWorld()
+        self.lock = self.Lock(self.world, False, True)
+        
+    def tearDown(self):
+        self.lock = None
+        self.world = None
+        
+    def testOpenClose(self):
+        lockable.open(self.lock.lockable)
+        self.assertFalse(self.lock.lockable.closed)
+        lockable.close(self.lock.lockable)
+        self.assertTrue(self.lock.lockable.closed)
+         
     def testLockUnlock(self):
-        """ Test Lockable mixin locking and unlocking """
-        lockable = self.LockableObject(4)
-        lockable.open()
-        self.assertEqual(lockable.is_open, True)
-        lockable.lock()
-        self.assertEqual(lockable.locked, True)
-        self.assertEqual(lockable.is_open,False)
-        self.assertRaises(ValueError, lockable.open)
-        lockable.unlock()
-        self.assertEqual(lockable.locked, False)
-        self.assertEqual(lockable.is_open,False)    
+        lockable.lock(self.lock.lockable)
+        self.assertTrue(self.lock.lockable.locked)
+        self.assertRaises(lockable.LockedError, lockable.open, self.lock.lockable)
+        lockable.unlock(self.lock.lockable)
+        self.assertFalse(self.lock.lockable.locked)
+        lockable.open(self.lock.lockable)
+        self.assertRaises(lockable.OpenError, lockable.lock, self.lock.lockable)
 
-if __name__ == '__main__':
-    unittest.main()
-