Mercurial > genshi > genshi-test
annotate markup/tests/template.py @ 48:06c642ba2b08
convert the result of expressions in attributes to strings so that values like ints are output correctly
author | mgood |
---|---|
date | Tue, 04 Jul 2006 04:49:22 +0000 |
parents | 224b0b41d1da |
children | a053ffb834cb |
rev | line source |
---|---|
1 | 1 # -*- coding: utf-8 -*- |
2 # | |
27 | 3 # Copyright (C) 2006 Christopher Lenz |
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 | |
27 | 8 # are also available at http://markup.cmlenz.net/wiki/License. |
1 | 9 # |
10 # This software consists of voluntary contributions made by many | |
11 # individuals. For the exact contribution history, see the revision | |
27 | 12 # history and logs, available at http://markup.cmlenz.net/log/. |
1 | 13 |
14 import doctest | |
15 import unittest | |
16 import sys | |
17 | |
18 from markup.core import Stream | |
19 from markup.template import BadDirectiveError, Context, Template, \ | |
20 TemplateSyntaxError | |
21 | |
22 | |
36
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
23 class MatchDirectiveTestCase(unittest.TestCase): |
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
24 """Tests for the `py:match` template directive.""" |
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
25 |
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
26 def test_without_strip(self): |
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
27 """ |
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
28 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
|
29 it matched without entering an infinite recursion. |
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
30 """ |
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
|
31 tmpl = Template("""<doc xmlns:py="http://purl.org/kid/ns#"> |
36
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
32 <elem py:match="elem"> |
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
33 <div class="elem">${select('*/text()')}</div> |
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
34 </elem> |
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
35 <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
|
36 </doc>""") |
36
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
37 self.assertEqual("""<doc> |
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
38 <elem> |
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
39 <div class="elem">Hey Joe</div> |
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
40 </elem> |
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
41 </doc>""", str(tmpl.generate())) |
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
42 |
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
43 def test_recursive_match_1(self): |
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
44 """ |
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
45 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
|
46 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
|
47 """ |
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
|
48 tmpl = Template("""<doc xmlns:py="http://purl.org/kid/ns#"> |
36
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
49 <elem py:match="elem"> |
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
50 <div class="elem"> |
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
51 ${select('*/*')} |
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
52 </div> |
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
53 </elem> |
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
54 <elem> |
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
55 <subelem> |
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
56 <elem/> |
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
57 </subelem> |
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
58 </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
|
59 </doc>""") |
36
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
60 self.assertEqual("""<doc> |
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
61 <elem> |
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
62 <div class="elem"> |
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
63 <subelem> |
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
64 <elem> |
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
65 <div class="elem"> |
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
66 </div> |
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
67 </elem> |
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
68 </subelem> |
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
69 </div> |
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
70 </elem> |
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
71 </doc>""", str(tmpl.generate())) |
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
72 |
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
73 def test_recursive_match_2(self): |
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
74 """ |
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
75 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
|
76 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
|
77 more complex, but should work. |
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
78 """ |
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
|
79 tmpl = Template("""<html xmlns:py="http://purl.org/kid/ns#"> |
36
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
80 <body py:match="body"> |
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
81 <div id="header"/> |
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
82 ${select('*/*')} |
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
83 </body> |
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
84 <body py:match="body"> |
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
85 ${select('*/*')} |
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
86 <div id="footer"/> |
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
87 </body> |
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
88 <body> |
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
89 <h1>Foo</h1> |
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
90 </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
|
91 </html>""") |
36
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
92 self.assertEqual("""<html> |
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
93 <body> |
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
94 <div id="header"/><h1>Foo</h1> |
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
95 <div id="footer"/> |
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
96 </body> |
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
97 </html>""", str(tmpl.generate())) |
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
98 |
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
99 |
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
|
100 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
|
101 """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
|
102 |
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
|
103 def test_strip_false(self): |
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
|
104 tmpl = Template("""<div xmlns:py="http://purl.org/kid/ns#"> |
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
|
105 <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
|
106 </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
|
107 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
|
108 <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
|
109 </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
|
110 |
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
|
111 def test_strip_empty(self): |
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
|
112 tmpl = Template("""<div xmlns:py="http://purl.org/kid/ns#"> |
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
|
113 <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
|
114 </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
|
115 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
|
116 <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
|
117 </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
|
118 |
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
|
119 |
1 | 120 class TemplateTestCase(unittest.TestCase): |
36
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
121 """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
|
122 reporting. |
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
123 """ |
1 | 124 |
125 def test_interpolate_string(self): | |
126 parts = list(Template._interpolate('bla')) | |
127 self.assertEqual(1, len(parts)) | |
128 self.assertEqual(Stream.TEXT, parts[0][0]) | |
129 self.assertEqual('bla', parts[0][1]) | |
130 | |
131 def test_interpolate_simple(self): | |
132 parts = list(Template._interpolate('${bla}')) | |
133 self.assertEqual(1, len(parts)) | |
10
c5890ef863ba
Moved the template-specific stream event kinds into the template module.
cmlenz
parents:
1
diff
changeset
|
134 self.assertEqual(Template.EXPR, parts[0][0]) |
1 | 135 self.assertEqual('bla', parts[0][1].source) |
136 | |
137 def test_interpolate_escaped(self): | |
138 parts = list(Template._interpolate('$${bla}')) | |
139 self.assertEqual(1, len(parts)) | |
140 self.assertEqual(Stream.TEXT, parts[0][0]) | |
141 self.assertEqual('${bla}', parts[0][1]) | |
142 | |
143 def test_interpolate_short(self): | |
144 parts = list(Template._interpolate('$bla')) | |
145 self.assertEqual(1, len(parts)) | |
10
c5890ef863ba
Moved the template-specific stream event kinds into the template module.
cmlenz
parents:
1
diff
changeset
|
146 self.assertEqual(Template.EXPR, parts[0][0]) |
1 | 147 self.assertEqual('bla', parts[0][1].source) |
148 | |
149 def test_interpolate_mixed1(self): | |
150 parts = list(Template._interpolate('$foo bar $baz')) | |
151 self.assertEqual(3, len(parts)) | |
10
c5890ef863ba
Moved the template-specific stream event kinds into the template module.
cmlenz
parents:
1
diff
changeset
|
152 self.assertEqual(Template.EXPR, parts[0][0]) |
1 | 153 self.assertEqual('foo', parts[0][1].source) |
154 self.assertEqual(Stream.TEXT, parts[1][0]) | |
155 self.assertEqual(' bar ', parts[1][1]) | |
10
c5890ef863ba
Moved the template-specific stream event kinds into the template module.
cmlenz
parents:
1
diff
changeset
|
156 self.assertEqual(Template.EXPR, parts[2][0]) |
1 | 157 self.assertEqual('baz', parts[2][1].source) |
158 | |
159 def test_interpolate_mixed2(self): | |
160 parts = list(Template._interpolate('foo $bar baz')) | |
161 self.assertEqual(3, len(parts)) | |
162 self.assertEqual(Stream.TEXT, parts[0][0]) | |
163 self.assertEqual('foo ', parts[0][1]) | |
10
c5890ef863ba
Moved the template-specific stream event kinds into the template module.
cmlenz
parents:
1
diff
changeset
|
164 self.assertEqual(Template.EXPR, parts[1][0]) |
1 | 165 self.assertEqual('bar', parts[1][1].source) |
166 self.assertEqual(Stream.TEXT, parts[2][0]) | |
167 self.assertEqual(' baz', parts[2][1]) | |
168 | |
48
06c642ba2b08
convert the result of expressions in attributes to strings so that values like ints are output correctly
mgood
parents:
37
diff
changeset
|
169 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
|
170 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
|
171 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
|
172 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
|
173 |
1 | 174 def test_bad_directive_error(self): |
175 xml = '<p xmlns:py="http://purl.org/kid/ns#" py:do="nothing" />' | |
176 try: | |
21
eca77129518a
* Include paths are now interpreted relative to the path of the including template. Closes #3.
cmlenz
parents:
10
diff
changeset
|
177 tmpl = Template(xml, filename='test.html') |
1 | 178 except BadDirectiveError, e: |
179 self.assertEqual('test.html', e.filename) | |
180 if sys.version_info[:2] >= (2, 4): | |
181 self.assertEqual(1, e.lineno) | |
182 | |
183 def test_directive_value_syntax_error(self): | |
184 xml = '<p xmlns:py="http://purl.org/kid/ns#" 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
|
185 tmpl = Template(xml, filename='test.html') |
1 | 186 try: |
187 list(tmpl.generate(Context())) | |
188 self.fail('Expected SyntaxError') | |
189 except TemplateSyntaxError, e: | |
190 self.assertEqual('test.html', e.filename) | |
191 if sys.version_info[:2] >= (2, 4): | |
192 self.assertEqual(1, e.lineno) | |
193 # We don't really care about the offset here, do we? | |
194 | |
195 def test_expression_syntax_error(self): | |
196 xml = '<p>\n Foo <em>${bar"}</em>\n</p>' | |
197 tmpl = Template(xml, filename='test.html') | |
198 ctxt = Context(bar='baz') | |
199 try: | |
200 list(tmpl.generate(ctxt)) | |
201 self.fail('Expected SyntaxError') | |
202 except TemplateSyntaxError, e: | |
203 self.assertEqual('test.html', e.filename) | |
204 if sys.version_info[:2] >= (2, 4): | |
205 self.assertEqual(2, e.lineno) | |
206 self.assertEqual(10, e.offset) | |
207 | |
208 | |
209 def suite(): | |
210 suite = unittest.TestSuite() | |
211 suite.addTest(doctest.DocTestSuite(Template.__module__)) | |
212 suite.addTest(unittest.makeSuite(TemplateTestCase, 'test')) | |
36
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
213 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
|
214 suite.addTest(unittest.makeSuite(StripDirectiveTestCase, 'test')) |
1 | 215 return suite |
216 | |
217 if __name__ == '__main__': | |
218 unittest.main(defaultTest='suite') |