2012-06-08 00:08:47 +08:00
from __future__ import absolute_import , unicode_literals
2011-10-14 05:34:56 +08:00
2013-07-01 20:22:27 +08:00
from unittest import TestCase
2009-03-25 11:45:56 +08:00
from django import template
2012-09-08 01:17:09 +08:00
from django . utils import six
2011-10-14 05:34:56 +08:00
from . templatetags import custom
2009-03-25 11:45:56 +08:00
2010-12-19 23:00:50 +08:00
class CustomFilterTests ( TestCase ) :
2010-11-04 12:47:05 +08:00
def test_filter ( self ) :
t = template . Template ( " { % lo ad custom % } {{ string|trim:5 }} " )
self . assertEqual (
t . render ( template . Context ( { " string " : " abcdefghijklmnopqrstuvwxyz " } ) ) ,
2012-06-08 00:08:47 +08:00
" abcde "
2010-11-04 12:47:05 +08:00
)
2010-12-19 23:00:50 +08:00
class CustomTagTests ( TestCase ) :
def verify_tag ( self , tag , name ) :
2011-03-03 23:04:39 +08:00
self . assertEqual ( tag . __name__ , name )
self . assertEqual ( tag . __doc__ , ' Expected %s __doc__ ' % name )
self . assertEqual ( tag . __dict__ [ ' anything ' ] , ' Expected %s __dict__ ' % name )
2010-12-19 23:00:50 +08:00
def test_simple_tags ( self ) :
c = template . Context ( { ' value ' : 42 } )
t = template . Template ( ' { % lo ad custom % } { % no_params % } ' )
2012-06-08 00:08:47 +08:00
self . assertEqual ( t . render ( c ) , ' no_params - Expected result ' )
2010-12-19 23:00:50 +08:00
t = template . Template ( ' { % lo ad custom % } { % o ne_param 37 % } ' )
2012-06-08 00:08:47 +08:00
self . assertEqual ( t . render ( c ) , ' one_param - Expected result: 37 ' )
2010-12-19 23:00:50 +08:00
t = template . Template ( ' { % lo ad custom % } { % e xplicit_no_context 37 % } ' )
2012-06-08 00:08:47 +08:00
self . assertEqual ( t . render ( c ) , ' explicit_no_context - Expected result: 37 ' )
2010-12-19 23:00:50 +08:00
t = template . Template ( ' { % lo ad custom % } { % no_params_with_context % } ' )
2012-06-08 00:08:47 +08:00
self . assertEqual ( t . render ( c ) , ' no_params_with_context - Expected result (context value: 42) ' )
2010-12-19 23:00:50 +08:00
t = template . Template ( ' { % lo ad custom % } { % params_and_context 37 % } ' )
2012-06-08 00:08:47 +08:00
self . assertEqual ( t . render ( c ) , ' params_and_context - Expected result (context value: 42): 37 ' )
2010-12-19 23:00:50 +08:00
2011-09-27 20:15:15 +08:00
t = template . Template ( ' { % lo ad custom % } { % s imple_two_params 37 42 % } ' )
2012-06-08 00:08:47 +08:00
self . assertEqual ( t . render ( c ) , ' simple_two_params - Expected result: 37, 42 ' )
2011-09-27 20:15:15 +08:00
t = template . Template ( ' { % lo ad custom % } { % s imple_one_default 37 % } ' )
2012-06-08 00:08:47 +08:00
self . assertEqual ( t . render ( c ) , ' simple_one_default - Expected result: 37, hi ' )
2011-09-27 20:15:15 +08:00
t = template . Template ( ' { % lo ad custom % } { % s imple_one_default 37 two= " hello " % } ' )
2012-06-08 00:08:47 +08:00
self . assertEqual ( t . render ( c ) , ' simple_one_default - Expected result: 37, hello ' )
2011-09-27 20:15:15 +08:00
t = template . Template ( ' { % lo ad custom % } { % s imple_one_default one=99 two= " hello " % } ' )
2012-06-08 00:08:47 +08:00
self . assertEqual ( t . render ( c ) , ' simple_one_default - Expected result: 99, hello ' )
2011-09-27 20:15:15 +08:00
2012-09-08 01:17:09 +08:00
six . assertRaisesRegex ( self , template . TemplateSyntaxError ,
2011-09-27 20:15:15 +08:00
" ' simple_one_default ' received unexpected keyword argument ' three ' " ,
template . Template , ' { % lo ad custom % } { % s imple_one_default 99 two= " hello " three= " foo " % } ' )
t = template . Template ( ' { % lo ad custom % } { % s imple_one_default 37 42 % } ' )
2012-06-08 00:08:47 +08:00
self . assertEqual ( t . render ( c ) , ' simple_one_default - Expected result: 37, 42 ' )
2011-09-27 20:15:15 +08:00
t = template . Template ( ' { % lo ad custom % } { % s imple_unlimited_args 37 % } ' )
2012-06-08 00:08:47 +08:00
self . assertEqual ( t . render ( c ) , ' simple_unlimited_args - Expected result: 37, hi ' )
2011-09-27 20:15:15 +08:00
t = template . Template ( ' { % lo ad custom % } { % s imple_unlimited_args 37 42 56 89 % } ' )
2012-06-08 00:08:47 +08:00
self . assertEqual ( t . render ( c ) , ' simple_unlimited_args - Expected result: 37, 42, 56, 89 ' )
2011-09-27 20:15:15 +08:00
t = template . Template ( ' { % lo ad custom % } { % s imple_only_unlimited_args % } ' )
2012-06-08 00:08:47 +08:00
self . assertEqual ( t . render ( c ) , ' simple_only_unlimited_args - Expected result: ' )
2011-09-27 20:15:15 +08:00
t = template . Template ( ' { % lo ad custom % } { % s imple_only_unlimited_args 37 42 56 89 % } ' )
2012-06-08 00:08:47 +08:00
self . assertEqual ( t . render ( c ) , ' simple_only_unlimited_args - Expected result: 37, 42, 56, 89 ' )
2011-09-27 20:15:15 +08:00
2012-09-08 01:17:09 +08:00
six . assertRaisesRegex ( self , template . TemplateSyntaxError ,
2011-09-27 20:15:15 +08:00
" ' simple_two_params ' received too many positional arguments " ,
template . Template , ' { % lo ad custom % } { % s imple_two_params 37 42 56 % } ' )
2012-09-08 01:17:09 +08:00
six . assertRaisesRegex ( self , template . TemplateSyntaxError ,
2011-09-27 20:15:15 +08:00
" ' simple_one_default ' received too many positional arguments " ,
template . Template , ' { % lo ad custom % } { % s imple_one_default 37 42 56 % } ' )
t = template . Template ( ' { % lo ad custom % } { % s imple_unlimited_args_kwargs 37 40|add:2 56 eggs= " scrambled " four=1|add:3 % } ' )
2012-06-08 00:08:47 +08:00
self . assertEqual ( t . render ( c ) , ' simple_unlimited_args_kwargs - Expected result: 37, 42, 56 / eggs=scrambled, four=4 ' )
2011-09-27 20:15:15 +08:00
2012-09-08 01:17:09 +08:00
six . assertRaisesRegex ( self , template . TemplateSyntaxError ,
2011-09-27 20:15:15 +08:00
" ' simple_unlimited_args_kwargs ' received some positional argument \ (s \ ) after some keyword argument \ (s \ ) " ,
template . Template , ' { % lo ad custom % } { % s imple_unlimited_args_kwargs 37 40|add:2 eggs= " scrambled " 56 four=1|add:3 % } ' )
2012-09-08 01:17:09 +08:00
six . assertRaisesRegex ( self , template . TemplateSyntaxError ,
2011-09-27 20:15:15 +08:00
" ' simple_unlimited_args_kwargs ' received multiple values for keyword argument ' eggs ' " ,
template . Template , ' { % lo ad custom % } { % s imple_unlimited_args_kwargs 37 eggs= " scrambled " eggs= " scrambled " % } ' )
2010-12-19 23:00:50 +08:00
def test_simple_tag_registration ( self ) :
# Test that the decorators preserve the decorated function's docstring, name and attributes.
self . verify_tag ( custom . no_params , ' no_params ' )
self . verify_tag ( custom . one_param , ' one_param ' )
self . verify_tag ( custom . explicit_no_context , ' explicit_no_context ' )
self . verify_tag ( custom . no_params_with_context , ' no_params_with_context ' )
self . verify_tag ( custom . params_and_context , ' params_and_context ' )
2011-09-27 20:15:15 +08:00
self . verify_tag ( custom . simple_unlimited_args_kwargs , ' simple_unlimited_args_kwargs ' )
self . verify_tag ( custom . simple_tag_without_context_parameter , ' simple_tag_without_context_parameter ' )
2010-12-19 23:00:50 +08:00
def test_simple_tag_missing_context ( self ) :
2011-09-27 20:15:15 +08:00
# The 'context' parameter must be present when takes_context is True
2012-09-08 01:17:09 +08:00
six . assertRaisesRegex ( self , template . TemplateSyntaxError ,
2011-09-27 20:15:15 +08:00
" ' simple_tag_without_context_parameter ' is decorated with takes_context=True so it must have a first argument of ' context ' " ,
template . Template , ' { % lo ad custom % } { % s imple_tag_without_context_parameter 123 % } ' )
2011-03-15 16:20:14 +08:00
def test_inclusion_tags ( self ) :
c = template . Context ( { ' value ' : 42 } )
t = template . Template ( ' { % lo ad custom % } { % i nclusion_no_params % } ' )
2012-06-08 00:08:47 +08:00
self . assertEqual ( t . render ( c ) , ' inclusion_no_params - Expected result \n ' )
2011-03-15 16:20:14 +08:00
t = template . Template ( ' { % lo ad custom % } { % i nclusion_one_param 37 % } ' )
2012-06-08 00:08:47 +08:00
self . assertEqual ( t . render ( c ) , ' inclusion_one_param - Expected result: 37 \n ' )
2011-03-15 16:20:14 +08:00
t = template . Template ( ' { % lo ad custom % } { % i nclusion_explicit_no_context 37 % } ' )
2012-06-08 00:08:47 +08:00
self . assertEqual ( t . render ( c ) , ' inclusion_explicit_no_context - Expected result: 37 \n ' )
2011-03-15 16:20:14 +08:00
t = template . Template ( ' { % lo ad custom % } { % i nclusion_no_params_with_context % } ' )
2012-06-08 00:08:47 +08:00
self . assertEqual ( t . render ( c ) , ' inclusion_no_params_with_context - Expected result (context value: 42) \n ' )
2011-03-15 16:20:14 +08:00
t = template . Template ( ' { % lo ad custom % } { % i nclusion_params_and_context 37 % } ' )
2012-06-08 00:08:47 +08:00
self . assertEqual ( t . render ( c ) , ' inclusion_params_and_context - Expected result (context value: 42): 37 \n ' )
2011-03-15 16:20:14 +08:00
2011-09-27 20:15:15 +08:00
t = template . Template ( ' { % lo ad custom % } { % i nclusion_two_params 37 42 % } ' )
2012-06-08 00:08:47 +08:00
self . assertEqual ( t . render ( c ) , ' inclusion_two_params - Expected result: 37, 42 \n ' )
2011-09-27 20:15:15 +08:00
t = template . Template ( ' { % lo ad custom % } { % i nclusion_one_default 37 % } ' )
2012-06-08 00:08:47 +08:00
self . assertEqual ( t . render ( c ) , ' inclusion_one_default - Expected result: 37, hi \n ' )
2011-09-27 20:15:15 +08:00
t = template . Template ( ' { % lo ad custom % } { % i nclusion_one_default 37 two= " hello " % } ' )
2012-06-08 00:08:47 +08:00
self . assertEqual ( t . render ( c ) , ' inclusion_one_default - Expected result: 37, hello \n ' )
2011-09-27 20:15:15 +08:00
t = template . Template ( ' { % lo ad custom % } { % i nclusion_one_default one=99 two= " hello " % } ' )
2012-06-08 00:08:47 +08:00
self . assertEqual ( t . render ( c ) , ' inclusion_one_default - Expected result: 99, hello \n ' )
2011-09-27 20:15:15 +08:00
2012-09-08 01:17:09 +08:00
six . assertRaisesRegex ( self , template . TemplateSyntaxError ,
2011-09-27 20:15:15 +08:00
" ' inclusion_one_default ' received unexpected keyword argument ' three ' " ,
template . Template , ' { % lo ad custom % } { % i nclusion_one_default 99 two= " hello " three= " foo " % } ' )
t = template . Template ( ' { % lo ad custom % } { % i nclusion_one_default 37 42 % } ' )
2012-06-08 00:08:47 +08:00
self . assertEqual ( t . render ( c ) , ' inclusion_one_default - Expected result: 37, 42 \n ' )
2011-09-27 20:15:15 +08:00
t = template . Template ( ' { % lo ad custom % } { % i nclusion_unlimited_args 37 % } ' )
2012-06-08 00:08:47 +08:00
self . assertEqual ( t . render ( c ) , ' inclusion_unlimited_args - Expected result: 37, hi \n ' )
2011-09-27 20:15:15 +08:00
t = template . Template ( ' { % lo ad custom % } { % i nclusion_unlimited_args 37 42 56 89 % } ' )
2012-06-08 00:08:47 +08:00
self . assertEqual ( t . render ( c ) , ' inclusion_unlimited_args - Expected result: 37, 42, 56, 89 \n ' )
2011-09-27 20:15:15 +08:00
t = template . Template ( ' { % lo ad custom % } { % i nclusion_only_unlimited_args % } ' )
2012-06-08 00:08:47 +08:00
self . assertEqual ( t . render ( c ) , ' inclusion_only_unlimited_args - Expected result: \n ' )
2011-09-27 20:15:15 +08:00
t = template . Template ( ' { % lo ad custom % } { % i nclusion_only_unlimited_args 37 42 56 89 % } ' )
2012-06-08 00:08:47 +08:00
self . assertEqual ( t . render ( c ) , ' inclusion_only_unlimited_args - Expected result: 37, 42, 56, 89 \n ' )
2011-09-27 20:15:15 +08:00
2012-09-08 01:17:09 +08:00
six . assertRaisesRegex ( self , template . TemplateSyntaxError ,
2011-09-27 20:15:15 +08:00
" ' inclusion_two_params ' received too many positional arguments " ,
template . Template , ' { % lo ad custom % } { % i nclusion_two_params 37 42 56 % } ' )
2012-09-08 01:17:09 +08:00
six . assertRaisesRegex ( self , template . TemplateSyntaxError ,
2011-09-27 20:15:15 +08:00
" ' inclusion_one_default ' received too many positional arguments " ,
template . Template , ' { % lo ad custom % } { % i nclusion_one_default 37 42 56 % } ' )
2012-09-08 01:17:09 +08:00
six . assertRaisesRegex ( self , template . TemplateSyntaxError ,
2011-09-27 20:15:15 +08:00
" ' inclusion_one_default ' did not receive value \ (s \ ) for the argument \ (s \ ): ' one ' " ,
template . Template , ' { % lo ad custom % } { % i nclusion_one_default % } ' )
2012-09-08 01:17:09 +08:00
six . assertRaisesRegex ( self , template . TemplateSyntaxError ,
2011-09-27 20:15:15 +08:00
" ' inclusion_unlimited_args ' did not receive value \ (s \ ) for the argument \ (s \ ): ' one ' " ,
template . Template , ' { % lo ad custom % } { % i nclusion_unlimited_args % } ' )
t = template . Template ( ' { % lo ad custom % } { % i nclusion_unlimited_args_kwargs 37 40|add:2 56 eggs= " scrambled " four=1|add:3 % } ' )
2012-06-08 00:08:47 +08:00
self . assertEqual ( t . render ( c ) , ' inclusion_unlimited_args_kwargs - Expected result: 37, 42, 56 / eggs=scrambled, four=4 \n ' )
2011-09-27 20:15:15 +08:00
2012-09-08 01:17:09 +08:00
six . assertRaisesRegex ( self , template . TemplateSyntaxError ,
2011-09-27 20:15:15 +08:00
" ' inclusion_unlimited_args_kwargs ' received some positional argument \ (s \ ) after some keyword argument \ (s \ ) " ,
template . Template , ' { % lo ad custom % } { % i nclusion_unlimited_args_kwargs 37 40|add:2 eggs= " scrambled " 56 four=1|add:3 % } ' )
2012-09-08 01:17:09 +08:00
six . assertRaisesRegex ( self , template . TemplateSyntaxError ,
2011-09-27 20:15:15 +08:00
" ' inclusion_unlimited_args_kwargs ' received multiple values for keyword argument ' eggs ' " ,
template . Template , ' { % lo ad custom % } { % i nclusion_unlimited_args_kwargs 37 eggs= " scrambled " eggs= " scrambled " % } ' )
def test_include_tag_missing_context ( self ) :
# The 'context' parameter must be present when takes_context is True
2012-09-08 01:17:09 +08:00
six . assertRaisesRegex ( self , template . TemplateSyntaxError ,
2011-09-27 20:15:15 +08:00
" ' inclusion_tag_without_context_parameter ' is decorated with takes_context=True so it must have a first argument of ' context ' " ,
template . Template , ' { % lo ad custom % } { % i nclusion_tag_without_context_parameter 123 % } ' )
2011-06-12 00:22:45 +08:00
def test_inclusion_tags_from_template ( self ) :
c = template . Context ( { ' value ' : 42 } )
t = template . Template ( ' { % lo ad custom % } { % i nclusion_no_params_from_template % } ' )
2012-06-08 00:08:47 +08:00
self . assertEqual ( t . render ( c ) , ' inclusion_no_params_from_template - Expected result \n ' )
2011-06-12 00:22:45 +08:00
t = template . Template ( ' { % lo ad custom % } { % i nclusion_one_param_from_template 37 % } ' )
2012-06-08 00:08:47 +08:00
self . assertEqual ( t . render ( c ) , ' inclusion_one_param_from_template - Expected result: 37 \n ' )
2011-06-12 00:22:45 +08:00
t = template . Template ( ' { % lo ad custom % } { % i nclusion_explicit_no_context_from_template 37 % } ' )
2012-06-08 00:08:47 +08:00
self . assertEqual ( t . render ( c ) , ' inclusion_explicit_no_context_from_template - Expected result: 37 \n ' )
2011-06-12 00:22:45 +08:00
t = template . Template ( ' { % lo ad custom % } { % i nclusion_no_params_with_context_from_template % } ' )
2012-06-08 00:08:47 +08:00
self . assertEqual ( t . render ( c ) , ' inclusion_no_params_with_context_from_template - Expected result (context value: 42) \n ' )
2011-06-12 00:22:45 +08:00
t = template . Template ( ' { % lo ad custom % } { % i nclusion_params_and_context_from_template 37 % } ' )
2012-06-08 00:08:47 +08:00
self . assertEqual ( t . render ( c ) , ' inclusion_params_and_context_from_template - Expected result (context value: 42): 37 \n ' )
2011-06-12 00:22:45 +08:00
2011-09-27 20:15:15 +08:00
t = template . Template ( ' { % lo ad custom % } { % i nclusion_two_params_from_template 37 42 % } ' )
2012-06-08 00:08:47 +08:00
self . assertEqual ( t . render ( c ) , ' inclusion_two_params_from_template - Expected result: 37, 42 \n ' )
2011-09-27 20:15:15 +08:00
t = template . Template ( ' { % lo ad custom % } { % i nclusion_one_default_from_template 37 % } ' )
2012-06-08 00:08:47 +08:00
self . assertEqual ( t . render ( c ) , ' inclusion_one_default_from_template - Expected result: 37, hi \n ' )
2011-09-27 20:15:15 +08:00
t = template . Template ( ' { % lo ad custom % } { % i nclusion_one_default_from_template 37 42 % } ' )
2012-06-08 00:08:47 +08:00
self . assertEqual ( t . render ( c ) , ' inclusion_one_default_from_template - Expected result: 37, 42 \n ' )
2011-09-27 20:15:15 +08:00
t = template . Template ( ' { % lo ad custom % } { % i nclusion_unlimited_args_from_template 37 % } ' )
2012-06-08 00:08:47 +08:00
self . assertEqual ( t . render ( c ) , ' inclusion_unlimited_args_from_template - Expected result: 37, hi \n ' )
2011-09-27 20:15:15 +08:00
t = template . Template ( ' { % lo ad custom % } { % i nclusion_unlimited_args_from_template 37 42 56 89 % } ' )
2012-06-08 00:08:47 +08:00
self . assertEqual ( t . render ( c ) , ' inclusion_unlimited_args_from_template - Expected result: 37, 42, 56, 89 \n ' )
2011-09-27 20:15:15 +08:00
t = template . Template ( ' { % lo ad custom % } { % i nclusion_only_unlimited_args_from_template % } ' )
2012-06-08 00:08:47 +08:00
self . assertEqual ( t . render ( c ) , ' inclusion_only_unlimited_args_from_template - Expected result: \n ' )
2011-09-27 20:15:15 +08:00
t = template . Template ( ' { % lo ad custom % } { % i nclusion_only_unlimited_args_from_template 37 42 56 89 % } ' )
2012-06-08 00:08:47 +08:00
self . assertEqual ( t . render ( c ) , ' inclusion_only_unlimited_args_from_template - Expected result: 37, 42, 56, 89 \n ' )
2011-09-27 20:15:15 +08:00
2011-03-15 16:20:14 +08:00
def test_inclusion_tag_registration ( self ) :
# Test that the decorators preserve the decorated function's docstring, name and attributes.
self . verify_tag ( custom . inclusion_no_params , ' inclusion_no_params ' )
self . verify_tag ( custom . inclusion_one_param , ' inclusion_one_param ' )
self . verify_tag ( custom . inclusion_explicit_no_context , ' inclusion_explicit_no_context ' )
self . verify_tag ( custom . inclusion_no_params_with_context , ' inclusion_no_params_with_context ' )
self . verify_tag ( custom . inclusion_params_and_context , ' inclusion_params_and_context ' )
2011-09-27 20:15:15 +08:00
self . verify_tag ( custom . inclusion_two_params , ' inclusion_two_params ' )
self . verify_tag ( custom . inclusion_one_default , ' inclusion_one_default ' )
self . verify_tag ( custom . inclusion_unlimited_args , ' inclusion_unlimited_args ' )
self . verify_tag ( custom . inclusion_only_unlimited_args , ' inclusion_only_unlimited_args ' )
self . verify_tag ( custom . inclusion_tag_without_context_parameter , ' inclusion_tag_without_context_parameter ' )
self . verify_tag ( custom . inclusion_tag_use_l10n , ' inclusion_tag_use_l10n ' )
self . verify_tag ( custom . inclusion_tag_current_app , ' inclusion_tag_current_app ' )
self . verify_tag ( custom . inclusion_unlimited_args_kwargs , ' inclusion_unlimited_args_kwargs ' )
2011-04-28 21:41:28 +08:00
def test_15070_current_app ( self ) :
"""
Test that inclusion tag passes down ` current_app ` of context to the
Context of the included / rendered template as well .
"""
c = template . Context ( { } )
t = template . Template ( ' { % lo ad custom % } { % i nclusion_tag_current_app % } ' )
2012-06-08 00:08:47 +08:00
self . assertEqual ( t . render ( c ) . strip ( ) , ' None ' )
2011-04-28 21:41:28 +08:00
c . current_app = ' advanced '
2012-06-08 00:08:47 +08:00
self . assertEqual ( t . render ( c ) . strip ( ) , ' advanced ' )
2011-04-28 21:41:28 +08:00
def test_15070_use_l10n ( self ) :
"""
Test that inclusion tag passes down ` use_l10n ` of context to the
Context of the included / rendered template as well .
"""
c = template . Context ( { } )
t = template . Template ( ' { % lo ad custom % } { % i nclusion_tag_use_l10n % } ' )
2012-06-08 00:08:47 +08:00
self . assertEqual ( t . render ( c ) . strip ( ) , ' None ' )
2011-04-28 21:41:28 +08:00
c . use_l10n = True
2012-06-08 00:08:47 +08:00
self . assertEqual ( t . render ( c ) . strip ( ) , ' True ' )
2011-05-03 19:52:42 +08:00
def test_assignment_tags ( self ) :
c = template . Context ( { ' value ' : 42 } )
t = template . Template ( ' { % lo ad custom % } { % a ssignment_no_params as var % }The result is: {{ var }} ' )
2012-06-08 00:08:47 +08:00
self . assertEqual ( t . render ( c ) , ' The result is: assignment_no_params - Expected result ' )
2011-05-03 19:52:42 +08:00
t = template . Template ( ' { % lo ad custom % } { % a ssignment_one_param 37 as var % }The result is: {{ var }} ' )
2012-06-08 00:08:47 +08:00
self . assertEqual ( t . render ( c ) , ' The result is: assignment_one_param - Expected result: 37 ' )
2011-05-03 19:52:42 +08:00
t = template . Template ( ' { % lo ad custom % } { % a ssignment_explicit_no_context 37 as var % }The result is: {{ var }} ' )
2012-06-08 00:08:47 +08:00
self . assertEqual ( t . render ( c ) , ' The result is: assignment_explicit_no_context - Expected result: 37 ' )
2011-05-03 19:52:42 +08:00
t = template . Template ( ' { % lo ad custom % } { % a ssignment_no_params_with_context as var % }The result is: {{ var }} ' )
2012-06-08 00:08:47 +08:00
self . assertEqual ( t . render ( c ) , ' The result is: assignment_no_params_with_context - Expected result (context value: 42) ' )
2011-05-03 19:52:42 +08:00
t = template . Template ( ' { % lo ad custom % } { % a ssignment_params_and_context 37 as var % }The result is: {{ var }} ' )
2012-06-08 00:08:47 +08:00
self . assertEqual ( t . render ( c ) , ' The result is: assignment_params_and_context - Expected result (context value: 42): 37 ' )
2011-05-03 19:52:42 +08:00
2011-09-27 20:15:15 +08:00
t = template . Template ( ' { % lo ad custom % } { % a ssignment_two_params 37 42 as var % }The result is: {{ var }} ' )
2012-06-08 00:08:47 +08:00
self . assertEqual ( t . render ( c ) , ' The result is: assignment_two_params - Expected result: 37, 42 ' )
2011-09-27 20:15:15 +08:00
t = template . Template ( ' { % lo ad custom % } { % a ssignment_one_default 37 as var % }The result is: {{ var }} ' )
2012-06-08 00:08:47 +08:00
self . assertEqual ( t . render ( c ) , ' The result is: assignment_one_default - Expected result: 37, hi ' )
2011-09-27 20:15:15 +08:00
t = template . Template ( ' { % lo ad custom % } { % a ssignment_one_default 37 two= " hello " as var % }The result is: {{ var }} ' )
2012-06-08 00:08:47 +08:00
self . assertEqual ( t . render ( c ) , ' The result is: assignment_one_default - Expected result: 37, hello ' )
2011-09-27 20:15:15 +08:00
t = template . Template ( ' { % lo ad custom % } { % a ssignment_one_default one=99 two= " hello " as var % }The result is: {{ var }} ' )
2012-06-08 00:08:47 +08:00
self . assertEqual ( t . render ( c ) , ' The result is: assignment_one_default - Expected result: 99, hello ' )
2011-09-27 20:15:15 +08:00
2012-09-08 01:17:09 +08:00
six . assertRaisesRegex ( self , template . TemplateSyntaxError ,
2011-09-27 20:15:15 +08:00
" ' assignment_one_default ' received unexpected keyword argument ' three ' " ,
template . Template , ' { % lo ad custom % } { % a ssignment_one_default 99 two= " hello " three= " foo " as var % } ' )
t = template . Template ( ' { % lo ad custom % } { % a ssignment_one_default 37 42 as var % }The result is: {{ var }} ' )
2012-06-08 00:08:47 +08:00
self . assertEqual ( t . render ( c ) , ' The result is: assignment_one_default - Expected result: 37, 42 ' )
2011-09-27 20:15:15 +08:00
t = template . Template ( ' { % lo ad custom % } { % a ssignment_unlimited_args 37 as var % }The result is: {{ var }} ' )
2012-06-08 00:08:47 +08:00
self . assertEqual ( t . render ( c ) , ' The result is: assignment_unlimited_args - Expected result: 37, hi ' )
2011-09-27 20:15:15 +08:00
t = template . Template ( ' { % lo ad custom % } { % a ssignment_unlimited_args 37 42 56 89 as var % }The result is: {{ var }} ' )
2012-06-08 00:08:47 +08:00
self . assertEqual ( t . render ( c ) , ' The result is: assignment_unlimited_args - Expected result: 37, 42, 56, 89 ' )
2011-09-27 20:15:15 +08:00
t = template . Template ( ' { % lo ad custom % } { % a ssignment_only_unlimited_args as var % }The result is: {{ var }} ' )
2012-06-08 00:08:47 +08:00
self . assertEqual ( t . render ( c ) , ' The result is: assignment_only_unlimited_args - Expected result: ' )
2011-09-27 20:15:15 +08:00
t = template . Template ( ' { % lo ad custom % } { % a ssignment_only_unlimited_args 37 42 56 89 as var % }The result is: {{ var }} ' )
2012-06-08 00:08:47 +08:00
self . assertEqual ( t . render ( c ) , ' The result is: assignment_only_unlimited_args - Expected result: 37, 42, 56, 89 ' )
2011-09-27 20:15:15 +08:00
2012-09-08 01:17:09 +08:00
six . assertRaisesRegex ( self , template . TemplateSyntaxError ,
2011-05-03 19:52:42 +08:00
" ' assignment_one_param ' tag takes at least 2 arguments and the second last argument must be ' as ' " ,
template . Template , ' { % lo ad custom % } { % a ssignment_one_param 37 % }The result is: {{ var }} ' )
2012-09-08 01:17:09 +08:00
six . assertRaisesRegex ( self , template . TemplateSyntaxError ,
2011-05-03 19:52:42 +08:00
" ' assignment_one_param ' tag takes at least 2 arguments and the second last argument must be ' as ' " ,
template . Template , ' { % lo ad custom % } { % a ssignment_one_param 37 as % }The result is: {{ var }} ' )
2012-09-08 01:17:09 +08:00
six . assertRaisesRegex ( self , template . TemplateSyntaxError ,
2011-05-03 19:52:42 +08:00
" ' assignment_one_param ' tag takes at least 2 arguments and the second last argument must be ' as ' " ,
template . Template , ' { % lo ad custom % } { % a ssignment_one_param 37 ass var % }The result is: {{ var }} ' )
2012-09-08 01:17:09 +08:00
six . assertRaisesRegex ( self , template . TemplateSyntaxError ,
2011-09-27 20:15:15 +08:00
" ' assignment_two_params ' received too many positional arguments " ,
template . Template , ' { % lo ad custom % } { % a ssignment_two_params 37 42 56 as var % }The result is: {{ var }} ' )
2012-09-08 01:17:09 +08:00
six . assertRaisesRegex ( self , template . TemplateSyntaxError ,
2011-09-27 20:15:15 +08:00
" ' assignment_one_default ' received too many positional arguments " ,
template . Template , ' { % lo ad custom % } { % a ssignment_one_default 37 42 56 as var % }The result is: {{ var }} ' )
2012-09-08 01:17:09 +08:00
six . assertRaisesRegex ( self , template . TemplateSyntaxError ,
2011-09-27 20:15:15 +08:00
" ' assignment_one_default ' did not receive value \ (s \ ) for the argument \ (s \ ): ' one ' " ,
template . Template , ' { % lo ad custom % } { % a ssignment_one_default as var % }The result is: {{ var }} ' )
2012-09-08 01:17:09 +08:00
six . assertRaisesRegex ( self , template . TemplateSyntaxError ,
2011-09-27 20:15:15 +08:00
" ' assignment_unlimited_args ' did not receive value \ (s \ ) for the argument \ (s \ ): ' one ' " ,
template . Template , ' { % lo ad custom % } { % a ssignment_unlimited_args as var % }The result is: {{ var }} ' )
t = template . Template ( ' { % lo ad custom % } { % a ssignment_unlimited_args_kwargs 37 40|add:2 56 eggs= " scrambled " four=1|add:3 as var % }The result is: {{ var }} ' )
2012-06-08 00:08:47 +08:00
self . assertEqual ( t . render ( c ) , ' The result is: assignment_unlimited_args_kwargs - Expected result: 37, 42, 56 / eggs=scrambled, four=4 ' )
2011-09-27 20:15:15 +08:00
2012-09-08 01:17:09 +08:00
six . assertRaisesRegex ( self , template . TemplateSyntaxError ,
2011-09-27 20:15:15 +08:00
" ' assignment_unlimited_args_kwargs ' received some positional argument \ (s \ ) after some keyword argument \ (s \ ) " ,
template . Template , ' { % lo ad custom % } { % a ssignment_unlimited_args_kwargs 37 40|add:2 eggs= " scrambled " 56 four=1|add:3 as var % }The result is: {{ var }} ' )
2012-09-08 01:17:09 +08:00
six . assertRaisesRegex ( self , template . TemplateSyntaxError ,
2011-09-27 20:15:15 +08:00
" ' assignment_unlimited_args_kwargs ' received multiple values for keyword argument ' eggs ' " ,
template . Template , ' { % lo ad custom % } { % a ssignment_unlimited_args_kwargs 37 eggs= " scrambled " eggs= " scrambled " as var % }The result is: {{ var }} ' )
2011-05-03 19:52:42 +08:00
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 ' )
2011-09-27 20:15:15 +08:00
self . verify_tag ( custom . assignment_one_default , ' assignment_one_default ' )
self . verify_tag ( custom . assignment_two_params , ' assignment_two_params ' )
self . verify_tag ( custom . assignment_unlimited_args , ' assignment_unlimited_args ' )
self . verify_tag ( custom . assignment_only_unlimited_args , ' assignment_only_unlimited_args ' )
self . verify_tag ( custom . assignment_unlimited_args , ' assignment_unlimited_args ' )
self . verify_tag ( custom . assignment_unlimited_args_kwargs , ' assignment_unlimited_args_kwargs ' )
self . verify_tag ( custom . assignment_tag_without_context_parameter , ' assignment_tag_without_context_parameter ' )
2011-05-03 19:52:42 +08:00
def test_assignment_tag_missing_context ( self ) :
2011-09-27 20:15:15 +08:00
# The 'context' parameter must be present when takes_context is True
2012-09-08 01:17:09 +08:00
six . assertRaisesRegex ( self , template . TemplateSyntaxError ,
2011-09-27 20:15:15 +08:00
" ' assignment_tag_without_context_parameter ' is decorated with takes_context=True so it must have a first argument of ' context ' " ,
template . Template , ' { % lo ad custom % } { % a ssignment_tag_without_context_parameter 123 as var % } ' )