commit:     f76dddb6a9e66ba3ffcb44822af0016d2b158b25
Author:     Kerin Millar <kfm <AT> plushkava <DOT> net>
AuthorDate: Thu Aug  8 08:10:32 2024 +0000
Commit:     Sam James <sam <AT> gentoo <DOT> org>
CommitDate: Sun Aug 11 10:10:58 2024 +0000
URL:        
https://gitweb.gentoo.org/proj/gentoo-functions.git/commit/?id=f76dddb6

test-functions: have three tests employ callback functions

Convert test_local(), test_ebegin() and test_quote_args() so as to
declare and use callbacks, just like the other tests. An appreciable
code cleanup is the result.

Signed-off-by: Kerin Millar <kfm <AT> plushkava.net>
Signed-off-by: Sam James <sam <AT> gentoo.org>

 test-functions | 99 ++++++++++++++++++++++++++++------------------------------
 1 file changed, 48 insertions(+), 51 deletions(-)

diff --git a/test-functions b/test-functions
index 0b0e127..79c66a9 100755
--- a/test-functions
+++ b/test-functions
@@ -24,26 +24,27 @@ cleanup_tmpdir() {
 }
 
 test_local() {
-       (
-               var=1
-               f() {
-                       local var=2
-                       g
-                       test "${var}" = 3 || exit
-               }
-               g() {
-                       test "${var}" = 2 || exit
-                       var=3
-               }
-               f
-               test "${var}" = 1
-       ) 2>/dev/null
-       retval=$?
-       if [ "${retval}" -ne 0 ]; then
-               printf 'not '
-       fi
-       printf 'ok %d - /bin/sh supports local\n' "$((testnum += 1))"
-       return "${retval}"
+       set -- eq 0
+
+       callback() {
+               test_description="/bin/sh supports local"
+               (
+                       var=1
+                       f() {
+                               local var=2
+                               g
+                               test "${var}" = 3 || exit
+                       }
+                       g() {
+                               test "${var}" = 2 || exit
+                               var=3
+                       }
+                       f
+                       test "${var}" = 1
+               ) 2>/dev/null
+       }
+
+       iterate_tests 2 "$@"
 }
 
 test_chdir() {
@@ -104,21 +105,18 @@ test_die() {
 }
 
 test_ebegin() {
-       _eprint() {
-               shift
-               _ends_with_newline "$*"
-       }
-
-       set -- "message"
-       ebegin "$1"
-       retval=$?
+       set -- eq 0
 
-       if [ "${retval}" -ne 0 ]; then
-               printf 'not '
-       fi
-       printf 'ok %d - ebegin %s (expecting terminating newline)\n' 
"$((testnum + 1))" "$1"
+       callback() (
+               test_description="ebegin message (expecting terminating 
newline)"
+               _eprint() {
+                       shift
+                       _ends_with_newline "$*"
+               }
+               ebegin "message"
+       )
 
-       return "${retval}"
+       iterate_tests 2 "$@"
 }
 
 test_edo() {
@@ -926,25 +924,24 @@ test_contains_any() {
 }
 
 test_quote_args() {
-       local POSIXLY_CORRECT
+       set -- eq 0
 
-       testnum=$((testnum + 1))
-       retval=0
-       i=0
-       while [ "$(( i += 1 ))" -le 255 ]; do
-               fmt=$(printf '\\%o' "$i")
-               # shellcheck disable=2059
-               str=$(printf "$fmt.")
-               quote_args "${str%.}" || break
-       done | cksum | {
-               read -r cksum _
-               if [ "${cksum}" != "380900690" ]; then
-                       printf 'not '
-                       retval=1
-               fi
-               printf 'ok %d - quote_args output test (expected cksum 
380900690, got %s)\n' "${testnum}" "${cksum}"
-               return "${retval}"
+       callback() {
+               local POSIXLY_CORRECT i
+
+               test_description="quote_args output test (expecting cksum 
380900690)"
+               i=0
+               while [ "$((i += 1))" -le 255 ]; do
+                       fmt=$(printf '\\%o' "$i")
+                       # shellcheck disable=2059
+                       str=$(printf "$fmt.")
+                       quote_args "${str%.}" || break
+               done \
+               | cksum \
+               | { read -r cksum _ && test "${cksum}" = "380900690"; }
        }
+
+       iterate_tests 2 "$@"
 }
 
 test_assign() {
@@ -1096,7 +1093,7 @@ elif ! GENFUN_MODULES="portage rc" . ./functions.sh; then
 else
        assign_tmpdir
        test_chdir || rc=1
-       ( test_ebegin ) || rc=1; testnum=$((testnum + 1))
+       test_ebegin || rc=1; testnum=$((testnum + 1))
        test_is_older_than || rc=1
        test_get_bootparam || rc=1
        test_esyslog || rc=1

Reply via email to