-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Json matching function #1690
Json matching function #1690
Conversation
@@ -3,6 +3,7 @@ | |||
package assert | |||
|
|||
import ( | |||
jsonmatch "github.com/stretchr/testify/assert/jsonmatch" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fix the import order please
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is what is meant? bdff014
@@ -3,6 +3,7 @@ | |||
package assert | |||
|
|||
import ( | |||
jsonmatch "github.com/stretchr/testify/assert/jsonmatch" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fix the imports order please
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what do you mean by the order actually? like alphabetically by package name?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
also here: bdff014
assert/assertions.go
Outdated
@@ -1703,7 +1689,6 @@ func NotRegexp(t TestingT, rx interface{}, str interface{}, msgAndArgs ...interf | |||
} | |||
|
|||
return !match | |||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These files might need formating for sure, but I would encourage you to revert all these spaces changes. It mess up the history of the files.
If you want to format them, why not, but then please open a PR about it or use a separate commit in your current PR
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
makes sense, undid the auto formatting: bdff014
assert/assertions_test.go
Outdated
@@ -842,11 +829,9 @@ func TestNil(t *testing.T) { | |||
if Nil(mockT, new(AssertionTesterConformingObject)) { | |||
t.Error("Nil should return false: object is not nil") | |||
} | |||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here also your formatter committed so many changes
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oops, i'll adjust this
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
removed: bdff014
assert/assertions_test.go
Outdated
var cases = []struct { | ||
cases := []struct { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm pretty sure you are using gofumpt as I do 😁
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hehe, indeed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
also here: bdff014
case float64: | ||
n.kind = Number | ||
n.value = v |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about json.Number?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
instead of float64 for instance?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not opposed to it, but what benefit would it provide?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm sorry if I didn't explain it well.
Of course, the float64 case is needed.
My point was about the fact, and maybe I'm wrong, this function could receive data that was, or will be, decoded with UseNumber
https://pkg.go.dev/encoding/json#Decoder.UseNumber
But maybe I'm wrong and it cannot happen
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ahh, i see. I think that would have been caught by this test here: https://github.com/stretchr/testify/pull/1690/files#diff-7190065248695df477329c4cada2d71f323ab7940ecffff2d3d5fa43caf79427R2454
assert "github.com/stretchr/testify/assert" | ||
jsonmatch "github.com/stretchr/testify/assert/jsonmatch" | ||
http "net/http" | ||
url "net/url" | ||
time "time" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please fix these imports please
Yes existing ones are already messed up
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
adjusted: bdff014
assert "github.com/stretchr/testify/assert" | ||
jsonmatch "github.com/stretchr/testify/assert/jsonmatch" | ||
http "net/http" | ||
url "net/url" | ||
time "time" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
f199b16
to
80861e6
Compare
80861e6
to
bdff014
Compare
assert/assertion_format.go
Outdated
import ( | ||
http "net/http" | ||
jsonmatch "github.com/stretchr/testify/assert/jsonmatch" | ||
url "net/url" | ||
time "time" | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The canonical way is this
import ( | |
http "net/http" | |
jsonmatch "github.com/stretchr/testify/assert/jsonmatch" | |
url "net/url" | |
time "time" | |
) | |
import ( | |
http "net/http" | |
url "net/url" | |
time "time" | |
jsonmatch "github.com/stretchr/testify/assert/jsonmatch" | |
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ah, i see. fixed then!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
looks like go generate doesn't let me fix the import order: https://github.com/stretchr/testify/actions/runs/12549993792/job/34992001258?pr=1690
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh indeed then
The files are generated. I assume there is something to improve in the script that generate them. But then it's out of the scope of this PR.
Thank you for trying, and I'm sorry if you had to undo
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no prob, thanks for the review!
3cf1fa9
to
ecbed80
Compare
thanks @ccoVeille for your review and suggested changes! If you have a chance to look over it as well, I'd be grateful to be able to merge it to the main package @boyan-soubachov @dolmen @MovieStoreGuy @arjunmahishi or @brackendawson and also for an idea when/how we might be able to release it. thanks for the help! |
This seems to be an interesting feature, but at this time we do not want to increase the API surface of this framework which is already too huge for the limited maintainers resources. |
Summary
This enables flexible json matching based on matcher functions in addition to json equality checks.
Changes
Motivation
My colleagues and I from every go based team I've worked with have always had difficulty with json. Oftentimes we want to check aspects of certain parts of JSON objects without having to do a full equality check. For instance, determining that a value is a uuid format, or that a datetime is in the past etc etc. While this can be done in other ways such as unmarshalling an object and checking individual values, giving an overview in the form of a json object can be much more clear.
an exmple from one of the tests:
The only issue I'd like to still solve in another PR is adjusting the display of the diff to factor in matchers. I think it would be better to solve that problem separately.
Related issues