Mercurial > genshi > mirror
annotate markup/tests/template.py @ 165:54a4be707664 trunk
Fix handling of keyword arguments in `py:def` directive. Thanks to Christian Boos for reporting the problem and providing the basic patch for this change.
author | cmlenz |
---|---|
date | Thu, 17 Aug 2006 14:02:58 +0000 |
parents | 3f23fafeef99 |
children | d43f50402cf2 |
rev | line source |
---|---|
1 | 1 # -*- coding: utf-8 -*- |
2 # | |
66
59eb24184e9c
Switch copyright to Edgewall and URLs to markup.edgewall.org.
cmlenz
parents:
65
diff
changeset
|
3 # Copyright (C) 2006 Edgewall Software |
1 | 4 # All rights reserved. |
5 # | |
6 # This software is licensed as described in the file COPYING, which | |
7 # you should have received as part of this distribution. The terms | |
66
59eb24184e9c
Switch copyright to Edgewall and URLs to markup.edgewall.org.
cmlenz
parents:
65
diff
changeset
|
8 # are also available at http://markup.edgewall.org/wiki/License. |
1 | 9 # |
10 # This software consists of voluntary contributions made by many | |
11 # individuals. For the exact contribution history, see the revision | |
66
59eb24184e9c
Switch copyright to Edgewall and URLs to markup.edgewall.org.
cmlenz
parents:
65
diff
changeset
|
12 # history and logs, available at http://markup.edgewall.org/log/. |
1 | 13 |
14 import doctest | |
152
cdb2a1f930e2
Add some tests for relative template includes (see #27).
cmlenz
parents:
149
diff
changeset
|
15 import os |
1 | 16 import unittest |
152
cdb2a1f930e2
Add some tests for relative template includes (see #27).
cmlenz
parents:
149
diff
changeset
|
17 import shutil |
1 | 18 import sys |
152
cdb2a1f930e2
Add some tests for relative template includes (see #27).
cmlenz
parents:
149
diff
changeset
|
19 import tempfile |
1 | 20 |
54 | 21 from markup.core import Markup, Stream |
152
cdb2a1f930e2
Add some tests for relative template includes (see #27).
cmlenz
parents:
149
diff
changeset
|
22 from markup.template import BadDirectiveError, Template, TemplateLoader, \ |
cdb2a1f930e2
Add some tests for relative template includes (see #27).
cmlenz
parents:
149
diff
changeset
|
23 TemplateSyntaxError |
1 | 24 |
25 | |
50
d3842cd76e92
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 class AttrsDirectiveTestCase(unittest.TestCase): |
d3842cd76e92
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 """Tests for the `py:attrs` template directive.""" |
d3842cd76e92
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 |
d3842cd76e92
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 def test_combined_with_loop(self): |
d3842cd76e92
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 """ |
d3842cd76e92
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 Verify that the directive has access to the loop variables. |
d3842cd76e92
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 """ |
61 | 33 tmpl = Template("""<doc xmlns:py="http://markup.edgewall.org/"> |
50
d3842cd76e92
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 <elem py:for="item in items" py:attrs="item"/> |
d3842cd76e92
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 </doc>""") |
d3842cd76e92
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 items = [{'id': 1, 'class': 'foo'}, {'id': 2, 'class': 'bar'}] |
d3842cd76e92
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 self.assertEqual("""<doc> |
d3842cd76e92
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 <elem id="1" class="foo"/><elem id="2" class="bar"/> |
149
537f819c547b
`Template.generate()` now accepts the context data as keyword arguments, so that you don't have to import the `Context` class every time you want to pass data into a template.
cmlenz
parents:
145
diff
changeset
|
39 </doc>""", str(tmpl.generate(items=items))) |
50
d3842cd76e92
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 |
54 | 41 def test_update_existing_attr(self): |
42 """ | |
43 Verify that an attribute value that evaluates to `None` removes an | |
44 existing attribute of that name. | |
45 """ | |
61 | 46 tmpl = Template("""<doc xmlns:py="http://markup.edgewall.org/"> |
54 | 47 <elem class="foo" py:attrs="{'class': 'bar'}"/> |
48 </doc>""") | |
49 self.assertEqual("""<doc> | |
50 <elem class="bar"/> | |
51 </doc>""", str(tmpl.generate())) | |
52 | |
53 def test_remove_existing_attr(self): | |
54 """ | |
55 Verify that an attribute value that evaluates to `None` removes an | |
56 existing attribute of that name. | |
57 """ | |
61 | 58 tmpl = Template("""<doc xmlns:py="http://markup.edgewall.org/"> |
54 | 59 <elem class="foo" py:attrs="{'class': None}"/> |
60 </doc>""") | |
61 self.assertEqual("""<doc> | |
62 <elem/> | |
63 </doc>""", str(tmpl.generate())) | |
64 | |
50
d3842cd76e92
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
|
65 |
53
512eb72dbb19
* Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents:
51
diff
changeset
|
66 class ChooseDirectiveTestCase(unittest.TestCase): |
512eb72dbb19
* Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents:
51
diff
changeset
|
67 """Tests for the `py:choose` template directive and the complementary |
512eb72dbb19
* Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents:
51
diff
changeset
|
68 directives `py:when` and `py:otherwise`.""" |
512eb72dbb19
* Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents:
51
diff
changeset
|
69 |
512eb72dbb19
* Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents:
51
diff
changeset
|
70 def test_multiple_true_whens(self): |
512eb72dbb19
* Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents:
51
diff
changeset
|
71 """ |
512eb72dbb19
* Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents:
51
diff
changeset
|
72 Verify that, if multiple `py:when` bodies match, only the first is |
512eb72dbb19
* Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents:
51
diff
changeset
|
73 output. |
512eb72dbb19
* Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents:
51
diff
changeset
|
74 """ |
61 | 75 tmpl = Template("""<div xmlns:py="http://markup.edgewall.org/" py:choose=""> |
53
512eb72dbb19
* Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents:
51
diff
changeset
|
76 <span py:when="1 == 1">1</span> |
512eb72dbb19
* Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents:
51
diff
changeset
|
77 <span py:when="2 == 2">2</span> |
512eb72dbb19
* Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents:
51
diff
changeset
|
78 <span py:when="3 == 3">3</span> |
512eb72dbb19
* Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents:
51
diff
changeset
|
79 </div>""") |
512eb72dbb19
* Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents:
51
diff
changeset
|
80 self.assertEqual("""<div> |
512eb72dbb19
* Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents:
51
diff
changeset
|
81 <span>1</span> |
512eb72dbb19
* Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents:
51
diff
changeset
|
82 </div>""", str(tmpl.generate())) |
512eb72dbb19
* Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents:
51
diff
changeset
|
83 |
512eb72dbb19
* Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents:
51
diff
changeset
|
84 def test_otherwise(self): |
61 | 85 tmpl = Template("""<div xmlns:py="http://markup.edgewall.org/" py:choose=""> |
53
512eb72dbb19
* Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents:
51
diff
changeset
|
86 <span py:when="False">hidden</span> |
512eb72dbb19
* Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents:
51
diff
changeset
|
87 <span py:otherwise="">hello</span> |
512eb72dbb19
* Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents:
51
diff
changeset
|
88 </div>""") |
512eb72dbb19
* Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents:
51
diff
changeset
|
89 self.assertEqual("""<div> |
512eb72dbb19
* Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents:
51
diff
changeset
|
90 <span>hello</span> |
512eb72dbb19
* Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents:
51
diff
changeset
|
91 </div>""", str(tmpl.generate())) |
512eb72dbb19
* Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents:
51
diff
changeset
|
92 |
512eb72dbb19
* Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents:
51
diff
changeset
|
93 def test_nesting(self): |
512eb72dbb19
* Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents:
51
diff
changeset
|
94 """ |
512eb72dbb19
* Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents:
51
diff
changeset
|
95 Verify that `py:choose` blocks can be nested: |
512eb72dbb19
* Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents:
51
diff
changeset
|
96 """ |
61 | 97 tmpl = Template("""<doc xmlns:py="http://markup.edgewall.org/"> |
53
512eb72dbb19
* Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents:
51
diff
changeset
|
98 <div py:choose="1"> |
512eb72dbb19
* Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents:
51
diff
changeset
|
99 <div py:when="1" py:choose="3"> |
512eb72dbb19
* Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents:
51
diff
changeset
|
100 <span py:when="2">2</span> |
512eb72dbb19
* Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents:
51
diff
changeset
|
101 <span py:when="3">3</span> |
512eb72dbb19
* Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents:
51
diff
changeset
|
102 </div> |
512eb72dbb19
* Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents:
51
diff
changeset
|
103 </div> |
512eb72dbb19
* Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents:
51
diff
changeset
|
104 </doc>""") |
512eb72dbb19
* Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents:
51
diff
changeset
|
105 self.assertEqual("""<doc> |
512eb72dbb19
* Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents:
51
diff
changeset
|
106 <div> |
512eb72dbb19
* Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents:
51
diff
changeset
|
107 <div> |
512eb72dbb19
* Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents:
51
diff
changeset
|
108 <span>3</span> |
512eb72dbb19
* Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents:
51
diff
changeset
|
109 </div> |
512eb72dbb19
* Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents:
51
diff
changeset
|
110 </div> |
512eb72dbb19
* Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents:
51
diff
changeset
|
111 </doc>""", str(tmpl.generate())) |
512eb72dbb19
* Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents:
51
diff
changeset
|
112 |
512eb72dbb19
* Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents:
51
diff
changeset
|
113 def test_when_with_strip(self): |
512eb72dbb19
* Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents:
51
diff
changeset
|
114 """ |
512eb72dbb19
* Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents:
51
diff
changeset
|
115 Verify that a when directive with a strip directive actually strips of |
512eb72dbb19
* Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents:
51
diff
changeset
|
116 the outer element. |
512eb72dbb19
* Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents:
51
diff
changeset
|
117 """ |
61 | 118 tmpl = Template("""<doc xmlns:py="http://markup.edgewall.org/"> |
53
512eb72dbb19
* Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents:
51
diff
changeset
|
119 <div py:choose="" py:strip=""> |
512eb72dbb19
* Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents:
51
diff
changeset
|
120 <span py:otherwise="">foo</span> |
512eb72dbb19
* Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents:
51
diff
changeset
|
121 </div> |
512eb72dbb19
* Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents:
51
diff
changeset
|
122 </doc>""") |
512eb72dbb19
* Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents:
51
diff
changeset
|
123 self.assertEqual("""<doc> |
512eb72dbb19
* Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents:
51
diff
changeset
|
124 <span>foo</span> |
512eb72dbb19
* Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents:
51
diff
changeset
|
125 </doc>""", str(tmpl.generate())) |
512eb72dbb19
* Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents:
51
diff
changeset
|
126 |
65
b3fdf93057ab
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
127 def test_as_element(self): |
b3fdf93057ab
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
128 """ |
b3fdf93057ab
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
129 Verify that the directive can also be used as an element. |
b3fdf93057ab
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
130 """ |
b3fdf93057ab
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
131 tmpl = Template("""<doc xmlns:py="http://markup.edgewall.org/"> |
b3fdf93057ab
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
132 <py:choose> |
b3fdf93057ab
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
133 <py:when test="1 == 1">1</py:when> |
b3fdf93057ab
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
134 <py:when test="2 == 2">2</py:when> |
b3fdf93057ab
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
135 <py:when test="3 == 3">3</py:when> |
b3fdf93057ab
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
136 </py:choose> |
b3fdf93057ab
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
137 </doc>""") |
b3fdf93057ab
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
138 self.assertEqual("""<doc> |
b3fdf93057ab
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
139 1 |
b3fdf93057ab
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
140 </doc>""", str(tmpl.generate())) |
b3fdf93057ab
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
141 |
53
512eb72dbb19
* Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents:
51
diff
changeset
|
142 |
50
d3842cd76e92
Fix the way multiple directives are applied to a single `SUB` in many cases by making the directives themselves responsible for applying any remaining directives.
cmlenz
parents:
48
diff
changeset
|
143 class DefDirectiveTestCase(unittest.TestCase): |
d3842cd76e92
Fix the way multiple directives are applied to a single `SUB` in many cases by making the directives themselves responsible for applying any remaining directives.
cmlenz
parents:
48
diff
changeset
|
144 """Tests for the `py:def` template directive.""" |
d3842cd76e92
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
|
145 |
d3842cd76e92
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
|
146 def test_function_with_strip(self): |
d3842cd76e92
Fix the way multiple directives are applied to a single `SUB` in many cases by making the directives themselves responsible for applying any remaining directives.
cmlenz
parents:
48
diff
changeset
|
147 """ |
53
512eb72dbb19
* Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents:
51
diff
changeset
|
148 Verify that a named template function with a strip directive actually |
512eb72dbb19
* Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents:
51
diff
changeset
|
149 strips of the outer element. |
50
d3842cd76e92
Fix the way multiple directives are applied to a single `SUB` in many cases by making the directives themselves responsible for applying any remaining directives.
cmlenz
parents:
48
diff
changeset
|
150 """ |
61 | 151 tmpl = Template("""<doc xmlns:py="http://markup.edgewall.org/"> |
50
d3842cd76e92
Fix the way multiple directives are applied to a single `SUB` in many cases by making the directives themselves responsible for applying any remaining directives.
cmlenz
parents:
48
diff
changeset
|
152 <div py:def="echo(what)" py:strip=""> |
d3842cd76e92
Fix the way multiple directives are applied to a single `SUB` in many cases by making the directives themselves responsible for applying any remaining directives.
cmlenz
parents:
48
diff
changeset
|
153 <b>${what}</b> |
d3842cd76e92
Fix the way multiple directives are applied to a single `SUB` in many cases by making the directives themselves responsible for applying any remaining directives.
cmlenz
parents:
48
diff
changeset
|
154 </div> |
d3842cd76e92
Fix the way multiple directives are applied to a single `SUB` in many cases by making the directives themselves responsible for applying any remaining directives.
cmlenz
parents:
48
diff
changeset
|
155 ${echo('foo')} |
d3842cd76e92
Fix the way multiple directives are applied to a single `SUB` in many cases by making the directives themselves responsible for applying any remaining directives.
cmlenz
parents:
48
diff
changeset
|
156 </doc>""") |
d3842cd76e92
Fix the way multiple directives are applied to a single `SUB` in many cases by making the directives themselves responsible for applying any remaining directives.
cmlenz
parents:
48
diff
changeset
|
157 self.assertEqual("""<doc> |
d3842cd76e92
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
|
158 <b>foo</b> |
d3842cd76e92
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
|
159 </doc>""", str(tmpl.generate())) |
d3842cd76e92
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
|
160 |
90
c835e81c50af
When an expression evaluates to a callable, it is called implicitly.
cmlenz
parents:
89
diff
changeset
|
161 def test_exec_in_replace(self): |
c835e81c50af
When an expression evaluates to a callable, it is called implicitly.
cmlenz
parents:
89
diff
changeset
|
162 tmpl = Template("""<div xmlns:py="http://markup.edgewall.org/"> |
c835e81c50af
When an expression evaluates to a callable, it is called implicitly.
cmlenz
parents:
89
diff
changeset
|
163 <p py:def="echo(greeting, name='world')" class="message"> |
c835e81c50af
When an expression evaluates to a callable, it is called implicitly.
cmlenz
parents:
89
diff
changeset
|
164 ${greeting}, ${name}! |
c835e81c50af
When an expression evaluates to a callable, it is called implicitly.
cmlenz
parents:
89
diff
changeset
|
165 </p> |
c835e81c50af
When an expression evaluates to a callable, it is called implicitly.
cmlenz
parents:
89
diff
changeset
|
166 <div py:replace="echo('hello')"></div> |
c835e81c50af
When an expression evaluates to a callable, it is called implicitly.
cmlenz
parents:
89
diff
changeset
|
167 </div>""") |
c835e81c50af
When an expression evaluates to a callable, it is called implicitly.
cmlenz
parents:
89
diff
changeset
|
168 self.assertEqual("""<div> |
c835e81c50af
When an expression evaluates to a callable, it is called implicitly.
cmlenz
parents:
89
diff
changeset
|
169 <p class="message"> |
c835e81c50af
When an expression evaluates to a callable, it is called implicitly.
cmlenz
parents:
89
diff
changeset
|
170 hello, world! |
c835e81c50af
When an expression evaluates to a callable, it is called implicitly.
cmlenz
parents:
89
diff
changeset
|
171 </p> |
c835e81c50af
When an expression evaluates to a callable, it is called implicitly.
cmlenz
parents:
89
diff
changeset
|
172 </div>""", str(tmpl.generate())) |
c835e81c50af
When an expression evaluates to a callable, it is called implicitly.
cmlenz
parents:
89
diff
changeset
|
173 |
65
b3fdf93057ab
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
174 def test_as_element(self): |
b3fdf93057ab
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
175 """ |
b3fdf93057ab
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
176 Verify that the directive can also be used as an element. |
b3fdf93057ab
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
177 """ |
b3fdf93057ab
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
178 tmpl = Template("""<doc xmlns:py="http://markup.edgewall.org/"> |
b3fdf93057ab
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
179 <py:def function="echo(what)"> |
b3fdf93057ab
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
180 <b>${what}</b> |
b3fdf93057ab
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
181 </py:def> |
b3fdf93057ab
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
182 ${echo('foo')} |
b3fdf93057ab
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
183 </doc>""") |
b3fdf93057ab
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
184 self.assertEqual("""<doc> |
b3fdf93057ab
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
185 <b>foo</b> |
b3fdf93057ab
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
186 </doc>""", str(tmpl.generate())) |
b3fdf93057ab
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
187 |
154
8bd5c8cd33e0
* Make sure `py:def` macros don't go out of scope if they are defined inside another directive.
cmlenz
parents:
152
diff
changeset
|
188 def test_nested_defs(self): |
8bd5c8cd33e0
* Make sure `py:def` macros don't go out of scope if they are defined inside another directive.
cmlenz
parents:
152
diff
changeset
|
189 """ |
8bd5c8cd33e0
* Make sure `py:def` macros don't go out of scope if they are defined inside another directive.
cmlenz
parents:
152
diff
changeset
|
190 Verify that a template function defined inside a conditional block can |
8bd5c8cd33e0
* Make sure `py:def` macros don't go out of scope if they are defined inside another directive.
cmlenz
parents:
152
diff
changeset
|
191 be called from outside that block. |
8bd5c8cd33e0
* Make sure `py:def` macros don't go out of scope if they are defined inside another directive.
cmlenz
parents:
152
diff
changeset
|
192 """ |
8bd5c8cd33e0
* Make sure `py:def` macros don't go out of scope if they are defined inside another directive.
cmlenz
parents:
152
diff
changeset
|
193 tmpl = Template("""<doc xmlns:py="http://markup.edgewall.org/"> |
8bd5c8cd33e0
* Make sure `py:def` macros don't go out of scope if they are defined inside another directive.
cmlenz
parents:
152
diff
changeset
|
194 <py:if test="semantic"> |
8bd5c8cd33e0
* Make sure `py:def` macros don't go out of scope if they are defined inside another directive.
cmlenz
parents:
152
diff
changeset
|
195 <strong py:def="echo(what)">${what}</strong> |
8bd5c8cd33e0
* Make sure `py:def` macros don't go out of scope if they are defined inside another directive.
cmlenz
parents:
152
diff
changeset
|
196 </py:if> |
8bd5c8cd33e0
* Make sure `py:def` macros don't go out of scope if they are defined inside another directive.
cmlenz
parents:
152
diff
changeset
|
197 <py:if test="not semantic"> |
8bd5c8cd33e0
* Make sure `py:def` macros don't go out of scope if they are defined inside another directive.
cmlenz
parents:
152
diff
changeset
|
198 <b py:def="echo(what)">${what}</b> |
8bd5c8cd33e0
* Make sure `py:def` macros don't go out of scope if they are defined inside another directive.
cmlenz
parents:
152
diff
changeset
|
199 </py:if> |
8bd5c8cd33e0
* Make sure `py:def` macros don't go out of scope if they are defined inside another directive.
cmlenz
parents:
152
diff
changeset
|
200 ${echo('foo')} |
8bd5c8cd33e0
* Make sure `py:def` macros don't go out of scope if they are defined inside another directive.
cmlenz
parents:
152
diff
changeset
|
201 </doc>""") |
8bd5c8cd33e0
* Make sure `py:def` macros don't go out of scope if they are defined inside another directive.
cmlenz
parents:
152
diff
changeset
|
202 self.assertEqual("""<doc> |
8bd5c8cd33e0
* Make sure `py:def` macros don't go out of scope if they are defined inside another directive.
cmlenz
parents:
152
diff
changeset
|
203 <strong>foo</strong> |
8bd5c8cd33e0
* Make sure `py:def` macros don't go out of scope if they are defined inside another directive.
cmlenz
parents:
152
diff
changeset
|
204 </doc>""", str(tmpl.generate(semantic=True))) |
8bd5c8cd33e0
* Make sure `py:def` macros don't go out of scope if they are defined inside another directive.
cmlenz
parents:
152
diff
changeset
|
205 |
165
54a4be707664
Fix handling of keyword arguments in `py:def` directive. Thanks to Christian Boos for reporting the problem and providing the basic patch for this change.
cmlenz
parents:
158
diff
changeset
|
206 def test_function_with_default_arg(self): |
54a4be707664
Fix handling of keyword arguments in `py:def` directive. Thanks to Christian Boos for reporting the problem and providing the basic patch for this change.
cmlenz
parents:
158
diff
changeset
|
207 """ |
54a4be707664
Fix handling of keyword arguments in `py:def` directive. Thanks to Christian Boos for reporting the problem and providing the basic patch for this change.
cmlenz
parents:
158
diff
changeset
|
208 Verify that keyword arguments work with `py:def` directives. |
54a4be707664
Fix handling of keyword arguments in `py:def` directive. Thanks to Christian Boos for reporting the problem and providing the basic patch for this change.
cmlenz
parents:
158
diff
changeset
|
209 """ |
54a4be707664
Fix handling of keyword arguments in `py:def` directive. Thanks to Christian Boos for reporting the problem and providing the basic patch for this change.
cmlenz
parents:
158
diff
changeset
|
210 tmpl = Template("""<doc xmlns:py="http://markup.edgewall.org/"> |
54a4be707664
Fix handling of keyword arguments in `py:def` directive. Thanks to Christian Boos for reporting the problem and providing the basic patch for this change.
cmlenz
parents:
158
diff
changeset
|
211 <b py:def="echo(what, bold=False)" py:strip="not bold">${what}</b> |
54a4be707664
Fix handling of keyword arguments in `py:def` directive. Thanks to Christian Boos for reporting the problem and providing the basic patch for this change.
cmlenz
parents:
158
diff
changeset
|
212 ${echo('foo')} |
54a4be707664
Fix handling of keyword arguments in `py:def` directive. Thanks to Christian Boos for reporting the problem and providing the basic patch for this change.
cmlenz
parents:
158
diff
changeset
|
213 </doc>""") |
54a4be707664
Fix handling of keyword arguments in `py:def` directive. Thanks to Christian Boos for reporting the problem and providing the basic patch for this change.
cmlenz
parents:
158
diff
changeset
|
214 self.assertEqual("""<doc> |
54a4be707664
Fix handling of keyword arguments in `py:def` directive. Thanks to Christian Boos for reporting the problem and providing the basic patch for this change.
cmlenz
parents:
158
diff
changeset
|
215 foo |
54a4be707664
Fix handling of keyword arguments in `py:def` directive. Thanks to Christian Boos for reporting the problem and providing the basic patch for this change.
cmlenz
parents:
158
diff
changeset
|
216 </doc>""", str(tmpl.generate())) |
54a4be707664
Fix handling of keyword arguments in `py:def` directive. Thanks to Christian Boos for reporting the problem and providing the basic patch for this change.
cmlenz
parents:
158
diff
changeset
|
217 |
50
d3842cd76e92
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
|
218 |
51
b2383634ec04
Fix `py:for` directive when combined with other directives (such as `py:strip`).
cmlenz
parents:
50
diff
changeset
|
219 class ForDirectiveTestCase(unittest.TestCase): |
53
512eb72dbb19
* Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents:
51
diff
changeset
|
220 """Tests for the `py:for` template directive.""" |
51
b2383634ec04
Fix `py:for` directive when combined with other directives (such as `py:strip`).
cmlenz
parents:
50
diff
changeset
|
221 |
b2383634ec04
Fix `py:for` directive when combined with other directives (such as `py:strip`).
cmlenz
parents:
50
diff
changeset
|
222 def test_loop_with_strip(self): |
b2383634ec04
Fix `py:for` directive when combined with other directives (such as `py:strip`).
cmlenz
parents:
50
diff
changeset
|
223 """ |
53
512eb72dbb19
* Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents:
51
diff
changeset
|
224 Verify that the combining the `py:for` directive with `py:strip` works |
512eb72dbb19
* Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents:
51
diff
changeset
|
225 correctly. |
51
b2383634ec04
Fix `py:for` directive when combined with other directives (such as `py:strip`).
cmlenz
parents:
50
diff
changeset
|
226 """ |
61 | 227 tmpl = Template("""<doc xmlns:py="http://markup.edgewall.org/"> |
51
b2383634ec04
Fix `py:for` directive when combined with other directives (such as `py:strip`).
cmlenz
parents:
50
diff
changeset
|
228 <div py:for="item in items" py:strip=""> |
b2383634ec04
Fix `py:for` directive when combined with other directives (such as `py:strip`).
cmlenz
parents:
50
diff
changeset
|
229 <b>${item}</b> |
b2383634ec04
Fix `py:for` directive when combined with other directives (such as `py:strip`).
cmlenz
parents:
50
diff
changeset
|
230 </div> |
b2383634ec04
Fix `py:for` directive when combined with other directives (such as `py:strip`).
cmlenz
parents:
50
diff
changeset
|
231 </doc>""") |
b2383634ec04
Fix `py:for` directive when combined with other directives (such as `py:strip`).
cmlenz
parents:
50
diff
changeset
|
232 self.assertEqual("""<doc> |
b2383634ec04
Fix `py:for` directive when combined with other directives (such as `py:strip`).
cmlenz
parents:
50
diff
changeset
|
233 <b>1</b> |
b2383634ec04
Fix `py:for` directive when combined with other directives (such as `py:strip`).
cmlenz
parents:
50
diff
changeset
|
234 <b>2</b> |
b2383634ec04
Fix `py:for` directive when combined with other directives (such as `py:strip`).
cmlenz
parents:
50
diff
changeset
|
235 <b>3</b> |
b2383634ec04
Fix `py:for` directive when combined with other directives (such as `py:strip`).
cmlenz
parents:
50
diff
changeset
|
236 <b>4</b> |
b2383634ec04
Fix `py:for` directive when combined with other directives (such as `py:strip`).
cmlenz
parents:
50
diff
changeset
|
237 <b>5</b> |
149
537f819c547b
`Template.generate()` now accepts the context data as keyword arguments, so that you don't have to import the `Context` class every time you want to pass data into a template.
cmlenz
parents:
145
diff
changeset
|
238 </doc>""", str(tmpl.generate(items=range(1, 6)))) |
51
b2383634ec04
Fix `py:for` directive when combined with other directives (such as `py:strip`).
cmlenz
parents:
50
diff
changeset
|
239 |
65
b3fdf93057ab
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
240 def test_as_element(self): |
b3fdf93057ab
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
241 """ |
b3fdf93057ab
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
242 Verify that the directive can also be used as an element. |
b3fdf93057ab
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
243 """ |
b3fdf93057ab
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
244 tmpl = Template("""<doc xmlns:py="http://markup.edgewall.org/"> |
b3fdf93057ab
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
245 <py:for each="item in items"> |
b3fdf93057ab
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
246 <b>${item}</b> |
b3fdf93057ab
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
247 </py:for> |
b3fdf93057ab
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
248 </doc>""") |
b3fdf93057ab
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
249 self.assertEqual("""<doc> |
b3fdf93057ab
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
250 <b>1</b> |
b3fdf93057ab
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
251 <b>2</b> |
b3fdf93057ab
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
252 <b>3</b> |
b3fdf93057ab
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
253 <b>4</b> |
b3fdf93057ab
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
254 <b>5</b> |
149
537f819c547b
`Template.generate()` now accepts the context data as keyword arguments, so that you don't have to import the `Context` class every time you want to pass data into a template.
cmlenz
parents:
145
diff
changeset
|
255 </doc>""", str(tmpl.generate(items=range(1, 6)))) |
65
b3fdf93057ab
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
256 |
b3fdf93057ab
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
257 |
b3fdf93057ab
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
258 class IfDirectiveTestCase(unittest.TestCase): |
b3fdf93057ab
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
259 """Tests for the `py:if` template directive.""" |
b3fdf93057ab
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
260 |
b3fdf93057ab
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
261 def test_loop_with_strip(self): |
b3fdf93057ab
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
262 """ |
b3fdf93057ab
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
263 Verify that the combining the `py:if` directive with `py:strip` works |
b3fdf93057ab
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
264 correctly. |
b3fdf93057ab
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
265 """ |
b3fdf93057ab
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
266 tmpl = Template("""<doc xmlns:py="http://markup.edgewall.org/"> |
b3fdf93057ab
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
267 <b py:if="foo" py:strip="">${bar}</b> |
b3fdf93057ab
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
268 </doc>""") |
b3fdf93057ab
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
269 self.assertEqual("""<doc> |
b3fdf93057ab
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
270 Hello |
149
537f819c547b
`Template.generate()` now accepts the context data as keyword arguments, so that you don't have to import the `Context` class every time you want to pass data into a template.
cmlenz
parents:
145
diff
changeset
|
271 </doc>""", str(tmpl.generate(foo=True, bar='Hello'))) |
65
b3fdf93057ab
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
272 |
b3fdf93057ab
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
273 def test_as_element(self): |
b3fdf93057ab
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
274 """ |
b3fdf93057ab
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
275 Verify that the directive can also be used as an element. |
b3fdf93057ab
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
276 """ |
b3fdf93057ab
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
277 tmpl = Template("""<doc xmlns:py="http://markup.edgewall.org/"> |
b3fdf93057ab
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
278 <py:if test="foo">${bar}</py:if> |
b3fdf93057ab
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
279 </doc>""") |
b3fdf93057ab
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
280 self.assertEqual("""<doc> |
b3fdf93057ab
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
281 Hello |
149
537f819c547b
`Template.generate()` now accepts the context data as keyword arguments, so that you don't have to import the `Context` class every time you want to pass data into a template.
cmlenz
parents:
145
diff
changeset
|
282 </doc>""", str(tmpl.generate(foo=True, bar='Hello'))) |
65
b3fdf93057ab
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
283 |
51
b2383634ec04
Fix `py:for` directive when combined with other directives (such as `py:strip`).
cmlenz
parents:
50
diff
changeset
|
284 |
36
ed370ebfa794
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
285 class MatchDirectiveTestCase(unittest.TestCase): |
ed370ebfa794
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
286 """Tests for the `py:match` template directive.""" |
ed370ebfa794
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
287 |
50
d3842cd76e92
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
|
288 def test_with_strip(self): |
d3842cd76e92
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
|
289 """ |
d3842cd76e92
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
|
290 Verify that a match template can produce the same kind of element that |
d3842cd76e92
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
|
291 it matched without entering an infinite recursion. |
d3842cd76e92
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
|
292 """ |
61 | 293 tmpl = Template("""<doc xmlns:py="http://markup.edgewall.org/"> |
50
d3842cd76e92
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
|
294 <elem py:match="elem" py:strip=""> |
d3842cd76e92
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
|
295 <div class="elem">${select('*/text()')}</div> |
d3842cd76e92
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
|
296 </elem> |
d3842cd76e92
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
|
297 <elem>Hey Joe</elem> |
d3842cd76e92
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
|
298 </doc>""") |
d3842cd76e92
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
|
299 self.assertEqual("""<doc> |
d3842cd76e92
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
|
300 <div class="elem">Hey Joe</div> |
d3842cd76e92
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
|
301 </doc>""", str(tmpl.generate())) |
d3842cd76e92
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
|
302 |
36
ed370ebfa794
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
303 def test_without_strip(self): |
ed370ebfa794
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
304 """ |
ed370ebfa794
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
305 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
|
306 it matched without entering an infinite recursion. |
ed370ebfa794
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
307 """ |
61 | 308 tmpl = Template("""<doc xmlns:py="http://markup.edgewall.org/"> |
36
ed370ebfa794
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
309 <elem py:match="elem"> |
ed370ebfa794
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
310 <div class="elem">${select('*/text()')}</div> |
ed370ebfa794
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
311 </elem> |
ed370ebfa794
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
312 <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
|
313 </doc>""") |
36
ed370ebfa794
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
314 self.assertEqual("""<doc> |
ed370ebfa794
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
315 <elem> |
ed370ebfa794
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
316 <div class="elem">Hey Joe</div> |
ed370ebfa794
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
317 </elem> |
ed370ebfa794
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
318 </doc>""", str(tmpl.generate())) |
ed370ebfa794
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
319 |
65
b3fdf93057ab
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
320 def test_as_element(self): |
b3fdf93057ab
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
321 """ |
b3fdf93057ab
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
322 Verify that the directive can also be used as an element. |
b3fdf93057ab
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
323 """ |
b3fdf93057ab
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
324 tmpl = Template("""<doc xmlns:py="http://markup.edgewall.org/"> |
b3fdf93057ab
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
325 <py:match path="elem"> |
b3fdf93057ab
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
326 <div class="elem">${select('*/text()')}</div> |
b3fdf93057ab
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
327 </py:match> |
b3fdf93057ab
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
328 <elem>Hey Joe</elem> |
b3fdf93057ab
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
329 </doc>""") |
b3fdf93057ab
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
330 self.assertEqual("""<doc> |
b3fdf93057ab
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
331 <div class="elem">Hey Joe</div> |
b3fdf93057ab
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
332 </doc>""", str(tmpl.generate())) |
b3fdf93057ab
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
333 |
36
ed370ebfa794
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
334 def test_recursive_match_1(self): |
ed370ebfa794
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
335 """ |
ed370ebfa794
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
336 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
|
337 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
|
338 """ |
61 | 339 tmpl = Template("""<doc xmlns:py="http://markup.edgewall.org/"> |
36
ed370ebfa794
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
340 <elem py:match="elem"> |
ed370ebfa794
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
341 <div class="elem"> |
ed370ebfa794
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
342 ${select('*/*')} |
ed370ebfa794
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
343 </div> |
ed370ebfa794
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
344 </elem> |
ed370ebfa794
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
345 <elem> |
ed370ebfa794
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
346 <subelem> |
ed370ebfa794
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
347 <elem/> |
ed370ebfa794
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
348 </subelem> |
ed370ebfa794
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
349 </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
|
350 </doc>""") |
36
ed370ebfa794
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
351 self.assertEqual("""<doc> |
ed370ebfa794
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
352 <elem> |
ed370ebfa794
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
353 <div class="elem"> |
ed370ebfa794
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
354 <subelem> |
ed370ebfa794
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
355 <elem> |
ed370ebfa794
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
356 <div class="elem"> |
ed370ebfa794
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
357 </div> |
ed370ebfa794
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
358 </elem> |
ed370ebfa794
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
359 </subelem> |
ed370ebfa794
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
360 </div> |
ed370ebfa794
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
361 </elem> |
ed370ebfa794
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
362 </doc>""", str(tmpl.generate())) |
ed370ebfa794
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
363 |
ed370ebfa794
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
364 def test_recursive_match_2(self): |
ed370ebfa794
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
365 """ |
ed370ebfa794
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
366 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
|
367 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
|
368 more complex, but should work. |
ed370ebfa794
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
369 """ |
61 | 370 tmpl = Template("""<html xmlns:py="http://markup.edgewall.org/"> |
36
ed370ebfa794
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
371 <body py:match="body"> |
ed370ebfa794
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
372 <div id="header"/> |
ed370ebfa794
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
373 ${select('*/*')} |
ed370ebfa794
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
374 </body> |
ed370ebfa794
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
375 <body py:match="body"> |
ed370ebfa794
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
376 ${select('*/*')} |
ed370ebfa794
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
377 <div id="footer"/> |
ed370ebfa794
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
378 </body> |
ed370ebfa794
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
379 <body> |
ed370ebfa794
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
380 <h1>Foo</h1> |
ed370ebfa794
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
381 </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
|
382 </html>""") |
36
ed370ebfa794
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
383 self.assertEqual("""<html> |
ed370ebfa794
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
384 <body> |
ed370ebfa794
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
385 <div id="header"/><h1>Foo</h1> |
ed370ebfa794
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
386 <div id="footer"/> |
ed370ebfa794
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
387 </body> |
ed370ebfa794
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
388 </html>""", str(tmpl.generate())) |
ed370ebfa794
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
389 |
77
f5ec6d4a61e4
* Simplify implementation of the individual XPath tests (use closures instead of callable classes)
cmlenz
parents:
75
diff
changeset
|
390 def test_select_all_attrs(self): |
f5ec6d4a61e4
* Simplify implementation of the individual XPath tests (use closures instead of callable classes)
cmlenz
parents:
75
diff
changeset
|
391 tmpl = Template("""<doc xmlns:py="http://markup.edgewall.org/"> |
f5ec6d4a61e4
* Simplify implementation of the individual XPath tests (use closures instead of callable classes)
cmlenz
parents:
75
diff
changeset
|
392 <div py:match="elem" py:attrs="select('@*')"> |
f5ec6d4a61e4
* Simplify implementation of the individual XPath tests (use closures instead of callable classes)
cmlenz
parents:
75
diff
changeset
|
393 ${select('*/text()')} |
f5ec6d4a61e4
* Simplify implementation of the individual XPath tests (use closures instead of callable classes)
cmlenz
parents:
75
diff
changeset
|
394 </div> |
f5ec6d4a61e4
* Simplify implementation of the individual XPath tests (use closures instead of callable classes)
cmlenz
parents:
75
diff
changeset
|
395 <elem id="joe">Hey Joe</elem> |
f5ec6d4a61e4
* Simplify implementation of the individual XPath tests (use closures instead of callable classes)
cmlenz
parents:
75
diff
changeset
|
396 </doc>""") |
f5ec6d4a61e4
* Simplify implementation of the individual XPath tests (use closures instead of callable classes)
cmlenz
parents:
75
diff
changeset
|
397 self.assertEqual("""<doc> |
f5ec6d4a61e4
* Simplify implementation of the individual XPath tests (use closures instead of callable classes)
cmlenz
parents:
75
diff
changeset
|
398 <div id="joe"> |
f5ec6d4a61e4
* Simplify implementation of the individual XPath tests (use closures instead of callable classes)
cmlenz
parents:
75
diff
changeset
|
399 Hey Joe |
f5ec6d4a61e4
* Simplify implementation of the individual XPath tests (use closures instead of callable classes)
cmlenz
parents:
75
diff
changeset
|
400 </div> |
f5ec6d4a61e4
* Simplify implementation of the individual XPath tests (use closures instead of callable classes)
cmlenz
parents:
75
diff
changeset
|
401 </doc>""", str(tmpl.generate())) |
f5ec6d4a61e4
* Simplify implementation of the individual XPath tests (use closures instead of callable classes)
cmlenz
parents:
75
diff
changeset
|
402 |
f5ec6d4a61e4
* Simplify implementation of the individual XPath tests (use closures instead of callable classes)
cmlenz
parents:
75
diff
changeset
|
403 def test_select_all_attrs_empty(self): |
f5ec6d4a61e4
* Simplify implementation of the individual XPath tests (use closures instead of callable classes)
cmlenz
parents:
75
diff
changeset
|
404 tmpl = Template("""<doc xmlns:py="http://markup.edgewall.org/"> |
f5ec6d4a61e4
* Simplify implementation of the individual XPath tests (use closures instead of callable classes)
cmlenz
parents:
75
diff
changeset
|
405 <div py:match="elem" py:attrs="select('@*')"> |
f5ec6d4a61e4
* Simplify implementation of the individual XPath tests (use closures instead of callable classes)
cmlenz
parents:
75
diff
changeset
|
406 ${select('*/text()')} |
f5ec6d4a61e4
* Simplify implementation of the individual XPath tests (use closures instead of callable classes)
cmlenz
parents:
75
diff
changeset
|
407 </div> |
f5ec6d4a61e4
* Simplify implementation of the individual XPath tests (use closures instead of callable classes)
cmlenz
parents:
75
diff
changeset
|
408 <elem>Hey Joe</elem> |
f5ec6d4a61e4
* Simplify implementation of the individual XPath tests (use closures instead of callable classes)
cmlenz
parents:
75
diff
changeset
|
409 </doc>""") |
f5ec6d4a61e4
* Simplify implementation of the individual XPath tests (use closures instead of callable classes)
cmlenz
parents:
75
diff
changeset
|
410 self.assertEqual("""<doc> |
f5ec6d4a61e4
* Simplify implementation of the individual XPath tests (use closures instead of callable classes)
cmlenz
parents:
75
diff
changeset
|
411 <div> |
f5ec6d4a61e4
* Simplify implementation of the individual XPath tests (use closures instead of callable classes)
cmlenz
parents:
75
diff
changeset
|
412 Hey Joe |
f5ec6d4a61e4
* Simplify implementation of the individual XPath tests (use closures instead of callable classes)
cmlenz
parents:
75
diff
changeset
|
413 </div> |
f5ec6d4a61e4
* Simplify implementation of the individual XPath tests (use closures instead of callable classes)
cmlenz
parents:
75
diff
changeset
|
414 </doc>""", str(tmpl.generate())) |
f5ec6d4a61e4
* Simplify implementation of the individual XPath tests (use closures instead of callable classes)
cmlenz
parents:
75
diff
changeset
|
415 |
f5ec6d4a61e4
* Simplify implementation of the individual XPath tests (use closures instead of callable classes)
cmlenz
parents:
75
diff
changeset
|
416 def test_select_all_attrs_in_body(self): |
f5ec6d4a61e4
* Simplify implementation of the individual XPath tests (use closures instead of callable classes)
cmlenz
parents:
75
diff
changeset
|
417 tmpl = Template("""<doc xmlns:py="http://markup.edgewall.org/"> |
f5ec6d4a61e4
* Simplify implementation of the individual XPath tests (use closures instead of callable classes)
cmlenz
parents:
75
diff
changeset
|
418 <div py:match="elem"> |
f5ec6d4a61e4
* Simplify implementation of the individual XPath tests (use closures instead of callable classes)
cmlenz
parents:
75
diff
changeset
|
419 Hey ${select('text()')} ${select('@*')} |
f5ec6d4a61e4
* Simplify implementation of the individual XPath tests (use closures instead of callable classes)
cmlenz
parents:
75
diff
changeset
|
420 </div> |
f5ec6d4a61e4
* Simplify implementation of the individual XPath tests (use closures instead of callable classes)
cmlenz
parents:
75
diff
changeset
|
421 <elem title="Cool">Joe</elem> |
f5ec6d4a61e4
* Simplify implementation of the individual XPath tests (use closures instead of callable classes)
cmlenz
parents:
75
diff
changeset
|
422 </doc>""") |
f5ec6d4a61e4
* Simplify implementation of the individual XPath tests (use closures instead of callable classes)
cmlenz
parents:
75
diff
changeset
|
423 self.assertEqual("""<doc> |
f5ec6d4a61e4
* Simplify implementation of the individual XPath tests (use closures instead of callable classes)
cmlenz
parents:
75
diff
changeset
|
424 <div> |
f5ec6d4a61e4
* Simplify implementation of the individual XPath tests (use closures instead of callable classes)
cmlenz
parents:
75
diff
changeset
|
425 Hey Joe Cool |
f5ec6d4a61e4
* Simplify implementation of the individual XPath tests (use closures instead of callable classes)
cmlenz
parents:
75
diff
changeset
|
426 </div> |
f5ec6d4a61e4
* Simplify implementation of the individual XPath tests (use closures instead of callable classes)
cmlenz
parents:
75
diff
changeset
|
427 </doc>""", str(tmpl.generate())) |
f5ec6d4a61e4
* Simplify implementation of the individual XPath tests (use closures instead of callable classes)
cmlenz
parents:
75
diff
changeset
|
428 |
36
ed370ebfa794
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
429 |
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
|
430 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
|
431 """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
|
432 |
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
|
433 def test_strip_false(self): |
61 | 434 tmpl = Template("""<div xmlns:py="http://markup.edgewall.org/"> |
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
|
435 <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
|
436 </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
|
437 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
|
438 <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
|
439 </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
|
440 |
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
|
441 def test_strip_empty(self): |
61 | 442 tmpl = Template("""<div xmlns:py="http://markup.edgewall.org/"> |
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
|
443 <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
|
444 </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
|
445 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
|
446 <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
|
447 </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
|
448 |
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
|
449 |
104
f12e7987d7f4
Added `py:with` directive based on Jonas' patch in #17.
cmlenz
parents:
90
diff
changeset
|
450 class WithDirectiveTestCase(unittest.TestCase): |
f12e7987d7f4
Added `py:with` directive based on Jonas' patch in #17.
cmlenz
parents:
90
diff
changeset
|
451 """Tests for the `py:with` template directive.""" |
f12e7987d7f4
Added `py:with` directive based on Jonas' patch in #17.
cmlenz
parents:
90
diff
changeset
|
452 |
f12e7987d7f4
Added `py:with` directive based on Jonas' patch in #17.
cmlenz
parents:
90
diff
changeset
|
453 def test_shadowing(self): |
f12e7987d7f4
Added `py:with` directive based on Jonas' patch in #17.
cmlenz
parents:
90
diff
changeset
|
454 tmpl = Template("""<div xmlns:py="http://markup.edgewall.org/"> |
f12e7987d7f4
Added `py:with` directive based on Jonas' patch in #17.
cmlenz
parents:
90
diff
changeset
|
455 ${x} |
f12e7987d7f4
Added `py:with` directive based on Jonas' patch in #17.
cmlenz
parents:
90
diff
changeset
|
456 <span py:with="x = x * 2" py:replace="x"/> |
f12e7987d7f4
Added `py:with` directive based on Jonas' patch in #17.
cmlenz
parents:
90
diff
changeset
|
457 ${x} |
f12e7987d7f4
Added `py:with` directive based on Jonas' patch in #17.
cmlenz
parents:
90
diff
changeset
|
458 </div>""") |
f12e7987d7f4
Added `py:with` directive based on Jonas' patch in #17.
cmlenz
parents:
90
diff
changeset
|
459 self.assertEqual("""<div> |
f12e7987d7f4
Added `py:with` directive based on Jonas' patch in #17.
cmlenz
parents:
90
diff
changeset
|
460 42 |
f12e7987d7f4
Added `py:with` directive based on Jonas' patch in #17.
cmlenz
parents:
90
diff
changeset
|
461 84 |
f12e7987d7f4
Added `py:with` directive based on Jonas' patch in #17.
cmlenz
parents:
90
diff
changeset
|
462 42 |
149
537f819c547b
`Template.generate()` now accepts the context data as keyword arguments, so that you don't have to import the `Context` class every time you want to pass data into a template.
cmlenz
parents:
145
diff
changeset
|
463 </div>""", str(tmpl.generate(x=42))) |
104
f12e7987d7f4
Added `py:with` directive based on Jonas' patch in #17.
cmlenz
parents:
90
diff
changeset
|
464 |
f12e7987d7f4
Added `py:with` directive based on Jonas' patch in #17.
cmlenz
parents:
90
diff
changeset
|
465 def test_as_element(self): |
f12e7987d7f4
Added `py:with` directive based on Jonas' patch in #17.
cmlenz
parents:
90
diff
changeset
|
466 tmpl = Template("""<div xmlns:py="http://markup.edgewall.org/"> |
f12e7987d7f4
Added `py:with` directive based on Jonas' patch in #17.
cmlenz
parents:
90
diff
changeset
|
467 <py:with vars="x = x * 2">${x}</py:with> |
f12e7987d7f4
Added `py:with` directive based on Jonas' patch in #17.
cmlenz
parents:
90
diff
changeset
|
468 </div>""") |
f12e7987d7f4
Added `py:with` directive based on Jonas' patch in #17.
cmlenz
parents:
90
diff
changeset
|
469 self.assertEqual("""<div> |
f12e7987d7f4
Added `py:with` directive based on Jonas' patch in #17.
cmlenz
parents:
90
diff
changeset
|
470 84 |
149
537f819c547b
`Template.generate()` now accepts the context data as keyword arguments, so that you don't have to import the `Context` class every time you want to pass data into a template.
cmlenz
parents:
145
diff
changeset
|
471 </div>""", str(tmpl.generate(x=42))) |
104
f12e7987d7f4
Added `py:with` directive based on Jonas' patch in #17.
cmlenz
parents:
90
diff
changeset
|
472 |
f12e7987d7f4
Added `py:with` directive based on Jonas' patch in #17.
cmlenz
parents:
90
diff
changeset
|
473 |
1 | 474 class TemplateTestCase(unittest.TestCase): |
36
ed370ebfa794
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
475 """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
|
476 reporting. |
ed370ebfa794
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
477 """ |
1 | 478 |
479 def test_interpolate_string(self): | |
480 parts = list(Template._interpolate('bla')) | |
481 self.assertEqual(1, len(parts)) | |
482 self.assertEqual(Stream.TEXT, parts[0][0]) | |
483 self.assertEqual('bla', parts[0][1]) | |
484 | |
485 def test_interpolate_simple(self): | |
486 parts = list(Template._interpolate('${bla}')) | |
487 self.assertEqual(1, len(parts)) | |
10
f77f7a91aa46
Moved the template-specific stream event kinds into the template module.
cmlenz
parents:
1
diff
changeset
|
488 self.assertEqual(Template.EXPR, parts[0][0]) |
1 | 489 self.assertEqual('bla', parts[0][1].source) |
490 | |
491 def test_interpolate_escaped(self): | |
492 parts = list(Template._interpolate('$${bla}')) | |
493 self.assertEqual(1, len(parts)) | |
494 self.assertEqual(Stream.TEXT, parts[0][0]) | |
495 self.assertEqual('${bla}', parts[0][1]) | |
496 | |
497 def test_interpolate_short(self): | |
498 parts = list(Template._interpolate('$bla')) | |
499 self.assertEqual(1, len(parts)) | |
10
f77f7a91aa46
Moved the template-specific stream event kinds into the template module.
cmlenz
parents:
1
diff
changeset
|
500 self.assertEqual(Template.EXPR, parts[0][0]) |
1 | 501 self.assertEqual('bla', parts[0][1].source) |
502 | |
503 def test_interpolate_mixed1(self): | |
504 parts = list(Template._interpolate('$foo bar $baz')) | |
505 self.assertEqual(3, len(parts)) | |
10
f77f7a91aa46
Moved the template-specific stream event kinds into the template module.
cmlenz
parents:
1
diff
changeset
|
506 self.assertEqual(Template.EXPR, parts[0][0]) |
1 | 507 self.assertEqual('foo', parts[0][1].source) |
508 self.assertEqual(Stream.TEXT, parts[1][0]) | |
509 self.assertEqual(' bar ', parts[1][1]) | |
10
f77f7a91aa46
Moved the template-specific stream event kinds into the template module.
cmlenz
parents:
1
diff
changeset
|
510 self.assertEqual(Template.EXPR, parts[2][0]) |
1 | 511 self.assertEqual('baz', parts[2][1].source) |
512 | |
513 def test_interpolate_mixed2(self): | |
514 parts = list(Template._interpolate('foo $bar baz')) | |
515 self.assertEqual(3, len(parts)) | |
516 self.assertEqual(Stream.TEXT, parts[0][0]) | |
517 self.assertEqual('foo ', parts[0][1]) | |
10
f77f7a91aa46
Moved the template-specific stream event kinds into the template module.
cmlenz
parents:
1
diff
changeset
|
518 self.assertEqual(Template.EXPR, parts[1][0]) |
1 | 519 self.assertEqual('bar', parts[1][1].source) |
520 self.assertEqual(Stream.TEXT, parts[2][0]) | |
521 self.assertEqual(' baz', parts[2][1]) | |
522 | |
74
d54b5fd60b52
Fix expression interpolation where both shorthand notation and full notation are used inside a single text node. Thanks Jonas.
cmlenz
parents:
66
diff
changeset
|
523 def test_interpolate_mixed3(self): |
d54b5fd60b52
Fix expression interpolation where both shorthand notation and full notation are used inside a single text node. Thanks Jonas.
cmlenz
parents:
66
diff
changeset
|
524 tmpl = Template('<root> ${var} $var</root>') |
149
537f819c547b
`Template.generate()` now accepts the context data as keyword arguments, so that you don't have to import the `Context` class every time you want to pass data into a template.
cmlenz
parents:
145
diff
changeset
|
525 self.assertEqual('<root> 42 42</root>', str(tmpl.generate(var=42))) |
74
d54b5fd60b52
Fix expression interpolation where both shorthand notation and full notation are used inside a single text node. Thanks Jonas.
cmlenz
parents:
66
diff
changeset
|
526 |
48
a5d585dd38c4
convert the result of expressions in attributes to strings so that values like ints are output correctly
mgood
parents:
37
diff
changeset
|
527 def test_interpolate_non_string_attrs(self): |
a5d585dd38c4
convert the result of expressions in attributes to strings so that values like ints are output correctly
mgood
parents:
37
diff
changeset
|
528 tmpl = Template('<root attr="${1}"/>') |
75
3722696d0343
Empty attributes in templates were being stripped out. Thanks to Jonas for the patch.
cmlenz
parents:
74
diff
changeset
|
529 self.assertEqual('<root attr="1"/>', str(tmpl.generate())) |
3722696d0343
Empty attributes in templates were being stripped out. Thanks to Jonas for the patch.
cmlenz
parents:
74
diff
changeset
|
530 |
145
47bbd9d2a5af
* Fix error in expression evaluation when the expression evaluates to an iterable that does not produce event tuples.
cmlenz
parents:
134
diff
changeset
|
531 def test_interpolate_list_result(self): |
47bbd9d2a5af
* Fix error in expression evaluation when the expression evaluates to an iterable that does not produce event tuples.
cmlenz
parents:
134
diff
changeset
|
532 tmpl = Template('<root>$foo</root>') |
149
537f819c547b
`Template.generate()` now accepts the context data as keyword arguments, so that you don't have to import the `Context` class every time you want to pass data into a template.
cmlenz
parents:
145
diff
changeset
|
533 self.assertEqual('<root>buzz</root>', str(tmpl.generate(foo=('buzz',)))) |
145
47bbd9d2a5af
* Fix error in expression evaluation when the expression evaluates to an iterable that does not produce event tuples.
cmlenz
parents:
134
diff
changeset
|
534 |
75
3722696d0343
Empty attributes in templates were being stripped out. Thanks to Jonas for the patch.
cmlenz
parents:
74
diff
changeset
|
535 def test_empty_attr(self): |
3722696d0343
Empty attributes in templates were being stripped out. Thanks to Jonas for the patch.
cmlenz
parents:
74
diff
changeset
|
536 tmpl = Template('<root attr=""/>') |
3722696d0343
Empty attributes in templates were being stripped out. Thanks to Jonas for the patch.
cmlenz
parents:
74
diff
changeset
|
537 self.assertEqual('<root attr=""/>', str(tmpl.generate())) |
48
a5d585dd38c4
convert the result of expressions in attributes to strings so that values like ints are output correctly
mgood
parents:
37
diff
changeset
|
538 |
1 | 539 def test_bad_directive_error(self): |
61 | 540 xml = '<p xmlns:py="http://markup.edgewall.org/" py:do="nothing" />' |
1 | 541 try: |
21
b4d17897d053
* Include paths are now interpreted relative to the path of the including template. Closes #3.
cmlenz
parents:
10
diff
changeset
|
542 tmpl = Template(xml, filename='test.html') |
1 | 543 except BadDirectiveError, e: |
544 self.assertEqual('test.html', e.filename) | |
545 if sys.version_info[:2] >= (2, 4): | |
546 self.assertEqual(1, e.lineno) | |
547 | |
548 def test_directive_value_syntax_error(self): | |
81
d60486018004
Template expressions are now compiled to Python bytecode.
cmlenz
parents:
77
diff
changeset
|
549 xml = """<p xmlns:py="http://markup.edgewall.org/" py:if="bar'" />""" |
1 | 550 try: |
81
d60486018004
Template expressions are now compiled to Python bytecode.
cmlenz
parents:
77
diff
changeset
|
551 tmpl = Template(xml, filename='test.html') |
1 | 552 self.fail('Expected SyntaxError') |
553 except TemplateSyntaxError, e: | |
554 self.assertEqual('test.html', e.filename) | |
555 if sys.version_info[:2] >= (2, 4): | |
556 self.assertEqual(1, e.lineno) | |
557 | |
558 def test_expression_syntax_error(self): | |
81
d60486018004
Template expressions are now compiled to Python bytecode.
cmlenz
parents:
77
diff
changeset
|
559 xml = """<p> |
d60486018004
Template expressions are now compiled to Python bytecode.
cmlenz
parents:
77
diff
changeset
|
560 Foo <em>${bar"}</em> |
d60486018004
Template expressions are now compiled to Python bytecode.
cmlenz
parents:
77
diff
changeset
|
561 </p>""" |
1 | 562 try: |
81
d60486018004
Template expressions are now compiled to Python bytecode.
cmlenz
parents:
77
diff
changeset
|
563 tmpl = Template(xml, filename='test.html') |
1 | 564 self.fail('Expected SyntaxError') |
565 except TemplateSyntaxError, e: | |
566 self.assertEqual('test.html', e.filename) | |
567 if sys.version_info[:2] >= (2, 4): | |
568 self.assertEqual(2, e.lineno) | |
569 | |
134
d681d2c3cd8d
* Improve the accuracy of line numbers for text nodes, so that reported errors about syntax or evaluation errors in expressions point to the right line (not quite perfect yet, though).
cmlenz
parents:
104
diff
changeset
|
570 def test_expression_syntax_error_multi_line(self): |
d681d2c3cd8d
* Improve the accuracy of line numbers for text nodes, so that reported errors about syntax or evaluation errors in expressions point to the right line (not quite perfect yet, though).
cmlenz
parents:
104
diff
changeset
|
571 xml = """<p><em></em> |
d681d2c3cd8d
* Improve the accuracy of line numbers for text nodes, so that reported errors about syntax or evaluation errors in expressions point to the right line (not quite perfect yet, though).
cmlenz
parents:
104
diff
changeset
|
572 |
d681d2c3cd8d
* Improve the accuracy of line numbers for text nodes, so that reported errors about syntax or evaluation errors in expressions point to the right line (not quite perfect yet, though).
cmlenz
parents:
104
diff
changeset
|
573 ${bar"} |
d681d2c3cd8d
* Improve the accuracy of line numbers for text nodes, so that reported errors about syntax or evaluation errors in expressions point to the right line (not quite perfect yet, though).
cmlenz
parents:
104
diff
changeset
|
574 |
d681d2c3cd8d
* Improve the accuracy of line numbers for text nodes, so that reported errors about syntax or evaluation errors in expressions point to the right line (not quite perfect yet, though).
cmlenz
parents:
104
diff
changeset
|
575 </p>""" |
d681d2c3cd8d
* Improve the accuracy of line numbers for text nodes, so that reported errors about syntax or evaluation errors in expressions point to the right line (not quite perfect yet, though).
cmlenz
parents:
104
diff
changeset
|
576 try: |
d681d2c3cd8d
* Improve the accuracy of line numbers for text nodes, so that reported errors about syntax or evaluation errors in expressions point to the right line (not quite perfect yet, though).
cmlenz
parents:
104
diff
changeset
|
577 tmpl = Template(xml, filename='test.html') |
d681d2c3cd8d
* Improve the accuracy of line numbers for text nodes, so that reported errors about syntax or evaluation errors in expressions point to the right line (not quite perfect yet, though).
cmlenz
parents:
104
diff
changeset
|
578 self.fail('Expected SyntaxError') |
d681d2c3cd8d
* Improve the accuracy of line numbers for text nodes, so that reported errors about syntax or evaluation errors in expressions point to the right line (not quite perfect yet, though).
cmlenz
parents:
104
diff
changeset
|
579 except TemplateSyntaxError, e: |
d681d2c3cd8d
* Improve the accuracy of line numbers for text nodes, so that reported errors about syntax or evaluation errors in expressions point to the right line (not quite perfect yet, though).
cmlenz
parents:
104
diff
changeset
|
580 self.assertEqual('test.html', e.filename) |
d681d2c3cd8d
* Improve the accuracy of line numbers for text nodes, so that reported errors about syntax or evaluation errors in expressions point to the right line (not quite perfect yet, though).
cmlenz
parents:
104
diff
changeset
|
581 if sys.version_info[:2] >= (2, 4): |
d681d2c3cd8d
* Improve the accuracy of line numbers for text nodes, so that reported errors about syntax or evaluation errors in expressions point to the right line (not quite perfect yet, though).
cmlenz
parents:
104
diff
changeset
|
582 self.assertEqual(3, e.lineno) |
d681d2c3cd8d
* Improve the accuracy of line numbers for text nodes, so that reported errors about syntax or evaluation errors in expressions point to the right line (not quite perfect yet, though).
cmlenz
parents:
104
diff
changeset
|
583 |
54 | 584 def test_markup_noescape(self): |
585 """ | |
586 Verify that outputting context data that is a `Markup` instance is not | |
587 escaped. | |
588 """ | |
61 | 589 tmpl = Template("""<div xmlns:py="http://markup.edgewall.org/"> |
54 | 590 $myvar |
591 </div>""") | |
592 self.assertEqual("""<div> | |
593 <b>foo</b> | |
149
537f819c547b
`Template.generate()` now accepts the context data as keyword arguments, so that you don't have to import the `Context` class every time you want to pass data into a template.
cmlenz
parents:
145
diff
changeset
|
594 </div>""", str(tmpl.generate(myvar=Markup('<b>foo</b>')))) |
54 | 595 |
596 def test_text_noescape_quotes(self): | |
597 """ | |
598 Verify that outputting context data in text nodes doesn't escape quotes. | |
599 """ | |
61 | 600 tmpl = Template("""<div xmlns:py="http://markup.edgewall.org/"> |
54 | 601 $myvar |
602 </div>""") | |
603 self.assertEqual("""<div> | |
604 "foo" | |
149
537f819c547b
`Template.generate()` now accepts the context data as keyword arguments, so that you don't have to import the `Context` class every time you want to pass data into a template.
cmlenz
parents:
145
diff
changeset
|
605 </div>""", str(tmpl.generate(myvar='"foo"'))) |
54 | 606 |
607 def test_attr_escape_quotes(self): | |
608 """ | |
609 Verify that outputting context data in attribtes escapes quotes. | |
610 """ | |
61 | 611 tmpl = Template("""<div xmlns:py="http://markup.edgewall.org/"> |
54 | 612 <elem class="$myvar"/> |
613 </div>""") | |
614 self.assertEqual("""<div> | |
615 <elem class=""foo""/> | |
149
537f819c547b
`Template.generate()` now accepts the context data as keyword arguments, so that you don't have to import the `Context` class every time you want to pass data into a template.
cmlenz
parents:
145
diff
changeset
|
616 </div>""", str(tmpl.generate(myvar='"foo"'))) |
54 | 617 |
65
b3fdf93057ab
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
618 def test_directive_element(self): |
b3fdf93057ab
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
619 tmpl = Template("""<div xmlns:py="http://markup.edgewall.org/"> |
b3fdf93057ab
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
620 <py:if test="myvar">bar</py:if> |
b3fdf93057ab
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
621 </div>""") |
b3fdf93057ab
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
622 self.assertEqual("""<div> |
b3fdf93057ab
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
623 bar |
149
537f819c547b
`Template.generate()` now accepts the context data as keyword arguments, so that you don't have to import the `Context` class every time you want to pass data into a template.
cmlenz
parents:
145
diff
changeset
|
624 </div>""", str(tmpl.generate(myvar='"foo"'))) |
65
b3fdf93057ab
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
625 |
89
80386d62814f
Support comments in templates that are not included in the output, in the same way Kid does: if the comment text starts with a `!` character, it is stripped from the output.
cmlenz
parents:
81
diff
changeset
|
626 def test_normal_comment(self): |
80386d62814f
Support comments in templates that are not included in the output, in the same way Kid does: if the comment text starts with a `!` character, it is stripped from the output.
cmlenz
parents:
81
diff
changeset
|
627 tmpl = Template("""<div xmlns:py="http://markup.edgewall.org/"> |
80386d62814f
Support comments in templates that are not included in the output, in the same way Kid does: if the comment text starts with a `!` character, it is stripped from the output.
cmlenz
parents:
81
diff
changeset
|
628 <!-- foo bar --> |
80386d62814f
Support comments in templates that are not included in the output, in the same way Kid does: if the comment text starts with a `!` character, it is stripped from the output.
cmlenz
parents:
81
diff
changeset
|
629 </div>""") |
80386d62814f
Support comments in templates that are not included in the output, in the same way Kid does: if the comment text starts with a `!` character, it is stripped from the output.
cmlenz
parents:
81
diff
changeset
|
630 self.assertEqual("""<div> |
80386d62814f
Support comments in templates that are not included in the output, in the same way Kid does: if the comment text starts with a `!` character, it is stripped from the output.
cmlenz
parents:
81
diff
changeset
|
631 <!-- foo bar --> |
80386d62814f
Support comments in templates that are not included in the output, in the same way Kid does: if the comment text starts with a `!` character, it is stripped from the output.
cmlenz
parents:
81
diff
changeset
|
632 </div>""", str(tmpl.generate())) |
80386d62814f
Support comments in templates that are not included in the output, in the same way Kid does: if the comment text starts with a `!` character, it is stripped from the output.
cmlenz
parents:
81
diff
changeset
|
633 |
80386d62814f
Support comments in templates that are not included in the output, in the same way Kid does: if the comment text starts with a `!` character, it is stripped from the output.
cmlenz
parents:
81
diff
changeset
|
634 def test_template_comment(self): |
80386d62814f
Support comments in templates that are not included in the output, in the same way Kid does: if the comment text starts with a `!` character, it is stripped from the output.
cmlenz
parents:
81
diff
changeset
|
635 tmpl = Template("""<div xmlns:py="http://markup.edgewall.org/"> |
80386d62814f
Support comments in templates that are not included in the output, in the same way Kid does: if the comment text starts with a `!` character, it is stripped from the output.
cmlenz
parents:
81
diff
changeset
|
636 <!-- !foo --> |
80386d62814f
Support comments in templates that are not included in the output, in the same way Kid does: if the comment text starts with a `!` character, it is stripped from the output.
cmlenz
parents:
81
diff
changeset
|
637 <!--!bar--> |
80386d62814f
Support comments in templates that are not included in the output, in the same way Kid does: if the comment text starts with a `!` character, it is stripped from the output.
cmlenz
parents:
81
diff
changeset
|
638 </div>""") |
80386d62814f
Support comments in templates that are not included in the output, in the same way Kid does: if the comment text starts with a `!` character, it is stripped from the output.
cmlenz
parents:
81
diff
changeset
|
639 self.assertEqual("""<div> |
80386d62814f
Support comments in templates that are not included in the output, in the same way Kid does: if the comment text starts with a `!` character, it is stripped from the output.
cmlenz
parents:
81
diff
changeset
|
640 </div>""", str(tmpl.generate())) |
80386d62814f
Support comments in templates that are not included in the output, in the same way Kid does: if the comment text starts with a `!` character, it is stripped from the output.
cmlenz
parents:
81
diff
changeset
|
641 |
1 | 642 |
152
cdb2a1f930e2
Add some tests for relative template includes (see #27).
cmlenz
parents:
149
diff
changeset
|
643 class TemplateLoaderTestCase(unittest.TestCase): |
cdb2a1f930e2
Add some tests for relative template includes (see #27).
cmlenz
parents:
149
diff
changeset
|
644 """Tests for the template loader.""" |
cdb2a1f930e2
Add some tests for relative template includes (see #27).
cmlenz
parents:
149
diff
changeset
|
645 |
cdb2a1f930e2
Add some tests for relative template includes (see #27).
cmlenz
parents:
149
diff
changeset
|
646 def setUp(self): |
cdb2a1f930e2
Add some tests for relative template includes (see #27).
cmlenz
parents:
149
diff
changeset
|
647 self.dirname = tempfile.mkdtemp(suffix='markup_test') |
cdb2a1f930e2
Add some tests for relative template includes (see #27).
cmlenz
parents:
149
diff
changeset
|
648 |
cdb2a1f930e2
Add some tests for relative template includes (see #27).
cmlenz
parents:
149
diff
changeset
|
649 def tearDown(self): |
cdb2a1f930e2
Add some tests for relative template includes (see #27).
cmlenz
parents:
149
diff
changeset
|
650 shutil.rmtree(self.dirname) |
cdb2a1f930e2
Add some tests for relative template includes (see #27).
cmlenz
parents:
149
diff
changeset
|
651 |
cdb2a1f930e2
Add some tests for relative template includes (see #27).
cmlenz
parents:
149
diff
changeset
|
652 def test_relative_include_samedir(self): |
cdb2a1f930e2
Add some tests for relative template includes (see #27).
cmlenz
parents:
149
diff
changeset
|
653 file1 = open(os.path.join(self.dirname, 'tmpl1.html'), 'w') |
cdb2a1f930e2
Add some tests for relative template includes (see #27).
cmlenz
parents:
149
diff
changeset
|
654 try: |
cdb2a1f930e2
Add some tests for relative template includes (see #27).
cmlenz
parents:
149
diff
changeset
|
655 file1.write("""<div>Included</div>""") |
cdb2a1f930e2
Add some tests for relative template includes (see #27).
cmlenz
parents:
149
diff
changeset
|
656 finally: |
cdb2a1f930e2
Add some tests for relative template includes (see #27).
cmlenz
parents:
149
diff
changeset
|
657 file1.close() |
cdb2a1f930e2
Add some tests for relative template includes (see #27).
cmlenz
parents:
149
diff
changeset
|
658 |
cdb2a1f930e2
Add some tests for relative template includes (see #27).
cmlenz
parents:
149
diff
changeset
|
659 file2 = open(os.path.join(self.dirname, 'tmpl2.html'), 'w') |
cdb2a1f930e2
Add some tests for relative template includes (see #27).
cmlenz
parents:
149
diff
changeset
|
660 try: |
cdb2a1f930e2
Add some tests for relative template includes (see #27).
cmlenz
parents:
149
diff
changeset
|
661 file2.write("""<html xmlns:xi="http://www.w3.org/2001/XInclude"> |
158
3f23fafeef99
* Add test case for SVG content embedded in an HTML document.
cmlenz
parents:
154
diff
changeset
|
662 <xi:include href="tmpl1.html" /> |
3f23fafeef99
* Add test case for SVG content embedded in an HTML document.
cmlenz
parents:
154
diff
changeset
|
663 </html>""") |
152
cdb2a1f930e2
Add some tests for relative template includes (see #27).
cmlenz
parents:
149
diff
changeset
|
664 finally: |
cdb2a1f930e2
Add some tests for relative template includes (see #27).
cmlenz
parents:
149
diff
changeset
|
665 file2.close() |
cdb2a1f930e2
Add some tests for relative template includes (see #27).
cmlenz
parents:
149
diff
changeset
|
666 |
cdb2a1f930e2
Add some tests for relative template includes (see #27).
cmlenz
parents:
149
diff
changeset
|
667 loader = TemplateLoader([self.dirname]) |
cdb2a1f930e2
Add some tests for relative template includes (see #27).
cmlenz
parents:
149
diff
changeset
|
668 tmpl = loader.load('tmpl2.html') |
cdb2a1f930e2
Add some tests for relative template includes (see #27).
cmlenz
parents:
149
diff
changeset
|
669 self.assertEqual("""<html> |
158
3f23fafeef99
* Add test case for SVG content embedded in an HTML document.
cmlenz
parents:
154
diff
changeset
|
670 <div>Included</div> |
3f23fafeef99
* Add test case for SVG content embedded in an HTML document.
cmlenz
parents:
154
diff
changeset
|
671 </html>""", tmpl.generate().render()) |
152
cdb2a1f930e2
Add some tests for relative template includes (see #27).
cmlenz
parents:
149
diff
changeset
|
672 |
cdb2a1f930e2
Add some tests for relative template includes (see #27).
cmlenz
parents:
149
diff
changeset
|
673 def test_relative_include_subdir(self): |
cdb2a1f930e2
Add some tests for relative template includes (see #27).
cmlenz
parents:
149
diff
changeset
|
674 os.mkdir(os.path.join(self.dirname, 'sub')) |
cdb2a1f930e2
Add some tests for relative template includes (see #27).
cmlenz
parents:
149
diff
changeset
|
675 file1 = open(os.path.join(self.dirname, 'sub', 'tmpl1.html'), 'w') |
cdb2a1f930e2
Add some tests for relative template includes (see #27).
cmlenz
parents:
149
diff
changeset
|
676 try: |
cdb2a1f930e2
Add some tests for relative template includes (see #27).
cmlenz
parents:
149
diff
changeset
|
677 file1.write("""<div>Included</div>""") |
cdb2a1f930e2
Add some tests for relative template includes (see #27).
cmlenz
parents:
149
diff
changeset
|
678 finally: |
cdb2a1f930e2
Add some tests for relative template includes (see #27).
cmlenz
parents:
149
diff
changeset
|
679 file1.close() |
cdb2a1f930e2
Add some tests for relative template includes (see #27).
cmlenz
parents:
149
diff
changeset
|
680 |
cdb2a1f930e2
Add some tests for relative template includes (see #27).
cmlenz
parents:
149
diff
changeset
|
681 file2 = open(os.path.join(self.dirname, 'tmpl2.html'), 'w') |
cdb2a1f930e2
Add some tests for relative template includes (see #27).
cmlenz
parents:
149
diff
changeset
|
682 try: |
cdb2a1f930e2
Add some tests for relative template includes (see #27).
cmlenz
parents:
149
diff
changeset
|
683 file2.write("""<html xmlns:xi="http://www.w3.org/2001/XInclude"> |
158
3f23fafeef99
* Add test case for SVG content embedded in an HTML document.
cmlenz
parents:
154
diff
changeset
|
684 <xi:include href="sub/tmpl1.html" /> |
3f23fafeef99
* Add test case for SVG content embedded in an HTML document.
cmlenz
parents:
154
diff
changeset
|
685 </html>""") |
152
cdb2a1f930e2
Add some tests for relative template includes (see #27).
cmlenz
parents:
149
diff
changeset
|
686 finally: |
cdb2a1f930e2
Add some tests for relative template includes (see #27).
cmlenz
parents:
149
diff
changeset
|
687 file2.close() |
cdb2a1f930e2
Add some tests for relative template includes (see #27).
cmlenz
parents:
149
diff
changeset
|
688 |
cdb2a1f930e2
Add some tests for relative template includes (see #27).
cmlenz
parents:
149
diff
changeset
|
689 loader = TemplateLoader([self.dirname]) |
cdb2a1f930e2
Add some tests for relative template includes (see #27).
cmlenz
parents:
149
diff
changeset
|
690 tmpl = loader.load('tmpl2.html') |
cdb2a1f930e2
Add some tests for relative template includes (see #27).
cmlenz
parents:
149
diff
changeset
|
691 self.assertEqual("""<html> |
158
3f23fafeef99
* Add test case for SVG content embedded in an HTML document.
cmlenz
parents:
154
diff
changeset
|
692 <div>Included</div> |
3f23fafeef99
* Add test case for SVG content embedded in an HTML document.
cmlenz
parents:
154
diff
changeset
|
693 </html>""", tmpl.generate().render()) |
152
cdb2a1f930e2
Add some tests for relative template includes (see #27).
cmlenz
parents:
149
diff
changeset
|
694 |
cdb2a1f930e2
Add some tests for relative template includes (see #27).
cmlenz
parents:
149
diff
changeset
|
695 def test_relative_include_parentdir(self): |
cdb2a1f930e2
Add some tests for relative template includes (see #27).
cmlenz
parents:
149
diff
changeset
|
696 file1 = open(os.path.join(self.dirname, 'tmpl1.html'), 'w') |
cdb2a1f930e2
Add some tests for relative template includes (see #27).
cmlenz
parents:
149
diff
changeset
|
697 try: |
cdb2a1f930e2
Add some tests for relative template includes (see #27).
cmlenz
parents:
149
diff
changeset
|
698 file1.write("""<div>Included</div>""") |
cdb2a1f930e2
Add some tests for relative template includes (see #27).
cmlenz
parents:
149
diff
changeset
|
699 finally: |
cdb2a1f930e2
Add some tests for relative template includes (see #27).
cmlenz
parents:
149
diff
changeset
|
700 file1.close() |
cdb2a1f930e2
Add some tests for relative template includes (see #27).
cmlenz
parents:
149
diff
changeset
|
701 |
cdb2a1f930e2
Add some tests for relative template includes (see #27).
cmlenz
parents:
149
diff
changeset
|
702 os.mkdir(os.path.join(self.dirname, 'sub')) |
cdb2a1f930e2
Add some tests for relative template includes (see #27).
cmlenz
parents:
149
diff
changeset
|
703 file2 = open(os.path.join(self.dirname, 'sub', 'tmpl2.html'), 'w') |
cdb2a1f930e2
Add some tests for relative template includes (see #27).
cmlenz
parents:
149
diff
changeset
|
704 try: |
cdb2a1f930e2
Add some tests for relative template includes (see #27).
cmlenz
parents:
149
diff
changeset
|
705 file2.write("""<html xmlns:xi="http://www.w3.org/2001/XInclude"> |
158
3f23fafeef99
* Add test case for SVG content embedded in an HTML document.
cmlenz
parents:
154
diff
changeset
|
706 <xi:include href="../tmpl1.html" /> |
3f23fafeef99
* Add test case for SVG content embedded in an HTML document.
cmlenz
parents:
154
diff
changeset
|
707 </html>""") |
152
cdb2a1f930e2
Add some tests for relative template includes (see #27).
cmlenz
parents:
149
diff
changeset
|
708 finally: |
cdb2a1f930e2
Add some tests for relative template includes (see #27).
cmlenz
parents:
149
diff
changeset
|
709 file2.close() |
cdb2a1f930e2
Add some tests for relative template includes (see #27).
cmlenz
parents:
149
diff
changeset
|
710 |
cdb2a1f930e2
Add some tests for relative template includes (see #27).
cmlenz
parents:
149
diff
changeset
|
711 loader = TemplateLoader([self.dirname]) |
cdb2a1f930e2
Add some tests for relative template includes (see #27).
cmlenz
parents:
149
diff
changeset
|
712 tmpl = loader.load('sub/tmpl2.html') |
cdb2a1f930e2
Add some tests for relative template includes (see #27).
cmlenz
parents:
149
diff
changeset
|
713 self.assertEqual("""<html> |
158
3f23fafeef99
* Add test case for SVG content embedded in an HTML document.
cmlenz
parents:
154
diff
changeset
|
714 <div>Included</div> |
3f23fafeef99
* Add test case for SVG content embedded in an HTML document.
cmlenz
parents:
154
diff
changeset
|
715 </html>""", tmpl.generate().render()) |
152
cdb2a1f930e2
Add some tests for relative template includes (see #27).
cmlenz
parents:
149
diff
changeset
|
716 |
cdb2a1f930e2
Add some tests for relative template includes (see #27).
cmlenz
parents:
149
diff
changeset
|
717 |
1 | 718 def suite(): |
719 suite = unittest.TestSuite() | |
720 suite.addTest(doctest.DocTestSuite(Template.__module__)) | |
50
d3842cd76e92
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
|
721 suite.addTest(unittest.makeSuite(AttrsDirectiveTestCase, 'test')) |
53
512eb72dbb19
* Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents:
51
diff
changeset
|
722 suite.addTest(unittest.makeSuite(ChooseDirectiveTestCase, 'test')) |
50
d3842cd76e92
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
|
723 suite.addTest(unittest.makeSuite(DefDirectiveTestCase, 'test')) |
51
b2383634ec04
Fix `py:for` directive when combined with other directives (such as `py:strip`).
cmlenz
parents:
50
diff
changeset
|
724 suite.addTest(unittest.makeSuite(ForDirectiveTestCase, 'test')) |
65
b3fdf93057ab
Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents:
61
diff
changeset
|
725 suite.addTest(unittest.makeSuite(IfDirectiveTestCase, 'test')) |
36
ed370ebfa794
Fix for #7: match templates no longer process their own output.
cmlenz
parents:
27
diff
changeset
|
726 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
|
727 suite.addTest(unittest.makeSuite(StripDirectiveTestCase, 'test')) |
104
f12e7987d7f4
Added `py:with` directive based on Jonas' patch in #17.
cmlenz
parents:
90
diff
changeset
|
728 suite.addTest(unittest.makeSuite(WithDirectiveTestCase, 'test')) |
152
cdb2a1f930e2
Add some tests for relative template includes (see #27).
cmlenz
parents:
149
diff
changeset
|
729 suite.addTest(unittest.makeSuite(TemplateTestCase, 'test')) |
cdb2a1f930e2
Add some tests for relative template includes (see #27).
cmlenz
parents:
149
diff
changeset
|
730 suite.addTest(unittest.makeSuite(TemplateLoaderTestCase, 'test')) |
1 | 731 return suite |
732 | |
733 if __name__ == '__main__': | |
734 unittest.main(defaultTest='suite') |