[Checkins] SVN: zope.component/tseaver-test_cleanup/ Split out tests for zope.component.interface.

Tres Seaver cvs-admin at zope.org
Thu Jun 21 21:03:44 UTC 2012


Log message for revision 126991:
  Split out tests for zope.component.interface.
  
  Move examples to Sphinx docs.

Changed:
  _U  zope.component/tseaver-test_cleanup/
  A   zope.component/tseaver-test_cleanup/docs/api/interface.rst
  U   zope.component/tseaver-test_cleanup/docs/api.rst
  U   zope.component/tseaver-test_cleanup/src/zope/component/interface.py
  U   zope.component/tseaver-test_cleanup/src/zope/component/tests/test_doctests.py
  A   zope.component/tseaver-test_cleanup/src/zope/component/tests/test_interface.py

-=-
Added: zope.component/tseaver-test_cleanup/docs/api/interface.rst
===================================================================
--- zope.component/tseaver-test_cleanup/docs/api/interface.rst	                        (rev 0)
+++ zope.component/tseaver-test_cleanup/docs/api/interface.rst	2012-06-21 21:03:41 UTC (rev 126991)
@@ -0,0 +1,184 @@
+Interface Registration APIs
+===========================
+
+.. testsetup::
+
+   from zope.component.testing import setUp
+   setUp()
+
+Registering Interfaces as Utilities
+-----------------------------------
+
+.. autofunction:: zope.component.interface.provideInterface
+
+We can register a given interface with the global site manager as a utility.
+First, declare a new interface, which itself provides only the core API,
+:class:`zope.interface.interfaces.IInterface`:
+
+.. doctest::
+
+   >>> from zope.interface import Interface
+   >>> from zope.interface.interfaces import IInterface
+   >>> from zope.component.tests.test_doctests import ITestType
+   >>> from zope.component import getGlobalSiteManager
+   >>> gsm = getGlobalSiteManager()
+
+   >>> class IDemo(Interface):
+   ...     pass
+   >>> IInterface.providedBy(IDemo)
+   True
+   >>> ITestType.providedBy(IDemo)
+   False
+   >>> list(gsm.getUtilitiesFor(ITestType))
+   []
+
+Now, register ``IDemo`` as providing ``ITestType``
+
+.. doctest::
+
+   >>> from zope.component.interface import provideInterface
+   >>> provideInterface('', IDemo, ITestType)
+   >>> ITestType.providedBy(IDemo)
+   True
+   >>> interfaces = list(gsm.getUtilitiesFor(ITestType))
+   >>> [iface.__name__ for (name, iface) in interfaces]
+   ['IDemo']
+
+We can register ``IDemo`` as providing more than one interface:
+
+.. doctest::
+
+   >>> class IOtherType(IInterface):
+   ...     pass
+   >>> provideInterface('', IDemo, IOtherType)
+   >>> ITestType.providedBy(IDemo)
+   True
+   >>> IOtherType.providedBy(IDemo)
+   True
+   >>> interfaces = list(gsm.getUtilitiesFor(ITestType))
+   >>> [iface.__name__ for (name, iface) in interfaces]
+   ['IDemo']
+   >>> interfaces = list(gsm.getUtilitiesFor(IOtherType))
+   >>> [iface.__name__ for (name, iface) in interfaces]
+   ['IDemo']
+
+.. testcleanup::
+
+   from zope.component.testing import tearDown
+   tearDown()
+
+
+.. autofunction:: zope.component.interface.getInterface
+
+.. doctest::
+
+   >>> from zope.interface import Interface
+   >>> from zope.component.interface import getInterface
+   >>> from zope.component.tests.test_doctests import ITestType
+   >>> from zope.component.tests.test_doctests import IGI
+
+   >>> IInterface.providedBy(IGI)
+   True
+   >>> ITestType.providedBy(IGI)
+   False
+   >>> getInterface(None, 'zope.component.tests.test_doctests.IGI')
+   Traceback (most recent call last):
+   ...
+   ComponentLookupError: zope.component.tests.test_doctests.interface.IGI
+   >>> provideInterface('', IGI, ITestType)
+   >>> ITestType.providedBy(IGI)
+   True
+   >>> iface = getInterface(None,
+   ...                      'zope.component.tests.test_doctests.IGI')
+   >>> iface.__name__
+   'IGI'
+
+.. testcleanup::
+
+   from zope.component.testing import tearDown
+   tearDown()
+
+
+.. autofunction:: zope.component.interface.queryInterface
+
+.. doctest::
+
+   >>> from zope.interface import Interface
+   >>> from zope.interface.interfaces import IInterface
+   >>> from zope.component.interface import queryInterface
+   >>> from zope.component.tests.test_doctests import ITestType
+   >>> from zope.component.tests.test_doctests import IQI
+
+   >>> IInterface.providedBy(IQI)
+   True
+   >>> ITestType.providedBy(IQI)
+   False
+   >>> queryInterface('zope.component.tests.test_doctests.IQI') is None
+   True
+   
+   >>> provideInterface('', IQI, ITestType)
+   >>> ITestType.providedBy(IQI)
+   True
+   >>> iface = queryInterface('zope.component.tests.test_doctests.IQI')
+   >>> iface.__name__
+   'IQI'
+
+.. testcleanup::
+
+   from zope.component.testing import tearDown
+   tearDown()
+
+
+.. autofunction:: zope.component.interface.searchInterface
+
+.. doctest::
+
+    >>> from zope.interface import Interface
+    >>> from zope.interface.interfaces import IInterface
+    >>> from zope.component.interface import searchInterface
+    >>> from zope.component.tests.test_doctests import ITestType
+    >>> from zope.component.tests.test_doctests import ISI
+
+    >>> IInterface.providedBy(ISI)
+    True
+    >>> ITestType.providedBy(ISI)
+    False
+    >>> searchInterface(None, 'zope.component.tests.test_doctests.ISI')
+    []
+    >>> provideInterface('', ISI, ITestType)
+    >>> ITestType.providedBy(ISI)
+    True
+    >>> searchInterface(None, 'zope.component.tests.test_doctests.ISI') == [ISI]
+    True
+
+.. testcleanup::
+
+   from zope.component.testing import tearDown
+   tearDown()
+
+.. autofunction:: zope.component.interface.searchInterfaceIds
+
+.. doctest::
+
+   >>> from zope.interface import Interface
+   >>> from zope.interface.interfaces import IInterface
+   >>> from zope.component.interface import searchInterfaceIds
+   >>> from zope.component.tests.test_doctests import ITestType
+   >>> from zope.component.tests.test_doctests import ISII
+
+   >>> IInterface.providedBy(ISII)
+   True
+   >>> ITestType.providedBy(ISII)
+   False
+   >>> searchInterfaceIds(None, 'zope.component.tests.test_doctests.ISII')
+   []
+   >>> provideInterface('', ISII, ITestType)
+   >>> ITestType.providedBy(ISII)
+   True
+   >>> searchInterfaceIds(None, 'zope.component.tests.test_doctests.ISII')
+   [u'zope.component.tests.test_doctests.ISII']
+
+.. testcleanup::
+
+   from zope.component.testing import tearDown
+   tearDown()

Modified: zope.component/tseaver-test_cleanup/docs/api.rst
===================================================================
--- zope.component/tseaver-test_cleanup/docs/api.rst	2012-06-21 21:03:37 UTC (rev 126990)
+++ zope.component/tseaver-test_cleanup/docs/api.rst	2012-06-21 21:03:41 UTC (rev 126991)
@@ -10,3 +10,4 @@
    api/utility
    api/adapter
    api/factory
+   api/interface

Modified: zope.component/tseaver-test_cleanup/src/zope/component/interface.py
===================================================================
--- zope.component/tseaver-test_cleanup/src/zope/component/interface.py	2012-06-21 21:03:37 UTC (rev 126990)
+++ zope.component/tseaver-test_cleanup/src/zope/component/interface.py	2012-06-21 21:03:41 UTC (rev 126991)
@@ -23,64 +23,7 @@
 from zope.interface.interfaces import IInterface
 
 def provideInterface(id, interface, iface_type=None, info=''):
-    """register Interface with global site manager as utility
-
-    >>> from zope.interface import Interface
-    >>> from zope.interface.interfaces import IInterface
-    >>> from zope.component.testing import setUp, tearDown
-    >>> from zope.component.tests.test_doctests import ITestType
-    >>> setUp()
-    >>> gsm = zope.component.getGlobalSiteManager()
-
-    >>> class I(Interface):
-    ...     pass
-    >>> IInterface.providedBy(I)
-    True
-    >>> ITestType.providedBy(I)
-    False
-    >>> interfaces = gsm.getUtilitiesFor(ITestType)
-    >>> list(interfaces)
-    []
-
-    # provide first interface type
-    >>> provideInterface('', I, ITestType)
-    >>> ITestType.providedBy(I)
-    True
-    >>> interfaces = list(gsm.getUtilitiesFor(ITestType))
-    >>> [name for (name, iface) in interfaces]
-    [u'zope.component.interface.I']
-    >>> [iface.__name__ for (name, iface) in interfaces]
-    ['I']
-
-    # provide second interface type
-    >>> class IOtherType(IInterface):
-    ...     pass
-    >>> provideInterface('', I, IOtherType)
-
-    >>> ITestType.providedBy(I)
-    True
-    >>> IOtherType.providedBy(I)
-    True
-    >>> interfaces = list(gsm.getUtilitiesFor(ITestType))
-    >>> [name for (name, iface) in interfaces]
-    [u'zope.component.interface.I']
-    >>> interfaces = list(gsm.getUtilitiesFor(IOtherType))
-    >>> [name for (name, iface) in interfaces]
-    [u'zope.component.interface.I']
-
-    >>> class I1(Interface):
-    ...     pass
-    >>> provideInterface('', I1)
-    >>> IInterface.providedBy(I1)
-    True
-    >>> ITestType.providedBy(I1)
-    False
-    >>> interfaces = list(gsm.getUtilitiesFor(ITestType))
-    >>> [name for (name, iface) in interfaces]
-    [u'zope.component.interface.I']
-    >>> [iface.__name__ for (name, iface) in interfaces]
-    ['I']
-    >>> tearDown()
+    """ Mark 'interface' as a named utilty providing 'iface_type'.
     """
     if not id:
         id = "%s.%s" % (interface.__module__, interface.__name__)
@@ -103,30 +46,6 @@
 
 def getInterface(context, id):
     """Return interface or raise ComponentLookupError
