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

JsonApprovals.verifyAsJson fails when fields are not in the same order between tests #301

Closed
horiaconstantin-cpi opened this issue Sep 8, 2022 · 3 comments

Comments

@horiaconstantin-cpi
Copy link

Data that I use:
Map<String, String> test = new HashMap<>(); test.put("2test", "test"); test.put("1test", "test");
when I use JsonApprovals.verifyAsJson, it creates an output file as expected.
However, if I sort the keys in the map (test1, test2), the test fails. Since JSON field order is not mandatory, I think that the test should be passing.
What do you think?

@isidore
Copy link
Member

isidore commented Sep 8, 2022

if the order is different, the approval will fail.
We can enforce an ordering, but it sounds like you want to change the ordering and have it still pass. is that correct?

@horiaconstantin-cpi
Copy link
Author

I can confirm that the behavior that you describe in the first sentence is what I'm also seeing.

Indeed, I want the approval to pass if the ordering of the JSON fields changes. Or I'd like a way to control if the verify is lenient regarding ordering. GSONBuilder doesn't offer that

@LarsEckart
Copy link
Contributor

  1. we're not about to fix that
  2. we're gonna give you a method to fix it :)

with release 18.5.0 (releasing right now) there is now a new configuration for Options that allows you to provide a custom comparator.
example:

@Test
  void verifyCustomComparator()
  {
    Approvals.verify("The approval file is empty", new Options().withComparator((a, b) -> VerifyResult.SUCCESS));
  }

So your custom implementation would probably have to read files a and b into a JsonObject from gson and then does gson offer an equals method to tell you that they are the same, even with different order?!

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

No branches or pull requests

3 participants