[Zope-Checkins] CVS: Zope3/lib/python/Zope/App/OFS/Container/tests - testContainerTraversable.py:1.1.2.5 testContainerTraverser.py:1.1.2.9 testIContainer.py:1.1.2.6

Jim Fulton jim@zope.com
Mon, 10 Jun 2002 15:34:50 -0400


Update of /cvs-repository/Zope3/lib/python/Zope/App/OFS/Container/tests
In directory cvs.zope.org:/tmp/cvs-serv5490/lib/python/Zope/App/OFS/Container/tests

Modified Files:
      Tag: Zope-3x-branch
	testContainerTraversable.py testContainerTraverser.py 
	testIContainer.py 
Log Message:
Implemented 

http://dev.zope.org/Wikis/DevSite/Projects/ComponentArchitecture/IContainerPythonification

Along the way:

- Converted most uses of has_key to use in.

- Fixed a bug in Interface names and namesAndDescriptions methods 
  that caused base class attributes to be missed.



=== Zope3/lib/python/Zope/App/OFS/Container/tests/testContainerTraversable.py 1.1.2.4 => 1.1.2.5 ===
             
             
-    def getObject(self, name, default=None):
+    def __getitem__(self, name):
+        return self.__objs[name]
+            
+    def get(self, name, default=None):
         return self.__objs.get(name, default)
 
-    def hasObject(self, name):
+    def __contains__(self, name):
         return self.__objs.has_key(name)
 
 class Test(CleanUp, unittest.TestCase):


=== Zope3/lib/python/Zope/App/OFS/Container/tests/testContainerTraverser.py 1.1.2.8 => 1.1.2.9 ===
 
 
-    def getObject(self, name, default=None):
+    def get(self, name, default=None):
         
         return getattr(self, name, default)
 


=== Zope3/lib/python/Zope/App/OFS/Container/tests/testIContainer.py 1.1.2.5 => 1.1.2.6 ===
         verifyObject(IContainer, self._Test__new())
 
-    def test_objectIds(self):
+    def test_keys(self):
         '''See interface IReadContainer'''
         container = self._Test__new()
-        data = container.objectIds()
+        data = container.keys()
         self.assertEqual(list(data), [])
         
         container = self.__setUp()
-        data = container.objectIds()
+        data = container.keys()
         data = list(data); data.sort() # convert to sorted list
         self.assertEqual(data, range(10))
         
-    def test_getObject(self):
+    def test_get(self):
         '''See interface IReadContainer'''
         container = self._Test__new()
-        self.assertRaises(KeyError, container.getObject, 1)
-        self.assertEqual(container.getObject(1, 99), 99)
+        self.assertRaises(KeyError, container.__getitem__, 1)
+        self.assertEqual(container.get(1, 99), 99)
         
         container = self.__setUp()
-        self.assertRaises(KeyError, container.getObject, 100)
-        self.assertEqual(container.getObject(100, 99), 99)
-        self.assertEqual(container.getObject(1, 99), 6)
-        self.assertEqual(container.getObject(7), 8)
-        self.assertEqual(container.getObject(0), 4)
-        self.assertEqual(container.getObject(9), 9)
+        self.assertRaises(KeyError, container.__getitem__, 100)
+        self.assertEqual(container.get(100, 99), 99)
+        self.assertEqual(container.get(1, 99), 6)
+        self.assertEqual(container[7], 8)
+        self.assertEqual(container[0], 4)
+        self.assertEqual(container[9], 9)
         
-    def test_objectValues(self):
+    def test_values(self):
         '''See interface IReadContainer'''
         container = self._Test__new()
-        data = container.objectValues()
+        data = container.values()
         self.assertEqual(list(data), [])
         
         container = self.__setUp()
-        data = container.objectValues()
+        data = container.values()
         data = list(data); data.sort() # convert to sorted list
         self.assertEqual(data, range(10))
 
-    def test_objectCount(self):
+    def test_len(self):
         '''See interface IReadContainer'''
         container = self._Test__new()
-        self.assertEqual(container.objectCount(), 0)
+        self.assertEqual(len(container), 0)
         
         container = self.__setUp()
-        self.assertEqual(container.objectCount(), 10)
+        self.assertEqual(len(container), 10)
 
-    def test_objectItems(self):
+    def test_items(self):
         '''See interface IReadContainer'''
         container = self._Test__new()
-        data = container.objectItems()
+        data = container.items()
         self.assertEqual(list(data), [])
         
         container = self.__setUp()
