cd8bb5e440046b4928be69945e7404fdd3f59be9
[barcat.git] / t / regress.t
1 #!/bin/sh
2
3 cd "${0%/*}" || exit 1
4
5 test_count=0
6 fail_count=0
7
8 COLUMNS=40
9 colorize=
10 test -t 1 && colorize=1
11 color () {
12         test -n "$colorize" &&
13         printf '\e[%sm' $@
14 }
15 regenerate=
16 diffcmd () {
17         comm --nocheck-order --output-delimiter=::: -3 $@ |
18         perl -pe"END{exit !!\$.} s/^:::/$(color 31)>/ || s/^/$(color 32)</"
19 }
20
21 for option in "$@"
22 do
23         case "$option" in
24         -G) regenerate=1 && shift;;
25         -*) echo "Usage: $0 [-G] [<files>...]"; exit 64;;
26         esac
27 done
28
29 for candidate in ${@:-t*.out}
30 do
31         test_count=$((test_count+1))
32         file="${candidate%.out}"
33         input="${file%%_-*}.in"
34         name="$(echo ${file#*-} | tr _ \ )"
35
36         set -- barcat
37         [ -r "$input" ] && set -- "$@" "$input"
38         case "$name" in *\ -*) set -- "$@" -"${name#* -}";; esac
39         case "$name" in
40                 *' ?' ) set -- sh -c "\$0 \$@ 2>/dev/null" "$@";;
41                 *' ?'*) set -- sh -c "\$0 \$@ | test \$\? = ${name#* \?}" "$@";;
42                 *' |'*) set -- sh -c "\$0 \$@ | ${name#* |}" "$@";;
43                 *)      eval set -- "$1" $2 $3
44         esac
45
46         if test -n "$regenerate"
47         then
48                 if test -e $file.sh
49                 then
50                         echo "ok $test_count # skip $file.out"
51                         continue
52                 fi
53                 "$@" >$file.out 2>&1
54         elif test -e "$file.out"
55         then
56                 "$@" 2>&1 | diffcmd "$file.out" -
57         else
58                 color 33
59                 echo "not ok $test_count - $name # TODO"
60                 color 0
61                 continue
62         fi
63
64         if test 0 != $?
65         then
66                 fail_count=$((fail_count+1))
67                 color 1\;31
68                 printf 'not '
69         fi
70         echo "ok $test_count - $name"
71         color 0
72 done
73
74 if test $fail_count = 0
75 then
76         color 32
77         echo "# passed all $test_count test(s)"
78 else
79         color 31
80         echo "# failed $fail_count among $test_count test(s)"
81         fail_count=1  # exit code
82 fi
83
84 color 0\;36
85 echo "1..$test_count"
86 color 0
87 exit $fail_count