testers.testEqualArrayOrMap: use arrayUtilities where possible

Signed-off-by: Connor Baker <ConnorBaker01@gmail.com>
This commit is contained in:
Connor Baker
2025-03-24 03:41:54 +00:00
parent 38a8232461
commit 4b80e5995e
3 changed files with 14 additions and 27 deletions

View File

@@ -1,11 +1,5 @@
# shellcheck shell=bash
# Tests if an array is declared.
isDeclaredArray() {
# shellcheck disable=SC2034
local -nr arrayRef="$1" && [[ ${!arrayRef@a} =~ a ]]
}
# Asserts that two arrays are equal, printing out differences if they are not.
# Does not short circuit on the first difference.
assertEqualArray() {
@@ -19,12 +13,12 @@ assertEqualArray() {
local -nr actualArrayRef="$2"
if ! isDeclaredArray "${!expectedArrayRef}"; then
nixErrorLog "first arugment expectedArrayRef must be an array reference to a declared array"
nixErrorLog "first argument expectedArrayRef must be a reference to an indexed array"
exit 1
fi
if ! isDeclaredArray "${!actualArrayRef}"; then
nixErrorLog "second arugment actualArrayRef must be an array reference to a declared array"
nixErrorLog "second argument actualArrayRef must be a reference to an indexed array"
exit 1
fi

View File

@@ -1,11 +1,5 @@
# shellcheck shell=bash
# Tests if a map is declared.
isDeclaredMap() {
# shellcheck disable=SC2034
local -nr mapRef="$1" && [[ ${!mapRef@a} =~ A ]]
}
# Asserts that two maps are equal, printing out differences if they are not.
# Does not short circuit on the first difference.
assertEqualMap() {
@@ -19,26 +13,15 @@ assertEqualMap() {
local -nr actualMapRef="$2"
if ! isDeclaredMap "${!expectedMapRef}"; then
nixErrorLog "first arugment expectedMapRef must be an associative array reference to a declared associative array"
nixErrorLog "first argument expectedMapRef must be a reference to an associative array"
exit 1
fi
if ! isDeclaredMap "${!actualMapRef}"; then
nixErrorLog "second arugment actualMapRef must be an associative array reference to a declared associative array"
nixErrorLog "second argument actualMapRef must be a reference to an associative array"
exit 1
fi
# NOTE:
# From the `sort` manpage: "The locale specified by the environment affects sort order. Set LC_ALL=C to get the
# traditional sort order that uses native byte values."
# We specify the environment variable in a subshell to avoid polluting the caller's environment.
local -a sortedExpectedKeys
mapfile -d '' -t sortedExpectedKeys < <(printf '%s\0' "${!expectedMapRef[@]}" | LC_ALL=C sort --stable --zero-terminated)
local -a sortedActualKeys
mapfile -d '' -t sortedActualKeys < <(printf '%s\0' "${!actualMapRef[@]}" | LC_ALL=C sort --stable --zero-terminated)
local -ir expectedLength=${#expectedMapRef[@]}
local -ir actualLength=${#actualMapRef[@]}
@@ -49,6 +32,12 @@ assertEqualMap() {
hasDiff=1
fi
local -a sortedExpectedKeys=()
getSortedMapKeys "${!expectedMapRef}" sortedExpectedKeys
local -a sortedActualKeys=()
getSortedMapKeys "${!actualMapRef}" sortedActualKeys
local -i expectedKeyIdx=0
local expectedKey
local expectedValue

View File

@@ -1,4 +1,5 @@
{
arrayUtilities,
lib,
stdenvNoCC,
}:
@@ -21,7 +22,10 @@ lib.makeOverridable (
inherit name;
nativeBuildInputs = [
arrayUtilities.isDeclaredArray
./assert-equal-array.sh
arrayUtilities.isDeclaredMap
arrayUtilities.getSortedMapKeys
./assert-equal-map.sh
];