Skip to content

Commit

Permalink
100% coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
jamadden committed Jun 29, 2017
1 parent 43d7351 commit 79b5fd2
Show file tree
Hide file tree
Showing 21 changed files with 181 additions and 306 deletions.
1 change: 1 addition & 0 deletions .coveragerc
Expand Up @@ -7,3 +7,4 @@ exclude_lines =
pragma NO COVER
if __name__ == '__main__':
raise NotImplementedError
self.fail
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -14,3 +14,4 @@ coverage.xml
.coverage
dist/
.eggs/
htmlcov/
4 changes: 2 additions & 2 deletions .travis.yml
Expand Up @@ -3,7 +3,7 @@ sudo: false
matrix:
include:
- python: 2.7
env: MINIMAL="-t !persistentregistry -t !security"
env: MINIMAL=1
python:
- 2.7
- 3.4
Expand All @@ -12,7 +12,7 @@ python:
- pypy-5.6.0

script:
- coverage run -m zope.testrunner --test-path=src $MINIMAL
- coverage run -m zope.testrunner --test-path=src
- if [[ -z "$MINIMAL" ]]; then sphinx-build -b html -d docs/_build/doctrees docs docs/_build/html; fi
- if [[ -z "$MINIMAL" ]]; then coverage run -a `which sphinx-build` -b doctest -d docs/_build/doctrees docs docs/_build/doctest; fi

Expand Down
12 changes: 8 additions & 4 deletions docs/testlayer.rst
Expand Up @@ -38,7 +38,7 @@ need to, as these methods do nothing at all, but we just ensure that
they are there in the first place.

Let's instantiate our layer. We need to supply it with the package the
layer is defined in:
layer is defined in, and we can provide it with ZCML features to enable:

.. doctest::

Expand Down Expand Up @@ -83,17 +83,21 @@ is ``ftesting.zcml``, but here we'll load a test ``testlayer.zcml``.
>>> import zope.component.testfiles
>>> zcml_file_layer = ZCMLFileLayer(
... zope.component.testfiles,
... 'testlayer.zcml')
... 'testlayer.zcml',
... features=["devmode"])

>>> class TestCase(unittest.TestCase):
... layer = zcml_file_layer
...
... def testFoo(self):
... # The feature was registered
... self.assertTrue(self.layer.context.hasFeature('devmode'))
... # we should now have the adapter registered
... from zope import component
... from zope.component.testfiles import components
... self.assert_(isinstance(
... components.IApp2(components.content), components.Comp2))
... self.assertIsInstance(
... components.IApp2(components.content), components.Comp2)


Since the ZCML sets up an adapter, we expect the tests to pass:

Expand Down
9 changes: 5 additions & 4 deletions src/zope/component/standalonetests.py
@@ -1,13 +1,14 @@
"""
See: https://bugs.launchpad.net/zope3/+bug/98401
"""
import sys
import pickle

def write(x): # pragma: NO COVER
import sys # pragma: no cover (runs in subprocess)
import pickle # pragma: no cover (runs in subprocess)

def write(x):
sys.stdout.write('%s\n' % x)

if __name__ == "__main__": #pragma NO COVER (runs in subprocess)
if __name__ == "__main__": # pragma: no cover (runs in subprocess)
if sys.version_info[0] >= 3:
# TextIO? Are you kidding me?
data = sys.stdin.buffer.read()
Expand Down
3 changes: 1 addition & 2 deletions src/zope/component/testfiles/components.py
Expand Up @@ -54,8 +54,7 @@ def __init__(self, context):
self.context = context

class Comp3(object):
def __init__(self, context):
self.context = context
pass

@adapter(IContent)
@implementer(IApp)
Expand Down
18 changes: 9 additions & 9 deletions src/zope/component/testing.py
Expand Up @@ -18,17 +18,17 @@
import zope.component.event

# we really don't need special setup now:
class _PlacelessSetupFallback(object):
def cleanUp(self):
from zope.component.globalregistry import base
base.__init__('base')

setUp = tearDown = cleanUp

try:
from zope.testing.cleanup import CleanUp as PlacelessSetup
except ImportError:
class PlacelessSetup(object):
def cleanUp(self):
from zope.component.globalregistry import base
base.__init__('base')
def setUp(self):
self.cleanUp()
def tearDown(self):
self.cleanUp()
except ImportError: # pragma: no cover
PlacelessSetup = _PlacelessSetupFallback

def setUp(test=None):
PlacelessSetup().setUp()
Expand Down
4 changes: 2 additions & 2 deletions src/zope/component/testlayer.py
Expand Up @@ -17,7 +17,7 @@
from zope.configuration import xmlconfig, config
try:
from zope.testing.cleanup import cleanUp
except ImportError:
except ImportError: # pragma: no cover
def cleanUp():
pass

Expand Down Expand Up @@ -62,7 +62,7 @@ def __init__(self, package, name=None):
self.__name__ = name
self.__module__ = package.__name__
self.package = package

def setUp(self):
pass

Expand Down
9 changes: 8 additions & 1 deletion src/zope/component/tests/__init__.py
@@ -1 +1,8 @@
# tests package
import unittest

def skipIfNoSecurity(testfunc):
try:
import zope.security
except ImportError:
return unittest.skip("zope.security not installed")(testfunc)
return testfunc
5 changes: 2 additions & 3 deletions src/zope/component/tests/examples.py
Expand Up @@ -54,7 +54,7 @@ class ISII(Interface):
pass

def noop(*args):
pass
pass # pragma: no cover

class U(object):

Expand Down Expand Up @@ -123,8 +123,7 @@ def __init__(self, context):

@implementer(I3)
class Comp2(object):
def __init__(self, context):
self.context = context
pass


class ConformsToIComponentLookup(object):
Expand Down
48 changes: 7 additions & 41 deletions src/zope/component/tests/test__api.py
Expand Up @@ -88,8 +88,7 @@ class IBar(Interface):
pass
@implementer(IFoo)
class Global(object):
def __init__(self, context):
self.context = context
pass
@implementer(IFoo)
class Local(object):
def __init__(self, context):
Expand All @@ -114,10 +113,6 @@ class Test_queryAdapterInContext(unittest.TestCase):

from zope.component.testing import setUp, tearDown

def _callFUT(self, *args, **kw):
from zope.component import queryAdapterInContext
return queryAdapterInContext(*args, **kw)

def test_miss(self):
from zope.interface import Interface
from zope.component import queryAdapterInContext
Expand All @@ -136,7 +131,7 @@ class Foo(object):
def __conform__(self, iface, default=None):
if iface is IFoo:
return _adapted
return default
raise NotImplementedError()
self.assertTrue(
queryAdapterInContext(Foo(), IFoo, context=None) is _adapted)

Expand All @@ -148,9 +143,7 @@ class IFoo(Interface):
_adapted = object()
class Foo(object):
def __conform__(self, iface, default=None):
if iface is IFoo:
return _adapted
return default
raise NotImplementedError()
# call via class, triggering TypeError
self.assertEqual(queryAdapterInContext(Foo, IFoo, context=None), None)

Expand Down Expand Up @@ -338,8 +331,7 @@ class IBar(Interface):
pass
@implementer(IFoo)
class Global(object):
def __init__(self, context):
self.context = context
pass
@implementer(IFoo)
class Local(object):
def __init__(self, context):
Expand Down Expand Up @@ -570,8 +562,7 @@ class Baz(object):
pass
@implementer(IFoo)
class Global(object):
def __init__(self, first, second):
self.first, self.second = first, second
pass
@implementer(IFoo)
class Local(object):
def __init__(self, first, second):
Expand Down Expand Up @@ -1060,7 +1051,7 @@ def __conform__(self, iface):
def queryUtility(self, iface, name, default):
if iface is IFactory and name == 'test':
return _factory
return default
raise NotImplementedError()
context = Context()
self.assertTrue(self._callFUT('test', context=context) is _object)
self.assertEqual(_factory_called, [((), {})])
Expand Down Expand Up @@ -1092,7 +1083,7 @@ def __conform__(self, iface):
def queryUtility(self, iface, name, default):
if iface is IFactory and name == 'test':
return _Factory()
return default
raise NotImplementedError()
context = Context()
self.assertEqual(self._callFUT('test', context=context), [IFoo])

Expand Down Expand Up @@ -1177,28 +1168,3 @@ def __init__(self, id, sm):
self.sitemanager = sm

return MyUtility(name, sm)


def test_suite():
return unittest.TestSuite((
unittest.makeSuite(Test_getSiteManager),
unittest.makeSuite(Test_getAdapterInContext),
unittest.makeSuite(Test_queryAdapterInContext),
unittest.makeSuite(Test_getAdapter),
unittest.makeSuite(Test_queryAdapter),
unittest.makeSuite(Test_getMultiAdapter),
unittest.makeSuite(Test_queryMultiAdapter),
unittest.makeSuite(Test_getAdapters),
unittest.makeSuite(Test_subscribers),
unittest.makeSuite(Test_handle),
unittest.makeSuite(Test_getUtility),
unittest.makeSuite(Test_queryUtility),
unittest.makeSuite(Test_getUtilitiesFor),
unittest.makeSuite(Test_getAllUtilitiesRegisteredFor),
unittest.makeSuite(Test_getNextUtility),
unittest.makeSuite(Test_queryNextUtility),
unittest.makeSuite(Test_createObject),
unittest.makeSuite(Test_getFactoryInterfaces),
unittest.makeSuite(Test_getFactoriesFor),
))

25 changes: 3 additions & 22 deletions src/zope/component/tests/test__declaration.py
Expand Up @@ -80,22 +80,11 @@ def _run_generated_code(self, code, globs, locs,
fails_under_py3k=True,
):
import warnings
#from zope.component._compat import PYTHON3
PYTHON3 = False
with warnings.catch_warnings(record=True) as log:
warnings.resetwarnings()
if not PYTHON3:
exec(code, globs, locs)
self.assertEqual(len(log), 0) # no longer warn
return True
else:
try:
exec(code, globs, locs)
except TypeError:
return False
else:
if fails_under_py3k:
self.fail("Didn't raise TypeError")
exec(code, globs, locs)
self.assertEqual(len(log), 0) # no longer warn
return True

def test_instances_not_affected(self):
from zope.component._declaration import adapts
Expand Down Expand Up @@ -211,11 +200,3 @@ class Baz(object):
baz = Baz()
baz.__component_adapts__ = (IFoo, IBar)
self.assertEqual(self._callFUT(baz), (IFoo, IBar))


def test_suite():
return unittest.TestSuite((
unittest.makeSuite(Test_adapter),
unittest.makeSuite(Test_adapts),
unittest.makeSuite(Test_adaptedBy),
))
11 changes: 3 additions & 8 deletions src/zope/component/tests/test_factory.py
Expand Up @@ -84,7 +84,7 @@ class IBaz(Interface):
pass
@implementer(IBaz)
def _callable():
pass
raise NotImplementedError()
factory = self._makeOne(_callable, interfaces=(IFoo, IBar))
spec = factory.getInterfaces()
self.assertEqual(spec.__name__, '_callable')
Expand All @@ -97,15 +97,10 @@ class IBaz(Interface):
pass
@implementer(IBaz)
def _callable():
pass
raise NotImplementedError()
factory = self._makeOne(_callable)
spec = factory.getInterfaces()
self.assertEqual(list(spec), [IBaz])

def _test_callable(*args, **kw):
pass

def test_suite():
return unittest.TestSuite((
unittest.makeSuite(FactoryTests),
))
raise NotImplementedError()
21 changes: 8 additions & 13 deletions src/zope/component/tests/test_globalregistry.py
Expand Up @@ -68,7 +68,12 @@ class Foo(object):
foo = Foo()
self._callFUT(foo)
gsm = getGlobalSiteManager()
self.assertTrue(gsm.getUtility(IFoo, '') is foo)
self.assertIs(gsm.getUtility(IFoo, ''), foo)

# We can clean it up using the fallback and it will be gone
from zope.component.testing import _PlacelessSetupFallback
_PlacelessSetupFallback().cleanUp()
self.assertIsNone(gsm.queryUtility(IFoo, ''))

def test_named_w_provides(self):
from zope.interface import Interface
Expand Down Expand Up @@ -214,7 +219,7 @@ class Foo(object):
pass
@adapter(IFoo)
def _handler(context):
assert 0, "DON'T GO HERE"
raise NotImplementedError()
self._callFUT(_handler)
gsm = getGlobalSiteManager()
regs = list(gsm.registeredHandlers())
Expand All @@ -230,7 +235,7 @@ def test_w_adapts(self):
class IFoo(Interface):
pass
def _handler(context):
assert 0, "DON'T GO HERE"
raise NotImplementedError()
self._callFUT(_handler, (IFoo,))
gsm = getGlobalSiteManager()
regs = list(gsm.registeredHandlers())
Expand All @@ -239,13 +244,3 @@ def _handler(context):
self.assertEqual(list(hr.required), [IFoo])
self.assertEqual(hr.name, '')
self.assertTrue(hr.factory is _handler)


def test_suite():
return unittest.TestSuite((
unittest.makeSuite(Test_getGlobalSiteManager),
unittest.makeSuite(Test_provideUtility),
unittest.makeSuite(Test_provideAdapter),
unittest.makeSuite(Test_provideSubscriptionAdapter),
unittest.makeSuite(Test_provideHandler),
))

0 comments on commit 79b5fd2

Please sign in to comment.