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

enhance Json assertions reuseability #3438

Merged
merged 15 commits into from
Apr 15, 2023
Merged

Conversation

abendt
Copy link
Contributor

@abendt abendt commented Mar 1, 2023

This is a suggested change to improve the reusability of the json matchers provided by kotest

Context

In a project we were using http4k and its associated kotest assertions. There is assertion shouldHaveBody that accepts a Matcher<String?> as parameter. Ideally we would have liked to used a matcher from kotests json module here:

request.shouldHaveBody(beEqualJson("expected")) // shouldHaveBody from http4k, beEqualJson from kotest

What we found is that the matchers from kotest are currently not very suitable for external re-use (JsonTree factory method is internal, parse method is internal, too much logic inside the shouldX functions).

Changes

We are proposing some changes here to allow easier re-use of the json matchers by external code:

  • move more of the matching logic into the beX matcher for easier re-use
  • re-named equalJson to beEqualJsonTree for consistency
  • introduce beEqualJson that works on strings
  • extract some more matchers (beJsonObject, beJsonArray, beJsonObject) from the corresponding shouldX assertions to make them re-usable

Open questions

  • it seems like there is no way to construct a JsonTree outside of this module (toJsonTree is internal). should beEqualJsonTree be made internal too?

@abendt abendt changed the title Json assertions reuse enhance Json assertions reuseability Mar 1, 2023
@Kantis Kantis self-requested a review March 2, 2023 14:38
@Kantis
Copy link
Member

Kantis commented Mar 2, 2023

I would like to keep the name equalJson over beEqualJson.
I think we can change equalJson from returning a Matcher<JsonTree> to a Matcher<String> , because as you say, there's no way to create a JsonTree outside of using the shouldX functions.

* rename beEqualJsonTree to equalJsonTree and make it private
* can old JsonTree related equalJson functions be removed?
@abendt
Copy link
Contributor Author

abendt commented Mar 3, 2023

I would like to keep the name equalJson over beEqualJson.
I think we can change equalJson from returning a Matcher<JsonTree> to a Matcher<String> , because as you say, there's no way to create a JsonTree outside of using the shouldX functions.

i have renamed beEqualJson to equalJson and renamed beEqualJsonTree to equalJsonTree and made it private.
can i remove the old JsonTree related equalJson functions? they are not used within the codebase and are most likely not used by external code.

@sksamuel
Copy link
Member

sksamuel commented Apr 2, 2023

@Kantis you happy with this to be merged?
I think we should merge this before my PR to deprecate some older json matchers.

Copy link
Member

@Kantis Kantis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! Sorry for taking so long @abendt

@Kantis Kantis enabled auto-merge (squash) April 4, 2023 05:26
@Kantis Kantis merged commit 2573657 into kotest:master Apr 15, 2023
17 checks passed
@abendt abendt deleted the json-assertions-reuse branch April 16, 2023 09:59
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.

None yet

3 participants