Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Tree: 6d7d85239c
Fetching contributors…

Cannot retrieve contributors at this time

69 lines (57 sloc) 2.521 kB
'''Module to test default template tags such as if, for, with, include, etc.
'''
import unittest
from lighty.templates import Template
from lighty.templates.loaders import FSLoader
class DefaultTagsTestCase(unittest.TestCase):
"""Test case for if template tag
"""
def assertResult(self, name, result, value):
assert result == value, 'Error on tag "%s" applying to: %s' % (
name, ' '.join((str(result), 'except', str(value))))
def testSpacelless(self):
'''Test spaceless template tag'''
template = Template()
template.parse('''{% spaceless %}
Some
broken
text
{% endspaceless %}''')
result = template({})
right = 'Some broken text'
assert result == right, 'Spaceless tag error:\n%s' % (
"\n".join(result, 'except', right))
def testSimpleWith(self):
'''Test with template tag'''
template = Template()
template.parse('{% with user.name as name %}{{ name }}{% endwith %}')
result = template({'user': {'name': 'John'}})
self.assertResult('with', result.strip(), 'John')
def testSimpleIf(self):
'''Test if template tag'''
template = Template()
template.parse('{% if a %}Foo{% endif %}')
result = template({'a': 1})
self.assertResult('if', result.strip(), 'Foo')
result = template({'a': 0})
self.assertResult('if', result.strip(), '')
def testSimpleFor(self):
'''Test for template tag'''
template = Template()
template.parse('{% for a in list %}{{ a }} {% endfor %}')
result = template({'list': [1, 2, 3, 4, 5]})
self.assertResult('for', result.strip(), '1 2 3 4 5')
def testSimpleInclude(self):
'''Test include template tag'''
template = Template('{% include "simple.html" %}', name="test.html",
loader=FSLoader(['tests/templates']))
result = template({'name': 'Peter'})
self.assertResult('include', result.strip(), 'Hello, Peter')
def test():
suite = unittest.TestSuite()
suite.addTest(DefaultTagsTestCase('testSpacelless'))
suite.addTest(DefaultTagsTestCase('testSimpleWith'))
suite.addTest(DefaultTagsTestCase('testSimpleIf'))
suite.addTest(DefaultTagsTestCase('testSimpleFor'))
suite.addTest(DefaultTagsTestCase('testSimpleInclude'))
return suite
Jump to Line
Something went wrong with that request. Please try again.