summaryrefslogtreecommitdiff
path: root/42sh/tests/functional/testsuite.sh
blob: a06308321c99e507a894f2131616ab01965fb8d2 (plain)
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
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
#!/bin/sh

REF_OUT="ref.out"
REF_REDIR="ref.red"
REF_ERR="ref.err"
TEST_OUT="test.out"
TEST_REDIR="test.red"
TEST_ERR="test.err"

score=0
testcount=0
failures=""

# COLORS
RED='\033[0;31m'
GREEN='\033[0;32m'
CYAN='\033[0;36m'
LIGHT_PURPLE='\033[1;35m'
NO_COLOR='\033[0m'

SEPARATOR='=================================================================='

debug()
{
    echo "REF stderr (exit code $1) :"
    cat "$REF_ERR"
    echo
    echo "TEST stderr (exit code $2) :"
    cat "$TEST_ERR"
    echo
}

testbasic()
{
    echo ${SEPARATOR}
    testcount=$((testcount + 1))
    printf "Testcase (%d) ${CYAN}\'%s\'${NO_COLOR}\n" "$testcount" "$1"
    bash --posix -c "$1" > "$REF_OUT" 2>"$REF_ERR"
    ref_code=$(echo $?)
    "$BIN_PATH" -c "$1" > "$TEST_OUT" 2>"$TEST_ERR"
    test_code=$(echo $?)
    [ "$DEBUG" ] && debug "$ref_code" "$test_code"
    [ "$test_code" -eq "$ref_code" ] && diff "$REF_OUT" "$TEST_OUT" && score=$((score + 1)) && printf "${GREEN}Success${NO_COLOR}\n" && return 0
    printf "${RED}Failure${NO_COLOR}\n" && failures="$failures $testcount"
}

testredir()
{
    echo ${SEPARATOR}
    testcount=$((testcount + 1))
    printf "Testcase (%d) ${CYAN}\'%s\'${NO_COLOR}\n" "$testcount" "$1$2$3"
    bash --posix -c "$1$2$3"
    ref_code=$(echo $?)
    cat "$3" > "$REF_OUT"
    "$BIN_PATH" -c "$1$2$3"
    test_code=$(echo $?)
    cat "$3" > "$TEST_OUT"
    [ "$test_code" -eq "$ref_code" ] && diff "$REF_OUT" "$TEST_OUT" && score=$((score + 1)) && printf "${GREEN}Success${NO_COLOR}\n" && return 0
    printf "${RED}Failure${NO_COLOR}\n" && failures="$failures $testcount"
}

testloop()
{
    echo ${SEPARATOR}
    testcount=$((testcount + 1))
    printf "Testcase (%d) ${CYAN}\'%s\'${NO_COLOR}\n" "$testcount" "$1"
    timeout --preserve-status 1s bash --posix -c "$1" 2>"$REF_ERR" > "$REF_OUT"
    ref_code=$(echo $?)
    timeout --preserve-status 1s "$BIN_PATH" -c "$1" 2>"$REF_ERR" > "$REF_OUT"
    test_code=$(echo $?)
    [ "$DEBUG" ] && debug "$ref_code" "$test_code"
    [ "$test_code" -eq "$ref_code" ] && diff "$REF_OUT" "$TEST_OUT" > /dev/null && score=$((score + 1)) && printf "${GREEN}Success${NO_COLOR}\n" && return 0
    printf "${RED}Failure${NO_COLOR}\n" && failures="$failures $testcount"
}

testerror()
{
    echo ${SEPARATOR}
    testcount=$((testcount + 1))
    printf "Testcase (%d) ${CYAN}\'%s\'${NO_COLOR}\n" "$testcount" "$1"
    timeout --preserve-status 1s bash --posix -c "$1" > "$REF_OUT" 2>"$REF_ERR"
    ref_code=$(echo $?)
    timeout --preserve-status 1s "$BIN_PATH" -c "$1" > "$TEST_OUT" 2>"$TEST_ERR"
    test_code=$(echo $?)
    [ "$DEBUG" ] && debug "$ref_code" "$test_code"
    [ "$test_code" -eq "$ref_code" ] && score=$((score + 1)) && printf "${GREEN}Success${NO_COLOR}\n" && return 0
    printf "${RED}Failure${NO_COLOR}\n" && failures="$failures $testcount"
}

testbadusage()
{
    echo ${SEPARATOR}
    testcount=$((testcount + 1))
    printf "Testcase (%d) ${CYAN}\'Bad Usage\'${NO_COLOR}\n" "$testcount"
    timeout --preserve-status 1s bash --posix -notaflag "$1" > "$REF_OUT" 2>"$REF_ERR"
    ref_code=$(echo $?)
    timeout --preserve-status 1s "$BIN_PATH" -notaflag "$1" > "$TEST_OUT" 2>"$TEST_ERR"
    test_code=$(echo $?)
    [ "$DEBUG" ] && debug "$ref_code" "$test_code"
    [ "$test_code" -eq "$ref_code" ] && score=$((score + 1)) && printf "${GREEN}Success${NO_COLOR}\n" && return 0
    printf "${RED}Failure${NO_COLOR}\n" && failures="$failures $testcount"
}

testprettyprint()
{
    echo ${SEPARATOR}
    testcount=$((testcount + 1))
    printf "Testcase (%d) ${CYAN}\'%s\'${NO_COLOR}\n" "$testcount" "$1"
    bash --posix -c "$1" > "$REF_OUT" 2>"$REF_ERR"
    ref_code=$(echo $?)
    "$BIN_PATH" --pretty-print -c "$1" > "$TEST_OUT" 2>"$TEST_ERR"
    test_code=$(echo $?)
    [ "$DEBUG" ] && debug "$ref_code" "$test_code"
    [ "$test_code" -eq "$ref_code" ] && diff "$REF_OUT" "$TEST_OUT" && score=$((score + 1)) && printf "${GREEN}Success${NO_COLOR}\n" && return 0
    printf "${RED}Failure${NO_COLOR}\n" && failures="$failures $testcount"
}

testasscript()
{
    echo ${SEPARATOR}
    testcount=$((testcount + 1))
    printf "Testcase (as script) (%d) ${CYAN}\'%s\'${NO_COLOR}\n" "$testcount" "$1"
    echo "$1" > 'as_script.sh'
    bash --posix 'as_script.sh' eee.sh > "$REF_OUT" 2>"$REF_ERR"
    ref_code=$(echo $?)
    "$BIN_PATH" 'as_script.sh' eee.sh > "$TEST_OUT" 2>"$TEST_ERR"
    test_code=$(echo $?)
    [ "$DEBUG" ] && debug "$ref_code" "$test_code"
    [ "$test_code" -eq "$ref_code" ] && diff "$REF_OUT" "$TEST_OUT" && score=$((score + 1)) && printf "${GREEN}Success${NO_COLOR}\n" && return 0
    printf "${RED}Failure${NO_COLOR}\n" && failures="$failures $testcount"
}

testasargument()
{
    echo ${SEPARATOR}
    testcount=$((testcount + 1))
    printf "Testcase (as script) (%d) ${CYAN}\'%s\'${NO_COLOR}\n" "$testcount" "$1"
    echo "$1" > 'as_script.sh'
    bash --posix < 'as_script.sh' eee.sh > "$REF_OUT" 2>"$REF_ERR"
    ref_code=$(echo $?)
    "$BIN_PATH" < 'as_script.sh' eee.sh > "$TEST_OUT" 2>"$TEST_ERR"
    test_code=$(echo $?)
    [ "$DEBUG" ] && debug "$ref_code" "$test_code"
    [ "$test_code" -eq "$ref_code" ] && diff "$REF_OUT" "$TEST_OUT" && score=$((score + 1)) && printf "${GREEN}Success${NO_COLOR}\n" && return 0
    printf "${RED}Failure${NO_COLOR}\n" && failures="$failures $testcount"
}

print_header()
{
    printf "\n"
    printf "${LIGHT_PURPLE}>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n"
    printf "%s\n" "$1"
    printf "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<${NO_COLOR}\n"
}

clean()
{
    rm "$REF_OUT" "$TEST_OUT" "$TEST_ERR" "$REF_ERR" "$TEST_REDIR" "22" "as_script.sh" "le_test.oui"
}

### Valid tests
VALID_TESTS="Valid tests"
print_header ${VALID_TESTS}

## Builtins
# true
testbasic 'true'
testbasic 'true -c -e'

# false
testbasic 'false'
testbasic 'false -c -e'