-
-    >>> from zope.interface import Interface
-    >>> from zope.component.testing import setUp, tearDown
-    >>> from zope.component.tests.test_doctests import ITestType
-    >>> setUp()
-
-    >>> class I4(Interface):
-    ...     pass
-    >>> IInterface.providedBy(I4)
-    True
-    >>> ITestType.providedBy(I4)
-    False
-    >>> getInterface(None, 'zope.component.interface.I4')
-    Traceback (most recent call last):
-    ...
-    ComponentLookupError: zope.component.interface.I4
-    >>> provideInterface('', I4, ITestType)
-    >>> ITestType.providedBy(I4)
-    True
-    >>> iface = queryInterface( """\
-                """ 'zope.component.interface.I4')
-    >>> iface.__name__
-    'I4'
-    >>> tearDown()
     """
     iface = queryInterface(id, None)
     if iface is None:
@@ -135,57 +54,13 @@
 
 
 def queryInterface(id, default=None):
-    """return interface or ``None``
-
-    >>> from zope.interface import Interface
-    >>> from zope.interface.interfaces import IInterface
-    >>> from zope.component.testing import setUp, tearDown
-    >>> from zope.component.tests.test_doctests import ITestType
-    >>> setUp()
-
-    >>> class I3(Interface):
-    ...     pass
-    >>> IInterface.providedBy(I3)
-    True
-    >>> ITestType.providedBy(I3)
-    False
-    >>> queryInterface('zope.component.interface.I3')
-    
-    >>> provideInterface('', I3, ITestType)
-    >>> ITestType.providedBy(I3)
-    True
-    >>> iface = queryInterface('zope.component.interface.I3')
-    >>> iface.__name__
-    'I3'
-    >>> tearDown()
+    """Return an interface or ``None``
     """
     return zope.component.queryUtility(IInterface, id, default)
 
 
 def searchInterface(context, search_string=None, base=None):
     """Interfaces search
-
-    >>> from zope.interface import Interface
-    >>> from zope.interface.interfaces import IInterface
-    >>> from zope.component.testing import setUp, tearDown
-    >>> from zope.component.tests.test_doctests import ITestType
-    >>> setUp()
-
-    >>> class I5(Interface):
-    ...     pass
-    >>> IInterface.providedBy(I5)
-    True
-    >>> ITestType.providedBy(I5)
-    False
-    >>> searchInterface(None, 'zope.component.interface.I5')
-    []
-    >>> provideInterface('', I5, ITestType)
-    >>> ITestType.providedBy(I5)
-    True
-    >>> iface = searchInterface(None, 'zope.component.interface.I5')
-    >>> iface[0].__name__
-    'I5'
-    >>> tearDown()
     """
     return [iface_util[1] for iface_util in
             searchInterfaceUtilities(context, search_string, base)]
