Skip to content

Commit

Permalink
Add a description in case the mismatch fails #138
Browse files Browse the repository at this point in the history
  • Loading branch information
Olivier B. OURA committed Jan 16, 2021
1 parent 64e2122 commit 40f1101
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 6 deletions.
24 changes: 19 additions & 5 deletions src/main/java/org/llorllale/cactoos/matchers/Mismatches.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,16 @@
public final class Mismatches<X, M extends Matcher<X>> extends
TypeSafeDiagnosingMatcher<M> {

/**
* Expected key word.
*/
private static final String EXPECTED = "Expected: ";

/**
* But was key word.
*/
private static final String BUT_WAS = " but was: ";

/**
* The testing arguments for the target matcher.
*/
Expand Down Expand Up @@ -103,8 +113,8 @@ public Mismatches(
new Joined(
new FormattedText("%n"),
new TextOf(""),
new Joined(new TextOf(""), new TextOf("Expected: "), expected),
new Joined(new TextOf(""), new TextOf(" but was: "), actual)
new Joined(new TextOf(""), new TextOf(Mismatches.EXPECTED), expected),
new Joined(new TextOf(""), new TextOf(Mismatches.BUT_WAS), actual)
)
);
}
Expand All @@ -128,19 +138,23 @@ public void describeTo(final Description desc) {
.appendValue(this.message);
}

// @todo #106:30min Add a description in case the mismatch fails.
// And then introduce some tests to validate that the description
// is properly constructed.
@Override
protected boolean matchesSafely(final M matcher, final Description dsc) {
boolean mismatch;
try {
new Assertion<>("", this.args, matcher).affirm();
mismatch = false;
dsc.appendText(Mismatches.EXPECTED);
matcher.describeTo(dsc);
dsc.appendText(Mismatches.BUT_WAS);
matcher.describeMismatch(this.args, dsc);
} catch (final AssertionError err) {
mismatch = err.getMessage().equals(
new UncheckedText(this.message).asString()
);
if (!mismatch) {
dsc.appendText(err.getMessage());
}
}
return mismatch;
}
Expand Down
25 changes: 24 additions & 1 deletion src/test/java/org/llorllale/cactoos/matchers/MismatchesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@

package org.llorllale.cactoos.matchers;

import org.cactoos.Text;
import org.cactoos.text.TextOf;
import org.hamcrest.Description;
import org.hamcrest.StringDescription;
import org.hamcrest.core.IsEqual;
import org.junit.jupiter.api.Test;

/**
Expand All @@ -53,6 +57,25 @@ void mismatches() {
).affirm();
}

/**
* Matcher prints the actual value(s) properly.
*/
@Test
void describeActual() {
final Description description = new StringDescription();
new Mismatches<Text, IsText>(
new TextOf("e"),
new TextOf("Mismatches <a> with message <e>")
).matchesSafely(
new IsText("e"), description
);
new Assertion<>(
"describes the matcher",
description.toString(),
new IsEqual<>("Expected: Text with value \"e\" but was: Text is \"e\"")
).affirm();
}

@Test
void matches() {
new Assertion<>(
Expand All @@ -61,7 +84,7 @@ void matches() {
new Mismatches<>(
new IsText("a"),
"Mismatches <a> with message <expected>",
""
"Expected: Text with value \"a\" but was: Text is \"a\""
)
).affirm();
}
Expand Down

0 comments on commit 40f1101

Please sign in to comment.