App Engine Python SDK version 1.7.4 (2)
[gae.git] / python / lib / django_1_4 / tests / regressiontests / templates / callables.py
blob8afa703f63b442a6e51deda01e0ba020dd0bd013
1 from django import template
2 from django.utils.unittest import TestCase
4 class CallableVariablesTests(TestCase):
6 def test_callable(self):
8 class Doodad(object):
9 def __init__(self, value):
10 self.num_calls = 0
11 self.value = value
12 def __call__(self):
13 self.num_calls += 1
14 return {"the_value": self.value}
16 my_doodad = Doodad(42)
17 c = template.Context({"my_doodad": my_doodad})
19 # We can't access ``my_doodad.value`` in the template, because
20 # ``my_doodad.__call__`` will be invoked first, yielding a dictionary
21 # without a key ``value``.
22 t = template.Template('{{ my_doodad.value }}')
23 self.assertEqual(t.render(c), u'')
25 # We can confirm that the doodad has been called
26 self.assertEqual(my_doodad.num_calls, 1)
28 # But we can access keys on the dict that's returned
29 # by ``__call__``, instead.
30 t = template.Template('{{ my_doodad.the_value }}')
31 self.assertEqual(t.render(c), u'42')
32 self.assertEqual(my_doodad.num_calls, 2)
34 def test_alters_data(self):
36 class Doodad(object):
37 alters_data = True
38 def __init__(self, value):
39 self.num_calls = 0
40 self.value = value
41 def __call__(self):
42 self.num_calls += 1
43 return {"the_value": self.value}
45 my_doodad = Doodad(42)
46 c = template.Context({"my_doodad": my_doodad})
48 # Since ``my_doodad.alters_data`` is True, the template system will not
49 # try to call our doodad but will use TEMPLATE_STRING_IF_INVALID
50 t = template.Template('{{ my_doodad.value }}')
51 self.assertEqual(t.render(c), u'')
52 t = template.Template('{{ my_doodad.the_value }}')
53 self.assertEqual(t.render(c), u'')
55 # Double-check that the object was really never called during the
56 # template rendering.
57 self.assertEqual(my_doodad.num_calls, 0)
59 def test_do_not_call(self):
61 class Doodad(object):
62 do_not_call_in_templates = True
63 def __init__(self, value):
64 self.num_calls = 0
65 self.value = value
66 def __call__(self):
67 self.num_calls += 1
68 return {"the_value": self.value}
70 my_doodad = Doodad(42)
71 c = template.Context({"my_doodad": my_doodad})
73 # Since ``my_doodad.do_not_call_in_templates`` is True, the template
74 # system will not try to call our doodad. We can access its attributes
75 # as normal, and we don't have access to the dict that it returns when
76 # called.
77 t = template.Template('{{ my_doodad.value }}')
78 self.assertEqual(t.render(c), u'42')
79 t = template.Template('{{ my_doodad.the_value }}')
80 self.assertEqual(t.render(c), u'')
82 # Double-check that the object was really never called during the
83 # template rendering.
84 self.assertEqual(my_doodad.num_calls, 0)
86 def test_do_not_call_and_alters_data(self):
87 # If we combine ``alters_data`` and ``do_not_call_in_templates``, the
88 # ``alters_data`` attribute will not make any difference in the
89 # template system's behavior.
91 class Doodad(object):
92 do_not_call_in_templates = True
93 alters_data = True
94 def __init__(self, value):
95 self.num_calls = 0
96 self.value = value
97 def __call__(self):
98 self.num_calls += 1
99 return {"the_value": self.value}
101 my_doodad = Doodad(42)
102 c = template.Context({"my_doodad": my_doodad})
104 t = template.Template('{{ my_doodad.value }}')
105 self.assertEqual(t.render(c), u'42')
106 t = template.Template('{{ my_doodad.the_value }}')
107 self.assertEqual(t.render(c), u'')
109 # Double-check that the object was really never called during the
110 # template rendering.
111 self.assertEqual(my_doodad.num_calls, 0)