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
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
1 # -*- coding: utf-8 -*-
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
2 #
27
b8456279c444 * Fix the boilerplate in the Python source files.
cmlenz
parents: 21
diff changeset
3 # Copyright (C) 2006 Christopher Lenz
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
4 # All rights reserved.
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
5 #
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
6 # This software is licensed as described in the file COPYING, which
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
7 # you should have received as part of this distribution. The terms
27
b8456279c444 * Fix the boilerplate in the Python source files.
cmlenz
parents: 21
diff changeset
8 # are also available at http://markup.cmlenz.net/wiki/License.
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
9 #
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
10 # This software consists of voluntary contributions made by many
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
11 # individuals. For the exact contribution history, see the revision
27
b8456279c444 * Fix the boilerplate in the Python source files.
cmlenz
parents: 21
diff changeset
12 # history and logs, available at http://markup.cmlenz.net/log/.
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
13
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
14 import doctest
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
15 import unittest
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
16 import sys
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
17
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
18 from markup.core import Stream
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
19 from markup.template import BadDirectiveError, Context, Template, \
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
20 TemplateSyntaxError
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
21
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
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
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
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
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
124
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
125 def test_interpolate_string(self):
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
126 parts = list(Template._interpolate('bla'))
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
127 self.assertEqual(1, len(parts))
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
128 self.assertEqual(Stream.TEXT, parts[0][0])
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
129 self.assertEqual('bla', parts[0][1])
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
130
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
131 def test_interpolate_simple(self):
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
132 parts = list(Template._interpolate('${bla}'))
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
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
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
135 self.assertEqual('bla', parts[0][1].source)
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
136
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
137 def test_interpolate_escaped(self):
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
138 parts = list(Template._interpolate('$${bla}'))
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
139 self.assertEqual(1, len(parts))
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
140 self.assertEqual(Stream.TEXT, parts[0][0])
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
141 self.assertEqual('${bla}', parts[0][1])
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
142
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
143 def test_interpolate_short(self):
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
144 parts = list(Template._interpolate('$bla'))
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
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
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
147 self.assertEqual('bla', parts[0][1].source)
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
148
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
149 def test_interpolate_mixed1(self):
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
150 parts = list(Template._interpolate('$foo bar $baz'))
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
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
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
153 self.assertEqual('foo', parts[0][1].source)
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
154 self.assertEqual(Stream.TEXT, parts[1][0])
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
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
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
157 self.assertEqual('baz', parts[2][1].source)
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
158
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
159 def test_interpolate_mixed2(self):
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
160 parts = list(Template._interpolate('foo $bar baz'))
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
161 self.assertEqual(3, len(parts))
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
162 self.assertEqual(Stream.TEXT, parts[0][0])
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
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
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
165 self.assertEqual('bar', parts[1][1].source)
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
166 self.assertEqual(Stream.TEXT, parts[2][0])
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
167 self.assertEqual(' baz', parts[2][1])
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
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
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
174 def test_bad_directive_error(self):
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
175 xml = '<p xmlns:py="http://purl.org/kid/ns#" py:do="nothing" />'
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
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
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
178 except BadDirectiveError, e:
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
179 self.assertEqual('test.html', e.filename)
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
180 if sys.version_info[:2] >= (2, 4):
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
181 self.assertEqual(1, e.lineno)
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
182
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
183 def test_directive_value_syntax_error(self):
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
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
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
186 try:
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
187 list(tmpl.generate(Context()))
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
188 self.fail('Expected SyntaxError')
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
189 except TemplateSyntaxError, e:
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
190 self.assertEqual('test.html', e.filename)
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
191 if sys.version_info[:2] >= (2, 4):
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
192 self.assertEqual(1, e.lineno)
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
193 # We don't really care about the offset here, do we?
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
194
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
195 def test_expression_syntax_error(self):
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
196 xml = '<p>\n Foo <em>${bar"}</em>\n</p>'
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
197 tmpl = Template(xml, filename='test.html')
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
198 ctxt = Context(bar='baz')
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
199 try:
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
200 list(tmpl.generate(ctxt))
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
201 self.fail('Expected SyntaxError')
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
202 except TemplateSyntaxError, e:
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
203 self.assertEqual('test.html', e.filename)
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
204 if sys.version_info[:2] >= (2, 4):
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
205 self.assertEqual(2, e.lineno)
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
206 self.assertEqual(10, e.offset)
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
207
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
208
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
209 def suite():
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
210 suite = unittest.TestSuite()
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
211 suite.addTest(doctest.DocTestSuite(Template.__module__))
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
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
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
215 return suite
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
216
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
217 if __name__ == '__main__':
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
218 unittest.main(defaultTest='suite')
Copyright (C) 2012-2017 Edgewall Software