Debian Codegraph @

contrib/fypp-3.2-b8dd58b-20230822/test/test_fypp.py

raw
1 '''Unit tests for testing Fypp.'''
2 from pathlib import Path
3 import platform
4 import unittest
5 import fypp
6
7
8 def _linenum(linenr, fname=None, flag=None):
9 if fname is None:
10 fname = fypp.STRING
11 return fypp.linenumdir_cpp(linenr, fname, flag)
12
13 def _defvar(var, val):
14 return '-D{0}={1}'.format(var, val)
15
16 def _incdir(path):
17 return '-I{0}'.format(path)
18
19 def _linelen(linelen):
20 return '-l{0}'.format(linelen)
21
22 def _indentation(ind):
23 return '--indentation={0}'.format(ind)
24
25 def _folding(fold):
26 return '-f{0}'.format(fold)
27
28 def _moddir(path):
29 return '-M{0}'.format(path)
30
31 def _linenumbering(nummode):
32 return '-N{0}'.format(nummode)
33
34 def _linenum_gfortran5():
35 return '--line-marker-format=gfortran5'
36
37 def _linenum_std():
38 return '--line-marker-format=std'
39
40 def _importmodule(module):
41 return '-m{0}'.format(module)
42
43
44 _LINENUM_FLAG = '-n'
45
46 _FIXED_FORMAT_FLAG = '--fixed-format'
47
48 _NO_FOLDING_FLAG = '-F'
49
50 _NEW_FILE = 1
51
52 _RETURN_TO_FILE = 2
53
54
55 # Various basic tests
56 #
57 # Each test consists of a tuple containing the test name and a tuple with the
58 # arguments of the get_test_output_method() routine.
59 #
60 SIMPLE_TESTS = [
61 ('if_true',
62 ([_defvar('TESTVAR', 1)],
63 '#:if TESTVAR > 0\nTrue\n#:endif\n',
64 'True\n'
65 )
66 ),
67 ('if_false',
68 ([_defvar('TESTVAR', 0)],
69 '#:if TESTVAR > 0\nTrue\n#:endif\n',
70 ''
71 )
72 ),
73 ('if_else_true',
74 ([_defvar('TESTVAR', 1)],
75 '#:if TESTVAR > 0\nTrue\n#:else\nFalse\n#:endif\n',
76 'True\n'
77 )
78 ),
79 ('if_else_false',
80 ([_defvar('TESTVAR', 0)],
81 '#:if TESTVAR > 0\nTrue\n#:else\nFalse\n#:endif\n',
82 'False\n'
83 )
84 ),
85 ('if_elif_true1',
86 ([_defvar('TESTVAR', 1)],
87 '#:if TESTVAR == 1\nTrue1\n#:elif TESTVAR == 2\nTrue2\n#:endif\n',
88 'True1\n'
89 )
90 ),
91 ('if_elif_true2',
92 ([_defvar('TESTVAR', 2)],
93 '#:if TESTVAR == 1\nTrue1\n#:elif TESTVAR == 2\nTrue2\n#:endif\n',
94 'True2\n'
95 )
96 ),
97 ('if_elif_false',
98 ([_defvar('TESTVAR', 0)],
99 '#:if TESTVAR == 1\nTrue1\n#:elif TESTVAR == 2\nTrue2\n#:endif\n',
100 ''
101 )
102 ),
103 ('if_elif_else_true1',
104 ([_defvar('TESTVAR', 1)],
105 '#:if TESTVAR == 1\nTrue1\n#:elif TESTVAR == 2\nTrue2\n'
106 '#:else\nFalse\n#:endif\n',
107 'True1\n'
108 )
109 ),
110 ('if_elif_else_true2',
111 ([_defvar('TESTVAR', 2)],
112 '#:if TESTVAR == 1\nTrue1\n#:elif TESTVAR == 2\nTrue2\n'
113 '#:else\nFalse\n#:endif\n',
114 'True2\n'
115 )
116 ),
117 ('if_elif_else_false',
118 ([_defvar('TESTVAR', 0)],
119 '#:if TESTVAR == 1\nTrue1\n#:elif TESTVAR == 2\nTrue2\n'
120 '#:else\nFalse\n#:endif\n',
121 'False\n'
122 )
123 ),
124 ('inline_if_true',
125 ([_defvar('TESTVAR', 1)],
126 '#{if TESTVAR > 0}#True#{endif}#Done',
127 'TrueDone'
128 )
129 ),
130 ('inline_if_false',
131 ([_defvar('TESTVAR', 0)],
132 '#{if TESTVAR > 0}#True#{endif}#Done',
133 'Done'
134 )
135 ),
136 ('inline_if_else_true',
137 ([_defvar('TESTVAR', 1)],
138 '#{if TESTVAR > 0}#True#{else}#False#{endif}#Done',
139 'TrueDone'
140 )
141 ),
142 ('inline_if_else_false',
143 ([_defvar('TESTVAR', 0)],
144 '#{if TESTVAR > 0}#True#{else}#False#{endif}#Done',
145 'FalseDone'
146 )
147 ),
148 ('inline_if_elif_true1',
149 ([_defvar('TESTVAR', 1)],
150 '#{if TESTVAR == 1}#True1#{elif TESTVAR == 2}#True2#{endif}#Done',
151 'True1Done'
152 )
153 ),
154 ('inline_if_elif_true2',
155 ([_defvar('TESTVAR', 2)],
156 '#{if TESTVAR == 1}#True1#{elif TESTVAR == 2}#True2#{endif}#Done',
157 'True2Done'
158 )
159 ),
160 ('inline_if_elif_false',
161 ([_defvar('TESTVAR', 0)],
162 '#{if TESTVAR == 1}#True1#{elif TESTVAR == 2}#True2#{endif}#Done',
163 'Done'
164 )
165 ),
166 ('inline_if_elif_else_true1',
167 ([_defvar('TESTVAR', 1)],
168 '#{if TESTVAR == 1}#True1#{elif TESTVAR == 2}#True2#{else}#False#{endif}#'
169 'Done',
170 'True1Done'
171 )
172 ),
173 ('inline_if_elif_else_true2',
174 ([_defvar('TESTVAR', 2)],
175 '#{if TESTVAR == 1}#True1#{elif TESTVAR == 2}#True2#{else}#False#{endif}#'
176 'Done',
177 'True2Done'
178 )
179 ),
180 ('inline_if_elif_else_false',
181 ([_defvar('TESTVAR', 0)],
182 '#{if TESTVAR == 1}#True1#{elif TESTVAR == 2}#True2#{else}#False#{endif}#'
183 'Done',
184 'FalseDone'
185 )
186 ),
187 ('linesub_eol',
188 ([_defvar('TESTVAR', 1)],
189 'A\n$: TESTVAR + 1\nB\n',
190 'A\n2\nB\n'
191 )
192 ),
193 ('linesub_contlines',
194 ([_defvar('TESTVAR', 1)],
195 '$: TESTVAR & \n & + 1\n',
196 '2\n'
197 )
198 ),
199 ('linesub_contlines2',
200 ([_defvar('TESTVAR', 1)],
201 '$: TEST& \n &VAR & \n & + 1\n',
202 '2\n'
203 )
204 ),
205 ('linesub_contlines_contchar1',
206 ([],
207 '$: \'hello&\n world\'\n',
208 'hello world\n'
209 )
210 ),
211 ('linesub_contlines2_contchar1',
212 ([],
213 '$: \'hello&\n world&\n !\'\n',
214 'hello world !\n'
215 )
216 ),
217 ('exprsub',
218 ([_defvar('TESTVAR', 1)],
219 'A${TESTVAR}$B${TESTVAR + 1}$C',
220 'A1B2C'
221 )
222 ),
223 ('exprsub_ignored_contlines',
224 ([_defvar('TESTVAR', 1)],
225 'A${TEST&\n &VAR}$B${TESTVAR + 1}$C',
226 'A${TEST&\n &VAR}$B2C'
227 )
228 ),
229 ('macrosubs',
230 ([],
231 '#:def macro(var)\nMACRO|${var}$|\n#:enddef\n${macro(1)}$',
232 'MACRO|1|'
233 )
234 ),
235 ('macrosubs_named_enddef',
236 ([],
237 '#:def macro(var)\nMACRO|${var}$|\n#:enddef macro\n${macro(1)}$',
238 'MACRO|1|'
239 )
240 ),
241 ('macrodef_whitespace',
242 ([],
243 '#:def macro (var)\nMACRO|${var}$|\n#:enddef macro\n${macro(1)}$',
244 'MACRO|1|'
245 )
246 ),
247 ('macro_noargs',
248 ([],
249 '#:def macro()\nMACRO\n#:enddef\n${macro()}$',
250 'MACRO'
251 )
252 ),
253 ('recursive_macrosubs',
254 ([],
255 '#:def macro(var)\nMACRO|${var}$|\n#:enddef\n${macro(macro(1))}$',
256 'MACRO|MACRO|1||'
257 )
258 ),
259 ('macrosubs_extvarsubs',
260 ([_defvar('TESTVAR', 1)],
261 '#:def macro(var)\nMACRO|${var}$-${TESTVAR}$|\n#:enddef\n${macro(2)}$',
262 'MACRO|2-1|'
263 )
264 ),
265 ('macro_trailing_newlines',
266 ([],
267 '#:def macro()\nL1\n\n#:enddef\n$: macro()\n',
268 'L1\n\n',
269 )
270 ),
271 ('macro_trailing_newlines_inline',
272 ([],
273 '#:def macro()\nL1\n\n#:enddef\n|${macro()}$|',
274 '|L1\n|',
275 )
276 ),
277 ('macro_call_named_arguments',
278 ([],
279 '#:def mymacro(A, B)\nA=${A}$,B=${B}$\n#:enddef mymacro\n'\
280 '$:mymacro(B=1, A=2)\n',
281 'A=2,B=1\n'
282 )
283 ),
284 ('macro_call_positional_and_named_arguments',
285 ([],
286 '#:def mymacro(A, B, C)\nA=${A}$,B=${B}$,C=${C}$\n#:enddef mymacro\n'\
287 '$:mymacro(1, C=3, B=2)\n',
288 'A=1,B=2,C=3\n'
289 )
290 ),
291 ('optarg_macro_call_all_args',
292 ([],
293 '#:def mymacro(A, B=2)\nA=${A}$,B=${B}$\n#:enddef mymacro\n'\
294 '#:call mymacro\n1\n#:nextarg\n2\n#:endcall\n',
295 'A=1,B=2\n'
296 )
297 ),
298 ('optarg_macro_block_all_args',
299 ([],
300 '#:def mymacro(A, B=2)\nA=${A}$,B=${B}$\n#:enddef mymacro\n'\
301 '#:block mymacro\n1\n#:contains\n2\n#:endblock\n',
302 'A=1,B=2\n'
303 )
304 ),
305 ('optarg_macro_call_missing_args',
306 ([],
307 '#:def mymacro(A, B=2)\nA=${A}$,B=${B}$\n#:enddef mymacro\n'\
308 '#:call mymacro\n1\n#:endcall\n',
309 'A=1,B=2\n'
310 )
311 ),
312 ('optarg_macro_block_missing_args',
313 ([],
314 '#:def mymacro(A, B=2)\nA=${A}$,B=${B}$\n#:enddef mymacro\n'\
315 '#:block mymacro\n1\n#:endblock\n',
316 'A=1,B=2\n'
317 )
318 ),
319 ('optarg_macro_eval_call_all_args',
320 ([],
321 '#:def mymacro(A, B=2)\nA=${A}$,B=${B}$\n#:enddef mymacro\n'\
322 '$:mymacro(1, 2)\n',
323 'A=1,B=2\n'
324 )
325 ),
326 ('optarg_macro_eval_call_missing_args',
327 ([],
328 '#:def mymacro(A, B=2)\nA=${A}$,B=${B}$\n#:enddef mymacro\n'\
329 '$:mymacro(1)\n',
330 'A=1,B=2\n'
331 )
332 ),
333 ('optarg_macro_direct_call_all_args',
334 ([],
335 '#:def mymacro(A, B=2)\nA=${A}$,B=${B}$\n#:enddef mymacro\n'\
336 '@:mymacro(1, 2)\n',
337 'A=1,B=2\n'
338 )
339 ),
340 ('optarg_macro_direct_call_all_args_inline',
341 ([],
342 '#:def mymacro(A, B=2)\nA=${A}$,B=${B}$\n#:enddef mymacro\n'\
343 '@{mymacro(1, 2)}@',
344 'A=1,B=2'
345 )
346 ),
347 ('optarg_macro_direct_call_missing_args',
348 ([],
349 '#:def mymacro(A, B=2)\nA=${A}$,B=${B}$\n#:enddef mymacro\n'\
350 '@:mymacro(1)\n',
351 'A=1,B=2\n'
352 )
353 ),
354 ('optarg_macro_direct_call_missing_args_inline',
355 ([],
356 '#:def mymacro(A, B=2)\nA=${A}$,B=${B}$\n#:enddef mymacro\n'\
357 '|@{mymacro(1)}@|',
358 '|A=1,B=2|'
359 )
360 ),
361 ('optarg_macro_tuple_as_default',
362 ([],
363 '#:def macro(X, Y=2, Z=(1,2==3))\nX=${X}$,Y=${Y}$,Z=${Z[0]}$,${Z[1]}$\n'\
364 '#:enddef\n@:macro(1)\n',
365 'X=1,Y=2,Z=1,False\n'
366 )
367 ),
368 ('macro_vararg_no_varargs',
369 ([],
370 '#:def macro(x, y, *vararg)\n|${x}$${y}$${vararg}$|\n#:enddef\n'\
371 '$:macro(1, 2)\n',
372 '|12[]|\n'
373 )
374 ),
375 ('macro_vararg_one_vararg',
376 ([],
377 '#:def macro(x, y, *vararg)\n|${x}$${y}$${vararg}$|\n#:enddef\n'\
378 '$:macro(1, 2, 3)\n',
379 '|12[3]|\n'
380 )
381 ),
382 ('macro_vararg_two_varargs',
383 ([],
384 '#:def macro(x, y, *vararg)\n|${x}$${y}$${vararg}$|\n#:enddef\n'\
385 '$:macro(1, 2, 3, 4)\n',
386 '|12[3, 4]|\n'
387 )
388 ),
389 ('macro_vararg_named_arguments_eval',
390 ([],
391 '#:def macro(x, y, *vararg)\n|${x}$${y}$${vararg}$|\n#:enddef\n'\
392 '$:macro(y=2, x=1)\n',
393 '|12[]|\n'
394 )
395 ),
396 ('macro_vararg_named_arguments_directcall',
397 ([],
398 '#:def macro(x, y, *vararg)\n|${x}$${y}$${vararg}$|\n#:enddef\n'\
399 '@:macro(y=2, x=1)\n',
400 '|12[]|\n'
401 )
402 ),
403 ('macro_vararg_named_arguments_call',
404 ([],
405 '#:def macro(x, y, *vararg)\n|${x}$${y}$${vararg}$|\n#:enddef\n'\
406 '#:call macro\n#:nextarg y\n2\n#:nextarg x\n1\n#:endcall\n',
407 '|12[]|\n'
408 )
409 ),
410 ('macro_vararg_named_arguments_block',
411 ([],
412 '#:def macro(x, y, *vararg)\n|${x}$${y}$${vararg}$|\n#:enddef\n'\
413 '#:block macro\n#:contains y\n2\n#:contains x\n1\n#:endblock\n',
414 '|12[]|\n'
415 )
416 ),
417 ('macro_vararg_named_arguments_inline_call',
418 ([],
419 '#:def macro(x, y, *vararg)\n|${x}$${y}$${vararg}$|\n#:enddef\n'\
420 '#{call macro}##{nextarg y}#2#{nextarg x}#1#{endcall}#',
421 '|12[]|'
422 )
423 ),
424 ('macro_vararg_named_arguments_inline_block',
425 ([],
426 '#:def macro(x, y, *vararg)\n|${x}$${y}$${vararg}$|\n#:enddef\n'\
427 '#{block macro}##{contains y}#2#{contains x}#1#{endblock}#',
428 '|12[]|'
429 )
430 ),
431 ('macro_vararg_mixed_arguments_eval',
432 ([],
433 '#:def macro(x, y, z, *vararg)\n|${x}$${y}$${z}$${vararg}$|\n#:enddef\n'\
434 '$:macro(1, z=3, y=2)\n',
435 '|123[]|\n'
436 )
437 ),
438 ('macro_vararg_mixed_arguments_directcall',
439 ([],
440 '#:def macro(x, y, z, *vararg)\n|${x}$${y}$${z}$${vararg}$|\n#:enddef\n'\
441 '@:macro(1, z=3, y=2)\n',
442 '|123[]|\n'
443 )
444 ),
445 ('macro_vararg_mixed_arguments_call',
446 ([],
447 '#:def macro(x, y, z, *vararg)\n|${x}$${y}$${z}$${vararg}$|\n#:enddef\n'\
448 '#:call macro\n1\n#:nextarg z\n3\n#:nextarg y\n2\n#:endcall\n',
449 '|123[]|\n'
450 )
451 ),
452 ('macro_vararg_mixed_arguments_block',
453 ([],
454 '#:def macro(x, y, z, *vararg)\n|${x}$${y}$${z}$${vararg}$|\n#:enddef\n'\
455 '#:block macro\n1\n#:contains z\n3\n#:contains y\n2\n#:endblock\n',
456 '|123[]|\n'
457 )
458 ),
459 ('macro_vararg_mixed_arguments_call2',
460 ([],
461 '#:def macro(x, y, z, *vararg)\n|${x}$${y}$${z}$${vararg}$|\n#:enddef\n'\
462 '#:call macro\n#:nextarg\n1\n#:nextarg z\n3\n#:nextarg y\n2\n#:endcall\n',
463 '|123[]|\n'
464 )
465 ),
466 ('macro_vararg_mixed_arguments_block2',
467 ([],
468 '#:def macro(x, y, z, *vararg)\n|${x}$${y}$${z}$${vararg}$|\n#:enddef\n'\
469 '#:block macro\n'\
470 '#:contains\n1\n'\
471 '#:contains z\n3\n'\
472 '#:contains y\n'\
473 '2\n'\
474 '#:endblock\n',
475 '|123[]|\n'
476 )
477 ),
478 ('macro_vararg_mixed_arguments_inline_call',
479 ([],
480 '#:def macro(x, y, z, *vararg)\n|${x}$${y}$${z}$${vararg}$|\n#:enddef\n'\
481 '#{call macro}#1#{nextarg z}#3#{nextarg y}#2#{endcall}#',
482 '|123[]|'
483 )
484 ),
485 ('macro_vararg_mixed_arguments_inline_block',
486 ([],
487 '#:def macro(x, y, z, *vararg)\n|${x}$${y}$${z}$${vararg}$|\n#:enddef\n'\
488 '#{block macro}#1#{contains z}#3#{contains y}#2#{endblock}#',
489 '|123[]|'
490 )
491 ),
492 ('macro_vararg_mixed_arguments_inline_call2',
493 ([],
494 '#:def macro(x, y, z, *vararg)\n|${x}$${y}$${z}$${vararg}$|\n#:enddef\n'\
495 '#{call macro}##{nextarg}#1#{nextarg z}#3#{nextarg y}#2#{endcall}#',
496 '|123[]|'
497 )
498 ),
499 ('macro_vararg_mixed_arguments_inline_block2',
500 ([],
501 '#:def macro(x, y, z, *vararg)\n|${x}$${y}$${z}$${vararg}$|\n#:enddef\n'\
502 '#{block macro}##{contains}#1#{contains z}#3#{contains y}#2#{endblock}#',
503 '|123[]|'
504 )
505 ),
506 ('macro_varpos_varkw_with_keyword_arguments',
507 ([],
508 '#:def macro(x, y, *vararg, **varkw)\n'\
509 '|${x}$${y}$${varkw["z"]}$${vararg}$|\n'\
510 '#:enddef\n'\
511 '$:macro(1, 2, z=3)\n',
512 '|123[]|\n'
513 )
514 ),
515 ('macro_varpos_varkw_with_pos_arguments',
516 ([],
517 '#:def macro(x, y, *vararg, **varkw)\n'\
518 '|${x}$${y}$${vararg}$|\n'\
519 '#:enddef\n'\
520 '$:macro(1, 2, 4, 5)\n',
521 '|12[4, 5]|\n'
522 )
523 ),
524 ('macro_varpos_varkw_with_pos_and_kw_arguments',
525 ([],
526 '#:def macro(x, y, *vararg, **varkw)\n'\
527 '|${x}$${y}$${varkw["z"]}$${vararg}$|\n'\
528 '#:enddef\n'\
529 '$:macro(1, 2, 4, 5, z=3)\n',
530 '|123[4, 5]|\n'
531 )
532 ),
533 ('for',
534 ([],
535 '#:for i in (1, 2, 3)\n${i}$\n#:endfor\n',
536 '1\n2\n3\n'
537 )
538 ),
539 ('for_macro',
540 ([],
541 '#:def mymacro(val)\nVAL:${val}$\n#:enddef\n'
542 '#:for i in (1, 2, 3)\n$: mymacro(i)\n#:endfor\n',
543 'VAL:1\nVAL:2\nVAL:3\n'
544 )
545 ),
546 ('inline_for',
547 ([],
548 '#{for i in (1, 2, 3)}#${i}$#{endfor}#Done\n',
549 '123Done\n'
550 )
551 ),
552 ('inline_for_macro',
553 ([],
554 '#:def mymacro(val)\nVAL:${val}$\n#:enddef\n'
555 '#{for i in (1, 2, 3)}#${mymacro(i)}$#{endfor}#Done\n',
556 'VAL:1VAL:2VAL:3Done\n'
557 )
558 ),
559 ('call_directive',
560 ([],
561 '#:def mymacro(val)\n|${val}$|\n#:enddef\n'\
562 '#:call mymacro\nL1\nL2\nL3\n#:endcall\n',
563 '|L1\nL2\nL3|\n',
564 )
565 ),
566 ('block_directive',
567 ([],
568 '#:def mymacro(val)\n|${val}$|\n#:enddef\n'\
569 '#:block mymacro\nL1\nL2\nL3\n#:endblock\n',
570 '|L1\nL2\nL3|\n',
571 )
572 ),
573 ('call_directive_named_endcall',
574 ([],
575 '#:def mymacro(val)\n|${val}$|\n#:enddef\n'\
576 '#:call mymacro\nL1\nL2\nL3\n#:endcall mymacro\n',
577 '|L1\nL2\nL3|\n',
578 )
579 ),
580 ('block_directive_named_endblock',
581 ([],
582 '#:def mymacro(val)\n|${val}$|\n#:enddef\n'\
583 '#:block mymacro\nL1\nL2\nL3\n#:endblock mymacro\n',
584 '|L1\nL2\nL3|\n',
585 )
586 ),
587 ('inine_call_directive_named_endcall',
588 ([],
589 '#:def mymacro(val)\n|${val}$|\n#:enddef\n'\
590 '#{call mymacro}#L1 L2 L3#{endcall mymacro}#',
591 '|L1 L2 L3|',
592 )
593 ),
594 ('inine_block_directive_named_endblock',
595 ([],
596 '#:def mymacro(val)\n|${val}$|\n#:enddef\n'\
597 '#{block mymacro}#L1 L2 L3#{endblock mymacro}#',
598 '|L1 L2 L3|',
599 )
600 ),
601 ('call_directive_quotation',
602 ([],
603 '#:def mymacro(val)\n|${val}$|\n#:enddef\n'\
604 '#:call mymacro\n"""L1"""\nL2\nL3\n#:endcall\n',
605 '|"""L1"""\nL2\nL3|\n',
606 )
607 ),
608 ('block_directive_quotation',
609 ([],
610 '#:def mymacro(val)\n|${val}$|\n#:enddef\n'\
611 '#:block mymacro\n"""L1"""\nL2\nL3\n#:endblock\n',
612 '|"""L1"""\nL2\nL3|\n',
613 )
614 ),
615 ('call_directive_backslash_escape1',
616 ([],
617 '#:def mymacro(val)\n|${val}$|\n#:enddef\n'\
618 '#:call mymacro\nL1\\n\nL2\nL3\n#:endcall\n',
619 '|L1\\n\nL2\nL3|\n',
620 )
621 ),
622 ('block_directive_backslash_escape1',
623 ([],
624 '#:def mymacro(val)\n|${val}$|\n#:enddef\n'\
625 '#:block mymacro\nL1\\n\nL2\nL3\n#:endblock\n',
626 '|L1\\n\nL2\nL3|\n',
627 )
628 ),
629 ('call_directive_backslash_escape2',
630 ([],
631 '#:def mymacro(val)\n|${val}$|\n#:enddef\n'\
632 '#:call mymacro\nL1\\"a\\"\\n\nL2\nL3\n#:endcall\n',
633 '|L1\\"a\\"\\n\nL2\nL3|\n',
634 )
635 ),
636 ('block_directive_backslash_escape2',
637 ([],
638 '#:def mymacro(val)\n|${val}$|\n#:enddef\n'\
639 '#:block mymacro\nL1\\"a\\"\\n\nL2\nL3\n#:endblock\n',
640 '|L1\\"a\\"\\n\nL2\nL3|\n',
641 )
642 ),
643 ('call_directive_2_args',
644 ([],
645 '#:def mymacro(val1, val2)\n|${val1}$|${val2}$|\n#:enddef\n'\
646 '#:call mymacro\n"""L1"""\nL2\n#:nextarg\nL3\n#:endcall\n',
647 '|"""L1"""\nL2|L3|\n',
648 )
649 ),
650 ('block_directive_2_args',
651 ([],
652 '#:def mymacro(val1, val2)\n|${val1}$|${val2}$|\n#:enddef\n'\
653 '#:block mymacro\n"""L1"""\nL2\n#:contains\nL3\n#:endblock\n',
654 '|"""L1"""\nL2|L3|\n',
655 )
656 ),
657 ('call_directive_2_args_inline',
658 ([],
659 '#:def mymacro(val1, val2)\n|${val1}$|${val2}$|\n#:enddef\n'\
660 '#{call mymacro}#A1#{nextarg}#A2#{endcall}#',
661 '|A1|A2|',
662 )
663 ),
664 ('block_directive_2_args_inline',
665 ([],
666 '#:def mymacro(val1, val2)\n|${val1}$|${val2}$|\n#:enddef\n'\
667 '#{block mymacro}#A1#{contains}#A2#{endblock}#',
668 '|A1|A2|',
669 )
670 ),
671 ('call_lambda_func',
672 ([],
673 '#:set convert = lambda s: s.lower()\n'\
674 '#:call convert\nHELLO\n#:endcall\n',
675 'hello\n',
676 )
677 ),
678 ('call_no_header_arg_no_body_arg',
679 ([],
680 '#:def macro0()\nNOARG\n#:enddef\n'\
681 '#:call macro0()\n#:endcall\n',
682 'NOARG\n',
683 )
684 ),
685 ('block_no_header_arg_no_body_arg',
686 ([],
687 '#:def macro0()\nNOARG\n#:enddef\n'\
688 '#:block macro0()\n#:endblock\n',
689 'NOARG\n',
690 )
691 ),
692 ('call_header_pos_arg_no_body_arg',
693 ([],
694 '#:def macro(arg)\n|${arg}$|\n#:enddef\n'\
695 '#:call macro("h1")\n#:endcall\n',
696 '|h1|\n',
697 )
698 ),
699 ('block_header_pos_arg_no_body_arg',
700 ([],
701 '#:def macro(arg)\n|${arg}$|\n#:enddef\n'\
702 '#:block macro("h1")\n#:endblock\n',
703 '|h1|\n',
704 )
705 ),
706 ('call_header_kwarg_no_body_arg',
707 ([],
708 '#:def macro(arg)\n|${arg}$|\n#:enddef\n'\
709 '#:call macro(arg="h1")\n#:endcall\n',
710 '|h1|\n',
711 )
712 ),
713 ('block_header_kwarg_no_body_arg',
714 ([],
715 '#:def macro(arg)\n|${arg}$|\n#:enddef\n'\
716 '#:block macro(arg="h1")\n#:endblock\n',
717 '|h1|\n',
718 )
719 ),
720 ('call_header_mixed_args_no_body_arg',
721 ([],
722 '#:def macro(arg, arg2)\n|${arg}$|${arg2}$|\n#:enddef\n'\
723 '#:call macro("h1", arg2="h2")\n#:endcall\n',
724 '|h1|h2|\n',
725 )
726 ),
727 ('block_header_mixed_args_no_body_arg',
728 ([],
729 '#:def macro(arg, arg2)\n|${arg}$|${arg2}$|\n#:enddef\n'\
730 '#:block macro("h1", arg2="h2")\n#:endblock\n',
731 '|h1|h2|\n',
732 )
733 ),
734 ('call_header_mixed_args_body_pos_arg',
735 ([],
736 '#:def macro(arg, arg2, arg3)\n|${arg}$|${arg2}$|${arg3}$|\n#:enddef\n'\
737 '#:call macro("h1", arg3="h3")\nB1\n#:endcall\n',
738 '|h1|B1|h3|\n',
739 )
740 ),
741 ('block_header_mixed_args_body_pos_arg',
742 ([],
743 '#:def macro(arg, arg2, arg3)\n|${arg}$|${arg2}$|${arg3}$|\n#:enddef\n'\
744 '#:block macro("h1", arg3="h3")\nB1\n#:endblock\n',
745 '|h1|B1|h3|\n',
746 )
747 ),
748 ('call_header_kwargs_body_pos_arg',
749 ([],
750 '#:def macro(arg, arg2, arg3)\n|${arg}$|${arg2}$|${arg3}$|\n#:enddef\n'\
751 '#:call macro(arg3="h3", arg2="h2")\nB1\n#:endcall\n',
752 '|B1|h2|h3|\n',
753 )
754 ),
755 ('block_header_kwargs_body_pos_arg',
756 ([],
757 '#:def macro(arg, arg2, arg3)\n|${arg}$|${arg2}$|${arg3}$|\n#:enddef\n'\
758 '#:block macro(arg3="h3", arg2="h2")\nB1\n#:endblock\n',
759 '|B1|h2|h3|\n',
760 )
761 ),
762 ('call_header_kwargs_body_kwarg',
763 ([],
764 '#:def macro(arg1, arg2, arg3)\n|${arg1}$|${arg2}$|${arg3}$|\n#:enddef\n'\
765 '#:call macro(arg1="h1", arg3="h3")\n#:nextarg arg2\nB1\n#:endcall\n',
766 '|h1|B1|h3|\n',
767 )
768 ),
769 ('block_header_kwargs_body_kwarg',
770 ([],
771 '#:def macro(arg1, arg2, arg3)\n|${arg1}$|${arg2}$|${arg3}$|\n#:enddef\n'\
772 '#:block macro(arg1="h1", arg3="h3")\n#:contains arg2\nB1\n#:endblock\n',
773 '|h1|B1|h3|\n',
774 )
775 ),
776 ('direct_call',
777 ([],
778 '#:def mymacro(val)\n|${val}$|\n#:enddef\n'\
779 '@:mymacro(a < b)\n',
780 '|a < b|\n',
781 )
782 ),
783 ('direct_call_whitespace',
784 ([],
785 '#:def mymacro(val)\n|${val}$|\n#:enddef\n'\
786 '@:mymacro (a < b)\n',
787 '|a < b|\n',
788 )
789 ),
790 ('direct_call_inline',
791 ([],
792 '#:def mymacro(val)\n|${val}$|\n#:enddef\n'\
793 '@{mymacro(a < b)}@',
794 '|a < b|',
795 )
796 ),
797 ('direct_call2',
798 ([],
799 '#:def mymacro(val)\n|${val}$|\n#:enddef\n'\
800 '@:mymacro(a < b )\n',
801 '|a < b|\n',
802 )
803 ),
804 ('direct_call2_inline',
805 ([],
806 '#:def mymacro(val)\n|${val}$|\n#:enddef\n'\
807 '@{mymacro(a < b )}@',
808 '|a < b|',
809 )
810 ),
811 ('direct_call3',
812 ([],
813 '#:def mymacro(val)\n|${val}$|\n#:enddef\n'\
814 '@:mymacro( a < b)\n',
815 '|a < b|\n',
816 )
817 ),
818 ('direct_call3_inline',
819 ([],
820 '#:def mymacro(val)\n|${val}$|\n#:enddef\n'\
821 '@{mymacro( a < b)}@',
822 '|a < b|',
823 )
824 ),
825 ('direct_call4',
826 ([],
827 '#:def mymacro(val)\n|${val}$|\n#:enddef\n'\
828 '@:mymacro(a < b)\n',
829 '|a < b|\n',
830 )
831 ),
832 ('direct_call4_inline',
833 ([],
834 '#:def mymacro(val)\n|${val}$|\n#:enddef\n'\
835 '@{mymacro(a < b)}@',
836 '|a < b|',
837 )
838 ),
839 ('direct_call_contline',
840 ([],
841 '#:def mymacro(val)\n|${val}$|\n#:enddef\n'\
842 '@:mymacro(a &\n &< b&\n &)\n',
843 '|a < b|\n',
844 )
845 ),
846 ('direct_call_quotation',
847 ([],
848 '#:def mymacro(val)\n|${val}$|\n#:enddef\n'\
849 '@:mymacro( """L1""" )\n',
850 '|"""L1"""|\n',
851 )
852 ),
853 ('direct_call_quotation_inline',
854 ([],
855 '#:def mymacro(val)\n|${val}$|\n#:enddef\n'\
856 '@{mymacro( """L1""" )}@',
857 '|"""L1"""|',
858 )
859 ),
860 ('direct_call_backslash_escape1',
861 ([],
862 '#:def mymacro(val)\n|${val}$|\n#:enddef\n'\
863 '@:mymacro(L1\\n)\n',
864 '|L1\\n|\n',
865 )
866 ),
867 ('direct_call_backslash_escape1_inline',
868 ([],
869 '#:def mymacro(val)\n|${val}$|\n#:enddef\n'\
870 '@{mymacro(L1\\n)}@',
871 '|L1\\n|',
872 )
873 ),
874 ('direct_call_backslash_escape2',
875 ([],
876 '#:def mymacro(val)\n|${val}$|\n#:enddef\n'\
877 '@:mymacro(L1\\"a\\"\\n)\n',
878 '|L1\\"a\\"\\n|\n',
879 )
880 ),
881 ('direct_call_backslash_escape2_inline',
882 ([],
883 '#:def mymacro(val)\n|${val}$|\n#:enddef\n'\
884 '@{mymacro(L1\\"a\\"\\n)}@',
885 '|L1\\"a\\"\\n|',
886 )
887 ),
888 ('direct_call_2_args',
889 ([],
890 '#:def mymacro(val1, val2)\n|${val1}$|${val2}$|\n#:enddef\n'\
891 '@:mymacro("""L1""", L2)\n',
892 '|"""L1"""|L2|\n',
893 )
894 ),
895 ('direct_call_2_args_inline',
896 ([],
897 '#:def mymacro(val1, val2)\n|${val1}$|${val2}$|\n#:enddef\n'\
898 '@{mymacro("""L1""", L2)}@',
899 '|"""L1"""|L2|',
900 )
901 ),
902 ('direct_call_2_args_escape1',
903 ([],
904 '#:def mymacro(val1, val2)\n|${val1}$|${val2}$|\n#:enddef\n'\
905 '@:mymacro("""L1"""","L2, L3)\n',
906 '|"""L1"""","L2|L3|\n',
907 )
908 ),
909 ('direct_call_2_args_escape1_inline',
910 ([],
911 '#:def mymacro(val1, val2)\n|${val1}$|${val2}$|\n#:enddef\n'\
912 '@{mymacro("""L1"""","L2, L3)}@',
913 '|"""L1"""","L2|L3|',
914 )
915 ),
916 ('direct_call_2_args_escape2',
917 ([],
918 '#:def mymacro(val1, val2)\n|${val1}$|${val2}$|\n#:enddef\n'\
919 '@:mymacro((L1, L2), L3)\n',
920 '|(L1, L2)|L3|\n',
921 )
922 ),
923 ('direct_call_2_args_escape2_inline',
924 ([],
925 '#:def mymacro(val1, val2)\n|${val1}$|${val2}$|\n#:enddef\n'\
926 '@{mymacro((L1, L2), L3)}@',
927 '|(L1, L2)|L3|',
928 )
929 ),
930 ('direct_call_2_args_escape3',
931 ([],
932 '#:def mymacro(val1, val2)\n|${val1}$|${val2}$|\n#:enddef\n'\
933 '@:mymacro({L1, L2}, L3)\n',
934 '|L1, L2|L3|\n',
935 )
936 ),
937 ('direct_call_2_args_escape3_inline',
938 ([],
939 '#:def mymacro(val1, val2)\n|${val1}$|${val2}$|\n#:enddef\n'\
940 '@{mymacro({L1, L2}, L3)}@',
941 '|L1, L2|L3|',
942 )
943 ),
944 ('direct_call_2_args_escape4',
945 ([],
946 '#:def mymacro(val1, val2)\n|${val1}$|${val2}$|\n#:enddef\n'\
947 '@:mymacro([L1, L2], L3)\n',
948 '|[L1, L2]|L3|\n',
949 )
950 ),
951 ('direct_call_2_args_escape4_inline',
952 ([],
953 '#:def mymacro(val1, val2)\n|${val1}$|${val2}$|\n#:enddef\n'\
954 '@{mymacro([L1, L2], L3)}@',
955 '|[L1, L2]|L3|',
956 )
957 ),
958 ('direct_call_2_args_escape5',
959 ([],
960 '#:def mymacro(val1, val2)\n|${val1}$|${val2}$|\n#:enddef\n'\
961 '@:mymacro("L1, L2", L3)\n',
962 '|"L1, L2"|L3|\n',
963 )
964 ),
965 ('direct_call_2_args_escape5_inline',
966 ([],
967 '#:def mymacro(val1, val2)\n|${val1}$|${val2}$|\n#:enddef\n'\
968 '@{mymacro("L1, L2", L3)}@',
969 '|"L1, L2"|L3|',
970 )
971 ),
972 ('direct_call_2_args_escape6',
973 ([],
974 '#:def mymacro(val1, val2)\n|${val1}$|${val2}$|\n#:enddef\n'\
975 '@:mymacro(\'L1, L2\', L3)\n',
976 '|\'L1, L2\'|L3|\n',
977 )
978 ),
979 ('direct_call_2_args_escape6_inline',
980 ([],
981 '#:def mymacro(val1, val2)\n|${val1}$|${val2}$|\n#:enddef\n'\
982 '@{mymacro(\'L1, L2\', L3)}@',
983 '|\'L1, L2\'|L3|',
984 )
985 ),
986 ('direct_call_2_args_escape7',
987 ([],
988 '#:def mymacro(val1, val2)\n|${val1}$|${val2}$|\n#:enddef\n'\
989 '@:mymacro(L1 ${2, 2}$, L3)\n',
990 '|L1 (2, 2)|L3|\n',
991 )
992 ),
993 ('direct_call_2_args_escape7_inline',
994 ([],
995 '#:def mymacro(val1, val2)\n|${val1}$|${val2}$|\n#:enddef\n'\
996 '@{mymacro(L1 ${2, 2}$, L3)}@',
997 '|L1 (2, 2)|L3|',
998 )
999 ),
1000 ('direct_call_2_args_escape8',
1001 ([],
1002 '#:def mymacro(val1, val2)\n|${val1}$|${val2}$|\n#:enddef\n'\
1003 '@:mymacro({{L1, L2}}, L3)\n',
1004 '|{L1, L2}|L3|\n',
1005 )
1006 ),
1007 ('direct_call_2_args_escape8_inline',
1008 ([],
1009 '#:def mymacro(val1, val2)\n|${val1}$|${val2}$|\n#:enddef\n'\
1010 '@{mymacro({{L1, L2}}, L3)}@',
1011 '|{L1, L2}|L3|',
1012 )
1013 ),
1014 ('direct_call_kwarg',
1015 ([],
1016 '#:def mymacro(a)\n|${a}$|\n#:enddef\n'\
1017 '@:mymacro(a = b)\n',
1018 '|b|\n',
1019 )
1020 ),
1021 ('direct_call_kwarg_eq_operator',
1022 ([],
1023 '#:def mymacro(a)\n|${a}$|\n#:enddef\n'\
1024 '@:mymacro(a == b)\n',
1025 '|a == b|\n',
1026 )
1027 ),
1028 ('direct_call_kwarg_ptr_assignment',
1029 ([],
1030 '#:def mymacro(a)\n|${a}$|\n#:enddef\n'\
1031 '@:mymacro(a => b)\n',
1032 '|> b|\n',
1033 )
1034 ),
1035 ('direct_call_kwarg_escape',
1036 ([],
1037 '#:def mymacro(val1)\n|${val1}$|\n#:enddef\n'\
1038 '@:mymacro({a = b})\n',
1039 '|a = b|\n',
1040 )
1041 ),
1042 ('direct_call_varsubs',
1043 ([],
1044 '#:def mymacro(val1)\n|${val1}$|\n#:enddef\n'\
1045 '@:mymacro(2x2=${2*2}$)\n',
1046 '|2x2=4|\n',
1047 )
1048 ),
1049 ('direct_call_varsubs_inline',
1050 ([],
1051 '#:def mymacro(val1)\n|${val1}$|\n#:enddef\n'\
1052 '@{mymacro(2x2=${2*2}$)}@',
1053 '|2x2=4|',
1054 )
1055 ),
1056 ('direct_call_varsubs_2_args',
1057 ([],
1058 '#:def mymacro(val1, val2)\n|${val1}$|${val2}$|\n#:enddef\n'\
1059 '@:mymacro(${2*1}$, ${2*2}$)\n',
1060 '|2|4|\n',
1061 )
1062 ),
1063 ('direct_call_varsubs_2_args_inline',
1064 ([],
1065 '#:def mymacro(val1, val2)\n|${val1}$|${val2}$|\n#:enddef\n'\
1066 '@{mymacro(${2*1}$, ${2*2}$)}@',
1067 '|2|4|',
1068 )
1069 ),
1070 ('direct_call_varsubs_2_args_escape',
1071 ([],
1072 '#:def mymacro(val1, val2)\n|${val1}$|${val2}$|\n#:enddef\n'\
1073 '@:mymacro((${2*1}$, ${2*2}$), ${2*3}$)\n',
1074 '|(2, 4)|6|\n',
1075 )
1076 ),
1077 ('direct_call_varsubs_2_args_escape_inline',
1078 ([],
1079 '#:def mymacro(val1, val2)\n|${val1}$|${val2}$|\n#:enddef\n'\
1080 '@{mymacro((${2*1}$, ${2*2}$), ${2*3}$)}@',
1081 '|(2, 4)|6|',
1082 )
1083 ),
1084 ('direct_call_no_param',
1085 ([],
1086 '#:def mymacro()\n||\n#:enddef mymacro\n'\
1087 '@:mymacro()\n',
1088 '||\n'
1089 )
1090 ),
1091 ('direct_call_no_param_inline',
1092 ([],
1093 '#:def mymacro()\n||\n#:enddef mymacro\n'\
1094 '@{mymacro()}@',
1095 '||'
1096 )
1097 ),
1098 ('direct_call_no_param2',
1099 ([],
1100 '#:def mymacro()\n||\n#:enddef mymacro\n'\
1101 '@:mymacro( )\n',
1102 '||\n'
1103 )
1104 ),
1105 ('direct_call_no_param2_inline',
1106 ([],
1107 '#:def mymacro()\n||\n#:enddef mymacro\n'\
1108 '@{mymacro( )}@',
1109 '||'
1110 )
1111 ),
1112 ('call_no_param_inline',
1113 ([],
1114 '#:def mymacro()\n||\n#:enddef mymacro\n'\
1115 '#{call mymacro}##{endcall}#\n',
1116 '||\n'
1117 )
1118 ),
1119 ('block_no_param_inline',
1120 ([],
1121 '#:def mymacro()\n||\n#:enddef mymacro\n'\
1122 '#{block mymacro}##{endblock}#\n',
1123 '||\n'
1124 )
1125 ),
1126 ('call_no_param',
1127 ([],
1128 '#:def mymacro()\n||\n#:enddef mymacro\n'\
1129 '#:call mymacro\n#:endcall\n',
1130 '||\n'
1131 )
1132 ),
1133 ('block_no_param',
1134 ([],
1135 '#:def mymacro()\n||\n#:enddef mymacro\n'\
1136 '#:block mymacro\n#:endblock\n',
1137 '||\n'
1138 )
1139 ),
1140 ('call_empty_param_inline',
1141 ([],
1142 '#:def mymacro(txt)\n|${txt}$|\n#:enddef mymacro\n'\
1143 '#{call mymacro}# #{endcall}#\n',
1144 '| |\n'
1145 )
1146 ),
1147 ('block_empty_param_inline',
1148 ([],
1149 '#:def mymacro(txt)\n|${txt}$|\n#:enddef mymacro\n'\
1150 '#{block mymacro}# #{endblock}#\n',
1151 '| |\n'
1152 )
1153 ),
1154 ('call_empty_param',
1155 ([],
1156 '#:def mymacro(txt)\n|${txt}$|\n#:enddef mymacro\n'\
1157 '#:call mymacro\n\n#:endcall\n',
1158 '||\n'
1159 )
1160 ),
1161 ('block_empty_param',
1162 ([],
1163 '#:def mymacro(txt)\n|${txt}$|\n#:enddef mymacro\n'\
1164 '#:block mymacro\n\n#:endblock\n',
1165 '||\n'
1166 )
1167 ),
1168 ('call_empty_param_directcall',
1169 ([],
1170 '#:def mymacro(txt)\n|${txt}$|\n#:enddef mymacro\n'\
1171 '@:mymacro({})\n',
1172 '||\n'
1173 )
1174 ),
1175 ('call_whitespace_param_directcall',
1176 ([],
1177 '#:def mymacro(txt)\n|${txt}$|\n#:enddef mymacro\n'\
1178 '@:mymacro({ })\n',
1179 '| |\n'
1180 )
1181 ),
1182 ('comment_single',
1183 ([],
1184 ' #! Comment here\nDone\n',
1185 'Done\n',
1186 )
1187 ),
1188 ('comment_multiple',
1189 ([],
1190 ' #! Comment1\n#! Comment2\nDone\n',
1191 'Done\n',
1192 )
1193 ),
1194 ('set',
1195 ([],
1196 '#:set x = 2\n$: x\n',
1197 '2\n',
1198 )
1199 ),
1200 ('set_no_rhs',
1201 ([],
1202 '#:set x\n$:x\n',
1203 '\n',
1204 )
1205 ),
1206 ('set_equal_sign_nospace',
1207 ([],
1208 '#:set x=2\n$: x\n',
1209 '2\n',
1210 )
1211 ),
1212 ('set_equal_sign_withspace',
1213 ([],
1214 '#:set x = 2\n$: x\n',
1215 '2\n',
1216 )
1217 ),
1218 ('inline_set_equal_withspace',
1219 ([],
1220 '#{set x = 2}#${x}$Done\n',
1221 '2Done\n',
1222 )
1223 ),
1224 ('inline_set_equal_nospace',
1225 ([],
1226 '#{set x=2}#${x}$Done\n',
1227 '2Done\n',
1228 )
1229 ),
1230 ('set_function',
1231 ([],
1232 '$:setvar("x", 2)\n${x}$\nDone\n',
1233 "\n2\nDone\n",
1234 )
1235 ),
1236 ('set_function_tuple',
1237 ([],
1238 '$:setvar("x, y", (2, 3))\n${x}$${y}$\nDone\n',
1239 "\n23\nDone\n",
1240 )
1241 ),
1242 ('set_function_tuple2',
1243 ([],
1244 '$:setvar("(x, y)", (2, 3))\n${x}$${y}$\nDone\n',
1245 "\n23\nDone\n",
1246 )
1247 ),
1248 ('set_function_multiple_args',
1249 ([],
1250 '$:setvar("x", 2, "y", 3)\n${x}$${y}$\nDone\n',
1251 "\n23\nDone\n",
1252 )
1253 ),
1254 ('getvar_existing_value',
1255 ([_defvar('VAR', '\"VAL\"')],
1256 '$:getvar("VAR", "DEFAULT")\n',
1257 'VAL\n',
1258 )
1259 ),
1260 ('getvar_default_value',
1261 ([],
1262 '$:getvar("VAR", "DEFAULT")\n',
1263 'DEFAULT\n',
1264 )
1265 ),
1266 ('getvar_local_scope',
1267 ([],
1268 '#:set X = 1\n'\
1269 '#:def test()\n$:getvar("X")\n#:set X = 2\n$:getvar("X")\n#:enddef\n'\
1270 '$:test()\n',
1271 '1\n2\n',
1272 )
1273 ),
1274 ('del_existing_variable',
1275 ([],
1276 '#:set X = 12\n$:defined("X")\n#:del X\n$:defined("X")\n',
1277 'True\nFalse\n',
1278 )
1279 ),
1280 ('del_variable_tuple',
1281 ([],
1282 '#:set X = 1\n#:set Y = 2\n${defined("X")}$${defined("Y")}$\n'\
1283 '#:del X, Y\n${defined("X")}$${defined("Y")}$\n',
1284 'TrueTrue\nFalseFalse\n',
1285 )
1286 ),
1287 ('del_variable_local_scope',
1288 ([],
1289 '#:set echo = lambda s: s\n#:set X = 1\n'\
1290 '#:call echo\n$:X\n#:set X = 2\n$:X\n$:defined("X")\n'\
1291 '#:del X\n$:defined("X")\n#:endcall\n$:X\n',
1292 '1\n2\nTrue\nFalse\n1\n',
1293 )
1294 ),
1295 ('del_macro',
1296 ([],
1297 '#:def mymacro(txt)\n|${txt}$|\n#:enddef mymacro\n$:defined("mymacro")\n'\
1298 '$:mymacro("A")\n#:del mymacro\n$:defined("mymacro")\n',
1299 'True\n|A|\nFalse\n',
1300 )
1301 ),
1302 ('del_inline',
1303 ([],
1304 '#:set X = 12\n$:defined("X")\n#{del X}#${defined("X")}$\n',
1305 'True\nFalse\n',
1306 )
1307 ),
1308 ('del_inline_tuple',
1309 ([],
1310 '#:set X = 1\n#:set Y = 2\n${defined("X")}$${defined("Y")}$\n'\
1311 '#{del X, Y}#${defined("X")}$${defined("Y")}$\n',
1312 'TrueTrue\nFalseFalse\n',
1313 )
1314 ),
1315 ('delvar_function',
1316 ([],
1317 '#:set X = 12\n$:defined("X")\n$:delvar("X")\n$:defined("X")\n',
1318 'True\n\nFalse\n',
1319 )
1320 ),
1321 ('delvar_function_tuple',
1322 ([],
1323 '#:set X = 1\n#:set Y = 2\n${defined("X")}$${defined("Y")}$\n'\
1324 '$:delvar("X, Y")\n${defined("X")}$${defined("Y")}$\n',
1325 'TrueTrue\n\nFalseFalse\n',
1326 )
1327 ),
1328 ('delvar_function_multiple_args',
1329 ([],
1330 '#:set X = 1\n#:set Y = 2\n${defined("X")}$${defined("Y")}$\n'\
1331 '$:delvar("X", "Y")\n${defined("X")}$${defined("Y")}$\n',
1332 'TrueTrue\n\nFalseFalse\n',
1333 )
1334 ),
1335 ('mute',
1336 ([],
1337 'A\n#:mute\nB\n#:set VAR = 2\n#:endmute\nVAR=${VAR}$\n',
1338 'A\nVAR=2\n'
1339 )
1340 ),
1341 ('builtin_var_line',
1342 ([],
1343 '${_LINE_}$',
1344 '1'
1345 )
1346 ),
1347 ('builtin_var_file',
1348 ([],
1349 '${_FILE_}$',
1350 fypp.STRING
1351 )
1352 ),
1353 ('builtin_var_line_in_lineeval',
1354 ([],
1355 '$:_LINE_\n',
1356 '1\n'
1357 )
1358 ),
1359 ('builtin_var_system',
1360 ([],
1361 '${_SYSTEM_}$',
1362 platform.system()
1363 )
1364 ),
1365 ('builtin_var_machine',
1366 ([],
1367 '${_MACHINE_}$',
1368 platform.machine()
1369 )
1370 ),
1371 ('escaped_control_inline',
1372 ([],
1373 r'A#\{if False}\#B#\{endif}\#',
1374 'A#{if False}#B#{endif}#'
1375 )
1376 ),
1377 ('escaped_control_line',
1378 ([],
1379 '#\\:if False\n',
1380 '#:if False\n'
1381 )
1382 ),
1383 ('escaped_eval_inline',
1384 ([],
1385 r'A$\{1 + 1}\$',
1386 'A${1 + 1}$'
1387 )
1388 ),
1389 ('escaped_eval_line',
1390 ([],
1391 '$\\: 1 + 1\n',
1392 '$: 1 + 1\n'
1393 )
1394 ),
1395 ('multi_escape',
1396 ([],
1397 r'$\\\{1 + 1}\\$',
1398 r'$\\{1 + 1}\$'
1399 )
1400 ),
1401 ('escape_direct_call',
1402 ([],
1403 '@\\:assertTrue(x > y)\n',
1404 '@:assertTrue(x > y)\n'
1405 )
1406 ),
1407 ('escape_direct_call_inline',
1408 ([],
1409 '@\\{assertTrue(x > y)}@',
1410 '@{assertTrue(x > y)}@'
1411 )
1412 ),
1413 ('escape_comment',
1414 ([],
1415 'A\n #\! Comment\n',
1416 'A\n #! Comment\n',
1417 )
1418 ),
1419 ('fold_lines',
1420 ([_linelen(10), _indentation(2), _folding('simple')],
1421 'This line is not folded\nThis line ${1 + 1}$ is folded\n',
1422 'This line is not folded\nThis line&\n & 2 is &\n &folded\n'
1423 )
1424 ),
1425 ('prevent_comment_folding',
1426 ([_linelen(10), _indentation(2), _folding('simple')],
1427 '#:def macro()\n ! Should be not folded\nShould be folded\n#:enddef\n'
1428 '$:macro()\n',
1429 ' ! Should be not folded\nShould be&\n & folded\n'
1430 )
1431 ),
1432 ('no_folding',
1433 ([_linelen(15), _indentation(4), _NO_FOLDING_FLAG],
1434 ' ${3}$456 89 123456 8',
1435 ' 3456 89 123456 8',
1436 )
1437 ),
1438 ('brute_folding',
1439 ([_linelen(15), _indentation(4), _folding('brute')],
1440 ' ${3}$456 89 123456 8',
1441 ' 3456 89 1234&\n &56 8',
1442 )
1443 ),
1444 ('simple_folding',
1445 ([_linelen(15), _indentation(4), _folding('simple')],
1446 ' ${3}$456 89 123456 8',
1447 ' 3456 89 1234&\n &56 8',
1448 )
1449 ),
1450 ('smart_folding',
1451 ([_linelen(15), _indentation(4), _folding('smart')],
1452 ' ${3}$456 89 123456 8',
1453 ' 3456 89&\n & 123456&\n & 8',
1454 )
1455 ),
1456 ('fixed_format_folding',
1457 ([_FIXED_FORMAT_FLAG],
1458 ' print *, ${\'aa\'}$, bb, cc, dd, ee, ff, gg, hh, ii, jj, kk, ll, '
1459 'mm, nn, oo, pp, qq, rr, ss, tt\n',
1460 ' print *, aa, bb, cc, dd, ee, ff, gg, hh, ii, jj, kk, ll, mm, nn, '
1461 'o\n &o, pp, qq, rr, ss, tt\n',
1462 )
1463 ),
1464 ('tuple_assignment',
1465 ([],
1466 '#:set mytuple = (1, 2, 3)\n#:set a, b, c = mytuple\n${a}$${b}$${c}$\n',
1467 '123\n'
1468 )
1469 ),
1470 ('tuple_assignment2',
1471 ([],
1472 '#:set a, b, c = (1, 2, 3)\n${a}$${b}$${c}$\n',
1473 '123\n'
1474 )
1475 ),
1476 ('tuple_assignment3',
1477 ([],
1478 '#:set a, b, c = 1, 2, 3\n${a}$${b}$${c}$\n',
1479 '123\n'
1480 )
1481 ),
1482 ('tuple_assignment_nospace',
1483 ([],
1484 '#:set a,b,c = (1, 2, 3)\n${a}$${b}$${c}$\n',
1485 '123\n'
1486 )
1487 ),
1488 ('tuple_assignment_vartuple',
1489 ([],
1490 '#:set (a, b, c) = (1, 2, 3)\n${a}$${b}$${c}$\n',
1491 '123\n'
1492 )
1493 ),
1494 ('tuple_assignment_vartuple2',
1495 ([],
1496 '#:set ( a, b, c ) = (1, 2, 3)\n${a}$${b}$${c}$\n',
1497 '123\n'
1498 )
1499 ),
1500 ('inline_tuple_assignment',
1501 ([],
1502 '#{set a, b, c = 1, 2, 3}#${a}$${b}$${c}$\n',
1503 '123\n'
1504 )
1505 ),
1506 ('inline_tuple_assignment_vartuple',
1507 ([],
1508 '#{set (a, b, c) = 1, 2, 3}#${a}$${b}$${c}$\n',
1509 '123\n'
1510 )
1511 ),
1512 ('whitespace_but_no_param',
1513 ([],
1514 '#:if True\nOK\n#:endif \n',
1515 'OK\n'
1516 )
1517 ),
1518 ('whitespace_but_no_param2',
1519 ([],
1520 '#:if True\nOK\n#:endif \n \n',
1521 'OK\n \n'
1522 )
1523 ),
1524 ('whitespace_but_no_param_inline',
1525 ([],
1526 '#{if True}#OK#{endif }#',
1527 'OK'
1528 )
1529 ),
1530 ('for_loop_scope',
1531 ([],
1532 '#{for i in range(4)}##{set X = i}##{endfor}#${X}$${i}$\n',
1533 '33\n'
1534 )
1535 ),
1536 ('macro_scope',
1537 ([],
1538 '#:set X = 3\n#:def setx()\n#:set X = -5\n#:enddef\n$:setx()\n$:X\n',
1539 '\n3\n'
1540 )
1541 ),
1542 ('local_macro_local_scope',
1543 ([],
1544 '#:set echo = lambda s: s\n'\
1545 '#:set X = 3\n#:call echo\n'\
1546 '#:def mymacro()\nX:${X}$\n#:enddef\n'\
1547 '#:set X = 2\n$:mymacro()\n#:endcall\n',
1548 'X:2\n',
1549 )
1550 ),
1551 ('local_macro_global_scope',
1552 ([],
1553 '#:set echo = lambda s: s\n'\
1554 '#:set X = 3\n#:call echo\n'
1555 '#:def mymacro()\nX:${X}$\n#:enddef\n'\
1556 '$:mymacro()\n#:endcall\n',
1557 'X:3\n',
1558 )
1559 ),
1560 ('scope_global_macro_called_from_local_scope',
1561 ([],
1562 '#:set echo = lambda s: s\n'\
1563 '#:def printX()\nX:${X}$\n#:enddef\n#:set X = 1\n'\
1564 '#:call echo\n#:set X = 2\n'\
1565 '#:call echo\n#:set X = 3\n$:printX()\n'\
1566 '#:endcall\n#:endcall\nX:${X}$\n',
1567 'X:1\nX:1\n',
1568 )
1569 ),
1570 ('scope_macro_lookup_locals_in_definition_scope',
1571 ([],
1572 '#:set X = 0\n'\
1573 '#:def macro1()\n#:set X = 1\n'\
1574 '#:def macro2()\n'\
1575 '#:def macro3a()\nX3a:${X}$\n#:enddef macro3a\n'\
1576 '#:def macro3b()\n#:set X = 3\n$:macro3a()\n#:enddef macro3b\n'\
1577 '#:set X = 2\n$:macro3b()\nX2:${X}$\n'\
1578 '#:enddef macro2\n$:macro2()\nX1:${X}$\n'\
1579 '#:enddef macro1\n$:macro1()\nX0:${X}$\n',
1580 'X3a:2\nX2:2\nX1:1\nX0:0\n',
1581 )
1582 ),
1583 ('scope_macro_lookup_locals_above_definition_scope',
1584 ([],
1585 '#:set X = 0\n'\
1586 '#:def macro1()\n#:set X = 1\n'\
1587 '#:def macro2()\n'\
1588 '#:def macro3a()\nX3a:${X}$\n#:enddef macro3a\n'\
1589 '#:def macro3b()\n#:set X = 3\n$:macro3a()\n#:enddef macro3b\n'\
1590 '$:macro3b()\nX2:${X}$\n'\
1591 '#:enddef macro2\n$:macro2()\nX1:${X}$\n'\
1592 '#:enddef macro1\n$:macro1()\nX0:${X}$\n',
1593 'X3a:1\nX2:1\nX1:1\nX0:0\n',
1594 )
1595 ),
1596 ('scope_macro_lookup_locals_global_scope',
1597 ([],
1598 '#:set X = 0\n'\
1599 '#:def macro1()\n'\
1600 '#:def macro2()\n'\
1601 '#:def macro3a()\nX3a:${X}$\n#:enddef macro3a\n'\
1602 '#:def macro3b()\n#:set X = 3\n$:macro3a()\n#:enddef macro3b\n'\
1603 '$:macro3b()\nX2:${X}$\n'\
1604 '#:enddef macro2\n$:macro2()\nX1:${X}$\n'\
1605 '#:enddef macro1\n$:macro1()\nX0:${X}$\n',
1606 'X3a:0\nX2:0\nX1:0\nX0:0\n',
1607 )
1608 ),
1609 ('scope_generator_within_macro',
1610 ([],
1611 '#:def foo()\n#:set b = 21\n$:sum([b for i in range(2)])\n#:enddef\n'\
1612 '$:foo()\n',
1613 '42\n'
1614 )
1615 ),
1616 ('correct_predefined_var_injection',
1617 ([],
1618 '#:def ASSERT(cond)\n"${cond}$", ${_FILE_}$, ${_LINE_}$\n#:enddef\n'\
1619 '@:ASSERT(2 < 3)\n',
1620 '"2 < 3", ' + fypp.STRING + ', 4\n'
1621 )
1622 ),
1623 ('correct_line_numbering_in_if',
1624 ([],
1625 '#:if _LINE_ == 1\nOK\n#:endif\n',
1626 'OK\n'
1627 )
1628 ),
1629 ('correct_line_numbering_in_for',
1630 ([],
1631 '#:for line in [_LINE_]\n${line}$ - ${_LINE_}$\n#:endfor\n',
1632 '1 - 2\n'
1633 )
1634 ),
1635 ('line_numbering_macro',
1636 ([],
1637 '#:def macro()\n${_THIS_LINE_}$,${_LINE_}$\n#:enddef macro\n'\
1638 '${_THIS_LINE_}$,${_LINE_}$|${macro()}$\n',
1639 '4,4|2,4\n'
1640 )
1641 ),
1642 ('line_numbering_argeval',
1643 ([],
1644 "#:set func = lambda s: str(_THIS_LINE_) + ',' + str(_LINE_) + '|' + s\n"\
1645 "#:call func\n${_THIS_LINE_}$,${_LINE_}$\n#:endcall\n",
1646 '2,2|3,3\n'
1647 )
1648 ),
1649 ('line_numbering_argeval_macrocall',
1650 ([_incdir('include')],
1651 "#:include 'assert.inc'\n"\
1652 "#:call ASSERT_CODE\n@:ASSERT()\n#:endcall ASSERT_CODE\n",
1653 'include/assert.inc:7|<string>:3\n'
1654 )
1655 ),
1656 ('line_numbering_eval_within_macro',
1657 ([],
1658 '#:def m1(A)\n${_LINE_}$\n#:enddef\n'\
1659 '#:def m2(A)\n#:call m1\n${A}$\n#:endcall\n#:enddef\n'\
1660 '$:m2(1)\n',
1661 '9\n'
1662 )
1663 ),
1664 ('global_existing',
1665 ([],
1666 '#:set A = 1\n#:def macro()\n#:global A\n#:set A = 2\n#:enddef macro\n'\
1667 '$:macro()\n$:A\n',
1668 '\n2\n'
1669 )
1670 ),
1671 ('global_non_existing',
1672 ([],
1673 '#:def macro()\n#:global A\n#:set A = 2\n#:enddef macro\n'\
1674 '$:defined("A")\n$:macro()\n$:A\n',
1675 'False\n\n2\n'
1676 )
1677 ),
1678 ('global_non_existing_evaldir',
1679 ([],
1680 '#:def macro()\n$:globalvar("A")\n#:set A = 2\n#:enddef macro\n'\
1681 '$:defined("A")\n$:macro()\n$:A\n',
1682 'False\n\n2\n'
1683 )
1684 ),
1685 ('global_non_existing_evaldir_tuple',
1686 ([],
1687 '#:def macro()\n$:globalvar("A, B")\n#:set A = 2\n#:set B = 3\n'\
1688 '#:enddef macro\n'\
1689 '$:defined("A")\n$:defined("B")\n$:macro()\n$:A\n$:B\n',
1690 'False\nFalse\n\n2\n3\n'
1691 )
1692 ),
1693 ('global_non_existing_evaldir_arglist',
1694 ([],
1695 '#:def macro()\n$:globalvar("A", "B")\n#:set A = 2\n#:set B = 3\n'\
1696 '#:enddef macro\n'\
1697 '$:defined("A")\n$:defined("B")\n$:macro()\n$:A\n$:B\n',
1698 'False\nFalse\n\n2\n3\n'
1699 )
1700 ),
1701 ('global_in_global_scope',
1702 ([],
1703 '#:set A = 1\n#:global A\n$:A\n',
1704 '1\n'
1705 )
1706 ),
1707 ('global_without_assignment',
1708 ([],
1709 '#:def macro()\n#:global A\n#:enddef macro\n'\
1710 '$:defined("A")\n$:macro()\n$:defined("A")\n',
1711 'False\n\nFalse\n'
1712 )
1713 ),
1714 ('define_with_equal_sign',
1715 ([_defvar("A", "'a=b'")],
1716 '${A}$',
1717 'a=b'
1718 )
1719 ),
1720 ]
1721
1722
1723 # Tests with line enumerations
1724 #
1725 # Each test consists of a tuple containing the test name and a tuple with the
1726 # arguments of the get_test_output_method() routine.
1727 #
1728 LINENUM_TESTS = [
1729 # Explicit test for line number marker format
1730 ('explicit_str_linenum_test',
1731 ([_LINENUM_FLAG],
1732 '',
1733 '# 1 "<string>"\n',
1734 )
1735 ),
1736 # Explicit test for line number marker format (GFortran5 compatibility)
1737 ('explicit_str_linenum_test_gfortran5',
1738 ([_LINENUM_FLAG, _linenum_gfortran5()],
1739 '',
1740 '# 1 "<string>" 1\n',
1741 )
1742 ),
1743 # Explicit test for standard line number marker format
1744 ('explicit_str_linenum_test_standard',
1745 ([_LINENUM_FLAG, _linenum_std()],
1746 '',
1747 '#line 1 "<string>"\n',
1748 )
1749 ),
1750 ('trivial',
1751 ([_LINENUM_FLAG],
1752 'Test\n',
1753 _linenum(0) + 'Test\n'
1754 )
1755 ),
1756 ('if_true',
1757 ([_LINENUM_FLAG],
1758 '#:if 1 < 2\nTrue\n#:endif\nDone\n',
1759 _linenum(0) + _linenum(1) + 'True\n' + _linenum(3)
1760 + 'Done\n'
1761 )
1762 ),
1763 ('if_false',
1764 ([_LINENUM_FLAG],
1765 '#:if 1 > 2\nTrue\n#:endif\nDone\n',
1766 _linenum(0) + _linenum(3) + 'Done\n'
1767 )
1768 ),
1769 ('if_else_true',
1770 ([_LINENUM_FLAG],
1771 '#:if 1 < 2\nTrue\n#:else\nFalse\n#:endif\nDone\n',
1772 _linenum(0) + _linenum(1) + 'True\n' + _linenum(5)
1773 + 'Done\n'
1774 )
1775 ),
1776 ('if_else_false',
1777 ([_LINENUM_FLAG],
1778 '#:if 1 > 2\nTrue\n#:else\nFalse\n#:endif\nDone\n',
1779 _linenum(0) + _linenum(3) + 'False\n' + _linenum(5)
1780 + 'Done\n'
1781 )
1782 ),
1783 ('if_elif_true1',
1784 ([_LINENUM_FLAG],
1785 '#:if 1 == 1\nTrue1\n#:elif 1 == 2\nTrue2\n#:endif\nDone\n',
1786 _linenum(0) + _linenum(1) + 'True1\n' + _linenum(5)
1787 + 'Done\n'
1788 )
1789 ),
1790 ('if_elif_true2',
1791 ([_LINENUM_FLAG],
1792 '#:if 2 == 1\nTrue1\n#:elif 2 == 2\nTrue2\n#:endif\nDone\n',
1793 _linenum(0) + _linenum(3) + 'True2\n' + _linenum(5)
1794 + 'Done\n'
1795 )
1796 ),
1797 ('if_elif_false',
1798 ([_LINENUM_FLAG],
1799 '#:if 0 == 1\nTrue1\n#:elif 0 == 2\nTrue2\n#:endif\nDone\n',
1800 _linenum(0) + _linenum(5) + 'Done\n'
1801 )
1802 ),
1803 ('if_elif_else_true1',
1804 ([_LINENUM_FLAG],
1805 '#:if 1 == 1\nTrue1\n#:elif 1 == 2\nTrue2\n'
1806 '#:else\nFalse\n#:endif\nDone\n',
1807 _linenum(0) + _linenum(1) + 'True1\n' + _linenum(7)
1808 + 'Done\n'
1809 )
1810 ),
1811 ('if_elif_else_true2',
1812 ([_LINENUM_FLAG],
1813 '#:if 2 == 1\nTrue1\n#:elif 2 == 2\nTrue2\n'
1814 '#:else\nFalse\n#:endif\nDone\n',
1815 _linenum(0) + _linenum(3) + 'True2\n' + _linenum(7)
1816 + 'Done\n'
1817 )
1818 ),
1819 ('if_elif_else_false',
1820 ([_LINENUM_FLAG],
1821 '#:if 0 == 1\nTrue1\n#:elif 0 == 2\nTrue2\n'
1822 '#:else\nFalse\n#:endif\nDone\n',
1823 _linenum(0) + _linenum(5) + 'False\n' + _linenum(7)
1824 + 'Done\n'
1825 )
1826 ),
1827 ('inline_if_true',
1828 ([_LINENUM_FLAG],
1829 '#{if 1 < 2}#True#{endif}#Done\n',
1830 _linenum(0) + 'TrueDone\n'
1831 )
1832 ),
1833 ('inline_if_false',
1834 ([_LINENUM_FLAG],
1835 '#{if 1 > 2}#True#{endif}#Done\n',
1836 _linenum(0) + 'Done\n'
1837 )
1838 ),
1839 ('inline_if_else_true',
1840 ([_LINENUM_FLAG],
1841 '#{if 1 < 2}#True#{else}#False#{endif}#Done\n',
1842 _linenum(0) + 'TrueDone\n'
1843 )
1844 ),
1845 ('inline_if_else_false',
1846 ([_LINENUM_FLAG],
1847 '#{if 1 > 2}#True#{else}#False#{endif}#Done\n',
1848 _linenum(0) + 'FalseDone\n'
1849 )
1850 ),
1851 ('inline_if_elif_true1',
1852 ([_LINENUM_FLAG],
1853 '#{if 1 == 1}#True1#{elif 1 == 2}#True2#{endif}#Done\n',
1854 _linenum(0) + 'True1Done\n'
1855 )
1856 ),
1857 ('inline_if_elif_true2',
1858 ([_LINENUM_FLAG],
1859 '#{if 2 == 1}#True1#{elif 2 == 2}#True2#{endif}#Done\n',
1860 _linenum(0) + 'True2Done\n'
1861 )
1862 ),
1863 ('inline_if_elif_false',
1864 ([_LINENUM_FLAG],
1865 '#{if 0 == 1}#True1#{elif 0 == 2}#True2#{endif}#Done\n',
1866 _linenum(0) + 'Done\n'
1867 )
1868 ),
1869 ('inline_if_elif_else_true1',
1870 ([_LINENUM_FLAG],
1871 '#{if 1 == 1}#True1#{elif 1 == 2}#True2#{else}#False#{endif}#Done\n',
1872 _linenum(0) + 'True1Done\n'
1873 )
1874 ),
1875 ('inline_if_elif_else_true2',
1876 ([_LINENUM_FLAG],
1877 '#{if 2 == 1}#True1#{elif 2 == 2}#True2#{else}#False#{endif}#Done\n',
1878 _linenum(0) + 'True2Done\n'
1879 )
1880 ),
1881 ('inline_if_elif_else_false',
1882 ([_LINENUM_FLAG],
1883 '#{if 0 == 1}#True1#{elif 0 == 2}#True2#{else}#False#{endif}#Done\n',
1884 _linenum(0) + 'FalseDone\n'
1885 )
1886 ),
1887 ('linesub_oneline',
1888 ([_LINENUM_FLAG],
1889 'A\n$: 1 + 1\nB\n',
1890 _linenum(0) + 'A\n2\nB\n'
1891 )
1892 ),
1893 ('linesub_contlines',
1894 ([_LINENUM_FLAG, _defvar('TESTVAR', 1)],
1895 '$: TESTVAR & \n & + 1\nDone\n',
1896 _linenum(0) + '2\n' + _linenum(2) + 'Done\n'
1897 )
1898 ),
1899 ('linesub_contlines2',
1900 ([_LINENUM_FLAG, _defvar('TESTVAR', 1)],
1901 '$: TEST& \n &VAR & \n & + 1\nDone\n',
1902 _linenum(0) + '2\n' + _linenum(3) + 'Done\n'
1903 )
1904 ),
1905 ('exprsub_single_line',
1906 ([_LINENUM_FLAG, _defvar('TESTVAR', 1)],
1907 'A${TESTVAR}$B${TESTVAR + 1}$C',
1908 _linenum(0) + 'A1B2C'
1909 )
1910 ),
1911 ('exprsub_multi_line',
1912 ([_LINENUM_FLAG],
1913 '${"line1\\nline2"}$\nDone\n',
1914 _linenum(0) + 'line1\n' + _linenum(0) + 'line2\nDone\n'
1915 )
1916 ),
1917 ('macrosubs',
1918 ([_LINENUM_FLAG],
1919 '#:def macro(var)\nMACRO|${var}$|\n#:enddef\n${macro(1)}$',
1920 _linenum(0) + _linenum(3) + 'MACRO|1|'
1921 )
1922 ),
1923 ('recursive_macrosubs',
1924 ([_LINENUM_FLAG],
1925 '#:def macro(var)\nMACRO|${var}$|\n#:enddef\n${macro(macro(1))}$',
1926 _linenum(0) + _linenum(3) + 'MACRO|MACRO|1||'
1927 )
1928 ),
1929 ('macrosubs_multiline',
1930 ([_LINENUM_FLAG],
1931 '#:def macro(c)\nMACRO1|${c}$|\nMACRO2|${c}$|\n#:enddef\n${macro(\'A\')}$'
1932 '\n',
1933 _linenum(0) + _linenum(4) + 'MACRO1|A|\n' + _linenum(4)
1934 + 'MACRO2|A|\n'
1935 )
1936 ),
1937 ('recursive_macrosubs_multiline',
1938 ([_LINENUM_FLAG],
1939 '#:def f(c)\nLINE1|${c}$|\nLINE2|${c}$|\n#:enddef\n$: f(f("A"))\n',
1940 (_linenum(0) + _linenum(4) + 'LINE1|LINE1|A|\n' +
1941 _linenum(4) + 'LINE2|A||\n' + _linenum(4) + 'LINE2|LINE1|A|\n' +
1942 _linenum(4) + 'LINE2|A||\n')
1943 )
1944 ),
1945 ('multiline_macrocall',
1946 ([_LINENUM_FLAG],
1947 '#:def macro(c)\nMACRO|${c}$|\n#:enddef\n$: mac& \n &ro(\'A\')\nDone\n',
1948 _linenum(0) + _linenum(3) + 'MACRO|A|\n' + _linenum(5)
1949 + 'Done\n'
1950 )
1951 ),
1952 ('call_directive_2_args',
1953 ([_LINENUM_FLAG],
1954 '#:def mymacro(val1, val2)\n|${val1}$|${val2}$|\n#:enddef\n'\
1955 '#:call mymacro\nL1\nL2\n#:nextarg\nL3\n#:endcall\n',
1956 _linenum(0) + _linenum(3) + '|L1\n' + _linenum(3)
1957 + 'L2|L3|\n' + _linenum(9),
1958 )
1959 ),
1960 ('for',
1961 ([_LINENUM_FLAG],
1962 '#:for i in (1, 2)\n${i}$\n#:endfor\nDone\n',
1963 (_linenum(0) + _linenum(1) + '1\n' + _linenum(1) + '2\n'
1964 + _linenum(3) + 'Done\n')
1965 )
1966 ),
1967 ('inline_for',
1968 ([_LINENUM_FLAG],
1969 '#{for i in (1, 2)}#${i}$#{endfor}#Done\n',
1970 _linenum(0) + '12Done\n'
1971 )
1972 ),
1973 ('set',
1974 ([_LINENUM_FLAG],
1975 '#:set x = 2\n$: x\n',
1976 _linenum(0) + _linenum(1) + '2\n',
1977 )
1978 ),
1979 ('inline_set',
1980 ([_LINENUM_FLAG],
1981 '#{set x = 2}#${x}$Done\n',
1982 _linenum(0) + '2Done\n',
1983 )
1984 ),
1985 ('comment_single',
1986 ([_LINENUM_FLAG],
1987 ' #! Comment here\nDone\n',
1988 _linenum(0) + _linenum(1) + 'Done\n'
1989 )
1990 ),
1991 ('comment_multiple',
1992 ([_LINENUM_FLAG],
1993 ' #! Comment1\n#! Comment2\nDone\n',
1994 _linenum(0) + _linenum(2) + 'Done\n',
1995 )
1996 ),
1997 ('mute',
1998 ([_LINENUM_FLAG],
1999 'A\n#:mute\nB\n#:set VAR = 2\n#:endmute\nVAR=${VAR}$\n',
2000 _linenum(0) + 'A\n' + _linenum(5) + 'VAR=2\n'
2001 )
2002 ),
2003 ('direct_call',
2004 ([_LINENUM_FLAG],
2005 '#:def mymacro(val)\n|${val}$|\n#:enddef\n'\
2006 '@:mymacro( a < b )\n',
2007 _linenum(0) + _linenum(3) + '|a < b|\n',
2008 )
2009 ),
2010 ('direct_call_contline',
2011 ([_LINENUM_FLAG],
2012 '#:def mymacro(val)\n|${val}$|\n#:enddef\n'\
2013 '@:mymacro(a &\n &< b&\n &)\nDone\n',
2014 _linenum(0) + _linenum(3) + '|a < b|\n' + _linenum(6)
2015 + 'Done\n',
2016 )
2017 ),
2018 ('assert_directive',
2019 ([_LINENUM_FLAG],
2020 '#:assert 1 < 2\nDone\n',
2021 _linenum(0) + _linenum(1) + 'Done\n',
2022 )
2023 ),
2024 ('assert_directive_contline',
2025 ([_LINENUM_FLAG],
2026 '#:assert 1&\n& < 2\nDone\n',
2027 _linenum(0) + _linenum(2) + 'Done\n',
2028 )
2029 ),
2030 ('smart_folding',
2031 ([_LINENUM_FLAG, _linelen(15), _indentation(4), _folding('smart')],
2032 ' ${3}$456 89 123456 8\nDone\n',
2033 _linenum(0) + ' 3456 89&\n' + _linenum(0)
2034 + ' & 123456&\n' + _linenum(0) + ' & 8\n' + 'Done\n'
2035 )
2036 ),
2037 ('smart_folding_nocontlines',
2038 ([_LINENUM_FLAG, _linenumbering('nocontlines'), _linelen(15),
2039 _indentation(4), _folding('smart')],
2040 ' ${3}$456 89 123456 8\nDone\n',
2041 _linenum(0) + ' 3456 89&\n' + ' & 123456&\n' \
2042 + ' & 8\n' + _linenum(1) + 'Done\n'
2043 )
2044 ),
2045 ]
2046
2047
2048 # Tests with include files
2049 #
2050 # Each test consists of a tuple containing the test name and a tuple with the
2051 # arguments of the get_test_output_method() routine.
2052 #
2053 INCLUDE_TESTS = [
2054 ('explicit_include',
2055 ([],
2056 '#:include "include/fypp1.inc"\n',
2057 'INCL1\nINCL5\n'
2058 )
2059 ),
2060 ('search_include',
2061 ([_incdir('include')],
2062 '#:include "fypp1.inc"\n',
2063 'INCL1\nINCL5\n'
2064 )
2065 ),
2066 ('nested_include_in_incpath',
2067 ([_incdir('include')],
2068 '#:include "subfolder/include_fypp1.inc"\n',
2069 'INCL1\nINCL5\n'
2070 )
2071 ),
2072 ('nested_include_in_folder_of_incfile',
2073 ([_incdir('include')],
2074 '#:include "subfolder/include_fypp2.inc"\n',
2075 'FYPP2\n'
2076 )
2077 ),
2078 ('search_include_linenum',
2079 ([_LINENUM_FLAG, _incdir('include')],
2080 '#:include "fypp1.inc"\n$: incmacro(1)\n',
2081 (_linenum(0)
2082 + _linenum(0, 'include/fypp1.inc', flag=_NEW_FILE)
2083 + 'INCL1\n' + _linenum(4, 'include/fypp1.inc')
2084 + 'INCL5\n' + _linenum(1, flag=_RETURN_TO_FILE) + 'INCMACRO(1)\n')
2085 )
2086 ),
2087 ('nested_include_in_incpath_linenum',
2088 ([_LINENUM_FLAG, _incdir('include')],
2089 '#:include "subfolder/include_fypp1.inc"\n',
2090 (_linenum(0)
2091 + _linenum(0, 'include/subfolder/include_fypp1.inc', flag=_NEW_FILE)
2092 + _linenum(0, 'include/fypp1.inc', flag=_NEW_FILE) + 'INCL1\n'
2093 + _linenum(4, 'include/fypp1.inc') + 'INCL5\n'
2094 + _linenum(1, 'include/subfolder/include_fypp1.inc',
2095 flag=_RETURN_TO_FILE)
2096 + _linenum(1, flag=_RETURN_TO_FILE))
2097 )
2098 ),
2099 ('nested_include_in_folder_of_incfile2',
2100 ([_LINENUM_FLAG, _incdir('include')],
2101 '#:include "subfolder/include_fypp2.inc"\n',
2102 (_linenum(0)
2103 + _linenum(0, 'include/subfolder/include_fypp2.inc', flag=_NEW_FILE)
2104 + _linenum(0, 'include/subfolder/fypp2.inc', flag=_NEW_FILE)
2105 + 'FYPP2\n'
2106 + _linenum(1, 'include/subfolder/include_fypp2.inc',
2107 flag=_RETURN_TO_FILE)
2108 + _linenum(1, flag=_RETURN_TO_FILE))
2109 )
2110 ),
2111 ('muted_include',
2112 ([_incdir('include')],
2113 'START\n#:mute\n#:include \'fypp1.inc\'\n#:endmute\nDONE\n',
2114 'START\nDONE\n'
2115 )
2116 ),
2117 ('muted_include_linenum',
2118 ([_LINENUM_FLAG, _incdir('include')],
2119 'START\n#:mute\n#:include \'fypp1.inc\'\n#:endmute\nDONE\n',
2120 _linenum(0) + 'START\n' + _linenum(4) + 'DONE\n'
2121 )
2122 ),
2123 ]
2124
2125
2126 # Tests which needs actual files as input
2127 #
2128 # Each test consists of a tuple containing the test name and a tuple with the
2129 # arguments of the get_test_output_from_file_input_method() routine.
2130 #
2131 INPUT_FILE_TESTS = [
2132 ('file_var_substitution',
2133 ([],
2134 "input/filevarroot.fypp",
2135 'FILE: input/filevarroot.fypp:1\n'
2136 'THIS_FILE: input/filevarroot.fypp:2\n'
2137 '---\n'
2138 'FILE: input/filevarroot.fypp:5\n'
2139 'THIS_FILE: input/filevarroot.inc:3\n'
2140 )
2141 ),
2142 ('file_var_root_rel',
2143 (["--file-var-root=input"],
2144 "input/filevarroot.fypp",
2145 'FILE: filevarroot.fypp:1\n'
2146 'THIS_FILE: filevarroot.fypp:2\n'
2147 '---\n'
2148 'FILE: filevarroot.fypp:5\n'
2149 'THIS_FILE: filevarroot.inc:3\n'
2150 )
2151 ),
2152 ('file_var_root_abs',
2153 ([f"--file-var-root={Path.cwd()}"],
2154 f"{Path.cwd() / 'input/filevarroot.fypp'}",
2155 'FILE: input/filevarroot.fypp:1\n'
2156 'THIS_FILE: input/filevarroot.fypp:2\n'
2157 '---\n'
2158 'FILE: input/filevarroot.fypp:5\n'
2159 'THIS_FILE: input/filevarroot.inc:3\n'
2160 )
2161 ),
2162 ]
2163
2164
2165 # Tests triggering exceptions
2166 #
2167 # Each test consists of a tuple containing the test name and a tuple with the
2168 # arguments of the get_test_exception_method() routine.
2169 #
2170 EXCEPTION_TESTS = [
2171 #
2172 # Parser errors
2173 #
2174 ('invalid_directive',
2175 ([],
2176 '#:invalid\n',
2177 [(fypp.FyppFatalError, fypp.STRING, (0, 1))]
2178 )
2179 ),
2180 ('invalid_macrodef',
2181 ([],
2182 '#:def alma[x]\n#:enddef\n',
2183 [(fypp.FyppFatalError, fypp.STRING, (0, 1))]
2184 )
2185 ),
2186 ('invalid_for_decl',
2187 ([],
2188 '#:for i = 1, 2\n',
2189 [(fypp.FyppFatalError, fypp.STRING, (0, 1))]
2190 )
2191 ),
2192 ('invalid_include',
2193 ([],
2194 '#:include <test.h>\n',
2195 [(fypp.FyppFatalError, fypp.STRING, (0, 1))]
2196 )
2197 ),
2198 ('inline_include',
2199 ([],
2200 '#{include "test.h"}#\n',
2201 [(fypp.FyppFatalError, fypp.STRING, (0, 0))]
2202 )
2203 ),
2204 ('wrong_include_file',
2205 ([],
2206 '#:include "testfkjsdlfkjslf.h"\n',
2207 [(fypp.FyppFatalError, fypp.STRING, (0, 1))]
2208 )
2209 ),
2210 ('invalid_else',
2211 ([],
2212 '#:if 1 > 2\nA\n#:else True\nB\n#:endif\n',
2213 [(fypp.FyppFatalError, fypp.STRING, (2, 3))]
2214 )
2215 ),
2216 ('invalid_endif',
2217 ([],
2218 '#:if 1 > 2\nA\n#:else\nB\n#:endif INV\n',
2219 [(fypp.FyppFatalError, fypp.STRING, (4, 5))]
2220 )
2221 ),
2222 ('invalid_endfor',
2223 ([],
2224 '#:for i in range(5)\n${i}$\n#:endfor INV\n',
2225 [(fypp.FyppFatalError, fypp.STRING, (2, 3))]
2226 )
2227 ),
2228 ('invalid_variable_assign',
2229 ([],
2230 '#:set A=\n',
2231 [(fypp.FyppFatalError, fypp.STRING, (0, 1))]
2232 )
2233 ),
2234 ('invalid_mute',
2235 ([],
2236 '#:mute TEST\n#:endmute\n',
2237 [(fypp.FyppFatalError, fypp.STRING, (0, 1))]
2238 )
2239 ),
2240 ('invalid_endmute',
2241 ([],
2242 '#:mute\n#:endmute INVALID\n',
2243 [(fypp.FyppFatalError, fypp.STRING, (1, 2))]
2244 )
2245 ),
2246 ('inline_mute',
2247 ([],
2248 '#{mute}#test#{endmute}#\n',
2249 [(fypp.FyppFatalError, fypp.STRING, (0, 0))]
2250 )
2251 ),
2252 ('inline_endmute',
2253 ([],
2254 '#:mute\ntest#{endmute}#\n',
2255 [(fypp.FyppFatalError, fypp.STRING, (1, 1))]
2256 )
2257 ),
2258 ('setvar_with_equal',
2259 ([],
2260 '#:setvar x = 2\n$: x\n',
2261 [(fypp.FyppFatalError, fypp.STRING, (0, 1))]
2262 )
2263 ),
2264 ('inline_set_without_equal',
2265 ([],
2266 '#{set x 2}#${x}$Done\n',
2267 [(fypp.FyppFatalError, fypp.STRING, (0, 0))]
2268 )
2269 ),
2270 ('missing_del_name',
2271 ([],
2272 '#:del\n',
2273 [(fypp.FyppFatalError, fypp.STRING, (0, 1))]
2274 )
2275 ),
2276 ('invalid_del_name',
2277 ([],
2278 '#:del [a, b]\n',
2279 [(fypp.FyppFatalError, fypp.STRING, (0, 1))]
2280 )
2281 ),
2282 ('inline_def',
2283 ([],
2284 '#{def macro()}#TEST#{enddef}#',
2285 [(fypp.FyppFatalError, fypp.STRING, (0, 0))]
2286 )
2287 ),
2288 ('invalid_direct_call_expr',
2289 ([],
2290 '#:def macro()\n#:enddef\n@:macro{}\n',
2291 [(fypp.FyppFatalError, fypp.STRING, (2, 3))]
2292 )
2293 ),
2294 ('invalid_direct_call_expr_inline',
2295 ([],
2296 '#:def macro()\n#:enddef\n@{macro{}}@\n',
2297 [(fypp.FyppFatalError, fypp.STRING, (2, 2))]
2298 )
2299 ),
2300 ('invalid_direct_call_expr2',
2301 ([],
2302 '#:def macro()\n#:enddef\n@:macro(\n',
2303 [(fypp.FyppFatalError, fypp.STRING, (2, 3))]
2304 )
2305 ),
2306 ('invalid_direct_call_expr2_inline',
2307 ([],
2308 '#:def macro()\n#:enddef\n@{macro(}@\n',
2309 [(fypp.FyppFatalError, fypp.STRING, (2, 2))]
2310 )
2311 ),
2312 ('direct_call_non_eval_dir',
2313 ([],
2314 '#:def mymacro(val1, val2)\n|${val1}$|${val2}$|\n#:enddef\n'\
2315 '@:mymacro(L1 #{if True}#2, 2#{endif}#)\n',
2316 [(fypp.FyppFatalError, fypp.STRING, (3, 3))]
2317 )
2318 ),
2319 ('direct_call_non_eval_dir_inline',
2320 ([],
2321 '#:def mymacro(val1, val2)\n|${val1}$|${val2}$|\n#:enddef\n'\
2322 '@{mymacro(L1 #{if True}#2, 2#{endif}#)}@',
2323 [(fypp.FyppFatalError, fypp.STRING, (3, 3))]
2324 )
2325 ),
2326 ('direct_call_unclosed quote',
2327 ([],
2328 '#:def mymacro(arg1)\n|${arg1}$|\n#:enddef\n'\
2329 '@:mymacro("something)\n',
2330 [(fypp.FyppFatalError, fypp.STRING, (3, 4)),
2331 (fypp.FyppFatalError, None, None)]
2332 )
2333 ),
2334 ('direct_call_unclosed bracket',
2335 ([],
2336 '#:def mymacro(arg1)\n|${arg1}$|\n#:enddef\n'\
2337 '@:mymacro({something)\n',
2338 [(fypp.FyppFatalError, fypp.STRING, (3, 4)),
2339 (fypp.FyppFatalError, None, None)]
2340 )
2341 ),
2342 ('direct_call_unbalanced bracket',
2343 ([],
2344 '#:def mymacro(arg1)\n|${arg1}$|\n#:enddef\n'\
2345 '@:mymacro({(})\n',
2346 [(fypp.FyppFatalError, fypp.STRING, (3, 4)),
2347 (fypp.FyppFatalError, None, None)]
2348 )
2349 ),
2350 ('missing_line_dir_content',
2351 ([],
2352 '#:\n',
2353 [(fypp.FyppFatalError, fypp.STRING, (0, 1))]
2354 )
2355 ),
2356 ('missing_line_dir_content2',
2357 ([],
2358 '#: \n',
2359 [(fypp.FyppFatalError, fypp.STRING, (0, 1))]
2360 )
2361 ),
2362 ('missing_inline_dir_content',
2363 ([],
2364 '#{}#',
2365 [(fypp.FyppFatalError, fypp.STRING, (0, 0))]
2366 )
2367 ),
2368 ('missing_inline_dir_content2',
2369 ([],
2370 '#{ }#',
2371 [(fypp.FyppFatalError, fypp.STRING, (0, 0))]
2372 )
2373 ),
2374 ('set_setvar',
2375 ([],
2376 '#:setvar x 2\n',
2377 [(fypp.FyppFatalError, fypp.STRING, (0, 1))]
2378 )
2379 ),
2380 ('inline_setvar',
2381 ([],
2382 '#{setvar x 2}#',
2383 [(fypp.FyppFatalError, fypp.STRING, (0, 0))]
2384 )
2385 ),
2386 #
2387 # Builder errors
2388 #
2389 ('line_if_inline_endif',
2390 ([],
2391 '#:if 1 < 2\nTrue\n#{endif}#\n',
2392 [(fypp.FyppFatalError, fypp.STRING, (2, 2))]
2393 )
2394 ),
2395 ('inline_if_line_endif',
2396 ([],
2397 '#{if 1 < 2}#True\n#:endif\n',
2398 [(fypp.FyppFatalError, fypp.STRING, (1, 2))]
2399 )
2400 ),
2401 ('line_if_inline_elif',
2402 ([],
2403 '#:if 1 < 2\nTrue\n#{elif 2 > 3}#\n',
2404 [(fypp.FyppFatalError, fypp.STRING, (2, 2))]
2405 )
2406 ),
2407 ('inline_if_line_elif',
2408 ([],
2409 '#{if 1 < 2}#True\n#:elif 2 > 3\n',
2410 [(fypp.FyppFatalError, fypp.STRING, (1, 2))]
2411 )
2412 ),
2413 ('line_if_inline_else',
2414 ([],
2415 '#:if 1 < 2\nTrue\n#{else}#\n',
2416 [(fypp.FyppFatalError, fypp.STRING, (2, 2))]
2417 )
2418 ),
2419 ('inline_if_line_else',
2420 ([],
2421 '#{if 1 < 2}#True\n#:else\n',
2422 [(fypp.FyppFatalError, fypp.STRING, (1, 2))]
2423 )
2424 ),
2425 ('loose_else',
2426 ([],
2427 'A\n#:else\n',
2428 [(fypp.FyppFatalError, fypp.STRING, (1, 2))]
2429 )
2430 ),
2431 ('loose_inline_else',
2432 ([],
2433 'A\n#{else}#\n',
2434 [(fypp.FyppFatalError, fypp.STRING, (1, 1))]
2435 )
2436 ),
2437 ('loose_elif',
2438 ([],
2439 'A\n#:elif 1 > 2\n',
2440 [(fypp.FyppFatalError, fypp.STRING, (1, 2))]
2441 )
2442 ),
2443 ('loose_inline_elif',
2444 ([],
2445 'A\n#{elif 1 > 2}#\n',
2446 [(fypp.FyppFatalError, fypp.STRING, (1, 1))]
2447 )
2448 ),
2449 ('loose_endif',
2450 ([],
2451 'A\n#:endif\n',
2452 [(fypp.FyppFatalError, fypp.STRING, (1, 2))]
2453 )
2454 ),
2455 ('loose_inline_endif',
2456 ([],
2457 'A\n#{endif}#\n',
2458 [(fypp.FyppFatalError, fypp.STRING, (1, 1))]
2459 )
2460 ),
2461 ('mismatching_else',
2462 ([],
2463 '#:if 1 < 2\n#:for i in range(3)\n#:else\n',
2464 [(fypp.FyppFatalError, fypp.STRING, (2, 3))]
2465 )
2466 ),
2467 ('mismatching_elif',
2468 ([],
2469 '#:if 1 < 2\n#:for i in range(3)\n#:elif 1 > 2\n',
2470 [(fypp.FyppFatalError, fypp.STRING, (2, 3))]
2471 )
2472 ),
2473 ('mismatching_endif',
2474 ([],
2475 '#:if 1 < 2\n#:for i in range(3)\n#:endif\n',
2476 [(fypp.FyppFatalError, fypp.STRING, (2, 3))]
2477 )
2478 ),
2479 ('line_def_inline_enddef',
2480 ([],
2481 '#:def alma(x)\n#{enddef}#\n',
2482 [(fypp.FyppFatalError, fypp.STRING, (1, 1))]
2483 )
2484 ),
2485 ('loose_enddef',
2486 ([],
2487 '#:enddef\n',
2488 [(fypp.FyppFatalError, fypp.STRING, (0, 1))]
2489 )
2490 ),
2491 ('loose_inline_enddef',
2492 ([],
2493 '#{enddef}#\n',
2494 [(fypp.FyppFatalError, fypp.STRING, (0, 0))]
2495 )
2496 ),
2497 ('mismatching_enddef',
2498 ([],
2499 '#:def test(x)\n#{if 1 < 2}#\n#:enddef\n',
2500 [(fypp.FyppFatalError, fypp.STRING, (2, 3))]
2501 )
2502 ),
2503 ('enddef_name_mismatch',
2504 ([],
2505 '#:def macro(var)\nMACRO|${var}$|\n#:enddef nonsense\n${macro(1)}$',
2506 [(fypp.FyppFatalError, fypp.STRING, (2, 3))]
2507 )
2508 ),
2509 ('endcall_name_mismatch',
2510 ([],
2511 '#:def macro(var)\nMACRO|${var}$|\n#:enddef\n'\
2512 '#:call macro\n1\n#:endcall nonsense\n',
2513 [(fypp.FyppFatalError, fypp.STRING, (5, 6))]
2514 )
2515 ),
2516 ('inline_endcall_name_mismatch',
2517 ([],
2518 '#:def macro(var)\nMACRO|${var}$|\n#:enddef\n'\
2519 '#{call macro}#1#{endcall nonsense}#',
2520 [(fypp.FyppFatalError, fypp.STRING, (3, 3))]
2521 )
2522 ),
2523 ('line_for_inline_endfor',
2524 ([],
2525 '#:for i in range(3)\nA\n#{endfor}#\n',
2526 [(fypp.FyppFatalError, fypp.STRING, (2, 2))]
2527 )
2528 ),
2529 ('inline_for_line_endfor',
2530 ([],
2531 '#{for i in range(3)}#Empty\n#:endfor\n',
2532 [(fypp.FyppFatalError, fypp.STRING, (1, 2))]
2533 )
2534 ),
2535 ('loose_endfor',
2536 ([],
2537 '#:endfor\n',
2538 [(fypp.FyppFatalError, fypp.STRING, (0, 1))]
2539 )
2540 ),
2541 ('loose_inline_endfor',
2542 ([],
2543 '#{endfor}#',
2544 [(fypp.FyppFatalError, fypp.STRING, (0, 0))]
2545 )
2546 ),
2547 ('mismatching_endfor',
2548 ([],
2549 '#:for i in range(3)\n#{if 1 < 2}#\n#:endfor\n',
2550 [(fypp.FyppFatalError, fypp.STRING, (2, 3))]
2551 )
2552 ),
2553 ('loose_endmute',
2554 ([],
2555 '#:endmute\n',
2556 [(fypp.FyppFatalError, fypp.STRING, (0, 1))]
2557 )
2558 ),
2559 ('mismatching_endmute',
2560 ([],
2561 '#:mute\n#{if 1 < 2}#\n#:endmute\n',
2562 [(fypp.FyppFatalError, fypp.STRING, (2, 3))]
2563 )
2564 ),
2565 ('unclosed_directive',
2566 ([],
2567 '#:if 1 > 2\nA\n',
2568 [(fypp.FyppFatalError, fypp.STRING, (0, 1))]
2569 )
2570 ),
2571 ('missing_space_after_directive',
2572 ([],
2573 '#:if(1 > 2)\nA\n#:endif',
2574 [(fypp.FyppFatalError, fypp.STRING, (0, 1))]
2575 )
2576 ),
2577 ('missing_space_after_inline_directive',
2578 ([],
2579 '#{if(1 > 2)}#A#{endif}#',
2580 [(fypp.FyppFatalError, fypp.STRING, (0, 0))]
2581 )
2582 ),
2583 ('mixing_block_and_endcall',
2584 ([],
2585 '#:def test(x)\n#:enddef\n#:block test\n1\n#:endcall\n',
2586 [(fypp.FyppFatalError, fypp.STRING, (4, 5))]
2587 )
2588 ),
2589 ('mixing_call_and_endblock',
2590 ([],
2591 '#:def test(x)\n#:enddef\n#:call test\n1\n#:endblock\n',
2592 [(fypp.FyppFatalError, fypp.STRING, (4, 5))]
2593 )
2594 ),
2595 ('mixing_call_and_contains',
2596 ([],
2597 '#:def test(x,y)\n#:enddef\n#:call test\n1\n#:contains\n2\n#:endcall\n',
2598 [(fypp.FyppFatalError, fypp.STRING, (4, 5))]
2599 )
2600 ),
2601 ('mixing_block_and_nextarg',
2602 ([],
2603 '#:def test(x,y)\n#:enddef\n#:block test\n1\n#:nextarg\n2\n#:endblock\n',
2604 [(fypp.FyppFatalError, fypp.STRING, (4, 5))]
2605 )
2606 ),
2607 #
2608 # Renderer errors
2609 #
2610 ('invalid_expression',
2611 ([],
2612 '${i}$',
2613 [(fypp.FyppFatalError, fypp.STRING, (0, 0))]
2614 )
2615 ),
2616 ('invalid_variable',
2617 ([],
2618 '#:set i 1.2.3\n',
2619 [(fypp.FyppFatalError, fypp.STRING, (0, 1))]
2620 )
2621 ),
2622 ('invalid_condition',
2623 ([],
2624 '#{if i >>> 3}##{endif}#',
2625 [(fypp.FyppFatalError, fypp.STRING, (0, 0))]
2626 )
2627 ),
2628 ('invalid_iterator',
2629 ([],
2630 '#:for i in 1.2.3\nDummy\n#:endfor\n',
2631 [(fypp.FyppFatalError, fypp.STRING, (0, 1))]
2632 )
2633 ),
2634 ('invalid_macro_argument_expression',
2635 ([],
2636 '#:def alma(x))\n#:enddef\n',
2637 [(fypp.FyppFatalError, fypp.STRING, (0, 1))]
2638 )
2639 ),
2640 ('tuple_macro_argument',
2641 ([],
2642 '#:def alma((x, y))\n#:enddef\n',
2643 [(fypp.FyppFatalError, fypp.STRING, (0, 1))],
2644 ),
2645 ),
2646 ('repeated_keyword_argument',
2647 ([],
2648 '#:def mymacro(A, B)\nA=${A}$,B=${B}$\n#:enddef mymacro\n'\
2649 '$:mymacro(A=1, A=2, B=3)\n',
2650 [(fypp.FyppFatalError, fypp.STRING, (3, 4))]
2651 )
2652 ),
2653 ('pos_arg_after_keyword_arg',
2654 ([],
2655 '#:def mymacro(A, B)\nA=${A}$,B=${B}$\n#:enddef mymacro\n'\
2656 '$:mymacro(B=4, 2)\n',
2657 [(fypp.FyppFatalError, fypp.STRING, (3, 4))]
2658 )
2659 ),
2660 ('macrodef_pos_arg_after_keyword_arg',
2661 ([],
2662 '#:def mymacro(A, B=2, C)\nA=${A}$,B=${B},C=${C}$$\n#:enddef mymacro\n',
2663 [(fypp.FyppFatalError, fypp.STRING, (0, 1))]
2664 )
2665 ),
2666 ('macrodef_pos_arg_after_var_arg',
2667 ([],
2668 '#:def mymacro(A, *B, C)\n#:enddef\n',
2669 [(fypp.FyppFatalError, fypp.STRING, (0, 1)),
2670 (fypp.FyppFatalError, None, None)]
2671 ),
2672 ),
2673 ('macrodef_pos_arg_after_var_kwarg',
2674 ([],
2675 '#:def mymacro(A, **B, C)\n#:enddef\n',
2676 [(fypp.FyppFatalError, fypp.STRING, (0, 1))]
2677 )
2678 ),
2679 ('invalid_macro_prefix',
2680 ([],
2681 '#:def __test(x)\n#:enddef\n',
2682 [(fypp.FyppFatalError, fypp.STRING, (0, 1)),
2683 (fypp.FyppFatalError, None, None)]
2684 )
2685 ),
2686 ('reserved_macro_name',
2687 ([],
2688 '#:def defined(x)\n#:enddef\n',
2689 [(fypp.FyppFatalError, fypp.STRING, (0, 1)),
2690 (fypp.FyppFatalError, None, None)]
2691 )
2692 ),
2693 ('macro_double_defined_arg',
2694 ([],
2695 '#:def macro(x, y, *vararg)\n|${x}$${y}$${vararg}$|\n#:enddef\n'\
2696 '$:macro(1, 2, x=1)\n',
2697 [(fypp.FyppFatalError, fypp.STRING, (3, 4)),
2698 (fypp.FyppFatalError, fypp.STRING, (0, 1))]
2699 )
2700 ),
2701 ('macro_invalid_argument_name',
2702 ([],
2703 '#:def macro(x, __y, *vararg)\n#:enddef\n',
2704 [(fypp.FyppFatalError, fypp.STRING, (0, 1))]
2705 )
2706 ),
2707 ('macro_invalid_varargument_name',
2708 ([],
2709 '#:def macro(x, y, *__vararg)\n#:enddef\n',
2710 [(fypp.FyppFatalError, fypp.STRING, (0, 1))]
2711 )
2712 ),
2713 ('invalid_variable_prefix',
2714 ([],
2715 '#:set __test = 2\n',
2716 [(fypp.FyppFatalError, fypp.STRING, (0, 1)),
2717 (fypp.FyppFatalError, None, None)]
2718 )
2719 ),
2720 ('reserved_variable_name',
2721 ([],
2722 '#:set _LINE_ = 2\n',
2723 [(fypp.FyppFatalError, fypp.STRING, (0, 1)),
2724 (fypp.FyppFatalError, None, None)]
2725 )
2726 ),
2727 ('macro_call_more_args',
2728 ([],
2729 '#:def test(x)\n${x}$\n#:enddef\n$: test(\'A\', 1)\n',
2730 [(fypp.FyppFatalError, fypp.STRING, (3, 4)),
2731 (fypp.FyppFatalError, fypp.STRING, (0, 1))]
2732 )
2733 ),
2734 ('macro_call_less_args',
2735 ([],
2736 '#:def test(x)\n${x}$\n#:enddef\n$: test()\n',
2737 [(fypp.FyppFatalError, fypp.STRING, (3, 4)),
2738 (fypp.FyppFatalError, fypp.STRING, (0, 1))]
2739 )
2740 ),
2741 ('macro_invalid_keyword_arguments',
2742 ([],
2743 '#:def macro(x, y)\n|${x}$${y}$|\n#:enddef\n'\
2744 '$:macro(1, 2, z=3)\n',
2745 [(fypp.FyppFatalError, fypp.STRING, (3, 4)),
2746 (fypp.FyppFatalError, fypp.STRING, (0, 1))]
2747 )
2748 ),
2749 ('macro_vararg_invalid_keyword_arguments',
2750 ([],
2751 '#:def macro(x, y, *vararg)\n|${x}$${y}$${z}$${vararg}$|\n#:enddef\n'\
2752 '$:macro(1, 2, z=3)\n',
2753 [(fypp.FyppFatalError, fypp.STRING, (3, 4)),
2754 (fypp.FyppFatalError, fypp.STRING, (0, 1))]
2755 )
2756 ),
2757 ('macro_kwarg_invalid_posarg',
2758 ([],
2759 '#:def macro(x, y, **varkw)\n|${x}$${y}$${varkw}$|\n#:enddef\n'\
2760 '$:macro(1, 2, 3, z=3)\n',
2761 [(fypp.FyppFatalError, fypp.STRING, (3, 4)),
2762 (fypp.FyppFatalError, fypp.STRING, (0, 1))]
2763 )
2764 ),
2765 ('short_line_length',
2766 ([_linelen(4)],
2767 '',
2768 [(fypp.FyppFatalError, None, None)]
2769 )
2770 ),
2771 ('failing_macro_in_include',
2772 ([],
2773 '#:include "include/failingmacro.inc"\n$:failingmacro()\n',
2774 [(fypp.FyppFatalError, fypp.STRING, (1, 2)),
2775 (fypp.FyppFatalError, 'include/failingmacro.inc', (2, 3))]
2776 )
2777 ),
2778 ('incompatible_tuple_assignment1',
2779 ([],
2780 '#:set a,b,c = (1, 2)\n${a}$${b}$${c}$\n',
2781 [(fypp.FyppFatalError, fypp.STRING, (0, 1)),
2782 (fypp.FyppFatalError, None, None)]
2783 )
2784 ),
2785 ('incompatible_tuple_assignment2',
2786 ([],
2787 '#:set a,b,c = (1, 2, 3, 4)\n${a}$${b}$${c}$\n',
2788 [(fypp.FyppFatalError, fypp.STRING, (0, 1)),
2789 (fypp.FyppFatalError, None, None)]
2790 )
2791 ),
2792 ('invalid_lhs_tuple1',
2793 ([],
2794 '#:set (a, b = (1, 2)\n',
2795 [(fypp.FyppFatalError, fypp.STRING, (0, 1)),
2796 (fypp.FyppFatalError, None, None)]
2797 )
2798 ),
2799 ('invalid_lhs_tuple2',
2800 ([],
2801 '#:set a, b) = (1, 2)\n',
2802 [(fypp.FyppFatalError, fypp.STRING, (0, 1)),
2803 (fypp.FyppFatalError, None, None)]
2804 )
2805 ),
2806 ('invalid_del_tuple1',
2807 ([],
2808 '#:del (a, b\n',
2809 [(fypp.FyppFatalError, fypp.STRING, (0, 1)),
2810 (fypp.FyppFatalError, None, None)]
2811 )
2812 ),
2813 ('invalid_del_tuple2',
2814 ([],
2815 '#:del a, b)\n',
2816 [(fypp.FyppFatalError, fypp.STRING, (0, 1)),
2817 (fypp.FyppFatalError, None, None)]
2818 )
2819 ),
2820 ('del_nonexisting_variable',
2821 ([],
2822 '#:del X\n',
2823 [(fypp.FyppFatalError, fypp.STRING, (0, 1)),
2824 (fypp.FyppFatalError, None, None)]
2825 )
2826 ),
2827 ('local_macro_visibility',
2828 ([],
2829 '#:set echo = lambda s: s\n'\
2830 '#:call echo\n'
2831 '#:def mymacro()\nX\n#:enddef\n'\
2832 '#:endcall\n$:mymacro()\n',
2833 [(fypp.FyppFatalError, fypp.STRING, (6, 7))]
2834 )
2835 ),
2836 #
2837 # Command line errors
2838 #
2839 ('def_error',
2840 (['-DVAR=1.2.2'],
2841 '',
2842 [(fypp.FyppFatalError, None, None)]
2843 )
2844 ),
2845 ('missing_module',
2846 (['-mWhateverDummyKJFDKf'],
2847 '',
2848 [(fypp.FyppFatalError, None, None)]
2849 )
2850 ),
2851 #
2852 # User requested stop
2853 #
2854 ('userstop',
2855 ([],
2856 '#:set A = 12\n#:if A > 10\n#:stop "Wrong A: {0}".format(A)\n#:endif\n',
2857 [(fypp.FyppStopRequest, fypp.STRING, (2, 3))]
2858 )
2859 ),
2860 ('invalid_userstop_expr',
2861 ([],
2862 '#:set A = 12\n#:if A > 10\n#:stop "Wrong A: {0}".format(BA)\n#:endif\n',
2863 [(fypp.FyppFatalError, fypp.STRING, (2, 3))]
2864 )
2865 ),
2866 ('invalid_inline_stop',
2867 ([],
2868 '#:set A = 1\n#:if A > 10\n#{stop "Wrong A: {0}".format(BA)}#\n#:endif\n',
2869 [(fypp.FyppFatalError, fypp.STRING, (2, 2))]
2870 )
2871 ),
2872 ('assert',
2873 ([],
2874 '#:set A = 12\n#:assert A < 10\n',
2875 [(fypp.FyppStopRequest, fypp.STRING, (1, 2))]
2876 )
2877 ),
2878 ('invalid_assert_expr',
2879 ([],
2880 '#:assert A < 10\n',
2881 [(fypp.FyppFatalError, fypp.STRING, (0, 1))]
2882 )
2883 ),
2884 ('invalid_inline_assert',
2885 ([],
2886 '#:set A = 12\n#{assert A < 10}#\n',
2887 [(fypp.FyppFatalError, fypp.STRING, (1, 1))]
2888 )
2889 ),
2890 ('global_existing_in_local_scope',
2891 ([],
2892 '#:def macro()\n#:set A = 12\n#:global A\n#:enddef\n$:macro()\n',
2893 [(fypp.FyppFatalError, fypp.STRING, (4, 5)),
2894 (fypp.FyppFatalError, fypp.STRING, (2, 3)),
2895 (fypp.FyppFatalError, None, None)]
2896 )
2897 ),
2898 ('setvar_func_odd_arguments',
2899 ([],
2900 '$:setvar("i", 1, "j")\n',
2901 [(fypp.FyppFatalError, fypp.STRING, (0, 1)),
2902 (fypp.FyppFatalError, None, None)]
2903 )
2904 ),
2905 ]
2906
2907
2908 # Tests with module imports
2909 #
2910 # Each test consists of a tuple containing the test name and a tuple with the
2911 # arguments of the get_test_output_method() routine.
2912 #
2913 # NOTE: imports are global in Python, so all instances of Fypp following after
2914 # the tests below will see the imported modules Therefore, this tests should be
2915 # executed as last to minimize unwanted interactions between unit tests. Also,
2916 # no tests before these should import any modules.
2917 #
2918 IMPORT_TESTS = [
2919 ('import_module',
2920 ([_importmodule('math')],
2921 '$:int(math.sqrt(4))\n',
2922 '2\n'
2923 )
2924 ),
2925 ('import_module_current_dir',
2926 ([_importmodule('inimod')],
2927 '${inimod.get_version()}$',
2928 '1'
2929 )
2930 ),
2931 ('import_module_modified_lookupdir',
2932 ([_moddir('include'), _importmodule('inimod2')],
2933 '${inimod2.get_version()}$',
2934 '2'
2935 )
2936 ),
2937 ('import_subpackage',
2938 ([_importmodule('os.path')],
2939 '${os.path.isabs("a")}$',
2940 'False'
2941 )
2942 ),
2943 ]
2944
2945
2946 def _get_test_output_method(args, inp, out):
2947 '''Returns a test method for checking correctness of Fypp output.
2948
2949 Args:
2950 args (list of str): Command-line arguments to pass to Fypp.
2951 inp (str): Input with Fypp directives.
2952 out (str): Expected output.
2953
2954 Returns:
2955 method: Method to test equality of output with result delivered by Fypp.
2956 '''
2957
2958 def test_output(self):
2959 '''Tests whether Fypp result matches expected output.'''
2960 optparser = fypp.get_option_parser()
2961 options, leftover = optparser.parse_args(args)
2962 self.assertEqual(len(leftover), 0)
2963 tool = fypp.Fypp(options)
2964 result = tool.process_text(inp)
2965 self.assertEqual(out, result)
2966 return test_output
2967
2968
2969 def _get_test_output_from_file_input_method(args, inputfile, out):
2970 '''Returns a test method for checking correctness of Fypp output.
2971
2972 Args:
2973 args (list of str): Command-line arguments to pass to Fypp.
2974 inputfile (str): Input file with Fypp directives.
2975 out (str): Expected output.
2976
2977 Returns:
2978 method: Method to test equality of output with result delivered by Fypp.
2979 '''
2980
2981 def test_output_from_file_input(self):
2982 '''Tests whether Fypp result matches expected output when input is in a file.'''
2983 optparser = fypp.get_option_parser()
2984 options, leftover = optparser.parse_args(args)
2985 self.assertEqual(len(leftover), 0)
2986 tool = fypp.Fypp(options)
2987 result = tool.process_file(inputfile)
2988 self.assertEqual(out, result)
2989 return test_output_from_file_input
2990
2991
2992
2993 def _get_test_exception_method(args, inp, exceptions):
2994 '''Returns a test method for checking correctness of thrown exception.
2995
2996 Args:
2997 args (list of str): Command-line arguments to pass to Fypp.
2998 inp (str): Input with Fypp directives.
2999 exceptions (list of tuples): Each tuple contains an exception, a file
3000 name and a span (tuple or int). The tuples should be in reverse
3001 order (latest raised exception first).
3002
3003 Returns:
3004 method: Method to test, whether Fypp throws the correct exception.
3005 '''
3006
3007 def test_exception(self):
3008 '''Tests whether Fypp throws the correct exception.'''
3009 optparser = fypp.get_option_parser()
3010 options, leftover = optparser.parse_args(args)
3011 self.assertEqual(len(leftover), 0)
3012 try:
3013 tool = fypp.Fypp(options)
3014 _ = tool.process_text(inp)
3015 except Exception as e:
3016 raised = e
3017 else:
3018 self.fail('No exception was raised')
3019 for exc, fname, span in exceptions:
3020 self.assertTrue(isinstance(raised, exc))
3021 if fname is None:
3022 self.assertTrue(raised.fname is None)
3023 else:
3024 self.assertEqual(fname, raised.fname)
3025 if span is None:
3026 self.assertTrue(raised.span is None)
3027 else:
3028 self.assertEqual(span, raised.span)
3029 raised = raised.__cause__
3030 self.assertTrue(not isinstance(raised, fypp.FyppError))
3031
3032 return test_exception
3033
3034
3035 def _test_needed(flag):
3036 return True
3037
3038
3039 class _TestContainer(unittest.TestCase):
3040 '''General test container class.'''
3041
3042 @classmethod
3043 def add_test_methods(cls, tests, methodfactory):
3044 '''Adds tests to a test case.
3045
3046 Args:
3047 tests (list of tuples): Tests to attach.
3048 testcase (TestCase): Class which the tests should be attached to.
3049 methodfactory (function): Functions which turns the tuples in
3050 tests into methods, which can be then attached to the test case.
3051 '''
3052 already_added = set()
3053 for itest, test in enumerate(tests):
3054 name = test[0]
3055 if name in already_added:
3056 msg = "multiple occurrence of test name '{0}'".format(name)
3057 raise ValueError(msg)
3058 already_added.add(name)
3059 testargs = test[1]
3060 methodname = 'test_' + name
3061 if len(test) < 3:
3062 addtest = True
3063 else:
3064 addtest = _test_needed(test[2])
3065 if addtest:
3066 setattr(cls, methodname, methodfactory(*testargs))
3067
3068
3069 class SimpleTest(_TestContainer): pass
3070 SimpleTest.add_test_methods(SIMPLE_TESTS, _get_test_output_method)
3071
3072 class LineNumberingTest(_TestContainer): pass
3073 LineNumberingTest.add_test_methods(LINENUM_TESTS, _get_test_output_method)
3074
3075 class IncludeTest(_TestContainer): pass
3076 IncludeTest.add_test_methods(INCLUDE_TESTS, _get_test_output_method)
3077
3078 class InputFileTest(_TestContainer): pass
3079 InputFileTest.add_test_methods(
3080 INPUT_FILE_TESTS, _get_test_output_from_file_input_method
3081 )
3082
3083 class ExceptionTest(_TestContainer): pass
3084 ExceptionTest.add_test_methods(EXCEPTION_TESTS, _get_test_exception_method)
3085
3086 class ImportTest(_TestContainer): pass
3087 ImportTest.add_test_methods(IMPORT_TESTS, _get_test_output_method)
3088
3089
3090 if __name__ == '__main__':
3091 unittest.main()