-
Notifications
You must be signed in to change notification settings - Fork 144
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
Add ShouldJSONEqual assertion #676
Add ShouldJSONEqual assertion #676
Conversation
9013995
to
8d58f06
Compare
Signed-off-by: Philipp Gillé <philipp@heetch.com>
Signed-off-by: Philipp Gillé <philipp@heetch.com>
Signed-off-by: Philipp Gillé <philipp@heetch.com>
Signed-off-by: Philipp Gillé <philipp@heetch.com>
7330120
to
dbb11f5
Compare
Sorry for the forced push, I forgot the commit sign-off again for the last two commits |
@yesnault is there room in v1.2.0 for this, or too late? |
Hi @philippgille , thank you for this PR, it's a great new feature. No problem, it could be integrated into the 1.2.0 version. |
var actualSlice []interface{} | ||
var obj bool | ||
switch actual.(type) { | ||
case map[string]interface{}: |
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 that the parser to detect the json could be more json compliant. There is a check for {
or [
, but a simple text or number is also a valid number, see all values on https://www.json.org/json-en.html
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.
You're right, an older spec restricted it to objects and arrays, but the latest spec doesn't: https://stackoverflow.com/questions/7487869/is-this-simple-string-considered-valid-json
It's a good point to be compliant to the latest spec, but I also wonder when someone writes an assertion for a string or bool, wouldn't it be preferable to use ShouldEqual "foo"
, ShouldBeTrue
/ShouldBeFalse
etc to be more precise? This could be documented to make clear.
What would you prefer:
- Allow any of the JSON types here, leave users the choice between ShouldEqual/ShouldBeTrue etc and ShouldJSONEqual
- Allow only objects and arrays and clearly document this in the README (the assertion list)
⚠️ Which makes me realize I didn't add the new keyword to that list yet, I'll do that when you decided on one of the options
- Change the name to even split between object/array to be precise, which also makes the implementation simpler / more concise:
ShouldJSONObjectEqual
+ShouldJSONArrayEqual
- One benefit could be to even allow an actual list as param to check against, like
ShouldJSONArrayEqual foo bar baz
instead ofShouldJSONArrayEqual '["foo", "bar", "baz"]'
, although not sure if that brings much value
- One benefit could be to even allow an actual list as param to check against, like
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 prefer the first proposal: let the choice to the user and having the ShouldJSONEqual supporting the different types. Maybe not the easiest to implement, but definitely the easiest for users to understand if we say that ShouldJSONEqual supports the full JSON spec.
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 👍
Done in 5c05103
null
are tricky, because Venom passes it as empty string and not Go's nil
. Haven't checked if this is a fundamental limitation in the upstream callers in Venom, or if it can be changed. Do you know maybe?
And added to README in ba3769c
Looking at the assertion list in the README it's a bit messy, I think it would make sense to sort the assertions alphabetically. But then the ShouldJSONEqual
feels misplaced. => WDYT about naming it ShouldEqualJSON
like originally proposed in #633, and then for consistency create ShouldEqualTime
as an alias for ShouldTimeEqual
, which is required for backward compatibility. The latter could be marked as deprecated in the README.
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.
About naming, ShouldEqualJSON
is ok too, perhaps we can add some alias in another pr than here if needed. Thank you for adding different json values.
LGTM
Signed-off-by: Philipp Gillé <philipp@heetch.com>
Signed-off-by: Philipp Gillé <philipp@heetch.com>
* Add ShouldJSONEqual assertion Signed-off-by: Philipp Gillé <philipp@heetch.com> * Fix example in Godoc Signed-off-by: Philipp Gillé <philipp@heetch.com> * Improve Godoc Signed-off-by: Philipp Gillé <philipp@heetch.com> * Add to test suite Signed-off-by: Philipp Gillé <philipp@heetch.com> * Add ShouldJSONEqual to README Signed-off-by: Philipp Gillé <philipp@heetch.com> * Support primitive JSON values Signed-off-by: Philipp Gillé <philipp@heetch.com> --------- Signed-off-by: Philipp Gillé <philipp@heetch.com> Signed-off-by: Fokion Sotiropoulos <fokion.s@gmail.com>
* Add ShouldJSONEqual assertion Signed-off-by: Philipp Gillé <philipp@heetch.com> * Fix example in Godoc Signed-off-by: Philipp Gillé <philipp@heetch.com> * Improve Godoc Signed-off-by: Philipp Gillé <philipp@heetch.com> * Add to test suite Signed-off-by: Philipp Gillé <philipp@heetch.com> * Add ShouldJSONEqual to README Signed-off-by: Philipp Gillé <philipp@heetch.com> * Support primitive JSON values Signed-off-by: Philipp Gillé <philipp@heetch.com> --------- Signed-off-by: Philipp Gillé <philipp@heetch.com> Signed-off-by: Ivan Velasco <ivan.velasco@socotra.com>
This implements
ShouldJSONEqual
, which is one of the requested features in #633.The naming can also be
ShouldEqualJSON
, but withShouldTimeEqual
already existing, the other one seems to be more consistent with that pattern.