t: strip options from input file names
[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 \$1 | ${name#* |}" "$@";;
41                 *)      set -- "$1" $2 $3
42         esac
43
44         if test -n "$regenerate"
45         then
46                 if test -e $file.sh
47                 then
48                         echo "ok $test_count # skip $file.out"
49                         continue
50                 fi
51                 "$@" >$file.out 2>&1
52         elif test -e "$file.out"
53         then
54                 "$@" 2>&1 | diffcmd "$file.out" -
55         else
56                 color 33
57                 echo "not ok $test_count - $name # TODO"
58                 color 0
59                 continue
60         fi
61
62         if test 0 != $?
63         then
64                 fail_count=$((fail_count+1))
65                 color 1\;31
66                 printf 'not '
67         fi
68         echo "ok $test_count - $name"
69         color 0
70 done
71
72 if test $fail_count = 0
73 then
74         color 32
75         echo "# passed all $test_count test(s)"
76 else
77         color 31
78         echo "# failed $fail_count among $test_count test(s)"
79         fail_count=1  # exit code
80 fi
81
82 color 0\;36
83 echo "1..$test_count"
84 color 0
85 exit $fail_count