comparison markup/tests/template.py @ 65:b3fdf93057ab trunk

Support the use of directives as elements to reduce the need for using `py:strip`.
author cmlenz
date Sun, 09 Jul 2006 15:23:26 +0000
parents 448792ab1303
children 59eb24184e9c
comparison
equal deleted inserted replaced
64:1db4839e6197 65:b3fdf93057ab
119 </doc>""") 119 </doc>""")
120 self.assertEqual("""<doc> 120 self.assertEqual("""<doc>
121 <span>foo</span> 121 <span>foo</span>
122 </doc>""", str(tmpl.generate())) 122 </doc>""", str(tmpl.generate()))
123 123
124 def test_as_element(self):
125 """
126 Verify that the directive can also be used as an element.
127 """
128 tmpl = Template("""<doc xmlns:py="http://markup.edgewall.org/">
129 <py:choose>
130 <py:when test="1 == 1">1</py:when>
131 <py:when test="2 == 2">2</py:when>
132 <py:when test="3 == 3">3</py:when>
133 </py:choose>
134 </doc>""")
135 self.assertEqual("""<doc>
136 1
137 </doc>""", str(tmpl.generate()))
138
124 139
125 class DefDirectiveTestCase(unittest.TestCase): 140 class DefDirectiveTestCase(unittest.TestCase):
126 """Tests for the `py:def` template directive.""" 141 """Tests for the `py:def` template directive."""
127 142
128 def test_function_with_strip(self): 143 def test_function_with_strip(self):
132 """ 147 """
133 tmpl = Template("""<doc xmlns:py="http://markup.edgewall.org/"> 148 tmpl = Template("""<doc xmlns:py="http://markup.edgewall.org/">
134 <div py:def="echo(what)" py:strip=""> 149 <div py:def="echo(what)" py:strip="">
135 <b>${what}</b> 150 <b>${what}</b>
136 </div> 151 </div>
152 ${echo('foo')}
153 </doc>""")
154 self.assertEqual("""<doc>
155 <b>foo</b>
156 </doc>""", str(tmpl.generate()))
157
158 def test_as_element(self):
159 """
160 Verify that the directive can also be used as an element.
161 """
162 tmpl = Template("""<doc xmlns:py="http://markup.edgewall.org/">
163 <py:def function="echo(what)">
164 <b>${what}</b>
165 </py:def>
137 ${echo('foo')} 166 ${echo('foo')}
138 </doc>""") 167 </doc>""")
139 self.assertEqual("""<doc> 168 self.assertEqual("""<doc>
140 <b>foo</b> 169 <b>foo</b>
141 </doc>""", str(tmpl.generate())) 170 </doc>""", str(tmpl.generate()))
160 <b>3</b> 189 <b>3</b>
161 <b>4</b> 190 <b>4</b>
162 <b>5</b> 191 <b>5</b>
163 </doc>""", str(tmpl.generate(Context(items=range(1, 6))))) 192 </doc>""", str(tmpl.generate(Context(items=range(1, 6)))))
164 193
194 def test_as_element(self):
195 """
196 Verify that the directive can also be used as an element.
197 """
198 tmpl = Template("""<doc xmlns:py="http://markup.edgewall.org/">
199 <py:for each="item in items">
200 <b>${item}</b>
201 </py:for>
202 </doc>""")
203 self.assertEqual("""<doc>
204 <b>1</b>
205 <b>2</b>
206 <b>3</b>
207 <b>4</b>
208 <b>5</b>
209 </doc>""", str(tmpl.generate(Context(items=range(1, 6)))))
210
211
212 class IfDirectiveTestCase(unittest.TestCase):
213 """Tests for the `py:if` template directive."""
214
215 def test_loop_with_strip(self):
216 """
217 Verify that the combining the `py:if` directive with `py:strip` works
218 correctly.
219 """
220 tmpl = Template("""<doc xmlns:py="http://markup.edgewall.org/">
221 <b py:if="foo" py:strip="">${bar}</b>
222 </doc>""")
223 self.assertEqual("""<doc>
224 Hello
225 </doc>""", str(tmpl.generate(Context(foo=True, bar='Hello'))))
226
227 def test_as_element(self):
228 """
229 Verify that the directive can also be used as an element.
230 """
231 tmpl = Template("""<doc xmlns:py="http://markup.edgewall.org/">
232 <py:if test="foo">${bar}</py:if>
233 </doc>""")
234 self.assertEqual("""<doc>
235 Hello
236 </doc>""", str(tmpl.generate(Context(foo=True, bar='Hello'))))
237
165 238
166 class MatchDirectiveTestCase(unittest.TestCase): 239 class MatchDirectiveTestCase(unittest.TestCase):
167 """Tests for the `py:match` template directive.""" 240 """Tests for the `py:match` template directive."""
168 241
169 def test_with_strip(self): 242 def test_with_strip(self):
194 </doc>""") 267 </doc>""")
195 self.assertEqual("""<doc> 268 self.assertEqual("""<doc>
196 <elem> 269 <elem>
197 <div class="elem">Hey Joe</div> 270 <div class="elem">Hey Joe</div>
198 </elem> 271 </elem>
272 </doc>""", str(tmpl.generate()))
273
274 def test_as_element(self):
275 """
276 Verify that the directive can also be used as an element.
277 """
278 tmpl = Template("""<doc xmlns:py="http://markup.edgewall.org/">
279 <py:match path="elem">
280 <div class="elem">${select('*/text()')}</div>
281 </py:match>
282 <elem>Hey Joe</elem>
283 </doc>""")
284 self.assertEqual("""<doc>
285 <div class="elem">Hey Joe</div>
199 </doc>""", str(tmpl.generate())) 286 </doc>""", str(tmpl.generate()))
200 287
201 def test_recursive_match_1(self): 288 def test_recursive_match_1(self):
202 """ 289 """
203 Match directives are applied recursively, meaning that they are also 290 Match directives are applied recursively, meaning that they are also
395 </div>""") 482 </div>""")
396 self.assertEqual("""<div> 483 self.assertEqual("""<div>
397 <elem class="&#34;foo&#34;"/> 484 <elem class="&#34;foo&#34;"/>
398 </div>""", str(tmpl.generate(Context(myvar='"foo"')))) 485 </div>""", str(tmpl.generate(Context(myvar='"foo"'))))
399 486
487 def test_directive_element(self):
488 tmpl = Template("""<div xmlns:py="http://markup.edgewall.org/">
489 <py:if test="myvar">bar</py:if>
490 </div>""")
491 self.assertEqual("""<div>
492 bar
493 </div>""", str(tmpl.generate(Context(myvar='"foo"'))))
494
400 495
401 def suite(): 496 def suite():
402 suite = unittest.TestSuite() 497 suite = unittest.TestSuite()
403 suite.addTest(doctest.DocTestSuite(Template.__module__)) 498 suite.addTest(doctest.DocTestSuite(Template.__module__))
404 suite.addTest(unittest.makeSuite(TemplateTestCase, 'test')) 499 suite.addTest(unittest.makeSuite(TemplateTestCase, 'test'))
405 suite.addTest(unittest.makeSuite(AttrsDirectiveTestCase, 'test')) 500 suite.addTest(unittest.makeSuite(AttrsDirectiveTestCase, 'test'))
406 suite.addTest(unittest.makeSuite(ChooseDirectiveTestCase, 'test')) 501 suite.addTest(unittest.makeSuite(ChooseDirectiveTestCase, 'test'))
407 suite.addTest(unittest.makeSuite(DefDirectiveTestCase, 'test')) 502 suite.addTest(unittest.makeSuite(DefDirectiveTestCase, 'test'))
408 suite.addTest(unittest.makeSuite(ForDirectiveTestCase, 'test')) 503 suite.addTest(unittest.makeSuite(ForDirectiveTestCase, 'test'))
504 suite.addTest(unittest.makeSuite(IfDirectiveTestCase, 'test'))
409 suite.addTest(unittest.makeSuite(MatchDirectiveTestCase, 'test')) 505 suite.addTest(unittest.makeSuite(MatchDirectiveTestCase, 'test'))
410 suite.addTest(unittest.makeSuite(StripDirectiveTestCase, 'test')) 506 suite.addTest(unittest.makeSuite(StripDirectiveTestCase, 'test'))
411 return suite 507 return suite
412 508
413 if __name__ == '__main__': 509 if __name__ == '__main__':
Copyright (C) 2012-2017 Edgewall Software