Skip to content
Myr

add tests for lstrip_blocks option

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Kristi Tsukida · committed by Kristi Tsukida · parent c5f6531 · Browse files · 1 file+162 added−0 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+162 added−0 removed

  • other:+162 added−0 removed

1 file+162 added−0 removed

modified:jinja2/testsuite/lexnparse.py+162 added
377 unmodified lines
378 tmpl = env.from_string('{{ -foo["bar"]|abs }}')379 assert tmpl.render(foo={'bar': 42}) == '42'380381382class LstripBlocksTestCase(JinjaTestCase):383384 def test_lstrip(self):385 env = Environment(lstrip_blocks=True, trim_blocks=False)386 tmpl = env.from_string(''' {% if True %}387 {% endif %}''')388 assert tmpl.render() == "\n"389390 def test_lstrip_trim(self):391 env = Environment(lstrip_blocks=True, trim_blocks=True)392 tmpl = env.from_string(''' {% if True %}393 {% endif %}''')394 assert tmpl.render() == ""395396 def test_lstrip_endline(self):397 env = Environment(lstrip_blocks=True, trim_blocks=True)398 tmpl = env.from_string(''' {% if True %} \n {% endif %} ''')399 assert tmpl.render() == " \n "400401 def test_lstrip_inline(self):402 env = Environment(lstrip_blocks=True, trim_blocks=False)403 tmpl = env.from_string(''' {% if True %}hello{% endif %}''')404 assert tmpl.render() == ' hello'405406 def test_lstrip_nested(self):407 env = Environment(lstrip_blocks=True, trim_blocks=False)408 tmpl = env.from_string(''' {% if True %}{% if True %}hello{% endif %}{% endif %}''')409 assert tmpl.render() == ' hello'410411 def test_lstrip_left_chars(self):412 env = Environment(lstrip_blocks=True, trim_blocks=False)413 tmpl = env.from_string(''' abc {% if True %}414 hello{% endif %}''')415 assert tmpl.render() == ' abc \n hello'416417 def test_lstrip_embeded_strings(self):418 env = Environment(lstrip_blocks=True, trim_blocks=False)419 tmpl = env.from_string(''' {% if "{% str %}" %}420hello421{% endif %}''')422 assert tmpl.render() == '\nhello\n'423424 def test_lstrip_preserve_leading_newlines(self):425 env = Environment(lstrip_blocks=True, trim_blocks=False)426 tmpl = env.from_string('''\427 428 {% if True %}429 hello430431 {% endif %}''')432 assert tmpl.render() == ' \n\n hello\n\n'433 434 def test_lstrip_comment(self):435 env = Environment(lstrip_blocks=True, trim_blocks=False)436 tmpl = env.from_string(''' {# if True #}437hello438 {#endif#}''')439 assert tmpl.render() == ' \nhello\n '440441 def test_lstrip_angle_bracket_simple(self):442 env = Environment('<%', '%>', '${', '}', '<%#', '%>', '%', '##',443 lstrip_blocks=True, trim_blocks=True)444 tmpl = env.from_string('''\445 <% if True %>hello<% endif %>''')446 assert tmpl.render() == ' hello'447448 def test_lstrip_angle_bracket_comment(self):449 env = Environment('<%', '%>', '${', '}', '<%#', '%>', '%', '##',450 lstrip_blocks=True, trim_blocks=True)451 tmpl = env.from_string('''\452 <%# if True %>hello<%# endif %>''')453 assert tmpl.render() == ' hello'454455 def test_lstrip_angle_bracket(self):456 env = Environment('<%', '%>', '${', '}', '<%#', '%>', '%', '##',457 lstrip_blocks=True, trim_blocks=True)458 tmpl = env.from_string('''\459 <%# regular comment %>460 <% for item in seq %>461${item} ## the rest of the stuff462 <% endfor %>''')463 assert tmpl.render(seq=range(5)) == \464 ' ' + ''.join(str(x) + '\n' for x in range(5))465 466 def test_lstrip_angle_bracket_compact(self):467 env = Environment('<%', '%>', '${', '}', '<%#', '%>', '%', '##',468 lstrip_blocks=True, trim_blocks=True)469 tmpl = env.from_string('''\470 <%#regular comment%>471 <%for item in seq%>472${item} ## the rest of the stuff473 <%endfor%>''')474 assert tmpl.render(seq=range(5)) == \475 ' ' + ''.join('%s\n' % x for x in range(5))476 477 def test_php_syntax_with_manual(self):478 env = Environment('<?', '?>', '<?=', '?>', '<!--', '-->',479 lstrip_blocks=True, trim_blocks=True)480 tmpl = env.from_string('''\481<!-- I'm a comment, I'm not interesting -->482 <? for item in seq -?>483 <?= item ?>484 <?- endfor ?>''')485 assert tmpl.render(seq=range(5)) == '01234'486487 def test_php_syntax(self):488 env = Environment('<?', '?>', '<?=', '?>', '<!--', '-->',489 lstrip_blocks=True, trim_blocks=True)490 tmpl = env.from_string('''\491 <!-- I'm a comment, I'm not interesting -->492 <? for item in seq ?>493 <?= item ?>494 <? endfor ?>''')495 assert tmpl.render(seq=range(5)) == ' ' + ''.join(' %s\n' % x for x in range(5))496497 def test_php_syntax_compact(self):498 env = Environment('<?', '?>', '<?=', '?>', '<!--', '-->',499 lstrip_blocks=True, trim_blocks=True)500 tmpl = env.from_string('''\501 <!-- I'm a comment, I'm not interesting -->502 <?for item in seq?>503 <?=item?>504 <?endfor?>''')505 assert tmpl.render(seq=range(5)) == ' ' + ''.join(' %s\n' % x for x in range(5))506507 def test_erb_syntax_with_manual(self):508 env = Environment('<%', '%>', '<%=', '%>', '<%#', '%>',509 lstrip_blocks=True, trim_blocks=True)510 tmpl = env.from_string('''\511<%# I'm a comment, I'm not interesting %>512 <% for item in seq -%>513 <%= item %>514 <%- endfor %>''')515 assert tmpl.render(seq=range(5)) == '01234'516517 def test_erb_syntax(self):518 env = Environment('<%', '%>', '<%=', '%>', '<%#', '%>',519 lstrip_blocks=True, trim_blocks=True)520 #env.from_string('')521 #for n,r in env.lexer.rules.iteritems():522 # print n523 #print env.lexer.rules['root'][0][0].pattern524 #print "'%s'" % tmpl.render(seq=range(5))525 tmpl = env.from_string('''\526<%# I'm a comment, I'm not interesting %>527 <% for item in seq %>528 <%= item %>529 <% endfor %>530''')531 assert tmpl.render(seq=range(5)) == ''.join(' %s\n' % x for x in range(5))532533 def test_comment_syntax(self):534 env = Environment('<!--', '-->', '${', '}', '<!--#', '-->',535 lstrip_blocks=True, trim_blocks=True)536 tmpl = env.from_string('''\537<!--# I'm a comment, I'm not interesting -->\538<!-- for item in seq --->539 ${item}540<!--- endfor -->''')541 assert tmpl.render(seq=range(5)) == '01234'542543def suite():544 suite = unittest.TestSuite()545 suite.addTest(unittest.makeSuite(LexerTestCase))546 suite.addTest(unittest.makeSuite(ParserTestCase))547 suite.addTest(unittest.makeSuite(SyntaxTestCase))548 suite.addTest(unittest.makeSuite(LstripBlocksTestCase))549 return suite