-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_stpl.py
462 lines (391 loc) · 15.9 KB
/
test_stpl.py
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
# -*- coding: utf-8 -*-
from __future__ import with_statement
import unittest
from stpl import SimpleTemplate, TemplateError, view, template, touni, tob, html_quote
import re, os
import traceback
class chdir(object):
def __init__(self, dir):
if os.path.isfile(dir):
dir = os.path.dirname(dir)
self.wd = os.path.abspath(dir)
self.old = os.path.abspath('.')
def __enter__(self):
os.chdir(self.wd)
def __exit__(self, exc_type, exc_val, tb):
os.chdir(self.old)
class TestSimpleTemplate(unittest.TestCase):
def assertRenders(self, tpl, to, *args, **vars):
if isinstance(tpl, str):
tpl = SimpleTemplate(tpl, lookup=[os.path.join(os.path.dirname(__file__), 'views')])
self.assertEqual(touni(to), tpl.render(*args, **vars))
def test_string(self):
""" Templates: Parse string"""
self.assertRenders('start {{var}} end', 'start var end', var='var')
def test_self_as_variable_name(self):
self.assertRenders('start {{self}} end', 'start var end', {'self':'var'})
def test_file(self):
with chdir(__file__):
t = SimpleTemplate(name='./views/stpl_simple.tpl', lookup=['.'])
self.assertRenders(t, 'start var end\n', var='var')
def test_name(self):
with chdir(__file__):
t = SimpleTemplate(name='stpl_simple', lookup=['./views/'])
self.assertRenders(t, 'start var end\n', var='var')
def test_unicode(self):
self.assertRenders('start {{var}} end', 'start äöü end', var=touni('äöü'))
self.assertRenders('start {{var}} end', 'start äöü end', var=tob('äöü'))
def test_unicode_code(self):
""" Templates: utf8 code in file"""
with chdir(__file__):
t = SimpleTemplate(name='./views/stpl_unicode.tpl', lookup=['.'])
self.assertRenders(t, 'start ñç äöü end\n', var=touni('äöü'))
def test_import(self):
""" Templates: import statement"""
t = '%from base64 import b64encode\nstart {{b64encode(var.encode("ascii") if hasattr(var, "encode") else var)}} end'
self.assertRenders(t, 'start dmFy end', var='var')
def test_data(self):
""" Templates: Data representation """
t = SimpleTemplate('<{{var}}>')
self.assertRenders('<{{var}}>', '<True>', var=True)
self.assertRenders('<{{var}}>', '<False>', var=False)
self.assertRenders('<{{var}}>', '<>', var=None)
self.assertRenders('<{{var}}>', '<0>', var=0)
self.assertRenders('<{{var}}>', '<5>', var=5)
self.assertRenders('<{{var}}>', '<b>', var=tob('b'))
self.assertRenders('<{{var}}>', '<1.0>', var=1.0)
self.assertRenders('<{{var}}>', '<[1, 2]>', var=[1,2])
def test_htmlutils_quote(self):
self.assertEqual('"<' 	"\\>"', html_quote('<\'\r\n\t"\\>'));
def test_escape(self):
self.assertRenders('<{{var}}>', '<b>', var='b')
self.assertRenders('<{{var}}>', '<<&>>',var='<&>')
def test_noescape(self):
self.assertRenders('<{{!var}}>', '<b>', var='b')
self.assertRenders('<{{!var}}>', '<<&>>', var='<&>')
def test_noescape_setting(self):
t = SimpleTemplate('<{{var}}>', noescape=True)
self.assertRenders(t, '<b>', var='b')
self.assertRenders(t, '<<&>>', var='<&>')
t = SimpleTemplate('<{{!var}}>', noescape=True)
self.assertRenders(t, '<b>', var='b')
self.assertRenders(t, '<<&>>', var='<&>')
def test_blocks(self):
""" Templates: Code blocks and loops """
t = "start\n%for i in l:\n{{i}} \n%end\nend"
self.assertRenders(t, 'start\n1 \n2 \n3 \nend', l=[1,2,3])
self.assertRenders(t, 'start\nend', l=[])
t = "start\n%if i:\n{{i}} \n%end\nend"
self.assertRenders(t, 'start\nTrue \nend', i=True)
self.assertRenders(t, 'start\nend', i=False)
def test_elsebug(self):
''' Whirespace between block keyword and colon is allowed '''
self.assertRenders("%if 1:\nyes\n%else:\nno\n%end\n", "yes\n")
self.assertRenders("%if 1:\nyes\n%else :\nno\n%end\n", "yes\n")
def test_commentbug(self):
''' A "#" sign within an string is not a comment '''
self.assertRenders("%if '#':\nyes\n%end\n", "yes\n")
def test_multiline(self):
''' Block statements with non-terminating newlines '''
self.assertRenders("%if 1\\\n%and 1:\nyes\n%end\n", "yes\n")
def test_newline_in_parameterlist(self):
''' Block statements with non-terminating newlines in list '''
self.assertRenders("%a=[1,\n%2]\n{{len(a)}}", "2")
def test_dedentbug(self):
''' One-Line dednet blocks should not change indention '''
t = '%if x: a="if"\n%else: a="else"\n%end\n{{a}}'
self.assertRenders(t, "if", x=True)
self.assertRenders(t, "else", x=False)
t = '%if x:\n%a="if"\n%else: a="else"\n%end\n{{a}}'
self.assertRenders(t, "if", x=True)
self.assertRenders(t, "else", x=False)
t = SimpleTemplate('%if x: a="if"\n%else: a="else"\n%end')
self.assertRaises(NameError, t.render)
def test_onelinebugs(self):
''' One-Line blocks should not change indention '''
t = '%if x:\n%a=1\n%end\n{{a}}'
self.assertRenders(t, "1", x=True)
t = '%if x: a=1; end\n{{a}}'
self.assertRenders(t, "1", x=True)
t = '%if x:\n%a=1\n%else:\n%a=2\n%end\n{{a}}'
self.assertRenders(t, "1", x=True)
self.assertRenders(t, "2", x=False)
t = '%if x: a=1\n%else:\n%a=2\n%end\n{{a}}'
self.assertRenders(t, "1", x=True)
self.assertRenders(t, "2", x=False)
t = '%if x:\n%a=1\n%else: a=2; end\n{{a}}'
self.assertRenders(t, "1", x=True)
self.assertRenders(t, "2", x=False)
t = '%if x: a=1\n%else: a=2; end\n{{a}}'
self.assertRenders(t, "1", x=True)
self.assertRenders(t, "2", x=False)
def test_onelineblocks(self):
""" Templates: one line code blocks """
t = "start\n%a=''\n%for i in l: a += str(i); end\n{{a}}\nend"
self.assertRenders(t, 'start\n123\nend', l=[1,2,3])
self.assertRenders(t, 'start\n\nend', l=[])
def test_escaped_codelines(self):
self.assertRenders('\\% test', '% test')
self.assertRenders('\\%% test', '%% test')
self.assertRenders(' \\% test', ' % test')
def test_nobreak(self):
""" Templates: Nobreak statements"""
self.assertRenders("start\\\\\n%pass\nend", 'startend')
def test_nonobreak(self):
""" Templates: Escaped nobreak statements"""
self.assertRenders("start\\\\\n\\\\\n%pass\nend", 'start\\\\\nend')
def test_include(self):
""" Templates: Include statements"""
with chdir(__file__):
t = SimpleTemplate(name='stpl_include', lookup=['./views/'])
self.assertRenders(t, 'before\nstart var end\nafter\n', var='var')
def test_include_to_global(self):
""" Templates: Updated globals() with include() result"""
with chdir(__file__):
t = SimpleTemplate(name='stpl_include_to_global', lookup=['./views/'])
self.assertRenders(t, 'before\nstart var end\nstart 3 then var end\nafter\n', var='var')
def test_rebase(self):
""" Templates: %rebase and method passing """
with chdir(__file__):
t = SimpleTemplate(name='stpl_t2main', lookup=['./views/'])
result='+base+\n+main+\n!1234!\n+include+\n-main-\n+include+\n-base-\n'
self.assertRenders(t, result, content='1234')
def test_get(self):
self.assertRenders('{{get("x", "default")}}', '1234', x='1234')
self.assertRenders('{{get("x", "default")}}', 'default')
def test_setdefault(self):
t = '%setdefault("x", "default")\n{{x}}'
self.assertRenders(t, '1234', x='1234')
self.assertRenders(t, 'default')
def test_defnied(self):
self.assertRenders('{{x if defined("x") else "no"}}', 'yes', x='yes')
self.assertRenders('{{x if defined("x") else "no"}}', 'no')
def test_notfound(self):
""" Templates: Unavailable templates"""
self.assertRaises(TemplateError, SimpleTemplate, name="abcdef", lookup=['.'])
def test_error(self):
""" Templates: Exceptions"""
self.assertRaises(SyntaxError, lambda: SimpleTemplate('%for badsyntax').co)
self.assertRaises(IndexError, SimpleTemplate('{{i[5]}}', lookup=['.']).render, i=[0])
def test_winbreaks(self):
""" Templates: Test windows line breaks """
self.assertRenders('%var+=1\r\n{{var}}\r\n', '6\r\n', var=5)
def test_winbreaks_end_bug(self):
d = { 'test': [ 1, 2, 3 ] }
self.assertRenders('%for i in test:\n{{i}}\n%end\n', '1\n2\n3\n', **d)
self.assertRenders('%for i in test:\n{{i}}\r\n%end\n', '1\r\n2\r\n3\r\n', **d)
self.assertRenders('%for i in test:\r\n{{i}}\n%end\r\n', '1\n2\n3\n', **d)
self.assertRenders('%for i in test:\r\n{{i}}\r\n%end\r\n', '1\r\n2\r\n3\r\n', **d)
def test_commentonly(self):
""" Templates: Commentd should behave like code-lines (e.g. flush text-lines) """
t = SimpleTemplate('...\n%#test\n...')
self.assertNotEqual('#test', t.code.splitlines()[0])
def test_template_shortcut(self):
result = template('start {{var}} end', var='middle')
self.assertEqual(touni('start middle end'), result)
def test_view_decorator(self):
@view('start {{var}} end')
def test():
return dict(var='middle')
self.assertEqual(touni('start middle end'), test())
def test_view_decorator_issue_407(self):
with chdir(__file__):
@view('stpl_no_vars')
def test():
pass
self.assertEqual(touni('hihi'), test())
@view('aaa {{x}}', x='bbb')
def test2():
pass
self.assertEqual(touni('aaa bbb'), test2())
def test_global_config(self):
SimpleTemplate.global_config('meh', 1)
t = SimpleTemplate('anything')
self.assertEqual(touni('anything'), t.render())
def test_bug_no_whitespace_before_stmt(self):
self.assertRenders('\n{{var}}', '\nx', var='x')
def test_bug_block_keywords_eat_prefixed_code(self):
''' #595: Everything before an 'if' statement is removed, resulting in
SyntaxError. '''
tpl = "% m = 'x' if True else 'y'\n{{m}}"
self.assertRenders(tpl, 'x')
class TestSTPLDir(unittest.TestCase):
def fix_ident(self, string):
lines = string.splitlines(True)
if not lines: return string
if not lines[0].strip(): lines.pop(0)
whitespace = re.match('([ \t]*)', lines[0]).group(0)
if not whitespace: return string
for i in range(len(lines)):
lines[i] = lines[i][len(whitespace):]
return lines[0][:0].join(lines)
def assertRenders(self, source, result, syntax=None, *args, **vars):
source = self.fix_ident(source)
result = self.fix_ident(result)
tpl = SimpleTemplate(source, syntax=syntax)
try:
tpl.co
self.assertEqual(touni(result), tpl.render(*args, **vars))
except SyntaxError:
self.fail('Syntax error in template:\n%s\n\nTemplate code:\n##########\n%s\n##########' %
(traceback.format_exc(), tpl.code))
def test_multiline_block(self):
source = '''
<% a = 5
b = 6
c = 7 %>
{{a+b+c}}
'''; result = '''
18
'''
self.assertRenders(source, result)
source_wineol = '<% a = 5\r\nb = 6\r\nc = 7\r\n%>\r\n{{a+b+c}}'
result_wineol = '18'
self.assertRenders(source_wineol, result_wineol)
def test_multiline_ignore_eob_in_string(self):
source = '''
<% x=5 # a comment
y = '%>' # a string
# this is still code
# lets end this %>
{{x}}{{!y}}
'''; result = '''
5%>
'''
self.assertRenders(source, result)
def test_multiline_find_eob_in_comments(self):
source = '''
<% # a comment
# %> ignore because not end of line
# this is still code
x=5
# lets end this here %>
{{x}}
'''; result = '''
5
'''
self.assertRenders(source, result)
def test_multiline_indention(self):
source = '''
<% if True:
a = 2
else:
a = 0
end
%>
{{a}}
'''; result = '''
2
'''
self.assertRenders(source, result)
def test_multiline_eob_after_end(self):
source = '''
<% if True:
a = 2
end %>
{{a}}
'''; result = '''
2
'''
self.assertRenders(source, result)
def test_multiline_eob_in_single_line_code(self):
# eob must be a valid python expression to allow this test.
source = '''
cline eob=5; eob
xxx
'''; result = '''
xxx
'''
self.assertRenders(source, result, syntax='sob eob cline foo bar')
def test_multiline_strings_in_code_line(self):
source = '''
% a = """line 1
line 2"""
{{a}}
'''; result = '''
line 1
line 2
'''
self.assertRenders(source, result)
def test_multiline_comprehensions_in_code_line(self):
self.assertRenders(source='''
% a = [
% (i + 1)
% for i in range(5)
% if i%2 == 0
% ]
{{a}}
''', result='''
[1, 3, 5]
''')
def test_end_keyword_on_same_line(self):
self.assertRenders('''
% if 1:
% 1; end
foo
''', '''
foo
''')
import pytest
from stpl import main
import os
from subprocess import Popen, PIPE
import sys
from mock import patch
if 'win' in sys.platform:
os.environ['PATH']+=";"+os.path.dirname(__file__)
else:
os.environ['PATH']+=":"+os.path.dirname(__file__)
@pytest.yield_fixture
def tmpworkdir(tmpdir):
cwd = os.getcwd()
os.chdir(tmpdir.strpath)
yield tmpdir
os.chdir(cwd)
def test_string(request,capfd):
main(file_or_string="{{i+j}}",directory=None,code=["i=2;j=3"])
out, err = capfd.readouterr()
assert out == '5'
@pytest.yield_fixture
def stplfile(tmpworkdir):
with open('tst.c.stpl','w',encoding='utf-8') as f:
f.write('''\
{{i+j}}'''
)
yield 'tst.c.stpl'
def test_file(stplfile,capfd):
main(file_or_string=stplfile,directory=None,code=["i=2;j=3"])
out, err = capfd.readouterr()
assert out == '5'
def test_dir(stplfile):
main(file_or_string=stplfile,directory='tst.c',code=["i=2;j=3"])
assert os.path.isfile('tst.c')
assert open('tst.c').read()=='5'
@pytest.yield_fixture
def nostplfile(tmpworkdir):
with open('tst.c','w',encoding='utf-8') as f:
f.write('''\
{{i+j}}'''
)
yield 'tst.c'
#like inplace
def test_nostpl(nostplfile):
main(file_or_string=nostplfile,directory='.',code=["i=2;j=3"])
assert os.path.isfile('tst.c')
assert open('tst.c').read()=='5'
def test_parse_args(stplfile):
testargs = ['stpl.py',stplfile,'.','i=2;j=3']
with patch.object(sys, 'argv', testargs):
main()
assert os.path.isfile('tst.c')
assert open('tst.c').read()=='5'
def test_raise(request):
with pytest.raises(ValueError):
main(file_or_string="{{i+j}}",directory='.',code=["i=2;j=3"])
def test_stdin(request):
p=Popen(['stpl','-','-','i=2;j=3'],stdin=PIPE,stdout=PIPE)
p.stdin.write(b'{{i+j}}\n')
p.stdin.close()
p.wait()
res=p.stdout.read()
assert res==b'5\n'