annotate markup/tests/template.py @ 158:8e81177059f3

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