[Zope3-checkins] CVS: Zope3/src/zope/component/tests - test_adapter.py:1.1 test_view.py:1.1 test_api.py:1.10

Jeremy Hylton jeremy@zope.com
Tue, 24 Jun 2003 11:29:56 -0400


Update of /cvs-repository/Zope3/src/zope/component/tests
In directory cvs.zope.org:/tmp/cvs-serv20091/src/zope/component/tests

Modified Files:
	test_api.py 
Added Files:
	test_adapter.py test_view.py 
Log Message:
Add getRegisteredMatching() for GlobalViewService.

Move IGlobalViewService to the intefaces module.
Change mangled variables to simple private variables (__ to _).
Reorganize the tests and add tests of getRegisteredMatching() for the view service.


=== Added File Zope3/src/zope/component/tests/test_adapter.py ===
##############################################################################
#
# Copyright (c) 2003 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.
#
##############################################################################

import unittest

from zope.component.adapter import GlobalAdapterService
from zope.interface import Interface

class R1(Interface): pass
class R12(Interface): pass
class R2(R1): pass
class R3(R2): pass
class R4(R3): pass

class P1(Interface): pass
class P2(P1): pass
class P3(P2): pass
class P4(P3): pass

class default_P3: pass
class any_P3: pass
class R2_P3: pass

class GlobalAdapterServiceTests(unittest.TestCase):
    
    def getRegistry(self):
        registry = GlobalAdapterService()

        registry.provideAdapter(None, P3, [default_P3])
        registry.provideAdapter(Interface, P3, [any_P3])
        registry.provideAdapter(R2, P3, [R2_P3])

        return registry
    
    def test_getRegisteredMatching_all(self):
        registry = self.getRegistry()

        got = list(registry.getRegisteredMatching())
        got.sort()
        expect = [
            ('', None, P3, [default_P3]),
            ('', Interface, P3, [any_P3]),
            ('', R2, P3, [R2_P3]),
            ]
        expect.sort()
        self.assertEqual(got, expect)

    def test_getRegisteredMatching_for_R1(self):
        registry = self.getRegistry()

        got = list(registry.getRegisteredMatching(
            for_interfaces = (R1, )
            ))
        got.sort()
        expect = [
            ('', None, P3, [default_P3]),
            ('', Interface, P3, [any_P3]),
            ]
        expect.sort()
        self.assertEqual(got, expect)

    def test_getRegisteredMatching_for_multiple(self):
        registry = self.getRegistry()

        got = list(registry.getRegisteredMatching(
            for_interfaces = (R12, R2)
            ))
        got.sort()
        expect = [
            ('', None, P3, [default_P3]),
            ('', Interface, P3, [any_P3]),
            ('', R2, P3, [R2_P3]),
            ]
        expect.sort()
        self.assertEqual(got, expect)

    def test_getRegisteredMatching_provided_P1(self):
        registry = self.getRegistry()

        got = list(registry.getRegisteredMatching(
            provided_interfaces = (P1, )
            ))

        got.sort()
        expect = [
            ('', None, P3, [default_P3]),
            ('', Interface, P3, [any_P3]),
            ('', R2, P3, [R2_P3]),
            ]
        expect.sort()
        self.assertEqual(got, expect)

    def test_getRegisteredMatching_provided_P2(self):
        registry = self.getRegistry()

        got = list(registry.getRegisteredMatching(
            provided_interfaces = (P3, )
            ))
        got.sort()
        expect = [
            ('', None, P3, [default_P3]),
            ('', Interface, P3, [any_P3]),
            ('', R2, P3, [R2_P3]),
            ]
        expect.sort()
        self.assertEqual(got, expect)

    def test_getRegisteredMatching_for_and_provided_1(self):
        registry = self.getRegistry()

        got = list(registry.getRegisteredMatching(
            for_interfaces = (R4, R12),
            provided_interfaces = (P1, ),
            ))
        got.sort()
        expect = [
            ('', None, P3, [default_P3]),
            ('', Interface, P3, [any_P3]),
            ('', R2, P3, [R2_P3]),
            ]
        expect.sort()
        self.assertEqual(got, expect)

    def test_getRegisteredMatching_for_and_provided_2(self):
        registry = self.getRegistry()

        got = list(registry.getRegisteredMatching(
            for_interfaces = (R4, R12),
            provided_interfaces = (P3, ),
            ))
        got.sort()
        expect = [
            ('', None, P3, [default_P3]),
            ('', Interface, P3, [any_P3]),
            ('', R2, P3, [R2_P3]),
            ]
        expect.sort()
        self.assertEqual(got, expect)

    def test_getRegisteredMatching_for_and_provided_exact(self):
        registry = self.getRegistry()

        got = list(registry.getRegisteredMatching(
            for_interfaces = (R2, ),
            provided_interfaces = (P3, ),
            ))
        got.sort()
        expect = [
            ('', None, P3, [default_P3]),
            ('', Interface, P3, [any_P3]),
            ('', R2, P3, [R2_P3]),
            ]
        expect.sort()
        self.assertEqual(got, expect)

