]> pd.if.org Git - zpackage/blob - t/tap.sh
8f9eabe6cf3b3bf825d218af41f4bac3e399e052
[zpackage] / t / tap.sh
1 #!/bin/sh
2
3 tn=0
4 planned=0
5
6 tryrun() {
7         if [ "$1" = "-v" ]; then
8                 allout=1
9                 shift
10         else
11                 allout=0
12         fi
13
14         program=$1
15         shift
16
17         $program "$@" > test.out 2>&1
18         tap_rv=$?
19         tn=$((tn + 1))
20
21         if [ $tap_rv -ne 0 ]; then
22                 printf 'not ';
23         fi
24         printf 'ok %d - %s %s\n' $tn $program "$*"
25
26         if [ $allout -eq 1 ] || [ $tap_rv -ne 0 ]; then
27                 if test -s test.out; then
28                         cat test.out | diagstdin
29                 fi
30                 rm test.out
31         fi
32
33         return $tap_rv
34 }
35
36 require() {
37         tryrun "$@"
38         rv=$?
39         if [ $rv -ne 0 ]; then
40                 bailout "exit status $rv";
41         fi
42 }
43
44 bailout() {
45         diag "$@"
46         printf "bail out!\n";
47         exit 255;
48 }
49
50 shownote() {
51         if [ "$note" != "" ]; then
52                 printf '# %s\n' "$note"
53         fi
54 }
55
56 diag() {
57         printf '# %s\n' "$@"
58 }
59
60 diagfile() {
61         sed -e 's/^/# /' $1
62 }
63
64 diagstdin() {
65         sed -e 's/^/# /'
66 }
67
68 okexit() {
69         exitwith 0 "$*"
70 }
71
72 failsok() {
73         rv=$?
74         note=
75         if [ $rv -eq 0 ]; then
76                 printf 'not ';
77                 note=$(printf 'got "%d" expected non-zero' "$rv")
78         fi
79         tn=$((tn + 1))
80         printf 'ok %d - %s\n' $tn "$*"
81         shownote
82 }
83
84 exitwith() {
85         rv=$?
86         note=
87         if [ $rv -ne $1 ]; then
88                 printf 'not ';
89                 note=$(printf 'got "%d" expected "%d"' "$rv" "$1")
90         fi
91         shift
92         tn=$((tn + 1))
93         printf 'ok %d - %s\n' $tn "$*"
94         shownote
95         return $rv
96 }
97
98 okstreq() {
99         note=
100         if [ "$1" != "$2" ]; then
101                 printf 'not ';
102                 note=$(printf 'got "%s" expected "%s"' "$1" "$2")
103         fi
104
105         shift;shift;
106         tn=$((tn + 1))
107         printf 'ok %d - %s\n' $tn "$*"
108         shownote
109 }
110
111 plan() {
112         planned=$1
113         printf '1..%d\n' $planned
114 }
115
116 finish() {
117         if [ $planned -eq 0 ]; then
118                 printf '1..%d\n' $tn
119         fi
120 }
121
122 skip() {
123         tap_skipping=$1
124         shift
125         while [ $tap_skipping -gt 0 ]; do
126                 tn=$((tn + 1))
127                 tap_skipping=$((tap_skipping - 1))
128                 printf 'ok %d - # skip %s\n' $tn "$*"
129                 #ok '# skip ' "$*"
130         done
131 }
132
133 ok() {
134         rv=$1
135         shift
136         if [ $rv = 1 ]; then
137                 printf 'not '
138         fi
139         tn=$((tn + 1))
140         printf 'ok %d - %s\n' $tn "$*"
141         return $rv
142 }
143
144 pass() {
145         ok 0 "$@"
146 }
147
148 fail() {
149         ok 1 "$@"
150 }