-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest_qree.py
More file actions
executable file
·244 lines (226 loc) · 6.45 KB
/
test_qree.py
File metadata and controls
executable file
·244 lines (226 loc) · 6.45 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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
import qree;
import pytest;
# Run tests with:
# pytest tests.py -vv
def test_identity ():
assert qree.renderStr("plain") == "plain";
assert qree.renderStr('foo\nbar\nbaz') == 'foo\nbar\nbaz';
def test_basic_substitution ():
tpl = "Hello, {{: data + '!' :}}";
assert qree.renderStr(tpl, "World") == 'Hello, World!';
assert qree.renderStr(tpl, "> World") == 'Hello, > World!';
tpl2 = "Hello, {{= data + '!' =}}";
assert qree.renderStr(tpl2, "World") == 'Hello, World!';
assert qree.renderStr(tpl2, "> World") == 'Hello, > World!';
def test_nonPyQuotesOk ():
"Test that single-quotes _outside_ py-code are OK.";
qree.renderStr("Trip ''' quotes") == "Trip ''' quotes";
qree.renderStr("'{{: data :}}'", None) == "'None'";
def test_pyQuotesOk ():
"Test that single-quotes _within_ py-code are OK.";
qree.renderStr("{{: data['foo'] :}}", {"foo": "bar"}) == "bar";
qree.renderStr("{{: data['''foo'''] :}}", {"foo": "bar"}) == "bar";
def test_qreeSpecialTokenError ():
"Test that special token `__qree` is disallowed.";
badTplList = [
# 1, 2, 3:
"__qree",
"{{: data.__qree :}}",
"{{= data['__qree'] =}}",
# 4:
"""
@= import json;
@= data.update({"__qree": "foobar"});
{{: json.dumps(data) :}}
""",
];
assert len(badTplList) == 4;
for badTpl in badTplList:
with pytest.raises(SyntaxError) as e:
qree.renderStr(badTpl, data={});
assert type(e.value) is SyntaxError;
assert "special token" in e.value.args[0];
# Next:
goodTpl = """
@= import json;
@= data.update({"foo": "bar"});
{{: json.dumps(data) :}}
""";
expected = """
{"foo": "bar"}
""";
assert qree.renderStr(goodTpl, data={}) == expected;
def test_substitution_tag_error ():
"Test unclosed/nested substitution tags raise SyntaxError.";
badTplList =[
"Hello, {{: data + '!' ]]", # Unclosed
"Hello, {{= {{: data + '!' :}} =}}", # Nesting banned
"Hello, {{: {{= data + '!' =}} :}}",
"""
@= import qree;
{{: qree.renderStr('{{= data.title() =}}', data=data) :}}
""",
];
for badTpl in badTplList:
with pytest.raises(SyntaxError) as e:
qree.renderStr(badTpl, "World");
assert type(e.value) is SyntaxError;
assert "Tag-mismatch" in e.value.args[0];
def test_indent_tag_error ():
badTplList = [
# 1, 2 :
"@{", "@}",
# --------------------------
# 3:
"""
@= if True:
@{
Unclosed indent.
#
""",
# --------------------------
# 4:
"""
@= if True:
#
Unopened indent.
@}
""",
# --------------------------
# 5:
"""
@= if True:
@{
if True:
@{
Unclosed inner indent.
#
@}
""",
# --------------------------
# 6:
"""
@= if True:
@{ Non-comment line along w/ open-indent.
@}
""",
# --------------------------
# 7:
"""
@= if True:
@{
@} Non-comment line along w/ close-indent.
""",
];
assert len(badTplList) == 7;
for badTpl in badTplList:
with pytest.raises(IndentationError):
print(qree.renderStr(badTpl, data=None));
def test_basic_indent ():
tpl = """
@= for n in range(1, data+1):
@{
{{: n :}} is {{: 'EVEN' if n % 2 == 0 else 'ODD' :}}
@}
""";
expected = """
1 is ODD
2 is EVEN
3 is ODD
4 is EVEN
""";
assert qree.renderStr(tpl, data=4) == expected;
def test_nested_indent ():
tpl = """
@= for n in range(1, data+1):
@{
@= if n % 15 == 0:
@{ # Comment w/ indent-open.
FizzBuzz!
@}
@= elif n % 3 == 0:
@{
Fizz!
@} # Comment w/ indent-close.
@= elif n % 5 == 0:
@{
Buzz!
@}
@= else:
@{
{{: n :}}
@}
@}
""";
expected = """
1
2
Fizz!
4
Buzz!
Fizz!
7
8
Fizz!
Buzz!
11
Fizz!
13
14
FizzBuzz!
""";
assert qree.renderStr(tpl, data=15) == expected;
def test_renderPath_basic_plus_with_view ():
footerPath = "./test-views/footer.html";
expectedFooter = "\n".join([
'<footer class="footer">',
' <a href="/linkA">Link A</a>',
' <a href="/linkB">Link B</a>',
' <a href="/linkC">Link C</a>',
'</footer>',
'', # <-- Qree seems to like adding trailing newlines.
]);
print(repr(expectedFooter));
print(repr(qree.renderPath(footerPath)));
assert qree.renderPath(footerPath) == expectedFooter;
@qree.view(footerPath)
def serveFooter ():
return None;
assert serveFooter() == expectedFooter;
def test_renderPath_nested_plus_with_view ():
homepagePath = "./test-views/homepage.html";
data = {
"title": "... TITLE 2 ...",
"body": "... BODY 2 ...",
}
expectedHomepage = "\n".join([
'<doctype html>',
'<html>',
'<head><title>... TITLE 2 ...</title></head>',
'<body>',
'<header class="header">',
' <a href="/link1">Link 1</a>',
' <a href="/link2">Link 2</a>',
' <a href="/link3">Link 3</a>',
'</header>',
'', # <-- \n via nested call.
'<h2>... TITLE 2 ...</h2>',
'<pre>... BODY 2 ...</pre>',
'<footer class="footer">',
' <a href="/linkA">Link A</a>',
' <a href="/linkB">Link B</a>',
' <a href="/linkC">Link C</a>',
'</footer>',
'', # <-- \n via nested call.
'</body>',
'<html>',
'', # <-- Qree seems to like adding trailing newlines.
]);
assert qree.renderPath(homepagePath, data) == expectedHomepage;
@qree.view(homepagePath)
def serveHomepage ():
return data;
assert serveHomepage() == expectedHomepage;
print(expectedHomepage);
test_renderPath_nested_plus_with_view();
# End ######################################################