def test_suite():
    return unittest.makeSuite(GlobalAdapterServiceTests)


=== Added File Zope3/src/zope/component/tests/test_view.py ===
##############################################################################
#
# Copyright (c) 2003 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.
#
##############################################################################

from pprint import pprint
import unittest

from zope.component.view import viewService
from zope.interface import Interface

from zope.testing.doctestunit import DocTestSuite

class R1(Interface):
    pass

class R12(R1):
    pass

class P1(Interface):
    pass

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

    def __repr__(self):
        return "Factory(%r)" % self.name

    def __call__(self):
        return

def getRegisteredMatching(**kwargs):
    L = viewService.getRegisteredMatching(**kwargs)
    return pprint([(r.__name__, p.__name__, f, l, n)
                   for r, p, f, l, n in L])

def test_getRegisteredMatching():
    """Try various combinations of arguments to getRegisteredMatching().

    First setup a couple of views.
    
    >>> chain = [Factory("shoe")]
    >>> chain2 = [Factory("glue")]
    >>> viewService.provideView(R1, "Bowser", P1, chain)
    >>> viewService.provideView(R12, "Bowser", P1, chain2)

    Start the tests.
    
    >>> getRegisteredMatching(required_interfaces=[R1])
    [('R1', 'P1', [Factory('shoe')], 'default', 'Bowser')]
    >>> getRegisteredMatching(required_interfaces=[R12])
    [('R1', 'P1', [Factory('shoe')], 'default', 'Bowser'),
     ('R12', 'P1', [Factory('glue')], 'default', 'Bowser')]
    >>> getRegisteredMatching(required_interfaces=[P1])
    []
    >>> getRegisteredMatching(presentation_type=P1)
    [('R1', 'P1', [Factory('shoe')], 'default', 'Bowser'),
     ('R12', 'P1', [Factory('glue')], 'default', 'Bowser')]
    >>> getRegisteredMatching(presentation_type=R1)
    []
    >>> getRegisteredMatching(required_interfaces=[R1], presentation_type=P1)
    [('R1', 'P1', [Factory('shoe')], 'default', 'Bowser')]
    >>> getRegisteredMatching(required_interfaces=[R12], presentation_type=P1)
    [('R12', 'P1', [Factory('glue')], 'default', 'Bowser'),
     ('R1', 'P1', [Factory('shoe')], 'default', 'Bowser')]
    >>> getRegisteredMatching(required_interfaces=[P1], presentation_type=P1)
    []
    >>> getRegisteredMatching(viewName="Bowser")
    [('R1', 'P1', [Factory('shoe')], 'default', 'Bowser'),
     ('R12', 'P1', [Factory('glue')], 'default', 'Bowser')]
    >>> getRegisteredMatching(viewName="Bowser", required_interfaces=[R1])
    [('R1', 'P1', [Factory('shoe')], 'default', 'Bowser')]
    >>> getRegisteredMatching(viewName="Bowser", required_interfaces=[R12])
    [('R1', 'P1', [Factory('shoe')], 'default', 'Bowser'),
     ('R12', 'P1', [Factory('glue')], 'default', 'Bowser')]
    >>> getRegisteredMatching(viewName="Bowser", required_interfaces=[R12],
    ...                       presentation_type=P1)
    [('R12', 'P1', [Factory('glue')], 'default', 'Bowser'),
     ('R1', 'P1', [Factory('shoe')], 'default', 'Bowser')]
    >>> getRegisteredMatching(viewName="Bowser", required_interfaces=[R1],
    ...                       presentation_type=P1)
    [('R1', 'P1', [Factory('shoe')], 'default', 'Bowser')]
    >>> getRegisteredMatching(viewName="Yoshi", required_interfaces=[R1],
    ...                       presentation_type=P1)
    []
    >>> getRegisteredMatching(layer=1)
    []
    >>> getRegisteredMatching(layer="default")
    [('R1', 'P1', [Factory('shoe')], 'default', 'Bowser'),
     ('R12', 'P1', [Factory('glue')], 'default', 'Bowser')]
    >>> getRegisteredMatching(viewName="Bowser", required_interfaces=[R1],
    ...                       presentation_type=P1, layer="default")
    [('R1', 'P1', [Factory('shoe')], 'default', 'Bowser')]
    >>> getRegisteredMatching(viewName="Bowser", required_interfaces=[R1],
    ...                       presentation_type=P1, layer="Default")
    []

    >>> viewService._clear()
    """

