[03/21] tests/lib.sh: Add function test_that_key_in_arry_has_value

Message ID 20240520090611.10406-4-jonatan.schlag@ipfire.org
State New
Headers
Series [01/21] test: Add bash lib for colors |

Commit Message

Jonatan Schlag May 20, 2024, 9:05 a.m. UTC
  I think the name says it all.

Signed-off-by: Jonatan Schlag <jonatan.schlag@ipfire.org>
---
 tests/lib.sh | 14 ++++++++++++++
 1 file changed, 14 insertions(+)
  

Comments

Michael Tremer May 31, 2024, 9:48 a.m. UTC | #1
> On 20 May 2024, at 10:05, Jonatan Schlag <jonatan.schlag@ipfire.org> wrote:
> 
> I think the name says it all.
> 
> Signed-off-by: Jonatan Schlag <jonatan.schlag@ipfire.org>
> ---
> tests/lib.sh | 14 ++++++++++++++
> 1 file changed, 14 insertions(+)
> 
> diff --git a/tests/lib.sh b/tests/lib.sh
> index 7749d5158..c51257e79 100644
> --- a/tests/lib.sh
> +++ b/tests/lib.sh
> @@ -18,3 +18,17 @@ test_that() {
> var_has_value() {
> [[ "${!1}" == "${2}" ]]
> }
> +
> +test_that_key_in_arry_has_value() {

This is again very long and has a typo.

> + local array="${!1}"
> + local key="${2}"
> + local value="${3}"
> +
> + if [[ "${array[${key}]}" == "${value}" ]] ; then
> + echo -e "${CLR_GREEN_BG}Test succeded: The array '${1}' contains the value '${value}' under the key '${key}' ${CLR_RESET}"
> + return 0
> + else
> + echo -e "${CLR_RED_BG}Test failed: The array '${1}' contains the value '${array[${key}]}' under the key '${key} and not '${value}' ${CLR_RESET}"
> + return 1
> + fi
> +}
> -- 
> 2.39.2
>
  

Patch

diff --git a/tests/lib.sh b/tests/lib.sh
index 7749d5158..c51257e79 100644
--- a/tests/lib.sh
+++ b/tests/lib.sh
@@ -18,3 +18,17 @@  test_that() {
 var_has_value() {
 	[[ "${!1}" == "${2}" ]]
 }
+
+test_that_key_in_arry_has_value() {
+	local array="${!1}"
+	local key="${2}"
+	local value="${3}"
+
+	if [[ "${array[${key}]}" == "${value}" ]] ; then
+		echo -e "${CLR_GREEN_BG}Test succeded: The array '${1}' contains the value '${value}' under the key '${key}' ${CLR_RESET}"
+		return 0
+	else
+		echo -e "${CLR_RED_BG}Test failed: The array '${1}' contains the value '${array[${key}]}' under the key '${key} and not '${value}' ${CLR_RESET}"
+		return 1
+	fi
+}