[Zope-Checkins] CVS: Zope3/lib/python/Zope/App/Traversing/tests - testConvenienceFunctions.py:1.1 testObjectName.py:1.1 testTraverser.py:1.3

Steve Alexander steve@cat-box.net
Sat, 15 Jun 2002 16:38:48 -0400


Update of /cvs-repository/Zope3/lib/python/Zope/App/Traversing/tests
In directory cvs.zope.org:/tmp/cvs-serv22871/lib/python/Zope/App/Traversing/tests

Modified Files:
	testTraverser.py 
Added Files:
	testConvenienceFunctions.py testObjectName.py 
Log Message:
Added a set of convenience functions that you can import from
Zope.App.Traversing.

Fixed a few typos in the ContextWrapper docstrings, and also added
an isWrapper method to Zope/Proxy/IContextWrapper, mainly so that
I can raise nice errors if the convenience functions are passed
an unwrapped object, rather than returning objs or Nones or []s and 
having programs fail at tenuously related other places.


=== Added File Zope3/lib/python/Zope/App/Traversing/tests/testConvenienceFunctions.py ===
##############################################################################
#
# Copyright (c) 2001, 2002 Zope Corporation and Contributors.
# All Rights Reserved.
# 
# This software is subject to the provisions of the Zope Public License,
# Version 2.0 (ZPL).  A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
# 
##############################################################################
"""

$Id: testConvenienceFunctions.py,v 1.1 2002/06/15 20:38:18 stevea Exp $
"""
from unittest import TestCase, TestSuite, main, makeSuite
from Zope.App.OFS.Services.ServiceManager.tests.PlacefulSetup \
    import PlacefulSetup
from Zope.Proxy.ContextWrapper import ContextWrapper
from Zope.App.Traversing.Traverser import Traverser
from Zope.ComponentArchitecture import getService
from Zope.App.Traversing.ITraverser import ITraverser
from Zope.App.Traversing.ITraversable import ITraversable
from Zope.App.Traversing.DefaultTraversable import DefaultTraversable
from Zope.App.Traversing.ObjectName import IObjectName, ObjectName

class C:
    def __init__(self, name):
        self.name = name

class Test(PlacefulSetup, TestCase):

    def setUp(self):
        PlacefulSetup.setUp(self)
        # Build up a wrapper chain
        root = C('root')
        folder = C('folder')
        item = C('item')
        
        self.root =   ContextWrapper(root,   None,        name='')
        self.folder = ContextWrapper(folder, self.root,   name='folder')
        self.item =   ContextWrapper(item,   self.folder, name='item')
        self.unwrapped_item = item

        root.folder = folder
        folder.item = item

        self.tr = Traverser(root)
        getService(None,"Adapters").provideAdapter(
              None, ITraverser, Traverser)
        getService(None,"Adapters").provideAdapter(
              None, ITraversable, DefaultTraversable)
        getService(None,"Adapters").provideAdapter(
              None, IObjectName, ObjectName)


    def testTraverse(self):
        from Zope.App.Traversing import traverse
        self.assertEqual(
            traverse(self.item, '/folder/item'),
            self.tr.traverse('/folder/item')
            )
            
    def testTraverseFromUnwrapped(self):
        from Zope.App.Traversing import traverse
        self.assertRaises(
            TypeError,
            traverse,
            self.unwrapped_item, '/folder/item'
            )
        
    def testObjectName(self):
        from Zope.App.Traversing import objectName
        self.assertEqual(
            objectName(self.item),
            'item'
            )  

    def testObjectNameFromUnwrapped(self):
        from Zope.App.Traversing import objectName
        self.assertRaises(
            TypeError,
            objectName,
            self.unwrapped_item
            )  

    def testGetParent(self):
        from Zope.App.Traversing import getParent
        self.assertEqual(
            getParent(self.item),
            self.folder
            )  

    def testGetParentFromUnwrapped(self):
        from Zope.App.Traversing import getParent
        self.assertRaises(
            TypeError,
            getParent,
            self.unwrapped_item
            )  

    def testGetParents(self):
        from Zope.App.Traversing import getParents
        self.assertEqual(
            getParents(self.item),
            [self.folder, self.root]
            )

    def testGetParentsFromUnwrapped(self):
        from Zope.App.Traversing import getParents
        self.assertRaises(
            TypeError,
            getParents,
            self.unwrapped_item
            )

    def testGetPhysicalPath(self):
        from Zope.App.Traversing import getPhysicalPath
        self.assertEqual(
            getPhysicalPath(self.item),
            ('', 'folder', 'item')
            )

    def testGetPhysicalPathFromUnwrapped(self):
        from Zope.App.Traversing import getPhysicalPath
        self.assertRaises(
            TypeError,
            getPhysicalPath,
            self.unwrapped_item
            )

    def testGetPhysicalRoot(self):
        from Zope.App.Traversing import getPhysicalRoot
        self.assertEqual(
            getPhysicalRoot(self.item),
            self.root
            )

    def testGetPhysicalRootFromUnwrapped(self):
        from Zope.App.Traversing import getPhysicalRoot
        self.assertRaises(
            TypeError,
            getPhysicalRoot,
            self.unwrapped_item
            )

