[Zope-Checkins] CVS: Zope3/lib/python/Zope/ComponentArchitecture/tests - __init__.py:1.1.2.1 testAPI.py:1.1.2.1 testIToIRegistry.py:1.1.2.1

Jim Fulton jim@zope.com
Fri, 16 Nov 2001 16:13:24 -0500


Update of /cvs-repository/Zope3/lib/python/Zope/ComponentArchitecture/tests
In directory cvs.zope.org:/tmp/cvs-serv6113/Zope/ComponentArchitecture/tests

Added Files:
      Tag: Zope-3x-branch
	__init__.py testAPI.py testIToIRegistry.py 
Log Message:
Component Architecture

=== Added File Zope3/lib/python/Zope/ComponentArchitecture/tests/__init__.py ===


=== Added File Zope3/lib/python/Zope/ComponentArchitecture/tests/testAPI.py ===

import unittest, sys, Interface

class I1(Interface.Interface): pass
class I2(Interface.Interface): pass
class Comp:
    def __init__(self, context): self._context=context
    def getContext(self): return self._context

comp=Comp(1)

class Ob:
    __implements__=I1

ob=Ob()

class Test(unittest.TestCase):

    def testFeature(self):
        from Zope.ComponentArchitecture import getFeature, provideFeature

        self.assertEquals(getFeature(ob, I1), ob)
        self.assertEquals(getFeature(ob, I2), None)
        self.assertEquals(getFeature(ob, I2, Test), Test)
        
        provideFeature(I1, I2, Comp)
        c=getFeature(ob, I2)
        self.assertEquals(c.__class__, Comp)
        self.assertEquals(c.getContext(), ob)

    def testUtility(self):
        from Zope.ComponentArchitecture import getUtility, provideUtility

        self.assertNotEquals(getUtility(ob, I1), ob)
        self.assertEquals(getUtility(ob, I2), None)
        self.assertEquals(getUtility(ob, I2, Test), Test)
        
        provideUtility(I2, comp)
        self.assertEquals(id(getUtility(ob, I2)), id(comp))

    def testPresentation(self):
        from Zope.ComponentArchitecture import providePresentation
        from Zope.ComponentArchitecture import getPresentation

        self.assertNotEquals(getPresentation(ob, 'foo', I1), ob)
        self.assertEquals(getPresentation(ob, 'foo', I2), None)
        self.assertEquals(getPresentation(ob, 'foo', I2, Test), Test)
        
        providePresentation(I1, 'foo', I2, Comp)
        c=getPresentation(ob, 'foo', I2)
        self.assertEquals(c.__class__, Comp)
        self.assertEquals(c.getContext(), ob)

        self.assertNotEquals(getPresentation(ob, 'foo2', I1), ob)
        self.assertEquals(getPresentation(ob, 'foo2', I2), None)
        self.assertEquals(getPresentation(ob, 'foo2', I2, Test), Test)

def test_suite():
    loader=unittest.TestLoader()
    return loader.loadTestsFromTestCase(Test)

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


=== Added File Zope3/lib/python/Zope/ComponentArchitecture/tests/testIToIRegistry.py ===

import unittest, sys

class Test(unittest.TestCase):

    def testItAll(self):
        # shane's tests

        import Interface
        from Zope.ComponentArchitecture.IToIRegistry import IToIRegistry

        class I1(Interface.Base): pass
        class I2(I1): pass
        class I3(I2): pass
        class I4(I3): pass
        class I5(I4): pass
        class I6(I5): pass

        class NamedClass:
            def name(self):
                return self.__class__.__name__

        class A2(NamedClass): __implements__ = I2  # Adapts I1 or I2
        class A4(NamedClass): __implements__ = I4  # Adapts I1, I2, I3, or I4
        class A6(NamedClass): __implements__ = I6  # Adapts I1 through I6

        class RequiredInterface (Interface.Base): pass
        class InputI (RequiredInterface): pass

        reg = IToIRegistry()
        reg.register(RequiredInterface, I2, A2())
        reg.register(RequiredInterface, I4, A4())
        reg.register(RequiredInterface, I6, A6())

        assert reg.get(InputI, I1).name() == 'A2'
        assert reg.get(InputI, I2).name() == 'A2'
        assert reg.get(InputI, I3).name() == 'A4'
        assert reg.get(InputI, I4).name() == 'A4'
        assert reg.get(InputI, I5).name() == 'A6'
        assert reg.get(InputI, I6).name() == 'A6'

        # Be sure that it doesn't matter in what order you register
        # non-conflicting adapters.
        reg = IToIRegistry()
        reg.register(RequiredInterface, I6, A6())
        reg.register(RequiredInterface, I4, A4())
        reg.register(RequiredInterface, I2, A2())

        assert reg.get(InputI, I1).name() == 'A2'
        assert reg.get(InputI, I2).name() == 'A2'
        assert reg.get(InputI, I3).name() == 'A4'
        assert reg.get(InputI, I4).name() == 'A4'
        assert reg.get(InputI, I5).name() == 'A6'
        assert reg.get(InputI, I6).name() == 'A6'
        

def test_suite():
    loader=unittest.TestLoader()
    return loader.loadTestsFromTestCase(Test)

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