Mercurial > genshi > genshi-test
annotate markup/tests/template.py @ 68:a1f93d095759
Add GIF variant of the logo, and better compression on the PNG.
author | cmlenz |
---|---|
date | Mon, 10 Jul 2006 09:17:41 +0000 |
parents | 822089ae65ce |
children | 3c271699c398 |
rev | line source |
---|---|
1 | 1 # -*- coding: utf-8 -*- |
2 # | |
66
822089ae65ce
Switch copyright to Edgewall and URLs to markup.edgewall.org.
cmlenz
parents:
65
diff
changeset
|
3 # Copyright (C) 2006 Edgewall Software |
1 | 4 # All rights reserved. |
5 # | |
6 # This software is licensed as described in the file COPYING, which | |
7 # you should have received as part of this distribution. The terms | |
66
822089ae65ce
Switch copyright to Edgewall and URLs to markup.edgewall.org.
cmlenz
parents:
65
diff
changeset
|
8 # are also available at http://markup.edgewall.org/wiki/License. |
1 | 9 # |
10 # This software consists of voluntary contributions made by many | |
11 # individuals. For the exact contribution history, see the revision | |
66
822089ae65ce
Switch copyright to Edgewall and URLs to markup.edgewall.org.
cmlenz
parents:
65
diff
changeset
|
12 # history and logs, available at http://markup.edgewall.org/log/. |
1 | 13 |
14 import doctest | |
15 import unittest | |
16 import sys | |
17 | |
54 | 18 from markup.core import Markup, Stream |
1 | 19 from markup.template import BadDirectiveError, Context, Template, \ |
20 TemplateSyntaxError | |
21 | |
22 | |
50
a053ffb834cb
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.
cmlenz
parents:
48
diff
changeset
|
23 class AttrsDirectiveTestCase(unittest.TestCase): |
a053ffb834cb
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.
cmlenz
parents:
48
diff
changeset
|
24 """Tests for the `py:attrs` template directive.""" |
a053ffb834cb
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.
cmlenz
parents:
48
diff
changeset
|
25 |
a053ffb834cb
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.
cmlenz
parents:
48
diff
changeset
|
26 def test_combined_with_loop(self): |
a053ffb834cb
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.
cmlenz
parents:
48
diff
changeset
|
27 """ |
a053ffb834cb
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.
cmlenz
parents:
48
diff
changeset
|
28 Verify that the directive has access to the loop variables. |
a053ffb834cb
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.
cmlenz
parents:
48
diff
changeset
|
29 """ |
61 | 30 tmpl = Template("""<doc xmlns:py="http://markup.edgewall.org/"> |
50
a053ffb834cb
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.
cmlenz
parents:
48
diff
changeset
|
31 <elem py:for="item in items" py:attrs="item"/> |
a053ffb834cb
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.
cmlenz
parents:
48
diff
changeset
|
32 </doc>""") |
a053ffb834cb
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.
cmlenz
parents:
48
diff
changeset
|
33 items = [{'id': 1, 'class': 'foo'}, {'id': 2, 'class': 'bar'}] |
a053ffb834cb
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.
cmlenz
parents:
48
diff
changeset
|
34 self.assertEqual("""<doc> |
a053ffb834cb
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.
cmlenz
parents:
48
diff
changeset
|
35 <elem id="1" class="foo"/><elem id="2" class="bar"/> |
a053ffb834cb
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.
cmlenz
parents:
48
diff
changeset
|
36 </doc>""", str(tmpl.generate(Context(items=items)))) |
a053ffb834cb
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.
cmlenz
parents:
48
diff
changeset
|
37 |
54 | 38 def test_update_existing_attr(self): |
39 """ | |
40 Verify that an attribute value that evaluates to `None` removes an | |
41 existing attribute of that name. | |
42 """ | |
61 | 43 tmpl = Template("""<doc xmlns:py="http://markup.edgewall.org/"> |
54 | 44 <elem class="foo" py:attrs="{'class': 'bar'}"/> |
45 </doc>""") | |
46 self.assertEqual("""<doc> | |
47 <elem class="bar"/> | |
48 </doc>""", str(tmpl.generate())) | |
49 | |
50 def test_remove_existing_attr(self): | |
51 """ | |
52 Verify that an attribute value that evaluates to `None` removes an | |
53 existing attribute of that name. | |
54 """ | |
61 | 55 tmpl = Template("""<doc xmlns:py="http://markup.edgewall.org/"> |
54 | 56 <elem class="foo" py:attrs="{'class': None}"/> |
57 </doc>""") | |
58 self.assertEqual("""<doc> | |
59 <elem/> | |
60 </doc>""", str(tmpl.generate())) | |
61 | |
50
a053ffb834cb
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.
cmlenz
parents:
48
diff
changeset
|
62 |
53
60f1a556690e
* Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents:
51
diff
changeset
|
63 class ChooseDirectiveTestCase(unittest.TestCase): |
60f1a556690e
* Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents:
51
diff
changeset
|
64 """Tests for the `py:choose` template directive and the complementary |
60f1a556690e
* Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents:
51
diff
changeset
|
65 directives `py:when` and `py:otherwise`.""" |
60f1a556690e
* Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents:
51
diff
changeset
|
66 |
60f1a556690e
* Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents:
51
diff
changeset
|
67 def test_multiple_true_whens(self): |
60f1a556690e
* Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents:
51
diff
changeset
|
68 """ |
60f1a556690e
* Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents:
51
diff
changeset
|
69 Verify that, if multiple `py:when` bodies match, only the first is |
60f1a556690e
* Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents:
51
diff
changeset
|
70 output. |
60f1a556690e
* Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents:
51
diff
changeset
|
71 """ |
61 | 72 tmpl = Template("""<div xmlns:py="http://markup.edgewall.org/" py:choose=""> |
53
60f1a556690e
* Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents:
51
diff
changeset
|
73 <span py:when="1 == 1">1</span> |
60f1a556690e
* Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents:
51
diff
changeset
|
74 <span py:when="2 == 2">2</span> |
60f1a556690e
* Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents:
51
diff
changeset
|
75 <span py:when="3 == 3">3</span> |
60f1a556690e
* Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents:
51
diff
changeset
|
76 </div>""") |
60f1a556690e
* Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents:
51
diff
changeset
|
77 self.assertEqual("""<div> |
60f1a556690e
* Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents:
51
diff
changeset
|
78 <span>1</span> |
60f1a556690e
* Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents:
51
diff
changeset
|
79 </div>""", str(tmpl.generate())) |
60f1a556690e
* Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents:
51
diff
changeset
|
80 |
60f1a556690e
* Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents:
51
diff
changeset
|
81 def test_otherwise(self): |
61 | 82 tmpl = Template("""<div xmlns:py="http://markup.edgewall.org/" py:choose=""> |
53
60f1a556690e
* Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents:
51
diff
changeset
|
83 <span py:when="False">hidden</span> |
60f1a556690e
* Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents:
51
diff
changeset
|
84 <span py:otherwise="">hello</span> |
60f1a556690e
* Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents:
51
diff
changeset
|
85 </div>""") |
60f1a556690e
* Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents:
51
diff
changeset
|
86 self.assertEqual("""<div> |
60f1a556690e
* Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents:
51
diff
changeset
|
87 <span>hello</span> |
60f1a556690e
* Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents:
51
diff
changeset
|
88 </div>""", str(tmpl.generate())) |
60f1a556690e
* Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents:
51
diff
changeset
|
89 |
60f1a556690e
* Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents:
51
diff
changeset
|
90 def test_nesting(self): |
60f1a556690e
* Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents:
51
diff
changeset
|
91 """ |
60f1a556690e
* Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents:
51
diff
changeset
|
92 Verify that `py:choose` blocks can be nested: |
60f1a556690e
* Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents:
51
diff
changeset
|
93 """ |
61 | 94 tmpl = Template("""<doc xmlns:py="http://markup.edgewall.org/"> |
53
60f1a556690e
* Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents:
51
diff
changeset
|
95 <div py:choose="1"> |
60f1a556690e
* Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents:
51
diff
changeset
|
96 <div py:when="1" py:choose="3"> |
60f1a556690e
* Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents:
51
diff
changeset
|
97 <span py:when="2">2</span> |
60f1a556690e
* Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents:
51
diff
changeset
|
98 <span py:when="3">3</span> |
60f1a556690e
* Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents:
51
diff
changeset
|
99 </div> |
60f1a556690e
* Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents:
51
diff
changeset
|
100 </div> |
60f1a556690e
* Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents:
51
diff
changeset
|
101 </doc>""") |
60f1a556690e
* Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents:
51
diff
changeset
|
102 self.assertEqual("""<doc> |
60f1a556690e
* Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents:
51
diff
changeset
|
103 <div> |
60f1a556690e
* Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents:
51
diff
changeset
|
104 <div> |
60f1a556690e
* Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents:
51
diff
changeset
|
105 <span>3</span> |
60f1a556690e
* Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents:
51
diff
changeset
|
106 </div> |
60f1a556690e
* Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents:
51
diff
changeset
|
107 </div> |
60f1a556690e
* Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents:
51
diff
changeset
|
108 </doc>""", str(tmpl.generate())) |
60f1a556690e
* Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents:
51
diff
changeset
|
109 |
60f1a556690e
* Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents:
51
diff
changeset
|
110 def test_when_with_strip(self): |
60f1a556690e
* Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents:
51
diff
changeset
|
111 """ |
60f1a556690e
* Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents:
51
diff
changeset
|
112 Verify that a when directive with a strip directive actually strips of |
60f1a556690e
* Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents:
51
diff
changeset
|
113 the outer element. |
60f1a556690e
* Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents:
51
diff
changeset
|
114 """ |
61 | 115 tmpl = Template("""<doc xmlns:py="http://markup.edgewall.org/"> |
53
60f1a556690e
* Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents:
51
diff
changeset
|
116 <div py:choose="" py:strip=""> |
60f1a556690e
* Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents:
51
diff
changeset
|
117 <span py:otherwise="">foo</span> |
60f1a556690e
* Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents:
51
diff
changeset
|
118 </div> |
60f1a556690e
* Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents:
51
diff
changeset
|
119 </doc>""") |
60f1a556690e
* Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents:
51
diff
changeset
|
120 self.assertEqual("""<doc> |
60f1a556690e
* Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents:
51
diff
changeset
|
121 <span>foo</span> |
60f1a556690e
* Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents:
51
diff
changeset
|
122 </doc>""", str(tmpl.generate())) |
60f1a556690e
* Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents:
51
diff
changeset
|
123 |
65
5c024cf58ecb
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
124 def test_as_element(self): |
5c024cf58ecb
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
125 """ |
5c024cf58ecb
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
126 Verify that the directive can also be used as an element. |
5c024cf58ecb
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
127 """ |
5c024cf58ecb
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
128 tmpl = Template("""<doc xmlns:py="http://markup.edgewall.org/"> |
5c024cf58ecb
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
129 <py:choose> |
5c024cf58ecb
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
130 <py:when test="1 == 1">1</py:when> |
5c024cf58ecb
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
131 <py:when test="2 == 2">2</py:when> |
5c024cf58ecb
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
132 <py:when test="3 == 3">3</py:when> |
5c024cf58ecb
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
133 </py:choose> |
5c024cf58ecb
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
134 </doc>""") |
5c024cf58ecb
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
135 self.assertEqual("""<doc> |
5c024cf58ecb
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
136 1 |
5c024cf58ecb
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
137 </doc>""", str(tmpl.generate())) |
5c024cf58ecb
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
138 |
53
60f1a556690e
* Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents:
51
diff
changeset
|
139 |
50
a053ffb834cb
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.
cmlenz
parents:
48
diff
changeset
|
140 class DefDirectiveTestCase(unittest.TestCase): |
a053ffb834cb
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.
cmlenz
parents:
48
diff
changeset
|
141 """Tests for the `py:def` template directive.""" |
a053ffb834cb
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.
cmlenz
parents:
48
diff
changeset
|
142 |
a053ffb834cb
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.
cmlenz
parents:
48
diff
changeset
|
143 def test_function_with_strip(self): |
a053ffb834cb
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.
cmlenz
parents:
48
diff
changeset
|
144 """ |
53
60f1a556690e
* Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents:
51
diff
changeset
|
145 Verify that a named template function with a strip directive actually |
60f1a556690e
* Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents:
51
diff
changeset
|
146 strips of the outer element. |
50
a053ffb834cb
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.
cmlenz
parents:
48
diff
changeset
|
147 """ |
61 | 148 tmpl = Template("""<doc xmlns:py="http://markup.edgewall.org/"> |
50
a053ffb834cb
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.
cmlenz
parents:
48
diff
changeset
|
149 <div py:def="echo(what)" py:strip=""> |
a053ffb834cb
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.
cmlenz
parents:
48
diff
changeset
|
150 <b>${what}</b> |
a053ffb834cb
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.
cmlenz
parents:
48
diff
changeset
|
151 </div> |
a053ffb834cb
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.
cmlenz
parents:
48
diff
changeset
|
152 ${echo('foo')} |
a053ffb834cb
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.
cmlenz
parents:
48
diff
changeset
|
153 </doc>""") |
a053ffb834cb
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.
cmlenz
parents:
48
diff
changeset
|
154 self.assertEqual("""<doc> |
a053ffb834cb
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.
cmlenz
parents:
48
diff
changeset
|
155 <b>foo</b> |
a053ffb834cb
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.
cmlenz
parents:
48
diff
changeset
|
156 </doc>""", str(tmpl.generate())) |
a053ffb834cb
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.
cmlenz
parents:
48
diff
changeset
|
157 |
65
5c024cf58ecb
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
158 def test_as_element(self): |
5c024cf58ecb
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
159 """ |
5c024cf58ecb
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
160 Verify that the directive can also be used as an element. |
5c024cf58ecb
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
161 """ |
5c024cf58ecb
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
162 tmpl = Template("""<doc xmlns:py="http://markup.edgewall.org/"> |
5c024cf58ecb
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
163 <py:def function="echo(what)"> |
5c024cf58ecb
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
164 <b>${what}</b> |
5c024cf58ecb
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
165 </py:def> |
5c024cf58ecb
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
166 ${echo('foo')} |
5c024cf58ecb
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
167 </doc>""") |
5c024cf58ecb
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
168 self.assertEqual("""<doc> |
5c024cf58ecb
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
169 <b>foo</b> |
5c024cf58ecb
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
170 </doc>""", str(tmpl.generate())) |
5c024cf58ecb
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
171 |
50
a053ffb834cb
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.
cmlenz
parents:
48
diff
changeset
|
172 |
51
a572b1018b66
Fix `py:for` directive when combined with other directives (such as `py:strip`).
cmlenz
parents:
50
diff
changeset
|
173 class ForDirectiveTestCase(unittest.TestCase): |
53
60f1a556690e
* Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents:
51
diff
changeset
|
174 """Tests for the `py:for` template directive.""" |
51
a572b1018b66
Fix `py:for` directive when combined with other directives (such as `py:strip`).
cmlenz
parents:
50
diff
changeset
|
175 |
a572b1018b66
Fix `py:for` directive when combined with other directives (such as `py:strip`).
cmlenz
parents:
50
diff
changeset
|
176 def test_loop_with_strip(self): |
a572b1018b66
Fix `py:for` directive when combined with other directives (such as `py:strip`).
cmlenz
parents:
50
diff
changeset
|
177 """ |
53
60f1a556690e
* Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents:
51
diff
changeset
|
178 Verify that the combining the `py:for` directive with `py:strip` works |
60f1a556690e
* Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents:
51
diff
changeset
|
179 correctly. |
51
a572b1018b66
Fix `py:for` directive when combined with other directives (such as `py:strip`).
cmlenz
parents:
50
diff
changeset
|
180 """ |
61 | 181 tmpl = Template("""<doc xmlns:py="http://markup.edgewall.org/"> |
51
a572b1018b66
Fix `py:for` directive when combined with other directives (such as `py:strip`).
cmlenz
parents:
50
diff
changeset
|
182 <div py:for="item in items" py:strip=""> |
a572b1018b66
Fix `py:for` directive when combined with other directives (such as `py:strip`).
cmlenz
parents:
50
diff
changeset
|
183 <b>${item}</b> |
a572b1018b66
Fix `py:for` directive when combined with other directives (such as `py:strip`).
cmlenz
parents:
50
diff
changeset
|
184 </div> |
a572b1018b66
Fix `py:for` directive when combined with other directives (such as `py:strip`).
cmlenz
parents:
50
diff
changeset
|
185 </doc>""") |
a572b1018b66
Fix `py:for` directive when combined with other directives (such as `py:strip`).
cmlenz
parents:
50
diff
changeset
|
186 self.assertEqual("""<doc> |
a572b1018b66
Fix `py:for` directive when combined with other directives (such as `py:strip`).
cmlenz
parents:
50
diff
changeset
|
187 <b>1</b> |
a572b1018b66
Fix `py:for` directive when combined with other directives (such as `py:strip`).
cmlenz
parents:
50
diff
changeset
|
188 <b>2</b> |
a572b1018b66
Fix `py:for` directive when combined with other directives (such as `py:strip`).
cmlenz
parents:
50
diff
changeset
|
189 <b>3</b> |
a572b1018b66
Fix `py:for` directive when combined with other directives (such as `py:strip`).
cmlenz
parents:
50
diff
changeset
|
190 <b>4</b> |
a572b1018b66
Fix `py:for` directive when combined with other directives (such as `py:strip`).
cmlenz
parents:
50
diff
changeset
|
191 <b>5</b> |
a572b1018b66
Fix `py:for` directive when combined with other directives (such as `py:strip`).
cmlenz
parents:
50
diff
changeset
|
192 </doc>""", str(tmpl.generate(Context(items=range(1, 6))))) |
a572b1018b66
Fix `py:for` directive when combined with other directives (such as `py:strip`).
cmlenz
parents:
50
diff
changeset
|
193 |
65
5c024cf58ecb
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
194 def test_as_element(self): |
5c024cf58ecb
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
195 """ |
5c024cf58ecb
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
196 Verify that the directive can also be used as an element. |
5c024cf58ecb
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
197 """ |
5c024cf58ecb
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
198 tmpl = Template("""<doc xmlns:py="http://markup.edgewall.org/"> |
5c024cf58ecb
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
199 <py:for each="item in items"> |
5c024cf58ecb
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
200 <b>${item}</b> |
5c024cf58ecb
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
201 </py:for> |
5c024cf58ecb
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
202 </doc>""") |
5c024cf58ecb
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
203 self.assertEqual("""<doc> |
5c024cf58ecb
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
204 <b>1</b> |
5c024cf58ecb
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
205 <b>2</b> |
5c024cf58ecb
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
206 <b>3</b> |
5c024cf58ecb
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
207 <b>4</b> |
5c024cf58ecb
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
208 <b>5</b> |
5c024cf58ecb
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
209 </doc>""", str(tmpl.generate(Context(items=range(1, 6))))) |
5c024cf58ecb
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
210 |
5c024cf58ecb
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
211 |
5c024cf58ecb
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
212 class IfDirectiveTestCase(unittest.TestCase): |
5c024cf58ecb
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
213 """Tests for the `py:if` template directive.""" |
5c024cf58ecb
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
214 |
5c024cf58ecb
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
215 def test_loop_with_strip(self): |
5c024cf58ecb
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
216 """ |
5c024cf58ecb
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
217 Verify that the combining the `py:if` directive with `py:strip` works |
5c024cf58ecb
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
218 correctly. |
5c024cf58ecb
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
219 """ |
5c024cf58ecb
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
220 tmpl = Template("""<doc xmlns:py="http://markup.edgewall.org/"> |
5c024cf58ecb
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
221 <b py:if="foo" py:strip="">${bar}</b> |
5c024cf58ecb
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
222 </doc>""") |
5c024cf58ecb
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
223 self.assertEqual("""<doc> |
5c024cf58ecb
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
224 Hello |
5c024cf58ecb
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
225 </doc>""", str(tmpl.generate(Context(foo=True, bar='Hello')))) |
5c024cf58ecb
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
226 |
5c024cf58ecb
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
227 def test_as_element(self): |
5c024cf58ecb
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
228 """ |
5c024cf58ecb
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
229 Verify that the directive can also be used as an element. |
5c024cf58ecb
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
230 """ |
5c024cf58ecb
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
231 tmpl = Template("""<doc xmlns:py="http://markup.edgewall.org/"> |
5c024cf58ecb
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
232 <py:if test="foo">${bar}</py:if> |
5c024cf58ecb
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
233 </doc>""") |
5c024cf58ecb
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
234 self.assertEqual("""<doc> |
5c024cf58ecb
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
235 Hello |
5c024cf58ecb
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
236 </doc>""", str(tmpl.generate(Context(foo=True, bar='Hello')))) |
5c024cf58ecb
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
237 |
51
a572b1018b66
Fix `py:for` directive when combined with other directives (such as `py:strip`).
cmlenz
parents:
50
diff
changeset
|
238 |
36
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
239 class MatchDirectiveTestCase(unittest.TestCase): |
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
240 """Tests for the `py:match` template directive.""" |
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
241 |
50
a053ffb834cb
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.
cmlenz
parents:
48
diff
changeset
|
242 def test_with_strip(self): |
a053ffb834cb
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.
cmlenz
parents:
48
diff
changeset
|
243 """ |
a053ffb834cb
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.
cmlenz
parents:
48
diff
changeset
|
244 Verify that a match template can produce the same kind of element that |
a053ffb834cb
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.
cmlenz
parents:
48
diff
changeset
|
245 it matched without entering an infinite recursion. |
a053ffb834cb
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.
cmlenz
parents:
48
diff
changeset
|
246 """ |
61 | 247 tmpl = Template("""<doc xmlns:py="http://markup.edgewall.org/"> |
50
a053ffb834cb
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.
cmlenz
parents:
48
diff
changeset
|
248 <elem py:match="elem" py:strip=""> |
a053ffb834cb
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.
cmlenz
parents:
48
diff
changeset
|
249 <div class="elem">${select('*/text()')}</div> |
a053ffb834cb
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.
cmlenz
parents:
48
diff
changeset
|
250 </elem> |
a053ffb834cb
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.
cmlenz
parents:
48
diff
changeset
|
251 <elem>Hey Joe</elem> |
a053ffb834cb
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.
cmlenz
parents:
48
diff
changeset
|
252 </doc>""") |
a053ffb834cb
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.
cmlenz
parents:
48
diff
changeset
|
253 self.assertEqual("""<doc> |
a053ffb834cb
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.
cmlenz
parents:
48
diff
changeset
|
254 <div class="elem">Hey Joe</div> |
a053ffb834cb
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.
cmlenz
parents:
48
diff
changeset
|
255 </doc>""", str(tmpl.generate())) |
a053ffb834cb
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.
cmlenz
parents:
48
diff
changeset
|
256 |
36
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
257 def test_without_strip(self): |
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
258 """ |
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
259 Verify that a match template can produce the same kind of element that |
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
260 it matched without entering an infinite recursion. |
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
261 """ |
61 | 262 tmpl = Template("""<doc xmlns:py="http://markup.edgewall.org/"> |
36
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
263 <elem py:match="elem"> |
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
264 <div class="elem">${select('*/text()')}</div> |
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
265 </elem> |
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
266 <elem>Hey Joe</elem> |
37
224b0b41d1da
Moved some of the tests for the strip directive to a new unittest test case to not clutter up the documentation.
cmlenz
parents:
36
diff
changeset
|
267 </doc>""") |
36
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
268 self.assertEqual("""<doc> |
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
269 <elem> |
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
270 <div class="elem">Hey Joe</div> |
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
271 </elem> |
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
272 </doc>""", str(tmpl.generate())) |
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
273 |
65
5c024cf58ecb
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
274 def test_as_element(self): |
5c024cf58ecb
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
275 """ |
5c024cf58ecb
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
276 Verify that the directive can also be used as an element. |
5c024cf58ecb
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
277 """ |
5c024cf58ecb
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
278 tmpl = Template("""<doc xmlns:py="http://markup.edgewall.org/"> |
5c024cf58ecb
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
279 <py:match path="elem"> |
5c024cf58ecb
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
280 <div class="elem">${select('*/text()')}</div> |
5c024cf58ecb
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
281 </py:match> |
5c024cf58ecb
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
282 <elem>Hey Joe</elem> |
5c024cf58ecb
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
283 </doc>""") |
5c024cf58ecb
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
284 self.assertEqual("""<doc> |
5c024cf58ecb
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
285 <div class="elem">Hey Joe</div> |
5c024cf58ecb
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
286 </doc>""", str(tmpl.generate())) |
5c024cf58ecb
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
287 |
36
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
288 def test_recursive_match_1(self): |
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
289 """ |
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
290 Match directives are applied recursively, meaning that they are also |
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
291 applied to any content they may have produced themselves: |
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
292 """ |
61 | 293 tmpl = Template("""<doc xmlns:py="http://markup.edgewall.org/"> |
36
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
294 <elem py:match="elem"> |
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
295 <div class="elem"> |
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
296 ${select('*/*')} |
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
297 </div> |
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
298 </elem> |
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
299 <elem> |
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
300 <subelem> |
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
301 <elem/> |
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
302 </subelem> |
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
303 </elem> |
37
224b0b41d1da
Moved some of the tests for the strip directive to a new unittest test case to not clutter up the documentation.
cmlenz
parents:
36
diff
changeset
|
304 </doc>""") |
36
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
305 self.assertEqual("""<doc> |
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
306 <elem> |
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
307 <div class="elem"> |
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
308 <subelem> |
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
309 <elem> |
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
310 <div class="elem"> |
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
311 </div> |
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
312 </elem> |
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
313 </subelem> |
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
314 </div> |
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
315 </elem> |
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
316 </doc>""", str(tmpl.generate())) |
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
317 |
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
318 def test_recursive_match_2(self): |
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
319 """ |
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
320 When two or more match templates match the same element and also |
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
321 themselves output the element they match, avoiding recursion is even |
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
322 more complex, but should work. |
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
323 """ |
61 | 324 tmpl = Template("""<html xmlns:py="http://markup.edgewall.org/"> |
36
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
325 <body py:match="body"> |
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
326 <div id="header"/> |
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
327 ${select('*/*')} |
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
328 </body> |
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
329 <body py:match="body"> |
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
330 ${select('*/*')} |
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
331 <div id="footer"/> |
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
332 </body> |
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
333 <body> |
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
334 <h1>Foo</h1> |
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
335 </body> |
37
224b0b41d1da
Moved some of the tests for the strip directive to a new unittest test case to not clutter up the documentation.
cmlenz
parents:
36
diff
changeset
|
336 </html>""") |
36
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
337 self.assertEqual("""<html> |
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
338 <body> |
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
339 <div id="header"/><h1>Foo</h1> |
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
340 <div id="footer"/> |
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
341 </body> |
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
342 </html>""", str(tmpl.generate())) |
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
343 |
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
344 |
37
224b0b41d1da
Moved some of the tests for the strip directive to a new unittest test case to not clutter up the documentation.
cmlenz
parents:
36
diff
changeset
|
345 class StripDirectiveTestCase(unittest.TestCase): |
224b0b41d1da
Moved some of the tests for the strip directive to a new unittest test case to not clutter up the documentation.
cmlenz
parents:
36
diff
changeset
|
346 """Tests for the `py:strip` template directive.""" |
224b0b41d1da
Moved some of the tests for the strip directive to a new unittest test case to not clutter up the documentation.
cmlenz
parents:
36
diff
changeset
|
347 |
224b0b41d1da
Moved some of the tests for the strip directive to a new unittest test case to not clutter up the documentation.
cmlenz
parents:
36
diff
changeset
|
348 def test_strip_false(self): |
61 | 349 tmpl = Template("""<div xmlns:py="http://markup.edgewall.org/"> |
37
224b0b41d1da
Moved some of the tests for the strip directive to a new unittest test case to not clutter up the documentation.
cmlenz
parents:
36
diff
changeset
|
350 <div py:strip="False"><b>foo</b></div> |
224b0b41d1da
Moved some of the tests for the strip directive to a new unittest test case to not clutter up the documentation.
cmlenz
parents:
36
diff
changeset
|
351 </div>""") |
224b0b41d1da
Moved some of the tests for the strip directive to a new unittest test case to not clutter up the documentation.
cmlenz
parents:
36
diff
changeset
|
352 self.assertEqual("""<div> |
224b0b41d1da
Moved some of the tests for the strip directive to a new unittest test case to not clutter up the documentation.
cmlenz
parents:
36
diff
changeset
|
353 <div><b>foo</b></div> |
224b0b41d1da
Moved some of the tests for the strip directive to a new unittest test case to not clutter up the documentation.
cmlenz
parents:
36
diff
changeset
|
354 </div>""", str(tmpl.generate())) |
224b0b41d1da
Moved some of the tests for the strip directive to a new unittest test case to not clutter up the documentation.
cmlenz
parents:
36
diff
changeset
|
355 |
224b0b41d1da
Moved some of the tests for the strip directive to a new unittest test case to not clutter up the documentation.
cmlenz
parents:
36
diff
changeset
|
356 def test_strip_empty(self): |
61 | 357 tmpl = Template("""<div xmlns:py="http://markup.edgewall.org/"> |
37
224b0b41d1da
Moved some of the tests for the strip directive to a new unittest test case to not clutter up the documentation.
cmlenz
parents:
36
diff
changeset
|
358 <div py:strip=""><b>foo</b></div> |
224b0b41d1da
Moved some of the tests for the strip directive to a new unittest test case to not clutter up the documentation.
cmlenz
parents:
36
diff
changeset
|
359 </div>""") |
224b0b41d1da
Moved some of the tests for the strip directive to a new unittest test case to not clutter up the documentation.
cmlenz
parents:
36
diff
changeset
|
360 self.assertEqual("""<div> |
224b0b41d1da
Moved some of the tests for the strip directive to a new unittest test case to not clutter up the documentation.
cmlenz
parents:
36
diff
changeset
|
361 <b>foo</b> |
224b0b41d1da
Moved some of the tests for the strip directive to a new unittest test case to not clutter up the documentation.
cmlenz
parents:
36
diff
changeset
|
362 </div>""", str(tmpl.generate())) |
224b0b41d1da
Moved some of the tests for the strip directive to a new unittest test case to not clutter up the documentation.
cmlenz
parents:
36
diff
changeset
|
363 |
224b0b41d1da
Moved some of the tests for the strip directive to a new unittest test case to not clutter up the documentation.
cmlenz
parents:
36
diff
changeset
|
364 |
1 | 365 class TemplateTestCase(unittest.TestCase): |
36
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
366 """Tests for basic template processing, expression evaluation and error |
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
367 reporting. |
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
368 """ |
1 | 369 |
370 def test_interpolate_string(self): | |
371 parts = list(Template._interpolate('bla')) | |
372 self.assertEqual(1, len(parts)) | |
373 self.assertEqual(Stream.TEXT, parts[0][0]) | |
374 self.assertEqual('bla', parts[0][1]) | |
375 | |
376 def test_interpolate_simple(self): | |
377 parts = list(Template._interpolate('${bla}')) | |
378 self.assertEqual(1, len(parts)) | |
10
c5890ef863ba
Moved the template-specific stream event kinds into the template module.
cmlenz
parents:
1
diff
changeset
|
379 self.assertEqual(Template.EXPR, parts[0][0]) |
1 | 380 self.assertEqual('bla', parts[0][1].source) |
381 | |
382 def test_interpolate_escaped(self): | |
383 parts = list(Template._interpolate('$${bla}')) | |
384 self.assertEqual(1, len(parts)) | |
385 self.assertEqual(Stream.TEXT, parts[0][0]) | |
386 self.assertEqual('${bla}', parts[0][1]) | |
387 | |
388 def test_interpolate_short(self): | |
389 parts = list(Template._interpolate('$bla')) | |
390 self.assertEqual(1, len(parts)) | |
10
c5890ef863ba
Moved the template-specific stream event kinds into the template module.
cmlenz
parents:
1
diff
changeset
|
391 self.assertEqual(Template.EXPR, parts[0][0]) |
1 | 392 self.assertEqual('bla', parts[0][1].source) |
393 | |
394 def test_interpolate_mixed1(self): | |
395 parts = list(Template._interpolate('$foo bar $baz')) | |
396 self.assertEqual(3, len(parts)) | |
10
c5890ef863ba
Moved the template-specific stream event kinds into the template module.
cmlenz
parents:
1
diff
changeset
|
397 self.assertEqual(Template.EXPR, parts[0][0]) |
1 | 398 self.assertEqual('foo', parts[0][1].source) |
399 self.assertEqual(Stream.TEXT, parts[1][0]) | |
400 self.assertEqual(' bar ', parts[1][1]) | |
10
c5890ef863ba
Moved the template-specific stream event kinds into the template module.
cmlenz
parents:
1
diff
changeset
|
401 self.assertEqual(Template.EXPR, parts[2][0]) |
1 | 402 self.assertEqual('baz', parts[2][1].source) |
403 | |
404 def test_interpolate_mixed2(self): | |
405 parts = list(Template._interpolate('foo $bar baz')) | |
406 self.assertEqual(3, len(parts)) | |
407 self.assertEqual(Stream.TEXT, parts[0][0]) | |
408 self.assertEqual('foo ', parts[0][1]) | |
10
c5890ef863ba
Moved the template-specific stream event kinds into the template module.
cmlenz
parents:
1
diff
changeset
|
409 self.assertEqual(Template.EXPR, parts[1][0]) |
1 | 410 self.assertEqual('bar', parts[1][1].source) |
411 self.assertEqual(Stream.TEXT, parts[2][0]) | |
412 self.assertEqual(' baz', parts[2][1]) | |
413 | |
48
06c642ba2b08
convert the result of expressions in attributes to strings so that values like ints are output correctly
mgood
parents:
37
diff
changeset
|
414 def test_interpolate_non_string_attrs(self): |
06c642ba2b08
convert the result of expressions in attributes to strings so that values like ints are output correctly
mgood
parents:
37
diff
changeset
|
415 ctxt = Context() |
06c642ba2b08
convert the result of expressions in attributes to strings so that values like ints are output correctly
mgood
parents:
37
diff
changeset
|
416 tmpl = Template('<root attr="${1}"/>') |
06c642ba2b08
convert the result of expressions in attributes to strings so that values like ints are output correctly
mgood
parents:
37
diff
changeset
|
417 self.assertEqual('<root attr="1"/>', str(tmpl.generate(ctxt))) |
06c642ba2b08
convert the result of expressions in attributes to strings so that values like ints are output correctly
mgood
parents:
37
diff
changeset
|
418 |
1 | 419 def test_bad_directive_error(self): |
61 | 420 xml = '<p xmlns:py="http://markup.edgewall.org/" py:do="nothing" />' |
1 | 421 try: |
21
eca77129518a
* Include paths are now interpreted relative to the path of the including template. Closes #3.
cmlenz
parents:
10
diff
changeset
|
422 tmpl = Template(xml, filename='test.html') |
1 | 423 except BadDirectiveError, e: |
424 self.assertEqual('test.html', e.filename) | |
425 if sys.version_info[:2] >= (2, 4): | |
426 self.assertEqual(1, e.lineno) | |
427 | |
428 def test_directive_value_syntax_error(self): | |
61 | 429 xml = '<p xmlns:py="http://markup.edgewall.org/" py:if="bar\'" />' |
21
eca77129518a
* Include paths are now interpreted relative to the path of the including template. Closes #3.
cmlenz
parents:
10
diff
changeset
|
430 tmpl = Template(xml, filename='test.html') |
1 | 431 try: |
432 list(tmpl.generate(Context())) | |
433 self.fail('Expected SyntaxError') | |
434 except TemplateSyntaxError, e: | |
435 self.assertEqual('test.html', e.filename) | |
436 if sys.version_info[:2] >= (2, 4): | |
437 self.assertEqual(1, e.lineno) | |
438 # We don't really care about the offset here, do we? | |
439 | |
440 def test_expression_syntax_error(self): | |
441 xml = '<p>\n Foo <em>${bar"}</em>\n</p>' | |
442 tmpl = Template(xml, filename='test.html') | |
443 ctxt = Context(bar='baz') | |
444 try: | |
445 list(tmpl.generate(ctxt)) | |
446 self.fail('Expected SyntaxError') | |
447 except TemplateSyntaxError, e: | |
448 self.assertEqual('test.html', e.filename) | |
449 if sys.version_info[:2] >= (2, 4): | |
450 self.assertEqual(2, e.lineno) | |
451 self.assertEqual(10, e.offset) | |
452 | |
54 | 453 def test_markup_noescape(self): |
454 """ | |
455 Verify that outputting context data that is a `Markup` instance is not | |
456 escaped. | |
457 """ | |
61 | 458 tmpl = Template("""<div xmlns:py="http://markup.edgewall.org/"> |
54 | 459 $myvar |
460 </div>""") | |
461 self.assertEqual("""<div> | |
462 <b>foo</b> | |
463 </div>""", str(tmpl.generate(Context(myvar=Markup('<b>foo</b>'))))) | |
464 | |
465 def test_text_noescape_quotes(self): | |
466 """ | |
467 Verify that outputting context data in text nodes doesn't escape quotes. | |
468 """ | |
61 | 469 tmpl = Template("""<div xmlns:py="http://markup.edgewall.org/"> |
54 | 470 $myvar |
471 </div>""") | |
472 self.assertEqual("""<div> | |
473 "foo" | |
474 </div>""", str(tmpl.generate(Context(myvar='"foo"')))) | |
475 | |
476 def test_attr_escape_quotes(self): | |
477 """ | |
478 Verify that outputting context data in attribtes escapes quotes. | |
479 """ | |
61 | 480 tmpl = Template("""<div xmlns:py="http://markup.edgewall.org/"> |
54 | 481 <elem class="$myvar"/> |
482 </div>""") | |
483 self.assertEqual("""<div> | |
484 <elem class=""foo""/> | |
485 </div>""", str(tmpl.generate(Context(myvar='"foo"')))) | |
486 | |
65
5c024cf58ecb
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
487 def test_directive_element(self): |
5c024cf58ecb
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
488 tmpl = Template("""<div xmlns:py="http://markup.edgewall.org/"> |
5c024cf58ecb
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
489 <py:if test="myvar">bar</py:if> |
5c024cf58ecb
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
490 </div>""") |
5c024cf58ecb
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
491 self.assertEqual("""<div> |
5c024cf58ecb
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
492 bar |
5c024cf58ecb
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
493 </div>""", str(tmpl.generate(Context(myvar='"foo"')))) |
5c024cf58ecb
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
494 |
1 | 495 |
496 def suite(): | |
497 suite = unittest.TestSuite() | |
498 suite.addTest(doctest.DocTestSuite(Template.__module__)) | |
499 suite.addTest(unittest.makeSuite(TemplateTestCase, 'test')) | |
50
a053ffb834cb
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.
cmlenz
parents:
48
diff
changeset
|
500 suite.addTest(unittest.makeSuite(AttrsDirectiveTestCase, 'test')) |
53
60f1a556690e
* Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents:
51
diff
changeset
|
501 suite.addTest(unittest.makeSuite(ChooseDirectiveTestCase, 'test')) |
50
a053ffb834cb
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.
cmlenz
parents:
48
diff
changeset
|
502 suite.addTest(unittest.makeSuite(DefDirectiveTestCase, 'test')) |
51
a572b1018b66
Fix `py:for` directive when combined with other directives (such as `py:strip`).
cmlenz
parents:
50
diff
changeset
|
503 suite.addTest(unittest.makeSuite(ForDirectiveTestCase, 'test')) |
65
5c024cf58ecb
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
504 suite.addTest(unittest.makeSuite(IfDirectiveTestCase, 'test')) |
36
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
505 suite.addTest(unittest.makeSuite(MatchDirectiveTestCase, 'test')) |
37
224b0b41d1da
Moved some of the tests for the strip directive to a new unittest test case to not clutter up the documentation.
cmlenz
parents:
36
diff
changeset
|
506 suite.addTest(unittest.makeSuite(StripDirectiveTestCase, 'test')) |
1 | 507 return suite |
508 | |
509 if __name__ == '__main__': | |
510 unittest.main(defaultTest='suite') |