[Zope3-checkins] CVS: Zope3/src/zope/app/traversing/tests - __init__.py:1.1.2.1 test_acquire.py:1.1.2.1 test_atteritem.py:1.1.2.1 test_conveniencefunctions.py:1.1.2.1 test_etc.py:1.1.2.1 test_namespacetrversal.py:1.1.2.1 test_objectname.py:1.1.2.1 test_physicallocationadapters.py:1.1.2.1 test_presentation.py:1.1.2.1 test_skin.py:1.1.2.1 test_traverser.py:1.1.2.1

Jim Fulton jim@zope.com
Mon, 23 Dec 2002 14:32:36 -0500


Update of /cvs-repository/Zope3/src/zope/app/traversing/tests
In directory cvs.zope.org:/tmp/cvs-serv19908/zope/app/traversing/tests

Added Files:
      Tag: NameGeddon-branch
	__init__.py test_acquire.py test_atteritem.py 
	test_conveniencefunctions.py test_etc.py 
	test_namespacetrversal.py test_objectname.py 
	test_physicallocationadapters.py test_presentation.py 
	test_skin.py test_traverser.py 
Log Message:
Initial renaming before debugging

=== Added File Zope3/src/zope/app/traversing/tests/__init__.py ===
#
# This file is necessary to make this directory a package.


=== Added File Zope3/src/zope/app/traversing/tests/test_acquire.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
# 
##############################################################################
"""

Revision information:
$Id: test_acquire.py,v 1.1.2.1 2002/12/23 19:32:34 jim Exp $
"""

from unittest import TestCase, TestSuite, main, makeSuite
from zope.app.tests.placelesssetup import PlacelessSetup
from zope.app.interfaces.traversing.traversable import ITraversable
from zope.app.traversing.defaulttraversable import DefaultTraversable
from zope.component.adapter import provideAdapter
from zope.proxy.context.context import ContextWrapper, getWrapperContext
from zope.app.traversing.acquirenamespace import acquire
from zope.exceptions import NotFoundError

class Test(PlacelessSetup, TestCase):

    def test(self):
        provideAdapter(None, ITraversable, DefaultTraversable)

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

        a = C('a')
        a.a1 = C('a1')
        a.a2 = C('a2')
        a.a2.a21 = C('a21')
        a.a2.a21.a211 = C('a211')

        a2 = ContextWrapper(a.a2, a)
        a21 = ContextWrapper(a.a2.a21, a2)
        a211 = ContextWrapper(a.a2.a21.a211, a21)

        acquired = acquire('a1', (), 'a1;acquire', a211, None)

        self.assertEqual(acquired.name, 'a1')

        self.assertRaises(NotFoundError,
                          acquire, 'a3', (), 'a1;acquire', a211, None)
        
        

def test_suite():
    return makeSuite(Test)

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


=== Added File Zope3/src/zope/app/traversing/tests/test_atteritem.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
# 
##############################################################################
"""

Revision information:
$Id: test_atteritem.py,v 1.1.2.1 2002/12/23 19:32:34 jim Exp $
"""

from unittest import TestCase, TestSuite, main, makeSuite
from zope.testing.cleanup import CleanUp # Base class w registry cleanup


class C:
    a = 1
    def __getitem__(self, key): return key+'value'
c=C()


class Test(CleanUp, TestCase):

    def testAttr(self):
        from zope.app.traversing.attritemnamespaces import attr
        self.assertEqual(attr('a', (), 'a;attribute', c, None), 1)

    def testItem(self):
        from zope.app.traversing.attritemnamespaces import item
        self.assertEqual(item('a', (), 'a;item', c, None), 'avalue')
        
        

def test_suite():
    return makeSuite(Test)

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


