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