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

#820 Fix Hamcrest matcher error message #839

Merged
merged 1 commit into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ void matchHamcrestMatcher(Context context, Node actualNode, Matcher patternMatch
Object value = actualNode.getValue();
if (!matcher.matches(value)) {
Description description = new StringDescription();
description.appendText("Expected ");
matcher.describeTo(description);
description.appendText(" but ");
matcher.describeMismatch(value, description);
valueDifferenceReporter.differenceFound(
context,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ void useMatcher() {
.node("result.decimal")
.isEqualTo("${json-unit.matches:negative}")))
.hasMessage(
"JSON documents are different:\nMatcher \"negative\" does not match value 1.00001 in node \"result.decimal\". <1.00001> was greater than <0>\n");
"JSON documents are different:\nMatcher \"negative\" does not match value 1.00001 in node \"result.decimal\". Expected a value less than <0> but <1.00001> was greater than <0>\n");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1311,7 +1311,7 @@ void ifMatcherDoesNotMatchReportDifference() {
.withDifferenceListener(listener)
.isEqualTo("{\"test\": \"${json-unit.matches:positive}\"}"))
.hasMessage(
"JSON documents are different:\nMatcher \"positive\" does not match value -1 in node \"test\". <-1> was less than <0>\n");
"JSON documents are different:\nMatcher \"positive\" does not match value -1 in node \"test\". Expected a value greater than <0> but <-1> was less than <0>\n");

assertThat(listener.getDifferenceList()).hasSize(1);
assertThat(listener.getDifferenceList().get(0).toString())
Expand All @@ -1327,7 +1327,7 @@ void parametrizedMatcherShouldFail() {
.isEqualTo("{\"test\": \"${json-unit.matches:isDivisibleBy}3\"}");
} catch (AssertionError e) {
assertEquals(
"JSON documents are different:\nMatcher \"isDivisibleBy\" does not match value 5 in node \"test\". It is not divisible by <3>\n",
"JSON documents are different:\nMatcher \"isDivisibleBy\" does not match value 5 in node \"test\". Expected value divisible by <3> but was <5>\n",
e.getMessage());
}
}
Expand Down Expand Up @@ -2311,6 +2311,28 @@ void shouldFailFast() {
""");
}

@Test
void hamcrestMessageTest() {
assertThatThrownBy(
() -> assertThatJson("""
{
"someText": "abc123"
}
""")
.withMatcher("exampleMatcher", org.hamcrest.Matchers.equalTo("def456"))
.isEqualTo(
"""
{
"someText": "${json-unit.matches:exampleMatcher}"
}
"""))
.hasMessage(
"""
JSON documents are different:
Matcher "exampleMatcher" does not match value "abc123" in node "someText". Expected "def456" but was "abc123"
""");
}

private static final String json =
"""
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1049,7 +1049,7 @@ void parametrizedMatcherShouldFail() {
JsonAssert.withMatcher("isDivisibleBy", divisionMatcher));
} catch (AssertionError e) {
assertEquals(
"JSON documents are different:\nMatcher \"isDivisibleBy\" does not match value 5 in node \"test\". It is not divisible by <3>\n",
"JSON documents are different:\nMatcher \"isDivisibleBy\" does not match value 5 in node \"test\". Expected value divisible by <3> but was <5>\n",
e.getMessage());
}
}
Expand Down Expand Up @@ -1102,7 +1102,7 @@ void parameterMatchingShouldWork() {
"""
JSON documents are different:
Different value found when comparing expected array element test[0] to actual element test[1].
Matcher "eq" does not match value 2 in node "test[1]".\s
Matcher "eq" does not match value 2 in node "test[1]". Expected number equal to <1> but was <2>
""");
}

Expand All @@ -1116,12 +1116,7 @@ public boolean matches(Object item) {

@Override
public void describeTo(Description description) {
description.appendValue(param);
}

@Override
public void describeMismatch(Object item, Description description) {
description.appendText("It is not divisible by ").appendValue(param);
description.appendText("value divisible by ").appendValue(param);
}

@Override
Expand Down Expand Up @@ -1163,12 +1158,9 @@ public boolean matches(Object item) {

@Override
public void describeTo(Description description) {
description.appendValue(param);
description.appendText("number equal to ").appendValue(param);
}

@Override
public void describeMismatch(Object item, Description description) {}

@Override
public void setParameter(String parameter) {
this.param = new BigDecimal(parameter);
Expand Down Expand Up @@ -1295,7 +1287,7 @@ void ifMatcherDoesNotMatchReportDifference() {
"{\"test\":-1}",
withMatcher("positive", greaterThan(valueOf(0)))))
.hasMessage(
"JSON documents are different:\nMatcher \"positive\" does not match value -1 in node \"test\". <-1> was less than <0>\n");
"JSON documents are different:\nMatcher \"positive\" does not match value -1 in node \"test\". Expected a value greater than <0> but <-1> was less than <0>\n");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,7 @@ void ifMatcherDoesNotMatchReportDifference() {
.withDifferenceListener(listener)
.isEqualTo("{\"test\": \"#{json-unit.matches:positive}\"}"))
.hasMessage(
"JSON documents are different:\nMatcher \"positive\" does not match value -1 in node \"test\". <-1> was less than <0>\n");
"JSON documents are different:\nMatcher \"positive\" does not match value -1 in node \"test\". Expected a value greater than <0> but <-1> was less than <0>\n");
assertThat(listener.getDifferenceList()).hasSize(1);
assertThat(listener.getDifferenceList().get(0).toString())
.isEqualTo("DIFFERENT Expected #{json-unit.matches:positive} in test got -1 in test");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ void ifMatcherDoesNotMatchReportDifference() {

Expected: {"test": "${json-unit.matches:positive}"}
but: JSON documents are different:
Matcher "positive" does not match value -1 in node "test". <-1> was less than <0>
Matcher "positive" does not match value -1 in node "test". Expected a value greater than <0> but <-1> was less than <0>
""");
}

Expand Down