kinbiko / jsonassert

A Go test assertion library for verifying that two representations of JSON are semantically equal
MIT License
127 stars 16 forks source link

[BUG] Fail to compare <<UNORDERED>> arrays where elements have <<PRESENCE>> #39

Closed yinonavraham closed 2 years ago

yinonavraham commented 2 years ago

What exactly did you do?

Compare <<UNORDERED>> JSON arrays where the elements are objects and the expectation for some fields only checks for <<PRESENCE>>.

Test example:

func TestUNORDEREDwithPRESENCE(t *testing.T) {
    act := `{
        "data": [
            { "foo": 1, "bar": 2 },
            { "foo": 11, "bar": 22 }
        ]
    }`
    exp := `{
        "data": [
            "<<UNORDERED>>",
            { "foo": 11, "bar": "<<PRESENCE>>" },
            { "foo": 1, "bar": "<<PRESENCE>>" }
        ]
    }`
    ja := jsonassert.New(t)
    ja.Assertf(act, exp)
}

See test examples with some sanity in this Go Playground.

What did you expect would happen?

The test should pass - I should be able to compare <<UNORDERED>> arrays having elements with <<PRESENCE>> in them.

What actually happened?

=== RUN   TestUNORDEREDwithPRESENCE
    prog.go:81: actual JSON at '$.data[0]' contained an unexpected element: {"bar":2,"foo":1}
    prog.go:81: actual JSON at '$.data[1]' contained an unexpected element: {"bar":22,"foo":11}
    prog.go:81: expected JSON at '$.data[0]':
        {"bar":"\u003c\u003cPRESENCE\u003e\u003e","foo":11}
        was missing from actual payload
    prog.go:81: expected JSON at '$.data[1]':
        {"bar":"\u003c\u003cPRESENCE\u003e\u003e","foo":1}
        was missing from actual payload
--- FAIL: TestUNORDEREDwithPRESENCE (0.00s)
FAIL

Additional info

As can be seen in the Go Playground example, I did some sanity tests, and it seems that <<UNORDERED>> and <<PRESENCE>> work well for the example above, except for when they are mixed together. Looking in the code, it seems to be caused by. the difference between Asserter.checkArrayUnordered and Asserter.checkArrayOrdered. The unordered version uses Asserter.deepEqual, whereas the ordered version uses Asserter.pathassertf.

kinbiko commented 2 years ago

@yinonavraham Thanks for reporting. I managed to reproduce the issue, and believe I've got a fix for it in #40.

kinbiko commented 2 years ago

Fixed in the newly released v1.1.1

yinonavraham commented 2 years ago

Awesome! Thanks for the quick response, works like a charm!