# echo
# echo no flag
testbasic 'echo'
testbasic 'echo OK'
testbasic 'echo A AA AAA A AA AAA'
testbasic 'echo !'
testbasic "echo Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Egestas purus viverra accumsan in nisl nisi. At quis risus sed vulputate. Neque laoreet suspendisse interdum consectetur libero. Et molestie ac feugiat sed lectus vestibulum mattis. Tristique nulla aliquet enim tortor at auctor. Aliquet porttitor lacus luctus accumsan tortor. Tellus cras adipiscing enim eu turpis. Mattis ullamcorper velit sed ullamcorper morbi tincidunt ornare. Nec sagittis aliquam malesuada bibendum arcu vitae elementum. Consequat id porta nibh venenatis cras. Dolor magna eget est lorem ipsum. Vivamus at augue eget arcu dictum varius duis. Aliquam eleifend mi in nulla. Cursus risus at ultrices mi tempus imperdiet nulla malesuada. Adipiscing elit ut aliquam purus sit amet luctus venenatis. Risus at ultrices mi tempus imperdiet nulla malesuada pellentesque elit. Mauris cursus mattis molestie a iaculis at erat pellentesque adipiscing."
testbasic "echo if then do fi elif while for done until else in !"
# echo flag -n
testbasic 'echo -n'
testbasic 'echo -n OK'
testbasic 'echo -n A AA AAA A AA AAA'
testbasic 'Echo -n Ugly presentation innit?'
# echo flag -e
testbasic 'echo -e'
testbasic 'echo -e OK'
testbasic 'echo -e A AA AAA A AA AAA'
testbasic 'echo -e \n\t\e'
testbasic 'echo -e \n \t \e \\ \f'
testbasic 'echo -e some stuff \\ is in here'
testbasic 'echo -e Cutni\n half'
testbasic 'echo -e Testing the tab \t Just like this, see?\n'
# echo flag -E
testbasic 'echo -E'
testbasic 'echo -E OK'
testbasic 'echo -E A AA AAA A AA AAA'
testbasic 'echo -E \n\t\e'
testbasic 'echo -E \n \t \e \\ \f'
testbasic 'echo -E Nothing to see here'
# echo multiple flags
testbasic 'echo -E -n'
testbasic 'echo -E -n OK'
testbasic 'echo -E -n A AA AAA A AA AAA'
testbasic 'echo -E -e \n\t\e'
testbasic 'echo -e -E \n\t\e'
testbasic 'echo -e -E -n \n\t\e'
testbasic 'echo -E -e -n \n \t \e \\ \f'
testbasic 'echo -e -E \n \t \e \\ \f'
testbasic "echo -e -n Something\'s wrong\nI can \\feel\\ it\n"
testbasic 'echo -E -n Still nothing to \tsee here\n'
testbasic 'echo -e -n Testing the tab \t Just like this, see?\n Abd I also force a newline\n'
testbasic 'echo -e -n Something'\''s wrong\nI can \feel\\ it\n'
testbasic 'echo -E -e does this \t works ??'

# dot
testbasic '.'
testbasic '. ./tests/resources/dot/script1.sh'
testbasic '. ./tests/resources/dot/script2.sh'

# exit
testbasic 'exit'
testbasic 'exit -e'
testbasic 'exit 5'
testbasic '. ./tests/resources/exit/script1.sh'

# cd
testbasic 'cd'
testbasic 'cd -'
testbasic 'cd /usr/'
testbasic 'cd ..'

# export
testbasic 'export'
testbasic 'export A=42'
testbasic 'export A'

# unset
testbasic 'export A=42; unset A'
testbasic 'unset A'

# break & continue
testbasic 'break'
testbasic 'break 5'
testbasic 'continue'
testbasic 'continue 5'

## Simple Command
testbasic 'ls'
testbasic 'cat /etc/passwd'
testbasic 'git status'
testbasic 'git log --all --decorate --oneline --graph'
testbasic 'tree'
testbasic 'date'
testbasic 'echo Testsuite now functional!'
testbasic 'echo if'
testbasic 'echo then'
testbasic 'ls then'
testbasic 'cat fi'
testbasic '  cat for a while'

## Command List
testbasic 'true; false;'
testbasic 'false; true;'
testbasic 'true; true;'
testbasic 'false; false;'
testbasic 'echo aaa bbb ccc; true; ls;'
testbasic 'echo aaa bbb ccc; true; false; if true; then echo ok; fi'
testbasic 'echo a; echo b; echo c; echo ; echo ; echo ; echo d; echo ls; ls;'
testbasic 'true; true; true; true; true; true; true; true; true; true; false'
testbasic 'ls;ls .;ls ./;ls;ls;ls;ls ..;ls;ls;ls ../;ls;ls;'
testbasic 'true; true; true; true; echo aaa; true; true; false'
testbasic 'false; false; false; false; echo aaa; true'

## Single Quote
testbasic "'echo' OK"
testbasic "'ech'o OK"
testbasic "cat 'fi'"
testbasic "l'''s'"
testbasic "'c''a''t' '/etc/pa'sswd"
testbasic "g'i't s'tat'us"
testbasic "g'i't l'o'g --all --decorate --oneline --graph"
testbasic "'tree'"
testbasic "'d''a''t''e'"
testbasic "'d''''a''''''t''''''''e'"
testbasic "'e''c'ho 'test aaaa'"

## Comment
testbasic "echo \'# comment ça ???\'"
testbasic "echo \'comment ça ???\' \# comment ça \'????\'"
testbasic "echo \# et là ?"
testbasic "echo '\# et là ?'"
testbasic "echo han ? \# '\# et là ?'"
testbasic "echo \# '\# et là ?'"
testbasic "#######################################"
testbasic "echo \#Inexistant"
testbasic "echo Existe \#pas"

## If then elif else
testbasic "if echo test; false; then echo ok; else echo ko; fi"
testbasic "if false; then echo ok; else echo ko; fi"
testbasic "if false; then echo ok; elif true; then echo bogos binted; else echo ko; fi"
testbasic "if false; then echo ok; elif ls; then echo bogos binted; else echo ko; fi"
testbasic "if false; then echo KO1; elif false; then echo KO2; elif false; then echo KO3; elif false; then echo KO4; elif false; then echo KO5; fi"
testbasic "if false; then echo KO1; elif false; then echo KO2; elif false; then echo KO3; elif false; then echo KO4; elif false; then echo KO5; else echo OK; fi"
testbasic "if false; then     echo ok;                  elif true;     then   echo  bogos binted;           else echo ko; fi"
testbasic "if false; then \n\n\n\n\n\n\n\n echo ok; else echo ko\n\n\n\n; fi"
testbasic "if 'true' ; then e'ch'o O'K' ; else e'ch'o K'O'; fi"
testbasic "if 'tr'ue ; then 'echo' OK ; else 'echo' KO; fi"
testbasic "if true ; then echo OK ; elif false; then echo KO; elif false; then echo KO; fi"
testbasic "if false; then echo ok; else echo ko; fi ; if false; then echo ok; else echo ko; fi"
testbasic "if echo count with me; then echo 1 2 3 4 5 6 7 8 9 10 | wc; else echo too bad; fi"
testbasic "if echo -n many comms; then echo a; echo b; echo c; else echo too bad; fi"
testbasic "if echo testing redirs in the condition | wc; then echo it works; else echo it breakes; fi"
testbasic "if echo testing compound lists; echo another one; echo and another one; false; then echo problem; else echo fine; fi"
testbasic "if echo testing compound lists; echo another one; echo and another one; false; echo this is actually true; then echo fine; else echo problem; fi"

## Pipeline 
testbasic "true | false"
testbasic "false | true"
testbasic "find -iname \"*.[ch]\" | grep '.*test.*'"
testbasic "echo aaaaa | cat -e | sed 's/$/€/g'"
testbasic "echo Bonjour je suis là | cut --characters o | sed 's/o/a/g'"
testbasic "echo aaa | cat"
testbasic "echo aaa | cat -e"
testbasic "cat testsuite.sh | wc -l | cat -e"
testbasic "cat testsuite.sh | wc -l | cat | wc -c"
testbasic "cat testsuite.sh | wc -l | cat | wc -l"
testbasic "cat testsuite.sh | wc -l | cat | wc | cat -e"
testbasic "echo ./ | ls"
testbasic "echo ./TrustMeBroItJustWorks | ls"
testbasic "ls | ls"
testbasic "echo | echo"

## Redirection
testredir "echo bonjour" '>' "$TEST_REDIR"
testredir "echo bonjour" '<' "$TEST_REDIR"
testredir "echo bonjour" '>>' "$TEST_REDIR"
testredir "echo bonjour" '<>' "$TEST_REDIR"
testredir "echo bonjour" '>|' "$TEST_REDIR"
testredir "echo -a bonjour" '>&' "$TEST_REDIR"
testredir "echo bonjour" '<&' "$TEST_REDIR"