-        data = container.objectItems()
+        data = container.items()
         data = list(data); data.sort() # convert to sorted list
         self.assertEqual(data, [
             (0, 4), (1, 6), (2, 1), (3, 0), (4, 2),
             (5, 5), (6, 3), (7, 8), (8, 7), (9, 9)
             ])
 
-    def test_hasObject(self):
+    def test___contains__(self):
         '''See interface IReadContainer'''
         container = self._Test__new()
-        self.assertEqual(not not container.hasObject(1), 0)
+        self.assertEqual(not not (1 in container), 0)
         
         container = self.__setUp()
-        self.assertEqual(not not container.hasObject(100), 0)
-        self.assertEqual(not not container.hasObject(1), 1)
-        self.assertEqual(not not container.hasObject(0), 1)
-        self.assertEqual(not not container.hasObject(9), 1)
+        self.assertEqual(not not (100 in container), 0)
+        self.assertEqual(not not (1 in container), 1)
+        self.assertEqual(not not (0 in container), 1)
+        self.assertEqual(not not (9 in container), 1)
 
     def test_delObject(self):
         '''See interface IWriteContainer'''
         container = self._Test__new()
-        self.assertRaises(KeyError, container.delObject, 1)
+        self.assertRaises(KeyError, container.__delitem__, 1)
         
         container = self.__setUp()
-        self.assertRaises(KeyError, container.delObject, 100)
-        container.delObject(1)
-        container.delObject(9)
-        self.assertRaises(KeyError, container.getObject, 1)
-        self.assertRaises(KeyError, container.getObject, 9)
-        self.assertEqual(container.getObject(1, 99), 99)
-        self.assertEqual(container.getObject(7), 8)
-        self.assertEqual(container.getObject(0), 4)
-        self.assertEqual(container.getObject(9, 88), 88)
+        self.assertRaises(KeyError, container.__delitem__, 100)
+        del container[1]
+        del container[9]
+        self.assertRaises(KeyError, container.__getitem__, 1)
+        self.assertRaises(KeyError, container.__getitem__, 9)
+        self.assertEqual(container.get(1, 99), 99)
+        self.assertEqual(container[7], 8)
+        self.assertEqual(container[0], 4)
+        self.assertEqual(container.get(9, 88), 88)
 
     ############################################################
     # Tests from Folder
@@ -132,16 +132,16 @@
     def testEmpty( self ):
 
         folder = self._Test__new()
-        self.failIf( folder.objectIds()         )
-        self.failIf( folder.objectValues()      )
-        self.failIf( folder.objectItems()       )
-        self.failIf( folder.objectCount()       )
-        self.failIf( folder.hasObject( 'foo' )  )
+        self.failIf(folder.keys())
+        self.failIf(folder.values())
+        self.failIf(folder.items())
+        self.failIf(len(folder))
+        self.failIf('foo' in folder)
 
-        self.assertEquals( folder.getObject( 'foo', None ), None )
-        self.assertRaises( KeyError, folder.getObject, 'foo' )
+        self.assertEquals( folder.get( 'foo', None ), None )
+        self.assertRaises( KeyError, folder.__getitem__, 'foo' )
 
-        self.assertRaises( KeyError, folder.delObject, 'foo' )
+        self.assertRaises( KeyError, folder.__delitem__, 'foo' )
 
     def testOneItem( self ):
 
@@ -149,44 +149,44 @@
         foo = []
         folder.setObject( 'foo', foo )
 
-        self.assertEquals( len( folder.objectIds() ), 1             )
-        self.assertEquals( folder.objectIds()[0], 'foo'             )
-        self.assertEquals( len( folder.objectValues() ), 1          )
-        self.assertEquals( folder.objectValues()[0], foo            )
-        self.assertEquals( len( folder.objectItems() ), 1           )
-        self.assertEquals( folder.objectItems()[0], ( 'foo', foo )  )
-        self.assertEquals( folder.objectCount(), 1                  )
+        self.assertEquals( len( folder.keys() ), 1             )
+        self.assertEquals( folder.keys()[0], 'foo'             )
+        self.assertEquals( len( folder.values() ), 1          )
+        self.assertEquals( folder.values()[0], foo            )
+        self.assertEquals( len( folder.items() ), 1           )
+        self.assertEquals( folder.items()[0], ( 'foo', foo )  )
+        self.assertEquals( len(folder), 1                  )
 
-        self.failUnless( folder.hasObject( 'foo' )  )
-        self.failIf( folder.hasObject( 'bar' ) )
+        self.failUnless('foo' in folder)
+        self.failIf('bar' in folder)
 
-        self.assertEquals( folder.getObject( 'foo', None ), foo )
-        self.assertEquals( folder.getObject( 'foo' ), foo )
+        self.assertEquals( folder.get( 'foo', None ), foo )
+        self.assertEquals( folder['foo'], foo )
 
-        self.assertRaises( KeyError, folder.getObject, 'qux' )
+        self.assertRaises( KeyError, folder.__getitem__, 'qux' )
 
         foo2 = []
         folder.setObject( 'foo', foo )
 
-        self.assertEquals( len( folder.objectIds() ), 1             )
-        self.assertEquals( folder.objectIds()[0], 'foo'             )
-        self.assertEquals( len( folder.objectValues() ), 1          )
-        self.assertEquals( folder.objectValues()[0], foo2           )
-        self.assertEquals( len( folder.objectItems() ), 1           )
-        self.assertEquals( folder.objectItems()[0], ( 'foo', foo2 ) )
-        self.assertEquals( folder.objectCount(), 1                  )
-
-        folder.delObject( 'foo' )
-
-        self.failIf( folder.objectIds()         )
-        self.failIf( folder.objectValues()      )
-        self.failIf( folder.objectItems()       )
-        self.failIf( folder.objectCount()       )
-        self.failIf( folder.hasObject( 'foo' )  )
-
-        self.assertRaises( KeyError, folder.getObject, 'foo' )
-        self.assertEquals( folder.getObject( 'foo', None ), None )
-        self.assertRaises( KeyError, folder.delObject, 'foo' )
+        self.assertEquals( len( folder.keys() ), 1             )
+        self.assertEquals( folder.keys()[0], 'foo'             )
+        self.assertEquals( len( folder.values() ), 1          )
+        self.assertEquals( folder.values()[0], foo2           )
+        self.assertEquals( len( folder.items() ), 1           )
+        self.assertEquals( folder.items()[0], ( 'foo', foo2 ) )
+        self.assertEquals( len(folder), 1                  )
+
+        del folder['foo']
+
+        self.failIf(folder.keys()         )
+        self.failIf(folder.values()      )
+        self.failIf(folder.items()       )
+        self.failIf(len(folder)       )
+        self.failIf('foo' in folder)
+
+        self.assertRaises( KeyError, folder.__getitem__, 'foo' )
+        self.assertEquals( folder.get( 'foo', None ), None )
+        self.assertRaises( KeyError, folder.__delitem__, 'foo' )
 
     def testManyItems( self ):
 
@@ -197,69 +197,69 @@
         folder.setObject( 'baz', objects[2] )
         folder.setObject( 'bam', objects[3] )
 
