summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorChris Jerdonek <chris.jerdonek@gmail.com>2012-03-23 21:30:54 -0700
committerChris Jerdonek <chris.jerdonek@gmail.com>2012-03-23 21:30:54 -0700
commiteaee99c66735a7c2bb052bd14e828545f619c0b8 (patch)
treeda5028a5553e02130775b9898b26c92a096ef43c /tests
parent4260e313f56b242ad91cb93093c4a5fa2c64553c (diff)
downloadpystache-eaee99c66735a7c2bb052bd14e828545f619c0b8.tar.gz
Converted assert_strings() to a mixin.
Diffstat (limited to 'tests')
-rw-r--r--tests/common.py28
-rw-r--r--tests/test_examples.py16
-rw-r--r--tests/test_renderengine.py54
-rw-r--r--tests/test_simple.py6
4 files changed, 56 insertions, 48 deletions
diff --git a/tests/common.py b/tests/common.py
index f345630..cb703d7 100644
--- a/tests/common.py
+++ b/tests/common.py
@@ -14,23 +14,31 @@ def get_data_path(file_name):
return os.path.join(DATA_DIR, file_name)
-# TODO: convert this to a mixin.
-def assert_strings(test_case, actual, expected):
- # Show both friendly and literal versions.
- message = """\
+class AssertStringMixin:
+ """A unittest.TestCase mixin to check string equality."""
- Expected: \"""%s\"""
- Actual: \"""%s\"""
+ def assertString(self, actual, expected):
+ """
+ Assert that the given strings are equal and have the same type.
- Expected: %s
- Actual: %s""" % (expected, actual, repr(expected), repr(actual))
- test_case.assertEquals(actual, expected, message)
+ """
+ # Show both friendly and literal versions.
+ 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:
- """A mixin for adding assertIs() to a unittest.TestCase."""
+ """A unittest.TestCase mixin adding assertIs()."""
# unittest.assertIs() is not available until Python 2.7:
# http://docs.python.org/library/unittest.html#unittest.TestCase.assertIsNone
diff --git a/tests/test_examples.py b/tests/test_examples.py
index 5c95de9..e4b41bf 100644
--- a/tests/test_examples.py
+++ b/tests/test_examples.py
@@ -12,21 +12,21 @@ from examples.unicode_output import UnicodeOutput
from examples.unicode_input import UnicodeInput
from examples.nested_context import NestedContext
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):
renderer = Renderer()
actual = renderer.render(obj)
- assert_strings(self, actual, expected)
+ self.assertString(actual, expected)
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):
- self._assert(DoubleSection(), "* first\n* second\n* third")
+ self._assert(DoubleSection(), u"* first\n* second\n* third")
def test_unicode_output(self):
self.assertEquals(UnicodeOutput().render(), u'<p>Name: Henri Poincaré</p>')
@@ -36,7 +36,7 @@ class TestView(unittest.TestCase):
u'<p>If alive today, Henri Poincaré would be 156 years old.</p>')
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):
self.assertEquals(Unescaped().render(), "<h1>Bear > Shark</h1>")
@@ -48,7 +48,7 @@ Again, Welcome!""")
def test_template_partial_extension(self):
view = TemplatePartial()
view.template_extension = 'txt'
- assert_strings(self, view.render(), u"""Welcome
+ self.assertString(view.render(), u"""Welcome
-------
## Again, Welcome! ##""")
@@ -56,7 +56,7 @@ Again, Welcome!""")
def test_delimiters(self):
renderer = Renderer()
actual = renderer.render(Delimiters())
- assert_strings(self, actual, """\
+ self.assertString(actual, u"""\
* It worked the first time.
* And it worked the second time.
* Then, surprisingly, it worked the third time.
diff --git a/tests/test_renderengine.py b/tests/test_renderengine.py
index ccb94aa..6c2831a 100644
--- a/tests/test_renderengine.py
+++ b/tests/test_renderengine.py
@@ -11,7 +11,7 @@ import unittest
from pystache.context import Context
from pystache.parser import ParsingError
from pystache.renderengine import RenderEngine
-from tests.common import assert_strings
+from tests.common import AssertStringMixin
class RenderEngineTestCase(unittest.TestCase):
@@ -31,7 +31,7 @@ class RenderEngineTestCase(unittest.TestCase):
self.assertEquals(engine.load_partial, "foo")
-class RenderTests(unittest.TestCase):
+class RenderTests(unittest.TestCase, AssertStringMixin):
"""
Tests RenderEngine.render().
@@ -66,10 +66,10 @@ class RenderTests(unittest.TestCase):
actual = engine.render(template, context)
- assert_strings(test_case=self, actual=actual, expected=expected)
+ self.assertString(actual=actual, expected=expected)
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):
"""
@@ -80,7 +80,7 @@ class RenderTests(unittest.TestCase):
partials = {'partial': u"{{person}}"}
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):
"""
@@ -90,13 +90,13 @@ class RenderTests(unittest.TestCase):
engine = self._engine()
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):
template = "<h1>{{& thing}}</h1>"
context = {'thing': 'Bear > Giraffe'}
- expected = "<h1>Bear > Giraffe</h1>"
+ expected = u"<h1>Bear > Giraffe</h1>"
self._assert_render(expected, template, context)
@@ -108,7 +108,7 @@ class RenderTests(unittest.TestCase):
engine = self._engine()
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):
"""
@@ -122,7 +122,7 @@ class RenderTests(unittest.TestCase):
template = 'literal: {{{foo}}} escaped: {{foo}}'
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):
"""
@@ -147,7 +147,7 @@ class RenderTests(unittest.TestCase):
template = '{{foo1}} {{foo2}}'
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):
"""
@@ -166,7 +166,7 @@ class RenderTests(unittest.TestCase):
template = '{{text}} {{int}} {{{int}}}'
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):
"""
@@ -184,7 +184,7 @@ class RenderTests(unittest.TestCase):
"""
template = '{{test}}'
context = {'test': '{{#hello}}'}
- self._assert_render('{{#hello}}', template, context)
+ self._assert_render(u'{{#hello}}', template, context)
def test_interpolation__built_in_type__string(self):
"""
@@ -227,7 +227,7 @@ class RenderTests(unittest.TestCase):
template = """{{#test}}{{{.}}}{{/test}}"""
context = {'test': ['<', '>']}
- self._assert_render('<>', template, context)
+ self._assert_render(u'<>', template, context)
def test_implicit_iterator__escaped(self):
"""
@@ -237,7 +237,7 @@ class RenderTests(unittest.TestCase):
template = """{{#test}}{{.}}{{/test}}"""
context = {'test': ['<', '>']}
- self._assert_render('&lt;&gt;', template, context)
+ self._assert_render(u'&lt;&gt;', template, context)
def test_literal__in_section(self):
"""
@@ -247,7 +247,7 @@ class RenderTests(unittest.TestCase):
template = '{{#test}}1 {{{less_than}}} 2{{/test}}'
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):
"""
@@ -258,11 +258,11 @@ class RenderTests(unittest.TestCase):
partials = {'partial': '1 {{{less_than}}} 2'}
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):
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):
"""
@@ -275,7 +275,7 @@ class RenderTests(unittest.TestCase):
partials = {'partial': 'unescaped: {{{foo}}} escaped: {{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.
@@ -311,7 +311,7 @@ class RenderTests(unittest.TestCase):
template = '{{#test}}unescaped: {{{foo}}} escaped: {{foo}}{{/test}}'
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):
"""
@@ -338,7 +338,7 @@ class RenderTests(unittest.TestCase):
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):
"""
@@ -382,7 +382,7 @@ class RenderTests(unittest.TestCase):
def test_section__lambda(self):
template = '{{#test}}Mom{{/test}}'
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):
"""
@@ -392,10 +392,10 @@ class RenderTests(unittest.TestCase):
template = '{{#iterable}}{{.}}{{/iterable}}'
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'
- self._assert_render('0123', template, context)
+ self._assert_render(u'0123', template, context)
d = {'foo': 0, 'bar': 0}
# We don't know what order of keys we'll be given, but from the
@@ -403,7 +403,7 @@ class RenderTests(unittest.TestCase):
# "If items(), keys(), values(), iteritems(), iterkeys(), and
# itervalues() are called with no intervening modifications to
# the dictionary, the lists will directly correspond."
- expected = ''.join(d.keys())
+ expected = u''.join(d.keys())
context = {'iterable': d.iterkeys()} # type 'dictionary-keyiterator'
self._assert_render(expected, template, context)
@@ -422,15 +422,15 @@ class RenderTests(unittest.TestCase):
"""
template = '{{#test}}Hi {{person}}{{/test}}'
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):
"""
Check that multiline comments are permitted.
"""
- self._assert_render('foobar', 'foo{{! baz }}bar')
- self._assert_render('foobar', 'foo{{! \nbaz }}bar')
+ self._assert_render(u'foobar', 'foo{{! baz }}bar')
+ self._assert_render(u'foobar', 'foo{{! \nbaz }}bar')
def test_custom_delimiters__sections(self):
"""
diff --git a/tests/test_simple.py b/tests/test_simple.py
index 91661f9..3b5f188 100644
--- a/tests/test_simple.py
+++ b/tests/test_simple.py
@@ -8,10 +8,10 @@ from examples.lambdas import Lambdas
from examples.template_partial import TemplatePartial
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):
view = NestedContext()
@@ -62,7 +62,7 @@ class TestSimple(unittest.TestCase):
"""
view = TemplatePartial()
view.template_extension = 'txt'
- assert_strings(self, view.render(), u"""Welcome
+ self.assertString(view.render(), u"""Welcome
-------
## Again, Welcome! ##""")