=== Added File Zope3/src/zope/app/traversing/tests/test_conveniencefunctions.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: test_conveniencefunctions.py,v 1.1.2.1 2002/12/23 19:32:34 jim Exp $
"""
from unittest import TestCase, TestSuite, main, makeSuite
from zope.app.services.tests.placefulsetup \
    import PlacefulSetup
from zope.proxy.context.context import ContextWrapper
from zope.app.traversing.traverser import Traverser
from zope.component import getService

from zope.app.interfaces.traversing.traverser import ITraverser
from zope.app.interfaces.traversing.traversable import ITraversable
from zope.app.traversing.defaulttraversable import DefaultTraversable
from zope.app.traversing.objectname import IObjectName, ObjectName

from zope.app.interfaces.traversing.physicallylocatable import IPhysicallyLocatable
from zope.app.interfaces.traversing.containmentroot import IContainmentRoot
from zope.app.traversing.physicallocationadapters \
     import WrapperPhysicallyLocatable, RootPhysicallyLocatable

from zope.security.proxy import Proxy
from zope.security.checker import selectChecker

from zope.exceptions import NotFoundError

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

def _proxied(*args):
    return Proxy(args, selectChecker(args))


class Test(PlacefulSetup, TestCase):

    def setUp(self):
        PlacefulSetup.setUp(self)
        # Build up a wrapper chain
        root = C('root')
        root.__implements__ = IContainmentRoot
        folder = C('folder')
        item = C('item')
        
        self.root =   ContextWrapper(root, None)
        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)
        getService(None, "Adapters").provideAdapter(
              None, IPhysicallyLocatable, WrapperPhysicallyLocatable)
        getService(None, "Adapters").provideAdapter(
              IContainmentRoot, IPhysicallyLocatable, RootPhysicallyLocatable)


    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 testTraverseName(self):
        from Zope.App.Traversing import traverseName
        self.assertEqual(
            traverseName(self.folder, 'item'),
            self.tr.traverse('/folder/item')
            )
        self.assertEqual(
            traverseName(self.item, '.'),
            self.tr.traverse('/folder/item')
            )
            
    def testTraverseNameBadValue(self):
        from Zope.App.Traversing import traverseName
        self.assertRaises(
            NotFoundError,
            traverseName,
            self.folder, '../root'
            )
        self.assertRaises(
            NotFoundError,
            traverseName,
            self.folder, '/root'
            )
        self.assertRaises(
            NotFoundError,
            traverseName,
            self.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 testGetParentFromUnwrapped(self):
        from Zope.App.Traversing import getParent
        self.assertRaises(
            TypeError,
            getParent,
            self.unwrapped_item
            )

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

    def testGetPhysicalPathString(self):
        from Zope.App.Traversing import getPhysicalPathString
        self.assertEqual(
            getPhysicalPathString(self.item),
            u'/folder/item'
            )

    def testGetPhysicalPathOfRoot(self):
        from Zope.App.Traversing import getPhysicalPath
        self.assertEqual(
            getPhysicalPath(self.root),
            ('',)
            )

    def testGetPhysicalPathStringOfRoot(self):
        from Zope.App.Traversing import getPhysicalPathString
        self.assertEqual(
            getPhysicalPathString(self.root),
            u'/',
            )

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

    _bad_locations = (
        (UnicodeError, ('',u'123','\xa323')),
        (UnicodeError, '\xa323'),
        (ValueError, ()),
        (ValueError, 23),
        (ValueError, ''),
        (ValueError, '//'),
##        (AssertionError, '/foo//bar'),

        # regarding the next four errors:
        # having a trailing slash on a location is undefined.
        # we might want to give it a particular meaning for zope3 later
        # for now, it is an invalid location identifier
        (ValueError, '/foo/bar/'),
        (ValueError, 'foo/bar/'),
        (ValueError, ('','foo','bar','')),
        (ValueError, ('foo','bar',''))
        )

    # sequence of N-tuples:
    #   (loc_returned_as_string, loc_returned_as_tuple, input, input, ...)
    # The string and tuple are tested as input as well as being the
    # specification for output.

    _good_locations = (
        # location returned as string   location returned as tuple
        ( u'xx/yy/zz',                  (u'xx',u'yy',u'zz'),          
            # arguments to try in addition to the above
            ('xx','yy','zz'),
            'xx/yy/zz',
        ),
        ( u'/xx/yy/zz',                 (u'',u'xx',u'yy',u'zz'),      
            ('','xx','yy','zz'),
            '/xx/yy/zz',
            _proxied('','xx','yy','zz'),
        ),
        ( u'xx',                        (u'xx',),
            ('xx',),
            'xx',
        ),
        ( u'/',                         (u'',),
            ('',),
            '/',
        ),
    )

    def testLocationAsTuple(self):
        from Zope.App.Traversing import locationAsTuple as lat

        for error_type, value in self._bad_locations:
            self.assertRaises(error_type, lat, value)

        for spec in self._good_locations:
            correct_answer = spec[1]
            for argument in spec:
                self.applyAssertEqual(lat, argument, correct_answer)
        
    def testLocationAsUnicode(self):
        from Zope.App.Traversing import locationAsUnicode as lau

        for error_type, value in self._bad_locations:
            self.assertRaises(error_type, lau, value)

        for spec in self._good_locations:
            correct_answer = spec[0]
            for argument in spec:
                self.applyAssertEqual(lau, argument, correct_answer)

    def applyAssertEqual(self, func, arg, answer):
        try:
            self.assertEqual(func(arg), answer)
        except:
            print "Failure on ", arg
            raise

def test_suite():
    return makeSuite(Test)

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


=== Added File Zope3/src/zope/app/traversing/tests/test_etc.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
# 
##############################################################################
"""

Revision information:
$Id: test_etc.py,v 1.1.2.1 2002/12/23 19:32:34 jim Exp $
"""

from unittest import TestCase, TestSuite, main, makeSuite
from zope.testing.cleanup import CleanUp # Base class w registry cleanup

class Test(CleanUp, TestCase):

    def testApplicationControl(self):
        from zope.app.traversing.etcnamespace import etc
        from zope.app.applicationcontrol.applicationcontrol \
             import applicationController, applicationControllerRoot
        
        self.assertEqual(
            etc('ApplicationController', (), '++etc++ApplicationController',
                applicationControllerRoot, None),
            applicationController)

    def testServices(self):
        from zope.app.traversing.etcnamespace import etc
        class C:
            def getServiceManager(self): return 42
        
        self.assertEqual(etc('Services', (), 'etc:Services', C(), None), 42)

        

def test_suite():
    return makeSuite(Test)

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


=== Added File Zope3/src/zope/app/traversing/tests/test_namespacetrversal.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
#
##############################################################################
"""

Revision information:
$Id: test_namespacetrversal.py,v 1.1.2.1 2002/12/23 19:32:34 jim Exp $
"""

from unittest import TestCase, TestSuite, main, makeSuite
from zope.testing.cleanup import CleanUp # Base class w registry cleanup

class C:
    a = 1
    def __getitem__(self, key): return key+'value'

c=C()


class Test(CleanUp, TestCase):

    def setUp(self):
        from zope.app.traversing.namespaces import provideNamespaceHandler
        from zope.app.traversing.attritemnamespaces import attr, item
        from zope.app.traversing.skinnamespace import skin
        provideNamespaceHandler('attribute', attr)
        provideNamespaceHandler('item', item)
        provideNamespaceHandler('skin', skin)

    def testAttr(self):
        from zope.app.traversing.traverser import Traverser
        traverser = Traverser(c)
        v = traverser.traverse('++attribute++a')
        self.assertEqual(v, 1)

    def testItem(self):
        from zope.app.traversing.traverser import Traverser
        traverser = Traverser(c)
        v = traverser.traverse('++item++a')
        self.assertEqual(v, 'avalue')

    def testSideEffectsContextDetail(self):
        # Check to make sure that when we traverse something in context,
        # that we get the right context for the result.
        from zope.proxy.context.context \
             import ContextWrapper, getWrapperContainer
        from zope.app.traversing.traverser import Traverser
        from zope.publisher.browser import TestRequest

        c1 = C()
        c2 = C()
        c2c1 = ContextWrapper(c2, c1)

        traverser = Traverser(c2c1)
        v = traverser.traverse('++skin++ZopeTop', request=TestRequest())
        self.assertEqual(v, c2)
        self.failUnless(getWrapperContainer(v) is c2c1)


def test_suite():
    return makeSuite(Test)


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


=== Added File Zope3/src/zope/app/traversing/tests/test_objectname.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: test_objectname.py,v 1.1.2.1 2002/12/23 19:32:34 jim Exp $
"""
from unittest import TestCase, TestSuite, main, makeSuite
from zope.interface import Interface

from zope.app.tests.placelesssetup import PlacelessSetup
from zope.component import getService, getAdapter

from zope.proxy.context.context 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 makeSuite(Test)

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



=== Added File Zope3/src/zope/app/traversing/tests/test_physicallocationadapters.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: test_physicallocationadapters.py,v 1.1.2.1 2002/12/23 19:32:34 jim Exp $
"""

from unittest import TestCase, TestSuite, main, makeSuite
from zope.app.tests.placelesssetup import PlacelessSetup
from zope.component.adapter import provideAdapter
from zope.component import getAdapter

from zope.app.interfaces.traversing.containmentroot import IContainmentRoot
from zope.app.interfaces.traversing.physicallylocatable import IPhysicallyLocatable
from zope.app.traversing.physicallocationadapters \
     import WrapperPhysicallyLocatable, RootPhysicallyLocatable
from zope.proxy.context.context import ContextWrapper

class Root: __implements__ = IContainmentRoot
class C: pass

class Test(PlacelessSetup, TestCase):

    def test(self):
        provideAdapter(None, IPhysicallyLocatable, WrapperPhysicallyLocatable)
        provideAdapter(IContainmentRoot, IPhysicallyLocatable,
                       RootPhysicallyLocatable)

        root = Root()
        f1 = ContextWrapper(C(), root, name='f1')
        f2 = ContextWrapper(C(),   f1, name='f2')
        f3 = ContextWrapper(C(),   f2, name='f3')

        adapter = getAdapter(f3, IPhysicallyLocatable)

        self.assertEqual(adapter.getPhysicalPath(), ('', 'f1', 'f2', 'f3'))
        self.assertEqual(adapter.getPhysicalRoot(), root)
        
        adapter = getAdapter(C(), IPhysicallyLocatable)
        self.assertRaises(TypeError, adapter.getPhysicalPath)
        self.assertRaises(TypeError, adapter.getPhysicalRoot)

    def testWSideEffectDataInFront(self):
        provideAdapter(None, IPhysicallyLocatable, WrapperPhysicallyLocatable)
        provideAdapter(IContainmentRoot, IPhysicallyLocatable,
                       RootPhysicallyLocatable)

        root = Root()
        root = ContextWrapper(root, root, name='.',
                              side_effect_name="++skin++ZopeTop")
        f1 = ContextWrapper(C(), root, name='f1')
        f2 = ContextWrapper(C(),   f1, name='f2')
        f3 = ContextWrapper(C(),   f2, name='f3')

        adapter = getAdapter(f3, IPhysicallyLocatable)

        self.assertEqual(adapter.getPhysicalPath(), ('', 'f1', 'f2', 'f3'))
        self.assertEqual(adapter.getPhysicalRoot(), root)
        
        adapter = getAdapter(C(), IPhysicallyLocatable)
        self.assertRaises(TypeError, adapter.getPhysicalPath)
        self.assertRaises(TypeError, adapter.getPhysicalRoot)

    def testWSideEffectDataInMiddle(self):
        provideAdapter(None, IPhysicallyLocatable, WrapperPhysicallyLocatable)
        provideAdapter(IContainmentRoot, IPhysicallyLocatable,
                       RootPhysicallyLocatable)

        root = Root()
        c = C()
        f1 = ContextWrapper(c, root, name='f1')
        f1 = ContextWrapper(c, f1, name='.',
                            side_effect_name="++skin++ZopeTop")
        f2 = ContextWrapper(C(),   f1, name='f2')
        f3 = ContextWrapper(C(),   f2, name='f3')

        adapter = getAdapter(f3, IPhysicallyLocatable)

        self.assertEqual(adapter.getPhysicalPath(), ('', 'f1', 'f2', 'f3'))
        self.assertEqual(adapter.getPhysicalRoot(), root)
        
        adapter = getAdapter(C(), IPhysicallyLocatable)
        self.assertRaises(TypeError, adapter.getPhysicalPath)
        self.assertRaises(TypeError, adapter.getPhysicalRoot)

def test_suite():
    return makeSuite(Test)

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







=== Added File Zope3/src/zope/app/traversing/tests/test_presentation.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
# 
##############################################################################
"""

Revision information:
$Id: test_presentation.py,v 1.1.2.1 2002/12/23 19:32:34 jim Exp $
"""

from unittest import TestCase, TestSuite, main, makeSuite
from zope.app.tests.placelesssetup import PlacelessSetup
from zope.component.view import provideView
from zope.component.resource import provideResource
from zope.app.traversing.presentationnamespaces import view, resource
from zope.exceptions import NotFoundError
from zope.interface import Interface

class IContent(Interface): pass
class IPresentationType(Interface): pass

class Content: __implements__ = IContent

class Resource:

    def __init__(self, request): pass
    __implements__ = IPresentationType

class View:
    __implements__ = IPresentationType
    
    def __init__(self, content, request):
        self.content = content

class Request:

    def getPresentationType(self): return IPresentationType
    def getPresentationSkin(self): return ''
    

class Test(PlacelessSetup, TestCase):

    def testView(self):
        provideView(IContent, 'foo', IPresentationType, [View])

        ob = Content()
        v = view('foo', (), '@@foo', ob, Request())
        self.assertEqual(v.__class__, View)

    def testResource(self):
        provideResource('foo', IPresentationType, Resource)

        ob = Content()
        r = resource('foo', (), '++resource++foo', ob, Request())
        self.assertEqual(r.__class__, Resource)
        

def test_suite():
    return makeSuite(Test)

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


=== Added File Zope3/src/zope/app/traversing/tests/test_skin.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
# 
##############################################################################
"""

