Skip to content

Commit

Permalink
100% coverage for pagetemplate.py
Browse files Browse the repository at this point in the history
  • Loading branch information
jamadden committed Oct 17, 2017
1 parent 508987d commit f09ad39
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 22 deletions.
8 changes: 7 additions & 1 deletion src/z3c/pt/pagetemplate.py
Expand Up @@ -135,7 +135,13 @@ def translate(
if msgid is MV:
# Special case handling of Zope2's Missing.MV
# (Missing.Value) used by the ZCatalog but is
# unhashable
# unhashable.

# This case cannot arise in ordinary templates; msgid
# comes from i18n:translate attributes, which does not
# take a TALES expression, just a literal string.
# However, the 'context' argument is available as an implementation
# detail for macros
return
return fast_translate(
msgid, domain, mapping, request, target_language, default)
Expand Down
6 changes: 5 additions & 1 deletion src/z3c/pt/tests/test_expressions.py
Expand Up @@ -141,6 +141,10 @@ def test_translate_components(self):
translated = expr.translate("a/?var/?var2", None)
self.assertEqual(len(translated), 1)
code = TemplateCodeGenerator(translated[0]).code
# XXX: Normally this starts with 'None =', but sometimes on Python 2,
# at least in tox, it starts with '__package__ ='. Why
# is this?
code = code.strip().replace("__package__", 'None')
self.assertEqual(
code.strip(),
code,
"None = _path_traverse(a, econtext, True, (('%s' % (var, )), ('%s' % (var2, )), ))")
132 changes: 113 additions & 19 deletions src/z3c/pt/tests/test_templates.py
Expand Up @@ -11,52 +11,53 @@
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
import os
import unittest

import zope.component.testing
from zope.testing.cleanup import CleanUp
import zope.configuration.xmlconfig

from z3c.pt import pagetemplate
from z3c.pt.pagetemplate import PageTemplateFile
from z3c.pt.pagetemplate import ViewPageTemplateFile

class Setup(CleanUp):

class TestPageTemplateFile(unittest.TestCase):
def setUp(self):
CleanUp.setUp(self)
import z3c.pt
zope.component.testing.setUp(self)
zope.configuration.xmlconfig.XMLConfig('configure.zcml', z3c.pt)()
zope.configuration.xmlconfig.file('configure.zcml', z3c.pt)


def tearDown(self):
zope.component.testing.tearDown(self)
class TestPageTemplateFile(Setup,
unittest.TestCase):

def test_nocall(self):
from z3c.pt.pagetemplate import PageTemplateFile
template = PageTemplateFile("nocall.pt")
def dont_call():
raise AssertionError("Should not be called")
result = template(callable=dont_call)
self.assertTrue(repr(dont_call) in result)

def test_exists(self):
from z3c.pt.pagetemplate import PageTemplateFile
template = PageTemplateFile("exists.pt")
def dont_call():
raise AssertionError("Should not be called")
result = template(callable=dont_call)
self.assertTrue('ok' in result)

def test_false_attribute(self):
from z3c.pt.pagetemplate import PageTemplateFile
template = PageTemplateFile("false.pt")
result = template()
self.assertTrue('False' in result)

def test_boolean_attribute(self):
from z3c.pt.pagetemplate import PageTemplateFile
template = PageTemplateFile("boolean.pt")
result = template()
self.assertFalse('False' in result)
self.assertTrue('checked="checked"' in result)

def test_path(self):
from z3c.pt.pagetemplate import PageTemplateFile
template = PageTemplateFile("path.pt")

class Context(object):
Expand All @@ -69,8 +70,10 @@ class Context(object):
self.assertTrue("supported" in result)
self.assertTrue("some path" in result)

class TestViewPageTemplateFile(Setup,
unittest.TestCase):

def test_provider(self):
from z3c.pt.pagetemplate import ViewPageTemplateFile

class Context(object):
pass
Expand All @@ -81,6 +84,9 @@ class Request(object):
class View(object):
__call__ = ViewPageTemplateFile("provider.pt")

# Test binding descriptor behaviour.
self.assertIsInstance(View.__call__, ViewPageTemplateFile)

from zope.interface import Interface
from zope.schema import Field
from zope.interface import implementer
Expand Down Expand Up @@ -118,19 +124,107 @@ def render(self):
implementedBy(Context),
implementedBy(Request),
implementedBy(View)
),
),
IContentProvider,
name="content"
)
)

context = Context()
request = Request()

result = view(context=context, request=request)
self.assertTrue(repr(data) in result)
self.assertTrue(repr({'context': context}) in result)
self.assertIn(repr(data), result)
self.assertIn(repr({'context': context}), result)

class TestOpaqueDict(unittest.TestCase):

def test_getitem(self):
import operator
d = {}
od = pagetemplate.OpaqueDict(d)
with self.assertRaises(KeyError):
operator.itemgetter('key')(od)

d['key'] = 42
self.assertEqual(od['key'], 42)

def test_len(self):
d = {}
od = pagetemplate.OpaqueDict(d)
self.assertEqual(0, len(od))

d['key'] = 42
self.assertEqual(1, len(od))

def test_repr(self):
d = {}
od = pagetemplate.OpaqueDict(d)
self.assertEqual('{...} (0 entries)', repr(od))

d['key'] = 42
self.assertEqual('{...} (1 entries)', repr(od))


class TestBaseTemplate(unittest.TestCase):

def test_negotiate_fails(self):
class I18N(object):
request = None
def negotiate(self, request):
self.request = request
raise Exception("This is caught")

i18n = I18N()
orig_i18n = pagetemplate.i18n
pagetemplate.i18n = i18n
try:
template = pagetemplate.BaseTemplate('<html />')
request = 'strings are allowed'
template.render(request=request)
self.assertIs(i18n.request, request)
finally:
pagetemplate.i18n = orig_i18n

def test_translate_mv(self):
template = pagetemplate.BaseTemplate("""
<html>
<body metal:use-macro="m" />
</html>
""")

class Macro(object):
translate = None
def include(self, stream, econtext, *args, **kwargs):
self.translate = econtext['translate']
macro = Macro()
template.render(m=macro)

self.assertIsNone(macro.translate(pagetemplate.MV))

class TestBaseTemplateFile(unittest.TestCase):

def test_init_with_path(self):

here = os.path.abspath(os.path.dirname(__file__))

template = pagetemplate.BaseTemplateFile('view.pt', path=here)

self.assertEqual(template.filename,
os.path.join(here, 'view.pt'))

class TestBoundPageTemplate(unittest.TestCase):

def test_setattr(self):
bound = pagetemplate.BoundPageTemplate(None, None)
with self.assertRaisesRegexp(AttributeError,
"Can't set attribute"):
setattr(bound, 'im_self', 42)

def test_repr(self):
# It requires the 'filename' attribute
class Template(object):
filename = 'file.pt'

def test_suite():
import sys
return unittest.findTestCases(sys.modules[__name__])
bound = pagetemplate.BoundPageTemplate(Template(), 'render')
self.assertEqual("<z3c.pt.tests.test_templates.BoundTemplate 'file.pt'>",
repr(bound))
2 changes: 1 addition & 1 deletion tox.ini
Expand Up @@ -14,7 +14,7 @@ basepython =
python2.7
commands =
coverage run -m zope.testrunner --test-path=src []
coverage report --fail-under=93
coverage report --fail-under=100
deps =
{[testenv]deps}
coverage

0 comments on commit f09ad39

Please sign in to comment.