7
7
INCLUDE_SEEN_PSST=" ${INCLUDE_SEEN_PSST-} :assert:"
8
8
9
9
10
+
10
11
# #
11
12
# FUNCTION
12
13
# assert_fail_psst <msg>
@@ -28,14 +29,41 @@ assert_fail_psst()
28
29
# shell in case an assertion is thrown. Thus we need to be careful to not
29
30
# conflict when defining local variables.
30
31
31
- assert_argc_psst " assert_fail_psst" 1 $#
32
- assert_hasarg_psst " assert_argc_psst" " msg" " $1 "
33
-
32
+ # msg=$1
34
33
printf " Assertion fail: %s!\n" " $1 " >&2
35
34
exit 127
36
35
}
37
36
38
37
38
+
39
+ # #
40
+ # FUNCTION
41
+ # assert_func_fail_psst <func> <msg>
42
+ #
43
+ # SUMMARY
44
+ # Prints `msg` to stderr and terminates current process with error 127,
45
+ # which is the highest possible error as values 128 and up are reserved
46
+ # for signals and lower values are used by functions as failure indicators.
47
+ #
48
+ # PARAMETERS
49
+ # msg: Message to print to stderr.
50
+ #
51
+ # SAMPLE
52
+ # [ "$index" -gt 0 ] || assert_func_fail_psst "$func" "Index must be > 0"
53
+ #
54
+ assert_func_fail_psst ()
55
+ {
56
+ # We cannot use a subshell for this function as we need to exit the main
57
+ # shell in case an assertion is thrown. Thus we need to be careful to not
58
+ # conflict when defining local variables.
59
+
60
+ # func=$1
61
+ # msg=$2
62
+ assert_fail_psst " In \" $1 \" : $2 "
63
+ }
64
+
65
+
66
+
39
67
# #
40
68
# FUNCTION
41
69
# assert_argc_psst <func> <expected> <actual>
@@ -60,26 +88,15 @@ assert_argc_psst()
60
88
# shell in case an assertion is thrown. Thus we need to be careful to not
61
89
# conflict when defining local variables.
62
90
63
- if [ $# -ne 3 ]
64
- then
65
- printf " %s: Function \" %s\" expects %s arguments, got %s!\n" \
66
- " Assertion fail" assert_argc_psst 3 $# >&2
67
- exit 127
68
- fi
69
-
70
- assert_hasarg_psst " assert_argc_psst" " func" " $1 "
71
- assert_hasarg_psst " assert_argc_psst" " expected" " $2 "
72
- assert_hasarg_psst " assert_argc_psst" " actual" " $3 "
73
-
74
- if [ " $2 " -ne " $3 " ]
75
- then
76
- printf " %s: Function \" %s\" expects %s arguments, got %s!\n" \
77
- " Assertion fail" " $@ " >&2
78
- exit 127
79
- fi
91
+ # func=$1
92
+ # expected=$2
93
+ # actual=$3
94
+ [ " $2 " -eq " $3 " ] || assert_func_fail_psst " $1 " \
95
+ " Expects $2 arguments, got $3 !"
80
96
}
81
97
82
98
99
+
83
100
# #
84
101
# FUNCTION
85
102
# assert_minargc_psst <func> <min> <actual>
@@ -104,20 +121,15 @@ assert_minargc_psst()
104
121
# shell in case an assertion is thrown. Thus we need to be careful to not
105
122
# conflict when defining local variables.
106
123
107
- assert_argc_psst " assert_minargc_psst" 3 $#
108
- assert_hasarg_psst " assert_minargc_psst" " func" " $1 "
109
- assert_hasarg_psst " assert_minargc_psst" " min" " $2 "
110
- assert_hasarg_psst " assert_minargc_psst" " actual" " $3 "
111
-
112
- if [ " $2 " -gt " $3 " ]
113
- then
114
- printf " %s: Function \" %s\" expects at least %s arguments, got %s!\n" \
115
- " Assertion fail" " $@ " >&2
116
- exit 127
117
- fi
124
+ # func=$1
125
+ # min=$2
126
+ # actual=$3
127
+ [ " $2 " -le " $3 " ] || assert_func_fail_psst " $1 " \
128
+ " Expects at least $2 arguments, got $3 !"
118
129
}
119
130
120
131
132
+
121
133
# #
122
134
# FUNCTION
123
135
# assert_maxargc_psst <func> <max> <actual>
@@ -142,20 +154,15 @@ assert_maxargc_psst()
142
154
# shell in case an assertion is thrown. Thus we need to be careful to not
143
155
# conflict when defining local variables.
144
156
145
- assert_argc_psst " assert_maxargc_psst" 3 $#
146
- assert_hasarg_psst " assert_maxargc_psst" " func" " $1 "
147
- assert_hasarg_psst " assert_maxargc_psst" " max" " $2 "
148
- assert_hasarg_psst " assert_maxargc_psst" " actual" " $3 "
149
-
150
- if [ " $2 " -lt " $3 " ]
151
- then
152
- printf " %s: Function \" %s\" expects at most %s arguments, got %s!\n" \
153
- " Assertion fail" " $@ " >&2
154
- exit 127
155
- fi
157
+ # func=$1
158
+ # max=$2
159
+ # actual=$3
160
+ [ " $2 " -ge " $3 " ] || assert_func_fail_psst " $1 " \
161
+ " Expects at most $2 arguments, got $3 !"
156
162
}
157
163
158
164
165
+
159
166
# #
160
167
# FUNCTION
161
168
# assert_hasarg_psst <func> <arg> [<value>]
@@ -176,58 +183,12 @@ assert_maxargc_psst()
176
183
#
177
184
assert_hasarg_psst ()
178
185
{
179
- # We cannot use a subshell for this function as we need to exit the main
180
- # shell in case an assertion is thrown. Thus we need to be careful to not
181
- # conflict when defining local variables.
182
-
183
- if [ $# -lt 2 ]
184
- then
185
- printf " %s: Function \" %s\" expects at least %s arguments, got %s!\n" \
186
- " Assertion fail" assert_argc_psst 2 $# >&2
187
- exit 127
188
- fi
189
-
190
- if [ $# -gt 3 ]
191
- then
192
- printf " %s: Function \" %s\" expects at most %s arguments, got %s!\n" \
193
- " Assertion fail" assert_argc_psst 3 $# >&2
194
- exit 127
195
- fi
196
-
197
186
if { [ $# = 3 ] && [ -z " $3 " ] ; } \
198
187
|| { [ $# = 2 ] && [ -z " $( eval " printf '%s' \"\$ {$2 -}\" " ) " ] ; }
199
188
then
200
- printf " %s: %s of %s must not be empty!\n" \
201
- " Assertion fail" " Argument \" $2 \" " " function \" $1 \" " >&2
202
- exit 127
189
+ # func=$1
190
+ # arg=$2
191
+ # value=$3
192
+ assert_func_fail_psst " $1 " " Argument \" $2 \" must not be empty!"
203
193
fi
204
- return
205
- }
206
-
207
-
208
-
209
- # #
210
- # FUNCTION
211
- # assert_func_fail_psst <func> <msg>
212
- #
213
- # SUMMARY
214
- # Prints `msg` to stderr and terminates current process with error 127,
215
- # which is the highest possible error as values 128 and up are reserved
216
- # for signals and lower values are used by functions as failure indicators.
217
- #
218
- # PARAMETERS
219
- # msg: Message to print to stderr.
220
- #
221
- # SAMPLE
222
- # [ "$index" -gt 0 ] || assert_func_fail_psst "$func" "Index must be > 0"
223
- #
224
- assert_func_fail_psst ()
225
- {
226
- # We cannot use a subshell for this function as we need to exit the main
227
- # shell in case an assertion is thrown. Thus we need to be careful to not
228
- # conflict when defining local variables.
229
-
230
- # func=$1
231
- # msg=$2
232
- assert_fail_psst " Assertion in \" $1 \" failed: $2 "
233
194
}
0 commit comments