Skip to content

Commit

Permalink
Do not append tolerance with zero value to diff message
Browse files Browse the repository at this point in the history
  • Loading branch information
valfirst committed Apr 2, 2020
1 parent ead603f commit 52a03a2
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
Expand Down Expand Up @@ -329,8 +330,14 @@ private void compareNodes(Context context) {
if (configuration.getTolerance() != null && !hasOption(context.getActualPath(), IGNORING_VALUES)) {
BigDecimal diff = expectedValue.subtract(actualValue).abs();
if (diff.compareTo(configuration.getTolerance()) > 0) {
reportValueDifference(context, "Different value found in node \"%s\", " + differenceString() + ", difference is %s, tolerance is %s",
fieldPath, quoteTextValue(expectedValue), quoteTextValue(actualValue), diff.toString(), configuration.getTolerance());
List<Object> arguments = new ArrayList<>(Arrays.asList(fieldPath, quoteTextValue(expectedValue), quoteTextValue(actualValue), diff.toString()));
String message = "Different value found in node \"%s\", " + differenceString() + ", difference is %s";
if (configuration.getTolerance().compareTo(BigDecimal.ZERO) != 0)
{
arguments.add(configuration.getTolerance());
message += ", tolerance is %s";
}
reportValueDifference(context, message, arguments.toArray());
}
} else {
compareValues(context, expectedValue, actualValue);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,14 @@ void testComparisonWhenOverTolerance() {
"Different value found in node \"\", expected: <1> but was: <1.1>, difference is 0.1, tolerance is 0.01\n");
}

@Test
void testComparisonWhenOverZeroTolerance() {
setTolerance(0);
assertThatThrownBy(() -> assertJsonEquals("1", "\n1.1\n"))
.hasMessage("JSON documents are different:\n" +
"Different value found in node \"\", expected: <1> but was: <1.1>, difference is 0.1\n");
}

@Test
void testNullOk() {
assertJsonEquals("{\"test\":null}", "{\n\"test\": null\n}");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,12 @@ void testAssertToleranceFailure() {
.hasMessage("JSON documents are different:\nDifferent value found in node \"test\", expected: <1> but was: <1.1>, difference is 0.1, tolerance is 0.001\n");
}

@Test
void testAssertZeroToleranceFailure() {
assertThatThrownBy(() -> assertThatJson("{\"test\":1.1}").node("test").withTolerance(0.0).isEqualTo(1))
.hasMessage("JSON documents are different:\nDifferent value found in node \"test\", expected: <1> but was: <1.1>, difference is 0.1\n");
}

@Test
void testAssertNode() {
assertThatThrownBy(() -> assertThatJson(readValue("{\"test\":1}")).isEqualTo(readValue("{\"test\":2}")))
Expand Down

0 comments on commit 52a03a2

Please sign in to comment.