Mercurial > genshi > genshi-test
annotate markup/tests/template.py @ 51:a572b1018b66
Fix `py:for` directive when combined with other directives (such as `py:strip`).
author | cmlenz |
---|---|
date | Tue, 04 Jul 2006 09:03:04 +0000 |
parents | a053ffb834cb |
children | 60f1a556690e |
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 | |
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 """ |
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
|
30 tmpl = Template("""<doc xmlns:py="http://purl.org/kid/ns#"> |
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 |
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
|
38 |
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
|
39 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
|
40 """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
|
41 |
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
|
42 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
|
43 """ |
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
|
44 Verify that the a named template function with a strip 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
|
45 actually strips of the outer element. |
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
|
46 """ |
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
|
47 tmpl = Template("""<doc xmlns:py="http://purl.org/kid/ns#"> |
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
|
48 <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
|
49 <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
|
50 </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
|
51 ${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
|
52 </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
|
53 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
|
54 <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
|
55 </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
|
56 |
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
|
57 |
51
a572b1018b66
Fix `py:for` directive when combined with other directives (such as `py:strip`).
cmlenz
parents:
50
diff
changeset
|
58 class ForDirectiveTestCase(unittest.TestCase): |
a572b1018b66
Fix `py:for` directive when combined with other directives (such as `py:strip`).
cmlenz
parents:
50
diff
changeset
|
59 """Tests for the `py:def` template directive.""" |
a572b1018b66
Fix `py:for` directive when combined with other directives (such as `py:strip`).
cmlenz
parents:
50
diff
changeset
|
60 |
a572b1018b66
Fix `py:for` directive when combined with other directives (such as `py:strip`).
cmlenz
parents:
50
diff
changeset
|
61 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
|
62 """ |
a572b1018b66
Fix `py:for` directive when combined with other directives (such as `py:strip`).
cmlenz
parents:
50
diff
changeset
|
63 Verify that the a named template function with a strip directive |
a572b1018b66
Fix `py:for` directive when combined with other directives (such as `py:strip`).
cmlenz
parents:
50
diff
changeset
|
64 actually strips of the outer element. |
a572b1018b66
Fix `py:for` directive when combined with other directives (such as `py:strip`).
cmlenz
parents:
50
diff
changeset
|
65 """ |
a572b1018b66
Fix `py:for` directive when combined with other directives (such as `py:strip`).
cmlenz
parents:
50
diff
changeset
|
66 tmpl = Template("""<doc xmlns:py="http://purl.org/kid/ns#"> |
a572b1018b66
Fix `py:for` directive when combined with other directives (such as `py:strip`).
cmlenz
parents:
50
diff
changeset
|
67 <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
|
68 <b>${item}</b> |
a572b1018b66
Fix `py:for` directive when combined with other directives (such as `py:strip`).
cmlenz
parents:
50
diff
changeset
|
69 </div> |
a572b1018b66
Fix `py:for` directive when combined with other directives (such as `py:strip`).
cmlenz
parents:
50
diff
changeset
|
70 </doc>""") |
a572b1018b66
Fix `py:for` directive when combined with other directives (such as `py:strip`).
cmlenz
parents:
50
diff
changeset
|
71 self.assertEqual("""<doc> |
a572b1018b66
Fix `py:for` directive when combined with other directives (such as `py:strip`).
cmlenz
parents:
50
diff
changeset
|
72 <b>1</b> |
a572b1018b66
Fix `py:for` directive when combined with other directives (such as `py:strip`).
cmlenz
parents:
50
diff
changeset
|
73 <b>2</b> |
a572b1018b66
Fix `py:for` directive when combined with other directives (such as `py:strip`).
cmlenz
parents:
50
diff
changeset
|
74 <b>3</b> |
a572b1018b66
Fix `py:for` directive when combined with other directives (such as `py:strip`).
cmlenz
parents:
50
diff
changeset
|
75 <b>4</b> |
a572b1018b66
Fix `py:for` directive when combined with other directives (such as `py:strip`).
cmlenz
parents:
50
diff
changeset
|
76 <b>5</b> |
a572b1018b66
Fix `py:for` directive when combined with other directives (such as `py:strip`).
cmlenz
parents:
50
diff
changeset
|
77 </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
|
78 |
a572b1018b66
Fix `py:for` directive when combined with other directives (such as `py:strip`).
cmlenz
parents:
50
diff
changeset
|
79 |
36
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
80 class MatchDirectiveTestCase(unittest.TestCase): |
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
81 """Tests for the `py:match` template directive.""" |
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
82 |
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
|
83 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
|
84 """ |
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
|
85 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
|
86 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
|
87 """ |
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
|
88 tmpl = Template("""<doc xmlns:py="http://purl.org/kid/ns#"> |
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
|
89 <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
|
90 <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
|
91 </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
|
92 <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
|
93 </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
|
94 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
|
95 <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
|
96 </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
|
97 |
36
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
98 def test_without_strip(self): |
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
99 """ |
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
100 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
|
101 it matched without entering an infinite recursion. |
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
102 """ |
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
|
103 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
|
104 <elem py:match="elem"> |
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
105 <div class="elem">${select('*/text()')}</div> |
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
106 </elem> |
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
107 <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
|
108 </doc>""") |
36
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
109 self.assertEqual("""<doc> |
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
110 <elem> |
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
111 <div class="elem">Hey Joe</div> |
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
112 </elem> |
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
113 </doc>""", str(tmpl.generate())) |
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
114 |
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
115 def test_recursive_match_1(self): |
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
116 """ |
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
117 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
|
118 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
|
119 """ |
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
|
120 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
|
121 <elem py:match="elem"> |
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
122 <div class="elem"> |
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
123 ${select('*/*')} |
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
124 </div> |
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
125 </elem> |
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
126 <elem> |
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
127 <subelem> |
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
128 <elem/> |
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
129 </subelem> |
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
130 </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
|
131 </doc>""") |
36
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
132 self.assertEqual("""<doc> |
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
133 <elem> |
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
134 <div class="elem"> |
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
135 <subelem> |
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
136 <elem> |
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
137 <div class="elem"> |
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
138 </div> |
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
139 </elem> |
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
140 </subelem> |
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
141 </div> |
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
142 </elem> |
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
143 </doc>""", str(tmpl.generate())) |
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
144 |
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
145 def test_recursive_match_2(self): |
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
146 """ |
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
147 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
|
148 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
|
149 more complex, but should work. |
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
150 """ |
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
|
151 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
|
152 <body py:match="body"> |
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
153 <div id="header"/> |
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
154 ${select('*/*')} |
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
155 </body> |
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
156 <body py:match="body"> |
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
157 ${select('*/*')} |
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
158 <div id="footer"/> |
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
159 </body> |
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
160 <body> |
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
161 <h1>Foo</h1> |
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
162 </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
|
163 </html>""") |
36
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
164 self.assertEqual("""<html> |
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
165 <body> |
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
166 <div id="header"/><h1>Foo</h1> |
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
167 <div id="footer"/> |
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
168 </body> |
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
169 </html>""", str(tmpl.generate())) |
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
170 |
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
171 |
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
|
172 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
|
173 """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
|
174 |
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
|
175 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
|
176 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
|
177 <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
|
178 </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
|
179 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
|
180 <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
|
181 </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
|
182 |
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
|
183 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
|
184 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
|
185 <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
|
186 </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
|
187 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
|
188 <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
|
189 </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
|
190 |
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
|
191 |
1 | 192 class TemplateTestCase(unittest.TestCase): |
36
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
193 """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
|
194 reporting. |
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
195 """ |
1 | 196 |
197 def test_interpolate_string(self): | |
198 parts = list(Template._interpolate('bla')) | |
199 self.assertEqual(1, len(parts)) | |
200 self.assertEqual(Stream.TEXT, parts[0][0]) | |
201 self.assertEqual('bla', parts[0][1]) | |
202 | |
203 def test_interpolate_simple(self): | |
204 parts = list(Template._interpolate('${bla}')) | |
205 self.assertEqual(1, len(parts)) | |
10
c5890ef863ba
Moved the template-specific stream event kinds into the template module.
cmlenz
parents:
1
diff
changeset
|
206 self.assertEqual(Template.EXPR, parts[0][0]) |
1 | 207 self.assertEqual('bla', parts[0][1].source) |
208 | |
209 def test_interpolate_escaped(self): | |
210 parts = list(Template._interpolate('$${bla}')) | |
211 self.assertEqual(1, len(parts)) | |
212 self.assertEqual(Stream.TEXT, parts[0][0]) | |
213 self.assertEqual('${bla}', parts[0][1]) | |
214 | |
215 def test_interpolate_short(self): | |
216 parts = list(Template._interpolate('$bla')) | |
217 self.assertEqual(1, len(parts)) | |
10
c5890ef863ba
Moved the template-specific stream event kinds into the template module.
cmlenz
parents:
1
diff
changeset
|
218 self.assertEqual(Template.EXPR, parts[0][0]) |
1 | 219 self.assertEqual('bla', parts[0][1].source) |
220 | |
221 def test_interpolate_mixed1(self): | |
222 parts = list(Template._interpolate('$foo bar $baz')) | |
223 self.assertEqual(3, len(parts)) | |
10
c5890ef863ba
Moved the template-specific stream event kinds into the template module.
cmlenz
parents:
1
diff
changeset
|
224 self.assertEqual(Template.EXPR, parts[0][0]) |
1 | 225 self.assertEqual('foo', parts[0][1].source) |
226 self.assertEqual(Stream.TEXT, parts[1][0]) | |
227 self.assertEqual(' bar ', parts[1][1]) | |
10
c5890ef863ba
Moved the template-specific stream event kinds into the template module.
cmlenz
parents:
1
diff
changeset
|
228 self.assertEqual(Template.EXPR, parts[2][0]) |
1 | 229 self.assertEqual('baz', parts[2][1].source) |
230 | |
231 def test_interpolate_mixed2(self): | |
232 parts = list(Template._interpolate('foo $bar baz')) | |
233 self.assertEqual(3, len(parts)) | |
234 self.assertEqual(Stream.TEXT, parts[0][0]) | |
235 self.assertEqual('foo ', parts[0][1]) | |
10
c5890ef863ba
Moved the template-specific stream event kinds into the template module.
cmlenz
parents:
1
diff
changeset
|
236 self.assertEqual(Template.EXPR, parts[1][0]) |
1 | 237 self.assertEqual('bar', parts[1][1].source) |
238 self.assertEqual(Stream.TEXT, parts[2][0]) | |
239 self.assertEqual(' baz', parts[2][1]) | |
240 | |
48
06c642ba2b08
convert the result of expressions in attributes to strings so that values like ints are output correctly
mgood
parents:
37
diff
changeset
|
241 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
|
242 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
|
243 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
|
244 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
|
245 |
1 | 246 def test_bad_directive_error(self): |
247 xml = '<p xmlns:py="http://purl.org/kid/ns#" py:do="nothing" />' | |
248 try: | |
21
eca77129518a
* Include paths are now interpreted relative to the path of the including template. Closes #3.
cmlenz
parents:
10
diff
changeset
|
249 tmpl = Template(xml, filename='test.html') |
1 | 250 except BadDirectiveError, e: |
251 self.assertEqual('test.html', e.filename) | |
252 if sys.version_info[:2] >= (2, 4): | |
253 self.assertEqual(1, e.lineno) | |
254 | |
255 def test_directive_value_syntax_error(self): | |
256 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
|
257 tmpl = Template(xml, filename='test.html') |
1 | 258 try: |
259 list(tmpl.generate(Context())) | |
260 self.fail('Expected SyntaxError') | |
261 except TemplateSyntaxError, e: | |
262 self.assertEqual('test.html', e.filename) | |
263 if sys.version_info[:2] >= (2, 4): | |
264 self.assertEqual(1, e.lineno) | |
265 # We don't really care about the offset here, do we? | |
266 | |
267 def test_expression_syntax_error(self): | |
268 xml = '<p>\n Foo <em>${bar"}</em>\n</p>' | |
269 tmpl = Template(xml, filename='test.html') | |
270 ctxt = Context(bar='baz') | |
271 try: | |
272 list(tmpl.generate(ctxt)) | |
273 self.fail('Expected SyntaxError') | |
274 except TemplateSyntaxError, e: | |
275 self.assertEqual('test.html', e.filename) | |
276 if sys.version_info[:2] >= (2, 4): | |
277 self.assertEqual(2, e.lineno) | |
278 self.assertEqual(10, e.offset) | |
279 | |
280 | |
281 def suite(): | |
282 suite = unittest.TestSuite() | |
283 suite.addTest(doctest.DocTestSuite(Template.__module__)) | |
284 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
|
285 suite.addTest(unittest.makeSuite(AttrsDirectiveTestCase, 'test')) |
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
|
286 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
|
287 suite.addTest(unittest.makeSuite(ForDirectiveTestCase, 'test')) |
36
57d607f25484
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
288 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
|
289 suite.addTest(unittest.makeSuite(StripDirectiveTestCase, 'test')) |
1 | 290 return suite |
291 | |
292 if __name__ == '__main__': | |
293 unittest.main(defaultTest='suite') |