comparison markup/tests/template.py @ 50:d3842cd76e92 trunk

Fix the way multiple directives are applied to a single `SUB` in many cases by making the directives themselves responsible for applying any remaining directives.
author cmlenz
date Tue, 04 Jul 2006 08:37:25 +0000
parents a5d585dd38c4
children b2383634ec04
comparison
equal deleted inserted replaced
49:6d1f79b2f7ef 50:d3842cd76e92
18 from markup.core import Stream 18 from markup.core import Stream
19 from markup.template import BadDirectiveError, Context, Template, \ 19 from markup.template import BadDirectiveError, Context, Template, \
20 TemplateSyntaxError 20 TemplateSyntaxError
21 21
22 22
23 class AttrsDirectiveTestCase(unittest.TestCase):
24 """Tests for the `py:attrs` template directive."""
25
26 def test_combined_with_loop(self):
27 """
28 Verify that the directive has access to the loop variables.
29 """
30 tmpl = Template("""<doc xmlns:py="http://purl.org/kid/ns#">
31 <elem py:for="item in items" py:attrs="item"/>
32 </doc>""")
33 items = [{'id': 1, 'class': 'foo'}, {'id': 2, 'class': 'bar'}]
34 self.assertEqual("""<doc>
35 <elem id="1" class="foo"/><elem id="2" class="bar"/>
36 </doc>""", str(tmpl.generate(Context(items=items))))
37
38
39 class DefDirectiveTestCase(unittest.TestCase):
40 """Tests for the `py:def` template directive."""
41
42 def test_function_with_strip(self):
43 """
44 Verify that the a named template function with a strip directive
45 actually strips of the outer element.
46 """
47 tmpl = Template("""<doc xmlns:py="http://purl.org/kid/ns#">
48 <div py:def="echo(what)" py:strip="">
49 <b>${what}</b>
50 </div>
51 ${echo('foo')}
52 </doc>""")
53 self.assertEqual("""<doc>
54 <b>foo</b>
55 </doc>""", str(tmpl.generate()))
56
57
23 class MatchDirectiveTestCase(unittest.TestCase): 58 class MatchDirectiveTestCase(unittest.TestCase):
24 """Tests for the `py:match` template directive.""" 59 """Tests for the `py:match` template directive."""
60
61 def test_with_strip(self):
62 """
63 Verify that a match template can produce the same kind of element that
64 it matched without entering an infinite recursion.
65 """
66 tmpl = Template("""<doc xmlns:py="http://purl.org/kid/ns#">
67 <elem py:match="elem" py:strip="">
68 <div class="elem">${select('*/text()')}</div>
69 </elem>
70 <elem>Hey Joe</elem>
71 </doc>""")
72 self.assertEqual("""<doc>
73 <div class="elem">Hey Joe</div>
74 </doc>""", str(tmpl.generate()))
25 75
26 def test_without_strip(self): 76 def test_without_strip(self):
27 """ 77 """
28 Verify that a match template can produce the same kind of element that 78 Verify that a match template can produce the same kind of element that
29 it matched without entering an infinite recursion. 79 it matched without entering an infinite recursion.
208 258
209 def suite(): 259 def suite():
210 suite = unittest.TestSuite() 260 suite = unittest.TestSuite()
211 suite.addTest(doctest.DocTestSuite(Template.__module__)) 261 suite.addTest(doctest.DocTestSuite(Template.__module__))
212 suite.addTest(unittest.makeSuite(TemplateTestCase, 'test')) 262 suite.addTest(unittest.makeSuite(TemplateTestCase, 'test'))
263 suite.addTest(unittest.makeSuite(AttrsDirectiveTestCase, 'test'))
264 suite.addTest(unittest.makeSuite(DefDirectiveTestCase, 'test'))
213 suite.addTest(unittest.makeSuite(MatchDirectiveTestCase, 'test')) 265 suite.addTest(unittest.makeSuite(MatchDirectiveTestCase, 'test'))
214 suite.addTest(unittest.makeSuite(StripDirectiveTestCase, 'test')) 266 suite.addTest(unittest.makeSuite(StripDirectiveTestCase, 'test'))
215 return suite 267 return suite
216 268
217 if __name__ == '__main__': 269 if __name__ == '__main__':
Copyright (C) 2012-2017 Edgewall Software