Skip to content
This repository was archived by the owner on May 20, 2024. It is now read-only.

Commit 0777364

Browse files
CodingMarkusCodingMarkus
CodingMarkus
authored and
CodingMarkus
committed
Add assert_func_fail and always quote func name
1 parent d409941 commit 0777364

File tree

1 file changed

+34
-6
lines changed

1 file changed

+34
-6
lines changed

lib/psst/basic/assert.inc.sh

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@ assert_argc_psst()
6262

6363
if [ $# -ne 3 ]
6464
then
65-
printf "Assertion fail: Function %s expects %s arguments, got %s!\n" \
66-
assert_argc_psst 3 $# >&2
65+
printf "%s: Function \"%s\" expects %s arguments, got %s!\n" \
66+
"Assertion fail" assert_argc_psst 3 $# >&2
6767
exit 127
6868
fi
6969

@@ -73,8 +73,8 @@ assert_argc_psst()
7373

7474
if [ "$2" -ne "$3" ]
7575
then
76-
printf "Assertion fail: Function %s expects %s arguments, got %s!\n" \
77-
"$@" >&2
76+
printf "%s: Function \"%s\" expects %s arguments, got %s!\n" \
77+
"Assertion fail" "$@" >&2
7878
exit 127
7979
fi
8080
}
@@ -111,7 +111,7 @@ assert_minargc_psst()
111111

112112
if [ "$2" -gt "$3" ]
113113
then
114-
printf "%s: Function %s expects at least %s arguments, got %s!\n" \
114+
printf "%s: Function \"%s\" expects at least %s arguments, got %s!\n" \
115115
"Assertion fail" "$@" >&2
116116
exit 127
117117
fi
@@ -149,7 +149,7 @@ assert_maxargc_psst()
149149

150150
if [ "$2" -lt "$3" ]
151151
then
152-
printf "%s: Function %s expects at most %s arguments, got %s!\n" \
152+
printf "%s: Function \"%s\" expects at most %s arguments, got %s!\n" \
153153
"Assertion fail" "$@" >&2
154154
exit 127
155155
fi
@@ -203,3 +203,31 @@ assert_hasarg_psst()
203203
fi
204204
return
205205
}
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+
}

0 commit comments

Comments
 (0)