def test_suite():
    return DocTestSuite()


=== Zope3/src/zope/component/tests/test_api.py 1.9 => 1.10 ===
--- Zope3/src/zope/component/tests/test_api.py:1.9	Fri Jun  6 15:29:08 2003
+++ Zope3/src/zope/component/tests/test_api.py	Tue Jun 24 11:29:54 2003
@@ -13,15 +13,17 @@
 ##############################################################################
 
 import unittest
-from zope.component.exceptions import ComponentLookupError
+
 from zope.component import getAdapter, queryAdapter
 from zope.component import getNamedAdapter, queryNamedAdapter
 from zope.component import getService
 from zope.component import getUtility, queryUtility
+from zope.component.exceptions import ComponentLookupError
 from zope.component.servicenames import Adapters
+from zope.component.tests.placelesssetup import PlacelessSetup
 from zope.component.tests.request import Request
-from zope.interface import Interface, implements
 
+from zope.interface import Interface, implements
 
 class I1(Interface): pass
 class I2(Interface): pass
@@ -46,23 +48,6 @@
             return Comp(self)
 
 
-class R1(Interface): pass
-class R12(Interface): pass
-class R2(R1): pass
-class R3(R2): pass
-class R4(R3): pass
-
-class P1(Interface): pass
-class P2(P1): pass
-class P3(P2): pass
-class P4(P3): pass
-
-class default_P3: pass
-class any_P3: pass
-class R2_P3: pass
-
-from zope.component.tests.placelesssetup import PlacelessSetup
-
 class Test(PlacelessSetup, unittest.TestCase):
 
     def testAdapter_via_conform(self):
@@ -311,144 +296,8 @@
                           viewService.getDefaultViewName,
                           ob, Request(I1))
 
