comparison markup/tests/template.py @ 190:769d945ac030 trunk

Improvements for the `py:with` directive: * One assignment in the same directive can now refer to an earlier assignment. * Semicolons used in string literals inside the expression are no longer treated as statement separators, and they don't need to be escaped then either. * Trailing semicolons are now ignored Many thanks to Oliver Cope for reporting these problems, and providing unit tests and a patch!
author cmlenz
date Wed, 23 Aug 2006 14:33:37 +0000
parents 95c3813a00de
children 3289055a8c32
comparison
equal deleted inserted replaced
189:0be9ba422a33 190:769d945ac030
572 </div>""") 572 </div>""")
573 self.assertEqual("""<div> 573 self.assertEqual("""<div>
574 84 574 84
575 </div>""", str(tmpl.generate(x=42))) 575 </div>""", str(tmpl.generate(x=42)))
576 576
577 def test_multiple_vars_same_name(self):
578 tmpl = Template("""<div xmlns:py="http://markup.edgewall.org/">
579 <py:with vars="
580 foo = 'bar';
581 foo = foo.replace('r', 'z')
582 ">
583 $foo
584 </py:with>
585 </div>""")
586 self.assertEqual("""<div>
587 baz
588 </div>""", str(tmpl.generate(x=42)))
589
590 def test_multiple_vars_single_assignment(self):
591 tmpl = Template("""<div xmlns:py="http://markup.edgewall.org/">
592 <py:with vars="x = y = z = 1">${x} ${y} ${z}</py:with>
593 </div>""")
594 self.assertEqual("""<div>
595 1 1 1
596 </div>""", str(tmpl.generate(x=42)))
597
598 def test_multiple_vars_trailing_semicolon(self):
599 tmpl = Template("""<div xmlns:py="http://markup.edgewall.org/">
600 <py:with vars="x = x * 2; y = x / 2;">${x} ${y}</py:with>
601 </div>""")
602 self.assertEqual("""<div>
603 84 42
604 </div>""", str(tmpl.generate(x=42)))
605
606 def test_semicolon_escape(self):
607 tmpl = Template("""<div xmlns:py="http://markup.edgewall.org/">
608 <py:with vars="x = 'here is a semicolon: ;'; y = 'here are two semicolons: ;;' ;">
609 ${x}
610 ${y}
611 </py:with>
612 </div>""")
613 self.assertEqual("""<div>
614 here is a semicolon: ;
615 here are two semicolons: ;;
616 </div>""", str(tmpl.generate()))
617
577 618
578 class TemplateTestCase(unittest.TestCase): 619 class TemplateTestCase(unittest.TestCase):
579 """Tests for basic template processing, expression evaluation and error 620 """Tests for basic template processing, expression evaluation and error
580 reporting. 621 reporting.
581 """ 622 """
Copyright (C) 2012-2017 Edgewall Software