Commit d13a4c43 by Chris Jerdonek

Refactoring: removed some of the cut-and-paste from test_pystache.py.

parent f5e9950b
...@@ -29,38 +29,38 @@ class PystacheTests(object): ...@@ -29,38 +29,38 @@ class PystacheTests(object):
def test_less_basic(self): def test_less_basic(self):
template = "It's a nice day for {{beverage}}, right {{person}}?" template = "It's a nice day for {{beverage}}, right {{person}}?"
ret = pystache.render(template, { 'beverage': 'soda', 'person': 'Bob' }) context = { 'beverage': 'soda', 'person': 'Bob' }
self.assertEquals(ret, "It's a nice day for soda, right Bob?") self._assert_rendered("It's a nice day for soda, right Bob?", template, context)
def test_even_less_basic(self): def test_even_less_basic(self):
template = "I think {{name}} wants a {{thing}}, right {{name}}?" template = "I think {{name}} wants a {{thing}}, right {{name}}?"
ret = pystache.render(template, { 'name': 'Jon', 'thing': 'racecar' }) context = { 'name': 'Jon', 'thing': 'racecar' }
self.assertEquals(ret, "I think Jon wants a racecar, right Jon?") self._assert_rendered("I think Jon wants a racecar, right Jon?", template, context)
def test_ignores_misses(self): def test_ignores_misses(self):
template = "I think {{name}} wants a {{thing}}, right {{name}}?" template = "I think {{name}} wants a {{thing}}, right {{name}}?"
ret = pystache.render(template, { 'name': 'Jon' }) context = { 'name': 'Jon' }
self.assertEquals(ret, "I think Jon wants a , right Jon?") self._assert_rendered("I think Jon wants a , right Jon?", template, context)
def test_render_zero(self): def test_render_zero(self):
template = 'My value is {{value}}.' template = 'My value is {{value}}.'
ret = pystache.render(template, { 'value': 0 }) context = { 'value': 0 }
self.assertEquals(ret, 'My value is 0.') self._assert_rendered('My value is 0.', template, context)
def test_comments(self): def test_comments(self):
template = "What {{! the }} what?" template = "What {{! the }} what?"
ret = pystache.render(template) actual = pystache.render(template)
self.assertEquals(ret, "What what?") self.assertEquals("What what?", actual)
def test_false_sections_are_hidden(self): def test_false_sections_are_hidden(self):
template = "Ready {{#set}}set {{/set}}go!" template = "Ready {{#set}}set {{/set}}go!"
ret = pystache.render(template, { 'set': False }) context = { 'set': False }
self.assertEquals(ret, "Ready go!") self._assert_rendered("Ready go!", template, context)
def test_true_sections_are_shown(self): def test_true_sections_are_shown(self):
template = "Ready {{#set}}set{{/set}} go!" template = "Ready {{#set}}set{{/set}} go!"
ret = pystache.render(template, { 'set': True }) context = { 'set': True }
self.assertEquals(ret, "Ready set go!") self._assert_rendered("Ready set go!", template, context)
non_strings_expected = """(123 & ['something'])(chris & 0.9)""" non_strings_expected = """(123 & ['something'])(chris & 0.9)"""
...@@ -69,34 +69,32 @@ class PystacheTests(object): ...@@ -69,34 +69,32 @@ class PystacheTests(object):
stats = [] stats = []
stats.append({'key': 123, 'value': ['something']}) stats.append({'key': 123, 'value': ['something']})
stats.append({'key': u"chris", 'value': 0.900}) stats.append({'key': u"chris", 'value': 0.900})
context = { 'stats': stats }
ret = pystache.render(template, { 'stats': stats }) self._assert_rendered(self.non_strings_expected, template, context)
self.assertEquals(ret, self.non_strings_expected)
def test_unicode(self): def test_unicode(self):
template = 'Name: {{name}}; Age: {{age}}' template = 'Name: {{name}}; Age: {{age}}'
ret = pystache.render(template, { 'name': u'Henri Poincaré', context = {'name': u'Henri Poincaré', 'age': 156 }
'age': 156 }) self._assert_rendered(u'Name: Henri Poincaré; Age: 156', template, context)
self.assertEquals(ret, u'Name: Henri Poincaré; Age: 156')
def test_sections(self): def test_sections(self):
template = """<ul>{{#users}}<li>{{name}}</li>{{/users}}</ul>""" template = """<ul>{{#users}}<li>{{name}}</li>{{/users}}</ul>"""
context = { 'users': [ {'name': 'Chris'}, {'name': 'Tom'}, {'name': 'PJ'} ] } context = { 'users': [ {'name': 'Chris'}, {'name': 'Tom'}, {'name': 'PJ'} ] }
ret = pystache.render(template, context) expected = """<ul><li>Chris</li><li>Tom</li><li>PJ</li></ul>"""
self.assertEquals(ret, """<ul><li>Chris</li><li>Tom</li><li>PJ</li></ul>""") self._assert_rendered(expected, template, context)
def test_implicit_iterator(self): def test_implicit_iterator(self):
template = """<ul>{{#users}}<li>{{.}}</li>{{/users}}</ul>""" template = """<ul>{{#users}}<li>{{.}}</li>{{/users}}</ul>"""
context = { 'users': [ 'Chris', 'Tom','PJ' ] } context = { 'users': [ 'Chris', 'Tom','PJ' ] }
ret = pystache.render(template, context) expected = """<ul><li>Chris</li><li>Tom</li><li>PJ</li></ul>"""
self.assertEquals(ret, """<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. # The spec says that sections should not alter surrounding whitespace.
def test_surrounding_whitepace_not_altered(self): def test_surrounding_whitepace_not_altered(self):
template = "first{{#spacing}} second {{/spacing}}third" template = "first{{#spacing}} second {{/spacing}}third"
ret = pystache.render(template, {"spacing": True}) context = {"spacing": True}
self.assertEquals(ret, "first second third") self._assert_rendered("first second third", template, context)
def test__section__non_false_value(self): def test__section__non_false_value(self):
""" """
...@@ -123,8 +121,7 @@ class PystacheTests(object): ...@@ -123,8 +121,7 @@ class PystacheTests(object):
""" """
template = """{{#s1}}foo{{/s1}} {{#s2}}<{{/s2}}""" template = """{{#s1}}foo{{/s1}} {{#s2}}<{{/s2}}"""
context = {'s1': True, 's2': [True]} context = {'s1': True, 's2': [True]}
actual = pystache.render(template, context) self._assert_rendered("foo <", template, context)
self.assertEquals(actual, """foo <""")
class PystacheWithoutMarkupsafeTests(PystacheTests, unittest.TestCase): class PystacheWithoutMarkupsafeTests(PystacheTests, unittest.TestCase):
......
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