The issue
Zunit produces false negative results when comparing strings containing newline characters. For example, this tiny test:
@test 'compare strings with newlines' {
assert "a\nb" same_as "a\nb"
}
fails:
✘ compare strings with newlines
'a
b' is not the same as 'a'
Why does it happen?
The problem is not related to the internal _zunit_assert_same_as or _zunit_assert_different_to functions; they work as expected if you call them directly.
The problem is in how the assert function passes the arguments to _zunit_assert_same_as function. First, it gets its parameters starting from the third, and stores them in the comparisons array:
Then, it runs the internal assertion function with the comparisons array as a parameter like that:
"_zunit_assert_${assertion}" "$value" ${(@f)comparisons[@]}
Here, each element of the comparisons array is passed to the _zunit_assert_same_as as a separate argument, splitting the arguments by newlines (because of the f flag). Since the comparisons values are split by newlines and _zunit_assert_same_as supports only two arguments, it receives only part of the string before the first newline.
As a result, the same_as assertion (and maybe others as well) doesn't support strings with newlines. I believe this behavior is incorrect because newline characters are valid string characters that can appear in strings, and string-oriented assertions should support them.
The issue
Zunit produces false negative results when comparing strings containing newline characters. For example, this tiny test:
fails:
Why does it happen?
The problem is not related to the internal _zunit_assert_same_as or _zunit_assert_different_to functions; they work as expected if you call them directly.
The problem is in how the assert function passes the arguments to
_zunit_assert_same_asfunction. First, it gets its parameters starting from the third, and stores them in thecomparisonsarray:comparisons=(${(@)@:3})Then, it runs the internal assertion function with the
comparisonsarray as a parameter like that:Here, each element of the
comparisonsarray is passed to the_zunit_assert_same_asas a separate argument, splitting the arguments by newlines (because of thefflag). Since thecomparisonsvalues are split by newlines and_zunit_assert_same_assupports only two arguments, it receives only part of the string before the first newline.As a result, the
same_asassertion (and maybe others as well) doesn't support strings with newlines. I believe this behavior is incorrect because newline characters are valid string characters that can appear in strings, and string-oriented assertions should support them.