Raindrops - test failures

Hello! Why do tests fail?

(( “$1” % 3 == 0 )) && echo -n “Pling”
(( “$1” % 5 == 0 )) && echo -n “Plang”
(( “$1” % 7 == 0 )) && echo -n “Plong”
(( “$1” % 3 != 0 && “$1” % 5 != 0 && “$1” % 7 != 0 )) && echo “$1”

Test Failure

(from function assert_success' in file bats-extra.bash, line 409, in test file raindrops.bats, line 16) assert_success’ failed

– command failed –
status : 1
output : Pling

etc

Your script is expected to “succeed”, ie have an exist status of 0. However, it has an exit status of 1, which is treated as a “command failed”.

What happens here if $1 is divisible by 3?

After having printed “Pling” from finding that the first argument was evenly divisible by 3 several lines eariler in the script, my guess with that particular line is that with short-circuit evaluation it would stop right there at the comparison “$1” % 3 != 0 && and not proceed with the next comparison, and go on to run the next following line in the script.

One very important point is missing in this explanation: The result code of the statement (the status of the command execution) is “failed” because the test expression was false. And as there is no “next line”, it becomes the result code of the script. It is this exit code, that triggered the failing of the test, not what was printed.