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

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

Closed
yinonavraham opened this issue Aug 18, 2022 · 3 comments · Fixed by #40
Closed

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

yinonavraham opened this issue Aug 18, 2022 · 3 comments · Fixed by #40
Labels
bug Something isn't working

Comments

@yinonavraham
Copy link

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

  • Output from go version: go version go1.18.2 darwin/arm64
  • Version of this package: v.1.1.0

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.

@yinonavraham yinonavraham added the bug Something isn't working label Aug 18, 2022
yinonavraham added a commit to yinonavraham/jsonassert that referenced this issue Aug 18, 2022
Signed-off-by: Yinon Avraham <yinonavraham@gmail.com>
kinbiko added a commit that referenced this issue Aug 18, 2022
@kinbiko
Copy link
Owner

kinbiko commented Aug 18, 2022

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

@kinbiko
Copy link
Owner

kinbiko commented Aug 18, 2022

Fixed in the newly released v1.1.1

@yinonavraham
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants