-
Notifications
You must be signed in to change notification settings - Fork 14
/
Parser_test.py
599 lines (524 loc) · 16.5 KB
/
Parser_test.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
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
# -*- coding: UTF-8 -*-
from __future__ import absolute_import
from __future__ import unicode_literals
import pytest
from Cheetah.compile import compile_to_class
from Cheetah.legacy_parser import ArgList
from Cheetah.legacy_parser import ParseError
from Cheetah.legacy_parser import UnknownDirectiveError
from testing.util import assert_raises_exactly
def test_ArgList_merge1():
"""Testing the ArgList case results from
Template.Preprocessors.test_complexUsage
"""
arglist = ArgList()
arglist.add_argument('arg')
assert arglist.merge() == [('arg', None)]
def test_ArgList_merge2():
"""Testing the ArgList case results from
SyntaxAndOutput.BlockDirective.test4
"""
arglist = ArgList()
arglist.add_argument('a')
arglist.add_default('999')
arglist.next()
arglist.add_argument('b')
arglist.add_default('444')
assert arglist.merge() == [('a', '999'), ('b', '444')]
def test_merge3():
"""Testing the ArgList case results
from SyntaxAndOutput.BlockDirective.test13
"""
arglist = ArgList()
arglist.add_argument('arg')
arglist.add_default("'This is my block'")
assert arglist.merge() == [('arg', "'This is my block'")]
def test_unknown_directive_name():
with assert_raises_exactly(
UnknownDirectiveError,
'\n\n'
'Bad directive name: "foo". You may want to escape that # sign?\n'
'Line 1, column 2\n'
'\n'
'Line|Cheetah Code\n'
'----|-------------------------------------------------------------\n'
'1 |#foo\n'
' ^\n',
):
compile_to_class('#foo\n')
def test_malformed_triple_quotes():
with assert_raises_exactly(
ParseError,
'\n\n'
'Malformed triple-quoted string\n'
'Line 1, column 3\n'
'\n'
'Line|Cheetah Code\n'
'----|-------------------------------------------------------------\n'
'1 |${"""}\n'
' ^\n',
):
compile_to_class('${"""}')
def test_unclosed_directives():
with assert_raises_exactly(
ParseError,
'\n\n'
'Some #directives are missing their corresponding #end ___ tag: if\n'
'Line 1, column 9\n'
'\n'
'Line|Cheetah Code\n'
'----|-------------------------------------------------------------\n'
'1 |#if True\n'
' ^\n'
):
compile_to_class('#if True\n')
def test_invalid_identifier():
with assert_raises_exactly(
ParseError,
'\n\n'
'Invalid identifier\n'
'Line 1, column 5\n'
'\n'
'Line|Cheetah Code\n'
'----|-------------------------------------------------------------\n'
'1 |#def\n'
' ^\n'
):
compile_to_class('#def\n')
def test_end_but_nothing_to_end():
with assert_raises_exactly(
ParseError,
'\n\n'
'#end found, but nothing to end\n'
'Line 1, column 8\n'
'\n'
'Line|Cheetah Code\n'
'----|-------------------------------------------------------------\n'
'1 |#end if\n'
' ^\n'
):
compile_to_class('#end if\n')
def test_invalid_end_directive():
with assert_raises_exactly(
ParseError,
'\n\n'
'Invalid end directive\n'
'Line 1, column 5\n'
'\n'
'Line|Cheetah Code\n'
'----|-------------------------------------------------------------\n'
'1 |#end\n'
' ^\n'
):
compile_to_class('#end\n')
def test_invalid_nesting_directives():
# TODO: this one is off by a bit on the exception message
with assert_raises_exactly(
ParseError,
'\n\n'
'#end if found, expected #end for\n'
'Line 4, column 1\n'
'\n'
'Line|Cheetah Code\n'
'----|-------------------------------------------------------------\n'
'1 |#if True\n'
'2 |#for i in range(5)\n'
'3 |#end if\n'
'4 |#end for\n'
' ^\n'
):
compile_to_class(
'#if True\n'
'#for i in range(5)\n'
'#end if\n'
'#end for\n'
)
def test_parse_error_for_implements_argspec():
with assert_raises_exactly(
ParseError,
'\n\n'
'yelp_cheetah does not support argspecs for #implements\n'
'Line 1, column 16\n'
'\n'
'Line|Cheetah Code\n'
'----|-------------------------------------------------------------\n'
'1 |#implements foo(bar)\n'
' ^\n'
):
compile_to_class('#implements foo(bar)')
def test_parse_error_for_multiple_inheritance():
with assert_raises_exactly(
ParseError,
'\n\n'
'yelp_cheetah does not support multiple inheritance\n'
'Line 1, column 33\n'
'\n'
'Line|Cheetah Code\n'
'----|-------------------------------------------------------------\n'
'1 |#extends Cheetah.Template, object\n'
' ^\n'
):
compile_to_class('#extends Cheetah.Template, object')
def test_parse_error_long_file():
with assert_raises_exactly(
ParseError,
'\n\n'
"EOF was reached before a matching ')' was found for the '('\n"
'Line 5, column 5\n'
'\n'
'Line|Cheetah Code\n'
'----|-------------------------------------------------------------\n'
'2 |2\n'
'3 |3\n'
'4 |4\n'
'5 |$foo(\n'
' ^\n'
'6 |6\n'
'7 |7\n'
'8 |8\n'
):
compile_to_class('1\n2\n3\n4\n$foo(\n6\n7\n8\n')
def test_unclosed_enclosure():
with assert_raises_exactly(
ParseError,
'\n\n'
"EOF was reached before a matching '}' was found for the '{'\n"
'Line 1, column 2\n'
'\n'
'Line|Cheetah Code\n'
'----|-------------------------------------------------------------\n'
'1 |${hai +\n'
' ^\n'
):
compile_to_class('${hai +')
def test_parse_error_on_attr_with_var():
with assert_raises_exactly(
ParseError,
'\n\n'
'Invalid #attr directive. It should contain simple Python literals.\n'
'Line 1, column 13\n'
'\n'
'Line|Cheetah Code\n'
'----|-------------------------------------------------------------\n'
'1 |#attr foo = $bar\n'
' ^\n'
):
compile_to_class('#attr foo = $bar\n')
def test_parse_error_on_attr_with_dollar_sign():
with assert_raises_exactly(
ParseError,
'\n\n'
'#attr directive must not contain `$`\n'
'Line 1, column 7\n'
'\n'
'Line|Cheetah Code\n'
'----|-------------------------------------------------------------\n'
'1 |#attr $foo = "hai"\n'
' ^\n'
):
compile_to_class('#attr $foo = "hai"\n')
def test_invalid_line_continuation():
with assert_raises_exactly(
ParseError,
'\n\n'
'Line ending expected\n'
'Line 1, column 20\n'
'\n'
'Line|Cheetah Code\n'
'----|-------------------------------------------------------------\n'
'1 |#py foo = "bar" + \\hi, not a new line\n'
' ^\n'
):
compile_to_class('#py foo = "bar" + \\hi, not a new line')
def test_close_wrong_enclosure():
with assert_raises_exactly(
ParseError,
'\n\n'
"A ']' was found at line 1, col 4 before a matching '}' was found for the '{'\n"
'Line 1, column 2\n'
'\n'
'Line|Cheetah Code\n'
'----|-------------------------------------------------------------\n'
'1 |${a]\n'
' ^\n'
):
compile_to_class('${a]')
def test_reach_eof():
with assert_raises_exactly(
ParseError,
'\n\n'
"EOF was reached before a matching ')' was found for the '('\n"
'Line 1, column 7\n'
'\n'
'Line|Cheetah Code\n'
'----|-------------------------------------------------------------\n'
'1 |#super(\n'
' ^\n'
):
compile_to_class('#super(')
def test_non_ending_compiler_settings():
with assert_raises_exactly(
ParseError,
'\n\n'
'Some #directives are missing their corresponding #end ___ tag: compiler-settings\n'
'Line 2, column 26\n'
'\n'
'Line|Cheetah Code\n'
'----|-------------------------------------------------------------\n'
'1 |#compiler-settings\n'
'2 |useLegacyImportMode = True\n'
' ^\n'
):
compile_to_class('#compiler-settings\nuseLegacyImportMode = True')
def test_weird_close_call():
with assert_raises_exactly(
ParseError,
'\n\n'
"Expected a ')' before an end '}'\n"
'Line 1, column 6\n'
'\n'
'Line|Cheetah Code\n'
'----|-------------------------------------------------------------\n'
'1 |$foo(}\n'
' ^\n'
):
compile_to_class('$foo(}')
def test_invalid_syntax_in_super():
# I'm not sure this error is actually correct
with assert_raises_exactly(
ParseError,
'\n\n'
'$ is not allowed here.\n'
'Line 1, column 12\n'
'\n'
'Line|Cheetah Code\n'
'----|-------------------------------------------------------------\n'
'1 |#super(foo=${bar})\n'
' ^\n'
):
compile_to_class('#super(foo=${bar})')
def test_invalid_syntax_in_call():
# I'm not sure this error is actually correct
with assert_raises_exactly(
ParseError,
'\n\n'
"Long-form placeholders - ${}, $(), $[], etc. are not valid inside "
"expressions. Use them in top-level $placeholders only.\n"
'Line 1, column 11\n'
'\n'
'Line|Cheetah Code\n'
'----|-------------------------------------------------------------\n'
'1 |$herp(foo=${bar})\n'
' ^\n'
):
compile_to_class('$herp(foo=${bar})')
def test_expected_identifier_after_star():
with assert_raises_exactly(
ParseError,
'\n\n'
'Expected an identifier.\n'
'Line 1, column 9\n'
'\n'
'Line|Cheetah Code\n'
'----|-------------------------------------------------------------\n'
'1 |#super(*)\n'
' ^\n'
):
compile_to_class('#super(*)')
def test_unexpected_character_parse_error():
with assert_raises_exactly(
ParseError,
'\n\n'
'Unexpected character.\n'
'Line 1, column 8\n'
'\n'
'Line|Cheetah Code\n'
'----|-------------------------------------------------------------\n'
'1 |#super(☃)\n'
' ^\n'
):
compile_to_class('#super(☃)')
def test_def_with_dollar_sign_invalid():
with assert_raises_exactly(
ParseError,
'\n\n'
'use #def func() instead of #def $func()\n'
'Line 1, column 6\n'
'\n'
'Line|Cheetah Code\n'
'----|-------------------------------------------------------------\n'
'1 |#def $foo()\n'
' ^\n'
'2 |#end def\n'
):
compile_to_class(
'#def $foo()\n'
'#end def\n'
)
def test_def_without_arglist_invalid():
with assert_raises_exactly(
ParseError,
'\n\n'
'#def must contain an argspec (at least ())\n'
'Line 1, column 9\n'
'\n'
'Line|Cheetah Code\n'
'----|-------------------------------------------------------------\n'
'1 |#def foo\n'
' ^\n'
'2 |#end def\n'
):
compile_to_class(
'#def foo\n'
'#end def\n'
)
def test_block_with_an_argspec_invalid():
with assert_raises_exactly(
ParseError,
'\n\n'
'#block must not have an argspec, did you mean #def?\n'
'Line 1, column 11\n'
'\n'
'Line|Cheetah Code\n'
'----|-------------------------------------------------------------\n'
'1 |#block foo(bar)\n'
' ^\n'
'2 |#end block\n'
):
compile_to_class(
'#block foo(bar)\n'
'#end block\n'
)
def test_self_in_arglist_invalid():
with assert_raises_exactly(
ParseError,
'\n\n'
'Do not specify `self` in an arglist, it is assumed\n'
'Line 1, column 10\n'
'\n'
'Line|Cheetah Code\n'
'----|-------------------------------------------------------------\n'
'1 |#def foo(self, bar)\n'
' ^\n'
'2 |#end def\n'
):
compile_to_class(
'#def foo(self, bar)\n'
'#end def\n'
)
def test_set_with_dollar_signs_raises():
with assert_raises_exactly(
ParseError,
'\n\n'
"SyntaxError: can't assign to function call (<unknown>, line 1)\n\n"
'Line 1, column 13\n'
'\n'
'Line|Cheetah Code\n'
'----|-------------------------------------------------------------\n'
'1 |#py $foo = 1\n'
' ^\n'
):
compile_to_class('#py $foo = 1\n')
@pytest.mark.parametrize('decorator', ('@classmethod', '@staticmethod'))
def test_classmethod_staticmethod_not_allowed(decorator):
with assert_raises_exactly(
ParseError,
'\n\n'
'@classmethod / @staticmethod are not supported\n'
'Line 1, column 2\n'
'\n'
'Line|Cheetah Code\n'
'----|-------------------------------------------------------------\n'
'1 |#{}\n'
' ^\n'
'2 |#def foo(bar)\n'
'3 | #return bar + 1\n'
'4 |#end def\n'.format(decorator)
):
compile_to_class(
'#{}\n'
'#def foo(bar)\n'
' #return bar + 1\n'
'#end def\n'.format(decorator)
)
def test_lvalue_for():
with assert_raises_exactly(
ParseError,
'\n\n'
'lvalue of for must not contain a `$`\n'
'Line 1, column 6\n'
'\n'
'Line|Cheetah Code\n'
'----|-------------------------------------------------------------\n'
'1 |#for $foo in bar\n'
' ^\n'
'2 |$foo\n'
'3 |#end for\n'
):
compile_to_class(
'#for $foo in bar\n'
'$foo\n'
'#end for\n',
)
def test_uncaught_syntax_error():
with assert_raises_exactly(
ParseError,
'\n\n'
"SyntaxError: can't assign to function call (<unknown>, line 1)\n\n"
'Line 3, column 15\n\n'
'Line|Cheetah Code\n'
'----|-------------------------------------------------------------\n'
'1 |Hello\n'
'2 |World\n'
'3 |#py x = $y = 1\n'
' ^\n'
):
compile_to_class('Hello\nWorld\n#py x = $y = 1\n')
def test_errors_on_invalid_setting():
with assert_raises_exactly(
ParseError,
'\n\n'
"UnexpectedSettingName: not_a_real_setting\n\n"
'Line 3, column 23\n\n'
'Line|Cheetah Code\n'
'----|-------------------------------------------------------------\n'
'1 |#compiler-settings\n'
'2 |not_a_real_setting = True\n'
'3 |#end compiler-settings\n'
' ^\n'
):
compile_to_class(
'#compiler-settings\n'
'not_a_real_setting = True\n'
'#end compiler-settings\n'
)
def test_errors_on_blinged_kwarg():
with assert_raises_exactly(
ParseError,
'\n\n'
'kwargs should not start with $\n'
'Line 1, column 6\n\n'
'Line|Cheetah Code\n'
'----|-------------------------------------------------------------\n'
'1 |$foo($bar=$baz)\n'
' ^\n'
):
compile_to_class(
'$foo($bar=$baz)'
)
def test_errors_garbage_after_end_directive():
with assert_raises_exactly(
ParseError,
'\n\n'
'Invalid garbage after #end directive\n'
'Line 2, column 15\n\n'
'Line|Cheetah Code\n'
'----|-------------------------------------------------------------\n'
'1 |#block foo\n'
'2 |#end block foo\n'
' ^\n'
):
compile_to_class(
'#block foo\n'
'#end block foo\n'
)