comparison markup/tests/template.py @ 37:37557b8fb925 trunk

Moved some of the tests for the strip directive to a new unittest test case to not clutter up the documentation.
author cmlenz
date Sun, 02 Jul 2006 23:10:27 +0000
parents ed370ebfa794
children a5d585dd38c4
comparison
equal deleted inserted replaced
36:ed370ebfa794 37:37557b8fb925
26 def test_without_strip(self): 26 def test_without_strip(self):
27 """ 27 """
28 Verify that a match template can produce the same kind of element that 28 Verify that a match template can produce the same kind of element that
29 it matched without entering an infinite recursion. 29 it matched without entering an infinite recursion.
30 """ 30 """
31 tmpl = Template('''<doc xmlns:py="http://purl.org/kid/ns#"> 31 tmpl = Template("""<doc xmlns:py="http://purl.org/kid/ns#">
32 <elem py:match="elem"> 32 <elem py:match="elem">
33 <div class="elem">${select('*/text()')}</div> 33 <div class="elem">${select('*/text()')}</div>
34 </elem> 34 </elem>
35 <elem>Hey Joe</elem> 35 <elem>Hey Joe</elem>
36 </doc>''') 36 </doc>""")
37 self.assertEqual("""<doc> 37 self.assertEqual("""<doc>
38 <elem> 38 <elem>
39 <div class="elem">Hey Joe</div> 39 <div class="elem">Hey Joe</div>
40 </elem> 40 </elem>
41 </doc>""", str(tmpl.generate())) 41 </doc>""", str(tmpl.generate()))
43 def test_recursive_match_1(self): 43 def test_recursive_match_1(self):
44 """ 44 """
45 Match directives are applied recursively, meaning that they are also 45 Match directives are applied recursively, meaning that they are also
46 applied to any content they may have produced themselves: 46 applied to any content they may have produced themselves:
47 """ 47 """
48 tmpl = Template('''<doc xmlns:py="http://purl.org/kid/ns#"> 48 tmpl = Template("""<doc xmlns:py="http://purl.org/kid/ns#">
49 <elem py:match="elem"> 49 <elem py:match="elem">
50 <div class="elem"> 50 <div class="elem">
51 ${select('*/*')} 51 ${select('*/*')}
52 </div> 52 </div>
53 </elem> 53 </elem>
54 <elem> 54 <elem>
55 <subelem> 55 <subelem>
56 <elem/> 56 <elem/>
57 </subelem> 57 </subelem>
58 </elem> 58 </elem>
59 </doc>''') 59 </doc>""")
60 self.assertEqual("""<doc> 60 self.assertEqual("""<doc>
61 <elem> 61 <elem>
62 <div class="elem"> 62 <div class="elem">
63 <subelem> 63 <subelem>
64 <elem> 64 <elem>
74 """ 74 """
75 When two or more match templates match the same element and also 75 When two or more match templates match the same element and also
76 themselves output the element they match, avoiding recursion is even 76 themselves output the element they match, avoiding recursion is even
77 more complex, but should work. 77 more complex, but should work.
78 """ 78 """
79 tmpl = Template('''<html xmlns:py="http://purl.org/kid/ns#"> 79 tmpl = Template("""<html xmlns:py="http://purl.org/kid/ns#">
80 <body py:match="body"> 80 <body py:match="body">
81 <div id="header"/> 81 <div id="header"/>
82 ${select('*/*')} 82 ${select('*/*')}
83 </body> 83 </body>
84 <body py:match="body"> 84 <body py:match="body">
86 <div id="footer"/> 86 <div id="footer"/>
87 </body> 87 </body>
88 <body> 88 <body>
89 <h1>Foo</h1> 89 <h1>Foo</h1>
90 </body> 90 </body>
91 </html>''') 91 </html>""")
92 self.assertEqual("""<html> 92 self.assertEqual("""<html>
93 <body> 93 <body>
94 <div id="header"/><h1>Foo</h1> 94 <div id="header"/><h1>Foo</h1>
95 <div id="footer"/> 95 <div id="footer"/>
96 </body> 96 </body>
97 </html>""", str(tmpl.generate())) 97 </html>""", str(tmpl.generate()))
98
99
100 class StripDirectiveTestCase(unittest.TestCase):
101 """Tests for the `py:strip` template directive."""
102
103 def test_strip_false(self):
104 tmpl = Template("""<div xmlns:py="http://purl.org/kid/ns#">
105 <div py:strip="False"><b>foo</b></div>
106 </div>""")
107 self.assertEqual("""<div>
108 <div><b>foo</b></div>
109 </div>""", str(tmpl.generate()))
110
111 def test_strip_empty(self):
112 tmpl = Template("""<div xmlns:py="http://purl.org/kid/ns#">
113 <div py:strip=""><b>foo</b></div>
114 </div>""")
115 self.assertEqual("""<div>
116 <b>foo</b>
117 </div>""", str(tmpl.generate()))
98 118
99 119
100 class TemplateTestCase(unittest.TestCase): 120 class TemplateTestCase(unittest.TestCase):
101 """Tests for basic template processing, expression evaluation and error 121 """Tests for basic template processing, expression evaluation and error
102 reporting. 122 reporting.
184 def suite(): 204 def suite():
185 suite = unittest.TestSuite() 205 suite = unittest.TestSuite()
186 suite.addTest(doctest.DocTestSuite(Template.__module__)) 206 suite.addTest(doctest.DocTestSuite(Template.__module__))
187 suite.addTest(unittest.makeSuite(TemplateTestCase, 'test')) 207 suite.addTest(unittest.makeSuite(TemplateTestCase, 'test'))
188 suite.addTest(unittest.makeSuite(MatchDirectiveTestCase, 'test')) 208 suite.addTest(unittest.makeSuite(MatchDirectiveTestCase, 'test'))
209 suite.addTest(unittest.makeSuite(StripDirectiveTestCase, 'test'))
189 return suite 210 return suite
190 211
191 if __name__ == '__main__': 212 if __name__ == '__main__':
192 unittest.main(defaultTest='suite') 213 unittest.main(defaultTest='suite')
Copyright (C) 2012-2017 Edgewall Software