commit: 53862bd97274363393c35f192763eddc0f2b351d
Author: Kerin Millar <kfm <AT> plushkava <DOT> net>
AuthorDate: Wed Feb 8 02:01:48 2023 +0000
Commit: Sam James <sam <AT> gentoo <DOT> org>
CommitDate: Wed Feb 8 03:37:34 2023 +0000
URL:
https://gitweb.gentoo.org/proj/gentoo-functions.git/commit/?id=53862bd9
test-functions: Reduce the boilerplate required for new tests
Test functions may now define a callback() function before calling
iterate_tests() to iterate over the tests defined by the positional
parameters. The callback() function is responsible for performing a
single given test, and for setting the description variable.
Signed-off-by: Kerin Millar <kfm <AT> plushkava.net>
Signed-off-by: Sam James <sam <AT> gentoo.org>
test-functions | 57 +++++++++++++++++++++++++++++++--------------------------
1 file changed, 31 insertions(+), 26 deletions(-)
diff --git a/test-functions b/test-functions
index f6985ef..d6471ed 100755
--- a/test-functions
+++ b/test-functions
@@ -87,28 +87,17 @@ test_is_older_than() {
tstamp=197001010100 # add an hour
done
- passed=0
- total=$(( $# / 3 ))
- i=1
- printf '%d..%d\n' "${i}" "${total}"
- while [ "${i}" -le "${total}" ]; do
+ callback() {
if [ "$2" != "N/A" ] && [ "$3" != "N/A" ]; then
- desc="is_older_than $2 $3 (expecting $1)"
+ description="is_older_than $2 $3"
is_older_than "$2" "$3"
else
- desc="is_older_than (expecting $1)"
+ description="is_older_than"
is_older_than
fi
- if [ "$?" -eq "$1" ]; then
- passed=$((passed + 1))
- else
- printf 'not '
- fi
- printf 'ok %d - %s\n' "${i}" "${desc}"
- i=$((i + 1))
- shift 3
- done
- return "$(( passed < total ))"
+ }
+
+ iterate_tests 3 "$@"
}
test_get_bootparam() {
@@ -127,26 +116,42 @@ test_get_bootparam() {
0 gentoo=1 "foo gentoo=gentoo=1,bar baz" \
1 quux "${cmdline}"
- passed=0
- total=$(( $# / 3 ))
- i=1
- printf '%d..%d\n' "${i}" "${total}"
- while [ "${i}" -le "${total}" ]; do
+ callback() {
if [ "$2" = "N/A" ]; then
- desc="get_bootparam (expecting $1)"
+ description="get_bootparam"
printf '%s\n' "$3" | get_bootparam
else
- desc="get_bootparam \"$2\" (expecting $1)"
+ description="get_bootparam \"$2\""
printf '%s\n' "$3" | get_bootparam "$2"
fi
+ }
+
+ iterate_tests 3 "$@"
+}
+
+iterate_tests() {
+ slice_width=$1
+ shift
+
+ total=$(( $# / slice_width ))
+ printf '1..%d\n' "${total}"
+ code="callback"
+ i=0
+ while [ "$((i += 1))" -le "${slice_width}" ]; do
+ code="${code} \"\$${i}\""
+ done
+ passed=0
+ i=1
+ while [ "${i}" -le "${total}" ]; do
+ eval "${code}"
if [ "$?" -eq "$1" ]; then
passed=$((passed + 1))
else
printf 'not '
fi
- printf 'ok %d - %s\n' "${i}" "${desc}"
+ printf 'ok %d - %s\n' "${i}" "${description} (expecting $1)"
i=$((i + 1))
- shift 3
+ shift "${slice_width}"
done
return "$(( passed < total ))"
}