Skip to content

More granular failure messages for collection comparisons, arrays in particular #440

@delebedev

Description

@delebedev

Consider the following examples:

expect([1,2,3,4]).to(equal([1,2,3,4,5]))
// produces "expected to equal <[1, 2, 3, 4, 5]>, got <[1, 2, 3, 4]>"

It is kinda readable, but the thing is that in my experience arrays could contain long strings and / or enums with associated values, and it makes it impossible to understand what actually went wrong.

In this particular case failure message could be "expected array to have 5 elements, but got 4".

Another case:

expect([1,2,3,4]).to(equal([1,4,3,4])
// produces "expected to equal <[1, 4, 3, 4]>, got <[1, 2, 3, 4]>"

Which is again, readable but only if we operate with short values.

In this case message could be "expected to have "4" at index 1, got "2"".

Of course, one could resort to per-element comparison, but it does not feel right (or, maybe I'm just overthinking, please let me know)

guard array.count == 3 else {
  fail("blabla")
}// we need to make sure that we don't overflow in per-index comparison

expect(array[0]) == x
expect(array[1]) == y
expect(array[2]) == z

Exact messages text could be reconsidered, but I hope you get the idea.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions