-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.bash_asserts
210 lines (166 loc) · 3.69 KB
/
.bash_asserts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#!/bin/bash
function assert_eq {
local arg1=$(echo "$1" | sed "s/^[ ]*//g")
local arg2=$(echo "$2" | sed "s/^[ ]*//g")
local prog="$(basename $0 .test)"
if [ ! "$arg1" == "$arg2" ]; then
print_err "test $prog error"
return 1
else
echo "test $prog ok"
fi
}
function assert_not_eq {
local arg1=$(echo "$1" | sed "s/^[ ]*//g")
local arg2=$(echo "$2" | sed "s/^[ ]*//g")
local prog="$(basename $0 .test)"
if [ "$arg1" == "$arg2" ]; then
print_err "test $prog error"
return 1
else
echo "test $prog ok"
fi
}
function assert_lt {
local arg1=$1
local arg2=$2
local prog="$(basename $0 .test)"
if [ ! $arg1 -lt $arg2 ]; then
print_err "test $prog error"
return 1
else
echo "test $prog ok"
fi
}
function assert_le {
local arg1=$1
local arg2=$2
local prog="$(basename $0 .test)"
if [ ! $arg1 -le $arg2 ]; then
print_err "test $prog error"
return 1
else
echo "test $prog ok"
fi
}
function assert_gt {
local arg1=$1
local arg2=$2
local prog="$(basename $0 .test)"
if [ ! $arg1 -gt $arg2 ]; then
print_err "test $prog error"
return 1
else
echo "test $prog ok"
fi
}
function assert_ge {
local arg1=$1
local arg2=$2
local prog="$(basename $0 .test)"
if [ ! $arg1 -ge $arg2 ]; then
print_err "test $prog error"
return 1
else
echo "test $prog ok"
fi
}
function assert_between {
local arg1=$1
local arg2=$2
local arg3=$3
local prog="$(basename $0 .test)"
if [ $arg1 -lt $arg2 ] || [ $arg1 -gt $arg3 ]; then
print_err "test $prog error"
return 1
else
echo "test $prog ok"
fi
}
function assert_contains {
local arg1="$1"
local arg2="$2"
local prog="$(basename $0 .test)"
if ! contains "$arg1" "$arg2"; then
print_err "test $prog error"
return 1
else
echo "test $prog ok"
fi
}
function assert_length {
local arg1=$1
local arg2=$2
local prog="$(basename $0 .test)"
if [ ${#arg1} -ne $arg2 ]; then
print_err "test $prog error"
return 1
else
echo "test $prog ok"
fi
}
function assert_result_pos {
assert_eq $1 0
return $?
}
function assert_result_neg {
assert_gt $1 0
return $?
}
function assert_files_exist {
local prog="$(basename $0 .test)"
local result="ok"
for file in "$@"; do
if [ ! -s $file ]; then
result="error"
break
fi
done
if [ $result != "ok" ]; then
print_err "test $prog error"
return 1
else
echo "test $prog ok"
fi
}
function assert_prog_exists {
local progs=$*
local missing=0
for prog in $progs; do
if [ `\which $prog 2> /dev/null | wc -l` -gt 0 ]; then
echo "program ${prog} ok"
else
print_err "program ${progs} missing"
let "missing++"
fi
done
return $missing
}
function assert_skip_test {
local arg="$1"
local prog="$(basename $0 .test)"
if [ -z "$arg" ]; then
echo "test $prog skip"
else
echo "test $prog skip: \"$arg\""
fi
}
function assert_skip_slow_test {
assert_skip_test "too slow"
return $?
}
export -f assert_eq
export -f assert_not_eq
export -f assert_lt
export -f assert_le
export -f assert_gt
export -f assert_ge
export -f assert_between
export -f assert_contains
export -f assert_length
export -f assert_result_pos
export -f assert_result_neg
export -f assert_files_exist
export -f assert_prog_exists
export -f assert_skip_test
export -f assert_skip_slow_test