summaryrefslogtreecommitdiff
path: root/tests/test_pystache.py
diff options
context:
space:
mode:
Diffstat (limited to 'tests/test_pystache.py')
-rw-r--r--tests/test_pystache.py93
1 files changed, 65 insertions, 28 deletions
diff --git a/tests/test_pystache.py b/tests/test_pystache.py
index c04489b..f9857cd 100644
--- a/tests/test_pystache.py
+++ b/tests/test_pystache.py
@@ -2,8 +2,15 @@
import unittest
import pystache
+from pystache import renderer
+
+
+class PystacheTests(unittest.TestCase):
+
+ def _assert_rendered(self, expected, template, context):
+ actual = pystache.render(template, context)
+ self.assertEquals(actual, expected)
-class TestPystache(unittest.TestCase):
def test_basic(self):
ret = pystache.render("Hi {{thing}}!", { 'thing': 'world' })
self.assertEquals(ret, "Hi world!")
@@ -14,66 +21,96 @@ class TestPystache(unittest.TestCase):
def test_less_basic(self):
template = "It's a nice day for {{beverage}}, right {{person}}?"
- ret = pystache.render(template, { 'beverage': 'soda', 'person': 'Bob' })
- self.assertEquals(ret, "It's a nice day for soda, right Bob?")
+ context = { 'beverage': 'soda', 'person': 'Bob' }
+ self._assert_rendered("It's a nice day for soda, right Bob?", template, context)
def test_even_less_basic(self):
template = "I think {{name}} wants a {{thing}}, right {{name}}?"
- ret = pystache.render(template, { 'name': 'Jon', 'thing': 'racecar' })
- self.assertEquals(ret, "I think Jon wants a racecar, right Jon?")
+ context = { 'name': 'Jon', 'thing': 'racecar' }
+ self._assert_rendered("I think Jon wants a racecar, right Jon?", template, context)
def test_ignores_misses(self):
template = "I think {{name}} wants a {{thing}}, right {{name}}?"
- ret = pystache.render(template, { 'name': 'Jon' })
- self.assertEquals(ret, "I think Jon wants a , right Jon?")
+ context = { 'name': 'Jon' }
+ self._assert_rendered("I think Jon wants a , right Jon?", template, context)
def test_render_zero(self):
template = 'My value is {{value}}.'
- ret = pystache.render(template, { 'value': 0 })
- self.assertEquals(ret, 'My value is 0.')
+ context = { 'value': 0 }
+ self._assert_rendered('My value is 0.', template, context)
def test_comments(self):
template = "What {{! the }} what?"
- ret = pystache.render(template)
- self.assertEquals(ret, "What what?")
+ actual = pystache.render(template)
+ self.assertEquals("What what?", actual)
def test_false_sections_are_hidden(self):
template = "Ready {{#set}}set {{/set}}go!"
- ret = pystache.render(template, { 'set': False })
- self.assertEquals(ret, "Ready go!")
+ context = { 'set': False }
+ self._assert_rendered("Ready go!", template, context)
def test_true_sections_are_shown(self):
template = "Ready {{#set}}set{{/set}} go!"
- ret = pystache.render(template, { 'set': True })
- self.assertEquals(ret, "Ready set go!")
+ context = { 'set': True }
+ self._assert_rendered("Ready set go!", template, context)
+
+ non_strings_expected = """(123 & ['something'])(chris & 0.9)"""
def test_non_strings(self):
template = "{{#stats}}({{key}} & {{value}}){{/stats}}"
stats = []
stats.append({'key': 123, 'value': ['something']})
stats.append({'key': u"chris", 'value': 0.900})
-
- ret = pystache.render(template, { 'stats': stats })
- self.assertEquals(ret, """(123 & ['something'])(chris & 0.9)""")
+ context = { 'stats': stats }
+ self._assert_rendered(self.non_strings_expected, template, context)
def test_unicode(self):
template = 'Name: {{name}}; Age: {{age}}'
- ret = pystache.render(template, { 'name': u'Henri Poincaré',
- 'age': 156 })
- self.assertEquals(ret, u'Name: Henri Poincaré; Age: 156')
+ context = {'name': u'Henri Poincaré', 'age': 156 }
+ self._assert_rendered(u'Name: Henri Poincaré; Age: 156', template, context)
def test_sections(self):
template = """<ul>{{#users}}<li>{{name}}</li>{{/users}}</ul>"""
context = { 'users': [ {'name': 'Chris'}, {'name': 'Tom'}, {'name': 'PJ'} ] }
- ret = pystache.render(template, context)
- self.assertEquals(ret, """<ul><li>Chris</li><li>Tom</li><li>PJ</li></ul>""")
-
+ expected = """<ul><li>Chris</li><li>Tom</li><li>PJ</li></ul>"""
+ self._assert_rendered(expected, template, context)
+
def test_implicit_iterator(self):
template = """<ul>{{#users}}<li>{{.}}</li>{{/users}}</ul>"""
context = { 'users': [ 'Chris', 'Tom','PJ' ] }
- ret = pystache.render(template, context)
- self.assertEquals(ret, """<ul><li>Chris</li><li>Tom</li><li>PJ</li></ul>""")
+ expected = """<ul><li>Chris</li><li>Tom</li><li>PJ</li></ul>"""
+ self._assert_rendered(expected, template, context)
+
+ # The spec says that sections should not alter surrounding whitespace.
+ def test_surrounding_whitepace_not_altered(self):
+ template = "first{{#spacing}} second {{/spacing}}third"
+ context = {"spacing": True}
+ self._assert_rendered("first second third", template, context)
+
+ def test__section__non_false_value(self):
+ """
+ Test when a section value is a (non-list) "non-false value".
+
+ From mustache(5):
+
+ When the value [of a section key] is non-false but not a list, it
+ will be used as the context for a single rendering of the block.
+
+ """
+ template = """{{#person}}Hi {{name}}{{/person}}"""
+ context = {"person": {"name": "Jon"}}
+ self._assert_rendered("Hi Jon", template, context)
+
+ def test_later_list_section_with_escapable_character(self):
+ """
+ This is a simple test case intended to cover issue #53.
+
+ The test case failed with markupsafe enabled, as follows:
+
+ AssertionError: Markup(u'foo &lt;') != 'foo <'
-if __name__ == '__main__':
- unittest.main()
+ """
+ template = """{{#s1}}foo{{/s1}} {{#s2}}<{{/s2}}"""
+ context = {'s1': True, 's2': [True]}
+ self._assert_rendered("foo <", template, context)