<s> import re <EOL> import unittest <EOL> from mako . template import Template <EOL> from mako . lookup import TemplateLookup <EOL> from mako . codegen import ( <EOL> _FOR_LOOP , mangle_mako_loop , LoopVariable <EOL> ) <EOL> from mako . runtime import LoopStack , LoopContext <EOL> from mako import exceptions <EOL> from test import assert_raises_message <EOL> from test import TemplateTest , eq_ <EOL> from test . util import flatten_result , result_lines <EOL> class TestLoop ( unittest . TestCase ) : <EOL> def test__FOR_LOOP ( self ) : <EOL> for statement , target_list , expression_list in ( <EOL> ( '<STR_LIT>' , '<STR_LIT:x>' , '<STR_LIT:y>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:z>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:z>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:a>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT:x>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT:x>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT:x>' , <EOL> '<STR_LIT>' ) <EOL> ) : <EOL> match = _FOR_LOOP . match ( statement ) <EOL> assert match and match . groups ( ) == ( target_list , expression_list ) <EOL> def test_no_loop ( self ) : <EOL> template = Template ( """<STR_LIT>""" ) <EOL> code = template . code <EOL> assert not re . match ( r"<STR_LIT>" , code ) , "<STR_LIT>" "<STR_LIT>" <EOL> print ( template . render ( ) ) <EOL> def test_loop_demo ( self ) : <EOL> template = Template ( """<STR_LIT>""" ) <EOL> expected = [ <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" <EOL> ] <EOL> code = template . code <EOL> assert "<STR_LIT>" in code , "<STR_LIT>" "<STR_LIT>" <EOL> rendered = template . render ( ) <EOL> print ( rendered ) <EOL> for line in expected : <EOL> assert line in rendered , "<STR_LIT>" "<STR_LIT>" <EOL> def test_nested_loops ( self ) : <EOL> template = Template ( """<STR_LIT>""" ) <EOL> code = template . code <EOL> rendered = template . render ( ) <EOL> expected = [ <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> ] <EOL> for line in expected : <EOL> assert line in rendered , "<STR_LIT>" "<STR_LIT>" <EOL> def test_parent_loops ( self ) : <EOL> template = Template ( """<STR_LIT>""" ) <EOL> code = template . code <EOL> rendered = template . render ( ) <EOL> expected = [ <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" <EOL> ] <EOL> for line in expected : <EOL> print ( code ) <EOL> assert line in rendered , "<STR_LIT>" "<STR_LIT>" <EOL> def test_out_of_context_access ( self ) : <EOL> template = Template ( """<STR_LIT>""" ) <EOL> assert_raises_message ( <EOL> exceptions . RuntimeException , <EOL> "<STR_LIT>" , <EOL> template . render <EOL> ) <EOL> class TestLoopStack ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> self . stack = LoopStack ( ) <EOL> self . bottom = '<STR_LIT>' <EOL> self . stack . stack = [ self . bottom ] <EOL> def test_enter ( self ) : <EOL> iterable = '<STR_LIT>' <EOL> s = self . stack . _enter ( iterable ) <EOL> assert s is self . stack . stack [ - <NUM_LIT:1> ] , "<STR_LIT>" "<STR_LIT>" <EOL> assert iterable == self . stack . stack [ - <NUM_LIT:1> ] . _iterable , "<STR_LIT>" "<STR_LIT>" <EOL> def test__top ( self ) : <EOL> assert self . bottom == self . stack . _top , "<STR_LIT>" "<STR_LIT>" <EOL> def test__pop ( self ) : <EOL> assert len ( self . stack . stack ) == <NUM_LIT:1> <EOL> top = self . stack . _pop ( ) <EOL> assert top == self . bottom <EOL> assert len ( self . stack . stack ) == <NUM_LIT:0> <EOL> def test__push ( self ) : <EOL> assert len ( self . stack . stack ) == <NUM_LIT:1> <EOL> iterable = '<STR_LIT>' <EOL> self . stack . _push ( iterable ) <EOL> assert len ( self . stack . stack ) == <NUM_LIT:2> <EOL> assert iterable is self . stack . _top . _iterable <EOL> def test_exit ( self ) : <EOL> iterable = '<STR_LIT>' <EOL> self . stack . _enter ( iterable ) <EOL> before = len ( self . stack . stack ) <EOL> self . stack . _exit ( ) <EOL> after = len ( self . stack . stack ) <EOL> assert before == ( after + <NUM_LIT:1> ) , "<STR_LIT>" <EOL> class TestLoopContext ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> self . iterable = [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] <EOL> self . ctx = LoopContext ( self . iterable ) <EOL> def test___len__ ( self ) : <EOL> assert len ( self . iterable ) == len ( self . ctx ) , "<STR_LIT>" "<STR_LIT>" <EOL> def test_index ( self ) : <EOL> expected = tuple ( range ( len ( self . iterable ) ) ) <EOL> actual = tuple ( self . ctx . index for i in self . ctx ) <EOL> assert expected == actual , "<STR_LIT>" "<STR_LIT>" <EOL> def test_reverse_index ( self ) : <EOL> length = len ( self . iterable ) <EOL> expected = tuple ( [ length - i - <NUM_LIT:1> for i in range ( length ) ] ) <EOL> actual = tuple ( self . ctx . reverse_index for i in self . ctx ) <EOL> print ( expected , actual ) <EOL> assert expected == actual , "<STR_LIT>" "<STR_LIT>" <EOL> def test_first ( self ) : <EOL> expected = ( True , False , False ) <EOL> actual = tuple ( self . ctx . first for i in self . ctx ) <EOL> assert expected == actual , "<STR_LIT>" <EOL> def test_last ( self ) : <EOL> expected = ( False , False , True ) <EOL> actual = tuple ( self . ctx . last for i in self . ctx ) <EOL> assert expected == actual , "<STR_LIT>" <EOL> def test_even ( self ) : <EOL> expected = ( True , False , True ) <EOL> actual = tuple ( self . ctx . even for i in self . ctx ) <EOL> assert expected == actual , "<STR_LIT>" <EOL> def test_odd ( self ) : <EOL> expected = ( False , True , False ) <EOL> actual = tuple ( self . ctx . odd for i in self . ctx ) <EOL> assert expected == actual , "<STR_LIT>" <EOL> def test_cycle ( self ) : <EOL> expected = ( '<STR_LIT:a>' , '<STR_LIT:b>' , '<STR_LIT:a>' ) <EOL> actual = tuple ( self . ctx . cycle ( '<STR_LIT:a>' , '<STR_LIT:b>' ) for i in self . ctx ) <EOL> assert expected == actual , "<STR_LIT>" <EOL> class TestLoopFlags ( TemplateTest ) : <EOL> def test_loop_disabled_template ( self ) : <EOL> self . _do_memory_test ( <EOL> """<STR_LIT>""" , <EOL> "<STR_LIT>" , <EOL> template_args = dict ( loop = '<STR_LIT>' ) , <EOL> filters = flatten_result , <EOL> enable_loop = False <EOL> ) <EOL> def test_loop_disabled_lookup ( self ) : <EOL> l = TemplateLookup ( enable_loop = False ) <EOL> l . put_string ( "<STR_LIT:x>" , <EOL> """<STR_LIT>""" <EOL> ) <EOL> self . _do_test ( <EOL> l . get_template ( "<STR_LIT:x>" ) , <EOL> "<STR_LIT>" , <EOL> template_args = dict ( loop = '<STR_LIT>' ) , <EOL> filters = flatten_result , <EOL> ) <EOL> def test_loop_disabled_override_template ( self ) : <EOL> self . _do_memory_test ( <EOL> """<STR_LIT>""" , <EOL> "<STR_LIT>" , <EOL> template_args = dict ( loop = '<STR_LIT>' ) , <EOL> filters = flatten_result , <EOL> enable_loop = False <EOL> ) <EOL> def test_loop_disabled_override_lookup ( self ) : <EOL> l = TemplateLookup ( enable_loop = False ) <EOL> l . put_string ( "<STR_LIT:x>" , <EOL> """<STR_LIT>""" <EOL> ) <EOL> self . _do_test ( <EOL> l . get_template ( "<STR_LIT:x>" ) , <EOL> "<STR_LIT>" , <EOL> template_args = dict ( loop = '<STR_LIT>' ) , <EOL> filters = flatten_result , <EOL> ) <EOL> def test_loop_enabled_override_template ( self ) : <EOL> self . _do_memory_test ( <EOL> """<STR_LIT>""" , <EOL> "<STR_LIT>" , <EOL> template_args = dict ( ) , <EOL> filters = flatten_result , <EOL> ) <EOL> def test_loop_enabled_override_lookup ( self ) : <EOL> l = TemplateLookup ( ) <EOL> l . put_string ( "<STR_LIT:x>" , <EOL> """<STR_LIT>""" <EOL> ) <EOL> self . _do_test ( <EOL> l . get_template ( "<STR_LIT:x>" ) , <EOL> "<STR_LIT>" , <EOL> template_args = dict ( ) , <EOL> filters = flatten_result , <EOL> ) </s>
