def testMultiargFilter(self): '''Test calling filter with multiply args''' multiarg_template = Template(name='multiarg-filter.html') multiarg_template.parse( '{{ simple_var|multiarg_filter:"John" "Peter" }}') result = multiarg_template.execute({'simple_var': 'Hello'}) self.assertResult(result, 'Hello, John, Peter')
def testMultiFilter(self): '''Test calling sequnce of filters''' multifilter_template = Template(name='multifilter.html') multifilter_template.parse( '{{ simple_var|simple_filter|argument_filter:"world" }}') result = multifilter_template.execute({'simple_var': 'Hello'}) self.assertResult(result, 'HELLO, world')
def setUp(self): self.value = 'value' self.variable_template = Template(name='base.html') self.variable_template.parse("{{ simple_var }}") self.object_field_template = Template(name='object-field.html') self.object_field_template.parse('{{ object.field }}') self.deep_template = Template(name='deep-field.html') self.deep_template.parse('{{ object.field.field }}')
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 testVaribaleArgFilter(self): '''Test calling filter with variable argument''' varargfilter_template = Template(name='vararg-filter.html') varargfilter_template.parse('{{ simple_var|argument_filter:arg }}') result = varargfilter_template.execute({ 'simple_var': 'Hello', 'arg': 'world' }) self.assertResult(result, 'Hello, world')
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))
class BlockTestCase(unittest.TestCase): """Test case for block template tag """ def setUp(self): self.base_template = Template(name='base.html') self.base_template.parse(BASE) def testExecuteTemplate(self): '''Test base template execution''' result = self.base_template.execute({'title': 'Hello'}) needed = BASE_RESULT % ('Hello') is_eq = fuzzy_equals(result, needed) assert is_eq, "Error template execution:\n%s" % ( "\n".join((result, "except", needed)))
class ExtendTestCase(BlockTestCase): """Test case for extend template tag """ def setUp(self): super(ExtendTestCase, self).setUp() self.extend_template = Template(loader=self.base_template.loader) self.extend_template.parse(EXTEND) def testExecuteTemplate(self): '''Test extending template execution''' result = self.extend_template.execute({'title': 'Hello'}) needed = EXTEND_RESULT % ('Hello') is_eq = fuzzy_equals(result, needed) assert is_eq, "Error template execution:\n%s" % ( "\n".join((result, "except", needed)))
class VariableFieldTestCase(unittest.TestCase): """Test case for block template tag """ def setUp(self): self.value = 'value' self.variable_template = Template(name='base.html') self.variable_template.parse("{{ simple_var }}") self.object_field_template = Template(name='object-field.html') self.object_field_template.parse('{{ object.field }}') self.deep_template = Template(name='deep-field.html') self.deep_template.parse('{{ object.field.field }}') def assertResult(self, result): assert result == self.value, 'Error emplate execution: %s' % ' '.join(( result, 'except', self.value)) def testSimpleVariable(self): '''Test simple variable accessing from template''' result = self.variable_template.execute({'simple_var': 'value'}) self.assertResult(result) def testObjectField(self): '''Test object's field accessing from template''' class TestClass(object): field = self.value result = self.object_field_template.execute({'object': TestClass()}) self.assertResult(result) def testDictValue(self): '''Test dict item accessing from template''' obj = {'field': self.value} result = self.object_field_template.execute({'object': obj}) self.assertResult(result) def testMultilevelField(self): '''Test accesing to dict item as object field''' class TestClass(object): field = {'field': self.value} result = self.deep_template.execute({'object': TestClass()}) self.assertResult(result)
def setUp(self): super(ExtendTestCase, self).setUp() self.extend_template = Template(loader=self.base_template.loader) self.extend_template.parse(EXTEND)
def testArgFilter(self): '''Test calling template filter with arg value specified''' argument_template = Template(name='argument-filter.html') argument_template.parse('{{ simple_var|argument_filter:"world" }}') result = argument_template.execute({'simple_var': 'Hello'}) self.assertResult(result, 'Hello, world')
def testNumericConstFilter(self): '''Test simple template filter function applied to number''' simple_template = Template(name="consts-filter.html") simple_template.parse('{{ 1|simple_filter }}') result = simple_template.execute({}) self.assertResult(result, '1')
def testStringConstFilter(self): '''Test simple template filter function applied to string constant''' simple_template = Template(name="consts-filter.html") simple_template.parse('{{ "hello"|simple_filter }}') result = simple_template.execute({}) self.assertResult(result, 'HELLO')
def testSimpleFilter(self): '''Test simple template filter function applied to variable''' simple_template = Template(name='simple-filter.html') simple_template.parse("{{ simple_var|simple_filter }}") result = simple_template.execute({'simple_var': 'Hello'}) self.assertResult(result, 'HELLO')
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 setUp(self): self.base_template = Template(name='base.html') self.base_template.parse(BASE)
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 setUp(self): base_template = Template(name="base") base_template.parse('{{ now }}{{ future }}') self.partial_template = base_template.partial({'now': 'Hello'}, 'part')