Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
fslev committed Oct 6, 2022
1 parent c918ff2 commit 3183f33
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 7 deletions.
40 changes: 33 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,39 @@ The library has some tweaks which helps you make assertions without writing any
compile 'com.github.fslev:json-compare:<version.from.maven.central>'
```

Match any JSON convertible Java objects
```javascript
String expectedString = "{\"a\":1, \"b\": [4, 2, \"\\\\d+\"]}";
String actualString = "{\"a\":1, \"b\":[4, 2, 5], \"c\":3}";
JSONCompare.assertMatches(expectedString, actualString); // assertion passes

// actual represented as Map
Map<String, Object> actualMap = new HashMap<>();
actualMap.put("a", 1);
actualMap.put("b", Arrays.asList(4, 2, 5));
actualMap.put("c", 3);
JSONCompare.assertMatches(expectedString, actualMap); // assertion passes

// Failed assertion
String anotherActualString = "{\"a\":10, \"b\":[4, 20, 5], \"c\":3}";
JSONCompare.assertNotMatches(expectedString, anotherActualString); // assertion passes
JSONCompare.assertMatches(expectedString, anotherActualString); // assertion fails

==>

org.opentest4j.AssertionFailedError: FOUND 2 DIFFERENCE(S):


_________________________DIFF__________________________
a ->
Expected value: 1 But got: 10

_________________________DIFF__________________________
b ->
Expected element from position 2 was NOT FOUND:
2
```

# <a name="compare-modes"></a>Compare modes

Assert that expected JSON is included within the actual JSON:
Expand Down Expand Up @@ -79,13 +112,6 @@ String actual = "{\"foobar\":\"some value\"}";
JSONCompare.assertMatches(expected, actual);
```

Match any JSON convertible Java objects
```javascript
String expected = "{\"a\":1,\"b\":[4, 2, \"\\\\d+\"]}";
Map<String, Object> actual = Map.of("a", 1, "b", Arrays.asList(1, 2, 3, 4));
JSONCompare.assertMatches(expected, actual);
```

JSONCompare by default matches JSON fields and values using regular expressions.
If you have unintentional regex characters inside either expected values or expected fields, then you can quote them:

Expand Down
36 changes: 36 additions & 0 deletions src/test/java/io/json/compare/matcher/readme/ReadmeTests.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package io.json.compare.matcher.readme;

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

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

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

public class ReadmeTests {

@Test
public void matchJsonConvertibleJavaObjects() {
String expectedString = "{\"a\":1, \"b\": [4, 2, \"\\\\d+\"]}";
String actualString = "{\"a\":1, \"b\":[4, 2, 5], \"c\":3}";
JSONCompare.assertMatches(expectedString, actualString); // assertion passes

// actual represented as Map
Map<String, Object> actualMap = new HashMap<>();
actualMap.put("a", 1);
actualMap.put("b", Arrays.asList(4, 2, 5));
actualMap.put("c", 3);
JSONCompare.assertMatches(expectedString, actualMap); // assertion passes

// Failed assertion
String anotherActualString = "{\"a\":10, \"b\":[4, 20, 5], \"c\":3}";
JSONCompare.assertNotMatches(expectedString, anotherActualString); // assertion passes
AssertionError error = assertThrows(AssertionError.class, () -> JSONCompare.assertMatches(expectedString, anotherActualString));
assertTrue(error.getMessage().matches("(?s).*FOUND 2 DIFFERENCE.*" +
"a ->.*Expected value: 1 But got: 10.*" +
"b ->.*Expected element from position 2 was NOT FOUND.*"));
}
}

0 comments on commit 3183f33

Please sign in to comment.