-        self.assertEquals( len( folder.objectIds() ), len( objects ) )
-        self.failUnless( 'foo' in folder.objectIds() )
-        self.failUnless( 'bar' in folder.objectIds() )
-        self.failUnless( 'baz' in folder.objectIds() )
-        self.failUnless( 'bam' in folder.objectIds() )
-
-        self.assertEquals( len( folder.objectValues() ), len( objects ) )
-        self.failUnless( objects[0] in folder.objectValues() )
-        self.failUnless( objects[1] in folder.objectValues() )
-        self.failUnless( objects[2] in folder.objectValues() )
-        self.failUnless( objects[3] in folder.objectValues() )
-
-        self.assertEquals( len( folder.objectItems() ), len( objects ) )
-        self.failUnless( ( 'foo', objects[0] ) in folder.objectItems() )
-        self.failUnless( ( 'bar', objects[1] ) in folder.objectItems() )
-        self.failUnless( ( 'baz', objects[2] ) in folder.objectItems() )
-        self.failUnless( ( 'bam', objects[3] ) in folder.objectItems() )
-
-        self.assertEquals( folder.objectCount(), len( objects ) )
-
-        self.failUnless( folder.hasObject( 'foo' )  )
-        self.failUnless( folder.hasObject( 'bar' )  )
-        self.failUnless( folder.hasObject( 'baz' )  )
-        self.failUnless( folder.hasObject( 'bam' )  )
-        self.failIf( folder.hasObject( 'qux' ) )
-
-        self.assertEquals( folder.getObject( 'foo', None ), objects[0] )
-        self.assertEquals( folder.getObject( 'foo' ),       objects[0] )
-        self.assertEquals( folder.getObject( 'bar', None ), objects[1] )
-        self.assertEquals( folder.getObject( 'bar' ),       objects[1] )
-        self.assertEquals( folder.getObject( 'baz', None ), objects[2] )
-        self.assertEquals( folder.getObject( 'baz' ),       objects[2] )
-        self.assertEquals( folder.getObject( 'bam', None ), objects[3] )
-        self.assertEquals( folder.getObject( 'bam' ),       objects[3] )
-
-        self.assertEquals( folder.getObject( 'qux', None ), None )
-        self.assertRaises( KeyError, folder.getObject, 'qux' )
-
-        folder.delObject( 'foo' )
-        self.assertEquals( folder.objectCount(), len( objects ) - 1 )
-        self.failIf( folder.hasObject( 'foo' )  )
-        self.failIf( 'foo' in folder.objectIds() )
-
-        self.failIf( objects[0] in folder.objectValues() )
-        self.failIf( ( 'foo', objects[0] ) in folder.objectItems() )
-
-        self.assertEquals( folder.getObject( 'foo', None ), None )
-        self.assertRaises( KeyError, folder.getObject, 'foo' )
-
-        self.assertRaises( KeyError, folder.delObject, 'foo' )
-
-        folder.delObject( 'bar' )
-        folder.delObject( 'baz' )
-        folder.delObject( 'bam' )
-
-        self.failIf( folder.objectIds()         )
-        self.failIf( folder.objectValues()      )
-        self.failIf( folder.objectItems()       )
-        self.failIf( folder.objectCount()       )
-        self.failIf( folder.hasObject( 'foo' )  )
-        self.failIf( folder.hasObject( 'bar' )  )
-        self.failIf( folder.hasObject( 'baz' )  )
-        self.failIf( folder.hasObject( 'bam' )  )
+        self.assertEquals( len( folder.keys() ), len( objects ) )
+        self.failUnless( 'foo' in folder.keys() )
+        self.failUnless( 'bar' in folder.keys() )
+        self.failUnless( 'baz' in folder.keys() )
+        self.failUnless( 'bam' in folder.keys() )
+
+        self.assertEquals( len( folder.values() ), len( objects ) )
+        self.failUnless( objects[0] in folder.values() )
+        self.failUnless( objects[1] in folder.values() )
+        self.failUnless( objects[2] in folder.values() )
+        self.failUnless( objects[3] in folder.values() )
+
+        self.assertEquals( len( folder.items() ), len( objects ) )
+        self.failUnless( ( 'foo', objects[0] ) in folder.items() )
+        self.failUnless( ( 'bar', objects[1] ) in folder.items() )
+        self.failUnless( ( 'baz', objects[2] ) in folder.items() )
+        self.failUnless( ( 'bam', objects[3] ) in folder.items() )
+
+        self.assertEquals(len(folder), len( objects ) )
+
+        self.failUnless('foo' in folder)
+        self.failUnless('bar' in folder)
+        self.failUnless('baz' in folder)
+        self.failUnless('bam' in folder)
+        self.failIf('qux' in folder)
+
+        self.assertEquals( folder.get( 'foo', None ), objects[0] )
+        self.assertEquals( folder['foo'],       objects[0] )
+        self.assertEquals( folder.get( 'bar', None ), objects[1] )
+        self.assertEquals( folder['bar'],       objects[1] )
+        self.assertEquals( folder.get( 'baz', None ), objects[2] )
+        self.assertEquals( folder['baz'],       objects[2] )
+        self.assertEquals( folder.get( 'bam', None ), objects[3] )
+        self.assertEquals( folder['bam'],       objects[3] )
+
+        self.assertEquals( folder.get( 'qux', None ), None )
+        self.assertRaises( KeyError, folder.__getitem__, 'qux' )
+
+        del folder['foo']
+        self.assertEquals(len(folder), len( objects ) - 1 )
+        self.failIf( 'foo' in folder)
+        self.failIf( 'foo' in folder.keys() )
+
+        self.failIf( objects[0] in folder.values() )
+        self.failIf( ( 'foo', objects[0] ) in folder.items() )
+
+        self.assertEquals( folder.get( 'foo', None ), None )
+        self.assertRaises( KeyError, folder.__getitem__, 'foo' )
+
+        self.assertRaises( KeyError, folder.__delitem__, 'foo' )
+
+        del folder['bar']
+        del folder['baz']
+        del folder['bam']
+
+        self.failIf( folder.keys()         )
+        self.failIf( folder.values()      )
+        self.failIf( folder.items()       )
+        self.failIf(len(folder)       )
+        self.failIf('foo' in folder)
+        self.failIf('bar' in folder)
+        self.failIf('baz' in folder)
+        self.failIf('bam' in folder)
 
 
 class Test(BaseTestIContainer, TestCase):