-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathparser_test.py
More file actions
213 lines (185 loc) · 4.1 KB
/
parser_test.py
File metadata and controls
213 lines (185 loc) · 4.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
from template import Template
from template.config import Config
from template.parser import Parser
from template.test import TestCase, main
class ParserTest(TestCase):
def testParser(self):
p2 = Parser({"START_TAG": r"\[\*",
"END_TAG": r"\*\]",
"ANYCASE": 1,
"PRE_CHOMP": 1,
"V1DOLLAR": 1})
# Test new/old styles.
s1 = p2.new_style({"TAG_STYLE": "metatext",
"PRE_CHOMP": 0,
"POST_CHOMP": 1})
self.assertTrue(s1)
self.assertEqual(r"%%", s1["START_TAG"])
self.assertEqual(0, s1["PRE_CHOMP"])
self.assertEqual(1, s1["POST_CHOMP"])
s2 = p2.old_style()
self.assertTrue(s2)
self.assertEqual(r"\[\*", s2["START_TAG"])
self.assertEqual(1, s2["PRE_CHOMP"])
self.assertEqual(0, s2["POST_CHOMP"])
p3 = Config.parser(
{"TAG_STYLE": "html", "POST_CHOMP": 1, "ANYCASE": 1, "INTERPOLATE": 1})
p4 = Config.parser({"ANYCASE": 0})
tt = (("tt1", Template({"ANYCASE": 1})),
("tt2", Template({"PARSER": p2})),
("tt3", Template({"PARSER": p3})),
("tt4", Template({"PARSER": p4})))
replace = self._callsign()
replace["alist"] = ["foo", 0, "bar", 0]
replace["wintxt"] = "foo\r\n\r\nbar\r\n\r\nbaz"
self.Expect(DATA, tt, replace)
DATA = r"""
#------------------------------------------------------------------------
# tt1
#------------------------------------------------------------------------
-- test --
start $a
[% BLOCK a %]
this is a
[% END %]
=[% INCLUDE a %]=
=[% include a %]=
end
-- expect --
start $a
=
this is a
=
=
this is a
=
end
#------------------------------------------------------------------------
# tt2
#------------------------------------------------------------------------
-- test --
-- use tt2 --
begin
[% this will be ignored %]
[* a *]
end
-- expect --
begin
[% this will be ignored %]alpha
end
-- test --
$b does nothing:
[* c = 'b'; 'hello' *]
stuff:
[* $c *]
-- expect --
$b does nothing: hello
stuff: b
#------------------------------------------------------------------------
# tt3
#------------------------------------------------------------------------
-- test --
-- use tt3 --
begin
[% this will be ignored %]
<!-- a -->
end
-- expect --
begin
[% this will be ignored %]
alphaend
-- test --
$b does something:
<!-- c = 'b'; 'hello' -->
stuff:
<!-- $c -->
end
-- expect --
bravo does something:
hellostuff:
bravoend
#------------------------------------------------------------------------
# tt4
#------------------------------------------------------------------------
-- test --
-- use tt4 --
start $a[% 'include' = 'hello world' %]
[% BLOCK a -%]
this is a
[%- END %]
=[% INCLUDE a %]=
=[% include %]=
end
-- expect --
start $a
=this is a=
=hello world=
end
#------------------------------------------------------------------------
-- test --
[% sql = "
SELECT *
FROM table"
-%]
SQL: [% sql %]
-- expect --
SQL:
SELECT *
FROM table
-- test --
[% a = "\a\b\c\ndef" -%]
a: [% a %]
-- expect --
a: abc
def
-- test --
[% a = "\f\o\o"
b = "a is '$a'"
c = "b is \$100"
-%]
a: [% a %] b: [% b %] c: [% c %]
-- expect --
a: foo b: a is 'foo' c: b is $100
-- test --
[% tag = {
a => "[\%"
z => "%\]"
}
quoted = "[\% INSERT foo %\]"
-%]
A directive looks like: [% tag.a %] INCLUDE foo [% tag.z %]
The quoted value is [% quoted %]
-- expect --
A directive looks like: [% INCLUDE foo %]
The quoted value is [% INSERT foo %]
-- test --
=[% wintxt | replace("(\r\n){2,}", "\n<break>\n") %]
-- expect --
=foo
<break>
bar
<break>
baz
-- test --
[% nl = "\n"
tab = "\t"
-%]
blah blah[% nl %][% tab %]x[% nl; tab %]y[% nl %]end
-- expect --
blah blah
x
y
end
#------------------------------------------------------------------------
# STOP RIGHT HERE!
#------------------------------------------------------------------------
-- stop --
-- test --
alist: [% $alist %]
-- expect --
alist: ??
-- test --
[% foo.bar.baz %]
"""
if __name__ == '__main__':
main()