Commits

Devin Jeanpierre committed 0dabb6c

added collision listing

Comments (0)

Files changed (2)

tetrisinventory/__init__.py

         self.offset_controller = offset_controller(self)
     
     def _can_insert(self, slots):
-        return not slots - set(
+        return not self._collisions(slots)
+    
+    def _collisions(self, slots):
+        return slots - set(
             k for k, v in self.available_slots.iteritems() if v is None)
     
     @adds_item
+    def collisions(self, item, offset):
+        return self._collisions(
+            self.offset_controller.transform(item.slots, offset))
+    
     def can_insert(self, item, offset):
-        return self._can_insert(
-            self.offset_controller.transform(item.slots, offset))
+        return not self.collisions(item, offset)
     
     @adds_item
     def insert(self, item, offset):

tetrisinventory/test/test_inventory.py

         self.assertRaises(InventoryItemNotFound,
             self.small_inventory.remove, self.dot())
 
+class TestInventoryCollisions(AbstractInventoryTest):
+    __test__ = True
+    # successes - no collisions
+    def test_emptyNoCollide(self):
+        self.assertEqual(
+            self.small_inventory.collisions(self.dot(), (0, 0)),
+            set())
+    
+    def test_noCollide(self):
+        self.small_inventory.insert(self.dot(), (0, 0))
+        self.assertEqual(
+            self.small_inventory.collisions(self.dot(),(0, 1)),
+            set())
+    
+    # successes - collisions
+    def test_oneCollision(self):
+        self.small_inventory.insert(self.dot(), (0, 0))
+        self.assertEqual(
+            self.small_inventory.collisions(self.dot(),(0, 0)),
+            set([(0, 0)]))
+    
+    def test_twoIndependentCollisions(self):
+        self.large_inventory.insert(self.dot(), (0, 0))
+        self.large_inventory.insert(self.dot(), (0, 1))
+        self.assertEqual(
+            self.large_inventory.collisions(self.vbar(), (0, 0)),
+            set([(0, 0), (0, 1)]))
+    
+    def test_twoCollisionsSameObject(self):
+        very_large_inventory = Inventory(set(product(range(4), repeat=2)))
+        
+        very_large_inventory.insert(self.vbar(), (0, 0))
+        self.assertEqual(
+            very_large_inventory.collisions(self.vbar(), (0, 1)),
+            set([(0, 1), (0, 2)]))
+    
+    def test_oneCollisionWithOutside(self):
+        """test against collisions with space outside the inventory"""
+        self.assertEqual(
+            self.small_inventory.collisions(self.dot(), (-1, 0)),
+            set([(-1, 0)]))
+    
+    def test_twoCollisionsWithOutside(self):
+        self.assertEqual(
+            self.small_inventory.collisions(self.hbar(), (-2, 0)),
+            set([(-2, 0), (-1, 0)]))
+
 class TestInventoryInsert(AbstractInventoryTest):
     __test__ = True
     # failures    
         self.item = item
         self.small_inventory = Inventory(set(product(range(2), repeat=2)))
     
+    def test_canCollideNull(self):
+        self.assertRaises(InvalidInventoryItemError,
+            self.small_inventory.collisions, self.item, (0, 0))
+    
     def test_canInsertNull(self):
         self.assertRaises(InvalidInventoryItemError,
             self.small_inventory.can_insert, self.item, (0, 0))