[Zope-Checkins] CVS: Zope3/lib/python/Zope/App/Traversing/tests - testTraverser.py:1.1.2.3
Martijn Pieters
mj@zope.com
Mon, 3 Dec 2001 17:08:13 -0500
Update of /cvs-repository/Zope3/lib/python/Zope/App/Traversing/tests
In directory cvs.zope.org:/tmp/cvs-serv23382/lib/python/Zope/App/Traversing/tests
Modified Files:
Tag: Zope-3x-branch
testTraverser.py
Log Message:
Implementation of unrestrictedTraverse, with new interface and feature
ITraversable. Include a default ITraversable implementation, and one for
containers. Note: No security assertions yet.
=== Zope3/lib/python/Zope/App/Traversing/tests/testTraverser.py 1.1.2.2 => 1.1.2.3 ===
import unittest
from Zope.App.Traversing.ITraverser import ITraverser
+from Zope.App.Traversing.ITraversable import ITraversable
from Zope.App.Traversing.Traverser import Traverser
-from Zope.ContextWrapper import Wrapper
+from Zope.App.Traversing.DefaultTraversable import DefaultTraversable
+from Zope.ContextWrapper import wrapper, Wrapper
+from Zope.Exceptions import NotFoundError
+from Zope.ComponentArchitecture import provideFeature, _clear
from Interface import verify, instancesOfObjectImplements
@@ -46,9 +50,105 @@
self.assertEquals(tr.getPhysicalPath(), ())
self.failUnless(tr.getPhysicalRoot() is unwrapped)
+class UnrestrictedTraverseTests(unittest.TestCase):
+ def setUp(self):
+ provideFeature(None, ITraversable, DefaultTraversable)
+
+ self.root = root = C('root')
+ self.folder = folder = C('folder')
+ self.item = item = C('item')
+
+ root.folder = folder
+ folder.item = item
+
+ self.tr = Traverser(root)
+
+ def tearDown(self):
+ _clear() # Clear out feature registration made
+
+ def testSimplePathString(self):
+ tr = self.tr
+ item = self.item
+
+ self.assertEquals(tr.unrestrictedTraverse('/folder/item'), item)
+ self.assertEquals(tr.unrestrictedTraverse('folder/item'), item)
+ self.assertEquals(tr.unrestrictedTraverse('/folder/item/'), item)
+
+ def testSimplePathTuple(self):
+ tr = self.tr
+ item = self.item
+
+ self.assertEquals(tr.unrestrictedTraverse(('', 'folder', 'item')), item)
+ self.assertEquals(tr.unrestrictedTraverse(('folder', 'item')), item)
+
+ def testComplexPathString(self):
+ tr = self.tr
+ item = self.item
+
+ self.assertEquals(tr.unrestrictedTraverse('/folder/../folder/./item'),
+ item)
+ self.assertEquals(tr.unrestrictedTraverse(
+ '/../folder/../../folder/item'), item)
+ self.assertEquals(tr.unrestrictedTraverse('../../folder/item'), item)
+
+ def testNoTraversable(self):
+ _clear()
+
+ self.assertRaises(NotFoundError, self.tr.unrestrictedTraverse, 'folder')
+
+ def testNotFoundDefault(self):
+ self.assertEquals(self.tr.unrestrictedTraverse('foo', 'notFound'),
+ 'notFound')
+
+ def testNotFoundNoDefault(self):
+ self.assertRaises(NotFoundError, self.tr.unrestrictedTraverse, 'foo')
+
+class DefaultTraversableTests(unittest.TestCase):
+ def testImplementsITraversable(self):
+ self.failUnless(ITraversable.isImplementedBy(DefaultTraversable(None)))
+
+ def testVerifyInterfaces(self):
+ for interface in instancesOfObjectImplements(DefaultTraversable):
+ verify(interface, DefaultTraversable)
+
+ def testAttributeTraverse(self):
+ root = C('root')
+ item = C('item')
+ root.item = item
+ df = DefaultTraversable(root)
+
+ further = []
+ next = df.traverse('item', further)
+ self.failUnless(next is item)
+ self.assertEquals(further, [])
+
+ def testDictionaryTraverse(self):
+ dict = {}
+ foo = C('foo')
+ dict['foo'] = foo
+ df = DefaultTraversable(dict)
+
+ further = []
+ next = df.traverse('foo', further)
+ self.failUnless(next is foo)
+ self.assertEquals(further, [])
+
+ def testUnderscoreNotFound(self):
+ df = DefaultTraversable(C('dummy'))
+
+ self.assertRaises(NotFoundError, df.traverse, '_foo', [])
+
+ def testNotFound(self):
+ df = DefaultTraversable(C('dummy'))
+
+ self.assertRaises(NotFoundError, df.traverse, 'bar', [])
+
def test_suite():
loader = unittest.TestLoader()
- return loader.loadTestsFromTestCase(TraverserTests)
+ suite = loader.loadTestsFromTestCase(TraverserTests)
+ suite.addTest(loader.loadTestsFromTestCase(DefaultTraversableTests))
+ suite.addTest(loader.loadTestsFromTestCase(UnrestrictedTraverseTests))
+ return suite
if __name__=='__main__':
unittest.TextTestRunner().run(test_suite())