Mercurial > hgrepos > FreeBSD > ports > sysutils > local-bsdtools
changeset 651:57ee25cec0dd
farray.sh: farray_istrue() and falist_istrue(): truth tests for arrays and alists.
The rules are as in Python: non-empty arrays/alists are truish, empty
arrays/alists are falsy.
| author | Franz Glasner <fzglas.hg@dom66.de> |
|---|---|
| date | Fri, 27 Sep 2024 22:45:45 +0200 |
| parents | 5a367d44b480 |
| children | 39a7594b45f9 |
| files | share/local-bsdtools/farray.sh tests/farray-alist.t tests/farray-array.t |
| diffstat | 3 files changed, 67 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- a/share/local-bsdtools/farray.sh Fri Sep 27 21:23:19 2024 +0200 +++ b/share/local-bsdtools/farray.sh Fri Sep 27 22:45:45 2024 +0200 @@ -974,6 +974,30 @@ #: +#: Test the boolean truth of an array. +#: +#: Args: +#: $1 (str): The name of the array. +#: +#: Returns: +#: int: 0 (truthy) if the array contains elements, +#: 1 (falsy) otherwise (empty or not created/destroyed properly). +#: +farray_istrue() { + local __farr_name + + local __farr_token __farr_gvrname __farr_len + + _farr_array_tryget_meta "$@" || return 1 + if [ "${__farr_len}" -gt 0 ]; then + return 0 + else + return 1 + fi +} + + +#: #: Determine whether any of given values is found within the array. #: #: Args: @@ -2055,6 +2079,30 @@ #: +#: Test the boolean truth of an alist. +#: +#: Args: +#: $1 (str): The name of the alist. +#: +#: Returns: +#: int: 0 (truthy) if the alist contains items, +#: 1 (falsy) otherwise (empty or not created/destroyed properly). +#: +falist_istrue() { + local __farr_name + + local __farr_token __farr_objname __farr_keyname __farr_valname __farr_len + + _farr_alist_tryget_meta "$@" || return 1 + if [ "${__farr_len}" -gt 0 ]; then + return 0 + else + return 1 + fi +} + + +#: #: Test whether two alists are equal. #: #: Two alists compare equal if and only if they have the same (key, value)
--- a/tests/farray-alist.t Fri Sep 27 21:23:19 2024 +0200 +++ b/tests/farray-alist.t Fri Sep 27 22:45:45 2024 +0200 @@ -25,6 +25,8 @@ $ falist_print_length LIST 0 (no-eol) + $ falist_istrue LIST + [1] $ falist_debug LIST DEBUG: alist `LIST' has length 0 @@ -40,7 +42,10 @@ ===== $ falist_create LIST + $ falist_istrue LIST + [1] $ falist_set LIST K1 V1 + $ falist_istrue LIST $ falist_set LIST K2 V2 $ falist_debug LIST DEBUG: alist `LIST' has length 2 @@ -56,10 +61,15 @@ $ falist_length _i LIST $ echo "$_i" 0 + $ falist_istrue LIST + [1] $ falist_print_length LIST 0 (no-eol) $ falist_destroy LIST + $ falist_istrue LIST + ERROR: falist `LIST' not created properly: token empty + [1] $ check_no_alist_artifacts
--- a/tests/farray-array.t Fri Sep 27 21:23:19 2024 +0200 +++ b/tests/farray-array.t Fri Sep 27 22:45:45 2024 +0200 @@ -22,6 +22,8 @@ 0 (no-eol) $ farray_length _var EMPTY $ test ${_var} -eq 0 + $ farray_istrue EMPTY + [1] $ farray_debug EMPTY DEBUG: array `EMPTY' has length 0 $ farray_destroy EMPTY @@ -34,6 +36,7 @@ 5 (no-eol) $ farray_length _var TEST $ test ${_var} -eq 5 + $ farray_istrue TEST $ farray_debug TEST DEBUG: array `TEST' has length 5 DEBUG: its contents: @@ -48,6 +51,7 @@ Create, clear and destroy an array $ farray_create TEST 0 1 2 '3 4 5' $'" 678" \\\'90 ' + $ farray_istrue TEST $ farray_print_length TEST 5 (no-eol) $ farray_length _var TEST @@ -61,11 +65,16 @@ DEBUG: 4: `3 4 5' DEBUG: 5: `" 678" \'90 ' $ farray_clear TEST + $ farray_istrue TEST + [1] $ farray_print_length TEST 0 (no-eol) $ farray_length _var TEST $ test ${_var} -eq 0 $ farray_destroy TEST + $ farray_istrue TEST + ERROR: farray `TEST' not created properly: token empty + [1] $ check_no_array_artifacts Duplicate destruction
