Skip to content

False negatives on strings with newlines #128

Description

@axmrnv

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:

comparisons=(${(@)@:3})

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions