From fb239fd7d2b2fb740c2887714b57ae740ee7bac7 Mon Sep 17 00:00:00 2001 From: miguel Date: Mon, 9 Jan 2023 14:31:51 +0000 Subject: [PATCH] fix(comparator):fixing test and adding info to the readme --- sh/tests/comparator_test.sh | 18 +++++++++++++++--- sh/tests/solutions/comparator.sh | 6 +++++- subjects/devops/comparator/README.md | 2 +- 3 files changed, 21 insertions(+), 5 deletions(-) diff --git a/sh/tests/comparator_test.sh b/sh/tests/comparator_test.sh index 8f0bb9e6..85442ea6 100755 --- a/sh/tests/comparator_test.sh +++ b/sh/tests/comparator_test.sh @@ -4,13 +4,22 @@ set -euo pipefail IFS=' ' + +# Test if there were only two arguments given + script_dirS=$(cd -P "$(dirname "$BASH_SOURCE")" &>/dev/null && pwd) challenge() { - submitted=$(bash "$script_dirS"/student/comparator.sh $1 $2) - expected=$(bash "$script_dirS"/solutions/comparator.sh $1 $2) - diff <(echo "$submitted") <(echo "$expected") + if [ $# -ne 2 ]; then + submitted=$(bash "$script_dirS"/student/comparator.sh $1) + expected=$(bash "$script_dirS"/solutions/comparator.sh $1) + else + submitted=$(bash "$script_dirS"/student/comparator.sh $1 $2) + expected=$(bash "$script_dirS"/solutions/comparator.sh $1 $2) + diff <(echo "$submitted") <(echo "$expected") + fi } + for i in $(seq 1 10); do n1=$(shuf -i 1-20 -n 1) n2=$(shuf -i 1-30 -n 1) @@ -20,3 +29,6 @@ done challenge "0" "0" challenge "10" "10" challenge "-11" "-11" +challenge "14" +challenge "-11" "-11" "4" +challenge "as" "str" diff --git a/sh/tests/solutions/comparator.sh b/sh/tests/solutions/comparator.sh index 891748c5..6f238ca9 100755 --- a/sh/tests/solutions/comparator.sh +++ b/sh/tests/solutions/comparator.sh @@ -1,6 +1,10 @@ #!/usr/bin/env bash -if [ "$1" -gt "$2" ]; then +if [ "$#" -ne 2 ]; then + echo "Error: script requires two arguments to run" +elif ! [[ $1 =~ ^-?[0-9]*\.?[0-9]+$ ]] || ! [[ $2 =~ ^-?[0-9]*\.?[0-9]+$ ]]; then + echo "Error: Only two numeric arguments are acceptable" +elif [ "$1" -gt "$2" ]; then echo "$1 > $2" elif [ "$1" -lt "$2" ]; then echo "$1 < $2" diff --git a/subjects/devops/comparator/README.md b/subjects/devops/comparator/README.md index f4fafab0..9016bdae 100644 --- a/subjects/devops/comparator/README.md +++ b/subjects/devops/comparator/README.md @@ -2,7 +2,7 @@ ### Instructions -Create a script `comparator.sh` which will verify if the first given argument is bigger, smaller or equal than the second given argument. +Create a script `comparator.sh` which will accept only two numbers as arguments and it will verify if the first given argument is bigger, smaller or equal than the second given argument. You must print te output like the example in usage. ### Usage