Browse Source

test(joker-num): add provisional tests

pull/1730/head
eslopfer 2 years ago committed by Michele
parent
commit
9025836ce8
  1. 51
      sh/tests/joker-num_test.sh
  2. 19
      sh/tests/solutions/joker-num.sh

51
sh/tests/joker-num_test.sh

@ -0,0 +1,51 @@
test_empty_input() {
output=$(echo -n "" | ./solutions/joker-num.sh)
expected_output="Error: Input is empty, please try again."
diff <(echo "$output") <(echo "$expected_output")
}
test_not_a_number() {
output=$(echo "a" | ./solutions/joker-num.sh)
expected_output="Error: Input is not a number, please try again."
diff <(echo "$output") <(echo "$expected_output")
}
test_number_out_of_range() {
output=$(echo "100001" | ./solutions/joker-num.sh)
expected_output="Error: Number out of range, please try again."
diff <(echo "$output") <(echo "$expected_output")
}
test_correct_guess() {
output=$(echo "50000" | ./solutions/joker-num.sh; echo "50000" | ./solutions/joker-num.sh)
expected_output="Congratulations! You guessed the number."
diff <(echo "$output") <(echo "$expected_output")
}
test_guess_too_low() {
output=$(echo "50000" | ./solutions/joker-num.sh; echo "49999" | ./solutions/joker-num.sh)
expected_output="Go up."
diff <(echo "$output") <(echo "$expected_output")
}
test_guess_too_high() {
output=$(echo "50000" | ./solutions/joker-num.sh; echo "50001" | ./solutions/joker-num.sh)
expected_output="Go down."
diff <(echo "$output") <(echo "$expected_output")
}
test_player_one() {
test_empty_input
test_not_a_number
test_number_out_of_range
}
test_player_2() {
test_empty_input
test_not_a_number
test_number_out_of_range
test_correct_guess
test_guess_too_low
test_guess_too_high
}
test_player_1
test_player_2

19
sh/tests/solutions/joker-num.sh

@ -4,29 +4,28 @@
for (( ; ; )) for (( ; ; ))
do do
echo "Player one, please enter a number between 1 and 100000 (inclusive) and press enter:" echo "Player one, please enter a number between 1 and 100000 (inclusive) and press enter:"
read -s number read number
# timeout 1s read -s number
# read -p "Player one, please enter a number between 1 and 100000 (inclusive) and press enter:" -s number
# sleep 5s
# Check if input is empty # Check if input is empty
if [[ -z "$number" ]] if [[ -z "$number" ]]
then then
echo "Error: Input is empty, please try again." echo "Error: Input is empty, please try again."
continue
fi
# Check if input is a number # Check if input is a number
if ! [[ "$number" =~ ^[0-9]+$ ]] elif ! [[ "$number" =~ ^[0-9]+$ ]]
then then
echo "Error: Input is not a number, please try again." echo "Error: Input is not a number, please try again."
continue
fi
# Check if input is between 1 and 100000 (inclusive) # Check if input is between 1 and 100000 (inclusive)
if [[ "$number" -lt 1 || "$number" -gt 100000 ]] elif [[ "$number" -lt 1 || "$number" -gt 100000 ]]
then then
echo "Error: Number out of range, please try again." echo "Error: Number out of range, please try again."
continue
else
break
fi fi
break
done done
# Start the for loop for player two # Start the for loop for player two

Loading…
Cancel
Save