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