annotate markup/tests/template.py @ 227:96a7e5011c69 trunk

Use list comprehension instead of `map()` in the AST transformer.
author cmlenz
date Fri, 08 Sep 2006 09:17:42 +0000
parents f4943d2babda
children
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
152
cdb2a1f930e2 Add some tests for relative template includes (see #27).
cmlenz
parents: 149
diff changeset
15 import os
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
16 import unittest
152
cdb2a1f930e2 Add some tests for relative template includes (see #27).
cmlenz
parents: 149
diff changeset
17 import shutil
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
18 import sys
152
cdb2a1f930e2 Add some tests for relative template includes (see #27).
cmlenz
parents: 149
diff changeset
19 import tempfile
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
20
54
1f3cd91325d9 Fix a number of escaping problems:
cmlenz
parents: 53
diff changeset
21 from markup.core import Markup, Stream
152
cdb2a1f930e2 Add some tests for relative template includes (see #27).
cmlenz
parents: 149
diff changeset
22 from markup.template import BadDirectiveError, Template, TemplateLoader, \
cdb2a1f930e2 Add some tests for relative template includes (see #27).
cmlenz
parents: 149
diff changeset
23 TemplateSyntaxError
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
24
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
25
50
d3842cd76e92 Fix the way multiple directives are applied to a single `SUB` in many cases by making the directives themselves responsible for applying any remaining directives.
cmlenz
parents: 48
diff changeset
26 class AttrsDirectiveTestCase(unittest.TestCase):
d3842cd76e92 Fix the way multiple directives are applied to a single `SUB` in many cases by making the directives themselves responsible for applying any remaining directives.
cmlenz
parents: 48
diff changeset
27 """Tests for the `py:attrs` template directive."""
d3842cd76e92 Fix the way multiple directives are applied to a single `SUB` in many cases by making the directives themselves responsible for applying any remaining directives.
cmlenz
parents: 48
diff changeset
28
d3842cd76e92 Fix the way multiple directives are applied to a single `SUB` in many cases by making the directives themselves responsible for applying any remaining directives.
cmlenz
parents: 48
diff changeset
29 def test_combined_with_loop(self):
d3842cd76e92 Fix the way multiple directives are applied to a single `SUB` in many cases by making the directives themselves responsible for applying any remaining directives.
cmlenz
parents: 48
diff changeset
30 """
d3842cd76e92 Fix the way multiple directives are applied to a single `SUB` in many cases by making the directives themselves responsible for applying any remaining directives.
cmlenz
parents: 48
diff changeset
31 Verify that the directive has access to the loop variables.
d3842cd76e92 Fix the way multiple directives are applied to a single `SUB` in many cases by making the directives themselves responsible for applying any remaining directives.
cmlenz
parents: 48
diff changeset
32 """
61
448792ab1303 Use a different namespace than Kid uses.
cmlenz
parents: 54
diff changeset
33 tmpl = Template("""<doc xmlns:py="http://markup.edgewall.org/">
50
d3842cd76e92 Fix the way multiple directives are applied to a single `SUB` in many cases by making the directives themselves responsible for applying any remaining directives.
cmlenz
parents: 48
diff changeset
34 <elem py:for="item in items" py:attrs="item"/>
d3842cd76e92 Fix the way multiple directives are applied to a single `SUB` in many cases by making the directives themselves responsible for applying any remaining directives.
cmlenz
parents: 48
diff changeset
35 </doc>""")
d3842cd76e92 Fix the way multiple directives are applied to a single `SUB` in many cases by making the directives themselves responsible for applying any remaining directives.
cmlenz
parents: 48
diff changeset
36 items = [{'id': 1, 'class': 'foo'}, {'id': 2, 'class': 'bar'}]
d3842cd76e92 Fix the way multiple directives are applied to a single `SUB` in many cases by making the directives themselves responsible for applying any remaining directives.
cmlenz
parents: 48
diff changeset
37 self.assertEqual("""<doc>
d3842cd76e92 Fix the way multiple directives are applied to a single `SUB` in many cases by making the directives themselves responsible for applying any remaining directives.
cmlenz
parents: 48
diff changeset
38 <elem id="1" class="foo"/><elem id="2" class="bar"/>
149
537f819c547b `Template.generate()` now accepts the context data as keyword arguments, so that you don't have to import the `Context` class every time you want to pass data into a template.
cmlenz
parents: 145
diff changeset
39 </doc>""", str(tmpl.generate(items=items)))
50
d3842cd76e92 Fix the way multiple directives are applied to a single `SUB` in many cases by making the directives themselves responsible for applying any remaining directives.
cmlenz
parents: 48
diff changeset
40
54
1f3cd91325d9 Fix a number of escaping problems:
cmlenz
parents: 53
diff changeset
41 def test_update_existing_attr(self):
1f3cd91325d9 Fix a number of escaping problems:
cmlenz
parents: 53
diff changeset
42 """
1f3cd91325d9 Fix a number of escaping problems:
cmlenz
parents: 53
diff changeset
43 Verify that an attribute value that evaluates to `None` removes an
1f3cd91325d9 Fix a number of escaping problems:
cmlenz
parents: 53
diff changeset
44 existing attribute of that name.
1f3cd91325d9 Fix a number of escaping problems:
cmlenz
parents: 53
diff changeset
45 """
61
448792ab1303 Use a different namespace than Kid uses.
cmlenz
parents: 54
diff changeset
46 tmpl = Template("""<doc xmlns:py="http://markup.edgewall.org/">
54
1f3cd91325d9 Fix a number of escaping problems:
cmlenz
parents: 53
diff changeset
47 <elem class="foo" py:attrs="{'class': 'bar'}"/>
1f3cd91325d9 Fix a number of escaping problems:
cmlenz
parents: 53
diff changeset
48 </doc>""")
1f3cd91325d9 Fix a number of escaping problems:
cmlenz
parents: 53
diff changeset
49 self.assertEqual("""<doc>
1f3cd91325d9 Fix a number of escaping problems:
cmlenz
parents: 53
diff changeset
50 <elem class="bar"/>
1f3cd91325d9 Fix a number of escaping problems:
cmlenz
parents: 53
diff changeset
51 </doc>""", str(tmpl.generate()))
1f3cd91325d9 Fix a number of escaping problems:
cmlenz
parents: 53
diff changeset
52
1f3cd91325d9 Fix a number of escaping problems:
cmlenz
parents: 53
diff changeset
53 def test_remove_existing_attr(self):
1f3cd91325d9 Fix a number of escaping problems:
cmlenz
parents: 53
diff changeset
54 """
1f3cd91325d9 Fix a number of escaping problems:
cmlenz
parents: 53
diff changeset
55 Verify that an attribute value that evaluates to `None` removes an
1f3cd91325d9 Fix a number of escaping problems:
cmlenz
parents: 53
diff changeset
56 existing attribute of that name.
1f3cd91325d9 Fix a number of escaping problems:
cmlenz
parents: 53
diff changeset
57 """
61
448792ab1303 Use a different namespace than Kid uses.
cmlenz
parents: 54
diff changeset
58 tmpl = Template("""<doc xmlns:py="http://markup.edgewall.org/">
54
1f3cd91325d9 Fix a number of escaping problems:
cmlenz
parents: 53
diff changeset
59 <elem class="foo" py:attrs="{'class': None}"/>
1f3cd91325d9 Fix a number of escaping problems:
cmlenz
parents: 53
diff changeset
60 </doc>""")
1f3cd91325d9 Fix a number of escaping problems:
cmlenz
parents: 53
diff changeset
61 self.assertEqual("""<doc>
1f3cd91325d9 Fix a number of escaping problems:
cmlenz
parents: 53
diff changeset
62 <elem/>
1f3cd91325d9 Fix a number of escaping problems:
cmlenz
parents: 53
diff changeset
63 </doc>""", str(tmpl.generate()))
1f3cd91325d9 Fix a number of escaping problems:
cmlenz
parents: 53
diff changeset
64
50
d3842cd76e92 Fix the way multiple directives are applied to a single `SUB` in many cases by making the directives themselves responsible for applying any remaining directives.
cmlenz
parents: 48
diff changeset
65
53
512eb72dbb19 * Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents: 51
diff changeset
66 class ChooseDirectiveTestCase(unittest.TestCase):
512eb72dbb19 * Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents: 51
diff changeset
67 """Tests for the `py:choose` template directive and the complementary
512eb72dbb19 * Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents: 51
diff changeset
68 directives `py:when` and `py:otherwise`."""
512eb72dbb19 * Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents: 51
diff changeset
69
512eb72dbb19 * Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents: 51
diff changeset
70 def test_multiple_true_whens(self):
512eb72dbb19 * Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents: 51
diff changeset
71 """
512eb72dbb19 * Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents: 51
diff changeset
72 Verify that, if multiple `py:when` bodies match, only the first is
512eb72dbb19 * Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents: 51
diff changeset
73 output.
512eb72dbb19 * Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents: 51
diff changeset
74 """
61
448792ab1303 Use a different namespace than Kid uses.
cmlenz
parents: 54
diff changeset
75 tmpl = Template("""<div xmlns:py="http://markup.edgewall.org/" py:choose="">
53
512eb72dbb19 * Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents: 51
diff changeset
76 <span py:when="1 == 1">1</span>
512eb72dbb19 * Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents: 51
diff changeset
77 <span py:when="2 == 2">2</span>
512eb72dbb19 * Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents: 51
diff changeset
78 <span py:when="3 == 3">3</span>
512eb72dbb19 * Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents: 51
diff changeset
79 </div>""")
512eb72dbb19 * Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents: 51
diff changeset
80 self.assertEqual("""<div>
512eb72dbb19 * Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents: 51
diff changeset
81 <span>1</span>
512eb72dbb19 * Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents: 51
diff changeset
82 </div>""", str(tmpl.generate()))
512eb72dbb19 * Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents: 51
diff changeset
83
512eb72dbb19 * Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents: 51
diff changeset
84 def test_otherwise(self):
61
448792ab1303 Use a different namespace than Kid uses.
cmlenz
parents: 54
diff changeset
85 tmpl = Template("""<div xmlns:py="http://markup.edgewall.org/" py:choose="">
53
512eb72dbb19 * Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents: 51
diff changeset
86 <span py:when="False">hidden</span>
512eb72dbb19 * Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents: 51
diff changeset
87 <span py:otherwise="">hello</span>
512eb72dbb19 * Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents: 51
diff changeset
88 </div>""")
512eb72dbb19 * Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents: 51
diff changeset
89 self.assertEqual("""<div>
512eb72dbb19 * Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents: 51
diff changeset
90 <span>hello</span>
512eb72dbb19 * Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents: 51
diff changeset
91 </div>""", str(tmpl.generate()))
512eb72dbb19 * Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents: 51
diff changeset
92
512eb72dbb19 * Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents: 51
diff changeset
93 def test_nesting(self):
512eb72dbb19 * Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents: 51
diff changeset
94 """
512eb72dbb19 * Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents: 51
diff changeset
95 Verify that `py:choose` blocks can be nested:
512eb72dbb19 * Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents: 51
diff changeset
96 """
61
448792ab1303 Use a different namespace than Kid uses.
cmlenz
parents: 54
diff changeset
97 tmpl = Template("""<doc xmlns:py="http://markup.edgewall.org/">
53
512eb72dbb19 * Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents: 51
diff changeset
98 <div py:choose="1">
512eb72dbb19 * Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents: 51
diff changeset
99 <div py:when="1" py:choose="3">
512eb72dbb19 * Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents: 51
diff changeset
100 <span py:when="2">2</span>
512eb72dbb19 * Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents: 51
diff changeset
101 <span py:when="3">3</span>
512eb72dbb19 * Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents: 51
diff changeset
102 </div>
512eb72dbb19 * Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents: 51
diff changeset
103 </div>
512eb72dbb19 * Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents: 51
diff changeset
104 </doc>""")
512eb72dbb19 * Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents: 51
diff changeset
105 self.assertEqual("""<doc>
512eb72dbb19 * Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents: 51
diff changeset
106 <div>
512eb72dbb19 * Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents: 51
diff changeset
107 <div>
512eb72dbb19 * Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents: 51
diff changeset
108 <span>3</span>
512eb72dbb19 * Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents: 51
diff changeset
109 </div>
512eb72dbb19 * Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents: 51
diff changeset
110 </div>
512eb72dbb19 * Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents: 51
diff changeset
111 </doc>""", str(tmpl.generate()))
512eb72dbb19 * Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents: 51
diff changeset
112
202
92353f28ae54 Remove the (hopefully) last instance where directives store state in instance variables, allowing templates to be cached and reused in a threadsafe manner. Closes #39. Many thanks to Christian Boos for the patch!
cmlenz
parents: 191
diff changeset
113 def test_complex_nesting(self):
92353f28ae54 Remove the (hopefully) last instance where directives store state in instance variables, allowing templates to be cached and reused in a threadsafe manner. Closes #39. Many thanks to Christian Boos for the patch!
cmlenz
parents: 191
diff changeset
114 """
92353f28ae54 Remove the (hopefully) last instance where directives store state in instance variables, allowing templates to be cached and reused in a threadsafe manner. Closes #39. Many thanks to Christian Boos for the patch!
cmlenz
parents: 191
diff changeset
115 Verify more complex nesting.
92353f28ae54 Remove the (hopefully) last instance where directives store state in instance variables, allowing templates to be cached and reused in a threadsafe manner. Closes #39. Many thanks to Christian Boos for the patch!
cmlenz
parents: 191
diff changeset
116 """
92353f28ae54 Remove the (hopefully) last instance where directives store state in instance variables, allowing templates to be cached and reused in a threadsafe manner. Closes #39. Many thanks to Christian Boos for the patch!
cmlenz
parents: 191
diff changeset
117 tmpl = Template("""<doc xmlns:py="http://markup.edgewall.org/">
92353f28ae54 Remove the (hopefully) last instance where directives store state in instance variables, allowing templates to be cached and reused in a threadsafe manner. Closes #39. Many thanks to Christian Boos for the patch!
cmlenz
parents: 191
diff changeset
118 <div py:choose="1">
92353f28ae54 Remove the (hopefully) last instance where directives store state in instance variables, allowing templates to be cached and reused in a threadsafe manner. Closes #39. Many thanks to Christian Boos for the patch!
cmlenz
parents: 191
diff changeset
119 <div py:when="1" py:choose="">
92353f28ae54 Remove the (hopefully) last instance where directives store state in instance variables, allowing templates to be cached and reused in a threadsafe manner. Closes #39. Many thanks to Christian Boos for the patch!
cmlenz
parents: 191
diff changeset
120 <span py:when="2">OK</span>
92353f28ae54 Remove the (hopefully) last instance where directives store state in instance variables, allowing templates to be cached and reused in a threadsafe manner. Closes #39. Many thanks to Christian Boos for the patch!
cmlenz
parents: 191
diff changeset
121 <span py:when="1">FAIL</span>
92353f28ae54 Remove the (hopefully) last instance where directives store state in instance variables, allowing templates to be cached and reused in a threadsafe manner. Closes #39. Many thanks to Christian Boos for the patch!
cmlenz
parents: 191
diff changeset
122 </div>
92353f28ae54 Remove the (hopefully) last instance where directives store state in instance variables, allowing templates to be cached and reused in a threadsafe manner. Closes #39. Many thanks to Christian Boos for the patch!
cmlenz
parents: 191
diff changeset
123 </div>
92353f28ae54 Remove the (hopefully) last instance where directives store state in instance variables, allowing templates to be cached and reused in a threadsafe manner. Closes #39. Many thanks to Christian Boos for the patch!
cmlenz
parents: 191
diff changeset
124 </doc>""")
92353f28ae54 Remove the (hopefully) last instance where directives store state in instance variables, allowing templates to be cached and reused in a threadsafe manner. Closes #39. Many thanks to Christian Boos for the patch!
cmlenz
parents: 191
diff changeset
125 self.assertEqual("""<doc>
92353f28ae54 Remove the (hopefully) last instance where directives store state in instance variables, allowing templates to be cached and reused in a threadsafe manner. Closes #39. Many thanks to Christian Boos for the patch!
cmlenz
parents: 191
diff changeset
126 <div>
92353f28ae54 Remove the (hopefully) last instance where directives store state in instance variables, allowing templates to be cached and reused in a threadsafe manner. Closes #39. Many thanks to Christian Boos for the patch!
cmlenz
parents: 191
diff changeset
127 <div>
92353f28ae54 Remove the (hopefully) last instance where directives store state in instance variables, allowing templates to be cached and reused in a threadsafe manner. Closes #39. Many thanks to Christian Boos for the patch!
cmlenz
parents: 191
diff changeset
128 <span>OK</span>
92353f28ae54 Remove the (hopefully) last instance where directives store state in instance variables, allowing templates to be cached and reused in a threadsafe manner. Closes #39. Many thanks to Christian Boos for the patch!
cmlenz
parents: 191
diff changeset
129 </div>
92353f28ae54 Remove the (hopefully) last instance where directives store state in instance variables, allowing templates to be cached and reused in a threadsafe manner. Closes #39. Many thanks to Christian Boos for the patch!
cmlenz
parents: 191
diff changeset
130 </div>
92353f28ae54 Remove the (hopefully) last instance where directives store state in instance variables, allowing templates to be cached and reused in a threadsafe manner. Closes #39. Many thanks to Christian Boos for the patch!
cmlenz
parents: 191
diff changeset
131 </doc>""", str(tmpl.generate()))
92353f28ae54 Remove the (hopefully) last instance where directives store state in instance variables, allowing templates to be cached and reused in a threadsafe manner. Closes #39. Many thanks to Christian Boos for the patch!
cmlenz
parents: 191
diff changeset
132
92353f28ae54 Remove the (hopefully) last instance where directives store state in instance variables, allowing templates to be cached and reused in a threadsafe manner. Closes #39. Many thanks to Christian Boos for the patch!
cmlenz
parents: 191
diff changeset
133 def test_complex_nesting_otherwise(self):
92353f28ae54 Remove the (hopefully) last instance where directives store state in instance variables, allowing templates to be cached and reused in a threadsafe manner. Closes #39. Many thanks to Christian Boos for the patch!
cmlenz
parents: 191
diff changeset
134 """
92353f28ae54 Remove the (hopefully) last instance where directives store state in instance variables, allowing templates to be cached and reused in a threadsafe manner. Closes #39. Many thanks to Christian Boos for the patch!
cmlenz
parents: 191
diff changeset
135 Verify more complex nesting using otherwise.
92353f28ae54 Remove the (hopefully) last instance where directives store state in instance variables, allowing templates to be cached and reused in a threadsafe manner. Closes #39. Many thanks to Christian Boos for the patch!
cmlenz
parents: 191
diff changeset
136 """
92353f28ae54 Remove the (hopefully) last instance where directives store state in instance variables, allowing templates to be cached and reused in a threadsafe manner. Closes #39. Many thanks to Christian Boos for the patch!
cmlenz
parents: 191
diff changeset
137 tmpl = Template("""<doc xmlns:py="http://markup.edgewall.org/">
92353f28ae54 Remove the (hopefully) last instance where directives store state in instance variables, allowing templates to be cached and reused in a threadsafe manner. Closes #39. Many thanks to Christian Boos for the patch!
cmlenz
parents: 191
diff changeset
138 <div py:choose="1">
92353f28ae54 Remove the (hopefully) last instance where directives store state in instance variables, allowing templates to be cached and reused in a threadsafe manner. Closes #39. Many thanks to Christian Boos for the patch!
cmlenz
parents: 191
diff changeset
139 <div py:when="1" py:choose="2">
92353f28ae54 Remove the (hopefully) last instance where directives store state in instance variables, allowing templates to be cached and reused in a threadsafe manner. Closes #39. Many thanks to Christian Boos for the patch!
cmlenz
parents: 191
diff changeset
140 <span py:when="1">FAIL</span>
92353f28ae54 Remove the (hopefully) last instance where directives store state in instance variables, allowing templates to be cached and reused in a threadsafe manner. Closes #39. Many thanks to Christian Boos for the patch!
cmlenz
parents: 191
diff changeset
141 <span py:otherwise="">OK</span>
92353f28ae54 Remove the (hopefully) last instance where directives store state in instance variables, allowing templates to be cached and reused in a threadsafe manner. Closes #39. Many thanks to Christian Boos for the patch!
cmlenz
parents: 191
diff changeset
142 </div>
92353f28ae54 Remove the (hopefully) last instance where directives store state in instance variables, allowing templates to be cached and reused in a threadsafe manner. Closes #39. Many thanks to Christian Boos for the patch!
cmlenz
parents: 191
diff changeset
143 </div>
92353f28ae54 Remove the (hopefully) last instance where directives store state in instance variables, allowing templates to be cached and reused in a threadsafe manner. Closes #39. Many thanks to Christian Boos for the patch!
cmlenz
parents: 191
diff changeset
144 </doc>""")
92353f28ae54 Remove the (hopefully) last instance where directives store state in instance variables, allowing templates to be cached and reused in a threadsafe manner. Closes #39. Many thanks to Christian Boos for the patch!
cmlenz
parents: 191
diff changeset
145 self.assertEqual("""<doc>
92353f28ae54 Remove the (hopefully) last instance where directives store state in instance variables, allowing templates to be cached and reused in a threadsafe manner. Closes #39. Many thanks to Christian Boos for the patch!
cmlenz
parents: 191
diff changeset
146 <div>
92353f28ae54 Remove the (hopefully) last instance where directives store state in instance variables, allowing templates to be cached and reused in a threadsafe manner. Closes #39. Many thanks to Christian Boos for the patch!
cmlenz
parents: 191
diff changeset
147 <div>
92353f28ae54 Remove the (hopefully) last instance where directives store state in instance variables, allowing templates to be cached and reused in a threadsafe manner. Closes #39. Many thanks to Christian Boos for the patch!
cmlenz
parents: 191
diff changeset
148 <span>OK</span>
92353f28ae54 Remove the (hopefully) last instance where directives store state in instance variables, allowing templates to be cached and reused in a threadsafe manner. Closes #39. Many thanks to Christian Boos for the patch!
cmlenz
parents: 191
diff changeset
149 </div>
92353f28ae54 Remove the (hopefully) last instance where directives store state in instance variables, allowing templates to be cached and reused in a threadsafe manner. Closes #39. Many thanks to Christian Boos for the patch!
cmlenz
parents: 191
diff changeset
150 </div>
92353f28ae54 Remove the (hopefully) last instance where directives store state in instance variables, allowing templates to be cached and reused in a threadsafe manner. Closes #39. Many thanks to Christian Boos for the patch!
cmlenz
parents: 191
diff changeset
151 </doc>""", str(tmpl.generate()))
92353f28ae54 Remove the (hopefully) last instance where directives store state in instance variables, allowing templates to be cached and reused in a threadsafe manner. Closes #39. Many thanks to Christian Boos for the patch!
cmlenz
parents: 191
diff changeset
152
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
153 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
154 """
512eb72dbb19 * Add helper function to let directives apply any remaining directives, and use that helper consistently in every directive.
cmlenz
parents: 51
diff changeset
155 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
156 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
157 """
61
448792ab1303 Use a different namespace than Kid uses.
cmlenz
parents: 54
diff changeset
158 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
159 <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
160 <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
161 </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
162 </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
163 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
164 <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
165 </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
166
166
d43f50402cf2 Better error reporting for errors in directive expressions, and when `py:otherwise`/`py:when` are used outside a `py:choose` directive. Thanks to Christian Boos for the initial patch.
cmlenz
parents: 165
diff changeset
167 def test_when_outside_choose(self):
d43f50402cf2 Better error reporting for errors in directive expressions, and when `py:otherwise`/`py:when` are used outside a `py:choose` directive. Thanks to Christian Boos for the initial patch.
cmlenz
parents: 165
diff changeset
168 """
d43f50402cf2 Better error reporting for errors in directive expressions, and when `py:otherwise`/`py:when` are used outside a `py:choose` directive. Thanks to Christian Boos for the initial patch.
cmlenz
parents: 165
diff changeset
169 Verify that a `when` directive outside of a `choose` directive is
d43f50402cf2 Better error reporting for errors in directive expressions, and when `py:otherwise`/`py:when` are used outside a `py:choose` directive. Thanks to Christian Boos for the initial patch.
cmlenz
parents: 165
diff changeset
170 reported as an error.
d43f50402cf2 Better error reporting for errors in directive expressions, and when `py:otherwise`/`py:when` are used outside a `py:choose` directive. Thanks to Christian Boos for the initial patch.
cmlenz
parents: 165
diff changeset
171 """
d43f50402cf2 Better error reporting for errors in directive expressions, and when `py:otherwise`/`py:when` are used outside a `py:choose` directive. Thanks to Christian Boos for the initial patch.
cmlenz
parents: 165
diff changeset
172 tmpl = Template("""<doc xmlns:py="http://markup.edgewall.org/">
d43f50402cf2 Better error reporting for errors in directive expressions, and when `py:otherwise`/`py:when` are used outside a `py:choose` directive. Thanks to Christian Boos for the initial patch.
cmlenz
parents: 165
diff changeset
173 <div py:when="xy" />
d43f50402cf2 Better error reporting for errors in directive expressions, and when `py:otherwise`/`py:when` are used outside a `py:choose` directive. Thanks to Christian Boos for the initial patch.
cmlenz
parents: 165
diff changeset
174 </doc>""")
d43f50402cf2 Better error reporting for errors in directive expressions, and when `py:otherwise`/`py:when` are used outside a `py:choose` directive. Thanks to Christian Boos for the initial patch.
cmlenz
parents: 165
diff changeset
175 self.assertRaises(TemplateSyntaxError, str, tmpl.generate())
d43f50402cf2 Better error reporting for errors in directive expressions, and when `py:otherwise`/`py:when` are used outside a `py:choose` directive. Thanks to Christian Boos for the initial patch.
cmlenz
parents: 165
diff changeset
176
181
e103b75a96ce Some error message improvements for template directives. Thanks to Christian Boos for the patch!
cmlenz
parents: 179
diff changeset
177 def test_otherwise_outside_choose(self):
166
d43f50402cf2 Better error reporting for errors in directive expressions, and when `py:otherwise`/`py:when` are used outside a `py:choose` directive. Thanks to Christian Boos for the initial patch.
cmlenz
parents: 165
diff changeset
178 """
d43f50402cf2 Better error reporting for errors in directive expressions, and when `py:otherwise`/`py:when` are used outside a `py:choose` directive. Thanks to Christian Boos for the initial patch.
cmlenz
parents: 165
diff changeset
179 Verify that an `otherwise` directive outside of a `choose` directive is
d43f50402cf2 Better error reporting for errors in directive expressions, and when `py:otherwise`/`py:when` are used outside a `py:choose` directive. Thanks to Christian Boos for the initial patch.
cmlenz
parents: 165
diff changeset
180 reported as an error.
d43f50402cf2 Better error reporting for errors in directive expressions, and when `py:otherwise`/`py:when` are used outside a `py:choose` directive. Thanks to Christian Boos for the initial patch.
cmlenz
parents: 165
diff changeset
181 """
d43f50402cf2 Better error reporting for errors in directive expressions, and when `py:otherwise`/`py:when` are used outside a `py:choose` directive. Thanks to Christian Boos for the initial patch.
cmlenz
parents: 165
diff changeset
182 tmpl = Template("""<doc xmlns:py="http://markup.edgewall.org/">
d43f50402cf2 Better error reporting for errors in directive expressions, and when `py:otherwise`/`py:when` are used outside a `py:choose` directive. Thanks to Christian Boos for the initial patch.
cmlenz
parents: 165
diff changeset
183 <div py:otherwise="" />
d43f50402cf2 Better error reporting for errors in directive expressions, and when `py:otherwise`/`py:when` are used outside a `py:choose` directive. Thanks to Christian Boos for the initial patch.
cmlenz
parents: 165
diff changeset
184 </doc>""")
d43f50402cf2 Better error reporting for errors in directive expressions, and when `py:otherwise`/`py:when` are used outside a `py:choose` directive. Thanks to Christian Boos for the initial patch.
cmlenz
parents: 165
diff changeset
185 self.assertRaises(TemplateSyntaxError, str, tmpl.generate())
d43f50402cf2 Better error reporting for errors in directive expressions, and when `py:otherwise`/`py:when` are used outside a `py:choose` directive. Thanks to Christian Boos for the initial patch.
cmlenz
parents: 165
diff changeset
186
181
e103b75a96ce Some error message improvements for template directives. Thanks to Christian Boos for the patch!
cmlenz
parents: 179
diff changeset
187 def test_when_without_test(self):
e103b75a96ce Some error message improvements for template directives. Thanks to Christian Boos for the patch!
cmlenz
parents: 179
diff changeset
188 """
e103b75a96ce Some error message improvements for template directives. Thanks to Christian Boos for the patch!
cmlenz
parents: 179
diff changeset
189 Verify that an `when` directive that doesn't have a `test` attribute
e103b75a96ce Some error message improvements for template directives. Thanks to Christian Boos for the patch!
cmlenz
parents: 179
diff changeset
190 is reported as an error.
e103b75a96ce Some error message improvements for template directives. Thanks to Christian Boos for the patch!
cmlenz
parents: 179
diff changeset
191 """
e103b75a96ce Some error message improvements for template directives. Thanks to Christian Boos for the patch!
cmlenz
parents: 179
diff changeset
192 tmpl = Template("""<doc xmlns:py="http://markup.edgewall.org/">
e103b75a96ce Some error message improvements for template directives. Thanks to Christian Boos for the patch!
cmlenz
parents: 179
diff changeset
193 <div py:choose="" py:strip="">
e103b75a96ce Some error message improvements for template directives. Thanks to Christian Boos for the patch!
cmlenz
parents: 179
diff changeset
194 <py:when>foo</py:when>
e103b75a96ce Some error message improvements for template directives. Thanks to Christian Boos for the patch!
cmlenz
parents: 179
diff changeset
195 </div>
e103b75a96ce Some error message improvements for template directives. Thanks to Christian Boos for the patch!
cmlenz
parents: 179
diff changeset
196 </doc>""")
e103b75a96ce Some error message improvements for template directives. Thanks to Christian Boos for the patch!
cmlenz
parents: 179
diff changeset
197 self.assertRaises(TemplateSyntaxError, str, tmpl.generate())
e103b75a96ce Some error message improvements for template directives. Thanks to Christian Boos for the patch!
cmlenz
parents: 179
diff changeset
198
e103b75a96ce Some error message improvements for template directives. Thanks to Christian Boos for the patch!
cmlenz
parents: 179
diff changeset
199 def test_otherwise_without_test(self):
e103b75a96ce Some error message improvements for template directives. Thanks to Christian Boos for the patch!
cmlenz
parents: 179
diff changeset
200 """
e103b75a96ce Some error message improvements for template directives. Thanks to Christian Boos for the patch!
cmlenz
parents: 179
diff changeset
201 Verify that an `otherwise` directive can be used without a `test`
e103b75a96ce Some error message improvements for template directives. Thanks to Christian Boos for the patch!
cmlenz
parents: 179
diff changeset
202 attribute.
e103b75a96ce Some error message improvements for template directives. Thanks to Christian Boos for the patch!
cmlenz
parents: 179
diff changeset
203 """
e103b75a96ce Some error message improvements for template directives. Thanks to Christian Boos for the patch!
cmlenz
parents: 179
diff changeset
204 tmpl = Template("""<doc xmlns:py="http://markup.edgewall.org/">
e103b75a96ce Some error message improvements for template directives. Thanks to Christian Boos for the patch!
cmlenz
parents: 179
diff changeset
205 <div py:choose="" py:strip="">
e103b75a96ce Some error message improvements for template directives. Thanks to Christian Boos for the patch!
cmlenz
parents: 179
diff changeset
206 <py:otherwise>foo</py:otherwise>
e103b75a96ce Some error message improvements for template directives. Thanks to Christian Boos for the patch!
cmlenz
parents: 179
diff changeset
207 </div>
e103b75a96ce Some error message improvements for template directives. Thanks to Christian Boos for the patch!
cmlenz
parents: 179
diff changeset
208 </doc>""")
e103b75a96ce Some error message improvements for template directives. Thanks to Christian Boos for the patch!
cmlenz
parents: 179
diff changeset
209 self.assertEqual("""<doc>
e103b75a96ce Some error message improvements for template directives. Thanks to Christian Boos for the patch!
cmlenz
parents: 179
diff changeset
210 foo
e103b75a96ce Some error message improvements for template directives. Thanks to Christian Boos for the patch!
cmlenz
parents: 179
diff changeset
211 </doc>""", str(tmpl.generate()))
e103b75a96ce Some error message improvements for template directives. Thanks to Christian Boos for the patch!
cmlenz
parents: 179
diff changeset
212
65
b3fdf93057ab Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents: 61
diff changeset
213 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
214 """
b3fdf93057ab Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents: 61
diff changeset
215 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
216 """
b3fdf93057ab Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents: 61
diff changeset
217 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
218 <py:choose>
b3fdf93057ab Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents: 61
diff changeset
219 <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
220 <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
221 <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
222 </py:choose>
b3fdf93057ab Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents: 61
diff changeset
223 </doc>""")
b3fdf93057ab Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents: 61
diff changeset
224 self.assertEqual("""<doc>
b3fdf93057ab Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents: 61
diff changeset
225 1
b3fdf93057ab Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents: 61
diff changeset
226 </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
227
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
228
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
229 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
230 """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
231
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
232 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
233 """
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
234 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
235 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
236 """
61
448792ab1303 Use a different namespace than Kid uses.
cmlenz
parents: 54
diff changeset
237 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
238 <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
239 <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
240 </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
241 ${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
242 </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
243 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
244 <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
245 </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
246
90
c835e81c50af When an expression evaluates to a callable, it is called implicitly.
cmlenz
parents: 89
diff changeset
247 def test_exec_in_replace(self):
c835e81c50af When an expression evaluates to a callable, it is called implicitly.
cmlenz
parents: 89
diff changeset
248 tmpl = Template("""<div xmlns:py="http://markup.edgewall.org/">
c835e81c50af When an expression evaluates to a callable, it is called implicitly.
cmlenz
parents: 89
diff changeset
249 <p py:def="echo(greeting, name='world')" class="message">
c835e81c50af When an expression evaluates to a callable, it is called implicitly.
cmlenz
parents: 89
diff changeset
250 ${greeting}, ${name}!
c835e81c50af When an expression evaluates to a callable, it is called implicitly.
cmlenz
parents: 89
diff changeset
251 </p>
c835e81c50af When an expression evaluates to a callable, it is called implicitly.
cmlenz
parents: 89
diff changeset
252 <div py:replace="echo('hello')"></div>
c835e81c50af When an expression evaluates to a callable, it is called implicitly.
cmlenz
parents: 89
diff changeset
253 </div>""")
c835e81c50af When an expression evaluates to a callable, it is called implicitly.
cmlenz
parents: 89
diff changeset
254 self.assertEqual("""<div>
c835e81c50af When an expression evaluates to a callable, it is called implicitly.
cmlenz
parents: 89
diff changeset
255 <p class="message">
c835e81c50af When an expression evaluates to a callable, it is called implicitly.
cmlenz
parents: 89
diff changeset
256 hello, world!
c835e81c50af When an expression evaluates to a callable, it is called implicitly.
cmlenz
parents: 89
diff changeset
257 </p>
c835e81c50af When an expression evaluates to a callable, it is called implicitly.
cmlenz
parents: 89
diff changeset
258 </div>""", str(tmpl.generate()))
c835e81c50af When an expression evaluates to a callable, it is called implicitly.
cmlenz
parents: 89
diff changeset
259
65
b3fdf93057ab Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents: 61
diff changeset
260 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
261 """
b3fdf93057ab Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents: 61
diff changeset
262 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
263 """
b3fdf93057ab Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents: 61
diff changeset
264 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
265 <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
266 <b>${what}</b>
b3fdf93057ab Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents: 61
diff changeset
267 </py:def>
b3fdf93057ab Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents: 61
diff changeset
268 ${echo('foo')}
b3fdf93057ab Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents: 61
diff changeset
269 </doc>""")
b3fdf93057ab Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents: 61
diff changeset
270 self.assertEqual("""<doc>
b3fdf93057ab Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents: 61
diff changeset
271 <b>foo</b>
b3fdf93057ab Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents: 61
diff changeset
272 </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
273
154
8bd5c8cd33e0 * Make sure `py:def` macros don't go out of scope if they are defined inside another directive.
cmlenz
parents: 152
diff changeset
274 def test_nested_defs(self):
8bd5c8cd33e0 * Make sure `py:def` macros don't go out of scope if they are defined inside another directive.
cmlenz
parents: 152
diff changeset
275 """
8bd5c8cd33e0 * Make sure `py:def` macros don't go out of scope if they are defined inside another directive.
cmlenz
parents: 152
diff changeset
276 Verify that a template function defined inside a conditional block can
8bd5c8cd33e0 * Make sure `py:def` macros don't go out of scope if they are defined inside another directive.
cmlenz
parents: 152
diff changeset
277 be called from outside that block.
8bd5c8cd33e0 * Make sure `py:def` macros don't go out of scope if they are defined inside another directive.
cmlenz
parents: 152
diff changeset
278 """
8bd5c8cd33e0 * Make sure `py:def` macros don't go out of scope if they are defined inside another directive.
cmlenz
parents: 152
diff changeset
279 tmpl = Template("""<doc xmlns:py="http://markup.edgewall.org/">
8bd5c8cd33e0 * Make sure `py:def` macros don't go out of scope if they are defined inside another directive.
cmlenz
parents: 152
diff changeset
280 <py:if test="semantic">
8bd5c8cd33e0 * Make sure `py:def` macros don't go out of scope if they are defined inside another directive.
cmlenz
parents: 152
diff changeset
281 <strong py:def="echo(what)">${what}</strong>
8bd5c8cd33e0 * Make sure `py:def` macros don't go out of scope if they are defined inside another directive.
cmlenz
parents: 152
diff changeset
282 </py:if>
8bd5c8cd33e0 * Make sure `py:def` macros don't go out of scope if they are defined inside another directive.
cmlenz
parents: 152
diff changeset
283 <py:if test="not semantic">
8bd5c8cd33e0 * Make sure `py:def` macros don't go out of scope if they are defined inside another directive.
cmlenz
parents: 152
diff changeset
284 <b py:def="echo(what)">${what}</b>
8bd5c8cd33e0 * Make sure `py:def` macros don't go out of scope if they are defined inside another directive.
cmlenz
parents: 152
diff changeset
285 </py:if>
8bd5c8cd33e0 * Make sure `py:def` macros don't go out of scope if they are defined inside another directive.
cmlenz
parents: 152
diff changeset
286 ${echo('foo')}
8bd5c8cd33e0 * Make sure `py:def` macros don't go out of scope if they are defined inside another directive.
cmlenz
parents: 152
diff changeset
287 </doc>""")
8bd5c8cd33e0 * Make sure `py:def` macros don't go out of scope if they are defined inside another directive.
cmlenz
parents: 152
diff changeset
288 self.assertEqual("""<doc>
8bd5c8cd33e0 * Make sure `py:def` macros don't go out of scope if they are defined inside another directive.
cmlenz
parents: 152
diff changeset
289 <strong>foo</strong>
8bd5c8cd33e0 * Make sure `py:def` macros don't go out of scope if they are defined inside another directive.
cmlenz
parents: 152
diff changeset
290 </doc>""", str(tmpl.generate(semantic=True)))
8bd5c8cd33e0 * Make sure `py:def` macros don't go out of scope if they are defined inside another directive.
cmlenz
parents: 152
diff changeset
291
165
54a4be707664 Fix handling of keyword arguments in `py:def` directive. Thanks to Christian Boos for reporting the problem and providing the basic patch for this change.
cmlenz
parents: 158
diff changeset
292 def test_function_with_default_arg(self):
54a4be707664 Fix handling of keyword arguments in `py:def` directive. Thanks to Christian Boos for reporting the problem and providing the basic patch for this change.
cmlenz
parents: 158
diff changeset
293 """
54a4be707664 Fix handling of keyword arguments in `py:def` directive. Thanks to Christian Boos for reporting the problem and providing the basic patch for this change.
cmlenz
parents: 158
diff changeset
294 Verify that keyword arguments work with `py:def` directives.
54a4be707664 Fix handling of keyword arguments in `py:def` directive. Thanks to Christian Boos for reporting the problem and providing the basic patch for this change.
cmlenz
parents: 158
diff changeset
295 """
54a4be707664 Fix handling of keyword arguments in `py:def` directive. Thanks to Christian Boos for reporting the problem and providing the basic patch for this change.
cmlenz
parents: 158
diff changeset
296 tmpl = Template("""<doc xmlns:py="http://markup.edgewall.org/">
54a4be707664 Fix handling of keyword arguments in `py:def` directive. Thanks to Christian Boos for reporting the problem and providing the basic patch for this change.
cmlenz
parents: 158
diff changeset
297 <b py:def="echo(what, bold=False)" py:strip="not bold">${what}</b>
54a4be707664 Fix handling of keyword arguments in `py:def` directive. Thanks to Christian Boos for reporting the problem and providing the basic patch for this change.
cmlenz
parents: 158
diff changeset
298 ${echo('foo')}
54a4be707664 Fix handling of keyword arguments in `py:def` directive. Thanks to Christian Boos for reporting the problem and providing the basic patch for this change.
cmlenz
parents: 158
diff changeset
299 </doc>""")
54a4be707664 Fix handling of keyword arguments in `py:def` directive. Thanks to Christian Boos for reporting the problem and providing the basic patch for this change.
cmlenz
parents: 158
diff changeset
300 self.assertEqual("""<doc>
54a4be707664 Fix handling of keyword arguments in `py:def` directive. Thanks to Christian Boos for reporting the problem and providing the basic patch for this change.
cmlenz
parents: 158
diff changeset
301 foo
54a4be707664 Fix handling of keyword arguments in `py:def` directive. Thanks to Christian Boos for reporting the problem and providing the basic patch for this change.
cmlenz
parents: 158
diff changeset
302 </doc>""", str(tmpl.generate()))
54a4be707664 Fix handling of keyword arguments in `py:def` directive. Thanks to Christian Boos for reporting the problem and providing the basic patch for this change.
cmlenz
parents: 158
diff changeset
303
185
95c3813a00de Fix for #34: `py:def` macros can now be invoked from within expressions in attribute values.
cmlenz
parents: 184
diff changeset
304 def test_invocation_in_attribute(self):
95c3813a00de Fix for #34: `py:def` macros can now be invoked from within expressions in attribute values.
cmlenz
parents: 184
diff changeset
305 tmpl = Template("""<doc xmlns:py="http://markup.edgewall.org/">
95c3813a00de Fix for #34: `py:def` macros can now be invoked from within expressions in attribute values.
cmlenz
parents: 184
diff changeset
306 <py:def function="echo(what)">${what or 'something'}</py:def>
95c3813a00de Fix for #34: `py:def` macros can now be invoked from within expressions in attribute values.
cmlenz
parents: 184
diff changeset
307 <p class="${echo('foo')}">bar</p>
95c3813a00de Fix for #34: `py:def` macros can now be invoked from within expressions in attribute values.
cmlenz
parents: 184
diff changeset
308 </doc>""")
95c3813a00de Fix for #34: `py:def` macros can now be invoked from within expressions in attribute values.
cmlenz
parents: 184
diff changeset
309 self.assertEqual("""<doc>
95c3813a00de Fix for #34: `py:def` macros can now be invoked from within expressions in attribute values.
cmlenz
parents: 184
diff changeset
310 <p class="foo">bar</p>
95c3813a00de Fix for #34: `py:def` macros can now be invoked from within expressions in attribute values.
cmlenz
parents: 184
diff changeset
311 </doc>""", str(tmpl.generate()))
95c3813a00de Fix for #34: `py:def` macros can now be invoked from within expressions in attribute values.
cmlenz
parents: 184
diff changeset
312
95c3813a00de Fix for #34: `py:def` macros can now be invoked from within expressions in attribute values.
cmlenz
parents: 184
diff changeset
313 def test_invocation_in_attribute_none(self):
95c3813a00de Fix for #34: `py:def` macros can now be invoked from within expressions in attribute values.
cmlenz
parents: 184
diff changeset
314 tmpl = Template("""<doc xmlns:py="http://markup.edgewall.org/">
95c3813a00de Fix for #34: `py:def` macros can now be invoked from within expressions in attribute values.
cmlenz
parents: 184
diff changeset
315 <py:def function="echo()">${None}</py:def>
95c3813a00de Fix for #34: `py:def` macros can now be invoked from within expressions in attribute values.
cmlenz
parents: 184
diff changeset
316 <p class="${echo()}">bar</p>
95c3813a00de Fix for #34: `py:def` macros can now be invoked from within expressions in attribute values.
cmlenz
parents: 184
diff changeset
317 </doc>""")
95c3813a00de Fix for #34: `py:def` macros can now be invoked from within expressions in attribute values.
cmlenz
parents: 184
diff changeset
318 self.assertEqual("""<doc>
95c3813a00de Fix for #34: `py:def` macros can now be invoked from within expressions in attribute values.
cmlenz
parents: 184
diff changeset
319 <p>bar</p>
95c3813a00de Fix for #34: `py:def` macros can now be invoked from within expressions in attribute values.
cmlenz
parents: 184
diff changeset
320 </doc>""", str(tmpl.generate()))
95c3813a00de Fix for #34: `py:def` macros can now be invoked from within expressions in attribute values.
cmlenz
parents: 184
diff changeset
321
206
75c9c019de88 `TypeError`s raised by `py:def` macros (and other expressions producing streams) are no longer silently ignored. Closes #44.
cmlenz
parents: 202
diff changeset
322 def test_function_raising_typeerror(self):
75c9c019de88 `TypeError`s raised by `py:def` macros (and other expressions producing streams) are no longer silently ignored. Closes #44.
cmlenz
parents: 202
diff changeset
323 def badfunc():
75c9c019de88 `TypeError`s raised by `py:def` macros (and other expressions producing streams) are no longer silently ignored. Closes #44.
cmlenz
parents: 202
diff changeset
324 raise TypeError
75c9c019de88 `TypeError`s raised by `py:def` macros (and other expressions producing streams) are no longer silently ignored. Closes #44.
cmlenz
parents: 202
diff changeset
325 tmpl = Template("""<html xmlns:py="http://markup.edgewall.org/">
75c9c019de88 `TypeError`s raised by `py:def` macros (and other expressions producing streams) are no longer silently ignored. Closes #44.
cmlenz
parents: 202
diff changeset
326 <div py:def="dobadfunc()">
75c9c019de88 `TypeError`s raised by `py:def` macros (and other expressions producing streams) are no longer silently ignored. Closes #44.
cmlenz
parents: 202
diff changeset
327 ${badfunc()}
75c9c019de88 `TypeError`s raised by `py:def` macros (and other expressions producing streams) are no longer silently ignored. Closes #44.
cmlenz
parents: 202
diff changeset
328 </div>
75c9c019de88 `TypeError`s raised by `py:def` macros (and other expressions producing streams) are no longer silently ignored. Closes #44.
cmlenz
parents: 202
diff changeset
329 <div py:content="dobadfunc()"/>
75c9c019de88 `TypeError`s raised by `py:def` macros (and other expressions producing streams) are no longer silently ignored. Closes #44.
cmlenz
parents: 202
diff changeset
330 </html>""")
75c9c019de88 `TypeError`s raised by `py:def` macros (and other expressions producing streams) are no longer silently ignored. Closes #44.
cmlenz
parents: 202
diff changeset
331 self.assertRaises(TypeError, list, tmpl.generate(badfunc=badfunc))
75c9c019de88 `TypeError`s raised by `py:def` macros (and other expressions producing streams) are no longer silently ignored. Closes #44.
cmlenz
parents: 202
diff changeset
332
208
bc146e63c159 Cleanup the application of template processing steps (flatten, eval, match) so that they are only performed when necessary. Results in a small performance boost, and also fixes #35.
cmlenz
parents: 206
diff changeset
333 def test_def_in_matched(self):
bc146e63c159 Cleanup the application of template processing steps (flatten, eval, match) so that they are only performed when necessary. Results in a small performance boost, and also fixes #35.
cmlenz
parents: 206
diff changeset
334 tmpl = Template("""<doc xmlns:py="http://markup.edgewall.org/">
bc146e63c159 Cleanup the application of template processing steps (flatten, eval, match) so that they are only performed when necessary. Results in a small performance boost, and also fixes #35.
cmlenz
parents: 206
diff changeset
335 <head py:match="head">${select('*')}</head>
bc146e63c159 Cleanup the application of template processing steps (flatten, eval, match) so that they are only performed when necessary. Results in a small performance boost, and also fixes #35.
cmlenz
parents: 206
diff changeset
336 <head>
bc146e63c159 Cleanup the application of template processing steps (flatten, eval, match) so that they are only performed when necessary. Results in a small performance boost, and also fixes #35.
cmlenz
parents: 206
diff changeset
337 <py:def function="maketitle(test)"><b py:replace="test" /></py:def>
bc146e63c159 Cleanup the application of template processing steps (flatten, eval, match) so that they are only performed when necessary. Results in a small performance boost, and also fixes #35.
cmlenz
parents: 206
diff changeset
338 <title>${maketitle(True)}</title>
bc146e63c159 Cleanup the application of template processing steps (flatten, eval, match) so that they are only performed when necessary. Results in a small performance boost, and also fixes #35.
cmlenz
parents: 206
diff changeset
339 </head>
bc146e63c159 Cleanup the application of template processing steps (flatten, eval, match) so that they are only performed when necessary. Results in a small performance boost, and also fixes #35.
cmlenz
parents: 206
diff changeset
340 </doc>""")
bc146e63c159 Cleanup the application of template processing steps (flatten, eval, match) so that they are only performed when necessary. Results in a small performance boost, and also fixes #35.
cmlenz
parents: 206
diff changeset
341 self.assertEqual("""<doc>
bc146e63c159 Cleanup the application of template processing steps (flatten, eval, match) so that they are only performed when necessary. Results in a small performance boost, and also fixes #35.
cmlenz
parents: 206
diff changeset
342 <head><title>True</title></head>
bc146e63c159 Cleanup the application of template processing steps (flatten, eval, match) so that they are only performed when necessary. Results in a small performance boost, and also fixes #35.
cmlenz
parents: 206
diff changeset
343 </doc>""", str(tmpl.generate()))
bc146e63c159 Cleanup the application of template processing steps (flatten, eval, match) so that they are only performed when necessary. Results in a small performance boost, and also fixes #35.
cmlenz
parents: 206
diff changeset
344
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
345
51
b2383634ec04 Fix `py:for` directive when combined with other directives (such as `py:strip`).
cmlenz
parents: 50
diff changeset
346 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
347 """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
348
b2383634ec04 Fix `py:for` directive when combined with other directives (such as `py:strip`).
cmlenz
parents: 50
diff changeset
349 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
350 """
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
351 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
352 correctly.
51
b2383634ec04 Fix `py:for` directive when combined with other directives (such as `py:strip`).
cmlenz
parents: 50
diff changeset
353 """
61
448792ab1303 Use a different namespace than Kid uses.
cmlenz
parents: 54
diff changeset
354 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
355 <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
356 <b>${item}</b>
b2383634ec04 Fix `py:for` directive when combined with other directives (such as `py:strip`).
cmlenz
parents: 50
diff changeset
357 </div>
b2383634ec04 Fix `py:for` directive when combined with other directives (such as `py:strip`).
cmlenz
parents: 50
diff changeset
358 </doc>""")
b2383634ec04 Fix `py:for` directive when combined with other directives (such as `py:strip`).
cmlenz
parents: 50
diff changeset
359 self.assertEqual("""<doc>
b2383634ec04 Fix `py:for` directive when combined with other directives (such as `py:strip`).
cmlenz
parents: 50
diff changeset
360 <b>1</b>
b2383634ec04 Fix `py:for` directive when combined with other directives (such as `py:strip`).
cmlenz
parents: 50
diff changeset
361 <b>2</b>
b2383634ec04 Fix `py:for` directive when combined with other directives (such as `py:strip`).
cmlenz
parents: 50
diff changeset
362 <b>3</b>
b2383634ec04 Fix `py:for` directive when combined with other directives (such as `py:strip`).
cmlenz
parents: 50
diff changeset
363 <b>4</b>
b2383634ec04 Fix `py:for` directive when combined with other directives (such as `py:strip`).
cmlenz
parents: 50
diff changeset
364 <b>5</b>
149
537f819c547b `Template.generate()` now accepts the context data as keyword arguments, so that you don't have to import the `Context` class every time you want to pass data into a template.
cmlenz
parents: 145
diff changeset
365 </doc>""", str(tmpl.generate(items=range(1, 6))))
51
b2383634ec04 Fix `py:for` directive when combined with other directives (such as `py:strip`).
cmlenz
parents: 50
diff changeset
366
65
b3fdf93057ab Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents: 61
diff changeset
367 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
368 """
b3fdf93057ab Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents: 61
diff changeset
369 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
370 """
b3fdf93057ab Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents: 61
diff changeset
371 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
372 <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
373 <b>${item}</b>
b3fdf93057ab Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents: 61
diff changeset
374 </py:for>
b3fdf93057ab Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents: 61
diff changeset
375 </doc>""")
b3fdf93057ab Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents: 61
diff changeset
376 self.assertEqual("""<doc>
b3fdf93057ab Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents: 61
diff changeset
377 <b>1</b>
b3fdf93057ab Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents: 61
diff changeset
378 <b>2</b>
b3fdf93057ab Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents: 61
diff changeset
379 <b>3</b>
b3fdf93057ab Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents: 61
diff changeset
380 <b>4</b>
b3fdf93057ab Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents: 61
diff changeset
381 <b>5</b>
149
537f819c547b `Template.generate()` now accepts the context data as keyword arguments, so that you don't have to import the `Context` class every time you want to pass data into a template.
cmlenz
parents: 145
diff changeset
382 </doc>""", str(tmpl.generate(items=range(1, 6))))
65
b3fdf93057ab Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents: 61
diff changeset
383
220
f4943d2babda Fix for #45 and #46: properly support assignment to nested tuples in `py:for` and `py:with` directives.
cmlenz
parents: 217
diff changeset
384 def test_multi_assignment(self):
f4943d2babda Fix for #45 and #46: properly support assignment to nested tuples in `py:for` and `py:with` directives.
cmlenz
parents: 217
diff changeset
385 """
f4943d2babda Fix for #45 and #46: properly support assignment to nested tuples in `py:for` and `py:with` directives.
cmlenz
parents: 217
diff changeset
386 Verify that assignment to tuples works correctly.
f4943d2babda Fix for #45 and #46: properly support assignment to nested tuples in `py:for` and `py:with` directives.
cmlenz
parents: 217
diff changeset
387 """
f4943d2babda Fix for #45 and #46: properly support assignment to nested tuples in `py:for` and `py:with` directives.
cmlenz
parents: 217
diff changeset
388 tmpl = Template("""<doc xmlns:py="http://markup.edgewall.org/">
f4943d2babda Fix for #45 and #46: properly support assignment to nested tuples in `py:for` and `py:with` directives.
cmlenz
parents: 217
diff changeset
389 <py:for each="k, v in items">
f4943d2babda Fix for #45 and #46: properly support assignment to nested tuples in `py:for` and `py:with` directives.
cmlenz
parents: 217
diff changeset
390 <p>key=$k, value=$v</p>
f4943d2babda Fix for #45 and #46: properly support assignment to nested tuples in `py:for` and `py:with` directives.
cmlenz
parents: 217
diff changeset
391 </py:for>
f4943d2babda Fix for #45 and #46: properly support assignment to nested tuples in `py:for` and `py:with` directives.
cmlenz
parents: 217
diff changeset
392 </doc>""")
f4943d2babda Fix for #45 and #46: properly support assignment to nested tuples in `py:for` and `py:with` directives.
cmlenz
parents: 217
diff changeset
393 self.assertEqual("""<doc>
f4943d2babda Fix for #45 and #46: properly support assignment to nested tuples in `py:for` and `py:with` directives.
cmlenz
parents: 217
diff changeset
394 <p>key=a, value=1</p>
f4943d2babda Fix for #45 and #46: properly support assignment to nested tuples in `py:for` and `py:with` directives.
cmlenz
parents: 217
diff changeset
395 <p>key=b, value=2</p>
f4943d2babda Fix for #45 and #46: properly support assignment to nested tuples in `py:for` and `py:with` directives.
cmlenz
parents: 217
diff changeset
396 </doc>""", str(tmpl.generate(items=dict(a=1, b=2).items())))
f4943d2babda Fix for #45 and #46: properly support assignment to nested tuples in `py:for` and `py:with` directives.
cmlenz
parents: 217
diff changeset
397
f4943d2babda Fix for #45 and #46: properly support assignment to nested tuples in `py:for` and `py:with` directives.
cmlenz
parents: 217
diff changeset
398 def test_nested_assignment(self):
f4943d2babda Fix for #45 and #46: properly support assignment to nested tuples in `py:for` and `py:with` directives.
cmlenz
parents: 217
diff changeset
399 """
f4943d2babda Fix for #45 and #46: properly support assignment to nested tuples in `py:for` and `py:with` directives.
cmlenz
parents: 217
diff changeset
400 Verify that assignment to nested tuples works correctly.
f4943d2babda Fix for #45 and #46: properly support assignment to nested tuples in `py:for` and `py:with` directives.
cmlenz
parents: 217
diff changeset
401 """
f4943d2babda Fix for #45 and #46: properly support assignment to nested tuples in `py:for` and `py:with` directives.
cmlenz
parents: 217
diff changeset
402 tmpl = Template("""<doc xmlns:py="http://markup.edgewall.org/">
f4943d2babda Fix for #45 and #46: properly support assignment to nested tuples in `py:for` and `py:with` directives.
cmlenz
parents: 217
diff changeset
403 <py:for each="idx, (k, v) in items">
f4943d2babda Fix for #45 and #46: properly support assignment to nested tuples in `py:for` and `py:with` directives.
cmlenz
parents: 217
diff changeset
404 <p>$idx: key=$k, value=$v</p>
f4943d2babda Fix for #45 and #46: properly support assignment to nested tuples in `py:for` and `py:with` directives.
cmlenz
parents: 217
diff changeset
405 </py:for>
f4943d2babda Fix for #45 and #46: properly support assignment to nested tuples in `py:for` and `py:with` directives.
cmlenz
parents: 217
diff changeset
406 </doc>""")
f4943d2babda Fix for #45 and #46: properly support assignment to nested tuples in `py:for` and `py:with` directives.
cmlenz
parents: 217
diff changeset
407 self.assertEqual("""<doc>
f4943d2babda Fix for #45 and #46: properly support assignment to nested tuples in `py:for` and `py:with` directives.
cmlenz
parents: 217
diff changeset
408 <p>0: key=a, value=1</p>
f4943d2babda Fix for #45 and #46: properly support assignment to nested tuples in `py:for` and `py:with` directives.
cmlenz
parents: 217
diff changeset
409 <p>1: key=b, value=2</p>
f4943d2babda Fix for #45 and #46: properly support assignment to nested tuples in `py:for` and `py:with` directives.
cmlenz
parents: 217
diff changeset
410 </doc>""", str(tmpl.generate(items=enumerate(dict(a=1, b=2).items()))))
f4943d2babda Fix for #45 and #46: properly support assignment to nested tuples in `py:for` and `py:with` directives.
cmlenz
parents: 217
diff changeset
411
65
b3fdf93057ab Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents: 61
diff changeset
412
b3fdf93057ab Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents: 61
diff changeset
413 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
414 """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
415
b3fdf93057ab Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents: 61
diff changeset
416 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
417 """
b3fdf93057ab Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents: 61
diff changeset
418 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
419 correctly.
b3fdf93057ab Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents: 61
diff changeset
420 """
b3fdf93057ab Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents: 61
diff changeset
421 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
422 <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
423 </doc>""")
b3fdf93057ab Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents: 61
diff changeset
424 self.assertEqual("""<doc>
b3fdf93057ab Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents: 61
diff changeset
425 Hello
149
537f819c547b `Template.generate()` now accepts the context data as keyword arguments, so that you don't have to import the `Context` class every time you want to pass data into a template.
cmlenz
parents: 145
diff changeset
426 </doc>""", str(tmpl.generate(foo=True, bar='Hello')))
65
b3fdf93057ab Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents: 61
diff changeset
427
b3fdf93057ab Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents: 61
diff changeset
428 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
429 """
b3fdf93057ab Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents: 61
diff changeset
430 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
431 """
b3fdf93057ab Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents: 61
diff changeset
432 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
433 <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
434 </doc>""")
b3fdf93057ab Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents: 61
diff changeset
435 self.assertEqual("""<doc>
b3fdf93057ab Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents: 61
diff changeset
436 Hello
149
537f819c547b `Template.generate()` now accepts the context data as keyword arguments, so that you don't have to import the `Context` class every time you want to pass data into a template.
cmlenz
parents: 145
diff changeset
437 </doc>""", str(tmpl.generate(foo=True, bar='Hello')))
65
b3fdf93057ab Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents: 61
diff changeset
438
51
b2383634ec04 Fix `py:for` directive when combined with other directives (such as `py:strip`).
cmlenz
parents: 50
diff changeset
439
36
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
440 class MatchDirectiveTestCase(unittest.TestCase):
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
441 """Tests for the `py:match` template directive."""
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
442
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
443 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
444 """
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
445 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
446 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
447 """
61
448792ab1303 Use a different namespace than Kid uses.
cmlenz
parents: 54
diff changeset
448 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
449 <elem py:match="elem" py:strip="">
216
636fe6766b4d Many fixes to XPath evaluation. Among other things, this should get rid of the bug that attributes were getting ?pulled up? by `py:match` directives using `py:attrs="select('@*')"` (see #50).
cmlenz
parents: 211
diff changeset
450 <div class="elem">${select('text()')}</div>
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
451 </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
452 <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
453 </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
454 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
455 <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
456 </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
457
36
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
458 def test_without_strip(self):
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
459 """
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
460 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
461 it matched without entering an infinite recursion.
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
462 """
61
448792ab1303 Use a different namespace than Kid uses.
cmlenz
parents: 54
diff changeset
463 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
464 <elem py:match="elem">
216
636fe6766b4d Many fixes to XPath evaluation. Among other things, this should get rid of the bug that attributes were getting ?pulled up? by `py:match` directives using `py:attrs="select('@*')"` (see #50).
cmlenz
parents: 211
diff changeset
465 <div class="elem">${select('text()')}</div>
36
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
466 </elem>
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
467 <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
468 </doc>""")
36
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
469 self.assertEqual("""<doc>
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
470 <elem>
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
471 <div class="elem">Hey Joe</div>
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
472 </elem>
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
473 </doc>""", str(tmpl.generate()))
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
474
65
b3fdf93057ab Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents: 61
diff changeset
475 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
476 """
b3fdf93057ab Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents: 61
diff changeset
477 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
478 """
b3fdf93057ab Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents: 61
diff changeset
479 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
480 <py:match path="elem">
216
636fe6766b4d Many fixes to XPath evaluation. Among other things, this should get rid of the bug that attributes were getting ?pulled up? by `py:match` directives using `py:attrs="select('@*')"` (see #50).
cmlenz
parents: 211
diff changeset
481 <div class="elem">${select('text()')}</div>
65
b3fdf93057ab Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents: 61
diff changeset
482 </py:match>
b3fdf93057ab Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents: 61
diff changeset
483 <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
484 </doc>""")
b3fdf93057ab Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents: 61
diff changeset
485 self.assertEqual("""<doc>
b3fdf93057ab Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents: 61
diff changeset
486 <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
487 </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
488
36
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
489 def test_recursive_match_1(self):
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
490 """
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
491 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
492 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
493 """
61
448792ab1303 Use a different namespace than Kid uses.
cmlenz
parents: 54
diff changeset
494 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
495 <elem py:match="elem">
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
496 <div class="elem">
216
636fe6766b4d Many fixes to XPath evaluation. Among other things, this should get rid of the bug that attributes were getting ?pulled up? by `py:match` directives using `py:attrs="select('@*')"` (see #50).
cmlenz
parents: 211
diff changeset
497 ${select('*')}
36
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
498 </div>
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
499 </elem>
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
500 <elem>
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
501 <subelem>
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
502 <elem/>
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
503 </subelem>
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
504 </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
505 </doc>""")
36
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
506 self.assertEqual("""<doc>
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
507 <elem>
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
508 <div class="elem">
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
509 <subelem>
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
510 <elem>
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
511 <div class="elem">
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
512 </div>
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
513 </elem>
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
514 </subelem>
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
515 </div>
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
516 </elem>
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
517 </doc>""", str(tmpl.generate()))
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
518
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
519 def test_recursive_match_2(self):
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
520 """
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
521 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
522 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
523 more complex, but should work.
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
524 """
61
448792ab1303 Use a different namespace than Kid uses.
cmlenz
parents: 54
diff changeset
525 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
526 <body py:match="body">
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
527 <div id="header"/>
216
636fe6766b4d Many fixes to XPath evaluation. Among other things, this should get rid of the bug that attributes were getting ?pulled up? by `py:match` directives using `py:attrs="select('@*')"` (see #50).
cmlenz
parents: 211
diff changeset
528 ${select('*')}
36
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
529 </body>
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
530 <body py:match="body">
216
636fe6766b4d Many fixes to XPath evaluation. Among other things, this should get rid of the bug that attributes were getting ?pulled up? by `py:match` directives using `py:attrs="select('@*')"` (see #50).
cmlenz
parents: 211
diff changeset
531 ${select('*')}
36
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
532 <div id="footer"/>
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
533 </body>
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
534 <body>
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
535 <h1>Foo</h1>
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
536 </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
537 </html>""")
36
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
538 self.assertEqual("""<html>
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
539 <body>
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
540 <div id="header"/><h1>Foo</h1>
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
541 <div id="footer"/>
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
542 </body>
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
543 </html>""", str(tmpl.generate()))
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
544
77
f5ec6d4a61e4 * Simplify implementation of the individual XPath tests (use closures instead of callable classes)
cmlenz
parents: 75
diff changeset
545 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
546 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
547 <div py:match="elem" py:attrs="select('@*')">
216
636fe6766b4d Many fixes to XPath evaluation. Among other things, this should get rid of the bug that attributes were getting ?pulled up? by `py:match` directives using `py:attrs="select('@*')"` (see #50).
cmlenz
parents: 211
diff changeset
548 ${select('text()')}
77
f5ec6d4a61e4 * Simplify implementation of the individual XPath tests (use closures instead of callable classes)
cmlenz
parents: 75
diff changeset
549 </div>
f5ec6d4a61e4 * Simplify implementation of the individual XPath tests (use closures instead of callable classes)
cmlenz
parents: 75
diff changeset
550 <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
551 </doc>""")
f5ec6d4a61e4 * Simplify implementation of the individual XPath tests (use closures instead of callable classes)
cmlenz
parents: 75
diff changeset
552 self.assertEqual("""<doc>
f5ec6d4a61e4 * Simplify implementation of the individual XPath tests (use closures instead of callable classes)
cmlenz
parents: 75
diff changeset
553 <div id="joe">
f5ec6d4a61e4 * Simplify implementation of the individual XPath tests (use closures instead of callable classes)
cmlenz
parents: 75
diff changeset
554 Hey Joe
f5ec6d4a61e4 * Simplify implementation of the individual XPath tests (use closures instead of callable classes)
cmlenz
parents: 75
diff changeset
555 </div>
f5ec6d4a61e4 * Simplify implementation of the individual XPath tests (use closures instead of callable classes)
cmlenz
parents: 75
diff changeset
556 </doc>""", str(tmpl.generate()))
f5ec6d4a61e4 * Simplify implementation of the individual XPath tests (use closures instead of callable classes)
cmlenz
parents: 75
diff changeset
557
f5ec6d4a61e4 * Simplify implementation of the individual XPath tests (use closures instead of callable classes)
cmlenz
parents: 75
diff changeset
558 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
559 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
560 <div py:match="elem" py:attrs="select('@*')">
216
636fe6766b4d Many fixes to XPath evaluation. Among other things, this should get rid of the bug that attributes were getting ?pulled up? by `py:match` directives using `py:attrs="select('@*')"` (see #50).
cmlenz
parents: 211
diff changeset
561 ${select('text()')}
77
f5ec6d4a61e4 * Simplify implementation of the individual XPath tests (use closures instead of callable classes)
cmlenz
parents: 75
diff changeset
562 </div>
f5ec6d4a61e4 * Simplify implementation of the individual XPath tests (use closures instead of callable classes)
cmlenz
parents: 75
diff changeset
563 <elem>Hey Joe</elem>
f5ec6d4a61e4 * Simplify implementation of the individual XPath tests (use closures instead of callable classes)
cmlenz
parents: 75
diff changeset
564 </doc>""")
f5ec6d4a61e4 * Simplify implementation of the individual XPath tests (use closures instead of callable classes)
cmlenz
parents: 75
diff changeset
565 self.assertEqual("""<doc>
f5ec6d4a61e4 * Simplify implementation of the individual XPath tests (use closures instead of callable classes)
cmlenz
parents: 75
diff changeset
566 <div>
f5ec6d4a61e4 * Simplify implementation of the individual XPath tests (use closures instead of callable classes)
cmlenz
parents: 75
diff changeset
567 Hey Joe
f5ec6d4a61e4 * Simplify implementation of the individual XPath tests (use closures instead of callable classes)
cmlenz
parents: 75
diff changeset
568 </div>
f5ec6d4a61e4 * Simplify implementation of the individual XPath tests (use closures instead of callable classes)
cmlenz
parents: 75
diff changeset
569 </doc>""", str(tmpl.generate()))
f5ec6d4a61e4 * Simplify implementation of the individual XPath tests (use closures instead of callable classes)
cmlenz
parents: 75
diff changeset
570
f5ec6d4a61e4 * Simplify implementation of the individual XPath tests (use closures instead of callable classes)
cmlenz
parents: 75
diff changeset
571 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
572 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
573 <div py:match="elem">
f5ec6d4a61e4 * Simplify implementation of the individual XPath tests (use closures instead of callable classes)
cmlenz
parents: 75
diff changeset
574 Hey ${select('text()')} ${select('@*')}
f5ec6d4a61e4 * Simplify implementation of the individual XPath tests (use closures instead of callable classes)
cmlenz
parents: 75
diff changeset
575 </div>
f5ec6d4a61e4 * Simplify implementation of the individual XPath tests (use closures instead of callable classes)
cmlenz
parents: 75
diff changeset
576 <elem title="Cool">Joe</elem>
f5ec6d4a61e4 * Simplify implementation of the individual XPath tests (use closures instead of callable classes)
cmlenz
parents: 75
diff changeset
577 </doc>""")
f5ec6d4a61e4 * Simplify implementation of the individual XPath tests (use closures instead of callable classes)
cmlenz
parents: 75
diff changeset
578 self.assertEqual("""<doc>
f5ec6d4a61e4 * Simplify implementation of the individual XPath tests (use closures instead of callable classes)
cmlenz
parents: 75
diff changeset
579 <div>
f5ec6d4a61e4 * Simplify implementation of the individual XPath tests (use closures instead of callable classes)
cmlenz
parents: 75
diff changeset
580 Hey Joe Cool
f5ec6d4a61e4 * Simplify implementation of the individual XPath tests (use closures instead of callable classes)
cmlenz
parents: 75
diff changeset
581 </div>
f5ec6d4a61e4 * Simplify implementation of the individual XPath tests (use closures instead of callable classes)
cmlenz
parents: 75
diff changeset
582 </doc>""", str(tmpl.generate()))
f5ec6d4a61e4 * Simplify implementation of the individual XPath tests (use closures instead of callable classes)
cmlenz
parents: 75
diff changeset
583
172
ff4f0d89eef7 Fix for #30 (trouble using `py:def`inside a match template)
cmlenz
parents: 166
diff changeset
584 def test_def_in_match(self):
ff4f0d89eef7 Fix for #30 (trouble using `py:def`inside a match template)
cmlenz
parents: 166
diff changeset
585 tmpl = Template("""<doc xmlns:py="http://markup.edgewall.org/">
ff4f0d89eef7 Fix for #30 (trouble using `py:def`inside a match template)
cmlenz
parents: 166
diff changeset
586 <py:def function="maketitle(test)"><b py:replace="test" /></py:def>
ff4f0d89eef7 Fix for #30 (trouble using `py:def`inside a match template)
cmlenz
parents: 166
diff changeset
587 <head py:match="head">${select('*')}</head>
ff4f0d89eef7 Fix for #30 (trouble using `py:def`inside a match template)
cmlenz
parents: 166
diff changeset
588 <head><title>${maketitle(True)}</title></head>
ff4f0d89eef7 Fix for #30 (trouble using `py:def`inside a match template)
cmlenz
parents: 166
diff changeset
589 </doc>""")
ff4f0d89eef7 Fix for #30 (trouble using `py:def`inside a match template)
cmlenz
parents: 166
diff changeset
590 self.assertEqual("""<doc>
ff4f0d89eef7 Fix for #30 (trouble using `py:def`inside a match template)
cmlenz
parents: 166
diff changeset
591 <head><title>True</title></head>
ff4f0d89eef7 Fix for #30 (trouble using `py:def`inside a match template)
cmlenz
parents: 166
diff changeset
592 </doc>""", str(tmpl.generate()))
ff4f0d89eef7 Fix for #30 (trouble using `py:def`inside a match template)
cmlenz
parents: 166
diff changeset
593
179
13909179e5e1 Implemented support for XPath variables in predicates (#31).
cmlenz
parents: 175
diff changeset
594 def test_match_with_xpath_variable(self):
13909179e5e1 Implemented support for XPath variables in predicates (#31).
cmlenz
parents: 175
diff changeset
595 tmpl = Template("""<div xmlns:py="http://markup.edgewall.org/">
13909179e5e1 Implemented support for XPath variables in predicates (#31).
cmlenz
parents: 175
diff changeset
596 <span py:match="*[name()=$tagname]">
13909179e5e1 Implemented support for XPath variables in predicates (#31).
cmlenz
parents: 175
diff changeset
597 Hello ${select('@name')}
13909179e5e1 Implemented support for XPath variables in predicates (#31).
cmlenz
parents: 175
diff changeset
598 </span>
13909179e5e1 Implemented support for XPath variables in predicates (#31).
cmlenz
parents: 175
diff changeset
599 <greeting name="Dude"/>
13909179e5e1 Implemented support for XPath variables in predicates (#31).
cmlenz
parents: 175
diff changeset
600 </div>""")
13909179e5e1 Implemented support for XPath variables in predicates (#31).
cmlenz
parents: 175
diff changeset
601 self.assertEqual("""<div>
13909179e5e1 Implemented support for XPath variables in predicates (#31).
cmlenz
parents: 175
diff changeset
602 <span>
13909179e5e1 Implemented support for XPath variables in predicates (#31).
cmlenz
parents: 175
diff changeset
603 Hello Dude
13909179e5e1 Implemented support for XPath variables in predicates (#31).
cmlenz
parents: 175
diff changeset
604 </span>
13909179e5e1 Implemented support for XPath variables in predicates (#31).
cmlenz
parents: 175
diff changeset
605 </div>""", str(tmpl.generate(tagname='greeting')))
13909179e5e1 Implemented support for XPath variables in predicates (#31).
cmlenz
parents: 175
diff changeset
606 self.assertEqual("""<div>
13909179e5e1 Implemented support for XPath variables in predicates (#31).
cmlenz
parents: 175
diff changeset
607 <greeting name="Dude"/>
13909179e5e1 Implemented support for XPath variables in predicates (#31).
cmlenz
parents: 175
diff changeset
608 </div>""", str(tmpl.generate(tagname='sayhello')))
13909179e5e1 Implemented support for XPath variables in predicates (#31).
cmlenz
parents: 175
diff changeset
609
210
9fd7535883f2 Fix regression introduced in [258]. More fixes needed?
cmlenz
parents: 208
diff changeset
610 def test_content_directive_in_match(self):
9fd7535883f2 Fix regression introduced in [258]. More fixes needed?
cmlenz
parents: 208
diff changeset
611 tmpl = Template("""<html xmlns:py="http://markup.edgewall.org/">
9fd7535883f2 Fix regression introduced in [258]. More fixes needed?
cmlenz
parents: 208
diff changeset
612 <div py:match="foo">I said <q py:content="select('text()')">something</q>.</div>
9fd7535883f2 Fix regression introduced in [258]. More fixes needed?
cmlenz
parents: 208
diff changeset
613 <foo>bar</foo>
9fd7535883f2 Fix regression introduced in [258]. More fixes needed?
cmlenz
parents: 208
diff changeset
614 </html>""")
9fd7535883f2 Fix regression introduced in [258]. More fixes needed?
cmlenz
parents: 208
diff changeset
615 self.assertEqual("""<html>
9fd7535883f2 Fix regression introduced in [258]. More fixes needed?
cmlenz
parents: 208
diff changeset
616 <div>I said <q>bar</q>.</div>
9fd7535883f2 Fix regression introduced in [258]. More fixes needed?
cmlenz
parents: 208
diff changeset
617 </html>""", str(tmpl.generate()))
9fd7535883f2 Fix regression introduced in [258]. More fixes needed?
cmlenz
parents: 208
diff changeset
618
211
e5151983df0d Fix another regression introduced in [258]: some kinds of cascaded match templates were broken, for example in the TurboGears example app.
cmlenz
parents: 210
diff changeset
619 def test_cascaded_matches(self):
e5151983df0d Fix another regression introduced in [258]: some kinds of cascaded match templates were broken, for example in the TurboGears example app.
cmlenz
parents: 210
diff changeset
620 tmpl = Template("""<html xmlns:py="http://markup.edgewall.org/">
e5151983df0d Fix another regression introduced in [258]: some kinds of cascaded match templates were broken, for example in the TurboGears example app.
cmlenz
parents: 210
diff changeset
621 <body py:match="body">${select('*')}</body>
e5151983df0d Fix another regression introduced in [258]: some kinds of cascaded match templates were broken, for example in the TurboGears example app.
cmlenz
parents: 210
diff changeset
622 <head py:match="head">${select('title')}</head>
e5151983df0d Fix another regression introduced in [258]: some kinds of cascaded match templates were broken, for example in the TurboGears example app.
cmlenz
parents: 210
diff changeset
623 <body py:match="body">${select('*')}<hr /></body>
e5151983df0d Fix another regression introduced in [258]: some kinds of cascaded match templates were broken, for example in the TurboGears example app.
cmlenz
parents: 210
diff changeset
624 <head><title>Welcome to Markup</title></head>
e5151983df0d Fix another regression introduced in [258]: some kinds of cascaded match templates were broken, for example in the TurboGears example app.
cmlenz
parents: 210
diff changeset
625 <body><h2>Are you ready to mark up?</h2></body>
e5151983df0d Fix another regression introduced in [258]: some kinds of cascaded match templates were broken, for example in the TurboGears example app.
cmlenz
parents: 210
diff changeset
626 </html>""")
e5151983df0d Fix another regression introduced in [258]: some kinds of cascaded match templates were broken, for example in the TurboGears example app.
cmlenz
parents: 210
diff changeset
627 self.assertEqual("""<html>
e5151983df0d Fix another regression introduced in [258]: some kinds of cascaded match templates were broken, for example in the TurboGears example app.
cmlenz
parents: 210
diff changeset
628 <head><title>Welcome to Markup</title></head>
e5151983df0d Fix another regression introduced in [258]: some kinds of cascaded match templates were broken, for example in the TurboGears example app.
cmlenz
parents: 210
diff changeset
629 <body><h2>Are you ready to mark up?</h2><hr/></body>
e5151983df0d Fix another regression introduced in [258]: some kinds of cascaded match templates were broken, for example in the TurboGears example app.
cmlenz
parents: 210
diff changeset
630 </html>""", str(tmpl.generate()))
e5151983df0d Fix another regression introduced in [258]: some kinds of cascaded match templates were broken, for example in the TurboGears example app.
cmlenz
parents: 210
diff changeset
631
217
f150cff4da18 Fix `py:match` directive which would screw up in some scenarios due to incorrect handling of the substream. Closes #49.
cmlenz
parents: 216
diff changeset
632 def test_multiple_matches(self):
f150cff4da18 Fix `py:match` directive which would screw up in some scenarios due to incorrect handling of the substream. Closes #49.
cmlenz
parents: 216
diff changeset
633 tmpl = Template("""<html xmlns:py="http://markup.edgewall.org/">
f150cff4da18 Fix `py:match` directive which would screw up in some scenarios due to incorrect handling of the substream. Closes #49.
cmlenz
parents: 216
diff changeset
634 <input py:match="form//input" py:attrs="select('@*')"
f150cff4da18 Fix `py:match` directive which would screw up in some scenarios due to incorrect handling of the substream. Closes #49.
cmlenz
parents: 216
diff changeset
635 value="${values[str(select('@name'))]}" />
f150cff4da18 Fix `py:match` directive which would screw up in some scenarios due to incorrect handling of the substream. Closes #49.
cmlenz
parents: 216
diff changeset
636 <form><p py:for="field in fields">
f150cff4da18 Fix `py:match` directive which would screw up in some scenarios due to incorrect handling of the substream. Closes #49.
cmlenz
parents: 216
diff changeset
637 <label>${field.capitalize()}</label>
f150cff4da18 Fix `py:match` directive which would screw up in some scenarios due to incorrect handling of the substream. Closes #49.
cmlenz
parents: 216
diff changeset
638 <input type="text" name="${field}" />
f150cff4da18 Fix `py:match` directive which would screw up in some scenarios due to incorrect handling of the substream. Closes #49.
cmlenz
parents: 216
diff changeset
639 </p></form>
f150cff4da18 Fix `py:match` directive which would screw up in some scenarios due to incorrect handling of the substream. Closes #49.
cmlenz
parents: 216
diff changeset
640 </html>""")
f150cff4da18 Fix `py:match` directive which would screw up in some scenarios due to incorrect handling of the substream. Closes #49.
cmlenz
parents: 216
diff changeset
641 fields = ['hello_%s' % i for i in range(5)]
f150cff4da18 Fix `py:match` directive which would screw up in some scenarios due to incorrect handling of the substream. Closes #49.
cmlenz
parents: 216
diff changeset
642 values = dict([('hello_%s' % i, i) for i in range(5)])
f150cff4da18 Fix `py:match` directive which would screw up in some scenarios due to incorrect handling of the substream. Closes #49.
cmlenz
parents: 216
diff changeset
643 self.assertEqual("""<html>
f150cff4da18 Fix `py:match` directive which would screw up in some scenarios due to incorrect handling of the substream. Closes #49.
cmlenz
parents: 216
diff changeset
644 <form><p>
f150cff4da18 Fix `py:match` directive which would screw up in some scenarios due to incorrect handling of the substream. Closes #49.
cmlenz
parents: 216
diff changeset
645 <label>Hello_0</label>
f150cff4da18 Fix `py:match` directive which would screw up in some scenarios due to incorrect handling of the substream. Closes #49.
cmlenz
parents: 216
diff changeset
646 <input value="0" type="text" name="hello_0"/>
f150cff4da18 Fix `py:match` directive which would screw up in some scenarios due to incorrect handling of the substream. Closes #49.
cmlenz
parents: 216
diff changeset
647 </p><p>
f150cff4da18 Fix `py:match` directive which would screw up in some scenarios due to incorrect handling of the substream. Closes #49.
cmlenz
parents: 216
diff changeset
648 <label>Hello_1</label>
f150cff4da18 Fix `py:match` directive which would screw up in some scenarios due to incorrect handling of the substream. Closes #49.
cmlenz
parents: 216
diff changeset
649 <input value="1" type="text" name="hello_1"/>
f150cff4da18 Fix `py:match` directive which would screw up in some scenarios due to incorrect handling of the substream. Closes #49.
cmlenz
parents: 216
diff changeset
650 </p><p>
f150cff4da18 Fix `py:match` directive which would screw up in some scenarios due to incorrect handling of the substream. Closes #49.
cmlenz
parents: 216
diff changeset
651 <label>Hello_2</label>
f150cff4da18 Fix `py:match` directive which would screw up in some scenarios due to incorrect handling of the substream. Closes #49.
cmlenz
parents: 216
diff changeset
652 <input value="2" type="text" name="hello_2"/>
f150cff4da18 Fix `py:match` directive which would screw up in some scenarios due to incorrect handling of the substream. Closes #49.
cmlenz
parents: 216
diff changeset
653 </p><p>
f150cff4da18 Fix `py:match` directive which would screw up in some scenarios due to incorrect handling of the substream. Closes #49.
cmlenz
parents: 216
diff changeset
654 <label>Hello_3</label>
f150cff4da18 Fix `py:match` directive which would screw up in some scenarios due to incorrect handling of the substream. Closes #49.
cmlenz
parents: 216
diff changeset
655 <input value="3" type="text" name="hello_3"/>
f150cff4da18 Fix `py:match` directive which would screw up in some scenarios due to incorrect handling of the substream. Closes #49.
cmlenz
parents: 216
diff changeset
656 </p><p>
f150cff4da18 Fix `py:match` directive which would screw up in some scenarios due to incorrect handling of the substream. Closes #49.
cmlenz
parents: 216
diff changeset
657 <label>Hello_4</label>
f150cff4da18 Fix `py:match` directive which would screw up in some scenarios due to incorrect handling of the substream. Closes #49.
cmlenz
parents: 216
diff changeset
658 <input value="4" type="text" name="hello_4"/>
f150cff4da18 Fix `py:match` directive which would screw up in some scenarios due to incorrect handling of the substream. Closes #49.
cmlenz
parents: 216
diff changeset
659 </p></form>
f150cff4da18 Fix `py:match` directive which would screw up in some scenarios due to incorrect handling of the substream. Closes #49.
cmlenz
parents: 216
diff changeset
660 </html>""", str(tmpl.generate(fields=fields, values=values)))
f150cff4da18 Fix `py:match` directive which would screw up in some scenarios due to incorrect handling of the substream. Closes #49.
cmlenz
parents: 216
diff changeset
661
184
181d292eafa2 Interpolate multiline expressions in templates. Thanks to Christian Boos for reporting the problem and providing the fix.
cmlenz
parents: 181
diff changeset
662 # FIXME
181d292eafa2 Interpolate multiline expressions in templates. Thanks to Christian Boos for reporting the problem and providing the fix.
cmlenz
parents: 181
diff changeset
663 #def test_match_after_step(self):
181d292eafa2 Interpolate multiline expressions in templates. Thanks to Christian Boos for reporting the problem and providing the fix.
cmlenz
parents: 181
diff changeset
664 # tmpl = Template("""<div xmlns:py="http://markup.edgewall.org/">
181d292eafa2 Interpolate multiline expressions in templates. Thanks to Christian Boos for reporting the problem and providing the fix.
cmlenz
parents: 181
diff changeset
665 # <span py:match="div/greeting">
181d292eafa2 Interpolate multiline expressions in templates. Thanks to Christian Boos for reporting the problem and providing the fix.
cmlenz
parents: 181
diff changeset
666 # Hello ${select('@name')}
181d292eafa2 Interpolate multiline expressions in templates. Thanks to Christian Boos for reporting the problem and providing the fix.
cmlenz
parents: 181
diff changeset
667 # </span>
181d292eafa2 Interpolate multiline expressions in templates. Thanks to Christian Boos for reporting the problem and providing the fix.
cmlenz
parents: 181
diff changeset
668 # <greeting name="Dude" />
181d292eafa2 Interpolate multiline expressions in templates. Thanks to Christian Boos for reporting the problem and providing the fix.
cmlenz
parents: 181
diff changeset
669 # </div>""")
181d292eafa2 Interpolate multiline expressions in templates. Thanks to Christian Boos for reporting the problem and providing the fix.
cmlenz
parents: 181
diff changeset
670 # self.assertEqual("""<div>
181d292eafa2 Interpolate multiline expressions in templates. Thanks to Christian Boos for reporting the problem and providing the fix.
cmlenz
parents: 181
diff changeset
671 # <span>
181d292eafa2 Interpolate multiline expressions in templates. Thanks to Christian Boos for reporting the problem and providing the fix.
cmlenz
parents: 181
diff changeset
672 # Hello Dude
181d292eafa2 Interpolate multiline expressions in templates. Thanks to Christian Boos for reporting the problem and providing the fix.
cmlenz
parents: 181
diff changeset
673 # </span>
181d292eafa2 Interpolate multiline expressions in templates. Thanks to Christian Boos for reporting the problem and providing the fix.
cmlenz
parents: 181
diff changeset
674 # </div>""", str(tmpl.generate()))
181d292eafa2 Interpolate multiline expressions in templates. Thanks to Christian Boos for reporting the problem and providing the fix.
cmlenz
parents: 181
diff changeset
675
36
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
676
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
677 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
678 """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
679
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
680 def test_strip_false(self):
61
448792ab1303 Use a different namespace than Kid uses.
cmlenz
parents: 54
diff changeset
681 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
682 <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
683 </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
684 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
685 <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
686 </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
687
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
688 def test_strip_empty(self):
61
448792ab1303 Use a different namespace than Kid uses.
cmlenz
parents: 54
diff changeset
689 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
690 <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
691 </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
692 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
693 <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
694 </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
695
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
696
104
f12e7987d7f4 Added `py:with` directive based on Jonas' patch in #17.
cmlenz
parents: 90
diff changeset
697 class WithDirectiveTestCase(unittest.TestCase):
f12e7987d7f4 Added `py:with` directive based on Jonas' patch in #17.
cmlenz
parents: 90
diff changeset
698 """Tests for the `py:with` template directive."""
f12e7987d7f4 Added `py:with` directive based on Jonas' patch in #17.
cmlenz
parents: 90
diff changeset
699
f12e7987d7f4 Added `py:with` directive based on Jonas' patch in #17.
cmlenz
parents: 90
diff changeset
700 def test_shadowing(self):
f12e7987d7f4 Added `py:with` directive based on Jonas' patch in #17.
cmlenz
parents: 90
diff changeset
701 tmpl = Template("""<div xmlns:py="http://markup.edgewall.org/">
f12e7987d7f4 Added `py:with` directive based on Jonas' patch in #17.
cmlenz
parents: 90
diff changeset
702 ${x}
f12e7987d7f4 Added `py:with` directive based on Jonas' patch in #17.
cmlenz
parents: 90
diff changeset
703 <span py:with="x = x * 2" py:replace="x"/>
f12e7987d7f4 Added `py:with` directive based on Jonas' patch in #17.
cmlenz
parents: 90
diff changeset
704 ${x}
f12e7987d7f4 Added `py:with` directive based on Jonas' patch in #17.
cmlenz
parents: 90
diff changeset
705 </div>""")
f12e7987d7f4 Added `py:with` directive based on Jonas' patch in #17.
cmlenz
parents: 90
diff changeset
706 self.assertEqual("""<div>
f12e7987d7f4 Added `py:with` directive based on Jonas' patch in #17.
cmlenz
parents: 90
diff changeset
707 42
f12e7987d7f4 Added `py:with` directive based on Jonas' patch in #17.
cmlenz
parents: 90
diff changeset
708 84
f12e7987d7f4 Added `py:with` directive based on Jonas' patch in #17.
cmlenz
parents: 90
diff changeset
709 42
149
537f819c547b `Template.generate()` now accepts the context data as keyword arguments, so that you don't have to import the `Context` class every time you want to pass data into a template.
cmlenz
parents: 145
diff changeset
710 </div>""", str(tmpl.generate(x=42)))
104
f12e7987d7f4 Added `py:with` directive based on Jonas' patch in #17.
cmlenz
parents: 90
diff changeset
711
f12e7987d7f4 Added `py:with` directive based on Jonas' patch in #17.
cmlenz
parents: 90
diff changeset
712 def test_as_element(self):
f12e7987d7f4 Added `py:with` directive based on Jonas' patch in #17.
cmlenz
parents: 90
diff changeset
713 tmpl = Template("""<div xmlns:py="http://markup.edgewall.org/">
f12e7987d7f4 Added `py:with` directive based on Jonas' patch in #17.
cmlenz
parents: 90
diff changeset
714 <py:with vars="x = x * 2">${x}</py:with>
f12e7987d7f4 Added `py:with` directive based on Jonas' patch in #17.
cmlenz
parents: 90
diff changeset
715 </div>""")
f12e7987d7f4 Added `py:with` directive based on Jonas' patch in #17.
cmlenz
parents: 90
diff changeset
716 self.assertEqual("""<div>
f12e7987d7f4 Added `py:with` directive based on Jonas' patch in #17.
cmlenz
parents: 90
diff changeset
717 84
149
537f819c547b `Template.generate()` now accepts the context data as keyword arguments, so that you don't have to import the `Context` class every time you want to pass data into a template.
cmlenz
parents: 145
diff changeset
718 </div>""", str(tmpl.generate(x=42)))
104
f12e7987d7f4 Added `py:with` directive based on Jonas' patch in #17.
cmlenz
parents: 90
diff changeset
719
190
769d945ac030 Improvements for the `py:with` directive:
cmlenz
parents: 185
diff changeset
720 def test_multiple_vars_same_name(self):
769d945ac030 Improvements for the `py:with` directive:
cmlenz
parents: 185
diff changeset
721 tmpl = Template("""<div xmlns:py="http://markup.edgewall.org/">
769d945ac030 Improvements for the `py:with` directive:
cmlenz
parents: 185
diff changeset
722 <py:with vars="
769d945ac030 Improvements for the `py:with` directive:
cmlenz
parents: 185
diff changeset
723 foo = 'bar';
769d945ac030 Improvements for the `py:with` directive:
cmlenz
parents: 185
diff changeset
724 foo = foo.replace('r', 'z')
769d945ac030 Improvements for the `py:with` directive:
cmlenz
parents: 185
diff changeset
725 ">
769d945ac030 Improvements for the `py:with` directive:
cmlenz
parents: 185
diff changeset
726 $foo
769d945ac030 Improvements for the `py:with` directive:
cmlenz
parents: 185
diff changeset
727 </py:with>
769d945ac030 Improvements for the `py:with` directive:
cmlenz
parents: 185
diff changeset
728 </div>""")
769d945ac030 Improvements for the `py:with` directive:
cmlenz
parents: 185
diff changeset
729 self.assertEqual("""<div>
769d945ac030 Improvements for the `py:with` directive:
cmlenz
parents: 185
diff changeset
730 baz
769d945ac030 Improvements for the `py:with` directive:
cmlenz
parents: 185
diff changeset
731 </div>""", str(tmpl.generate(x=42)))
769d945ac030 Improvements for the `py:with` directive:
cmlenz
parents: 185
diff changeset
732
769d945ac030 Improvements for the `py:with` directive:
cmlenz
parents: 185
diff changeset
733 def test_multiple_vars_single_assignment(self):
769d945ac030 Improvements for the `py:with` directive:
cmlenz
parents: 185
diff changeset
734 tmpl = Template("""<div xmlns:py="http://markup.edgewall.org/">
769d945ac030 Improvements for the `py:with` directive:
cmlenz
parents: 185
diff changeset
735 <py:with vars="x = y = z = 1">${x} ${y} ${z}</py:with>
769d945ac030 Improvements for the `py:with` directive:
cmlenz
parents: 185
diff changeset
736 </div>""")
769d945ac030 Improvements for the `py:with` directive:
cmlenz
parents: 185
diff changeset
737 self.assertEqual("""<div>
769d945ac030 Improvements for the `py:with` directive:
cmlenz
parents: 185
diff changeset
738 1 1 1
769d945ac030 Improvements for the `py:with` directive:
cmlenz
parents: 185
diff changeset
739 </div>""", str(tmpl.generate(x=42)))
769d945ac030 Improvements for the `py:with` directive:
cmlenz
parents: 185
diff changeset
740
220
f4943d2babda Fix for #45 and #46: properly support assignment to nested tuples in `py:for` and `py:with` directives.
cmlenz
parents: 217
diff changeset
741 def test_nested_vars_single_assignment(self):
f4943d2babda Fix for #45 and #46: properly support assignment to nested tuples in `py:for` and `py:with` directives.
cmlenz
parents: 217
diff changeset
742 tmpl = Template("""<div xmlns:py="http://markup.edgewall.org/">
f4943d2babda Fix for #45 and #46: properly support assignment to nested tuples in `py:for` and `py:with` directives.
cmlenz
parents: 217
diff changeset
743 <py:with vars="x, (y, z) = (1, (2, 3))">${x} ${y} ${z}</py:with>
f4943d2babda Fix for #45 and #46: properly support assignment to nested tuples in `py:for` and `py:with` directives.
cmlenz
parents: 217
diff changeset
744 </div>""")
f4943d2babda Fix for #45 and #46: properly support assignment to nested tuples in `py:for` and `py:with` directives.
cmlenz
parents: 217
diff changeset
745 self.assertEqual("""<div>
f4943d2babda Fix for #45 and #46: properly support assignment to nested tuples in `py:for` and `py:with` directives.
cmlenz
parents: 217
diff changeset
746 1 2 3
f4943d2babda Fix for #45 and #46: properly support assignment to nested tuples in `py:for` and `py:with` directives.
cmlenz
parents: 217
diff changeset
747 </div>""", str(tmpl.generate(x=42)))
f4943d2babda Fix for #45 and #46: properly support assignment to nested tuples in `py:for` and `py:with` directives.
cmlenz
parents: 217
diff changeset
748
190
769d945ac030 Improvements for the `py:with` directive:
cmlenz
parents: 185
diff changeset
749 def test_multiple_vars_trailing_semicolon(self):
769d945ac030 Improvements for the `py:with` directive:
cmlenz
parents: 185
diff changeset
750 tmpl = Template("""<div xmlns:py="http://markup.edgewall.org/">
769d945ac030 Improvements for the `py:with` directive:
cmlenz
parents: 185
diff changeset
751 <py:with vars="x = x * 2; y = x / 2;">${x} ${y}</py:with>
769d945ac030 Improvements for the `py:with` directive:
cmlenz
parents: 185
diff changeset
752 </div>""")
769d945ac030 Improvements for the `py:with` directive:
cmlenz
parents: 185
diff changeset
753 self.assertEqual("""<div>
769d945ac030 Improvements for the `py:with` directive:
cmlenz
parents: 185
diff changeset
754 84 42
769d945ac030 Improvements for the `py:with` directive:
cmlenz
parents: 185
diff changeset
755 </div>""", str(tmpl.generate(x=42)))
769d945ac030 Improvements for the `py:with` directive:
cmlenz
parents: 185
diff changeset
756
769d945ac030 Improvements for the `py:with` directive:
cmlenz
parents: 185
diff changeset
757 def test_semicolon_escape(self):
769d945ac030 Improvements for the `py:with` directive:
cmlenz
parents: 185
diff changeset
758 tmpl = Template("""<div xmlns:py="http://markup.edgewall.org/">
769d945ac030 Improvements for the `py:with` directive:
cmlenz
parents: 185
diff changeset
759 <py:with vars="x = 'here is a semicolon: ;'; y = 'here are two semicolons: ;;' ;">
769d945ac030 Improvements for the `py:with` directive:
cmlenz
parents: 185
diff changeset
760 ${x}
769d945ac030 Improvements for the `py:with` directive:
cmlenz
parents: 185
diff changeset
761 ${y}
769d945ac030 Improvements for the `py:with` directive:
cmlenz
parents: 185
diff changeset
762 </py:with>
769d945ac030 Improvements for the `py:with` directive:
cmlenz
parents: 185
diff changeset
763 </div>""")
769d945ac030 Improvements for the `py:with` directive:
cmlenz
parents: 185
diff changeset
764 self.assertEqual("""<div>
769d945ac030 Improvements for the `py:with` directive:
cmlenz
parents: 185
diff changeset
765 here is a semicolon: ;
769d945ac030 Improvements for the `py:with` directive:
cmlenz
parents: 185
diff changeset
766 here are two semicolons: ;;
769d945ac030 Improvements for the `py:with` directive:
cmlenz
parents: 185
diff changeset
767 </div>""", str(tmpl.generate()))
769d945ac030 Improvements for the `py:with` directive:
cmlenz
parents: 185
diff changeset
768
104
f12e7987d7f4 Added `py:with` directive based on Jonas' patch in #17.
cmlenz
parents: 90
diff changeset
769
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
770 class TemplateTestCase(unittest.TestCase):
36
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
771 """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
772 reporting.
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
773 """
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
774
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
775 def test_interpolate_string(self):
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
776 parts = list(Template._interpolate('bla'))
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
777 self.assertEqual(1, len(parts))
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
778 self.assertEqual(Stream.TEXT, parts[0][0])
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
779 self.assertEqual('bla', parts[0][1])
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
780
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
781 def test_interpolate_simple(self):
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
782 parts = list(Template._interpolate('${bla}'))
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
783 self.assertEqual(1, len(parts))
10
f77f7a91aa46 Moved the template-specific stream event kinds into the template module.
cmlenz
parents: 1
diff changeset
784 self.assertEqual(Template.EXPR, parts[0][0])
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
785 self.assertEqual('bla', parts[0][1].source)
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
786
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
787 def test_interpolate_escaped(self):
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
788 parts = list(Template._interpolate('$${bla}'))
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
789 self.assertEqual(1, len(parts))
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
790 self.assertEqual(Stream.TEXT, parts[0][0])
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
791 self.assertEqual('${bla}', parts[0][1])
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
792
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
793 def test_interpolate_short(self):
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
794 parts = list(Template._interpolate('$bla'))
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
795 self.assertEqual(1, len(parts))
10
f77f7a91aa46 Moved the template-specific stream event kinds into the template module.
cmlenz
parents: 1
diff changeset
796 self.assertEqual(Template.EXPR, parts[0][0])
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
797 self.assertEqual('bla', parts[0][1].source)
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
798
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
799 def test_interpolate_mixed1(self):
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
800 parts = list(Template._interpolate('$foo bar $baz'))
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
801 self.assertEqual(3, len(parts))
10
f77f7a91aa46 Moved the template-specific stream event kinds into the template module.
cmlenz
parents: 1
diff changeset
802 self.assertEqual(Template.EXPR, parts[0][0])
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
803 self.assertEqual('foo', parts[0][1].source)
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
804 self.assertEqual(Stream.TEXT, parts[1][0])
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
805 self.assertEqual(' bar ', parts[1][1])
10
f77f7a91aa46 Moved the template-specific stream event kinds into the template module.
cmlenz
parents: 1
diff changeset
806 self.assertEqual(Template.EXPR, parts[2][0])
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
807 self.assertEqual('baz', parts[2][1].source)
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
808
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
809 def test_interpolate_mixed2(self):
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
810 parts = list(Template._interpolate('foo $bar baz'))
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
811 self.assertEqual(3, len(parts))
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
812 self.assertEqual(Stream.TEXT, parts[0][0])
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
813 self.assertEqual('foo ', parts[0][1])
10
f77f7a91aa46 Moved the template-specific stream event kinds into the template module.
cmlenz
parents: 1
diff changeset
814 self.assertEqual(Template.EXPR, parts[1][0])
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
815 self.assertEqual('bar', parts[1][1].source)
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
816 self.assertEqual(Stream.TEXT, parts[2][0])
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
817 self.assertEqual(' baz', parts[2][1])
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
818
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
819 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
820 tmpl = Template('<root> ${var} $var</root>')
149
537f819c547b `Template.generate()` now accepts the context data as keyword arguments, so that you don't have to import the `Context` class every time you want to pass data into a template.
cmlenz
parents: 145
diff changeset
821 self.assertEqual('<root> 42 42</root>', str(tmpl.generate(var=42)))
74
d54b5fd60b52 Fix expression interpolation where both shorthand notation and full notation are used inside a single text node. Thanks Jonas.
cmlenz
parents: 66
diff changeset
822
191
3289055a8c32 Allow leading whitespace in expressions. Closes #38. Thanks to Christian Boos for the patch!
cmlenz
parents: 190
diff changeset
823 def test_interpolate_leading_trailing_space(self):
3289055a8c32 Allow leading whitespace in expressions. Closes #38. Thanks to Christian Boos for the patch!
cmlenz
parents: 190
diff changeset
824 tmpl = Template('<root>${ foo }</root>')
3289055a8c32 Allow leading whitespace in expressions. Closes #38. Thanks to Christian Boos for the patch!
cmlenz
parents: 190
diff changeset
825 self.assertEqual('<root>bar</root>', str(tmpl.generate(foo='bar')))
3289055a8c32 Allow leading whitespace in expressions. Closes #38. Thanks to Christian Boos for the patch!
cmlenz
parents: 190
diff changeset
826
184
181d292eafa2 Interpolate multiline expressions in templates. Thanks to Christian Boos for reporting the problem and providing the fix.
cmlenz
parents: 181
diff changeset
827 def test_interpolate_multiline(self):
181d292eafa2 Interpolate multiline expressions in templates. Thanks to Christian Boos for reporting the problem and providing the fix.
cmlenz
parents: 181
diff changeset
828 tmpl = Template("""<root>${dict(
181d292eafa2 Interpolate multiline expressions in templates. Thanks to Christian Boos for reporting the problem and providing the fix.
cmlenz
parents: 181
diff changeset
829 bar = 'baz'
181d292eafa2 Interpolate multiline expressions in templates. Thanks to Christian Boos for reporting the problem and providing the fix.
cmlenz
parents: 181
diff changeset
830 )[foo]}</root>""")
181d292eafa2 Interpolate multiline expressions in templates. Thanks to Christian Boos for reporting the problem and providing the fix.
cmlenz
parents: 181
diff changeset
831 self.assertEqual('<root>baz</root>', str(tmpl.generate(foo='bar')))
181d292eafa2 Interpolate multiline expressions in templates. Thanks to Christian Boos for reporting the problem and providing the fix.
cmlenz
parents: 181
diff changeset
832
48
a5d585dd38c4 convert the result of expressions in attributes to strings so that values like ints are output correctly
mgood
parents: 37
diff changeset
833 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
834 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
835 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
836
145
47bbd9d2a5af * Fix error in expression evaluation when the expression evaluates to an iterable that does not produce event tuples.
cmlenz
parents: 134
diff changeset
837 def test_interpolate_list_result(self):
47bbd9d2a5af * Fix error in expression evaluation when the expression evaluates to an iterable that does not produce event tuples.
cmlenz
parents: 134
diff changeset
838 tmpl = Template('<root>$foo</root>')
149
537f819c547b `Template.generate()` now accepts the context data as keyword arguments, so that you don't have to import the `Context` class every time you want to pass data into a template.
cmlenz
parents: 145
diff changeset
839 self.assertEqual('<root>buzz</root>', str(tmpl.generate(foo=('buzz',))))
145
47bbd9d2a5af * Fix error in expression evaluation when the expression evaluates to an iterable that does not produce event tuples.
cmlenz
parents: 134
diff changeset
840
75
3722696d0343 Empty attributes in templates were being stripped out. Thanks to Jonas for the patch.
cmlenz
parents: 74
diff changeset
841 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
842 tmpl = Template('<root attr=""/>')
3722696d0343 Empty attributes in templates were being stripped out. Thanks to Jonas for the patch.
cmlenz
parents: 74
diff changeset
843 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
844
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
845 def test_bad_directive_error(self):
61
448792ab1303 Use a different namespace than Kid uses.
cmlenz
parents: 54
diff changeset
846 xml = '<p xmlns:py="http://markup.edgewall.org/" py:do="nothing" />'
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
847 try:
21
b4d17897d053 * Include paths are now interpreted relative to the path of the including template. Closes #3.
cmlenz
parents: 10
diff changeset
848 tmpl = Template(xml, filename='test.html')
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
849 except BadDirectiveError, e:
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
850 self.assertEqual('test.html', e.filename)
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
851 if sys.version_info[:2] >= (2, 4):
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
852 self.assertEqual(1, e.lineno)
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
853
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
854 def test_directive_value_syntax_error(self):
81
d60486018004 Template expressions are now compiled to Python bytecode.
cmlenz
parents: 77
diff changeset
855 xml = """<p xmlns:py="http://markup.edgewall.org/" py:if="bar'" />"""
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
856 try:
81
d60486018004 Template expressions are now compiled to Python bytecode.
cmlenz
parents: 77
diff changeset
857 tmpl = Template(xml, filename='test.html')
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
858 self.fail('Expected SyntaxError')
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
859 except TemplateSyntaxError, e:
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
860 self.assertEqual('test.html', e.filename)
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
861 if sys.version_info[:2] >= (2, 4):
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
862 self.assertEqual(1, e.lineno)
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
863
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
864 def test_expression_syntax_error(self):
81
d60486018004 Template expressions are now compiled to Python bytecode.
cmlenz
parents: 77
diff changeset
865 xml = """<p>
d60486018004 Template expressions are now compiled to Python bytecode.
cmlenz
parents: 77
diff changeset
866 Foo <em>${bar"}</em>
d60486018004 Template expressions are now compiled to Python bytecode.
cmlenz
parents: 77
diff changeset
867 </p>"""
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
868 try:
81
d60486018004 Template expressions are now compiled to Python bytecode.
cmlenz
parents: 77
diff changeset
869 tmpl = Template(xml, filename='test.html')
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
870 self.fail('Expected SyntaxError')
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
871 except TemplateSyntaxError, e:
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
872 self.assertEqual('test.html', e.filename)
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
873 if sys.version_info[:2] >= (2, 4):
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
874 self.assertEqual(2, e.lineno)
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
875
134
d681d2c3cd8d * Improve the accuracy of line numbers for text nodes, so that reported errors about syntax or evaluation errors in expressions point to the right line (not quite perfect yet, though).
cmlenz
parents: 104
diff changeset
876 def test_expression_syntax_error_multi_line(self):
d681d2c3cd8d * Improve the accuracy of line numbers for text nodes, so that reported errors about syntax or evaluation errors in expressions point to the right line (not quite perfect yet, though).
cmlenz
parents: 104
diff changeset
877 xml = """<p><em></em>
d681d2c3cd8d * Improve the accuracy of line numbers for text nodes, so that reported errors about syntax or evaluation errors in expressions point to the right line (not quite perfect yet, though).
cmlenz
parents: 104
diff changeset
878
d681d2c3cd8d * Improve the accuracy of line numbers for text nodes, so that reported errors about syntax or evaluation errors in expressions point to the right line (not quite perfect yet, though).
cmlenz
parents: 104
diff changeset
879 ${bar"}
d681d2c3cd8d * Improve the accuracy of line numbers for text nodes, so that reported errors about syntax or evaluation errors in expressions point to the right line (not quite perfect yet, though).
cmlenz
parents: 104
diff changeset
880
d681d2c3cd8d * Improve the accuracy of line numbers for text nodes, so that reported errors about syntax or evaluation errors in expressions point to the right line (not quite perfect yet, though).
cmlenz
parents: 104
diff changeset
881 </p>"""
d681d2c3cd8d * Improve the accuracy of line numbers for text nodes, so that reported errors about syntax or evaluation errors in expressions point to the right line (not quite perfect yet, though).
cmlenz
parents: 104
diff changeset
882 try:
d681d2c3cd8d * Improve the accuracy of line numbers for text nodes, so that reported errors about syntax or evaluation errors in expressions point to the right line (not quite perfect yet, though).
cmlenz
parents: 104
diff changeset
883 tmpl = Template(xml, filename='test.html')
d681d2c3cd8d * Improve the accuracy of line numbers for text nodes, so that reported errors about syntax or evaluation errors in expressions point to the right line (not quite perfect yet, though).
cmlenz
parents: 104
diff changeset
884 self.fail('Expected SyntaxError')
d681d2c3cd8d * Improve the accuracy of line numbers for text nodes, so that reported errors about syntax or evaluation errors in expressions point to the right line (not quite perfect yet, though).
cmlenz
parents: 104
diff changeset
885 except TemplateSyntaxError, e:
d681d2c3cd8d * Improve the accuracy of line numbers for text nodes, so that reported errors about syntax or evaluation errors in expressions point to the right line (not quite perfect yet, though).
cmlenz
parents: 104
diff changeset
886 self.assertEqual('test.html', e.filename)
d681d2c3cd8d * Improve the accuracy of line numbers for text nodes, so that reported errors about syntax or evaluation errors in expressions point to the right line (not quite perfect yet, though).
cmlenz
parents: 104
diff changeset
887 if sys.version_info[:2] >= (2, 4):
d681d2c3cd8d * Improve the accuracy of line numbers for text nodes, so that reported errors about syntax or evaluation errors in expressions point to the right line (not quite perfect yet, though).
cmlenz
parents: 104
diff changeset
888 self.assertEqual(3, e.lineno)
d681d2c3cd8d * Improve the accuracy of line numbers for text nodes, so that reported errors about syntax or evaluation errors in expressions point to the right line (not quite perfect yet, though).
cmlenz
parents: 104
diff changeset
889
54
1f3cd91325d9 Fix a number of escaping problems:
cmlenz
parents: 53
diff changeset
890 def test_markup_noescape(self):
1f3cd91325d9 Fix a number of escaping problems:
cmlenz
parents: 53
diff changeset
891 """
1f3cd91325d9 Fix a number of escaping problems:
cmlenz
parents: 53
diff changeset
892 Verify that outputting context data that is a `Markup` instance is not
1f3cd91325d9 Fix a number of escaping problems:
cmlenz
parents: 53
diff changeset
893 escaped.
1f3cd91325d9 Fix a number of escaping problems:
cmlenz
parents: 53
diff changeset
894 """
61
448792ab1303 Use a different namespace than Kid uses.
cmlenz
parents: 54
diff changeset
895 tmpl = Template("""<div xmlns:py="http://markup.edgewall.org/">
54
1f3cd91325d9 Fix a number of escaping problems:
cmlenz
parents: 53
diff changeset
896 $myvar
1f3cd91325d9 Fix a number of escaping problems:
cmlenz
parents: 53
diff changeset
897 </div>""")
1f3cd91325d9 Fix a number of escaping problems:
cmlenz
parents: 53
diff changeset
898 self.assertEqual("""<div>
1f3cd91325d9 Fix a number of escaping problems:
cmlenz
parents: 53
diff changeset
899 <b>foo</b>
149
537f819c547b `Template.generate()` now accepts the context data as keyword arguments, so that you don't have to import the `Context` class every time you want to pass data into a template.
cmlenz
parents: 145
diff changeset
900 </div>""", str(tmpl.generate(myvar=Markup('<b>foo</b>'))))
54
1f3cd91325d9 Fix a number of escaping problems:
cmlenz
parents: 53
diff changeset
901
1f3cd91325d9 Fix a number of escaping problems:
cmlenz
parents: 53
diff changeset
902 def test_text_noescape_quotes(self):
1f3cd91325d9 Fix a number of escaping problems:
cmlenz
parents: 53
diff changeset
903 """
1f3cd91325d9 Fix a number of escaping problems:
cmlenz
parents: 53
diff changeset
904 Verify that outputting context data in text nodes doesn't escape quotes.
1f3cd91325d9 Fix a number of escaping problems:
cmlenz
parents: 53
diff changeset
905 """
61
448792ab1303 Use a different namespace than Kid uses.
cmlenz
parents: 54
diff changeset
906 tmpl = Template("""<div xmlns:py="http://markup.edgewall.org/">
54
1f3cd91325d9 Fix a number of escaping problems:
cmlenz
parents: 53
diff changeset
907 $myvar
1f3cd91325d9 Fix a number of escaping problems:
cmlenz
parents: 53
diff changeset
908 </div>""")
1f3cd91325d9 Fix a number of escaping problems:
cmlenz
parents: 53
diff changeset
909 self.assertEqual("""<div>
1f3cd91325d9 Fix a number of escaping problems:
cmlenz
parents: 53
diff changeset
910 "foo"
149
537f819c547b `Template.generate()` now accepts the context data as keyword arguments, so that you don't have to import the `Context` class every time you want to pass data into a template.
cmlenz
parents: 145
diff changeset
911 </div>""", str(tmpl.generate(myvar='"foo"')))
54
1f3cd91325d9 Fix a number of escaping problems:
cmlenz
parents: 53
diff changeset
912
1f3cd91325d9 Fix a number of escaping problems:
cmlenz
parents: 53
diff changeset
913 def test_attr_escape_quotes(self):
1f3cd91325d9 Fix a number of escaping problems:
cmlenz
parents: 53
diff changeset
914 """
1f3cd91325d9 Fix a number of escaping problems:
cmlenz
parents: 53
diff changeset
915 Verify that outputting context data in attribtes escapes quotes.
1f3cd91325d9 Fix a number of escaping problems:
cmlenz
parents: 53
diff changeset
916 """
61
448792ab1303 Use a different namespace than Kid uses.
cmlenz
parents: 54
diff changeset
917 tmpl = Template("""<div xmlns:py="http://markup.edgewall.org/">
54
1f3cd91325d9 Fix a number of escaping problems:
cmlenz
parents: 53
diff changeset
918 <elem class="$myvar"/>
1f3cd91325d9 Fix a number of escaping problems:
cmlenz
parents: 53
diff changeset
919 </div>""")
1f3cd91325d9 Fix a number of escaping problems:
cmlenz
parents: 53
diff changeset
920 self.assertEqual("""<div>
1f3cd91325d9 Fix a number of escaping problems:
cmlenz
parents: 53
diff changeset
921 <elem class="&#34;foo&#34;"/>
149
537f819c547b `Template.generate()` now accepts the context data as keyword arguments, so that you don't have to import the `Context` class every time you want to pass data into a template.
cmlenz
parents: 145
diff changeset
922 </div>""", str(tmpl.generate(myvar='"foo"')))
54
1f3cd91325d9 Fix a number of escaping problems:
cmlenz
parents: 53
diff changeset
923
65
b3fdf93057ab Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents: 61
diff changeset
924 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
925 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
926 <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
927 </div>""")
b3fdf93057ab Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents: 61
diff changeset
928 self.assertEqual("""<div>
b3fdf93057ab Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents: 61
diff changeset
929 bar
149
537f819c547b `Template.generate()` now accepts the context data as keyword arguments, so that you don't have to import the `Context` class every time you want to pass data into a template.
cmlenz
parents: 145
diff changeset
930 </div>""", str(tmpl.generate(myvar='"foo"')))
65
b3fdf93057ab Support the use of directives as elements to reduce the need for using `py:strip`.
cmlenz
parents: 61
diff changeset
931
89
80386d62814f Support comments in templates that are not included in the output, in the same way Kid does: if the comment text starts with a `!` character, it is stripped from the output.
cmlenz
parents: 81
diff changeset
932 def test_normal_comment(self):
80386d62814f Support comments in templates that are not included in the output, in the same way Kid does: if the comment text starts with a `!` character, it is stripped from the output.
cmlenz
parents: 81
diff changeset
933 tmpl = Template("""<div xmlns:py="http://markup.edgewall.org/">
80386d62814f Support comments in templates that are not included in the output, in the same way Kid does: if the comment text starts with a `!` character, it is stripped from the output.
cmlenz
parents: 81
diff changeset
934 <!-- foo bar -->
80386d62814f Support comments in templates that are not included in the output, in the same way Kid does: if the comment text starts with a `!` character, it is stripped from the output.
cmlenz
parents: 81
diff changeset
935 </div>""")
80386d62814f Support comments in templates that are not included in the output, in the same way Kid does: if the comment text starts with a `!` character, it is stripped from the output.
cmlenz
parents: 81
diff changeset
936 self.assertEqual("""<div>
80386d62814f Support comments in templates that are not included in the output, in the same way Kid does: if the comment text starts with a `!` character, it is stripped from the output.
cmlenz
parents: 81
diff changeset
937 <!-- foo bar -->
80386d62814f Support comments in templates that are not included in the output, in the same way Kid does: if the comment text starts with a `!` character, it is stripped from the output.
cmlenz
parents: 81
diff changeset
938 </div>""", str(tmpl.generate()))
80386d62814f Support comments in templates that are not included in the output, in the same way Kid does: if the comment text starts with a `!` character, it is stripped from the output.
cmlenz
parents: 81
diff changeset
939
80386d62814f Support comments in templates that are not included in the output, in the same way Kid does: if the comment text starts with a `!` character, it is stripped from the output.
cmlenz
parents: 81
diff changeset
940 def test_template_comment(self):
80386d62814f Support comments in templates that are not included in the output, in the same way Kid does: if the comment text starts with a `!` character, it is stripped from the output.
cmlenz
parents: 81
diff changeset
941 tmpl = Template("""<div xmlns:py="http://markup.edgewall.org/">
80386d62814f Support comments in templates that are not included in the output, in the same way Kid does: if the comment text starts with a `!` character, it is stripped from the output.
cmlenz
parents: 81
diff changeset
942 <!-- !foo -->
80386d62814f Support comments in templates that are not included in the output, in the same way Kid does: if the comment text starts with a `!` character, it is stripped from the output.
cmlenz
parents: 81
diff changeset
943 <!--!bar-->
80386d62814f Support comments in templates that are not included in the output, in the same way Kid does: if the comment text starts with a `!` character, it is stripped from the output.
cmlenz
parents: 81
diff changeset
944 </div>""")
80386d62814f Support comments in templates that are not included in the output, in the same way Kid does: if the comment text starts with a `!` character, it is stripped from the output.
cmlenz
parents: 81
diff changeset
945 self.assertEqual("""<div>
80386d62814f Support comments in templates that are not included in the output, in the same way Kid does: if the comment text starts with a `!` character, it is stripped from the output.
cmlenz
parents: 81
diff changeset
946 </div>""", str(tmpl.generate()))
80386d62814f Support comments in templates that are not included in the output, in the same way Kid does: if the comment text starts with a `!` character, it is stripped from the output.
cmlenz
parents: 81
diff changeset
947
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
948
152
cdb2a1f930e2 Add some tests for relative template includes (see #27).
cmlenz
parents: 149
diff changeset
949 class TemplateLoaderTestCase(unittest.TestCase):
cdb2a1f930e2 Add some tests for relative template includes (see #27).
cmlenz
parents: 149
diff changeset
950 """Tests for the template loader."""
cdb2a1f930e2 Add some tests for relative template includes (see #27).
cmlenz
parents: 149
diff changeset
951
cdb2a1f930e2 Add some tests for relative template includes (see #27).
cmlenz
parents: 149
diff changeset
952 def setUp(self):
cdb2a1f930e2 Add some tests for relative template includes (see #27).
cmlenz
parents: 149
diff changeset
953 self.dirname = tempfile.mkdtemp(suffix='markup_test')
cdb2a1f930e2 Add some tests for relative template includes (see #27).
cmlenz
parents: 149
diff changeset
954
cdb2a1f930e2 Add some tests for relative template includes (see #27).
cmlenz
parents: 149
diff changeset
955 def tearDown(self):
cdb2a1f930e2 Add some tests for relative template includes (see #27).
cmlenz
parents: 149
diff changeset
956 shutil.rmtree(self.dirname)
cdb2a1f930e2 Add some tests for relative template includes (see #27).
cmlenz
parents: 149
diff changeset
957
cdb2a1f930e2 Add some tests for relative template includes (see #27).
cmlenz
parents: 149
diff changeset
958 def test_relative_include_samedir(self):
cdb2a1f930e2 Add some tests for relative template includes (see #27).
cmlenz
parents: 149
diff changeset
959 file1 = open(os.path.join(self.dirname, 'tmpl1.html'), 'w')
cdb2a1f930e2 Add some tests for relative template includes (see #27).
cmlenz
parents: 149
diff changeset
960 try:
cdb2a1f930e2 Add some tests for relative template includes (see #27).
cmlenz
parents: 149
diff changeset
961 file1.write("""<div>Included</div>""")
cdb2a1f930e2 Add some tests for relative template includes (see #27).
cmlenz
parents: 149
diff changeset
962 finally:
cdb2a1f930e2 Add some tests for relative template includes (see #27).
cmlenz
parents: 149
diff changeset
963 file1.close()
cdb2a1f930e2 Add some tests for relative template includes (see #27).
cmlenz
parents: 149
diff changeset
964
cdb2a1f930e2 Add some tests for relative template includes (see #27).
cmlenz
parents: 149
diff changeset
965 file2 = open(os.path.join(self.dirname, 'tmpl2.html'), 'w')
cdb2a1f930e2 Add some tests for relative template includes (see #27).
cmlenz
parents: 149
diff changeset
966 try:
cdb2a1f930e2 Add some tests for relative template includes (see #27).
cmlenz
parents: 149
diff changeset
967 file2.write("""<html xmlns:xi="http://www.w3.org/2001/XInclude">
158
3f23fafeef99 * Add test case for SVG content embedded in an HTML document.
cmlenz
parents: 154
diff changeset
968 <xi:include href="tmpl1.html" />
3f23fafeef99 * Add test case for SVG content embedded in an HTML document.
cmlenz
parents: 154
diff changeset
969 </html>""")
152
cdb2a1f930e2 Add some tests for relative template includes (see #27).
cmlenz
parents: 149
diff changeset
970 finally:
cdb2a1f930e2 Add some tests for relative template includes (see #27).
cmlenz
parents: 149
diff changeset
971 file2.close()
cdb2a1f930e2 Add some tests for relative template includes (see #27).
cmlenz
parents: 149
diff changeset
972
cdb2a1f930e2 Add some tests for relative template includes (see #27).
cmlenz
parents: 149
diff changeset
973 loader = TemplateLoader([self.dirname])
cdb2a1f930e2 Add some tests for relative template includes (see #27).
cmlenz
parents: 149
diff changeset
974 tmpl = loader.load('tmpl2.html')
cdb2a1f930e2 Add some tests for relative template includes (see #27).
cmlenz
parents: 149
diff changeset
975 self.assertEqual("""<html>
158
3f23fafeef99 * Add test case for SVG content embedded in an HTML document.
cmlenz
parents: 154
diff changeset
976 <div>Included</div>
3f23fafeef99 * Add test case for SVG content embedded in an HTML document.
cmlenz
parents: 154
diff changeset
977 </html>""", tmpl.generate().render())
152
cdb2a1f930e2 Add some tests for relative template includes (see #27).
cmlenz
parents: 149
diff changeset
978
cdb2a1f930e2 Add some tests for relative template includes (see #27).
cmlenz
parents: 149
diff changeset
979 def test_relative_include_subdir(self):
cdb2a1f930e2 Add some tests for relative template includes (see #27).
cmlenz
parents: 149
diff changeset
980 os.mkdir(os.path.join(self.dirname, 'sub'))
cdb2a1f930e2 Add some tests for relative template includes (see #27).
cmlenz
parents: 149
diff changeset
981 file1 = open(os.path.join(self.dirname, 'sub', 'tmpl1.html'), 'w')
cdb2a1f930e2 Add some tests for relative template includes (see #27).
cmlenz
parents: 149
diff changeset
982 try:
cdb2a1f930e2 Add some tests for relative template includes (see #27).
cmlenz
parents: 149
diff changeset
983 file1.write("""<div>Included</div>""")
cdb2a1f930e2 Add some tests for relative template includes (see #27).
cmlenz
parents: 149
diff changeset
984 finally:
cdb2a1f930e2 Add some tests for relative template includes (see #27).
cmlenz
parents: 149
diff changeset
985 file1.close()
cdb2a1f930e2 Add some tests for relative template includes (see #27).
cmlenz
parents: 149
diff changeset
986
cdb2a1f930e2 Add some tests for relative template includes (see #27).
cmlenz
parents: 149
diff changeset
987 file2 = open(os.path.join(self.dirname, 'tmpl2.html'), 'w')
cdb2a1f930e2 Add some tests for relative template includes (see #27).
cmlenz
parents: 149
diff changeset
988 try:
cdb2a1f930e2 Add some tests for relative template includes (see #27).
cmlenz
parents: 149
diff changeset
989 file2.write("""<html xmlns:xi="http://www.w3.org/2001/XInclude">
158
3f23fafeef99 * Add test case for SVG content embedded in an HTML document.
cmlenz
parents: 154
diff changeset
990 <xi:include href="sub/tmpl1.html" />
3f23fafeef99 * Add test case for SVG content embedded in an HTML document.
cmlenz
parents: 154
diff changeset
991 </html>""")
152
cdb2a1f930e2 Add some tests for relative template includes (see #27).
cmlenz
parents: 149
diff changeset
992 finally:
cdb2a1f930e2 Add some tests for relative template includes (see #27).
cmlenz
parents: 149
diff changeset
993 file2.close()
cdb2a1f930e2 Add some tests for relative template includes (see #27).
cmlenz
parents: 149
diff changeset
994
cdb2a1f930e2 Add some tests for relative template includes (see #27).
cmlenz
parents: 149
diff changeset
995 loader = TemplateLoader([self.dirname])
cdb2a1f930e2 Add some tests for relative template includes (see #27).
cmlenz
parents: 149
diff changeset
996 tmpl = loader.load('tmpl2.html')
cdb2a1f930e2 Add some tests for relative template includes (see #27).
cmlenz
parents: 149
diff changeset
997 self.assertEqual("""<html>
158
3f23fafeef99 * Add test case for SVG content embedded in an HTML document.
cmlenz
parents: 154
diff changeset
998 <div>Included</div>
3f23fafeef99 * Add test case for SVG content embedded in an HTML document.
cmlenz
parents: 154
diff changeset
999 </html>""", tmpl.generate().render())
152
cdb2a1f930e2 Add some tests for relative template includes (see #27).
cmlenz
parents: 149
diff changeset
1000
cdb2a1f930e2 Add some tests for relative template includes (see #27).
cmlenz
parents: 149
diff changeset
1001 def test_relative_include_parentdir(self):
cdb2a1f930e2 Add some tests for relative template includes (see #27).
cmlenz
parents: 149
diff changeset
1002 file1 = open(os.path.join(self.dirname, 'tmpl1.html'), 'w')
cdb2a1f930e2 Add some tests for relative template includes (see #27).
cmlenz
parents: 149
diff changeset
1003 try:
cdb2a1f930e2 Add some tests for relative template includes (see #27).
cmlenz
parents: 149
diff changeset
1004 file1.write("""<div>Included</div>""")
cdb2a1f930e2 Add some tests for relative template includes (see #27).
cmlenz
parents: 149
diff changeset
1005 finally:
cdb2a1f930e2 Add some tests for relative template includes (see #27).
cmlenz
parents: 149
diff changeset
1006 file1.close()
cdb2a1f930e2 Add some tests for relative template includes (see #27).
cmlenz
parents: 149
diff changeset
1007
cdb2a1f930e2 Add some tests for relative template includes (see #27).
cmlenz
parents: 149
diff changeset
1008 os.mkdir(os.path.join(self.dirname, 'sub'))
cdb2a1f930e2 Add some tests for relative template includes (see #27).
cmlenz
parents: 149
diff changeset
1009 file2 = open(os.path.join(self.dirname, 'sub', 'tmpl2.html'), 'w')
cdb2a1f930e2 Add some tests for relative template includes (see #27).
cmlenz
parents: 149
diff changeset
1010 try:
cdb2a1f930e2 Add some tests for relative template includes (see #27).
cmlenz
parents: 149
diff changeset
1011 file2.write("""<html xmlns:xi="http://www.w3.org/2001/XInclude">
158
3f23fafeef99 * Add test case for SVG content embedded in an HTML document.
cmlenz
parents: 154
diff changeset
1012 <xi:include href="../tmpl1.html" />
3f23fafeef99 * Add test case for SVG content embedded in an HTML document.
cmlenz
parents: 154
diff changeset
1013 </html>""")
152
cdb2a1f930e2 Add some tests for relative template includes (see #27).
cmlenz
parents: 149
diff changeset
1014 finally:
cdb2a1f930e2 Add some tests for relative template includes (see #27).
cmlenz
parents: 149
diff changeset
1015 file2.close()
cdb2a1f930e2 Add some tests for relative template includes (see #27).
cmlenz
parents: 149
diff changeset
1016
cdb2a1f930e2 Add some tests for relative template includes (see #27).
cmlenz
parents: 149
diff changeset
1017 loader = TemplateLoader([self.dirname])
cdb2a1f930e2 Add some tests for relative template includes (see #27).
cmlenz
parents: 149
diff changeset
1018 tmpl = loader.load('sub/tmpl2.html')
cdb2a1f930e2 Add some tests for relative template includes (see #27).
cmlenz
parents: 149
diff changeset
1019 self.assertEqual("""<html>
158
3f23fafeef99 * Add test case for SVG content embedded in an HTML document.
cmlenz
parents: 154
diff changeset
1020 <div>Included</div>
3f23fafeef99 * Add test case for SVG content embedded in an HTML document.
cmlenz
parents: 154
diff changeset
1021 </html>""", tmpl.generate().render())
152
cdb2a1f930e2 Add some tests for relative template includes (see #27).
cmlenz
parents: 149
diff changeset
1022
174
a395c11cbced Added test case for includes without a search path.
cmlenz
parents: 172
diff changeset
1023 def test_relative_include_without_search_path(self):
a395c11cbced Added test case for includes without a search path.
cmlenz
parents: 172
diff changeset
1024 file1 = open(os.path.join(self.dirname, 'tmpl1.html'), 'w')
a395c11cbced Added test case for includes without a search path.
cmlenz
parents: 172
diff changeset
1025 try:
a395c11cbced Added test case for includes without a search path.
cmlenz
parents: 172
diff changeset
1026 file1.write("""<div>Included</div>""")
a395c11cbced Added test case for includes without a search path.
cmlenz
parents: 172
diff changeset
1027 finally:
a395c11cbced Added test case for includes without a search path.
cmlenz
parents: 172
diff changeset
1028 file1.close()
a395c11cbced Added test case for includes without a search path.
cmlenz
parents: 172
diff changeset
1029
a395c11cbced Added test case for includes without a search path.
cmlenz
parents: 172
diff changeset
1030 file2 = open(os.path.join(self.dirname, 'tmpl2.html'), 'w')
a395c11cbced Added test case for includes without a search path.
cmlenz
parents: 172
diff changeset
1031 try:
a395c11cbced Added test case for includes without a search path.
cmlenz
parents: 172
diff changeset
1032 file2.write("""<html xmlns:xi="http://www.w3.org/2001/XInclude">
a395c11cbced Added test case for includes without a search path.
cmlenz
parents: 172
diff changeset
1033 <xi:include href="tmpl1.html" />
a395c11cbced Added test case for includes without a search path.
cmlenz
parents: 172
diff changeset
1034 </html>""")
a395c11cbced Added test case for includes without a search path.
cmlenz
parents: 172
diff changeset
1035 finally:
a395c11cbced Added test case for includes without a search path.
cmlenz
parents: 172
diff changeset
1036 file2.close()
a395c11cbced Added test case for includes without a search path.
cmlenz
parents: 172
diff changeset
1037
a395c11cbced Added test case for includes without a search path.
cmlenz
parents: 172
diff changeset
1038 loader = TemplateLoader()
175
7f96149f28d5 Raise error when template search path is empty.
cmlenz
parents: 174
diff changeset
1039 tmpl = loader.load(os.path.join(self.dirname, 'tmpl2.html'))
174
a395c11cbced Added test case for includes without a search path.
cmlenz
parents: 172
diff changeset
1040 self.assertEqual("""<html>
a395c11cbced Added test case for includes without a search path.
cmlenz
parents: 172
diff changeset
1041 <div>Included</div>
a395c11cbced Added test case for includes without a search path.
cmlenz
parents: 172
diff changeset
1042 </html>""", tmpl.generate().render())
a395c11cbced Added test case for includes without a search path.
cmlenz
parents: 172
diff changeset
1043
152
cdb2a1f930e2 Add some tests for relative template includes (see #27).
cmlenz
parents: 149
diff changeset
1044
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
1045 def suite():
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
1046 suite = unittest.TestSuite()
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
1047 suite.addTest(doctest.DocTestSuite(Template.__module__))
50
d3842cd76e92 Fix the way multiple directives are applied to a single `SUB` in many cases by making the directives themselves responsible for applying any remaining directives.
cmlenz
parents: 48
diff changeset
1048 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
1049 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
1050 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
1051 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
1052 suite.addTest(unittest.makeSuite(IfDirectiveTestCase, 'test'))
36
ed370ebfa794 Fix for #7: match templates no longer process their own output.
cmlenz
parents: 27
diff changeset
1053 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
1054 suite.addTest(unittest.makeSuite(StripDirectiveTestCase, 'test'))
104
f12e7987d7f4 Added `py:with` directive based on Jonas' patch in #17.
cmlenz
parents: 90
diff changeset
1055 suite.addTest(unittest.makeSuite(WithDirectiveTestCase, 'test'))
152
cdb2a1f930e2 Add some tests for relative template includes (see #27).
cmlenz
parents: 149
diff changeset
1056 suite.addTest(unittest.makeSuite(TemplateTestCase, 'test'))
cdb2a1f930e2 Add some tests for relative template includes (see #27).
cmlenz
parents: 149
diff changeset
1057 suite.addTest(unittest.makeSuite(TemplateLoaderTestCase, 'test'))
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
1058 return suite
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
1059
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
1060 if __name__ == '__main__':
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
1061 unittest.main(defaultTest='suite')
Copyright (C) 2012-2017 Edgewall Software