[Checkins] SVN: zope.component/tseaver-test_cleanup/ 100% coverage for z.c._api.
Tres Seaver
cvs-admin at zope.org
Fri Jun 22 17:09:11 UTC 2012
Log message for revision 127036:
100% coverage for z.c._api.
Changed:
_U zope.component/tseaver-test_cleanup/
U zope.component/tseaver-test_cleanup/src/zope/component/_api.py
U zope.component/tseaver-test_cleanup/src/zope/component/tests/test__api.py
-=-
Modified: zope.component/tseaver-test_cleanup/src/zope/component/_api.py
===================================================================
--- zope.component/tseaver-test_cleanup/src/zope/component/_api.py 2012-06-22 17:09:04 UTC (rev 127035)
+++ zope.component/tseaver-test_cleanup/src/zope/component/_api.py 2012-06-22 17:09:08 UTC (rev 127036)
@@ -215,13 +215,23 @@
# Factories
def createObject(__factory_name, *args, **kwargs):
+ """Invoke the named factory and return the result.
+
+ ``__factory_name`` is a positional-only argument.
+ """
context = kwargs.pop('context', None)
return getUtility(IFactory, __factory_name, context)(*args, **kwargs)
def getFactoryInterfaces(name, context=None):
+ """Return the interface provided by the named factory's objects
+
+ Result might be a single interface. XXX
+ """
return getUtility(IFactory, name, context).getInterfaces()
def getFactoriesFor(interface, context=None):
+ """Return info on all factories implementing the given interface.
+ """
utils = getSiteManager(context)
for (name, factory) in utils.getUtilitiesFor(IFactory):
interfaces = factory.getInterfaces()
Modified: zope.component/tseaver-test_cleanup/src/zope/component/tests/test__api.py
===================================================================
--- zope.component/tseaver-test_cleanup/src/zope/component/tests/test__api.py 2012-06-22 17:09:04 UTC (rev 127035)
+++ zope.component/tseaver-test_cleanup/src/zope/component/tests/test__api.py 2012-06-22 17:09:08 UTC (rev 127036)
@@ -1013,7 +1013,141 @@
is custom_util)
self.assertTrue(self._callFUT(sm1, IMyUtility, 'myutil') is gutil)
+ def test_wo_sitemanager(self):
+ from zope.interface import Interface
+ from zope.component.interfaces import ComponentLookupError
+ class IFoo(Interface):
+ pass
+ class Context(object):
+ def __conform__(self, iface):
+ raise ComponentLookupError
+ self.assertEqual(self._callFUT(Context(), IFoo, 'myutil'), None)
+
+class Test_createObject(unittest.TestCase):
+
+ from zope.component.testing import setUp, tearDown
+
+ def _callFUT(self, *args, **kw):
+ from zope.component import createObject
+ return createObject(*args, **kw)
+
+ def test_miss(self):
+ from zope.component.interfaces import ComponentLookupError
+ self.assertRaises(ComponentLookupError, self._callFUT, 'nonesuch')
+
+ def test_hit(self):
+ from zope.component.interfaces import IFactory
+ _object = object()
+ _factory_called = []
+ def _factory(*args, **kw):
+ _factory_called.append((args, kw))
+ return _object
+ class Context(object):
+ def __conform__(self, iface):
+ return self
+ def queryUtility(self, iface, name, default):
+ if iface is IFactory and name == 'test':
+ return _factory
+ return default
+ context = Context()
+ self.assertTrue(self._callFUT('test', context=context) is _object)
+ self.assertEqual(_factory_called, [((), {})])
+
+
+class Test_getFactoryInterfaces(unittest.TestCase):
+
+ from zope.component.testing import setUp, tearDown
+
+ def _callFUT(self, *args, **kw):
+ from zope.component import getFactoryInterfaces
+ return getFactoryInterfaces(*args, **kw)
+
+ def test_miss(self):
+ from zope.component.interfaces import ComponentLookupError
+ self.assertRaises(ComponentLookupError, self._callFUT, 'nonesuch')
+
+ def test_hit(self):
+ from zope.component.interfaces import IFactory
+ from zope.interface import Interface
+ class IFoo(Interface):
+ pass
+ class _Factory(object):
+ def getInterfaces(self):
+ return [IFoo]
+ class Context(object):
+ def __conform__(self, iface):
+ return self
+ def queryUtility(self, iface, name, default):
+ if iface is IFactory and name == 'test':
+ return _Factory()
+ return default
+ context = Context()
+ self.assertEqual(self._callFUT('test', context=context), [IFoo])
+
+
+class Test_getFactoriesFor(unittest.TestCase):
+
+ from zope.component.testing import setUp, tearDown
+
+ def _callFUT(self, *args, **kw):
+ from zope.component import getFactoriesFor
+ return getFactoriesFor(*args, **kw)
+
+ def test_no_factories_registered(self):
+ from zope.interface import Interface
+ class IFoo(Interface):
+ pass
+ self.assertEqual(list(self._callFUT(IFoo)), [])
+
+ def test_w_factory_returning_spec(self):
+ from zope.interface import Interface
+ from zope.interface import implementer
+ from zope.interface import providedBy
+ from zope.component.interfaces import IFactory
+ class IFoo(Interface):
+ pass
+ class IBar(Interface):
+ pass
+ @implementer(IFoo, IBar)
+ class _Factory(object):
+ def getInterfaces(self):
+ return providedBy(self)
+ _factory = _Factory()
+ class Context(object):
+ def __conform__(self, iface):
+ return self
+ def getUtilitiesFor(self, iface):
+ if iface is IFactory:
+ return [('test', _factory)]
+ self.assertEqual(list(self._callFUT(IFoo, context=Context())),
+ [('test', _factory)])
+ self.assertEqual(list(self._callFUT(IBar, context=Context())),
+ [('test', _factory)])
+
+ def test_w_factory_returning_list_of_interfaces(self):
+ from zope.interface import Interface
+ from zope.component.interfaces import IFactory
+ class IFoo(Interface):
+ pass
+ class IBar(Interface):
+ pass
+ class _Factory(object):
+ def getInterfaces(self):
+ return [IFoo, IBar]
+ _factory = _Factory()
+ class Context(object):
+ def __conform__(self, iface):
+ return self
+ def getUtilitiesFor(self, iface):
+ if iface is IFactory:
+ return [('test', _factory)]
+ self.assertEqual(list(self._callFUT(IFoo, context=Context())),
+ [('test', _factory)])
+ self.assertEqual(list(self._callFUT(IBar, context=Context())),
+ [('test', _factory)])
+
+
IMyUtility = None
def _makeMyUtility(name, sm):
global IMyUtility
@@ -1052,5 +1186,8 @@
unittest.makeSuite(Test_getAllUtilitiesRegisteredFor),
unittest.makeSuite(Test_getNextUtility),
unittest.makeSuite(Test_queryNextUtility),
+ unittest.makeSuite(Test_createObject),
+ unittest.makeSuite(Test_getFactoryInterfaces),
+ unittest.makeSuite(Test_getFactoriesFor),
))
More information about the checkins
mailing list