annotate markup/tests/template.py @ 81:d60486018004 trunk

Template expressions are now compiled to Python bytecode.
author cmlenz
date Sat, 15 Jul 2006 11:29:25 +0000
parents f5ec6d4a61e4
children 80386d62814f
rev   line source
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
1 # -*- coding: utf-8 -*-
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
2 #
66
59eb24184e9c Switch copyright to Edgewall and URLs to markup.edgewall.org.
cmlenz
parents: 65
diff changeset
3 # Copyright (C) 2006 Edgewall Software
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
4 # All rights reserved.
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
5 #
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
6 # This software is licensed as described in the file COPYING, which
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
7 # you should have received as part of this distribution. The terms
66
59eb24184e9c Switch copyright to Edgewall and URLs to markup.edgewall.org.
cmlenz
parents: 65
diff changeset
8 # are also available at http://markup.edgewall.org/wiki/License.
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
9 #
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
10 # This software consists of voluntary contributions made by many
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
11 # individuals. For the exact contribution history, see the revision
66
59eb24184e9c Switch copyright to Edgewall and URLs to markup.edgewall.org.
cmlenz
parents: 65
diff changeset
12 # history and logs, available at http://markup.edgewall.org/log/.
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
13
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
14 import doctest
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
15 import unittest
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
16 import sys
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
17
54
1f3cd91325d9 Fix a number of escaping problems:
cmlenz
parents: 53
diff changeset
18 from markup.core import Markup, Stream
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
19 from markup.template import BadDirectiveError, Context, Template, \
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
20 TemplateSyntaxError
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
21
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
22
50
d3842cd76e92 Fix the way multiple directives are applied to a single `SUB` in many cases by making the directives themselves responsible for applying any remaining directives.
cmlenz
parents: 48
diff changeset
23 class AttrsDirectiveTestCase(unittest.TestCase):
d3842cd76e92 Fix the way multiple directives are applied to a single `SUB` in many cases by making the directives themselves responsible for applying any remaining directives.
cmlenz
parents: 48
diff changeset
24 """Tests for the `py:attrs` template directive."""
d3842cd76e92 Fix the way multiple directives are applied to a single `SUB` in many cases by making the directives themselves responsible for applying any remaining directives.
cmlenz
parents: 48
diff changeset
25
d3842cd76e92 Fix the way multiple directives are applied to a single `SUB` in many cases by making the directives themselves responsible for applying any remaining directives.
cmlenz
parents: 48
diff changeset
26 def test_combined_with_loop(self):
d3842cd76e92 Fix the way multiple directives are applied to a single `SUB` in many cases by making the directives themselves responsible for applying any remaining directives.
cmlenz
parents: 48
diff changeset
27 """
d3842cd76e92 Fix the way multiple directives are applied to a single `SUB` in many cases by making the directives themselves responsible for applying any remaining directives.
cmlenz
parents: 48
diff changeset
28 Verify that the directive has access to the loop variables.
d3842cd76e92 Fix the way multiple directives are applied to a single `SUB` in many cases by making the directives themselves responsible for applying any remaining directives.
cmlenz
parents: 48
diff changeset
29 """
61
448792ab1303 Use a different namespace than Kid uses.
cmlenz
parents: 54
diff changeset
30 tmpl = Template("""<doc xmlns:py="http://markup.edgewall.org/">
50
d3842cd76e92 Fix the way multiple directives are applied to a single `SUB` in many cases by making the directives themselves responsible for applying any remaining directives.
cmlenz
parents: 48
diff changeset
31 <elem py:for="item in items" py:attrs="item"/>
d3842cd76e92 Fix the way multiple directives are applied to a single `SUB` in many cases by making the directives themselves responsible for applying any remaining directives.
cmlenz
parents: 48
diff changeset
32 </doc>""")
d3842cd76e92 Fix the way multiple directives are applied to a single `SUB` in many cases by making the directives themselves responsible for applying any remaining directives.
cmlenz
parents: 48
diff changeset
33 items = [{'id': 1, 'class': 'foo'}, {'id': 2, 'class': 'bar'}]
d3842cd76e92 Fix the way multiple directives are applied to a single `SUB` in many cases by making the directives themselves responsible for applying any remaining directives.
cmlenz
parents: 48
diff changeset
34 self.assertEqual("""<doc>
d3842cd76e92 Fix the way multiple directives are applied to a single `SUB` in many cases by making the directives themselves responsible for applying any remaining directives.
cmlenz
parents: 48
diff changeset
35 <elem id="1" class="foo"/><elem id="2" class="bar"/>
d3842cd76e92 Fix the way multiple directives are applied to a single `SUB` in many cases by making the directives themselves responsible for applying any remaining directives.
cmlenz
parents: 48
diff changeset
36 </doc>""", str(tmpl.generate(Context(items=items))))
d3842cd76e92 Fix the way multiple directives are applied to a single `SUB` in many cases by making the directives themselves responsible for applying any remaining directives.
cmlenz
parents: 48
diff changeset
37
54
1f3cd91325d9 Fix a number of escaping problems:
cmlenz
parents: 53
diff changeset
38 def test_update_existing_attr(self):
1f3cd91325d9 Fix a number of escaping problems:
cmlenz
parents: 53
diff changeset
39 """
1f3cd91325d9 Fix a number of escaping problems:
cmlenz
parents: 53
diff changeset
40 Verify that an attribute value that evaluates to `None` removes an
1f3cd91325d9 Fix a number of escaping problems:
cmlenz
parents: 53
diff changeset
41 existing attribute of that name.
1f3cd91325d9 Fix a number of escaping problems:
cmlenz
parents: 53
diff changeset
42 """
61
448792ab1303 Use a different namespace than Kid uses.
cmlenz
parents: 54
diff changeset
43 tmpl = Template("""<doc xmlns:py="http://markup.edgewall.org/">
54
1f3cd91325d9 Fix a number of escaping problems:
cmlenz
parents: 53
diff changeset
44 <elem class="foo" py:attrs="{'class': 'bar'}"/>
1f3cd91325d9 Fix a number of escaping problems:
cmlenz
parents: 53
diff changeset
45 </doc>""")
1f3cd91325d9 Fix a number of escaping problems:
cmlenz
parents: 53
diff changeset
46 self.assertEqual("""<doc>
1f3cd91325d9 Fix a number of escaping problems:
cmlenz
parents: 53
diff changeset
47 <elem class="bar"/>
1f3cd91325d9 Fix a number of escaping problems:
cmlenz
parents: 53
diff changeset
48 </doc>""", str(tmpl.generate()))
1f3cd91325d9 Fix a number of escaping problems:
cmlenz
parents: 53
diff changeset
49
1f3cd91325d9 Fix a number of escaping problems:
cmlenz
parents: 53
diff changeset
50 def test_remove_existing_attr(self):
1f3cd91325d9 Fix a number of escaping problems:
cmlenz
parents: 53
diff changeset
51 """
1f3cd91325d9 Fix a number of escaping problems:
cmlenz
parents: 53
diff changeset
52 Verify that an attribute value that evaluates to `None` removes an
1f3cd91325d9 Fix a number of escaping problems:
cmlenz
parents: 53
diff changeset
53 existing attribute of that name.
1f3cd91325d9 Fix a number of escaping problems:
cmlenz
parents: 53
diff changeset
54 """
61
448792ab1303 Use a different namespace than Kid uses.
cmlenz
parents: 54
diff changeset
55 tmpl = Template("""<doc xmlns:py="http://markup.edgewall.org/">
54
1f3cd91325d9 Fix a number of escaping problems:
cmlenz
parents: 53
diff changeset
56 <elem class="foo" py:attrs="{'class': None}"/>
1f3cd91325d9 Fix a number of escaping problems:
cmlenz
parents: 53
diff changeset
57 </doc>""")
1f3cd91325d9 Fix a number of escaping problems:
cmlenz
parents: 53
diff changeset
58 self.assertEqual("""<doc>
1f3cd91325d9 Fix a number of escaping problems:
cmlenz
parents: 53
diff changeset
59 <elem/>
1f3cd91325d9 Fix a number of escaping problems:
cmlenz
parents: 53
diff changeset
60 </doc>""", str(tmpl.generate()))
1f3cd91325d9 Fix a number of escaping problems:
cmlenz
parents: 53
diff changeset
61
50
d3842cd76e92 Fix the way multiple directives are applied to a single `SUB` in many cases by making the directives themselves responsible for applying any remaining directives.
cmlenz
parents: 48
diff changeset
62
53
512eb72dbb19 * Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents: 51
diff changeset
63 class ChooseDirectiveTestCase(unittest.TestCase):
512eb72dbb19 * Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents: 51
diff changeset
64 """Tests for the `py:choose` template directive and the complementary
512eb72dbb19 * Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents: 51
diff changeset
65 directives `py:when` and `py:otherwise`."""
512eb72dbb19 * Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents: 51
diff changeset
66
512eb72dbb19 * Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents: 51
diff changeset
67 def test_multiple_true_whens(self):
512eb72dbb19 * Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents: 51
diff changeset
68 """
512eb72dbb19 * Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents: 51
diff changeset
69 Verify that, if multiple `py:when` bodies match, only the first is
512eb72dbb19 * Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents: 51
diff changeset
70 output.
512eb72dbb19 * Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents: 51
diff changeset
71 """
61
448792ab1303 Use a different namespace than Kid uses.
cmlenz
parents: 54
diff changeset
72 tmpl = Template("""<div xmlns:py="http://markup.edgewall.org/" py:choose="">
53
512eb72dbb19 * Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents: 51
diff changeset
73 <span py:when="1 == 1">1</span>
512eb72dbb19 * Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents: 51
diff changeset
74 <span py:when="2 == 2">2</span>
512eb72dbb19 * Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents: 51
diff changeset
75 <span py:when="3 == 3">3</span>
512eb72dbb19 * Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents: 51
diff changeset
76 </div>""")
512eb72dbb19 * Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents: 51
diff changeset
77 self.assertEqual("""<div>
512eb72dbb19 * Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents: 51
diff changeset
78 <span>1</span>
512eb72dbb19 * Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents: 51
diff changeset
79 </div>""", str(tmpl.generate()))
512eb72dbb19 * Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents: 51
diff changeset
80
512eb72dbb19 * Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents: 51
diff changeset
81 def test_otherwise(self):
61
448792ab1303 Use a different namespace than Kid uses.
cmlenz
parents: 54
diff changeset
82 tmpl = Template("""<div xmlns:py="http://markup.edgewall.org/" py:choose="">
53
512eb72dbb19 * Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents: 51
diff changeset
83 <span py:when="False">hidden</span>
512eb72dbb19 * Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents: 51
diff changeset
84 <span py:otherwise="">hello</span>
512eb72dbb19 * Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents: 51
diff changeset
85 </div>""")
512eb72dbb19 * Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents: 51
diff changeset
86 self.assertEqual("""<div>
512eb72dbb19 * Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents: 51
diff changeset
87 <span>hello</span>
512eb72dbb19 * Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents: 51
diff changeset
88 </div>""", str(tmpl.generate()))
512eb72dbb19 * Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents: 51
diff changeset
89
512eb72dbb19 * Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents: 51
diff changeset
90 def test_nesting(self):
512eb72dbb19 * Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents: 51
diff changeset
91 """
512eb72dbb19 * Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents: 51
diff changeset
92 Verify that `py:choose` blocks can be nested:
512eb72dbb19 * Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents: 51
diff changeset
93 """
61
448792ab1303 Use a different namespace than Kid uses.
cmlenz
parents: 54
diff changeset
94 tmpl = Template("""<doc xmlns:py="http://markup.edgewall.org/">
53
512eb72dbb19 * Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents: 51
diff changeset
95 <div py:choose="1">
512eb72dbb19 * Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents: 51
diff changeset
96 <div py:when="1" py:choose="3">
512eb72dbb19 * Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents: 51
diff changeset
97 <span py:when="2">2</span>
512eb72dbb19 * Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents: 51
diff changeset
98 <span py:when="3">3</span>
512eb72dbb19 * Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents: 51
diff changeset
99 </div>
512eb72dbb19 * Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents: 51
diff changeset
100 </div>
512eb72dbb19 * Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents: 51
diff changeset
101 </doc>""")
512eb72dbb19 * Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents: 51
diff changeset
102 self.assertEqual("""<doc>
512eb72dbb19 * Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents: 51
diff changeset
103 <div>
512eb72dbb19 * Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents: 51
diff changeset
104 <div>
512eb72dbb19 * Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents: 51
diff changeset
105 <span>3</span>
512eb72dbb19 * Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents: 51
diff changeset
106 </div>
512eb72dbb19 * Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents: 51
diff changeset
107 </div>
512eb72dbb19 * Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents: 51
diff changeset
108 </doc>""", str(tmpl.generate()))
512eb72dbb19 * Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents: 51
diff changeset
109
512eb72dbb19 * Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents: 51
diff changeset
110 def test_when_with_strip(self):
512eb72dbb19 * Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents: 51
diff changeset
111 """
512eb72dbb19 * Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents: 51
diff changeset
112 Verify that a when directive with a strip directive actually strips of
512eb72dbb19 * Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents: 51
diff changeset
113 the outer element.
512eb72dbb19 * Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents: 51
diff changeset
114 """
61
448792ab1303 Use a different namespace than Kid uses.
cmlenz
parents: 54
diff changeset
115 tmpl = Template("""<doc xmlns:py="http://markup.edgewall.org/">
53
512eb72dbb19 * Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents: 51
diff changeset
116 <div py:choose="" py:strip="">
512eb72dbb19 * Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents: 51
diff changeset
117 <span py:otherwise="">foo</span>
512eb72dbb19 * Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents: 51
diff changeset
118 </div>
512eb72dbb19 * Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents: 51
diff changeset
119 </doc>""")
512eb72dbb19 * Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents: 51
diff changeset
120 self.assertEqual("""<doc>
512eb72dbb19 * Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents: 51
diff changeset
121 <span>foo</span>
512eb72dbb19 * Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents: 51
diff changeset
122 </doc>""", str(tmpl.generate()))
512eb72dbb19 * Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents: 51
diff changeset
123
65
b3fdf93057ab Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents: 61
diff changeset
124 def test_as_element(self):
b3fdf93057ab Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents: 61
diff changeset
125 """
b3fdf93057ab Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents: 61
diff changeset
126 Verify that the directive can also be used as an element.
b3fdf93057ab Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents: 61
diff changeset
127 """
b3fdf93057ab Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents: 61
diff changeset
128 tmpl = Template("""<doc xmlns:py="http://markup.edgewall.org/">
b3fdf93057ab Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents: 61
diff changeset
129 <py:choose>
b3fdf93057ab Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents: 61
diff changeset
130 <py:when test="1 == 1">1</py:when>
b3fdf93057ab Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents: 61
diff changeset
131 <py:when test="2 == 2">2</py:when>
b3fdf93057ab Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents: 61
diff changeset
132 <py:when test="3 == 3">3</py:when>
b3fdf93057ab Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents: 61
diff changeset
133 </py:choose>
b3fdf93057ab Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents: 61
diff changeset
134 </doc>""")
b3fdf93057ab Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents: 61
diff changeset
135 self.assertEqual("""<doc>
b3fdf93057ab Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents: 61
diff changeset
136 1
b3fdf93057ab Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents: 61
diff changeset
137 </doc>""", str(tmpl.generate()))
b3fdf93057ab Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents: 61
diff changeset
138
53
512eb72dbb19 * Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents: 51
diff changeset
139
50
d3842cd76e92 Fix the way multiple directives are applied to a single `SUB` in many cases by making the directives themselves responsible for applying any remaining directives.
cmlenz
parents: 48
diff changeset
140 class DefDirectiveTestCase(unittest.TestCase):
d3842cd76e92 Fix the way multiple directives are applied to a single `SUB` in many cases by making the directives themselves responsible for applying any remaining directives.
cmlenz
parents: 48
diff changeset
141 """Tests for the `py:def` template directive."""
d3842cd76e92 Fix the way multiple directives are applied to a single `SUB` in many cases by making the directives themselves responsible for applying any remaining directives.
cmlenz
parents: 48
diff changeset
142
d3842cd76e92 Fix the way multiple directives are applied to a single `SUB` in many cases by making the directives themselves responsible for applying any remaining directives.
cmlenz
parents: 48
diff changeset
143 def test_function_with_strip(self):
d3842cd76e92 Fix the way multiple directives are applied to a single `SUB` in many cases by making the directives themselves responsible for applying any remaining directives.
cmlenz
parents: 48
diff changeset
144 """
53
512eb72dbb19 * Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents: 51
diff changeset
145 Verify that a named template function with a strip directive actually
512eb72dbb19 * Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents: 51
diff changeset
146 strips of the outer element.
50
d3842cd76e92 Fix the way multiple directives are applied to a single `SUB` in many cases by making the directives themselves responsible for applying any remaining directives.
cmlenz
parents: 48
diff changeset
147 """
61
448792ab1303 Use a different namespace than Kid uses.
cmlenz
parents: 54
diff changeset
148 tmpl = Template("""<doc xmlns:py="http://markup.edgewall.org/">
50
d3842cd76e92 Fix the way multiple directives are applied to a single `SUB` in many cases by making the directives themselves responsible for applying any remaining directives.
cmlenz
parents: 48
diff changeset
149 <div py:def="echo(what)" py:strip="">
d3842cd76e92 Fix the way multiple directives are applied to a single `SUB` in many cases by making the directives themselves responsible for applying any remaining directives.
cmlenz
parents: 48
diff changeset
150 <b>${what}</b>
d3842cd76e92 Fix the way multiple directives are applied to a single `SUB` in many cases by making the directives themselves responsible for applying any remaining directives.
cmlenz
parents: 48
diff changeset
151 </div>
d3842cd76e92 Fix the way multiple directives are applied to a single `SUB` in many cases by making the directives themselves responsible for applying any remaining directives.
cmlenz
parents: 48
diff changeset
152 ${echo('foo')}
d3842cd76e92 Fix the way multiple directives are applied to a single `SUB` in many cases by making the directives themselves responsible for applying any remaining directives.
cmlenz
parents: 48
diff changeset
153 </doc>""")
d3842cd76e92 Fix the way multiple directives are applied to a single `SUB` in many cases by making the directives themselves responsible for applying any remaining directives.
cmlenz
parents: 48
diff changeset
154 self.assertEqual("""<doc>
d3842cd76e92 Fix the way multiple directives are applied to a single `SUB` in many cases by making the directives themselves responsible for applying any remaining directives.
cmlenz
parents: 48
diff changeset
155 <b>foo</b>
d3842cd76e92 Fix the way multiple directives are applied to a single `SUB` in many cases by making the directives themselves responsible for applying any remaining directives.
cmlenz
parents: 48
diff changeset
156 </doc>""", str(tmpl.generate()))
d3842cd76e92 Fix the way multiple directives are applied to a single `SUB` in many cases by making the directives themselves responsible for applying any remaining directives.
cmlenz
parents: 48
diff changeset
157
65
b3fdf93057ab Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents: 61
diff changeset
158 def test_as_element(self):
b3fdf93057ab Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents: 61
diff changeset
159 """
b3fdf93057ab Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents: 61
diff changeset
160 Verify that the directive can also be used as an element.
b3fdf93057ab Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents: 61
diff changeset
161 """
b3fdf93057ab Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents: 61
diff changeset
162 tmpl = Template("""<doc xmlns:py="http://markup.edgewall.org/">
b3fdf93057ab Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents: 61
diff changeset
163 <py:def function="echo(what)">
b3fdf93057ab Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents: 61
diff changeset
164 <b>${what}</b>
b3fdf93057ab Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents: 61
diff changeset
165 </py:def>
b3fdf93057ab Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents: 61
diff changeset
166 ${echo('foo')}
b3fdf93057ab Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents: 61
diff changeset
167 </doc>""")
b3fdf93057ab Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents: 61
diff changeset
168 self.assertEqual("""<doc>
b3fdf93057ab Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents: 61
diff changeset
169 <b>foo</b>
b3fdf93057ab Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents: 61
diff changeset
170 </doc>""", str(tmpl.generate()))
b3fdf93057ab Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents: 61
diff changeset
171
50
d3842cd76e92 Fix the way multiple directives are applied to a single `SUB` in many cases by making the directives themselves responsible for applying any remaining directives.
cmlenz
parents: 48
diff changeset
172
51
b2383634ec04 Fix `py:for` directive when combined with other directives (such as `py:strip`).
cmlenz
parents: 50
diff changeset
173 class ForDirectiveTestCase(unittest.TestCase):
53
512eb72dbb19 * Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents: 51
diff changeset
174 """Tests for the `py:for` template directive."""
51
b2383634ec04 Fix `py:for` directive when combined with other directives (such as `py:strip`).
cmlenz
parents: 50
diff changeset
175
b2383634ec04 Fix `py:for` directive when combined with other directives (such as `py:strip`).
cmlenz
parents: 50
diff changeset
176 def test_loop_with_strip(self):
b2383634ec04 Fix `py:for` directive when combined with other directives (such as `py:strip`).
cmlenz
parents: 50
diff changeset
177 """
53
512eb72dbb19 * Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents: 51
diff changeset
178 Verify that the combining the `py:for` directive with `py:strip` works
512eb72dbb19 * Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents: 51
diff changeset
179 correctly.
51
b2383634ec04 Fix `py:for` directive when combined with other directives (such as `py:strip`).
cmlenz
parents: 50
diff changeset
180 """
61
448792ab1303 Use a different namespace than Kid uses.
cmlenz
parents: 54
diff changeset
181 tmpl = Template("""<doc xmlns:py="http://markup.edgewall.org/">
51
b2383634ec04 Fix `py:for` directive when combined with other directives (such as `py:strip`).
cmlenz
parents: 50
diff changeset
182 <div py:for="item in items" py:strip="">
b2383634ec04 Fix `py:for` directive when combined with other directives (such as `py:strip`).
cmlenz
parents: 50
diff changeset
183 <b>${item}</b>
b2383634ec04 Fix `py:for` directive when combined with other directives (such as `py:strip`).
cmlenz
parents: 50
diff changeset
184 </div>
b2383634ec04 Fix `py:for` directive when combined with other directives (such as `py:strip`).
cmlenz
parents: 50
diff changeset
185 </doc>""")
b2383634ec04 Fix `py:for` directive when combined with other directives (such as `py:strip`).
cmlenz
parents: 50
diff changeset
186 self.assertEqual("""<doc>
b2383634ec04 Fix `py:for` directive when combined with other directives (such as `py:strip`).
cmlenz
parents: 50
diff changeset
187 <b>1</b>
b2383634ec04 Fix `py:for` directive when combined with other directives (such as `py:strip`).
cmlenz
parents: 50
diff changeset
188 <b>2</b>
b2383634ec04 Fix `py:for` directive when combined with other directives (such as `py:strip`).
cmlenz
parents: 50
diff changeset
189 <b>3</b>
b2383634ec04 Fix `py:for` directive when combined with other directives (such as `py:strip`).
cmlenz
parents: 50
diff changeset
190 <b>4</b>
b2383634ec04 Fix `py:for` directive when combined with other directives (such as `py:strip`).
cmlenz
parents: 50
diff changeset
191 <b>5</b>
b2383634ec04 Fix `py:for` directive when combined with other directives (such as `py:strip`).
cmlenz
parents: 50
diff changeset
192 </doc>""", str(tmpl.generate(Context(items=range(1, 6)))))
b2383634ec04 Fix `py:for` directive when combined with other directives (such as `py:strip`).
cmlenz
parents: 50
diff changeset
193
65
b3fdf93057ab Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents: 61
diff changeset
194 def test_as_element(self):
b3fdf93057ab Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents: 61
diff changeset
195 """
b3fdf93057ab Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents: 61
diff changeset
196 Verify that the directive can also be used as an element.
b3fdf93057ab Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents: 61
diff changeset
197 """
b3fdf93057ab Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents: 61
diff changeset
198 tmpl = Template("""<doc xmlns:py="http://markup.edgewall.org/">
b3fdf93057ab Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents: 61
diff changeset
199 <py:for each="item in items">
b3fdf93057ab Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents: 61
diff changeset
200 <b>${item}</b>
b3fdf93057ab Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents: 61
diff changeset
201 </py:for>
b3fdf93057ab Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents: 61
diff changeset
202 </doc>""")
b3fdf93057ab Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents: 61
diff changeset
203 self.assertEqual("""<doc>
b3fdf93057ab Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents: 61
diff changeset
204 <b>1</b>
b3fdf93057ab Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents: 61
diff changeset
205 <b>2</b>
b3fdf93057ab Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents: 61
diff changeset
206 <b>3</b>
b3fdf93057ab Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents: 61
diff changeset
207 <b>4</b>
b3fdf93057ab Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents: 61
diff changeset
208 <b>5</b>
b3fdf93057ab Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents: 61
diff changeset
209 </doc>""", str(tmpl.generate(Context(items=range(1, 6)))))
b3fdf93057ab Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents: 61
diff changeset
210
b3fdf93057ab Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents: 61
diff changeset
211
b3fdf93057ab Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents: 61
diff changeset
212 class IfDirectiveTestCase(unittest.TestCase):
b3fdf93057ab Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents: 61
diff changeset
213 """Tests for the `py:if` template directive."""
b3fdf93057ab Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents: 61
diff changeset
214
b3fdf93057ab Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents: 61
diff changeset
215 def test_loop_with_strip(self):
b3fdf93057ab Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents: 61
diff changeset
216 """
b3fdf93057ab Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents: 61
diff changeset
217 Verify that the combining the `py:if` directive with `py:strip` works
b3fdf93057ab Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents: 61
diff changeset
218 correctly.
b3fdf93057ab Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents: 61
diff changeset
219 """
b3fdf93057ab Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents: 61
diff changeset
220 tmpl = Template("""<doc xmlns:py="http://markup.edgewall.org/">
b3fdf93057ab Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents: 61
diff changeset
221 <b py:if="foo" py:strip="">${bar}</b>
b3fdf93057ab Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents: 61
diff changeset
222 </doc>""")
b3fdf93057ab Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents: 61
diff changeset
223 self.assertEqual("""<doc>
b3fdf93057ab Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents: 61
diff changeset
224 Hello
b3fdf93057ab Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents: 61
diff changeset
225 </doc>""", str(tmpl.generate(Context(foo=True, bar='Hello'))))
b3fdf93057ab Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents: 61
diff changeset
226
b3fdf93057ab Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents: 61
diff changeset
227 def test_as_element(self):
b3fdf93057ab Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents: 61
diff changeset
228 """
b3fdf93057ab Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents: 61
diff changeset
229 Verify that the directive can also be used as an element.
b3fdf93057ab Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents: 61
diff changeset
230 """
b3fdf93057ab Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents: 61
diff changeset
231 tmpl = Template("""<doc xmlns:py="http://markup.edgewall.org/">
b3fdf93057ab Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents: 61
diff changeset
232 <py:if test="foo">${bar}</py:if>
b3fdf93057ab Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents: 61
diff changeset
233 </doc>""")
b3fdf93057ab Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents: 61
diff changeset
234 self.assertEqual("""<doc>
b3fdf93057ab Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents: 61
diff changeset
235 Hello
b3fdf93057ab Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents: 61
diff changeset
236 </doc>""", str(tmpl.generate(Context(foo=True, bar='Hello'))))
b3fdf93057ab Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents: 61
diff changeset
237
51
b2383634ec04 Fix `py:for` directive when combined with other directives (such as `py:strip`).
cmlenz
parents: 50
diff changeset
238
36
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
239 class MatchDirectiveTestCase(unittest.TestCase):
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
240 """Tests for the `py:match` template directive."""
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
241
50
d3842cd76e92 Fix the way multiple directives are applied to a single `SUB` in many cases by making the directives themselves responsible for applying any remaining directives.
cmlenz
parents: 48
diff changeset
242 def test_with_strip(self):
d3842cd76e92 Fix the way multiple directives are applied to a single `SUB` in many cases by making the directives themselves responsible for applying any remaining directives.
cmlenz
parents: 48
diff changeset
243 """
d3842cd76e92 Fix the way multiple directives are applied to a single `SUB` in many cases by making the directives themselves responsible for applying any remaining directives.
cmlenz
parents: 48
diff changeset
244 Verify that a match template can produce the same kind of element that
d3842cd76e92 Fix the way multiple directives are applied to a single `SUB` in many cases by making the directives themselves responsible for applying any remaining directives.
cmlenz
parents: 48
diff changeset
245 it matched without entering an infinite recursion.
d3842cd76e92 Fix the way multiple directives are applied to a single `SUB` in many cases by making the directives themselves responsible for applying any remaining directives.
cmlenz
parents: 48
diff changeset
246 """
61
448792ab1303 Use a different namespace than Kid uses.
cmlenz
parents: 54
diff changeset
247 tmpl = Template("""<doc xmlns:py="http://markup.edgewall.org/">
50
d3842cd76e92 Fix the way multiple directives are applied to a single `SUB` in many cases by making the directives themselves responsible for applying any remaining directives.
cmlenz
parents: 48
diff changeset
248 <elem py:match="elem" py:strip="">
d3842cd76e92 Fix the way multiple directives are applied to a single `SUB` in many cases by making the directives themselves responsible for applying any remaining directives.
cmlenz
parents: 48
diff changeset
249 <div class="elem">${select('*/text()')}</div>
d3842cd76e92 Fix the way multiple directives are applied to a single `SUB` in many cases by making the directives themselves responsible for applying any remaining directives.
cmlenz
parents: 48
diff changeset
250 </elem>
d3842cd76e92 Fix the way multiple directives are applied to a single `SUB` in many cases by making the directives themselves responsible for applying any remaining directives.
cmlenz
parents: 48
diff changeset
251 <elem>Hey Joe</elem>
d3842cd76e92 Fix the way multiple directives are applied to a single `SUB` in many cases by making the directives themselves responsible for applying any remaining directives.
cmlenz
parents: 48
diff changeset
252 </doc>""")
d3842cd76e92 Fix the way multiple directives are applied to a single `SUB` in many cases by making the directives themselves responsible for applying any remaining directives.
cmlenz
parents: 48
diff changeset
253 self.assertEqual("""<doc>
d3842cd76e92 Fix the way multiple directives are applied to a single `SUB` in many cases by making the directives themselves responsible for applying any remaining directives.
cmlenz
parents: 48
diff changeset
254 <div class="elem">Hey Joe</div>
d3842cd76e92 Fix the way multiple directives are applied to a single `SUB` in many cases by making the directives themselves responsible for applying any remaining directives.
cmlenz
parents: 48
diff changeset
255 </doc>""", str(tmpl.generate()))
d3842cd76e92 Fix the way multiple directives are applied to a single `SUB` in many cases by making the directives themselves responsible for applying any remaining directives.
cmlenz
parents: 48
diff changeset
256
36
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
257 def test_without_strip(self):
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
258 """
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
259 Verify that a match template can produce the same kind of element that
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
260 it matched without entering an infinite recursion.
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
261 """
61
448792ab1303 Use a different namespace than Kid uses.
cmlenz
parents: 54
diff changeset
262 tmpl = Template("""<doc xmlns:py="http://markup.edgewall.org/">
36
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
263 <elem py:match="elem">
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
264 <div class="elem">${select('*/text()')}</div>
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
265 </elem>
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
266 <elem>Hey Joe</elem>
37
37557b8fb925 Moved some of the tests for the strip directive to a new unittest test case to not clutter up the documentation.
cmlenz
parents: 36
diff changeset
267 </doc>""")
36
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
268 self.assertEqual("""<doc>
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
269 <elem>
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
270 <div class="elem">Hey Joe</div>
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
271 </elem>
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
272 </doc>""", str(tmpl.generate()))
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
273
65
b3fdf93057ab Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents: 61
diff changeset
274 def test_as_element(self):
b3fdf93057ab Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents: 61
diff changeset
275 """
b3fdf93057ab Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents: 61
diff changeset
276 Verify that the directive can also be used as an element.
b3fdf93057ab Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents: 61
diff changeset
277 """
b3fdf93057ab Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents: 61
diff changeset
278 tmpl = Template("""<doc xmlns:py="http://markup.edgewall.org/">
b3fdf93057ab Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents: 61
diff changeset
279 <py:match path="elem">
b3fdf93057ab Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents: 61
diff changeset
280 <div class="elem">${select('*/text()')}</div>
b3fdf93057ab Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents: 61
diff changeset
281 </py:match>
b3fdf93057ab Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents: 61
diff changeset
282 <elem>Hey Joe</elem>
b3fdf93057ab Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents: 61
diff changeset
283 </doc>""")
b3fdf93057ab Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents: 61
diff changeset
284 self.assertEqual("""<doc>
b3fdf93057ab Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents: 61
diff changeset
285 <div class="elem">Hey Joe</div>
b3fdf93057ab Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents: 61
diff changeset
286 </doc>""", str(tmpl.generate()))
b3fdf93057ab Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents: 61
diff changeset
287
36
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
288 def test_recursive_match_1(self):
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
289 """
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
290 Match directives are applied recursively, meaning that they are also
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
291 applied to any content they may have produced themselves:
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
292 """
61
448792ab1303 Use a different namespace than Kid uses.
cmlenz
parents: 54
diff changeset
293 tmpl = Template("""<doc xmlns:py="http://markup.edgewall.org/">
36
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
294 <elem py:match="elem">
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
295 <div class="elem">
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
296 ${select('*/*')}
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
297 </div>
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
298 </elem>
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
299 <elem>
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
300 <subelem>
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
301 <elem/>
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
302 </subelem>
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
303 </elem>
37
37557b8fb925 Moved some of the tests for the strip directive to a new unittest test case to not clutter up the documentation.
cmlenz
parents: 36
diff changeset
304 </doc>""")
36
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
305 self.assertEqual("""<doc>
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
306 <elem>
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
307 <div class="elem">
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
308 <subelem>
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
309 <elem>
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
310 <div class="elem">
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
311 </div>
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
312 </elem>
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
313 </subelem>
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
314 </div>
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
315 </elem>
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
316 </doc>""", str(tmpl.generate()))
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
317
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
318 def test_recursive_match_2(self):
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
319 """
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
320 When two or more match templates match the same element and also
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
321 themselves output the element they match, avoiding recursion is even
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
322 more complex, but should work.
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
323 """
61
448792ab1303 Use a different namespace than Kid uses.
cmlenz
parents: 54
diff changeset
324 tmpl = Template("""<html xmlns:py="http://markup.edgewall.org/">
36
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
325 <body py:match="body">
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
326 <div id="header"/>
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
327 ${select('*/*')}
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
328 </body>
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
329 <body py:match="body">
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
330 ${select('*/*')}
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
331 <div id="footer"/>
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
332 </body>
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
333 <body>
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
334 <h1>Foo</h1>
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
335 </body>
37
37557b8fb925 Moved some of the tests for the strip directive to a new unittest test case to not clutter up the documentation.
cmlenz
parents: 36
diff changeset
336 </html>""")
36
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
337 self.assertEqual("""<html>
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
338 <body>
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
339 <div id="header"/><h1>Foo</h1>
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
340 <div id="footer"/>
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
341 </body>
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
342 </html>""", str(tmpl.generate()))
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
343
77
f5ec6d4a61e4 * Simplify implementation of the individual XPath tests (use closures instead of callable classes)
cmlenz
parents: 75
diff changeset
344 def test_select_all_attrs(self):
f5ec6d4a61e4 * Simplify implementation of the individual XPath tests (use closures instead of callable classes)
cmlenz
parents: 75
diff changeset
345 tmpl = Template("""<doc xmlns:py="http://markup.edgewall.org/">
f5ec6d4a61e4 * Simplify implementation of the individual XPath tests (use closures instead of callable classes)
cmlenz
parents: 75
diff changeset
346 <div py:match="elem" py:attrs="select('@*')">
f5ec6d4a61e4 * Simplify implementation of the individual XPath tests (use closures instead of callable classes)
cmlenz
parents: 75
diff changeset
347 ${select('*/text()')}
f5ec6d4a61e4 * Simplify implementation of the individual XPath tests (use closures instead of callable classes)
cmlenz
parents: 75
diff changeset
348 </div>
f5ec6d4a61e4 * Simplify implementation of the individual XPath tests (use closures instead of callable classes)
cmlenz
parents: 75
diff changeset
349 <elem id="joe">Hey Joe</elem>
f5ec6d4a61e4 * Simplify implementation of the individual XPath tests (use closures instead of callable classes)
cmlenz
parents: 75
diff changeset
350 </doc>""")
f5ec6d4a61e4 * Simplify implementation of the individual XPath tests (use closures instead of callable classes)
cmlenz
parents: 75
diff changeset
351 self.assertEqual("""<doc>
f5ec6d4a61e4 * Simplify implementation of the individual XPath tests (use closures instead of callable classes)
cmlenz
parents: 75
diff changeset
352 <div id="joe">
f5ec6d4a61e4 * Simplify implementation of the individual XPath tests (use closures instead of callable classes)
cmlenz
parents: 75
diff changeset
353 Hey Joe
f5ec6d4a61e4 * Simplify implementation of the individual XPath tests (use closures instead of callable classes)
cmlenz
parents: 75
diff changeset
354 </div>
f5ec6d4a61e4 * Simplify implementation of the individual XPath tests (use closures instead of callable classes)
cmlenz
parents: 75
diff changeset
355 </doc>""", str(tmpl.generate()))
f5ec6d4a61e4 * Simplify implementation of the individual XPath tests (use closures instead of callable classes)
cmlenz
parents: 75
diff changeset
356
f5ec6d4a61e4 * Simplify implementation of the individual XPath tests (use closures instead of callable classes)
cmlenz
parents: 75
diff changeset
357 def test_select_all_attrs_empty(self):
f5ec6d4a61e4 * Simplify implementation of the individual XPath tests (use closures instead of callable classes)
cmlenz
parents: 75
diff changeset
358 tmpl = Template("""<doc xmlns:py="http://markup.edgewall.org/">
f5ec6d4a61e4 * Simplify implementation of the individual XPath tests (use closures instead of callable classes)
cmlenz
parents: 75
diff changeset
359 <div py:match="elem" py:attrs="select('@*')">
f5ec6d4a61e4 * Simplify implementation of the individual XPath tests (use closures instead of callable classes)
cmlenz
parents: 75
diff changeset
360 ${select('*/text()')}
f5ec6d4a61e4 * Simplify implementation of the individual XPath tests (use closures instead of callable classes)
cmlenz
parents: 75
diff changeset
361 </div>
f5ec6d4a61e4 * Simplify implementation of the individual XPath tests (use closures instead of callable classes)
cmlenz
parents: 75
diff changeset
362 <elem>Hey Joe</elem>
f5ec6d4a61e4 * Simplify implementation of the individual XPath tests (use closures instead of callable classes)
cmlenz
parents: 75
diff changeset
363 </doc>""")
f5ec6d4a61e4 * Simplify implementation of the individual XPath tests (use closures instead of callable classes)
cmlenz
parents: 75
diff changeset
364 self.assertEqual("""<doc>
f5ec6d4a61e4 * Simplify implementation of the individual XPath tests (use closures instead of callable classes)
cmlenz
parents: 75
diff changeset
365 <div>
f5ec6d4a61e4 * Simplify implementation of the individual XPath tests (use closures instead of callable classes)
cmlenz
parents: 75
diff changeset
366 Hey Joe
f5ec6d4a61e4 * Simplify implementation of the individual XPath tests (use closures instead of callable classes)
cmlenz
parents: 75
diff changeset
367 </div>
f5ec6d4a61e4 * Simplify implementation of the individual XPath tests (use closures instead of callable classes)
cmlenz
parents: 75
diff changeset
368 </doc>""", str(tmpl.generate()))
f5ec6d4a61e4 * Simplify implementation of the individual XPath tests (use closures instead of callable classes)
cmlenz
parents: 75
diff changeset
369
f5ec6d4a61e4 * Simplify implementation of the individual XPath tests (use closures instead of callable classes)
cmlenz
parents: 75
diff changeset
370 def test_select_all_attrs_in_body(self):
f5ec6d4a61e4 * Simplify implementation of the individual XPath tests (use closures instead of callable classes)
cmlenz
parents: 75
diff changeset
371 tmpl = Template("""<doc xmlns:py="http://markup.edgewall.org/">
f5ec6d4a61e4 * Simplify implementation of the individual XPath tests (use closures instead of callable classes)
cmlenz
parents: 75
diff changeset
372 <div py:match="elem">
f5ec6d4a61e4 * Simplify implementation of the individual XPath tests (use closures instead of callable classes)
cmlenz
parents: 75
diff changeset
373 Hey ${select('text()')} ${select('@*')}
f5ec6d4a61e4 * Simplify implementation of the individual XPath tests (use closures instead of callable classes)
cmlenz
parents: 75
diff changeset
374 </div>
f5ec6d4a61e4 * Simplify implementation of the individual XPath tests (use closures instead of callable classes)
cmlenz
parents: 75
diff changeset
375 <elem title="Cool">Joe</elem>
f5ec6d4a61e4 * Simplify implementation of the individual XPath tests (use closures instead of callable classes)
cmlenz
parents: 75
diff changeset
376 </doc>""")
f5ec6d4a61e4 * Simplify implementation of the individual XPath tests (use closures instead of callable classes)
cmlenz
parents: 75
diff changeset
377 self.assertEqual("""<doc>
f5ec6d4a61e4 * Simplify implementation of the individual XPath tests (use closures instead of callable classes)
cmlenz
parents: 75
diff changeset
378 <div>
f5ec6d4a61e4 * Simplify implementation of the individual XPath tests (use closures instead of callable classes)
cmlenz
parents: 75
diff changeset
379 Hey Joe Cool
f5ec6d4a61e4 * Simplify implementation of the individual XPath tests (use closures instead of callable classes)
cmlenz
parents: 75
diff changeset
380 </div>
f5ec6d4a61e4 * Simplify implementation of the individual XPath tests (use closures instead of callable classes)
cmlenz
parents: 75
diff changeset
381 </doc>""", str(tmpl.generate()))
f5ec6d4a61e4 * Simplify implementation of the individual XPath tests (use closures instead of callable classes)
cmlenz
parents: 75
diff changeset
382
36
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
383
37
37557b8fb925 Moved some of the tests for the strip directive to a new unittest test case to not clutter up the documentation.
cmlenz
parents: 36
diff changeset
384 class StripDirectiveTestCase(unittest.TestCase):
37557b8fb925 Moved some of the tests for the strip directive to a new unittest test case to not clutter up the documentation.
cmlenz
parents: 36
diff changeset
385 """Tests for the `py:strip` template directive."""
37557b8fb925 Moved some of the tests for the strip directive to a new unittest test case to not clutter up the documentation.
cmlenz
parents: 36
diff changeset
386
37557b8fb925 Moved some of the tests for the strip directive to a new unittest test case to not clutter up the documentation.
cmlenz
parents: 36
diff changeset
387 def test_strip_false(self):
61
448792ab1303 Use a different namespace than Kid uses.
cmlenz
parents: 54
diff changeset
388 tmpl = Template("""<div xmlns:py="http://markup.edgewall.org/">
37
37557b8fb925 Moved some of the tests for the strip directive to a new unittest test case to not clutter up the documentation.
cmlenz
parents: 36
diff changeset
389 <div py:strip="False"><b>foo</b></div>
37557b8fb925 Moved some of the tests for the strip directive to a new unittest test case to not clutter up the documentation.
cmlenz
parents: 36
diff changeset
390 </div>""")
37557b8fb925 Moved some of the tests for the strip directive to a new unittest test case to not clutter up the documentation.
cmlenz
parents: 36
diff changeset
391 self.assertEqual("""<div>
37557b8fb925 Moved some of the tests for the strip directive to a new unittest test case to not clutter up the documentation.
cmlenz
parents: 36
diff changeset
392 <div><b>foo</b></div>
37557b8fb925 Moved some of the tests for the strip directive to a new unittest test case to not clutter up the documentation.
cmlenz
parents: 36
diff changeset
393 </div>""", str(tmpl.generate()))
37557b8fb925 Moved some of the tests for the strip directive to a new unittest test case to not clutter up the documentation.
cmlenz
parents: 36
diff changeset
394
37557b8fb925 Moved some of the tests for the strip directive to a new unittest test case to not clutter up the documentation.
cmlenz
parents: 36
diff changeset
395 def test_strip_empty(self):
61
448792ab1303 Use a different namespace than Kid uses.
cmlenz
parents: 54
diff changeset
396 tmpl = Template("""<div xmlns:py="http://markup.edgewall.org/">
37
37557b8fb925 Moved some of the tests for the strip directive to a new unittest test case to not clutter up the documentation.
cmlenz
parents: 36
diff changeset
397 <div py:strip=""><b>foo</b></div>
37557b8fb925 Moved some of the tests for the strip directive to a new unittest test case to not clutter up the documentation.
cmlenz
parents: 36
diff changeset
398 </div>""")
37557b8fb925 Moved some of the tests for the strip directive to a new unittest test case to not clutter up the documentation.
cmlenz
parents: 36
diff changeset
399 self.assertEqual("""<div>
37557b8fb925 Moved some of the tests for the strip directive to a new unittest test case to not clutter up the documentation.
cmlenz
parents: 36
diff changeset
400 <b>foo</b>
37557b8fb925 Moved some of the tests for the strip directive to a new unittest test case to not clutter up the documentation.
cmlenz
parents: 36
diff changeset
401 </div>""", str(tmpl.generate()))
37557b8fb925 Moved some of the tests for the strip directive to a new unittest test case to not clutter up the documentation.
cmlenz
parents: 36
diff changeset
402
37557b8fb925 Moved some of the tests for the strip directive to a new unittest test case to not clutter up the documentation.
cmlenz
parents: 36
diff changeset
403
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
404 class TemplateTestCase(unittest.TestCase):
36
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
405 """Tests for basic template processing, expression evaluation and error
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
406 reporting.
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
407 """
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
408
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
409 def test_interpolate_string(self):
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
410 parts = list(Template._interpolate('bla'))
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
411 self.assertEqual(1, len(parts))
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
412 self.assertEqual(Stream.TEXT, parts[0][0])
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
413 self.assertEqual('bla', parts[0][1])
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
414
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
415 def test_interpolate_simple(self):
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
416 parts = list(Template._interpolate('${bla}'))
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
417 self.assertEqual(1, len(parts))
10
f77f7a91aa46 Moved the template-specific stream event kinds into the template module.
cmlenz
parents: 1
diff changeset
418 self.assertEqual(Template.EXPR, parts[0][0])
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
419 self.assertEqual('bla', parts[0][1].source)
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
420
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
421 def test_interpolate_escaped(self):
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
422 parts = list(Template._interpolate('$${bla}'))
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
423 self.assertEqual(1, len(parts))
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
424 self.assertEqual(Stream.TEXT, parts[0][0])
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
425 self.assertEqual('${bla}', parts[0][1])
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
426
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
427 def test_interpolate_short(self):
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
428 parts = list(Template._interpolate('$bla'))
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
429 self.assertEqual(1, len(parts))
10
f77f7a91aa46 Moved the template-specific stream event kinds into the template module.
cmlenz
parents: 1
diff changeset
430 self.assertEqual(Template.EXPR, parts[0][0])
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
431 self.assertEqual('bla', parts[0][1].source)
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
432
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
433 def test_interpolate_mixed1(self):
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
434 parts = list(Template._interpolate('$foo bar $baz'))
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
435 self.assertEqual(3, len(parts))
10
f77f7a91aa46 Moved the template-specific stream event kinds into the template module.
cmlenz
parents: 1
diff changeset
436 self.assertEqual(Template.EXPR, parts[0][0])
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
437 self.assertEqual('foo', parts[0][1].source)
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
438 self.assertEqual(Stream.TEXT, parts[1][0])
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
439 self.assertEqual(' bar ', parts[1][1])
10
f77f7a91aa46 Moved the template-specific stream event kinds into the template module.
cmlenz
parents: 1
diff changeset
440 self.assertEqual(Template.EXPR, parts[2][0])
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
441 self.assertEqual('baz', parts[2][1].source)
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
442
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
443 def test_interpolate_mixed2(self):
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
444 parts = list(Template._interpolate('foo $bar baz'))
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
445 self.assertEqual(3, len(parts))
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
446 self.assertEqual(Stream.TEXT, parts[0][0])
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
447 self.assertEqual('foo ', parts[0][1])
10
f77f7a91aa46 Moved the template-specific stream event kinds into the template module.
cmlenz
parents: 1
diff changeset
448 self.assertEqual(Template.EXPR, parts[1][0])
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
449 self.assertEqual('bar', parts[1][1].source)
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
450 self.assertEqual(Stream.TEXT, parts[2][0])
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
451 self.assertEqual(' baz', parts[2][1])
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
452
74
d54b5fd60b52 Fix expression interpolation where both shorthand notation and full notation are used inside a single text node. Thanks Jonas.
cmlenz
parents: 66
diff changeset
453 def test_interpolate_mixed3(self):
d54b5fd60b52 Fix expression interpolation where both shorthand notation and full notation are used inside a single text node. Thanks Jonas.
cmlenz
parents: 66
diff changeset
454 ctxt = Context(var=42)
d54b5fd60b52 Fix expression interpolation where both shorthand notation and full notation are used inside a single text node. Thanks Jonas.
cmlenz
parents: 66
diff changeset
455 tmpl = Template('<root> ${var} $var</root>')
d54b5fd60b52 Fix expression interpolation where both shorthand notation and full notation are used inside a single text node. Thanks Jonas.
cmlenz
parents: 66
diff changeset
456 self.assertEqual('<root> 42 42</root>', str(tmpl.generate(ctxt)))
d54b5fd60b52 Fix expression interpolation where both shorthand notation and full notation are used inside a single text node. Thanks Jonas.
cmlenz
parents: 66
diff changeset
457
48
a5d585dd38c4 convert the result of expressions in attributes to strings so that values like ints are output correctly
mgood
parents: 37
diff changeset
458 def test_interpolate_non_string_attrs(self):
a5d585dd38c4 convert the result of expressions in attributes to strings so that values like ints are output correctly
mgood
parents: 37
diff changeset
459 tmpl = Template('<root attr="${1}"/>')
75
3722696d0343 Empty attributes in templates were being stripped out. Thanks to Jonas for the patch.
cmlenz
parents: 74
diff changeset
460 self.assertEqual('<root attr="1"/>', str(tmpl.generate()))
3722696d0343 Empty attributes in templates were being stripped out. Thanks to Jonas for the patch.
cmlenz
parents: 74
diff changeset
461
3722696d0343 Empty attributes in templates were being stripped out. Thanks to Jonas for the patch.
cmlenz
parents: 74
diff changeset
462 def test_empty_attr(self):
3722696d0343 Empty attributes in templates were being stripped out. Thanks to Jonas for the patch.
cmlenz
parents: 74
diff changeset
463 tmpl = Template('<root attr=""/>')
3722696d0343 Empty attributes in templates were being stripped out. Thanks to Jonas for the patch.
cmlenz
parents: 74
diff changeset
464 self.assertEqual('<root attr=""/>', str(tmpl.generate()))
48
a5d585dd38c4 convert the result of expressions in attributes to strings so that values like ints are output correctly
mgood
parents: 37
diff changeset
465
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
466 def test_bad_directive_error(self):
61
448792ab1303 Use a different namespace than Kid uses.
cmlenz
parents: 54
diff changeset
467 xml = '<p xmlns:py="http://markup.edgewall.org/" py:do="nothing" />'
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
468 try:
21
b4d17897d053 * Include paths are now interpreted relative to the path of the including template. Closes #3.
cmlenz
parents: 10
diff changeset
469 tmpl = Template(xml, filename='test.html')
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
470 except BadDirectiveError, e:
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
471 self.assertEqual('test.html', e.filename)
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
472 if sys.version_info[:2] >= (2, 4):
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
473 self.assertEqual(1, e.lineno)
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
474
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
475 def test_directive_value_syntax_error(self):
81
d60486018004 Template expressions are now compiled to Python bytecode.
cmlenz
parents: 77
diff changeset
476 xml = """<p xmlns:py="http://markup.edgewall.org/" py:if="bar'" />"""
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
477 try:
81
d60486018004 Template expressions are now compiled to Python bytecode.
cmlenz
parents: 77
diff changeset
478 tmpl = Template(xml, filename='test.html')
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
479 self.fail('Expected SyntaxError')
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
480 except TemplateSyntaxError, e:
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
481 self.assertEqual('test.html', e.filename)
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
482 if sys.version_info[:2] >= (2, 4):
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
483 self.assertEqual(1, e.lineno)
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
484 # We don't really care about the offset here, do we?
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
485
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
486 def test_expression_syntax_error(self):
81
d60486018004 Template expressions are now compiled to Python bytecode.
cmlenz
parents: 77
diff changeset
487 xml = """<p>
d60486018004 Template expressions are now compiled to Python bytecode.
cmlenz
parents: 77
diff changeset
488 Foo <em>${bar"}</em>
d60486018004 Template expressions are now compiled to Python bytecode.
cmlenz
parents: 77
diff changeset
489 </p>"""
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
490 try:
81
d60486018004 Template expressions are now compiled to Python bytecode.
cmlenz
parents: 77
diff changeset
491 tmpl = Template(xml, filename='test.html')
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
492 self.fail('Expected SyntaxError')
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
493 except TemplateSyntaxError, e:
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
494 self.assertEqual('test.html', e.filename)
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
495 if sys.version_info[:2] >= (2, 4):
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
496 self.assertEqual(2, e.lineno)
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
497
54
1f3cd91325d9 Fix a number of escaping problems:
cmlenz
parents: 53
diff changeset
498 def test_markup_noescape(self):
1f3cd91325d9 Fix a number of escaping problems:
cmlenz
parents: 53
diff changeset
499 """
1f3cd91325d9 Fix a number of escaping problems:
cmlenz
parents: 53
diff changeset
500 Verify that outputting context data that is a `Markup` instance is not
1f3cd91325d9 Fix a number of escaping problems:
cmlenz
parents: 53
diff changeset
501 escaped.
1f3cd91325d9 Fix a number of escaping problems:
cmlenz
parents: 53
diff changeset
502 """
61
448792ab1303 Use a different namespace than Kid uses.
cmlenz
parents: 54
diff changeset
503 tmpl = Template("""<div xmlns:py="http://markup.edgewall.org/">
54
1f3cd91325d9 Fix a number of escaping problems:
cmlenz
parents: 53
diff changeset
504 $myvar
1f3cd91325d9 Fix a number of escaping problems:
cmlenz
parents: 53
diff changeset
505 </div>""")
1f3cd91325d9 Fix a number of escaping problems:
cmlenz
parents: 53
diff changeset
506 self.assertEqual("""<div>
1f3cd91325d9 Fix a number of escaping problems:
cmlenz
parents: 53
diff changeset
507 <b>foo</b>
1f3cd91325d9 Fix a number of escaping problems:
cmlenz
parents: 53
diff changeset
508 </div>""", str(tmpl.generate(Context(myvar=Markup('<b>foo</b>')))))
1f3cd91325d9 Fix a number of escaping problems:
cmlenz
parents: 53
diff changeset
509
1f3cd91325d9 Fix a number of escaping problems:
cmlenz
parents: 53
diff changeset
510 def test_text_noescape_quotes(self):
1f3cd91325d9 Fix a number of escaping problems:
cmlenz
parents: 53
diff changeset
511 """
1f3cd91325d9 Fix a number of escaping problems:
cmlenz
parents: 53
diff changeset
512 Verify that outputting context data in text nodes doesn't escape quotes.
1f3cd91325d9 Fix a number of escaping problems:
cmlenz
parents: 53
diff changeset
513 """
61
448792ab1303 Use a different namespace than Kid uses.
cmlenz
parents: 54
diff changeset
514 tmpl = Template("""<div xmlns:py="http://markup.edgewall.org/">
54
1f3cd91325d9 Fix a number of escaping problems:
cmlenz
parents: 53
diff changeset
515 $myvar
1f3cd91325d9 Fix a number of escaping problems:
cmlenz
parents: 53
diff changeset
516 </div>""")
1f3cd91325d9 Fix a number of escaping problems:
cmlenz
parents: 53
diff changeset
517 self.assertEqual("""<div>
1f3cd91325d9 Fix a number of escaping problems:
cmlenz
parents: 53
diff changeset
518 "foo"
1f3cd91325d9 Fix a number of escaping problems:
cmlenz
parents: 53
diff changeset
519 </div>""", str(tmpl.generate(Context(myvar='"foo"'))))
1f3cd91325d9 Fix a number of escaping problems:
cmlenz
parents: 53
diff changeset
520
1f3cd91325d9 Fix a number of escaping problems:
cmlenz
parents: 53
diff changeset
521 def test_attr_escape_quotes(self):
1f3cd91325d9 Fix a number of escaping problems:
cmlenz
parents: 53
diff changeset
522 """
1f3cd91325d9 Fix a number of escaping problems:
cmlenz
parents: 53
diff changeset
523 Verify that outputting context data in attribtes escapes quotes.
1f3cd91325d9 Fix a number of escaping problems:
cmlenz
parents: 53
diff changeset
524 """
61
448792ab1303 Use a different namespace than Kid uses.
cmlenz
parents: 54
diff changeset
525 tmpl = Template("""<div xmlns:py="http://markup.edgewall.org/">
54
1f3cd91325d9 Fix a number of escaping problems:
cmlenz
parents: 53
diff changeset
526 <elem class="$myvar"/>
1f3cd91325d9 Fix a number of escaping problems:
cmlenz
parents: 53
diff changeset
527 </div>""")
1f3cd91325d9 Fix a number of escaping problems:
cmlenz
parents: 53
diff changeset
528 self.assertEqual("""<div>
1f3cd91325d9 Fix a number of escaping problems:
cmlenz
parents: 53
diff changeset
529 <elem class="&#34;foo&#34;"/>
1f3cd91325d9 Fix a number of escaping problems:
cmlenz
parents: 53
diff changeset
530 </div>""", str(tmpl.generate(Context(myvar='"foo"'))))
1f3cd91325d9 Fix a number of escaping problems:
cmlenz
parents: 53
diff changeset
531
65
b3fdf93057ab Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents: 61
diff changeset
532 def test_directive_element(self):
b3fdf93057ab Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents: 61
diff changeset
533 tmpl = Template("""<div xmlns:py="http://markup.edgewall.org/">
b3fdf93057ab Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents: 61
diff changeset
534 <py:if test="myvar">bar</py:if>
b3fdf93057ab Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents: 61
diff changeset
535 </div>""")
b3fdf93057ab Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents: 61
diff changeset
536 self.assertEqual("""<div>
b3fdf93057ab Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents: 61
diff changeset
537 bar
b3fdf93057ab Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents: 61
diff changeset
538 </div>""", str(tmpl.generate(Context(myvar='"foo"'))))
b3fdf93057ab Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents: 61
diff changeset
539
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
540
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
541 def suite():
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
542 suite = unittest.TestSuite()
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
543 suite.addTest(doctest.DocTestSuite(Template.__module__))
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
544 suite.addTest(unittest.makeSuite(TemplateTestCase, 'test'))
50
d3842cd76e92 Fix the way multiple directives are applied to a single `SUB` in many cases by making the directives themselves responsible for applying any remaining directives.
cmlenz
parents: 48
diff changeset
545 suite.addTest(unittest.makeSuite(AttrsDirectiveTestCase, 'test'))
53
512eb72dbb19 * Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents: 51
diff changeset
546 suite.addTest(unittest.makeSuite(ChooseDirectiveTestCase, 'test'))
50
d3842cd76e92 Fix the way multiple directives are applied to a single `SUB` in many cases by making the directives themselves responsible for applying any remaining directives.
cmlenz
parents: 48
diff changeset
547 suite.addTest(unittest.makeSuite(DefDirectiveTestCase, 'test'))
51
b2383634ec04 Fix `py:for` directive when combined with other directives (such as `py:strip`).
cmlenz
parents: 50
diff changeset
548 suite.addTest(unittest.makeSuite(ForDirectiveTestCase, 'test'))
65
b3fdf93057ab Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents: 61
diff changeset
549 suite.addTest(unittest.makeSuite(IfDirectiveTestCase, 'test'))
36
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
550 suite.addTest(unittest.makeSuite(MatchDirectiveTestCase, 'test'))
37
37557b8fb925 Moved some of the tests for the strip directive to a new unittest test case to not clutter up the documentation.
cmlenz
parents: 36
diff changeset
551 suite.addTest(unittest.makeSuite(StripDirectiveTestCase, 'test'))
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
552 return suite
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
553
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
554 if __name__ == '__main__':
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
555 unittest.main(defaultTest='suite')
Copyright (C) 2012-2017 Edgewall Software