def test_suite():
    return TestSuite((
        makeSuite(Test),
        ))

if __name__=='__main__':
    main(defaultTest='test_suite')


=== Added File Zope3/lib/python/Zope/App/Traversing/tests/testObjectName.py ===
##############################################################################
#
# Copyright (c) 2001, 2002 Zope Corporation and Contributors.
# All Rights Reserved.
# 
# This software is subject to the provisions of the Zope Public License,
# Version 2.0 (ZPL).  A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
# 
##############################################################################
"""Test the ObjectName adapter

Revision information:
$Id: testObjectName.py,v 1.1 2002/06/15 20:38:18 stevea Exp $
"""
from unittest import TestCase, TestSuite, main, makeSuite
from Interface import Interface

from Zope.ComponentArchitecture.tests.PlacelessSetup import PlacelessSetup
from Zope.ComponentArchitecture import getService, getAdapter

from Zope.Proxy.ContextWrapper import ContextWrapper

from Zope.App.Traversing.ObjectName \
    import IObjectName, ObjectName, SiteObjectName

class IRoot(Interface): pass

class Root:
    __implements__ = IRoot
    
class TrivialContent(object):
    """Trivial content object, used because instances of object are rocks."""

class Test(PlacelessSetup, TestCase):

    def setUp(self):
        PlacelessSetup.setUp(self)
                    
        provideAdapter = getService(None, "Adapters").provideAdapter
        provideAdapter(None, IObjectName, [ObjectName])
        provideAdapter(IRoot, IObjectName, [ObjectName])

    def testAdapterBadObject(self):
        adapter = getAdapter(None, IObjectName)
        self.assertRaises(TypeError, adapter)
        
    def testAdapterNoContext(self):
        adapter = getAdapter(Root(), IObjectName)
        self.assertRaises(TypeError, adapter)
    
    def testAdapterBasicContext(self):
        content = ContextWrapper(TrivialContent(), Root(), name='a')
        content = ContextWrapper(TrivialContent(), content, name='b')
        content = ContextWrapper(TrivialContent(), content, name='c')
        adapter = getAdapter(content, IObjectName)
        self.assertEqual(adapter(), 'c')
        
def test_suite():
    return TestSuite((
        makeSuite(Test),
        ))

if __name__=='__main__':
    main(defaultTest='test_suite')



=== Zope3/lib/python/Zope/App/Traversing/tests/testTraverser.py 1.2 => 1.3 ===
 from Zope.Exceptions import NotFoundError, Unauthorized
 from Zope.ComponentArchitecture import getService
-from Zope.Security.SecurityManagement import setSecurityPolicy, \
-    noSecurityManager
+from Zope.Security.SecurityManagement \
+    import setSecurityPolicy, noSecurityManager
 
 from Interface.Verify import verifyClass
 from Interface.Implements import instancesOfObjectImplements
-from Zope.App.OFS.Services.ServiceManager.tests.PlacefulSetup import PlacefulSetup
+from Zope.App.OFS.Services.ServiceManager.tests.PlacefulSetup \
+    import PlacefulSetup
 from Zope.Security.Checker \
-     import ProxyFactory, defineChecker, NamesChecker, CheckerPublic, Checker
+    import ProxyFactory, defineChecker, NamesChecker, CheckerPublic, Checker
 from Zope.Security.SecurityManagement import newSecurityManager
 
 class C: