Skip to content

Commit

Permalink
100% coverage for namespaces.py
Browse files Browse the repository at this point in the history
Also fixes #3

Install zope.pagetemplate for tests and ensure that we use the correct
namespaces.
  • Loading branch information
jamadden committed Oct 17, 2017
1 parent 69a8910 commit 508987d
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 8 deletions.
4 changes: 4 additions & 0 deletions CHANGES.rst
Expand Up @@ -7,6 +7,10 @@

- Added support for Python 3.6.
- Dropped support for Python 3.3.
- Use the adapter namespace from ``zope.pagetemplate`` if it's
available, instead of the backwards compatibility shim in
``zope.app.pagetemplate``. See `issue 3
<https://github.com/zopefoundation/z3c.pt/issues/3>`_.


3.0 (2016-09-02)
Expand Down
1 change: 1 addition & 0 deletions setup.py
Expand Up @@ -37,6 +37,7 @@ def alltests():
return unittest.TestSuite(suites)

TESTS_REQUIRE = [
'zope.pagetemplate',
'zope.testing',
'zope.testrunner',
]
Expand Down
2 changes: 1 addition & 1 deletion src/z3c/pt/README.rst
Expand Up @@ -390,7 +390,7 @@ Adapters build upon).
... def parent(self):
... return self.context.parent

>>> function_namespaces.registerFunctionNamespace('ns1', ns1)
>>> function_namespaces.namespaces['ns1'] = ns1

>>> class ns2(object):
... def __init__(self, context):
Expand Down
20 changes: 13 additions & 7 deletions src/z3c/pt/namespaces.py
Expand Up @@ -93,15 +93,21 @@ def lower(self):


def getFunctionNamespace(self, namespacename):
""" Returns the function namespace """
"""
Returns the function namespace, if registered.
Unlike ``__getitem__``, this method will immediately raise a
KeyError if no such function is registered.
"""
return self.namespaces[namespacename]

function_namespaces = AdapterNamespaces()

try:
# If zope.app.pagetemplates is available, use the adapter
# registered with the main zope.app.pagetemplates engine so that
# If zope.pagetemplate is available, use the adapter
# registered with the main zope.pagetemplate engine so that
# we don't need to re-register them.
from zope.app.pagetemplates.engine import Engine
from zope.pagetemplate.engine import Engine
function_namespaces = Engine.namespaces
except (ImportError, AttributeError):
function_namespaces = AdapterNamespaces()

except (ImportError, AttributeError): # pragma: no cover
pass
34 changes: 34 additions & 0 deletions src/z3c/pt/tests/test_namespaces.py
@@ -0,0 +1,34 @@
# -*- coding: utf-8 -*-
"""
Tests for namespaces.py.
"""

import unittest

from z3c.pt import namespaces

class TestAdapterNamespaces(unittest.TestCase):

def test_registered_name(self):
nss = namespaces.AdapterNamespaces()
func = lambda ctx: ctx
nss.registerFunctionNamespace('ns', func)
self.assertIs(func, nss['ns'])
self.assertIs(func, nss.getFunctionNamespace('ns'))

def test_unregistered_name(self):
nss = namespaces.AdapterNamespaces()
with self.assertRaises(KeyError):
nss.getFunctionNamespace('ns')

# but __getitem__ makes one up
self.assertIsNotNone(nss['ns'])

def test_using_pagetemplate_version(self):
from zope.pagetemplate import engine

self.assertFalse(isinstance(namespaces.function_namespaces,
namespaces.AdapterNamespaces))
self.assertIsInstance(namespaces.function_namespaces,
engine.AdapterNamespaces)

0 comments on commit 508987d

Please sign in to comment.