t/regress: color sequences in dash
[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 '\33[%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 params="${@:-t*.out}"
30 color 0\;36
31 echo "1..$(echo $params | wc -w)"
32 color 0
33
34 for candidate in $params
35 do
36         test_count=$((test_count+1))
37         file="${candidate%.out}"
38         input="${file%%_-*}.in"
39         name="$(echo ${file#*-} | tr _ \ )"
40
41         set -- barcat
42         [ -r "$input" ] && set -- "$@" "$input"
43         case "$name" in
44                 *\ -*)
45                         args="${name#* -}"
46                         set -- "$@" -"${args% [?|]*}"
47                         ;;
48         esac
49         case "$name" in
50                 *' ?' ) set -- sh -c "\$0 \$@ 2>/dev/null" "$@";;
51                 *' ?'*) set -- sh -c "\$0 \$@ | test \$\? = ${name#* \?}" "$@";;
52                 *' |'*) set -- sh -c "\$0 \$@ | ${name#* |}" "$@";;
53                 *)      eval set -- "$1" $2 $3
54         esac
55
56         if test -n "$regenerate"
57         then
58                 if test -e $file.sh
59                 then
60                         echo "ok $test_count # skip $file.out"
61                         continue
62                 fi
63                 "$@" >$file.out 2>&1
64         elif test -e "$file.out"
65         then
66                 "$@" 2>&1 | diffcmd "$file.out" -
67         else
68                 color 33
69                 echo "not ok $test_count - $name # TODO"
70                 color 0
71                 continue
72         fi
73
74         if test 0 != $?
75         then
76                 case "$name" in
77                 *' #TODO')
78                         color 33
79                         ;;
80                 *)
81                         fail_count=$((fail_count+1))
82                         color 1\;31
83                 esac
84
85                 printf 'not '
86         fi
87         echo "ok $test_count - $name"
88         color 0
89 done
90
91 if test $fail_count = 0
92 then
93         color 32
94         echo "# passed all $test_count test(s)"
95 else
96         color 31
97         echo "# failed $fail_count among $test_count test(s)"
98         fail_count=1  # exit code
99 fi
100 color 0
101
102 exit $fail_count