Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
fslev committed Oct 4, 2022
1 parent 663d0b5 commit ee98352
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 27 deletions.
80 changes: 53 additions & 27 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,37 +109,63 @@ JSONCompare.assertMatches(expected, actual, new JsonComparator() {
// should fail
```

## Error messages
## Differences
```javascript
String expected = "{\"b\":{\"x\":\"val1\",\"y\":\"val2\"},\"a\":{\"t\":\"val3\",\"z\":\"val1\"}}";
String actual = "{\"a\":{\"t\":\"val3\",\"z\":\"val4\"},\"b\":{\"x\":\"val1\",\"y\":\"val2\"}}";
JSONCompare.assertMatches(expected, actual);
String expected = "{\n" +
" \"caught\": false,\n" +
" \"pain\": {\n" +
" \"range\": [\n" +
" \"bell\",\n" +
" \"blue\",\n" +
" -2059921070\n" +
" ],\n" +
" \"not_anyone\": -1760889549.4041045,\n" +
" \"flat\": -2099670336\n" +
" }\n" +
"}";
String actual = "{\n" +
" \"caught\": true,\n" +
" \"pain\": {\n" +
" \"range\": [\n" +
" \"bell\",\n" +
" \"red\",\n" +
" -2059921075\n" +
" ],\n" +
" \"anyone\": -1760889549.4041045,\n" +
" \"flat\": -2099670336\n" +
" },\n" +
" \"broad\": \"invented\"\n" +
"}";
JSONCompare.assertMatches(expected, actual);


Output:

java.lang.AssertionError: Expected ["val1"] but found ["val4"] <- field "z" <- field "a"
Expected:
{
"b" : {
"x" : "val1",
"y" : "val2"
},
"a" : {
"t" : "val3",
"z" : "val1"
}
}
But got:
{
"a" : {
"t" : "val3",
"z" : "val4"
},
"b" : {
"x" : "val1",
"y" : "val2"
}
}
org.opentest4j.AssertionFailedError: FOUND 4 DIFFERENCE(S):


_________________________DIFF__________________________
caught ->
Expected value: false But got: true

_________________________DIFF__________________________
pain -> range ->
Expected element from position 2 was NOT FOUND:
"blue"

_________________________DIFF__________________________
pain -> range ->
Expected element from position 3 was NOT FOUND:
-2059921070

_________________________DIFF__________________________
pain -> Field 'not_anyone' was NOT FOUND


Json matching by default uses regular expressions.
In case expected json contains any unintentional regexes, then quote them between \Q and \E delimiters or use a custom comparator.
==>
<Click to see difference>
```

## Matching with some tweaks
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package io.json.compare.matcher.diffs;

import io.json.compare.JSONCompare;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class JsonRealWorldDiffTests {

@Test
public void compareJsonObjectsAndCheckForDifferences() {
String expected = "{\n" +
" \"caught\": false,\n" +
" \"pain\": {\n" +
" \"range\": [\n" +
" \"bell\",\n" +
" \"blue\",\n" +
" -2059921070\n" +
" ],\n" +
" \"not_anyone\": -1760889549.4041045,\n" +
" \"flat\": -2099670336\n" +
" }\n" +
"}";
String actual = "{\n" +
" \"caught\": true,\n" +
" \"pain\": {\n" +
" \"range\": [\n" +
" \"bell\",\n" +
" \"red\",\n" +
" -2059921075\n" +
" ],\n" +
" \"anyone\": -1760889549.4041045,\n" +
" \"flat\": -2099670336\n" +
" },\n" +
" \"broad\": \"invented\"\n" +
"}";
AssertionError error = assertThrows(AssertionError.class, () -> JSONCompare.assertMatches(expected, actual));
assertTrue(error.getMessage().matches("(?s).*FOUND 4 DIFFERENCE.*" +
"caught ->.*Expected value: false But got: true.*"+
"pain -> range ->.*Expected element from position 2 was NOT FOUND.*\"blue\".*"+
"pain -> range ->.*Expected element from position 3 was NOT FOUND.*-2059921070.*"+
"pain -> Field 'not_anyone' was NOT FOUND.*"));
JSONCompare.assertNotMatches(expected, actual);
}


}

0 comments on commit ee98352

Please sign in to comment.