summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorJannis Leidel <jannis@leidel.info>2011-05-03 11:52:42 +0000
committerJannis Leidel <jannis@leidel.info>2011-05-03 11:52:42 +0000
commit950e05c3ff5daa360c3bddb84831d40b167062f1 (patch)
treefc7e06e7303e603d3dc61872b147999898779032 /tests
parent8ce352c21d9ce4c59fcd259103350772954a6f8e (diff)
Fixed #14262 -- Added new assignment_tag as a simple way to assign the result of a template tag to a context variable. Thanks, Julien Phalip.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@16149 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'tests')
-rw-r--r--tests/regressiontests/templates/custom.py53
-rw-r--r--tests/regressiontests/templates/templatetags/custom.py30
2 files changed, 83 insertions, 0 deletions
diff --git a/tests/regressiontests/templates/custom.py b/tests/regressiontests/templates/custom.py
index 7ca359d976..3ce4d99547 100644
--- a/tests/regressiontests/templates/custom.py
+++ b/tests/regressiontests/templates/custom.py
@@ -1,3 +1,5 @@
+from __future__ import with_statement
+
from django import template
from django.utils.unittest import TestCase
from templatetags import custom
@@ -102,3 +104,54 @@ class CustomTagTests(TestCase):
c.use_l10n = True
self.assertEquals(t.render(c).strip(), u'True')
+
+ def test_assignment_tags(self):
+ c = template.Context({'value': 42})
+
+ t = template.Template('{% load custom %}{% assignment_no_params as var %}The result is: {{ var }}')
+ self.assertEqual(t.render(c), u'The result is: assignment_no_params - Expected result')
+
+ t = template.Template('{% load custom %}{% assignment_one_param 37 as var %}The result is: {{ var }}')
+ self.assertEqual(t.render(c), u'The result is: assignment_one_param - Expected result: 37')
+
+ t = template.Template('{% load custom %}{% assignment_explicit_no_context 37 as var %}The result is: {{ var }}')
+ self.assertEqual(t.render(c), u'The result is: assignment_explicit_no_context - Expected result: 37')
+
+ t = template.Template('{% load custom %}{% assignment_no_params_with_context as var %}The result is: {{ var }}')
+ self.assertEqual(t.render(c), u'The result is: assignment_no_params_with_context - Expected result (context value: 42)')
+
+ t = template.Template('{% load custom %}{% assignment_params_and_context 37 as var %}The result is: {{ var }}')
+ self.assertEqual(t.render(c), u'The result is: assignment_params_and_context - Expected result (context value: 42): 37')
+
+ self.assertRaisesRegexp(template.TemplateSyntaxError,
+ "'assignment_one_param' tag takes at least 2 arguments and the second last argument must be 'as'",
+ template.Template, '{% load custom %}{% assignment_one_param 37 %}The result is: {{ var }}')
+
+ self.assertRaisesRegexp(template.TemplateSyntaxError,
+ "'assignment_one_param' tag takes at least 2 arguments and the second last argument must be 'as'",
+ template.Template, '{% load custom %}{% assignment_one_param 37 as %}The result is: {{ var }}')
+
+ self.assertRaisesRegexp(template.TemplateSyntaxError,
+ "'assignment_one_param' tag takes at least 2 arguments and the second last argument must be 'as'",
+ template.Template, '{% load custom %}{% assignment_one_param 37 ass var %}The result is: {{ var }}')
+
+ def test_assignment_tag_registration(self):
+ # Test that the decorators preserve the decorated function's docstring, name and attributes.
+ self.verify_tag(custom.assignment_no_params, 'assignment_no_params')
+ self.verify_tag(custom.assignment_one_param, 'assignment_one_param')
+ self.verify_tag(custom.assignment_explicit_no_context, 'assignment_explicit_no_context')
+ self.verify_tag(custom.assignment_no_params_with_context, 'assignment_no_params_with_context')
+ self.verify_tag(custom.assignment_params_and_context, 'assignment_params_and_context')
+
+ def test_assignment_tag_missing_context(self):
+ # That the 'context' parameter must be present when takes_context is True
+ def an_assignment_tag_without_parameters(arg):
+ """Expected __doc__"""
+ return "Expected result"
+
+ register = template.Library()
+ decorator = register.assignment_tag(takes_context=True)
+
+ self.assertRaisesRegexp(template.TemplateSyntaxError,
+ "Any tag function decorated with takes_context=True must have a first argument of 'context'",
+ decorator, an_assignment_tag_without_parameters)
diff --git a/tests/regressiontests/templates/templatetags/custom.py b/tests/regressiontests/templates/templatetags/custom.py
index 8db113a6f1..2e281f7b0f 100644
--- a/tests/regressiontests/templates/templatetags/custom.py
+++ b/tests/regressiontests/templates/templatetags/custom.py
@@ -84,3 +84,33 @@ def use_l10n(context):
@register.inclusion_tag('test_incl_tag_use_l10n.html', takes_context=True)
def inclusion_tag_use_l10n(context):
return {}
+
+@register.assignment_tag
+def assignment_no_params():
+ """Expected assignment_no_params __doc__"""
+ return "assignment_no_params - Expected result"
+assignment_no_params.anything = "Expected assignment_no_params __dict__"
+
+@register.assignment_tag
+def assignment_one_param(arg):
+ """Expected assignment_one_param __doc__"""
+ return "assignment_one_param - Expected result: %s" % arg
+assignment_one_param.anything = "Expected assignment_one_param __dict__"
+
+@register.assignment_tag(takes_context=False)
+def assignment_explicit_no_context(arg):
+ """Expected assignment_explicit_no_context __doc__"""
+ return "assignment_explicit_no_context - Expected result: %s" % arg
+assignment_explicit_no_context.anything = "Expected assignment_explicit_no_context __dict__"
+
+@register.assignment_tag(takes_context=True)
+def assignment_no_params_with_context(context):
+ """Expected assignment_no_params_with_context __doc__"""
+ return "assignment_no_params_with_context - Expected result (context value: %s)" % context['value']
+assignment_no_params_with_context.anything = "Expected assignment_no_params_with_context __dict__"
+
+@register.assignment_tag(takes_context=True)
+def assignment_params_and_context(context, arg):
+ """Expected assignment_params_and_context __doc__"""
+ return "assignment_params_and_context - Expected result (context value: %s): %s" % (context['value'], arg)
+assignment_params_and_context.anything = "Expected assignment_params_and_context __dict__"