Revision information:
$Id: test_skin.py,v 1.1.2.1 2002/12/23 19:32:34 jim Exp $
"""

from unittest import TestCase, TestSuite, main, makeSuite
from zope.testing.cleanup import CleanUp # Base class w registry cleanup
from zope.publisher.browser import TestRequest

class Test(CleanUp, TestCase):

    def test(self):
        from zope.app.traversing.skinnamespace import skin

        request = TestRequest()
        self.assertEqual(request.getPresentationSkin(), '')
        ob = object()
        ob2 = skin('foo', (), '++skin++foo', ob, request)
        self.assertEqual(ob, ob2)
        self.assertEqual(request.getPresentationSkin(), 'foo')

def test_suite():
    return makeSuite(Test)

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


=== Added File Zope3/src/zope/app/traversing/tests/test_traverser.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: test_traverser.py,v 1.1.2.1 2002/12/23 19:32:34 jim Exp $
"""

import unittest
from zope.app.interfaces.traversing.traverser import ITraverser
from zope.app.interfaces.traversing.traversable import ITraversable
from zope.app.traversing.traverser import Traverser
from zope.app.traversing.defaulttraversable import DefaultTraversable

from zope.app.interfaces.traversing.physicallylocatable import IPhysicallyLocatable
from zope.app.interfaces.traversing.containmentroot import IContainmentRoot
from zope.app.traversing.physicallocationadapters \
     import WrapperPhysicallyLocatable, RootPhysicallyLocatable

from zope.proxy.context.context import ContextWrapper
from zope.exceptions import NotFoundError, Unauthorized
from zope.component import getService
from zope.security.securitymanagement \
    import setSecurityPolicy, noSecurityManager

from zope.interface.verify import verifyClass
from zope.interface.implements import instancesOfObjectImplements
from zope.app.services.tests.placefulsetup \
    import PlacefulSetup
from zope.security.checker \
    import ProxyFactory, defineChecker, NamesChecker, CheckerPublic, Checker
from zope.security.securitymanagement import newSecurityManager

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

class TraverserTests(PlacefulSetup, unittest.TestCase):

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

    def testImplementsITraverser(self):
        self.failUnless(ITraverser.isImplementedBy(self.tr))

    def testVerifyInterfaces(self):
        for interface in instancesOfObjectImplements(Traverser):
            verifyClass(interface, Traverser)

class UnrestrictedNoTraverseTests(unittest.TestCase):
    def setUp(self):
        self.root = root = C('root')
        self.root.__implements__ = IContainmentRoot
        self.folder = folder = C('folder')
        self.item = item = C('item')

        root.folder = folder
        folder.item = item

        self.tr = Traverser(root)

    def testNoTraversable(self):
        self.assertRaises(NotFoundError, self.tr.traverse,
                          'folder')

