annotate markup/tests/template.py @ 37:37557b8fb925 trunk

Moved some of the tests for the strip directive to a new unittest test case to not clutter up the documentation.
author cmlenz
date Sun, 02 Jul 2006 23:10:27 +0000
parents ed370ebfa794
children a5d585dd38c4
rev   line source
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
1 # -*- coding: utf-8 -*-
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
2 #
27
b4f78c05e5c9 * Fix the boilerplate in the Python source files.
cmlenz
parents: 21
diff changeset
3 # Copyright (C) 2006 Christopher Lenz
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
4 # All rights reserved.
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
5 #
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
6 # This software is licensed as described in the file COPYING, which
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
7 # you should have received as part of this distribution. The terms
27
b4f78c05e5c9 * 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
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
9 #
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
10 # This software consists of voluntary contributions made by many
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
11 # individuals. For the exact contribution history, see the revision
27
b4f78c05e5c9 * 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
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
13
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
14 import doctest
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
15 import unittest
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
16 import sys
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
17
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
18 from markup.core import Stream
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
19 from markup.template import BadDirectiveError, Context, Template, \
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
20 TemplateSyntaxError
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
21
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
22
36
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
23 class MatchDirectiveTestCase(unittest.TestCase):
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
24 """Tests for the `py:match` template directive."""
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
25
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
26 def test_without_strip(self):
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
27 """
ed370ebfa794 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
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
29 it matched without entering an infinite recursion.
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
30 """
37
37557b8fb925 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
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
32 <elem py:match="elem">
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
33 <div class="elem">${select('*/text()')}</div>
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
34 </elem>
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
35 <elem>Hey Joe</elem>
37
37557b8fb925 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
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
37 self.assertEqual("""<doc>
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
38 <elem>
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
39 <div class="elem">Hey Joe</div>
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
40 </elem>
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
41 </doc>""", str(tmpl.generate()))
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
42
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
43 def test_recursive_match_1(self):
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
44 """
ed370ebfa794 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
ed370ebfa794 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:
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
47 """
37
37557b8fb925 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
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
49 <elem py:match="elem">
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
50 <div class="elem">
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
51 ${select('*/*')}
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
52 </div>
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
53 </elem>
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
54 <elem>
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
55 <subelem>
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
56 <elem/>
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
57 </subelem>
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
58 </elem>
37
37557b8fb925 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
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
60 self.assertEqual("""<doc>
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
61 <elem>
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
62 <div class="elem">
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
63 <subelem>
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
64 <elem>
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
65 <div class="elem">
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
66 </div>
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
67 </elem>
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
68 </subelem>
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
69 </div>
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
70 </elem>
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
71 </doc>""", str(tmpl.generate()))
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
72
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
73 def test_recursive_match_2(self):
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
74 """
ed370ebfa794 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
ed370ebfa794 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
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
77 more complex, but should work.
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
78 """
37
37557b8fb925 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
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
80 <body py:match="body">
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
81 <div id="header"/>
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
82 ${select('*/*')}
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
83 </body>
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
84 <body py:match="body">
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
85 ${select('*/*')}
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
86 <div id="footer"/>
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
87 </body>
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
88 <body>
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
89 <h1>Foo</h1>
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
90 </body>
37
37557b8fb925 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
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
92 self.assertEqual("""<html>
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
93 <body>
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
94 <div id="header"/><h1>Foo</h1>
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
95 <div id="footer"/>
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
96 </body>
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
97 </html>""", str(tmpl.generate()))
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
98
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
99
37
37557b8fb925 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):
37557b8fb925 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."""
37557b8fb925 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
37557b8fb925 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):
37557b8fb925 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#">
37557b8fb925 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>
37557b8fb925 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>""")
37557b8fb925 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>
37557b8fb925 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>
37557b8fb925 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()))
37557b8fb925 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
37557b8fb925 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):
37557b8fb925 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#">
37557b8fb925 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>
37557b8fb925 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>""")
37557b8fb925 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>
37557b8fb925 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>
37557b8fb925 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()))
37557b8fb925 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
37557b8fb925 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
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
120 class TemplateTestCase(unittest.TestCase):
36
ed370ebfa794 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
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
122 reporting.
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
123 """
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
124
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
125 def test_interpolate_string(self):
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
126 parts = list(Template._interpolate('bla'))
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
127 self.assertEqual(1, len(parts))
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
128 self.assertEqual(Stream.TEXT, parts[0][0])
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
129 self.assertEqual('bla', parts[0][1])
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
130
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
131 def test_interpolate_simple(self):
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
132 parts = list(Template._interpolate('${bla}'))
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
133 self.assertEqual(1, len(parts))
10
f77f7a91aa46 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
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
135 self.assertEqual('bla', parts[0][1].source)
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
136
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
137 def test_interpolate_escaped(self):
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
138 parts = list(Template._interpolate('$${bla}'))
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
139 self.assertEqual(1, len(parts))
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
140 self.assertEqual(Stream.TEXT, parts[0][0])
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
141 self.assertEqual('${bla}', parts[0][1])
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
142
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
143 def test_interpolate_short(self):
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
144 parts = list(Template._interpolate('$bla'))
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
145 self.assertEqual(1, len(parts))
10
f77f7a91aa46 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
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
147 self.assertEqual('bla', parts[0][1].source)
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
148
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
149 def test_interpolate_mixed1(self):
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
150 parts = list(Template._interpolate('$foo bar $baz'))
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
151 self.assertEqual(3, len(parts))
10
f77f7a91aa46 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
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
153 self.assertEqual('foo', parts[0][1].source)
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
154 self.assertEqual(Stream.TEXT, parts[1][0])
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
155 self.assertEqual(' bar ', parts[1][1])
10
f77f7a91aa46 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
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
157 self.assertEqual('baz', parts[2][1].source)
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
158
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
159 def test_interpolate_mixed2(self):
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
160 parts = list(Template._interpolate('foo $bar baz'))
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
161 self.assertEqual(3, len(parts))
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
162 self.assertEqual(Stream.TEXT, parts[0][0])
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
163 self.assertEqual('foo ', parts[0][1])
10
f77f7a91aa46 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
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
165 self.assertEqual('bar', parts[1][1].source)
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
166 self.assertEqual(Stream.TEXT, parts[2][0])
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
167 self.assertEqual(' baz', parts[2][1])
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
168
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
169 def test_bad_directive_error(self):
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
170 xml = '<p xmlns:py="http://purl.org/kid/ns#" py:do="nothing" />'
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
171 try:
21
b4d17897d053 * Include paths are now interpreted relative to the path of the including template. Closes #3.
cmlenz
parents: 10
diff changeset
172 tmpl = Template(xml, filename='test.html')
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
173 except BadDirectiveError, e:
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
174 self.assertEqual('test.html', e.filename)
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
175 if sys.version_info[:2] >= (2, 4):
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
176 self.assertEqual(1, e.lineno)
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
177
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
178 def test_directive_value_syntax_error(self):
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
179 xml = '<p xmlns:py="http://purl.org/kid/ns#" py:if="bar\'" />'
21
b4d17897d053 * Include paths are now interpreted relative to the path of the including template. Closes #3.
cmlenz
parents: 10
diff changeset
180 tmpl = Template(xml, filename='test.html')
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
181 try:
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
182 list(tmpl.generate(Context()))
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
183 self.fail('Expected SyntaxError')
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
184 except TemplateSyntaxError, e:
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
185 self.assertEqual('test.html', e.filename)
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
186 if sys.version_info[:2] >= (2, 4):
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
187 self.assertEqual(1, e.lineno)
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
188 # We don't really care about the offset here, do we?
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
189
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
190 def test_expression_syntax_error(self):
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
191 xml = '<p>\n Foo <em>${bar"}</em>\n</p>'
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
192 tmpl = Template(xml, filename='test.html')
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
193 ctxt = Context(bar='baz')
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
194 try:
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
195 list(tmpl.generate(ctxt))
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
196 self.fail('Expected SyntaxError')
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
197 except TemplateSyntaxError, e:
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
198 self.assertEqual('test.html', e.filename)
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
199 if sys.version_info[:2] >= (2, 4):
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
200 self.assertEqual(2, e.lineno)
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
201 self.assertEqual(10, e.offset)
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
202
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
203
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
204 def suite():
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
205 suite = unittest.TestSuite()
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
206 suite.addTest(doctest.DocTestSuite(Template.__module__))
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
207 suite.addTest(unittest.makeSuite(TemplateTestCase, 'test'))
36
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
208 suite.addTest(unittest.makeSuite(MatchDirectiveTestCase, 'test'))
37
37557b8fb925 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
209 suite.addTest(unittest.makeSuite(StripDirectiveTestCase, 'test'))
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
210 return suite
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
211
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
212 if __name__ == '__main__':
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
213 unittest.main(defaultTest='suite')
Copyright (C) 2012-2017 Edgewall Software