Skip to content

Commit eeb8345

Browse files
Thomas Deblockdeblockt
authored andcommitted
fix: empty array issue
- fix similarityRate = Nan when compare two empty array
1 parent 9dfab1e commit eeb8345

File tree

3 files changed

+32
-14
lines changed

3 files changed

+32
-14
lines changed

src/main/java/com/deblock/jsondiff/diff/JsonArrayDiff.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,10 @@ public void addExtraItem(int index, JsonNode extraReceivedValue) {
3131

3232
@Override
3333
public double similarityRate() {
34-
final var totalArraySize = valuesWithoutMatch.size() + valuesWithMatch.size() + this.extraValues.size();
34+
final var totalArraySize = valuesWithoutMatch.size() + valuesWithMatch.size() + extraValues.size();
35+
if (totalArraySize == 0) {
36+
return 100.0;
37+
}
3538
final var totalSimilarityRate = valuesWithMatch.values().stream()
3639
.mapToDouble(JsonDiff::similarityRate)
3740
.sum();

src/test/java/com/deblock/jsondiff/matcher/LenientJsonArrayPartialMatcherTest.java

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@
1212
import static org.junit.jupiter.api.Assertions.assertEquals;
1313
import static org.mockito.ArgumentMatchers.any;
1414

15-
public class LenientJsonArrayPartialMatcherTest {
15+
class LenientJsonArrayPartialMatcherTest {
1616
private final static Path path = Path.ROOT.add(Path.PathItem.of("a"));
1717

1818
@Test
19-
public void shouldReturnFullMatchWhenAllItemsAreFound() {
19+
void shouldReturnFullMatchWhenAllItemsAreFound() {
2020
final var array1 = new ArrayNode(null, List.of(TextNode.valueOf("a"), TextNode.valueOf("b")));
2121
final var array2 = new ArrayNode(null, List.of(TextNode.valueOf("a"), TextNode.valueOf("b")));
2222
final var jsonMatcher = Mockito.mock(JsonMatcher.class);
@@ -33,7 +33,7 @@ public void shouldReturnFullMatchWhenAllItemsAreFound() {
3333
}
3434

3535
@Test
36-
public void shouldReturnFullMatchWhenAllItemsAreFoundWithBadOrder() {
36+
void shouldReturnFullMatchWhenAllItemsAreFoundWithBadOrder() {
3737
final var array1 = new ArrayNode(null, List.of(TextNode.valueOf("a"), TextNode.valueOf("b")));
3838
final var array2 = new ArrayNode(null, List.of(TextNode.valueOf("b"), TextNode.valueOf("a")));
3939
final var jsonMatcher = Mockito.mock(JsonMatcher.class);
@@ -50,7 +50,22 @@ public void shouldReturnFullMatchWhenAllItemsAreFoundWithBadOrder() {
5050
}
5151

5252
@Test
53-
public void shouldReturnNoMatchWhenSameNumberItemWithNoMatch() {
53+
void shouldReturnFullMatchForEmptyArray() {
54+
final var array1 = new ArrayNode(null, List.of());
55+
final var array2 = new ArrayNode(null, List.of());
56+
final var jsonMatcher = Mockito.mock(JsonMatcher.class);
57+
Mockito.when(jsonMatcher.diff(any(), any(), any())).thenAnswer(this::matchByEquality);
58+
59+
final var result = new LenientJsonArrayPartialMatcher().jsonDiff(path, array1, array2, jsonMatcher);
60+
61+
assertEquals(100, result.similarityRate());
62+
assertEquals(path, result.path());
63+
new JsonDiffAsserter()
64+
.validate(result);
65+
}
66+
67+
@Test
68+
void shouldReturnNoMatchWhenSameNumberItemWithNoMatch() {
5469
final var array1 = new ArrayNode(null, List.of(TextNode.valueOf("a"), TextNode.valueOf("b")));
5570
final var array2 = new ArrayNode(null, List.of(TextNode.valueOf("c"), TextNode.valueOf("d")));
5671
final var jsonMatcher = Mockito.mock(JsonMatcher.class);
@@ -67,7 +82,7 @@ public void shouldReturnNoMatchWhenSameNumberItemWithNoMatch() {
6782
}
6883

6984
@Test
70-
public void shouldReturnPartialMatchWhenMissingItem() {
85+
void shouldReturnPartialMatchWhenMissingItem() {
7186
final var array1 = new ArrayNode(null, List.of(TextNode.valueOf("a"), TextNode.valueOf("b")));
7287
final var array2 = new ArrayNode(null, List.of(TextNode.valueOf("b")));
7388
final var jsonMatcher = Mockito.mock(JsonMatcher.class);
@@ -84,7 +99,7 @@ public void shouldReturnPartialMatchWhenMissingItem() {
8499
}
85100

86101
@Test
87-
public void shouldReturnPartialMatchWhenExtraItems() {
102+
void shouldReturnPartialMatchWhenExtraItems() {
88103
final var array1 = new ArrayNode(null, List.of(TextNode.valueOf("a"), TextNode.valueOf("c")));
89104
final var array2 = new ArrayNode(null, List.of(TextNode.valueOf("a"), TextNode.valueOf("b"), TextNode.valueOf("c"), TextNode.valueOf("d")));
90105
final var jsonMatcher = Mockito.mock(JsonMatcher.class);

src/test/java/com/deblock/jsondiff/matcher/LenientJsonObjectPartialMatcherTest.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@
1212
import static org.junit.jupiter.api.Assertions.assertEquals;
1313
import static org.mockito.ArgumentMatchers.any;
1414

15-
public class LenientJsonObjectPartialMatcherTest {
15+
class LenientJsonObjectPartialMatcherTest {
1616
private final Path path = Path.ROOT.add(Path.PathItem.of("foo"));
1717

1818
@Test
19-
public void shouldReturnFullMatchForTwoEmptyObjects() {
19+
void shouldReturnFullMatchForTwoEmptyObjects() {
2020
final var object1 = new ObjectNode(null);
2121
final var object2 = new ObjectNode(null);
2222

@@ -27,7 +27,7 @@ public void shouldReturnFullMatchForTwoEmptyObjects() {
2727
}
2828

2929
@Test
30-
public void shouldReturnNonMachIfAllPropertiesAreNotFound() {
30+
void shouldReturnNonMachIfAllPropertiesAreNotFound() {
3131
final var object1 = new ObjectNode(null, Map.of(
3232
"a", TextNode.valueOf("a"),
3333
"b", TextNode.valueOf("b")
@@ -45,7 +45,7 @@ public void shouldReturnNonMachIfAllPropertiesAreNotFound() {
4545
}
4646

4747
@Test
48-
public void shouldReturnNonMatchingPropertyIfAllPropertiesAreFoundWithoutMatch() {
48+
void shouldReturnNonMatchingPropertyIfAllPropertiesAreFoundWithoutMatch() {
4949
final var object1 = new ObjectNode(null, Map.of(
5050
"a", TextNode.valueOf("a"),
5151
"b", TextNode.valueOf("b")
@@ -67,7 +67,7 @@ public void shouldReturnNonMatchingPropertyIfAllPropertiesAreFoundWithoutMatch()
6767
}
6868

6969
@Test
70-
public void shouldMixMatchingAndNotFoundPropertiesOnSameResult() {
70+
void shouldMixMatchingAndNotFoundPropertiesOnSameResult() {
7171
final var object1 = new ObjectNode(null, Map.of(
7272
"a", TextNode.valueOf("a"),
7373
"b", TextNode.valueOf("b")
@@ -89,7 +89,7 @@ public void shouldMixMatchingAndNotFoundPropertiesOnSameResult() {
8989
}
9090

9191
@Test
92-
public void shouldReturnFullMatchingPropertyAllPropertiesAreFoundAndMatch() {
92+
void shouldReturnFullMatchingPropertyAllPropertiesAreFoundAndMatch() {
9393
final var object1 = new ObjectNode(null, Map.of(
9494
"a", TextNode.valueOf("a"),
9595
"b", TextNode.valueOf("b")
@@ -111,7 +111,7 @@ public void shouldReturnFullMatchingPropertyAllPropertiesAreFoundAndMatch() {
111111
}
112112

113113
@Test
114-
public void shouldReturnSimilarityIfOnlyOneProperty() {
114+
void shouldReturnSimilarityIfOnlyOneProperty() {
115115
final var object1 = new ObjectNode(null, Map.of(
116116
"a", TextNode.valueOf("a"),
117117
"b", TextNode.valueOf("b"),

0 commit comments

Comments
 (0)