class UnrestrictedTraverseTests(PlacefulSetup, unittest.TestCase):
    def setUp(self):
        PlacefulSetup.setUp(self)
        # Build up a wrapper chain

        getService(None, "Adapters").provideAdapter(
              None, ITraversable, DefaultTraversable)
        getService(None, "Adapters").provideAdapter(
              None, IPhysicallyLocatable, WrapperPhysicallyLocatable)
        getService(None, "Adapters").provideAdapter(
              IContainmentRoot, IPhysicallyLocatable, RootPhysicallyLocatable)

        self.root = root = C('root')
        self.root.__implements__ = IContainmentRoot
        self.folder = folder = C('folder')
        self.item = item = C('item')

        root.folder = folder
        folder.item = item

        self.tr = Traverser(root)

    def testSimplePathString(self):
        tr = self.tr
        item = self.item

        self.assertEquals(tr.traverse('/folder/item'), item)
        self.assertEquals(tr.traverse('folder/item'), item)
        self.assertEquals(tr.traverse('/folder/item/'), item)

    def testSimplePathUnicode(self):
        tr = self.tr
        item = self.item

        self.assertEquals(tr.traverse(u'/folder/item'), item)
        self.assertEquals(tr.traverse(u'folder/item'), item)
        self.assertEquals(tr.traverse(u'/folder/item/'), item)

    def testSimplePathTuple(self):
        tr = self.tr
        item = self.item

        self.assertEquals(tr.traverse(('', 'folder', 'item')),
                          item)
        self.assertEquals(tr.traverse(('folder', 'item')), item)

    def testComplexPathString(self):
        tr = self.tr
        item = self.item

        self.assertEquals(tr.traverse('/folder/../folder/./item'),
            item)
        self.assertEquals(tr.traverse(
            '/../folder/../../folder/item'), item)
        self.assertEquals(tr.traverse('../../folder/item'), item)

    def testNotFoundDefault(self):
        self.assertEquals(self.tr.traverse('foo', 'notFound'),
            'notFound')

    def testNotFoundNoDefault(self):
        self.assertRaises(NotFoundError, self.tr.traverse, 'foo')

def Denied(*names):

    def check(name):
        if name in names:
            return 'Waaaa'
        return CheckerPublic

    return Checker(check)

class RestrictedTraverseTests(PlacefulSetup, unittest.TestCase):
    _oldPolicy = None
    _deniedNames = ()

    def setUp(self):
        PlacefulSetup.setUp(self)

        getService(None,"Adapters").provideAdapter(
             None, ITraversable, DefaultTraversable)
        getService(None, "Adapters").provideAdapter(
              None, IPhysicallyLocatable, WrapperPhysicallyLocatable)
        getService(None, "Adapters").provideAdapter(
              IContainmentRoot, IPhysicallyLocatable, RootPhysicallyLocatable)

        self.root = root = C('root')
        root.__implements__ = IContainmentRoot
        self.folder = folder = C('folder')
        self.item = item = C('item')

        root.folder = folder
        folder.item = item

        self.tr = Traverser(ProxyFactory(root))

    def testAllAllowed(self):
        defineChecker(C, Checker(lambda name: CheckerPublic)) 
        tr = Traverser(ProxyFactory(self.root))
        item = self.item

        self.assertEquals(tr.traverse(('', 'folder', 'item')), item)
        self.assertEquals(tr.traverse(('folder', 'item')), item)
        
    def testItemDenied(self):
        newSecurityManager('no one')
        defineChecker(C, Denied('item')) 
        tr = Traverser(ProxyFactory(self.root))
        folder = self.folder

        self.assertRaises(Unauthorized, tr.traverse, 
            ('', 'folder', 'item'))
        self.assertRaises(Unauthorized, tr.traverse, 
            ('folder', 'item'))
        self.assertEquals(tr.traverse(('', 'folder')), folder)
        self.assertEquals(tr.traverse(('folder', '..', 'folder')),
                          folder)
        self.assertEquals(tr.traverse(('folder',)), folder)

class DefaultTraversableTests(unittest.TestCase):
    def testImplementsITraversable(self):
        self.failUnless(ITraversable.isImplementedBy(DefaultTraversable(None)))

    def testVerifyInterfaces(self):
        for interface in instancesOfObjectImplements(DefaultTraversable):
            verifyClass(interface, DefaultTraversable)

    def testAttributeTraverse(self):
        root = C('root')
        item = C('item')
        root.item = item
        df = DefaultTraversable(root)

        further = []
        next = df.traverse('item', (), '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', (), 'foo', further)
        self.failUnless(next is foo)
        self.assertEquals(further, [])

    def testNotFound(self):
        df = DefaultTraversable(C('dummy'))

        self.assertRaises(NotFoundError, df.traverse, 'bar', (), 'bar', [])

def test_suite():
    loader = unittest.TestLoader()
    suite = loader.loadTestsFromTestCase(TraverserTests)
    suite.addTest(loader.loadTestsFromTestCase(DefaultTraversableTests))
    suite.addTest(loader.loadTestsFromTestCase(UnrestrictedNoTraverseTests))
    suite.addTest(loader.loadTestsFromTestCase(UnrestrictedTraverseTests))
    suite.addTest(loader.loadTestsFromTestCase(RestrictedTraverseTests))
    return suite

if __name__=='__main__':
    unittest.TextTestRunner().run(test_suite())