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

Add ShouldJSONEqual assertion #676

Merged
merged 8 commits into from
Jul 19, 2023

Conversation

philippgille
Copy link
Contributor

This implements ShouldJSONEqual, which is one of the requested features in #633.

The naming can also be ShouldEqualJSON, but with ShouldTimeEqual already existing, the other one seems to be more consistent with that pattern.

@philippgille philippgille force-pushed the add-shouldjsonequal-assertion branch from 9013995 to 8d58f06 Compare June 1, 2023 15:55
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>
@philippgille philippgille force-pushed the add-shouldjsonequal-assertion branch from 7330120 to dbb11f5 Compare June 1, 2023 19:04
@philippgille
Copy link
Contributor Author

Sorry for the forced push, I forgot the commit sign-off again for the last two commits

@philippgille
Copy link
Contributor Author

@yesnault is there room in v1.2.0 for this, or too late?

@yesnault
Copy link
Member

yesnault commented Jun 8, 2023

@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{}:
Copy link
Member

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

Copy link
Contributor Author

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:

  1. Allow any of the JSON types here, leave users the choice between ShouldEqual/ShouldBeTrue etc and ShouldJSONEqual
  2. 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
  3. 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 of ShouldJSONArrayEqual '["foo", "bar", "baz"]', although not sure if that brings much value

Copy link
Member

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.

Copy link
Contributor Author

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

⚠️ JSON 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.

Copy link
Member

@yesnault yesnault Jun 23, 2023

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>
@philippgille philippgille requested a review from yesnault June 20, 2023 09:19
@yesnault yesnault merged commit 16b9b3b into ovh:master Jul 19, 2023
fokion pushed a commit to fokion/venom that referenced this pull request Aug 1, 2023
* 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>
ivan-velasco pushed a commit to socotra/venom that referenced this pull request Sep 20, 2023
* 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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants