annotate markup/tests/template.py @ 61:33c2702cf6da

Use a different namespace than Kid uses.
author cmlenz
date Fri, 07 Jul 2006 17:54:52 +0000
parents 01981cbc7575
children 5c024cf58ecb
rev   line source
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
1 # -*- coding: utf-8 -*-
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
2 #
27
b8456279c444 * Fix the boilerplate in the Python source files.
cmlenz
parents: 21
diff changeset
3 # Copyright (C) 2006 Christopher Lenz
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
27
b8456279c444 * Fix the boilerplate in the Python source files.
cmlenz
parents: 21
diff changeset
8 # are also available at http://markup.cmlenz.net/wiki/License.
1
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
27
b8456279c444 * Fix the boilerplate in the Python source files.
cmlenz
parents: 21
diff changeset
12 # history and logs, available at http://markup.cmlenz.net/log/.
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
13
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
14 import doctest
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
15 import unittest
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
16 import sys
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
17
54
01981cbc7575 Fix a number of escaping problems:
cmlenz
parents: 53
diff changeset
18 from markup.core import Markup, Stream
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
19 from markup.template import BadDirectiveError, Context, Template, \
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
20 TemplateSyntaxError
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
21
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
22
50
a053ffb834cb Fix the way multiple directives are applied to a single `SUB` in many cases by making the directives themselves responsible for applying any remaining directives.
cmlenz
parents: 48
diff changeset
23 class AttrsDirectiveTestCase(unittest.TestCase):
a053ffb834cb Fix the way multiple directives are applied to a single `SUB` in many cases by making the directives themselves responsible for applying any remaining directives.
cmlenz
parents: 48
diff changeset
24 """Tests for the `py:attrs` template directive."""
a053ffb834cb Fix the way multiple directives are applied to a single `SUB` in many cases by making the directives themselves responsible for applying any remaining directives.
cmlenz
parents: 48
diff changeset
25
a053ffb834cb Fix the way multiple directives are applied to a single `SUB` in many cases by making the directives themselves responsible for applying any remaining directives.
cmlenz
parents: 48
diff changeset
26 def test_combined_with_loop(self):
a053ffb834cb Fix the way multiple directives are applied to a single `SUB` in many cases by making the directives themselves responsible for applying any remaining directives.
cmlenz
parents: 48
diff changeset
27 """
a053ffb834cb Fix the way multiple directives are applied to a single `SUB` in many cases by making the directives themselves responsible for applying any remaining directives.
cmlenz
parents: 48
diff changeset
28 Verify that the directive has access to the loop variables.
a053ffb834cb Fix the way multiple directives are applied to a single `SUB` in many cases by making the directives themselves responsible for applying any remaining directives.
cmlenz
parents: 48
diff changeset
29 """
61
33c2702cf6da Use a different namespace than Kid uses.
cmlenz
parents: 54
diff changeset
30 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
31 <elem py:for="item in items" py:attrs="item"/>
a053ffb834cb Fix the way multiple directives are applied to a single `SUB` in many cases by making the directives themselves responsible for applying any remaining directives.
cmlenz
parents: 48
diff changeset
32 </doc>""")
a053ffb834cb Fix the way multiple directives are applied to a single `SUB` in many cases by making the directives themselves responsible for applying any remaining directives.
cmlenz
parents: 48
diff changeset
33 items = [{'id': 1, 'class': 'foo'}, {'id': 2, 'class': 'bar'}]
a053ffb834cb Fix the way multiple directives are applied to a single `SUB` in many cases by making the directives themselves responsible for applying any remaining directives.
cmlenz
parents: 48
diff changeset
34 self.assertEqual("""<doc>
a053ffb834cb Fix the way multiple directives are applied to a single `SUB` in many cases by making the directives themselves responsible for applying any remaining directives.
cmlenz
parents: 48
diff changeset
35 <elem id="1" class="foo"/><elem id="2" class="bar"/>
a053ffb834cb Fix the way multiple directives are applied to a single `SUB` in many cases by making the directives themselves responsible for applying any remaining directives.
cmlenz
parents: 48
diff changeset
36 </doc>""", str(tmpl.generate(Context(items=items))))
a053ffb834cb Fix the way multiple directives are applied to a single `SUB` in many cases by making the directives themselves responsible for applying any remaining directives.
cmlenz
parents: 48
diff changeset
37
54
01981cbc7575 Fix a number of escaping problems:
cmlenz
parents: 53
diff changeset
38 def test_update_existing_attr(self):
01981cbc7575 Fix a number of escaping problems:
cmlenz
parents: 53
diff changeset
39 """
01981cbc7575 Fix a number of escaping problems:
cmlenz
parents: 53
diff changeset
40 Verify that an attribute value that evaluates to `None` removes an
01981cbc7575 Fix a number of escaping problems:
cmlenz
parents: 53
diff changeset
41 existing attribute of that name.
01981cbc7575 Fix a number of escaping problems:
cmlenz
parents: 53
diff changeset
42 """
61
33c2702cf6da Use a different namespace than Kid uses.
cmlenz
parents: 54
diff changeset
43 tmpl = Template("""<doc xmlns:py="http://markup.edgewall.org/">
54
01981cbc7575 Fix a number of escaping problems:
cmlenz
parents: 53
diff changeset
44 <elem class="foo" py:attrs="{'class': 'bar'}"/>
01981cbc7575 Fix a number of escaping problems:
cmlenz
parents: 53
diff changeset
45 </doc>""")
01981cbc7575 Fix a number of escaping problems:
cmlenz
parents: 53
diff changeset
46 self.assertEqual("""<doc>
01981cbc7575 Fix a number of escaping problems:
cmlenz
parents: 53
diff changeset
47 <elem class="bar"/>
01981cbc7575 Fix a number of escaping problems:
cmlenz
parents: 53
diff changeset
48 </doc>""", str(tmpl.generate()))
01981cbc7575 Fix a number of escaping problems:
cmlenz
parents: 53
diff changeset
49
01981cbc7575 Fix a number of escaping problems:
cmlenz
parents: 53
diff changeset
50 def test_remove_existing_attr(self):
01981cbc7575 Fix a number of escaping problems:
cmlenz
parents: 53
diff changeset
51 """
01981cbc7575 Fix a number of escaping problems:
cmlenz
parents: 53
diff changeset
52 Verify that an attribute value that evaluates to `None` removes an
01981cbc7575 Fix a number of escaping problems:
cmlenz
parents: 53
diff changeset
53 existing attribute of that name.
01981cbc7575 Fix a number of escaping problems:
cmlenz
parents: 53
diff changeset
54 """
61
33c2702cf6da Use a different namespace than Kid uses.
cmlenz
parents: 54
diff changeset
55 tmpl = Template("""<doc xmlns:py="http://markup.edgewall.org/">
54
01981cbc7575 Fix a number of escaping problems:
cmlenz
parents: 53
diff changeset
56 <elem class="foo" py:attrs="{'class': None}"/>
01981cbc7575 Fix a number of escaping problems:
cmlenz
parents: 53
diff changeset
57 </doc>""")
01981cbc7575 Fix a number of escaping problems:
cmlenz
parents: 53
diff changeset
58 self.assertEqual("""<doc>
01981cbc7575 Fix a number of escaping problems:
cmlenz
parents: 53
diff changeset
59 <elem/>
01981cbc7575 Fix a number of escaping problems:
cmlenz
parents: 53
diff changeset
60 </doc>""", str(tmpl.generate()))
01981cbc7575 Fix a number of escaping problems:
cmlenz
parents: 53
diff changeset
61
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
62
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
63 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
64 """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
65 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
66
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 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
68 """
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 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
70 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
71 """
61
33c2702cf6da 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
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 <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
74 <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
75 <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
76 </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
77 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
78 <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
79 </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
80
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 def test_otherwise(self):
61
33c2702cf6da 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
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 <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
84 <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
85 </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
86 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
87 <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
88 </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
89
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 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
91 """
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 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
93 """
61
33c2702cf6da Use a different namespace than Kid uses.
cmlenz
parents: 54
diff changeset
94 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
95 <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
96 <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
97 <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
98 <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
99 </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
100 </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
101 </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
102 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
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 <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
105 <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
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 </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
109
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 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
111 """
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 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
113 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
114 """
61
33c2702cf6da Use a different namespace than Kid uses.
cmlenz
parents: 54
diff changeset
115 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
116 <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
117 <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
118 </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
119 </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
120 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
121 <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
122 </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
123
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
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
125 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
126 """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
127
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
128 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
129 """
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
130 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
131 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
132 """
61
33c2702cf6da Use a different namespace than Kid uses.
cmlenz
parents: 54
diff changeset
133 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
134 <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
135 <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
136 </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
137 ${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
138 </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
139 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
140 <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
141 </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
142
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
51
a572b1018b66 Fix `py:for` directive when combined with other directives (such as `py:strip`).
cmlenz
parents: 50
diff changeset
144 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
145 """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
146
a572b1018b66 Fix `py:for` directive when combined with other directives (such as `py:strip`).
cmlenz
parents: 50
diff changeset
147 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
148 """
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
149 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
150 correctly.
51
a572b1018b66 Fix `py:for` directive when combined with other directives (such as `py:strip`).
cmlenz
parents: 50
diff changeset
151 """
61
33c2702cf6da Use a different namespace than Kid uses.
cmlenz
parents: 54
diff changeset
152 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
153 <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
154 <b>${item}</b>
a572b1018b66 Fix `py:for` directive when combined with other directives (such as `py:strip`).
cmlenz
parents: 50
diff changeset
155 </div>
a572b1018b66 Fix `py:for` directive when combined with other directives (such as `py:strip`).
cmlenz
parents: 50
diff changeset
156 </doc>""")
a572b1018b66 Fix `py:for` directive when combined with other directives (such as `py:strip`).
cmlenz
parents: 50
diff changeset
157 self.assertEqual("""<doc>
a572b1018b66 Fix `py:for` directive when combined with other directives (such as `py:strip`).
cmlenz
parents: 50
diff changeset
158 <b>1</b>
a572b1018b66 Fix `py:for` directive when combined with other directives (such as `py:strip`).
cmlenz
parents: 50
diff changeset
159 <b>2</b>
a572b1018b66 Fix `py:for` directive when combined with other directives (such as `py:strip`).
cmlenz
parents: 50
diff changeset
160 <b>3</b>
a572b1018b66 Fix `py:for` directive when combined with other directives (such as `py:strip`).
cmlenz
parents: 50
diff changeset
161 <b>4</b>
a572b1018b66 Fix `py:for` directive when combined with other directives (such as `py:strip`).
cmlenz
parents: 50
diff changeset
162 <b>5</b>
a572b1018b66 Fix `py:for` directive when combined with other directives (such as `py:strip`).
cmlenz
parents: 50
diff changeset
163 </doc>""", str(tmpl.generate(Context(items=range(1, 6)))))
a572b1018b66 Fix `py:for` directive when combined with other directives (such as `py:strip`).
cmlenz
parents: 50
diff changeset
164
a572b1018b66 Fix `py:for` directive when combined with other directives (such as `py:strip`).
cmlenz
parents: 50
diff changeset
165
36
57d607f25484 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
166 class MatchDirectiveTestCase(unittest.TestCase):
57d607f25484 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
167 """Tests for the `py:match` template directive."""
57d607f25484 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
168
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
169 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
170 """
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
171 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
172 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
173 """
61
33c2702cf6da Use a different namespace than Kid uses.
cmlenz
parents: 54
diff changeset
174 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
175 <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
176 <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
177 </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
178 <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
179 </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
180 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
181 <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
182 </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
183
36
57d607f25484 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
184 def test_without_strip(self):
57d607f25484 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
185 """
57d607f25484 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
186 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
187 it matched without entering an infinite recursion.
57d607f25484 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
188 """
61
33c2702cf6da Use a different namespace than Kid uses.
cmlenz
parents: 54
diff changeset
189 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
190 <elem py:match="elem">
57d607f25484 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
191 <div class="elem">${select('*/text()')}</div>
57d607f25484 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
192 </elem>
57d607f25484 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
193 <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
194 </doc>""")
36
57d607f25484 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
195 self.assertEqual("""<doc>
57d607f25484 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
196 <elem>
57d607f25484 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
197 <div class="elem">Hey Joe</div>
57d607f25484 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
198 </elem>
57d607f25484 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
199 </doc>""", str(tmpl.generate()))
57d607f25484 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
200
57d607f25484 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
201 def test_recursive_match_1(self):
57d607f25484 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
202 """
57d607f25484 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
203 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
204 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
205 """
61
33c2702cf6da Use a different namespace than Kid uses.
cmlenz
parents: 54
diff changeset
206 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
207 <elem py:match="elem">
57d607f25484 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
208 <div class="elem">
57d607f25484 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
209 ${select('*/*')}
57d607f25484 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
210 </div>
57d607f25484 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
211 </elem>
57d607f25484 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
212 <elem>
57d607f25484 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
213 <subelem>
57d607f25484 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
214 <elem/>
57d607f25484 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
215 </subelem>
57d607f25484 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
216 </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
217 </doc>""")
36
57d607f25484 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
218 self.assertEqual("""<doc>
57d607f25484 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
219 <elem>
57d607f25484 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
220 <div class="elem">
57d607f25484 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
221 <subelem>
57d607f25484 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
222 <elem>
57d607f25484 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
223 <div class="elem">
57d607f25484 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
224 </div>
57d607f25484 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
225 </elem>
57d607f25484 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
226 </subelem>
57d607f25484 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
227 </div>
57d607f25484 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
228 </elem>
57d607f25484 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
229 </doc>""", str(tmpl.generate()))
57d607f25484 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
230
57d607f25484 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
231 def test_recursive_match_2(self):
57d607f25484 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
232 """
57d607f25484 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
233 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
234 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
235 more complex, but should work.
57d607f25484 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
236 """
61
33c2702cf6da Use a different namespace than Kid uses.
cmlenz
parents: 54
diff changeset
237 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
238 <body py:match="body">
57d607f25484 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
239 <div id="header"/>
57d607f25484 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
240 ${select('*/*')}
57d607f25484 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
241 </body>
57d607f25484 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
242 <body py:match="body">
57d607f25484 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
243 ${select('*/*')}
57d607f25484 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
244 <div id="footer"/>
57d607f25484 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
245 </body>
57d607f25484 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
246 <body>
57d607f25484 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
247 <h1>Foo</h1>
57d607f25484 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
248 </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
249 </html>""")
36
57d607f25484 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
250 self.assertEqual("""<html>
57d607f25484 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
251 <body>
57d607f25484 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
252 <div id="header"/><h1>Foo</h1>
57d607f25484 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
253 <div id="footer"/>
57d607f25484 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
254 </body>
57d607f25484 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
255 </html>""", str(tmpl.generate()))
57d607f25484 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
256
57d607f25484 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
257
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
258 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
259 """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
260
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
261 def test_strip_false(self):
61
33c2702cf6da Use a different namespace than Kid uses.
cmlenz
parents: 54
diff changeset
262 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
263 <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
264 </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
265 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
266 <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
267 </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
268
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
269 def test_strip_empty(self):
61
33c2702cf6da Use a different namespace than Kid uses.
cmlenz
parents: 54
diff changeset
270 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
271 <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
272 </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
273 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
274 <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
275 </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
276
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
277
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
278 class TemplateTestCase(unittest.TestCase):
36
57d607f25484 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
279 """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
280 reporting.
57d607f25484 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
281 """
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
282
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
283 def test_interpolate_string(self):
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
284 parts = list(Template._interpolate('bla'))
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
285 self.assertEqual(1, len(parts))
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
286 self.assertEqual(Stream.TEXT, parts[0][0])
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
287 self.assertEqual('bla', parts[0][1])
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
288
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
289 def test_interpolate_simple(self):
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
290 parts = list(Template._interpolate('${bla}'))
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
291 self.assertEqual(1, len(parts))
10
c5890ef863ba Moved the template-specific stream event kinds into the template module.
cmlenz
parents: 1
diff changeset
292 self.assertEqual(Template.EXPR, parts[0][0])
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
293 self.assertEqual('bla', parts[0][1].source)
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
294
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
295 def test_interpolate_escaped(self):
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
296 parts = list(Template._interpolate('$${bla}'))
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
297 self.assertEqual(1, len(parts))
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
298 self.assertEqual(Stream.TEXT, parts[0][0])
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
299 self.assertEqual('${bla}', parts[0][1])
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
300
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
301 def test_interpolate_short(self):
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
302 parts = list(Template._interpolate('$bla'))
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
303 self.assertEqual(1, len(parts))
10
c5890ef863ba Moved the template-specific stream event kinds into the template module.
cmlenz
parents: 1
diff changeset
304 self.assertEqual(Template.EXPR, parts[0][0])
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
305 self.assertEqual('bla', parts[0][1].source)
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
306
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
307 def test_interpolate_mixed1(self):
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
308 parts = list(Template._interpolate('$foo bar $baz'))
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
309 self.assertEqual(3, len(parts))
10
c5890ef863ba Moved the template-specific stream event kinds into the template module.
cmlenz
parents: 1
diff changeset
310 self.assertEqual(Template.EXPR, parts[0][0])
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
311 self.assertEqual('foo', parts[0][1].source)
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
312 self.assertEqual(Stream.TEXT, parts[1][0])
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
313 self.assertEqual(' bar ', parts[1][1])
10
c5890ef863ba Moved the template-specific stream event kinds into the template module.
cmlenz
parents: 1
diff changeset
314 self.assertEqual(Template.EXPR, parts[2][0])
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
315 self.assertEqual('baz', parts[2][1].source)
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
316
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
317 def test_interpolate_mixed2(self):
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
318 parts = list(Template._interpolate('foo $bar baz'))
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
319 self.assertEqual(3, len(parts))
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
320 self.assertEqual(Stream.TEXT, parts[0][0])
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
321 self.assertEqual('foo ', parts[0][1])
10
c5890ef863ba Moved the template-specific stream event kinds into the template module.
cmlenz
parents: 1
diff changeset
322 self.assertEqual(Template.EXPR, parts[1][0])
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
323 self.assertEqual('bar', parts[1][1].source)
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
324 self.assertEqual(Stream.TEXT, parts[2][0])
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
325 self.assertEqual(' baz', parts[2][1])
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
326
48
06c642ba2b08 convert the result of expressions in attributes to strings so that values like ints are output correctly
mgood
parents: 37
diff changeset
327 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
328 ctxt = Context()
06c642ba2b08 convert the result of expressions in attributes to strings so that values like ints are output correctly
mgood
parents: 37
diff changeset
329 tmpl = Template('<root attr="${1}"/>')
06c642ba2b08 convert the result of expressions in attributes to strings so that values like ints are output correctly
mgood
parents: 37
diff changeset
330 self.assertEqual('<root attr="1"/>', str(tmpl.generate(ctxt)))
06c642ba2b08 convert the result of expressions in attributes to strings so that values like ints are output correctly
mgood
parents: 37
diff changeset
331
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
332 def test_bad_directive_error(self):
61
33c2702cf6da Use a different namespace than Kid uses.
cmlenz
parents: 54
diff changeset
333 xml = '<p xmlns:py="http://markup.edgewall.org/" py:do="nothing" />'
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
334 try:
21
eca77129518a * Include paths are now interpreted relative to the path of the including template. Closes #3.
cmlenz
parents: 10
diff changeset
335 tmpl = Template(xml, filename='test.html')
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
336 except BadDirectiveError, e:
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
337 self.assertEqual('test.html', e.filename)
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
338 if sys.version_info[:2] >= (2, 4):
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
339 self.assertEqual(1, e.lineno)
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
340
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
341 def test_directive_value_syntax_error(self):
61
33c2702cf6da Use a different namespace than Kid uses.
cmlenz
parents: 54
diff changeset
342 xml = '<p xmlns:py="http://markup.edgewall.org/" py:if="bar\'" />'
21
eca77129518a * Include paths are now interpreted relative to the path of the including template. Closes #3.
cmlenz
parents: 10
diff changeset
343 tmpl = Template(xml, filename='test.html')
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
344 try:
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
345 list(tmpl.generate(Context()))
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
346 self.fail('Expected SyntaxError')
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
347 except TemplateSyntaxError, e:
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
348 self.assertEqual('test.html', e.filename)
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
349 if sys.version_info[:2] >= (2, 4):
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
350 self.assertEqual(1, e.lineno)
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
351 # We don't really care about the offset here, do we?
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
352
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
353 def test_expression_syntax_error(self):
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
354 xml = '<p>\n Foo <em>${bar"}</em>\n</p>'
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
355 tmpl = Template(xml, filename='test.html')
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
356 ctxt = Context(bar='baz')
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
357 try:
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
358 list(tmpl.generate(ctxt))
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
359 self.fail('Expected SyntaxError')
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
360 except TemplateSyntaxError, e:
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
361 self.assertEqual('test.html', e.filename)
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
362 if sys.version_info[:2] >= (2, 4):
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
363 self.assertEqual(2, e.lineno)
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
364 self.assertEqual(10, e.offset)
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
365
54
01981cbc7575 Fix a number of escaping problems:
cmlenz
parents: 53
diff changeset
366 def test_markup_noescape(self):
01981cbc7575 Fix a number of escaping problems:
cmlenz
parents: 53
diff changeset
367 """
01981cbc7575 Fix a number of escaping problems:
cmlenz
parents: 53
diff changeset
368 Verify that outputting context data that is a `Markup` instance is not
01981cbc7575 Fix a number of escaping problems:
cmlenz
parents: 53
diff changeset
369 escaped.
01981cbc7575 Fix a number of escaping problems:
cmlenz
parents: 53
diff changeset
370 """
61
33c2702cf6da Use a different namespace than Kid uses.
cmlenz
parents: 54
diff changeset
371 tmpl = Template("""<div xmlns:py="http://markup.edgewall.org/">
54
01981cbc7575 Fix a number of escaping problems:
cmlenz
parents: 53
diff changeset
372 $myvar
01981cbc7575 Fix a number of escaping problems:
cmlenz
parents: 53
diff changeset
373 </div>""")
01981cbc7575 Fix a number of escaping problems:
cmlenz
parents: 53
diff changeset
374 self.assertEqual("""<div>
01981cbc7575 Fix a number of escaping problems:
cmlenz
parents: 53
diff changeset
375 <b>foo</b>
01981cbc7575 Fix a number of escaping problems:
cmlenz
parents: 53
diff changeset
376 </div>""", str(tmpl.generate(Context(myvar=Markup('<b>foo</b>')))))
01981cbc7575 Fix a number of escaping problems:
cmlenz
parents: 53
diff changeset
377
01981cbc7575 Fix a number of escaping problems:
cmlenz
parents: 53
diff changeset
378 def test_text_noescape_quotes(self):
01981cbc7575 Fix a number of escaping problems:
cmlenz
parents: 53
diff changeset
379 """
01981cbc7575 Fix a number of escaping problems:
cmlenz
parents: 53
diff changeset
380 Verify that outputting context data in text nodes doesn't escape quotes.
01981cbc7575 Fix a number of escaping problems:
cmlenz
parents: 53
diff changeset
381 """
61
33c2702cf6da Use a different namespace than Kid uses.
cmlenz
parents: 54
diff changeset
382 tmpl = Template("""<div xmlns:py="http://markup.edgewall.org/">
54
01981cbc7575 Fix a number of escaping problems:
cmlenz
parents: 53
diff changeset
383 $myvar
01981cbc7575 Fix a number of escaping problems:
cmlenz
parents: 53
diff changeset
384 </div>""")
01981cbc7575 Fix a number of escaping problems:
cmlenz
parents: 53
diff changeset
385 self.assertEqual("""<div>
01981cbc7575 Fix a number of escaping problems:
cmlenz
parents: 53
diff changeset
386 "foo"
01981cbc7575 Fix a number of escaping problems:
cmlenz
parents: 53
diff changeset
387 </div>""", str(tmpl.generate(Context(myvar='"foo"'))))
01981cbc7575 Fix a number of escaping problems:
cmlenz
parents: 53
diff changeset
388
01981cbc7575 Fix a number of escaping problems:
cmlenz
parents: 53
diff changeset
389 def test_attr_escape_quotes(self):
01981cbc7575 Fix a number of escaping problems:
cmlenz
parents: 53
diff changeset
390 """
01981cbc7575 Fix a number of escaping problems:
cmlenz
parents: 53
diff changeset
391 Verify that outputting context data in attribtes escapes quotes.
01981cbc7575 Fix a number of escaping problems:
cmlenz
parents: 53
diff changeset
392 """
61
33c2702cf6da Use a different namespace than Kid uses.
cmlenz
parents: 54
diff changeset
393 tmpl = Template("""<div xmlns:py="http://markup.edgewall.org/">
54
01981cbc7575 Fix a number of escaping problems:
cmlenz
parents: 53
diff changeset
394 <elem class="$myvar"/>
01981cbc7575 Fix a number of escaping problems:
cmlenz
parents: 53
diff changeset
395 </div>""")
01981cbc7575 Fix a number of escaping problems:
cmlenz
parents: 53
diff changeset
396 self.assertEqual("""<div>
01981cbc7575 Fix a number of escaping problems:
cmlenz
parents: 53
diff changeset
397 <elem class="&#34;foo&#34;"/>
01981cbc7575 Fix a number of escaping problems:
cmlenz
parents: 53
diff changeset
398 </div>""", str(tmpl.generate(Context(myvar='"foo"'))))
01981cbc7575 Fix a number of escaping problems:
cmlenz
parents: 53
diff changeset
399
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
400
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
401 def suite():
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
402 suite = unittest.TestSuite()
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
403 suite.addTest(doctest.DocTestSuite(Template.__module__))
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
404 suite.addTest(unittest.makeSuite(TemplateTestCase, 'test'))
50
a053ffb834cb Fix the way multiple directives are applied to a single `SUB` in many cases by making the directives themselves responsible for applying any remaining directives.
cmlenz
parents: 48
diff changeset
405 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
406 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
407 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
408 suite.addTest(unittest.makeSuite(ForDirectiveTestCase, 'test'))
36
57d607f25484 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
409 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
410 suite.addTest(unittest.makeSuite(StripDirectiveTestCase, 'test'))
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
411 return suite
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
412
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
413 if __name__ == '__main__':
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
414 unittest.main(defaultTest='suite')
Copyright (C) 2012-2017 Edgewall Software