]> pd.if.org Git - zpackage/blob - t/tap.sh
add shell tap functions
[zpackage] / t / tap.sh
1 #!/bin/sh
2
3 tn=0
4 planned=0
5
6 tryrun() {
7         note="$@"
8         program=$1
9         shift
10         $program "$@" >> test.out 2>&1
11         if [ $? -ne 0 ]; then
12                 printf 'not ';
13         fi
14         tn=$((tn + 1))
15         printf 'ok %d - %s %s\n' $tn $program "$*"
16 }
17
18 require() {
19         note="$*"
20         program=$1
21         shift
22         $program "$@" >> test.out 2>&1
23         rv=$?
24         tn=$((tn + 1))
25         if [ $rv -ne 0 ]; then
26                 printf 'not ok %d - %s %s\n' $tn $program "$*"
27                 bailout "exit status $rv";
28         fi
29         printf 'ok %d - %s %s\n' $tn $program "$*"
30
31 }
32
33 bailout() {
34         diag "$@"
35         printf "bail out!\n";
36         exit 255;
37 }
38
39 shownote() {
40         if [ "$note" != "" ]; then
41                 printf '# %s\n' "$note"
42         fi
43 }
44
45 diag() {
46         printf '# %s\n' "$@"
47 }
48
49 diagfile() {
50         sed -e 's/^/# /' $1
51 }
52
53 diagstdin() {
54         sed -e 's/^/# /'
55 }
56
57 okexit() {
58         exitwith 0 "$*"
59 }
60
61 failsok() {
62         rv=$?
63         note=
64         if [ $rv -eq 0 ]; then
65                 printf 'not ';
66                 note=$(printf 'got "%d" expected "%d"' "$rv" "0")
67         fi
68         tn=$((tn + 1))
69         printf 'ok %d - %s\n' $tn "$*"
70         shownote
71 }
72
73 exitwith() {
74         rv=$?
75         note=
76         if [ $rv -ne $1 ]; then
77                 printf 'not ';
78                 note=$(printf '# got "%d" expected "%d"' "$rv" "$1")
79         fi
80         shift
81         tn=$((tn + 1))
82         printf 'ok %d - %s\n' $tn "$*"
83         shownote
84 }
85
86 okstreq() {
87         note=
88         if [ "$1" != "$2" ]; then
89                 printf 'not ';
90                 note=$(printf 'got "%s" expected "%s"' "$1" "$2")
91         fi
92
93         shift;shift;
94         tn=$((tn + 1))
95         printf 'ok %d - %s\n' $tn "$*"
96         shownote
97 }
98
99 plan() {
100         planned=$1
101         printf '1..%d\n' $planned
102 }
103
104 finish() {
105         if [ $planned -eq 0 ]; then
106                 printf '1..%d\n' $tn
107         fi
108 }