-    # The following tests are copied from
-    # Interface.Registry.tests.IAdapterRegistry
-
-    def __registery(self):
-        from zope.component.adapter import GlobalAdapterService
-
-        registry = GlobalAdapterService()
-
-
-        registry.provideAdapter(None, P3, [default_P3])
-        registry.provideAdapter(Interface, P3, [any_P3])
-        registry.provideAdapter(R2, P3, [R2_P3])
-
-        return registry
-
-    def test_getRegisteredMatching_all(self):
-        registry = self.__registery()
-
-        got = list(registry.getRegisteredMatching())
-        got.sort()
-        expect = [
-            ('', None, P3, [default_P3]),
-            ('', Interface, P3, [any_P3]),
-            ('', R2, P3, [R2_P3]),
-            ]
-        expect.sort()
-        self.assertEqual(got, expect)
-
-    def test_getRegisteredMatching_for_R1(self):
-        registry = self.__registery()
-
-        got = list(registry.getRegisteredMatching(
-            for_interfaces = (R1, )
-            ))
-        got.sort()
-        expect = [
-            ('', None, P3, [default_P3]),
-            ('', Interface, P3, [any_P3]),
-            ]
-        expect.sort()
-        self.assertEqual(got, expect)
-
-    def test_getRegisteredMatching_for_multiple(self):
-        registry = self.__registery()
-
-        got = list(registry.getRegisteredMatching(
-            for_interfaces = (R12, R2)
-            ))
-        got.sort()
-        expect = [
-            ('', None, P3, [default_P3]),
-            ('', Interface, P3, [any_P3]),
-            ('', R2, P3, [R2_P3]),
-            ]
-        expect.sort()
-        self.assertEqual(got, expect)
-
-    def test_getRegisteredMatching_provided_P1(self):
-        registry = self.__registery()
-
-        got = list(registry.getRegisteredMatching(
-            provided_interfaces = (P1, )
-            ))
-        got.sort()
-        expect = [
-            ('', None, P3, [default_P3]),
-            ('', Interface, P3, [any_P3]),
-            ('', R2, P3, [R2_P3]),
-            ]
-        expect.sort()
-        self.assertEqual(got, expect)
-
-    def test_getRegisteredMatching_provided_P2(self):
-        registry = self.__registery()
-
-        got = list(registry.getRegisteredMatching(
-            provided_interfaces = (P3, )
-            ))
-        got.sort()
-        expect = [
-            ('', None, P3, [default_P3]),
-            ('', Interface, P3, [any_P3]),
-            ('', R2, P3, [R2_P3]),
-            ]
-        expect.sort()
-        self.assertEqual(got, expect)
-
-    def test_getRegisteredMatching_for_and_provided_1(self):
-        registry = self.__registery()
-
-        got = list(registry.getRegisteredMatching(
-            for_interfaces = (R4, R12),
-            provided_interfaces = (P1, ),
-            ))
-        got.sort()
-        expect = [
-            ('', None, P3, [default_P3]),
-            ('', Interface, P3, [any_P3]),
-            ('', R2, P3, [R2_P3]),
-            ]
-        expect.sort()
-        self.assertEqual(got, expect)
-
-    def test_getRegisteredMatching_for_and_provided_2(self):
-        registry = self.__registery()
-
-        got = list(registry.getRegisteredMatching(
-            for_interfaces = (R4, R12),
-            provided_interfaces = (P3, ),
-            ))
-        got.sort()
-        expect = [
-            ('', None, P3, [default_P3]),
-            ('', Interface, P3, [any_P3]),
-            ('', R2, P3, [R2_P3]),
-            ]
-        expect.sort()
-        self.assertEqual(got, expect)
-
-    def test_getRegisteredMatching_for_and_provided_exact(self):
-        registry = self.__registery()
-
-        got = list(registry.getRegisteredMatching(
-            for_interfaces = (R2, ),
-            provided_interfaces = (P3, ),
-            ))
-        got.sort()
-        expect = [
-            ('', None, P3, [default_P3]),
-            ('', Interface, P3, [any_P3]),
-            ('', R2, P3, [R2_P3]),
-            ]
-        expect.sort()
-        self.assertEqual(got, expect)
-
 def test_suite():
-    loader = unittest.TestLoader()
-    return loader.loadTestsFromTestCase(Test)
+    return unittest.makeSuite(Test)
 
-if __name__ == '__main__':
+if __name__ == "__main__":
     unittest.TextTestRunner().run(test_suite())