mirror of
https://github.com/CHN-beta/nixpkgs.git
synced 2026-01-12 02:40:31 +08:00
testers.testEqualArrayOrMap: use arrayUtilities where possible
Signed-off-by: Connor Baker <ConnorBaker01@gmail.com>
This commit is contained in:
@@ -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
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
];
|
||||
|
||||
|
||||
Reference in New Issue
Block a user