annotate genshi/tests/template.py @ 245:b92a3648b9c1 trunk

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