comparison markup/tests/template.py @ 220:f4943d2babda trunk

Fix for #45 and #46: properly support assignment to nested tuples in `py:for` and `py:with` directives.
author cmlenz
date Tue, 05 Sep 2006 16:33:13 +0000
parents f150cff4da18
children
comparison
equal deleted inserted replaced
219:ebceef564b79 220:f4943d2babda
378 <b>2</b> 378 <b>2</b>
379 <b>3</b> 379 <b>3</b>
380 <b>4</b> 380 <b>4</b>
381 <b>5</b> 381 <b>5</b>
382 </doc>""", str(tmpl.generate(items=range(1, 6)))) 382 </doc>""", str(tmpl.generate(items=range(1, 6))))
383
384 def test_multi_assignment(self):
385 """
386 Verify that assignment to tuples works correctly.
387 """
388 tmpl = Template("""<doc xmlns:py="http://markup.edgewall.org/">
389 <py:for each="k, v in items">
390 <p>key=$k, value=$v</p>
391 </py:for>
392 </doc>""")
393 self.assertEqual("""<doc>
394 <p>key=a, value=1</p>
395 <p>key=b, value=2</p>
396 </doc>""", str(tmpl.generate(items=dict(a=1, b=2).items())))
397
398 def test_nested_assignment(self):
399 """
400 Verify that assignment to nested tuples works correctly.
401 """
402 tmpl = Template("""<doc xmlns:py="http://markup.edgewall.org/">
403 <py:for each="idx, (k, v) in items">
404 <p>$idx: key=$k, value=$v</p>
405 </py:for>
406 </doc>""")
407 self.assertEqual("""<doc>
408 <p>0: key=a, value=1</p>
409 <p>1: key=b, value=2</p>
410 </doc>""", str(tmpl.generate(items=enumerate(dict(a=1, b=2).items()))))
383 411
384 412
385 class IfDirectiveTestCase(unittest.TestCase): 413 class IfDirectiveTestCase(unittest.TestCase):
386 """Tests for the `py:if` template directive.""" 414 """Tests for the `py:if` template directive."""
387 415
708 </div>""") 736 </div>""")
709 self.assertEqual("""<div> 737 self.assertEqual("""<div>
710 1 1 1 738 1 1 1
711 </div>""", str(tmpl.generate(x=42))) 739 </div>""", str(tmpl.generate(x=42)))
712 740
741 def test_nested_vars_single_assignment(self):
742 tmpl = Template("""<div xmlns:py="http://markup.edgewall.org/">
743 <py:with vars="x, (y, z) = (1, (2, 3))">${x} ${y} ${z}</py:with>
744 </div>""")
745 self.assertEqual("""<div>
746 1 2 3
747 </div>""", str(tmpl.generate(x=42)))
748
713 def test_multiple_vars_trailing_semicolon(self): 749 def test_multiple_vars_trailing_semicolon(self):
714 tmpl = Template("""<div xmlns:py="http://markup.edgewall.org/"> 750 tmpl = Template("""<div xmlns:py="http://markup.edgewall.org/">
715 <py:with vars="x = x * 2; y = x / 2;">${x} ${y}</py:with> 751 <py:with vars="x = x * 2; y = x / 2;">${x} ${y}</py:with>
716 </div>""") 752 </div>""")
717 self.assertEqual("""<div> 753 self.assertEqual("""<div>
Copyright (C) 2012-2017 Edgewall Software