1 from django
import template
2 from django
.utils
.unittest
import TestCase
4 class CallableVariablesTests(TestCase
):
6 def test_callable(self
):
9 def __init__(self
, value
):
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
):
38 def __init__(self
, value
):
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
57 self
.assertEqual(my_doodad
.num_calls
, 0)
59 def test_do_not_call(self
):
62 do_not_call_in_templates
= True
63 def __init__(self
, value
):
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
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
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.
92 do_not_call_in_templates
= True
94 def __init__(self
, value
):
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)