Mercurial > genshi > mirror
comparison markup/tests/template.py @ 36:ed370ebfa794 trunk
Fix for #7: match templates no longer process their own output.
author | cmlenz |
---|---|
date | Sun, 02 Jul 2006 22:39:03 +0000 |
parents | b4f78c05e5c9 |
children | 37557b8fb925 |
comparison
equal
deleted
inserted
replaced
35:35b9e9318fb1 | 36:ed370ebfa794 |
---|---|
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 MatchDirectiveTestCase(unittest.TestCase): | |
24 """Tests for the `py:match` template directive.""" | |
25 | |
26 def test_without_strip(self): | |
27 """ | |
28 Verify that a match template can produce the same kind of element that | |
29 it matched without entering an infinite recursion. | |
30 """ | |
31 tmpl = Template('''<doc xmlns:py="http://purl.org/kid/ns#"> | |
32 <elem py:match="elem"> | |
33 <div class="elem">${select('*/text()')}</div> | |
34 </elem> | |
35 <elem>Hey Joe</elem> | |
36 </doc>''') | |
37 self.assertEqual("""<doc> | |
38 <elem> | |
39 <div class="elem">Hey Joe</div> | |
40 </elem> | |
41 </doc>""", str(tmpl.generate())) | |
42 | |
43 def test_recursive_match_1(self): | |
44 """ | |
45 Match directives are applied recursively, meaning that they are also | |
46 applied to any content they may have produced themselves: | |
47 """ | |
48 tmpl = Template('''<doc xmlns:py="http://purl.org/kid/ns#"> | |
49 <elem py:match="elem"> | |
50 <div class="elem"> | |
51 ${select('*/*')} | |
52 </div> | |
53 </elem> | |
54 <elem> | |
55 <subelem> | |
56 <elem/> | |
57 </subelem> | |
58 </elem> | |
59 </doc>''') | |
60 self.assertEqual("""<doc> | |
61 <elem> | |
62 <div class="elem"> | |
63 <subelem> | |
64 <elem> | |
65 <div class="elem"> | |
66 </div> | |
67 </elem> | |
68 </subelem> | |
69 </div> | |
70 </elem> | |
71 </doc>""", str(tmpl.generate())) | |
72 | |
73 def test_recursive_match_2(self): | |
74 """ | |
75 When two or more match templates match the same element and also | |
76 themselves output the element they match, avoiding recursion is even | |
77 more complex, but should work. | |
78 """ | |
79 tmpl = Template('''<html xmlns:py="http://purl.org/kid/ns#"> | |
80 <body py:match="body"> | |
81 <div id="header"/> | |
82 ${select('*/*')} | |
83 </body> | |
84 <body py:match="body"> | |
85 ${select('*/*')} | |
86 <div id="footer"/> | |
87 </body> | |
88 <body> | |
89 <h1>Foo</h1> | |
90 </body> | |
91 </html>''') | |
92 self.assertEqual("""<html> | |
93 <body> | |
94 <div id="header"/><h1>Foo</h1> | |
95 <div id="footer"/> | |
96 </body> | |
97 </html>""", str(tmpl.generate())) | |
98 | |
99 | |
23 class TemplateTestCase(unittest.TestCase): | 100 class TemplateTestCase(unittest.TestCase): |
101 """Tests for basic template processing, expression evaluation and error | |
102 reporting. | |
103 """ | |
24 | 104 |
25 def test_interpolate_string(self): | 105 def test_interpolate_string(self): |
26 parts = list(Template._interpolate('bla')) | 106 parts = list(Template._interpolate('bla')) |
27 self.assertEqual(1, len(parts)) | 107 self.assertEqual(1, len(parts)) |
28 self.assertEqual(Stream.TEXT, parts[0][0]) | 108 self.assertEqual(Stream.TEXT, parts[0][0]) |
103 | 183 |
104 def suite(): | 184 def suite(): |
105 suite = unittest.TestSuite() | 185 suite = unittest.TestSuite() |
106 suite.addTest(doctest.DocTestSuite(Template.__module__)) | 186 suite.addTest(doctest.DocTestSuite(Template.__module__)) |
107 suite.addTest(unittest.makeSuite(TemplateTestCase, 'test')) | 187 suite.addTest(unittest.makeSuite(TemplateTestCase, 'test')) |
188 suite.addTest(unittest.makeSuite(MatchDirectiveTestCase, 'test')) | |
108 return suite | 189 return suite |
109 | 190 |
110 if __name__ == '__main__': | 191 if __name__ == '__main__': |
111 unittest.main(defaultTest='suite') | 192 unittest.main(defaultTest='suite') |