## And Or Not (Logical)
testbasic "! echo"
testbasic "! true"
testbasic "! false"
testbasic "! ! echo Invalid double negation"
testbasic "if ! false; then echo OK; else echo KO; fi"
testbasic "! true; echo aaa"
testbasic "true && false"
testbasic "true || false"
testbasic "false && true"
testbasic "! false && true"
testbasic "echo aa && true"
testbasic "if test -z $aaaaaa && echo doesn\'t exist; then echo OK; fi"
testbasic "false || ! false"
testbasic "if ! true || true ; then echo KO ; else echo OK ; fi"
testbasic "true || false || false || false || false || false"
testbasic "false && false && false && false && false && false"
testbasic "! true || ! false || ! false || ! false || ! false || ! false"
testbasic "if false && ! true && ! true || false; then echo KO; else echo OK; fi"

## While
testbasic 'while false ; do echo LOOOOOOP ; done'
testbasic 'while invalid ; do echo LOOOOOOP ; done'

## Until
testbasic 'until true ; do echo LOOOOOOP ; done'
testbasic 'until cat /etc/passwd ; do echo LOOOOOOP ; done'

## Variable expansion 
# Basic var
testbasic 'A=42 echo $A sh'
testbasic 'A=42; echo $A sh'
testbasic 'echo $A; A=42'
testbasic 'A=42 B=43 C=44 echo $A $B $C'
testbasic 'A=42 B=43 C=44 echo; $A $B $C'
testbasic 'A=42; B=43; C=44; echo; $A $B $C'
testbasic 'A=42 echo $A Nope; echo $A YES'
testbasic 'A=42 ;echo $A; A=43; echo $A; A=44; echo $A'
testbasic 'A=42 A=43 A=44; echo $A'
testbasic 'echo; echo; A=42; echo; echo; echo $A'
testbasic 'A=42 B=43 A=43;echo $A $B'
testbasic 'A=42 B=42 C=43 D=44 E=45 F=46 G=47 H=48 I=49 K=50; echo $A $B $C $D $E $F $G $H $I $J $K'
testbasic 'A=42; B=$A ; echo $B'

# Special var
testbasic 'echo $@'
testbasic 'echo $*'
testbasic 'echo $?'
testbasic 'echo $$'
testbasic 'echo $1'
testbasic 'echo $2 $3'
testbasic 'echo $#'
testbasic 'echo $RANDOM'
testbasic 'echo $UID'
testbasic 'echo $OLDPWD'
testbasic 'echo $PWD'
testbasic 'echo $IFS'
testbasic 'echo ${PWD}'

## For
testbasic 'for i in foo bar baz ; do echo $i ; done'
testbasic 'for i in foo "bar baz" ; do echo $i ; done'
testbasic 'for i; do echo $i ; done'

## Command Substitution
testbasic 'echo $(echo toto)'
testbasic 'echo $(echo toto; echo tata)'
testbasic 'echo $(echo toto | cat -e)'
testbasic 'echo $(if true; then echo OK; else echo KO; fi)'
testbasic 'echo $(true)'
testbasic 'echo$(echo " toto")'
testbasic 'echo `echo $(printf " coucou")`'
testbasic 'echo `echo $(printf " coucou%s" $(echo aaaaaaaaaaaaaaa))`'
# This test will print stuff on STDERR but still succedes with exit code 0
testbasic 'echo `echo $(printf " coucou%s" $(aaaaaaaaaaaaaaa))`'
testbasic 'echo "$(echo OK)"'

## Subshells
testbasic 'echo aaa; (echo bbb); echo ccc;'
testbasic 'echo aaa; (A=42; echo $A sh);'
testbasic 'echo aaa; (A=42; echo $A sh); echo ccc'
testbasic 'A=41; (echo $A; A=42); echo $A'
testbasic 'A=41; (A=42; echo $A); echo $A'
testbasic 'A=41; (A=42 echo $A); echo $A'
testbasic '(A=42); echo $A'
testbasic '(A=1;(A=2;(A=3; echo $A); echo $A); echo $A)'
testbasic '(A=1); (A=2); (echo $A)'
testbasic '(A=1; (B=2)); echo $A $B'
testbasic '(A=1); (B=1); (C=1); (echo $A $B $C)'
testbasic 'A=1; B=2; C=3; (echo $A $B $C)'
testbasic '((A=1);(B=2); echo $A $B)'