@@ -193,28 +68,6 @@
 
 def searchInterfaceIds(context, search_string=None, base=None):
     """Interfaces search
-
-    >>> from zope.interface import Interface
-    >>> from zope.interface.interfaces import IInterface
-    >>> from zope.component.testing import setUp, tearDown
-    >>> from zope.component.tests.test_doctests import ITestType
-    >>> setUp()
-
-    >>> class I5(Interface):
-    ...     pass
-    >>> IInterface.providedBy(I5)
-    True
-    >>> ITestType.providedBy(I5)
-    False
-    >>> searchInterface(None, 'zope.component.interface.I5')
-    []
-    >>> provideInterface('', I5, ITestType)
-    >>> ITestType.providedBy(I5)
-    True
-    >>> iface = searchInterfaceIds(None, 'zope.component.interface.I5')
-    >>> iface
-    [u'zope.component.interface.I5']
-    >>> tearDown()
     """
     return [iface_util[0] for iface_util in
             searchInterfaceUtilities(context, search_string, base)]

Modified: zope.component/tseaver-test_cleanup/src/zope/component/tests/test_doctests.py
===================================================================
--- zope.component/tseaver-test_cleanup/src/zope/component/tests/test_doctests.py	2012-06-21 21:03:37 UTC (rev 126990)
+++ zope.component/tseaver-test_cleanup/src/zope/component/tests/test_doctests.py	2012-06-21 21:03:41 UTC (rev 126991)
@@ -41,6 +41,21 @@
 class I3(Interface):
     pass
 
+class I4(Interface):
+    pass
+
+class IGI(Interface):
+    pass
+
+class IQI(Interface):
+    pass
+
+class ISI(Interface):
+    pass
+
+class ISII(Interface):
+    pass
+
 def noop(*args):
     pass
 
@@ -122,8 +137,6 @@
 def test_suite():
     import doctest
     return unittest.TestSuite((
-        doctest.DocTestSuite('zope.component.interface',
-                             setUp=setUp, tearDown=tearDown),
         doctest.DocTestSuite('zope.component.nexttesting'),
         unittest.makeSuite(StandaloneTests),
         ))

Added: zope.component/tseaver-test_cleanup/src/zope/component/tests/test_interface.py
===================================================================
--- zope.component/tseaver-test_cleanup/src/zope/component/tests/test_interface.py	                        (rev 0)
+++ zope.component/tseaver-test_cleanup/src/zope/component/tests/test_interface.py	2012-06-21 21:03:41 UTC (rev 126991)
@@ -0,0 +1,54 @@
+##############################################################################
+#
+# Copyright (c) 2012 Zope Foundation and Contributors.
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.1 (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.
+#
+##############################################################################
+"""Tests for z.c.interface
+"""
+import unittest
+
+
+class Test_provideInterface(unittest.TestCase):
+
+    def _callFUT(self, *args, **kw):
+        from zope.component.interface import provideInterface
+        return provideInterface(*args, **kw)
+
+
+class Test_getInterface(unittest.TestCase):
+
+    def _callFUT(self, *args, **kw):
+        from zope.component.interface import getInterface
+        return getInterface(*args, **kw)
+
+
+class Test_queryInterface(unittest.TestCase):
+
+    def _callFUT(self, *args, **kw):
+        from zope.component.interface import queryInterface
+        return queryInterface(*args, **kw)
+
+
+class Test_searchInterface(unittest.TestCase):
+
+    def _callFUT(self, *args, **kw):
+        from zope.component.interface import searchInterface
+        return searchInterface(*args, **kw)
+
+
+def test_suite():
+    return unittest.TestSuite((
+        unittest.makeSuite(Test_provideInterface),
+        unittest.makeSuite(Test_getInterface),
+        unittest.makeSuite(Test_queryInterface),
+        unittest.makeSuite(Test_searchInterface),
+    ))
+



More information about the checkins mailing list