<s> from mako . template import Template <EOL> from mako import lookup <EOL> from test import TemplateTest <EOL> from test . util import flatten_result , result_lines <EOL> from test import eq_ , assert_raises <EOL> from mako import compat <EOL> class DefTest ( TemplateTest ) : <EOL> def test_def_noargs ( self ) : <EOL> template = Template ( """<STR_LIT>""" ) <EOL> eq_ ( <EOL> template . render ( variable = '<STR_LIT>' ) . strip ( ) , <EOL> """<STR_LIT>""" <EOL> ) <EOL> def test_def_blankargs ( self ) : <EOL> template = Template ( """<STR_LIT>""" ) <EOL> eq_ ( <EOL> template . render ( variable = '<STR_LIT>' ) . strip ( ) , <EOL> "<STR_LIT>" <EOL> ) <EOL> def test_def_args ( self ) : <EOL> template = Template ( """<STR_LIT>""" ) <EOL> eq_ ( <EOL> template . render ( variable = '<STR_LIT>' , a = <NUM_LIT:5> , b = <NUM_LIT:6> ) . strip ( ) , <EOL> """<STR_LIT>""" <EOL> ) <EOL> def test_inter_def ( self ) : <EOL> """<STR_LIT>""" <EOL> template = Template ( """<STR_LIT>""" ) <EOL> if compat . py3k : <EOL> assert "<STR_LIT:a>" not in template . module . render_c . __code__ . co_varnames <EOL> assert "<STR_LIT:a>" in template . module . render_b . __code__ . co_varnames <EOL> else : <EOL> assert "<STR_LIT:a>" not in template . module . render_c . func_code . co_varnames <EOL> assert "<STR_LIT:a>" in template . module . render_b . func_code . co_varnames <EOL> eq_ ( <EOL> flatten_result ( template . render ( ) ) , <EOL> "<STR_LIT>" <EOL> ) <EOL> def test_toplevel ( self ) : <EOL> """<STR_LIT>""" <EOL> template = Template ( """<STR_LIT>""" ) <EOL> self . _do_test ( template . get_def ( "<STR_LIT:a>" ) , <EOL> "<STR_LIT>" , <EOL> filters = flatten_result ) <EOL> self . _do_test ( template . get_def ( "<STR_LIT:b>" ) , <EOL> "<STR_LIT>" , <EOL> template_args = { '<STR_LIT:x>' : <NUM_LIT:10> , '<STR_LIT:y>' : <NUM_LIT:15> } , <EOL> filters = flatten_result ) <EOL> self . _do_test ( template . get_def ( "<STR_LIT:body>" ) , <EOL> "<STR_LIT>" , <EOL> filters = flatten_result ) <EOL> self . _do_test ( template . get_def ( "<STR_LIT:a>" ) , "<STR_LIT>" , <EOL> filters = flatten_result , <EOL> template_args = { '<STR_LIT:q>' : <NUM_LIT:5> , '<STR_LIT>' : '<STR_LIT:test>' } ) <EOL> class ScopeTest ( TemplateTest ) : <EOL> """<STR_LIT>""" <EOL> def test_scope_one ( self ) : <EOL> self . _do_memory_test ( """<STR_LIT>""" , <EOL> "<STR_LIT>" , <EOL> filters = flatten_result , <EOL> template_args = { '<STR_LIT:y>' : None } <EOL> ) <EOL> def test_scope_two ( self ) : <EOL> t = Template ( """<STR_LIT>""" ) <EOL> try : <EOL> t . render ( y = None ) <EOL> assert False <EOL> except UnboundLocalError : <EOL> assert True <EOL> def test_scope_four ( self ) : <EOL> """<STR_LIT>""" <EOL> t = Template ( """<STR_LIT>""" ) <EOL> eq_ ( <EOL> flatten_result ( t . render ( ) ) , <EOL> "<STR_LIT>" <EOL> ) <EOL> def test_scope_five ( self ) : <EOL> """<STR_LIT>""" <EOL> t = Template ( """<STR_LIT>""" ) <EOL> eq_ ( <EOL> flatten_result ( t . render ( ) ) , <EOL> "<STR_LIT>" <EOL> ) <EOL> def test_scope_six ( self ) : <EOL> """<STR_LIT>""" <EOL> t = Template ( """<STR_LIT>""" ) <EOL> eq_ ( <EOL> flatten_result ( t . render ( x = <NUM_LIT:5> ) ) , <EOL> "<STR_LIT>" <EOL> ) <EOL> def test_scope_seven ( self ) : <EOL> """<STR_LIT>""" <EOL> t = Template ( """<STR_LIT>""" ) <EOL> eq_ ( <EOL> flatten_result ( t . render ( x = <NUM_LIT:5> ) ) , <EOL> "<STR_LIT>" <EOL> ) <EOL> def test_scope_eight ( self ) : <EOL> """<STR_LIT>""" <EOL> t = Template ( """<STR_LIT>""" ) <EOL> eq_ ( <EOL> flatten_result ( t . render ( x = <NUM_LIT:5> ) ) , <EOL> "<STR_LIT>" <EOL> ) <EOL> def test_scope_nine ( self ) : <EOL> """<STR_LIT>""" <EOL> l = lookup . TemplateLookup ( ) <EOL> l . put_string ( '<STR_LIT>' , """<STR_LIT>""" ) <EOL> l . put_string ( '<STR_LIT>' , """<STR_LIT>""" ) <EOL> eq_ ( <EOL> flatten_result ( l . get_template ( '<STR_LIT>' ) . render ( x = <NUM_LIT:2> ) ) , <EOL> "<STR_LIT>" <EOL> ) <EOL> def test_scope_ten ( self ) : <EOL> t = Template ( """<STR_LIT>""" ) <EOL> eq_ ( <EOL> flatten_result ( t . render ( ) ) , <EOL> "<STR_LIT>" <EOL> ) <EOL> def test_scope_eleven ( self ) : <EOL> t = Template ( """<STR_LIT>""" ) <EOL> eq_ ( <EOL> result_lines ( t . render ( x = <NUM_LIT:10> ) ) , <EOL> [ <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" <EOL> ] ) <EOL> def test_unbound_scope ( self ) : <EOL> t = Template ( """<STR_LIT>""" ) <EOL> assert_raises ( <EOL> UnboundLocalError , <EOL> t . render <EOL> ) <EOL> def test_unbound_scope_two ( self ) : <EOL> t = Template ( """<STR_LIT>""" ) <EOL> try : <EOL> print ( t . render ( ) ) <EOL> assert False <EOL> except UnboundLocalError : <EOL> assert True <EOL> def test_canget_kwargs ( self ) : <EOL> """<STR_LIT>""" <EOL> l = lookup . TemplateLookup ( ) <EOL> l . put_string ( "<STR_LIT>" , """<STR_LIT>""" ) <EOL> l . put_string ( "<STR_LIT>" , """<STR_LIT>""" ) <EOL> eq_ ( <EOL> result_lines ( l . get_template ( "<STR_LIT>" ) . render ( ) ) , <EOL> [ <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" <EOL> ] ) <EOL> l . put_string ( "<STR_LIT>" , """<STR_LIT>""" ) <EOL> eq_ ( <EOL> result_lines ( l . get_template ( "<STR_LIT>" ) . render ( ) ) , <EOL> [ <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" <EOL> ] ) <EOL> def test_inline_expression_from_arg_one ( self ) : <EOL> """<STR_LIT>""" <EOL> t = Template ( """<STR_LIT>""" , strict_undefined = True , <EOL> cache_impl = "<STR_LIT>" ) <EOL> eq_ ( <EOL> result_lines ( t . render ( ) ) , <EOL> [ "<STR_LIT>" ] <EOL> ) <EOL> def test_interpret_expression_from_arg_two ( self ) : <EOL> """<STR_LIT>""" <EOL> t = Template ( """<STR_LIT>""" , cache_impl = "<STR_LIT>" ) <EOL> eq_ ( <EOL> result_lines ( t . render ( foo = '<STR_LIT:foo>' , value = <NUM_LIT:1> ) ) , <EOL> [ "<STR_LIT>" ] <EOL> ) <EOL> eq_ ( <EOL> result_lines ( t . render ( foo = '<STR_LIT:bar>' , value = <NUM_LIT:2> ) ) , <EOL> [ "<STR_LIT>" ] <EOL> ) <EOL> class NestedDefTest ( TemplateTest ) : <EOL> def test_nested_def ( self ) : <EOL> t = Template ( """<STR_LIT>""" ) <EOL> eq_ ( <EOL> flatten_result ( t . render ( ) ) , <EOL> "<STR_LIT>" <EOL> ) <EOL> def test_nested_2 ( self ) : <EOL> t = Template ( """<STR_LIT>""" ) <EOL> eq_ ( <EOL> flatten_result ( t . render ( x = <NUM_LIT:10> ) ) , <EOL> "<STR_LIT>" <EOL> ) <EOL> def test_nested_with_args ( self ) : <EOL> t = Template ( """<STR_LIT>""" ) <EOL> eq_ ( <EOL> flatten_result ( t . render ( ) ) , <EOL> "<STR_LIT>" <EOL> ) <EOL> def test_nested_def_2 ( self ) : <EOL> template = Template ( """<STR_LIT>""" ) <EOL> eq_ ( <EOL> flatten_result ( template . render ( ) ) , <EOL> "<STR_LIT>" <EOL> ) <EOL> def test_nested_nested_def ( self ) : <EOL> t = Template ( """<STR_LIT>""" ) <EOL> eq_ ( <EOL> flatten_result ( t . render ( x = <NUM_LIT:5> , y = None ) ) , <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> ) <EOL> def test_nested_nested_def_2 ( self ) : <EOL> t = Template ( """<STR_LIT>""" ) <EOL> eq_ ( <EOL> flatten_result ( t . render ( ) ) , <EOL> "<STR_LIT>" <EOL> ) <EOL> def test_outer_scope ( self ) : <EOL> t = Template ( """<STR_LIT>""" ) <EOL> eq_ ( <EOL> flatten_result ( t . render ( x = <NUM_LIT:5> ) ) , <EOL> "<STR_LIT>" <EOL> ) <EOL> class ExceptionTest ( TemplateTest ) : <EOL> def test_raise ( self ) : <EOL> template = Template ( """<STR_LIT>""" , format_exceptions = False ) <EOL> assert_raises ( <EOL> Exception , <EOL> template . render <EOL> ) <EOL> def test_handler ( self ) : <EOL> def handle ( context , error ) : <EOL> context . write ( "<STR_LIT>" + str ( error ) ) <EOL> return True <EOL> template = Template ( """<STR_LIT>""" , error_handler = handle ) <EOL> eq_ ( <EOL> template . render ( ) . strip ( ) , <EOL> "<STR_LIT>" <EOL> ) </s>