## Command Block
testredir "{ echo bonjour ; echo au revoir; }" '>' "$TEST_REDIR"
testredir "{ echo bonjour ; echo au revoir; }" '<' "$TEST_REDIR"
testredir "{ echo bonjour ; echo au revoir; }" '>>' "$TEST_REDIR"
testredir "{ echo bonjour ; echo au revoir; }" '<>' "$TEST_REDIR"
testredir "{ echo bonjour ; echo au revoir; }" '>|' "$TEST_REDIR"
testredir "{ echo bonjour ; echo au revoir; }" '>&' "$TEST_REDIR"
testredir "{ echo bonjour ; echo au revoir; }" '<&' "$TEST_REDIR"

## Mixed tests
testbasic "echo a; echo b; echo c | wc | cat; echo d; echo e"
testbasic "echo a; echo b; if true; then echo OK; else echo KO; fi; echo c; echo d"
testbasic "ls; echo a && t'ru'e || false; echo conditionals are now over | wc; if true; then echo OK; else echo KO; fi"
testbasic "echo aaa | wc | cat; echo bbb | cat | ls; echo ccc | echo"

## As Script
testasscript "echo a; echo b; echo c | wc | cat; echo d; echo e"
testasscript "echo a; echo b; if true; then echo OK; else echo KO; fi; echo c; echo d"
testasscript "ls; echo a && t'ru'e || false; echo conditionals are now over | wc; if true; then echo OK; else echo KO; fi"
testasscript "echo aaa | wc | cat; echo bbb | cat | ls; echo ccc | echo"

## As Argument
testasargument "echo a; echo b; echo c | wc | cat; echo d; echo e"
testasargument "echo a; echo b; if true; then echo OK; else echo KO; fi; echo c; echo d"
testasargument "ls; echo a && t'ru'e || false; echo conditionals are now over | wc; if true; then echo OK; else echo KO; fi"
testasargument "echo aaa | wc | cat; echo bbb | cat | ls; echo ccc | echo"

# Error tests
ERROR_TESTS="Error tests"
print_header ${ERROR_TESTS}

testerror "ghsdfloerijg"
testerror "if echo test; false; then\n echo ok;\n else\n echo ko;\n fi"
testerror "if djunkleschkog; then echo bear; else echo plushie; fi"
testerror "if true; then nope; else echo you sure?; fi"
testerror "if false; then echo Seriously?; else depression; fi"
testerror "if false; then echo not again; elif insanity; then echo Nope; else echo still nope; fi"
testerror "if false; then echo stop; elif true; then nope; else echo really, stop; fi"
testerror "if false; then echo stop; elif false; then echo STOP DAMMIT; else adog; fi"
testerror "ech'o a'aa"
testerror "'echo aaa'"
testerror "if true; echo OK ; else echo KO; fi"
testerror "if bobby; then echo GREEDY; else echo fine; fi"
testerror "'if' true; 'then' echo OK; 'else' echo KO; 'fi'"
testerror "if true; 'then' echo OK; 'else' echo KO; 'fi'"
testerror "e'cho aaa"
testerror "echo aa ><"
testerror "while while true; do echo ok else echo false"
testerror "while true; echo missing; done"
testerror "while true; do echo missing again;"
testerror "while ; do echo no cond; done"
testerror "while true; do echo no end done"
testerror "while echo infinity; do do echo incorrect; done"
testerror "while invalid; do echo no end; done"
testerror "while invalid; do incorrect; done"
testerror "until while true; do echo ok else echo false"
testerror "until true; echo missing; done"
testerror "until true; do echo missing again;"
testerror "until ; do echo no cond; done"
testerror "until true; do echo no end done"
testerror "until echo infinity; do do echo incorrect; done"
testerror "until invalid; do echo no end; done"
testerror "until invalid; do incorrect; done"

# Logic Combined with Pipelines
testerror "true && echo This | grep This || echo Fail"
testerror "false || echo Fallback | grep Fall"
testerror "echo Hello | grep H && echo Found || echo Not Found"
testerror "echo Hello | grep Z && echo Found || echo Not Found"
testerror "false || echo aaaaa | cat | sed 's/a/b/g'"
testerror "true && echo OK | cat -e && echo Done"
testerror "true || echo This should not print | grep This"
testerror "false && echo This should not print | grep This"

# Complex Conditions with Pipelines
testerror "if echo Hello | grep H; then echo OK; else echo KO; fi"
testerror "if echo Bonjour | grep Z; then echo OK; else echo KO; fi"
testerror "if echo Foo | grep F && echo Bar | grep B; then echo OK; else echo KO; fi"
testerror "if false || echo Hello | grep H; then echo OK; else echo KO; fi"

# Invalid Pipelines
testerror "| echo Leading pipe"
testerror "echo Trailing pipe |"
testerror "echo Double || pipe"
testerror "echo || echo || Invalid logic"
testerror "echo Missing command | | grep H"

# Invalid Redirections
testerror "echo Test >"
testerror "echo Test >< file"
testerror "echo Test ><< file"
# testerror 'echo Test >!< file'
testerror "echo Test > > file"
testerror "cat < nonexistentfile"
testerror "ls 2> /nonexistentdirectory/file"

# Invalid Logic
testerror '!echo'
testerror '!true'
testerror '!false'
testerror "true && && echo Invalid and"
testerror "true || || echo Invalid or"

# Micellanious
testerror ')'
testerror '}'
testerror '||'
testerror '&&'
testerror '$'
testerror '&'
testerror '|'

# If loops
testerror 'if'
testerror 'if true; echo aaa'
testerror 'if ; echo aaa'
testerror 'if true; then echo aaa'
testerror 'if true; then echo aaa; else'
testerror 'if true; then echo aaa; else echo bbb'
testerror 'if true; then echo aaa; else echo bbb; elif'
testerror 'if true; then echo aaa; elif'
testerror 'if true; then echo aaa; elif false; echo ccc'
testerror 'if true; then echo aaa; elif false; then echo ccc'
testerror 'if true; then echo aaa; elif false; then echo ccc;'
testerror 'if rue; then echo aaa'
testerror 'if true; then cho aaa; else'
testerror 'if true; then cho aaa; else cho bbb'
testerror 'if true; then cho aaa; else cho bbb; elif'
testerror 'if true; then cho aaa; elif'
testerror 'if true; then cho aaa; elif false; cho ccc'
testerror 'if true; then cho aaa; elif false; then cho ccc'
testerror 'if true; then cho aaa; elif false; then cho ccc;'

# While loops
testerror 'while'
testerror 'while false'
testerror 'while false; echo aaa'
testerror 'while false; do echo aaa;'

# Until loops
testerror 'until'
testerror 'until true'
testerror 'until true; echo aaa'
testerror 'until true; do echo aaa;'

# For loops
testerror 'for'
testerror 'for a'
testerror 'for abc def'
testerror 'for a in ;'
testerror 'for ('
testerror 'for a ;'
testerror 'for a in b c d e f g h ;'
testerror 'for i in range; do'
testerror 'for j in range; asd'
testerror 'for k in range; do echo aaa;'
testerror 'for l in range; do echo aaa'
testerror 'for in in in'

# Function declaration
testerror 'test ('
testerror 'test (0123456789'
testerror 'test ()'
testerror 'test &'
testerror 'test () {'

# Redirection
testerror '2'
testerror 'wipuevbhqipie 2>&'
testerror 'echo aaa 2>&'
testerror 'echo > >'
testerror 'echo aaa >& ('
testerror 'echo aaa >& )'
testerror '>22'
testerror '>>>>>>>>>'
testerror '() > ()'

# Bad Usage
testbadusage

## Pretty print
testprettyprint 'echo OK'
testprettyprint 'ls'
testprettyprint 'if true ; then exit ; else . ./tests/resources/dot/script1.sh'
testprettyprint 'while false ; do your mom ; done'
testprettyprint 'until true ; do nothing ; done'
testprettyprint 'for i in foo bar baz ; do echo $i ; done'
testprettyprint 'A=42 ; echo $A'
testprettyprint 'find . -name "*.[hc]" | xargs wc -l'
testprettyprint 'if ! false && true || true ; then echo OK ; else echo KO ; fi'
testprettyprint 'echo OK > le_test.oui && rm -f le_test.oui'

RESULTS="Results"
print_header ${RESULTS}
printf "%d%% ($score/$testcount) of the tests passed!\n" $((score * 100 / testcount))
[ "$failures" ] && echo "Failing tests ($((testcount - score))/$testcount): $failures"
echo ${SEPARATOR}
[ "$OUTPUT_FILE" ] && echo $((score * 100 / testcount)) > "$OUTPUT_FILE"

clean

exit 0