Closed pschuegr closed 6 years ago
Could you clarify a few things?
Yup, I should have been a bit more verbose...
actualJSON := `{"a":1, "b":2}`
expectedJSON := `{"b":2, "a":1}`
So(actualJSON, ShouldEqualJSON, expectedJSON)
map[string]interface{}
, and then marshal them again and compare (marshalling of map keys happens in a specific order https://golang.org/src/encoding/json/encode.go#L138). If anything goes wrong during this process it fails.Thoughts?
Edit: it occurs to me that arrays, strings, and numbers are also valid JSON, so there might be a better name for it (ShouldEqualJSONObject?). If you think this would be worth including then we could discuss.
Ah, now I see--comparing JSON strings regardless of key ordering. Thanks for the clarification.
Are your JSON strings coming from your own structs that were marshaled? (If so, you could compare the structs directly using ShouldResemble.) It almost feels like this is a variation on ShouldResemble
...
I have a concern about depending on the current standard-library implementation of JSON key sorting. If that were to change down the road, the assertion could break. I'd recommend just doing a ShouldResemble
on the map[string]interface{}
values, but there have been issues reported regarding that kind of usage of ShouldResemble
so I'm not sure if the suggestion is sound.
I'm actually using it more to compare strings directly, rather than structs that are already unmarshalled, so I think it's kind of a different use case? Maybe there's a better way to do this, but I'm using it to compare json responses from an api.
expectedRequestBody := `{"my":"json","format":"!"}`
c.So(body, ShouldEqualJSON, expectedRequestBody)
I can understand the concern with depending on the stlib sorting - but it would only break if they decided to enforce a non-stable sort or a random ordering on the marshalling. Which I suppose is a reasonable concern - ultimately your call. Feel free to close this issue if you think it's not worth it :)
Cheers
Ok, I'm starting to understand the use case. Is this a case where you don't ever plan to define a go struct into which you would unmarshal the data? Because if you did have such a struct definition it would be simple to unmarshal and compare with another (expected) instance of the same struct using ShouldResemble
. That's the approach we've employed when needing to assert on data coming from a JSON source. Thoughts?
Sorry, was on vacation for a bit. I'll give this approach a try and report back if I still think this would provide value.
@mdwhatcott I'm testing a custom JSON Marshaler comparing the output against json files stored on disk.
Yes, I can do a ShouldResemble on the map[string]interface{}
, but the diff will not look good.
@wedneyyuri - thanks for the feedback.
I'd welcome a pull request representing a draft of this assertion, including test cases that specify its basic behavior. Doesn't need to be production-grade just yet, more a proof-of-concept at this point. @pschuegr
Would you accept a PR for a "ShouldEqualJSON" assertion? I find myself writing/using this assertion frequently...