PageRenderTime 22ms CodeModel.GetById 8ms app.highlight 11ms RepoModel.GetById 1ms app.codeStats 1ms

/tests/regressiontests/templates/custom.py

https://code.google.com/p/mango-py/
Python | 80 lines | 57 code | 19 blank | 4 comment | 0 complexity | cb19c15e786c1556e120e2265c010dde MD5 | raw file
 1from django import template
 2from django.utils.unittest import TestCase
 3from templatetags import custom
 4
 5class CustomFilterTests(TestCase):
 6    def test_filter(self):
 7        t = template.Template("{% load custom %}{{ string|trim:5 }}")
 8        self.assertEqual(
 9            t.render(template.Context({"string": "abcdefghijklmnopqrstuvwxyz"})),
10            u"abcde"
11        )
12
13
14class CustomTagTests(TestCase):
15    def verify_tag(self, tag, name):
16        self.assertEqual(tag.__name__, name)
17        self.assertEqual(tag.__doc__, 'Expected %s __doc__' % name)
18        self.assertEqual(tag.__dict__['anything'], 'Expected %s __dict__' % name)
19
20    def test_simple_tags(self):
21        c = template.Context({'value': 42})
22
23        t = template.Template('{% load custom %}{% no_params %}')
24        self.assertEqual(t.render(c), u'no_params - Expected result')
25
26        t = template.Template('{% load custom %}{% one_param 37 %}')
27        self.assertEqual(t.render(c), u'one_param - Expected result: 37')
28
29        t = template.Template('{% load custom %}{% explicit_no_context 37 %}')
30        self.assertEqual(t.render(c), u'explicit_no_context - Expected result: 37')
31
32        t = template.Template('{% load custom %}{% no_params_with_context %}')
33        self.assertEqual(t.render(c), u'no_params_with_context - Expected result (context value: 42)')
34
35        t = template.Template('{% load custom %}{% params_and_context 37 %}')
36        self.assertEqual(t.render(c), u'params_and_context - Expected result (context value: 42): 37')
37
38    def test_simple_tag_registration(self):
39        # Test that the decorators preserve the decorated function's docstring, name and attributes.
40        self.verify_tag(custom.no_params, 'no_params')
41        self.verify_tag(custom.one_param, 'one_param')
42        self.verify_tag(custom.explicit_no_context, 'explicit_no_context')
43        self.verify_tag(custom.no_params_with_context, 'no_params_with_context')
44        self.verify_tag(custom.params_and_context, 'params_and_context')
45
46    def test_simple_tag_missing_context(self):
47        # That the 'context' parameter must be present when takes_context is True
48        def a_simple_tag_without_parameters(arg):
49            """Expected __doc__"""
50            return "Expected result"
51
52        register = template.Library()
53        decorator = register.simple_tag(takes_context=True)
54        self.assertRaises(template.TemplateSyntaxError, decorator, a_simple_tag_without_parameters)
55
56    def test_inclusion_tags(self):
57        c = template.Context({'value': 42})
58
59        t = template.Template('{% load custom %}{% inclusion_no_params %}')
60        self.assertEquals(t.render(c), u'inclusion_no_params - Expected result\n')
61
62        t = template.Template('{% load custom %}{% inclusion_one_param 37 %}')
63        self.assertEquals(t.render(c), u'inclusion_one_param - Expected result: 37\n')
64
65        t = template.Template('{% load custom %}{% inclusion_explicit_no_context 37 %}')
66        self.assertEquals(t.render(c), u'inclusion_explicit_no_context - Expected result: 37\n')
67
68        t = template.Template('{% load custom %}{% inclusion_no_params_with_context %}')
69        self.assertEquals(t.render(c), u'inclusion_no_params_with_context - Expected result (context value: 42)\n')
70
71        t = template.Template('{% load custom %}{% inclusion_params_and_context 37 %}')
72        self.assertEquals(t.render(c), u'inclusion_params_and_context - Expected result (context value: 42): 37\n')
73
74    def test_inclusion_tag_registration(self):
75        # Test that the decorators preserve the decorated function's docstring, name and attributes.
76        self.verify_tag(custom.inclusion_no_params, 'inclusion_no_params')
77        self.verify_tag(custom.inclusion_one_param, 'inclusion_one_param')
78        self.verify_tag(custom.inclusion_explicit_no_context, 'inclusion_explicit_no_context')
79        self.verify_tag(custom.inclusion_no_params_with_context, 'inclusion_no_params_with_context')
80        self.verify_tag(custom.inclusion_params_and_context, 'inclusion_params_and_context')