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