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

Commit 7d6c068

Browse files
CodingMarkusCodingMarkus
CodingMarkus
authored and
CodingMarkus
committed
Simplify assertions
1 parent b99789b commit 7d6c068

File tree

1 file changed

+53
-92
lines changed

1 file changed

+53
-92
lines changed

lib/psst/basic/assert.inc.sh

Lines changed: 53 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ esac
77
INCLUDE_SEEN_PSST="${INCLUDE_SEEN_PSST-}:assert:"
88

99

10+
1011
##
1112
# FUNCTION
1213
# assert_fail_psst <msg>
@@ -28,14 +29,41 @@ assert_fail_psst()
2829
# shell in case an assertion is thrown. Thus we need to be careful to not
2930
# conflict when defining local variables.
3031

31-
assert_argc_psst "assert_fail_psst" 1 $#
32-
assert_hasarg_psst "assert_argc_psst" "msg" "$1"
33-
32+
# msg=$1
3433
printf "Assertion fail: %s!\n" "$1" >&2
3534
exit 127
3635
}
3736

3837

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+
3967
##
4068
# FUNCTION
4169
# assert_argc_psst <func> <expected> <actual>
@@ -60,26 +88,15 @@ assert_argc_psst()
6088
# shell in case an assertion is thrown. Thus we need to be careful to not
6189
# conflict when defining local variables.
6290

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!"
8096
}
8197

8298

99+
83100
##
84101
# FUNCTION
85102
# assert_minargc_psst <func> <min> <actual>
@@ -104,20 +121,15 @@ assert_minargc_psst()
104121
# shell in case an assertion is thrown. Thus we need to be careful to not
105122
# conflict when defining local variables.
106123

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!"
118129
}
119130

120131

132+
121133
##
122134
# FUNCTION
123135
# assert_maxargc_psst <func> <max> <actual>
@@ -142,20 +154,15 @@ assert_maxargc_psst()
142154
# shell in case an assertion is thrown. Thus we need to be careful to not
143155
# conflict when defining local variables.
144156

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!"
156162
}
157163

158164

165+
159166
##
160167
# FUNCTION
161168
# assert_hasarg_psst <func> <arg> [<value>]
@@ -176,58 +183,12 @@ assert_maxargc_psst()
176183
#
177184
assert_hasarg_psst()
178185
{
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-
197186
if { [ $# = 3 ] && [ -z "$3" ] ; } \
198187
|| { [ $# = 2 ] && [ -z "$( eval "printf '%s' \"\${$2-}\"" )" ] ; }
199188
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!"
203193
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"
233194
}

0 commit comments

Comments
 (0)