Mercurial > genshi > genshi-test
comparison markup/tests/template.py @ 61:33c2702cf6da
Use a different namespace than Kid uses.
author | cmlenz |
---|---|
date | Fri, 07 Jul 2006 17:54:52 +0000 |
parents | 01981cbc7575 |
children | 5c024cf58ecb |
comparison
equal
deleted
inserted
replaced
60:94154a39c986 | 61:33c2702cf6da |
---|---|
25 | 25 |
26 def test_combined_with_loop(self): | 26 def test_combined_with_loop(self): |
27 """ | 27 """ |
28 Verify that the directive has access to the loop variables. | 28 Verify that the directive has access to the loop variables. |
29 """ | 29 """ |
30 tmpl = Template("""<doc xmlns:py="http://purl.org/kid/ns#"> | 30 tmpl = Template("""<doc xmlns:py="http://markup.edgewall.org/"> |
31 <elem py:for="item in items" py:attrs="item"/> | 31 <elem py:for="item in items" py:attrs="item"/> |
32 </doc>""") | 32 </doc>""") |
33 items = [{'id': 1, 'class': 'foo'}, {'id': 2, 'class': 'bar'}] | 33 items = [{'id': 1, 'class': 'foo'}, {'id': 2, 'class': 'bar'}] |
34 self.assertEqual("""<doc> | 34 self.assertEqual("""<doc> |
35 <elem id="1" class="foo"/><elem id="2" class="bar"/> | 35 <elem id="1" class="foo"/><elem id="2" class="bar"/> |
38 def test_update_existing_attr(self): | 38 def test_update_existing_attr(self): |
39 """ | 39 """ |
40 Verify that an attribute value that evaluates to `None` removes an | 40 Verify that an attribute value that evaluates to `None` removes an |
41 existing attribute of that name. | 41 existing attribute of that name. |
42 """ | 42 """ |
43 tmpl = Template("""<doc xmlns:py="http://purl.org/kid/ns#"> | 43 tmpl = Template("""<doc xmlns:py="http://markup.edgewall.org/"> |
44 <elem class="foo" py:attrs="{'class': 'bar'}"/> | 44 <elem class="foo" py:attrs="{'class': 'bar'}"/> |
45 </doc>""") | 45 </doc>""") |
46 self.assertEqual("""<doc> | 46 self.assertEqual("""<doc> |
47 <elem class="bar"/> | 47 <elem class="bar"/> |
48 </doc>""", str(tmpl.generate())) | 48 </doc>""", str(tmpl.generate())) |
50 def test_remove_existing_attr(self): | 50 def test_remove_existing_attr(self): |
51 """ | 51 """ |
52 Verify that an attribute value that evaluates to `None` removes an | 52 Verify that an attribute value that evaluates to `None` removes an |
53 existing attribute of that name. | 53 existing attribute of that name. |
54 """ | 54 """ |
55 tmpl = Template("""<doc xmlns:py="http://purl.org/kid/ns#"> | 55 tmpl = Template("""<doc xmlns:py="http://markup.edgewall.org/"> |
56 <elem class="foo" py:attrs="{'class': None}"/> | 56 <elem class="foo" py:attrs="{'class': None}"/> |
57 </doc>""") | 57 </doc>""") |
58 self.assertEqual("""<doc> | 58 self.assertEqual("""<doc> |
59 <elem/> | 59 <elem/> |
60 </doc>""", str(tmpl.generate())) | 60 </doc>""", str(tmpl.generate())) |
67 def test_multiple_true_whens(self): | 67 def test_multiple_true_whens(self): |
68 """ | 68 """ |
69 Verify that, if multiple `py:when` bodies match, only the first is | 69 Verify that, if multiple `py:when` bodies match, only the first is |
70 output. | 70 output. |
71 """ | 71 """ |
72 tmpl = Template("""<div xmlns:py="http://purl.org/kid/ns#" py:choose=""> | 72 tmpl = Template("""<div xmlns:py="http://markup.edgewall.org/" py:choose=""> |
73 <span py:when="1 == 1">1</span> | 73 <span py:when="1 == 1">1</span> |
74 <span py:when="2 == 2">2</span> | 74 <span py:when="2 == 2">2</span> |
75 <span py:when="3 == 3">3</span> | 75 <span py:when="3 == 3">3</span> |
76 </div>""") | 76 </div>""") |
77 self.assertEqual("""<div> | 77 self.assertEqual("""<div> |
78 <span>1</span> | 78 <span>1</span> |
79 </div>""", str(tmpl.generate())) | 79 </div>""", str(tmpl.generate())) |
80 | 80 |
81 def test_otherwise(self): | 81 def test_otherwise(self): |
82 tmpl = Template("""<div xmlns:py="http://purl.org/kid/ns#" py:choose=""> | 82 tmpl = Template("""<div xmlns:py="http://markup.edgewall.org/" py:choose=""> |
83 <span py:when="False">hidden</span> | 83 <span py:when="False">hidden</span> |
84 <span py:otherwise="">hello</span> | 84 <span py:otherwise="">hello</span> |
85 </div>""") | 85 </div>""") |
86 self.assertEqual("""<div> | 86 self.assertEqual("""<div> |
87 <span>hello</span> | 87 <span>hello</span> |
89 | 89 |
90 def test_nesting(self): | 90 def test_nesting(self): |
91 """ | 91 """ |
92 Verify that `py:choose` blocks can be nested: | 92 Verify that `py:choose` blocks can be nested: |
93 """ | 93 """ |
94 tmpl = Template("""<doc xmlns:py="http://purl.org/kid/ns#"> | 94 tmpl = Template("""<doc xmlns:py="http://markup.edgewall.org/"> |
95 <div py:choose="1"> | 95 <div py:choose="1"> |
96 <div py:when="1" py:choose="3"> | 96 <div py:when="1" py:choose="3"> |
97 <span py:when="2">2</span> | 97 <span py:when="2">2</span> |
98 <span py:when="3">3</span> | 98 <span py:when="3">3</span> |
99 </div> | 99 </div> |
110 def test_when_with_strip(self): | 110 def test_when_with_strip(self): |
111 """ | 111 """ |
112 Verify that a when directive with a strip directive actually strips of | 112 Verify that a when directive with a strip directive actually strips of |
113 the outer element. | 113 the outer element. |
114 """ | 114 """ |
115 tmpl = Template("""<doc xmlns:py="http://purl.org/kid/ns#"> | 115 tmpl = Template("""<doc xmlns:py="http://markup.edgewall.org/"> |
116 <div py:choose="" py:strip=""> | 116 <div py:choose="" py:strip=""> |
117 <span py:otherwise="">foo</span> | 117 <span py:otherwise="">foo</span> |
118 </div> | 118 </div> |
119 </doc>""") | 119 </doc>""") |
120 self.assertEqual("""<doc> | 120 self.assertEqual("""<doc> |
128 def test_function_with_strip(self): | 128 def test_function_with_strip(self): |
129 """ | 129 """ |
130 Verify that a named template function with a strip directive actually | 130 Verify that a named template function with a strip directive actually |
131 strips of the outer element. | 131 strips of the outer element. |
132 """ | 132 """ |
133 tmpl = Template("""<doc xmlns:py="http://purl.org/kid/ns#"> | 133 tmpl = Template("""<doc xmlns:py="http://markup.edgewall.org/"> |
134 <div py:def="echo(what)" py:strip=""> | 134 <div py:def="echo(what)" py:strip=""> |
135 <b>${what}</b> | 135 <b>${what}</b> |
136 </div> | 136 </div> |
137 ${echo('foo')} | 137 ${echo('foo')} |
138 </doc>""") | 138 </doc>""") |
147 def test_loop_with_strip(self): | 147 def test_loop_with_strip(self): |
148 """ | 148 """ |
149 Verify that the combining the `py:for` directive with `py:strip` works | 149 Verify that the combining the `py:for` directive with `py:strip` works |
150 correctly. | 150 correctly. |
151 """ | 151 """ |
152 tmpl = Template("""<doc xmlns:py="http://purl.org/kid/ns#"> | 152 tmpl = Template("""<doc xmlns:py="http://markup.edgewall.org/"> |
153 <div py:for="item in items" py:strip=""> | 153 <div py:for="item in items" py:strip=""> |
154 <b>${item}</b> | 154 <b>${item}</b> |
155 </div> | 155 </div> |
156 </doc>""") | 156 </doc>""") |
157 self.assertEqual("""<doc> | 157 self.assertEqual("""<doc> |
169 def test_with_strip(self): | 169 def test_with_strip(self): |
170 """ | 170 """ |
171 Verify that a match template can produce the same kind of element that | 171 Verify that a match template can produce the same kind of element that |
172 it matched without entering an infinite recursion. | 172 it matched without entering an infinite recursion. |
173 """ | 173 """ |
174 tmpl = Template("""<doc xmlns:py="http://purl.org/kid/ns#"> | 174 tmpl = Template("""<doc xmlns:py="http://markup.edgewall.org/"> |
175 <elem py:match="elem" py:strip=""> | 175 <elem py:match="elem" py:strip=""> |
176 <div class="elem">${select('*/text()')}</div> | 176 <div class="elem">${select('*/text()')}</div> |
177 </elem> | 177 </elem> |
178 <elem>Hey Joe</elem> | 178 <elem>Hey Joe</elem> |
179 </doc>""") | 179 </doc>""") |
184 def test_without_strip(self): | 184 def test_without_strip(self): |
185 """ | 185 """ |
186 Verify that a match template can produce the same kind of element that | 186 Verify that a match template can produce the same kind of element that |
187 it matched without entering an infinite recursion. | 187 it matched without entering an infinite recursion. |
188 """ | 188 """ |
189 tmpl = Template("""<doc xmlns:py="http://purl.org/kid/ns#"> | 189 tmpl = Template("""<doc xmlns:py="http://markup.edgewall.org/"> |
190 <elem py:match="elem"> | 190 <elem py:match="elem"> |
191 <div class="elem">${select('*/text()')}</div> | 191 <div class="elem">${select('*/text()')}</div> |
192 </elem> | 192 </elem> |
193 <elem>Hey Joe</elem> | 193 <elem>Hey Joe</elem> |
194 </doc>""") | 194 </doc>""") |
201 def test_recursive_match_1(self): | 201 def test_recursive_match_1(self): |
202 """ | 202 """ |
203 Match directives are applied recursively, meaning that they are also | 203 Match directives are applied recursively, meaning that they are also |
204 applied to any content they may have produced themselves: | 204 applied to any content they may have produced themselves: |
205 """ | 205 """ |
206 tmpl = Template("""<doc xmlns:py="http://purl.org/kid/ns#"> | 206 tmpl = Template("""<doc xmlns:py="http://markup.edgewall.org/"> |
207 <elem py:match="elem"> | 207 <elem py:match="elem"> |
208 <div class="elem"> | 208 <div class="elem"> |
209 ${select('*/*')} | 209 ${select('*/*')} |
210 </div> | 210 </div> |
211 </elem> | 211 </elem> |
232 """ | 232 """ |
233 When two or more match templates match the same element and also | 233 When two or more match templates match the same element and also |
234 themselves output the element they match, avoiding recursion is even | 234 themselves output the element they match, avoiding recursion is even |
235 more complex, but should work. | 235 more complex, but should work. |
236 """ | 236 """ |
237 tmpl = Template("""<html xmlns:py="http://purl.org/kid/ns#"> | 237 tmpl = Template("""<html xmlns:py="http://markup.edgewall.org/"> |
238 <body py:match="body"> | 238 <body py:match="body"> |
239 <div id="header"/> | 239 <div id="header"/> |
240 ${select('*/*')} | 240 ${select('*/*')} |
241 </body> | 241 </body> |
242 <body py:match="body"> | 242 <body py:match="body"> |
257 | 257 |
258 class StripDirectiveTestCase(unittest.TestCase): | 258 class StripDirectiveTestCase(unittest.TestCase): |
259 """Tests for the `py:strip` template directive.""" | 259 """Tests for the `py:strip` template directive.""" |
260 | 260 |
261 def test_strip_false(self): | 261 def test_strip_false(self): |
262 tmpl = Template("""<div xmlns:py="http://purl.org/kid/ns#"> | 262 tmpl = Template("""<div xmlns:py="http://markup.edgewall.org/"> |
263 <div py:strip="False"><b>foo</b></div> | 263 <div py:strip="False"><b>foo</b></div> |
264 </div>""") | 264 </div>""") |
265 self.assertEqual("""<div> | 265 self.assertEqual("""<div> |
266 <div><b>foo</b></div> | 266 <div><b>foo</b></div> |
267 </div>""", str(tmpl.generate())) | 267 </div>""", str(tmpl.generate())) |
268 | 268 |
269 def test_strip_empty(self): | 269 def test_strip_empty(self): |
270 tmpl = Template("""<div xmlns:py="http://purl.org/kid/ns#"> | 270 tmpl = Template("""<div xmlns:py="http://markup.edgewall.org/"> |
271 <div py:strip=""><b>foo</b></div> | 271 <div py:strip=""><b>foo</b></div> |
272 </div>""") | 272 </div>""") |
273 self.assertEqual("""<div> | 273 self.assertEqual("""<div> |
274 <b>foo</b> | 274 <b>foo</b> |
275 </div>""", str(tmpl.generate())) | 275 </div>""", str(tmpl.generate())) |
328 ctxt = Context() | 328 ctxt = Context() |
329 tmpl = Template('<root attr="${1}"/>') | 329 tmpl = Template('<root attr="${1}"/>') |
330 self.assertEqual('<root attr="1"/>', str(tmpl.generate(ctxt))) | 330 self.assertEqual('<root attr="1"/>', str(tmpl.generate(ctxt))) |
331 | 331 |
332 def test_bad_directive_error(self): | 332 def test_bad_directive_error(self): |
333 xml = '<p xmlns:py="http://purl.org/kid/ns#" py:do="nothing" />' | 333 xml = '<p xmlns:py="http://markup.edgewall.org/" py:do="nothing" />' |
334 try: | 334 try: |
335 tmpl = Template(xml, filename='test.html') | 335 tmpl = Template(xml, filename='test.html') |
336 except BadDirectiveError, e: | 336 except BadDirectiveError, e: |
337 self.assertEqual('test.html', e.filename) | 337 self.assertEqual('test.html', e.filename) |
338 if sys.version_info[:2] >= (2, 4): | 338 if sys.version_info[:2] >= (2, 4): |
339 self.assertEqual(1, e.lineno) | 339 self.assertEqual(1, e.lineno) |
340 | 340 |
341 def test_directive_value_syntax_error(self): | 341 def test_directive_value_syntax_error(self): |
342 xml = '<p xmlns:py="http://purl.org/kid/ns#" py:if="bar\'" />' | 342 xml = '<p xmlns:py="http://markup.edgewall.org/" py:if="bar\'" />' |
343 tmpl = Template(xml, filename='test.html') | 343 tmpl = Template(xml, filename='test.html') |
344 try: | 344 try: |
345 list(tmpl.generate(Context())) | 345 list(tmpl.generate(Context())) |
346 self.fail('Expected SyntaxError') | 346 self.fail('Expected SyntaxError') |
347 except TemplateSyntaxError, e: | 347 except TemplateSyntaxError, e: |
366 def test_markup_noescape(self): | 366 def test_markup_noescape(self): |
367 """ | 367 """ |
368 Verify that outputting context data that is a `Markup` instance is not | 368 Verify that outputting context data that is a `Markup` instance is not |
369 escaped. | 369 escaped. |
370 """ | 370 """ |
371 tmpl = Template("""<div xmlns:py="http://purl.org/kid/ns#"> | 371 tmpl = Template("""<div xmlns:py="http://markup.edgewall.org/"> |
372 $myvar | 372 $myvar |
373 </div>""") | 373 </div>""") |
374 self.assertEqual("""<div> | 374 self.assertEqual("""<div> |
375 <b>foo</b> | 375 <b>foo</b> |
376 </div>""", str(tmpl.generate(Context(myvar=Markup('<b>foo</b>'))))) | 376 </div>""", str(tmpl.generate(Context(myvar=Markup('<b>foo</b>'))))) |
377 | 377 |
378 def test_text_noescape_quotes(self): | 378 def test_text_noescape_quotes(self): |
379 """ | 379 """ |
380 Verify that outputting context data in text nodes doesn't escape quotes. | 380 Verify that outputting context data in text nodes doesn't escape quotes. |
381 """ | 381 """ |
382 tmpl = Template("""<div xmlns:py="http://purl.org/kid/ns#"> | 382 tmpl = Template("""<div xmlns:py="http://markup.edgewall.org/"> |
383 $myvar | 383 $myvar |
384 </div>""") | 384 </div>""") |
385 self.assertEqual("""<div> | 385 self.assertEqual("""<div> |
386 "foo" | 386 "foo" |
387 </div>""", str(tmpl.generate(Context(myvar='"foo"')))) | 387 </div>""", str(tmpl.generate(Context(myvar='"foo"')))) |
388 | 388 |
389 def test_attr_escape_quotes(self): | 389 def test_attr_escape_quotes(self): |
390 """ | 390 """ |
391 Verify that outputting context data in attribtes escapes quotes. | 391 Verify that outputting context data in attribtes escapes quotes. |
392 """ | 392 """ |
393 tmpl = Template("""<div xmlns:py="http://purl.org/kid/ns#"> | 393 tmpl = Template("""<div xmlns:py="http://markup.edgewall.org/"> |
394 <elem class="$myvar"/> | 394 <elem class="$myvar"/> |
395 </div>""") | 395 </div>""") |
396 self.assertEqual("""<div> | 396 self.assertEqual("""<div> |
397 <elem class=""foo""/> | 397 <elem class=""foo""/> |
398 </div>""", str(tmpl.generate(Context(myvar='"foo"')))) | 398 </div>""", str(tmpl.generate(Context(myvar='"foo"')))) |