Commit eaee99c6 by Chris Jerdonek

Converted assert_strings() to a mixin.

parent 4260e313
...@@ -14,23 +14,31 @@ def get_data_path(file_name): ...@@ -14,23 +14,31 @@ def get_data_path(file_name):
return os.path.join(DATA_DIR, file_name) return os.path.join(DATA_DIR, file_name)
# TODO: convert this to a mixin. class AssertStringMixin:
def assert_strings(test_case, actual, expected):
# Show both friendly and literal versions.
message = """\
"""A unittest.TestCase mixin to check string equality."""
Expected: \"""%s\""" def assertString(self, actual, expected):
Actual: \"""%s\""" """
Assert that the given strings are equal and have the same type.
Expected: %s """
Actual: %s""" % (expected, actual, repr(expected), repr(actual)) # Show both friendly and literal versions.
test_case.assertEquals(actual, expected, message) message = """\
Expected: \"""%s\"""
Actual: \"""%s\"""
Expected: %s
Actual: %s""" % (expected, actual, repr(expected), repr(actual))
self.assertEquals(actual, expected, message)
self.assertEquals(type(actual), type(expected), "Type mismatch: " + message)
class AssertIsMixin: class AssertIsMixin:
"""A mixin for adding assertIs() to a unittest.TestCase.""" """A unittest.TestCase mixin adding assertIs()."""
# unittest.assertIs() is not available until Python 2.7: # unittest.assertIs() is not available until Python 2.7:
# http://docs.python.org/library/unittest.html#unittest.TestCase.assertIsNone # http://docs.python.org/library/unittest.html#unittest.TestCase.assertIsNone
......
...@@ -12,21 +12,21 @@ from examples.unicode_output import UnicodeOutput ...@@ -12,21 +12,21 @@ from examples.unicode_output import UnicodeOutput
from examples.unicode_input import UnicodeInput from examples.unicode_input import UnicodeInput
from examples.nested_context import NestedContext from examples.nested_context import NestedContext
from pystache import Renderer from pystache import Renderer
from tests.common import assert_strings from tests.common import AssertStringMixin
class TestView(unittest.TestCase): class TestView(unittest.TestCase, AssertStringMixin):
def _assert(self, obj, expected): def _assert(self, obj, expected):
renderer = Renderer() renderer = Renderer()
actual = renderer.render(obj) actual = renderer.render(obj)
assert_strings(self, actual, expected) self.assertString(actual, expected)
def test_comments(self): def test_comments(self):
self._assert(Comments(), "<h1>A Comedy of Errors</h1>") self._assert(Comments(), u"<h1>A Comedy of Errors</h1>")
def test_double_section(self): def test_double_section(self):
self._assert(DoubleSection(), "* first\n* second\n* third") self._assert(DoubleSection(), u"* first\n* second\n* third")
def test_unicode_output(self): def test_unicode_output(self):
self.assertEquals(UnicodeOutput().render(), u'<p>Name: Henri Poincaré</p>') self.assertEquals(UnicodeOutput().render(), u'<p>Name: Henri Poincaré</p>')
...@@ -36,7 +36,7 @@ class TestView(unittest.TestCase): ...@@ -36,7 +36,7 @@ class TestView(unittest.TestCase):
u'<p>If alive today, Henri Poincaré would be 156 years old.</p>') u'<p>If alive today, Henri Poincaré would be 156 years old.</p>')
def test_escaping(self): def test_escaping(self):
self._assert(Escaped(), "<h1>Bear &gt; Shark</h1>") self._assert(Escaped(), u"<h1>Bear &gt; Shark</h1>")
def test_literal(self): def test_literal(self):
self.assertEquals(Unescaped().render(), "<h1>Bear > Shark</h1>") self.assertEquals(Unescaped().render(), "<h1>Bear > Shark</h1>")
...@@ -48,7 +48,7 @@ Again, Welcome!""") ...@@ -48,7 +48,7 @@ Again, Welcome!""")
def test_template_partial_extension(self): def test_template_partial_extension(self):
view = TemplatePartial() view = TemplatePartial()
view.template_extension = 'txt' view.template_extension = 'txt'
assert_strings(self, view.render(), u"""Welcome self.assertString(view.render(), u"""Welcome
------- -------
## Again, Welcome! ##""") ## Again, Welcome! ##""")
...@@ -56,7 +56,7 @@ Again, Welcome!""") ...@@ -56,7 +56,7 @@ Again, Welcome!""")
def test_delimiters(self): def test_delimiters(self):
renderer = Renderer() renderer = Renderer()
actual = renderer.render(Delimiters()) actual = renderer.render(Delimiters())
assert_strings(self, actual, """\ self.assertString(actual, u"""\
* It worked the first time. * It worked the first time.
* And it worked the second time. * And it worked the second time.
* Then, surprisingly, it worked the third time. * Then, surprisingly, it worked the third time.
......
...@@ -11,7 +11,7 @@ import unittest ...@@ -11,7 +11,7 @@ import unittest
from pystache.context import Context from pystache.context import Context
from pystache.parser import ParsingError from pystache.parser import ParsingError
from pystache.renderengine import RenderEngine from pystache.renderengine import RenderEngine
from tests.common import assert_strings from tests.common import AssertStringMixin
class RenderEngineTestCase(unittest.TestCase): class RenderEngineTestCase(unittest.TestCase):
...@@ -31,7 +31,7 @@ class RenderEngineTestCase(unittest.TestCase): ...@@ -31,7 +31,7 @@ class RenderEngineTestCase(unittest.TestCase):
self.assertEquals(engine.load_partial, "foo") self.assertEquals(engine.load_partial, "foo")
class RenderTests(unittest.TestCase): class RenderTests(unittest.TestCase, AssertStringMixin):
""" """
Tests RenderEngine.render(). Tests RenderEngine.render().
...@@ -66,10 +66,10 @@ class RenderTests(unittest.TestCase): ...@@ -66,10 +66,10 @@ class RenderTests(unittest.TestCase):
actual = engine.render(template, context) actual = engine.render(template, context)
assert_strings(test_case=self, actual=actual, expected=expected) self.assertString(actual=actual, expected=expected)
def test_render(self): def test_render(self):
self._assert_render('Hi Mom', 'Hi {{person}}', {'person': 'Mom'}) self._assert_render(u'Hi Mom', 'Hi {{person}}', {'person': 'Mom'})
def test__load_partial(self): def test__load_partial(self):
""" """
...@@ -80,7 +80,7 @@ class RenderTests(unittest.TestCase): ...@@ -80,7 +80,7 @@ class RenderTests(unittest.TestCase):
partials = {'partial': u"{{person}}"} partials = {'partial': u"{{person}}"}
engine.load_partial = lambda key: partials[key] engine.load_partial = lambda key: partials[key]
self._assert_render('Hi Mom', 'Hi {{>partial}}', {'person': 'Mom'}, engine=engine) self._assert_render(u'Hi Mom', 'Hi {{>partial}}', {'person': 'Mom'}, engine=engine)
def test__literal(self): def test__literal(self):
""" """
...@@ -90,13 +90,13 @@ class RenderTests(unittest.TestCase): ...@@ -90,13 +90,13 @@ class RenderTests(unittest.TestCase):
engine = self._engine() engine = self._engine()
engine.literal = lambda s: s.upper() engine.literal = lambda s: s.upper()
self._assert_render('BAR', '{{{foo}}}', {'foo': 'bar'}, engine=engine) self._assert_render(u'BAR', '{{{foo}}}', {'foo': 'bar'}, engine=engine)
def test_literal__sigil(self): def test_literal__sigil(self):
template = "<h1>{{& thing}}</h1>" template = "<h1>{{& thing}}</h1>"
context = {'thing': 'Bear > Giraffe'} context = {'thing': 'Bear > Giraffe'}
expected = "<h1>Bear > Giraffe</h1>" expected = u"<h1>Bear > Giraffe</h1>"
self._assert_render(expected, template, context) self._assert_render(expected, template, context)
...@@ -108,7 +108,7 @@ class RenderTests(unittest.TestCase): ...@@ -108,7 +108,7 @@ class RenderTests(unittest.TestCase):
engine = self._engine() engine = self._engine()
engine.escape = lambda s: "**" + s engine.escape = lambda s: "**" + s
self._assert_render('**bar', '{{foo}}', {'foo': 'bar'}, engine=engine) self._assert_render(u'**bar', '{{foo}}', {'foo': 'bar'}, engine=engine)
def test__escape_does_not_call_literal(self): def test__escape_does_not_call_literal(self):
""" """
...@@ -122,7 +122,7 @@ class RenderTests(unittest.TestCase): ...@@ -122,7 +122,7 @@ class RenderTests(unittest.TestCase):
template = 'literal: {{{foo}}} escaped: {{foo}}' template = 'literal: {{{foo}}} escaped: {{foo}}'
context = {'foo': 'bar'} context = {'foo': 'bar'}
self._assert_render('literal: BAR escaped: **bar', template, context, engine=engine) self._assert_render(u'literal: BAR escaped: **bar', template, context, engine=engine)
def test__escape_preserves_unicode_subclasses(self): def test__escape_preserves_unicode_subclasses(self):
""" """
...@@ -147,7 +147,7 @@ class RenderTests(unittest.TestCase): ...@@ -147,7 +147,7 @@ class RenderTests(unittest.TestCase):
template = '{{foo1}} {{foo2}}' template = '{{foo1}} {{foo2}}'
context = {'foo1': MyUnicode('bar'), 'foo2': 'bar'} context = {'foo1': MyUnicode('bar'), 'foo2': 'bar'}
self._assert_render('**bar bar**', template, context, engine=engine) self._assert_render(u'**bar bar**', template, context, engine=engine)
def test__non_basestring__literal_and_escaped(self): def test__non_basestring__literal_and_escaped(self):
""" """
...@@ -166,7 +166,7 @@ class RenderTests(unittest.TestCase): ...@@ -166,7 +166,7 @@ class RenderTests(unittest.TestCase):
template = '{{text}} {{int}} {{{int}}}' template = '{{text}} {{int}} {{{int}}}'
context = {'int': 100, 'text': 'foo'} context = {'int': 100, 'text': 'foo'}
self._assert_render('FOO 100 100', template, context, engine=engine) self._assert_render(u'FOO 100 100', template, context, engine=engine)
def test_tag__output_not_interpolated(self): def test_tag__output_not_interpolated(self):
""" """
...@@ -184,7 +184,7 @@ class RenderTests(unittest.TestCase): ...@@ -184,7 +184,7 @@ class RenderTests(unittest.TestCase):
""" """
template = '{{test}}' template = '{{test}}'
context = {'test': '{{#hello}}'} context = {'test': '{{#hello}}'}
self._assert_render('{{#hello}}', template, context) self._assert_render(u'{{#hello}}', template, context)
def test_interpolation__built_in_type__string(self): def test_interpolation__built_in_type__string(self):
""" """
...@@ -227,7 +227,7 @@ class RenderTests(unittest.TestCase): ...@@ -227,7 +227,7 @@ class RenderTests(unittest.TestCase):
template = """{{#test}}{{{.}}}{{/test}}""" template = """{{#test}}{{{.}}}{{/test}}"""
context = {'test': ['<', '>']} context = {'test': ['<', '>']}
self._assert_render('<>', template, context) self._assert_render(u'<>', template, context)
def test_implicit_iterator__escaped(self): def test_implicit_iterator__escaped(self):
""" """
...@@ -237,7 +237,7 @@ class RenderTests(unittest.TestCase): ...@@ -237,7 +237,7 @@ class RenderTests(unittest.TestCase):
template = """{{#test}}{{.}}{{/test}}""" template = """{{#test}}{{.}}{{/test}}"""
context = {'test': ['<', '>']} context = {'test': ['<', '>']}
self._assert_render('&lt;&gt;', template, context) self._assert_render(u'&lt;&gt;', template, context)
def test_literal__in_section(self): def test_literal__in_section(self):
""" """
...@@ -247,7 +247,7 @@ class RenderTests(unittest.TestCase): ...@@ -247,7 +247,7 @@ class RenderTests(unittest.TestCase):
template = '{{#test}}1 {{{less_than}}} 2{{/test}}' template = '{{#test}}1 {{{less_than}}} 2{{/test}}'
context = {'test': {'less_than': '<'}} context = {'test': {'less_than': '<'}}
self._assert_render('1 < 2', template, context) self._assert_render(u'1 < 2', template, context)
def test_literal__in_partial(self): def test_literal__in_partial(self):
""" """
...@@ -258,11 +258,11 @@ class RenderTests(unittest.TestCase): ...@@ -258,11 +258,11 @@ class RenderTests(unittest.TestCase):
partials = {'partial': '1 {{{less_than}}} 2'} partials = {'partial': '1 {{{less_than}}} 2'}
context = {'less_than': '<'} context = {'less_than': '<'}
self._assert_render('1 < 2', template, context, partials=partials) self._assert_render(u'1 < 2', template, context, partials=partials)
def test_partial(self): def test_partial(self):
partials = {'partial': "{{person}}"} partials = {'partial': "{{person}}"}
self._assert_render('Hi Mom', 'Hi {{>partial}}', {'person': 'Mom'}, partials=partials) self._assert_render(u'Hi Mom', 'Hi {{>partial}}', {'person': 'Mom'}, partials=partials)
def test_partial__context_values(self): def test_partial__context_values(self):
""" """
...@@ -275,7 +275,7 @@ class RenderTests(unittest.TestCase): ...@@ -275,7 +275,7 @@ class RenderTests(unittest.TestCase):
partials = {'partial': 'unescaped: {{{foo}}} escaped: {{foo}}'} partials = {'partial': 'unescaped: {{{foo}}} escaped: {{foo}}'}
context = {'foo': '<'} context = {'foo': '<'}
self._assert_render('unescaped: < escaped: &lt;', template, context, engine=engine, partials=partials) self._assert_render(u'unescaped: < escaped: &lt;', template, context, engine=engine, partials=partials)
## Test cases related specifically to sections. ## Test cases related specifically to sections.
...@@ -311,7 +311,7 @@ class RenderTests(unittest.TestCase): ...@@ -311,7 +311,7 @@ class RenderTests(unittest.TestCase):
template = '{{#test}}unescaped: {{{foo}}} escaped: {{foo}}{{/test}}' template = '{{#test}}unescaped: {{{foo}}} escaped: {{foo}}{{/test}}'
context = {'test': {'foo': '<'}} context = {'test': {'foo': '<'}}
self._assert_render('unescaped: < escaped: &lt;', template, context, engine=engine) self._assert_render(u'unescaped: < escaped: &lt;', template, context, engine=engine)
def test_section__context_precedence(self): def test_section__context_precedence(self):
""" """
...@@ -338,7 +338,7 @@ class RenderTests(unittest.TestCase): ...@@ -338,7 +338,7 @@ class RenderTests(unittest.TestCase):
template = "{{#list}}{{greeting}} {{name}}, {{/list}}" template = "{{#list}}{{greeting}} {{name}}, {{/list}}"
self._assert_render("Hi Al, Hi Bob, ", template, context) self._assert_render(u"Hi Al, Hi Bob, ", template, context)
def test_section__output_not_interpolated(self): def test_section__output_not_interpolated(self):
""" """
...@@ -382,7 +382,7 @@ class RenderTests(unittest.TestCase): ...@@ -382,7 +382,7 @@ class RenderTests(unittest.TestCase):
def test_section__lambda(self): def test_section__lambda(self):
template = '{{#test}}Mom{{/test}}' template = '{{#test}}Mom{{/test}}'
context = {'test': (lambda text: 'Hi %s' % text)} context = {'test': (lambda text: 'Hi %s' % text)}
self._assert_render('Hi Mom', template, context) self._assert_render(u'Hi Mom', template, context)
def test_section__iterable(self): def test_section__iterable(self):
""" """
...@@ -392,10 +392,10 @@ class RenderTests(unittest.TestCase): ...@@ -392,10 +392,10 @@ class RenderTests(unittest.TestCase):
template = '{{#iterable}}{{.}}{{/iterable}}' template = '{{#iterable}}{{.}}{{/iterable}}'
context = {'iterable': (i for i in range(3))} # type 'generator' context = {'iterable': (i for i in range(3))} # type 'generator'
self._assert_render('012', template, context) self._assert_render(u'012', template, context)
context = {'iterable': xrange(4)} # type 'xrange' context = {'iterable': xrange(4)} # type 'xrange'
self._assert_render('0123', template, context) self._assert_render(u'0123', template, context)
d = {'foo': 0, 'bar': 0} d = {'foo': 0, 'bar': 0}
# We don't know what order of keys we'll be given, but from the # We don't know what order of keys we'll be given, but from the
...@@ -403,7 +403,7 @@ class RenderTests(unittest.TestCase): ...@@ -403,7 +403,7 @@ class RenderTests(unittest.TestCase):
# "If items(), keys(), values(), iteritems(), iterkeys(), and # "If items(), keys(), values(), iteritems(), iterkeys(), and
# itervalues() are called with no intervening modifications to # itervalues() are called with no intervening modifications to
# the dictionary, the lists will directly correspond." # the dictionary, the lists will directly correspond."
expected = ''.join(d.keys()) expected = u''.join(d.keys())
context = {'iterable': d.iterkeys()} # type 'dictionary-keyiterator' context = {'iterable': d.iterkeys()} # type 'dictionary-keyiterator'
self._assert_render(expected, template, context) self._assert_render(expected, template, context)
...@@ -422,15 +422,15 @@ class RenderTests(unittest.TestCase): ...@@ -422,15 +422,15 @@ class RenderTests(unittest.TestCase):
""" """
template = '{{#test}}Hi {{person}}{{/test}}' template = '{{#test}}Hi {{person}}{{/test}}'
context = {'person': 'Mom', 'test': (lambda text: text + " :)")} context = {'person': 'Mom', 'test': (lambda text: text + " :)")}
self._assert_render('Hi Mom :)', template, context) self._assert_render(u'Hi Mom :)', template, context)
def test_comment__multiline(self): def test_comment__multiline(self):
""" """
Check that multiline comments are permitted. Check that multiline comments are permitted.
""" """
self._assert_render('foobar', 'foo{{! baz }}bar') self._assert_render(u'foobar', 'foo{{! baz }}bar')
self._assert_render('foobar', 'foo{{! \nbaz }}bar') self._assert_render(u'foobar', 'foo{{! \nbaz }}bar')
def test_custom_delimiters__sections(self): def test_custom_delimiters__sections(self):
""" """
......
...@@ -8,10 +8,10 @@ from examples.lambdas import Lambdas ...@@ -8,10 +8,10 @@ from examples.lambdas import Lambdas
from examples.template_partial import TemplatePartial from examples.template_partial import TemplatePartial
from examples.simple import Simple from examples.simple import Simple
from tests.common import assert_strings from tests.common import AssertStringMixin
class TestSimple(unittest.TestCase): class TestSimple(unittest.TestCase, AssertStringMixin):
def test_nested_context(self): def test_nested_context(self):
view = NestedContext() view = NestedContext()
...@@ -62,7 +62,7 @@ class TestSimple(unittest.TestCase): ...@@ -62,7 +62,7 @@ class TestSimple(unittest.TestCase):
""" """
view = TemplatePartial() view = TemplatePartial()
view.template_extension = 'txt' view.template_extension = 'txt'
assert_strings(self, view.render(), u"""Welcome self.assertString(view.render(), u"""Welcome
------- -------
## Again, Welcome! ##""") ## Again, Welcome! ##""")
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment