Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

If the actual value is "", then assertEquals() reports expected/actual incorrectly #49

Closed
jbrains opened this issue Feb 5, 2016 · 1 comment
Assignees

Comments

@jbrains
Copy link

jbrains commented Feb 5, 2016

It looks like assertEquals() can't tell the difference between "parameter 3 is not set, so assume that parameter 1 is the expected result and parameter 2 is the actual result" and "parameter 3 is set, but empty, so assume that parameter 1 is the failure message, parameter 2 is the expected result, and parameter 3 is the actual result".

I don't know bash deeply enough to know whether fixing this problem creates others. It seems that putting the failure message at the end would make the whole problem go away, even though it would annoy clients. Maybe for shunit3?

@kward
Copy link
Owner

kward commented Sep 24, 2017

This looks similar to #53, and the fix is to wrap your expected output in " chars.

I'm guessing you were doing something like this (script saved as test_equals.sh).

#!/bin/bash
cmd() { echo ''; }

test_cmd1() {
  assertEquals "cmd() failed" "" $(cmd)
}
test_cmd2() {
  output=$(cmd)
  assertEquals "cmd() failed" "" ${output}
}

. ~/lib/sh/shunit2

This will produce the behavior you are seeing.

$ ./test_equals.sh 
test_cmd1
ASSERT:expected:<cmd() failed> but was:<>
test_cmd2
ASSERT:expected:<cmd() failed> but was:<>

Ran 2 tests.

FAILED (failures=2)

Wrapping the output fixes the problem in both cases.

#!/bin/bash
cmd() { echo ''; }

test_cmd1() {
  assertEquals "cmd() failed" "" "$(cmd)"
}
test_cmd2() {
  output=$(cmd)
  assertEquals "cmd() failed" "" "${output}"
}

. ~/lib/sh/shunit2
$ ./test_equals.sh 
test_cmd1
test_cmd2

Ran 2 tests.

OK

@kward kward closed this as completed Sep 24, 2017
@kward kward self-assigned this Jan 9, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants