Skip to content

Commit

Permalink
(#203) Improve TextMatcher API and messages
Browse files Browse the repository at this point in the history
  • Loading branch information
victornoel committed Feb 13, 2021
1 parent 7cf16c1 commit ae8c090
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public HasString(final Text text) {
new TextMatcher(
text,
(act, txt) -> act.contains(txt),
"Text with"
"Text containing"
)
);
}
Expand Down
64 changes: 33 additions & 31 deletions src/main/java/org/llorllale/cactoos/matchers/TextMatcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,48 +28,63 @@

import org.cactoos.BiFunc;
import org.cactoos.Text;
import org.cactoos.text.FormattedText;
import org.cactoos.text.UncheckedText;
import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.hamcrest.TypeSafeDiagnosingMatcher;
import org.hamcrest.TypeSafeMatcher;

/**
* Generic {@link Matcher} of {@link Text}.
*
* @since 1.0.0
* @checkstyle ProtectedMethodInFinalClassCheck (200 lines)
*/
public final class TextMatcher extends TypeSafeDiagnosingMatcher<Text> {
public final class TextMatcher extends TypeSafeMatcher<Text> {

/**
* The matcher to test.
*/
private final Matcher<String> matcher;

/**
* The description of the matcher's actual text.
* Ctor.
* @param text The text to match against.
* @param func Function that compares actual to expected value.
* @param expected The description of the matcher's expected text.
*/
private final String actual;
public TextMatcher(
final Text text,
final BiFunc<String, String, Boolean> func,
final String expected
) {
this(text, func, expected, "Text with value");
}

/**
* Ctor.
* @param text The text to match against.
* @param func Function that compares actual to expected value.
* @param expected The description of the matcher's expected text.
* @param expected The prefix of the matcher's expected text.
* @param actual The prefix of the actual text.
* @checkstyle ParameterNumberCheck (2 lines)
*/
public TextMatcher(
final Text text,
final BiFunc<String, String, Boolean> func,
final String expected
final String expected,
final String actual
) {
this(
new MatcherOf<>(
act -> func.apply(act, text.asString()),
desc -> desc.appendText(
new FormattedText("%s \"%s\"", expected, text).asString()
),
(act, desc) -> desc.appendValue(act)
desc -> desc
.appendText(expected)
.appendText(" ")
.appendValue(text.asString()),
(act, desc) -> desc
.appendText(actual)
.appendText(" ")
.appendValue(act)
)
);
}
Expand All @@ -79,18 +94,8 @@ public TextMatcher(
* @param mtchr The matcher to test.
*/
public TextMatcher(final Matcher<String> mtchr) {
this(mtchr, "Text is ");
}

/**
* Ctor.
* @param mtchr The matcher to test.
* @param actual The description of the matcher's actual text.
*/
public TextMatcher(final Matcher<String> mtchr, final String actual) {
super();
this.matcher = mtchr;
this.actual = actual;
}

@Override
Expand All @@ -99,15 +104,12 @@ public void describeTo(final Description desc) {
}

@Override
protected boolean matchesSafely(final Text text, final Description desc) {
final String txt = new UncheckedText(text).asString();
final boolean matches = this.matcher.matches(txt);
desc.appendText(this.actual);
if (matches) {
desc.appendValue(txt);
} else {
this.matcher.describeMismatch(txt, desc);
}
return matches;
protected boolean matchesSafely(final Text text) {
return this.matcher.matches(new UncheckedText(text).asString());
}

@Override
protected void describeMismatchSafely(final Text text, final Description desc) {
this.matcher.describeMismatch(new UncheckedText(text).asString(), desc);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ void mismatches() {
new Mismatches<>(
new TextOf("The sentence."),
"Text ending with \"!\"",
"Text is \"The sentence.\""
"Text with value \"The sentence.\""
)
).affirm();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ void describesMismatch() {
new HasString("xyz456"),
new Mismatches<>(
new TextOf("abc123"),
"Text with \"xyz456\"",
"Text is \"abc123\""
"Text containing \"xyz456\"",
"Text with value \"abc123\""
)
).affirm();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ void mismatchesFromMatcher() {
new Mismatches<>(
new TextOf("b"),
"Text with value \"a\" runs in less than <1000L> milliseconds",
"Text is \"b\""
"Text with value \"b\""
)
).affirm();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ void mismatches() {
new Mismatches<>(
new TextOf("The sentence."),
"Text matches \"^.*!$\"",
"Text is \"The sentence.\""
"Text with value \"The sentence.\""
)
).affirm();
}
Expand Down
13 changes: 7 additions & 6 deletions src/test/java/org/llorllale/cactoos/matchers/MismatchesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
* @since 1.0.0
* @checkstyle JavadocMethodCheck (500 lines)
*/
@SuppressWarnings("PMD.AvoidDuplicateLiterals")
final class MismatchesTest {

/**
Expand All @@ -53,7 +54,7 @@ void mismatches() {
new Mismatches<>(
new TextOf("def"),
"Text with value \"abc\"",
"Text is \"def\""
"Text with value \"def\""
)
).affirm();
}
Expand All @@ -67,7 +68,7 @@ void mismatchesWithRightMessage() {
new Mismatches<Text, IsText>(
new TextOf("e"),
"Text with value \"actual\"",
"Text is \"e\""
"Text with value \"e\""
).matchesSafely(
new IsText("actual"), description
);
Expand All @@ -87,14 +88,14 @@ void mismatchesWithWrongMessage() {
new Mismatches<Text, IsText>(
new TextOf("c"),
"Text with value \"c\"",
"Text is \"c\""
"Text with value \"c\""
).matchesSafely(
new IsText("expec"), description
);
new Assertion<>(
"describes when mismatches with the wrong message",
description.toString(),
new IsEqual<>("\nExpected: Text with value \"expec\"\n but was: Text is \"c\"")
new IsEqual<>("\nExpected: Text with value \"expec\"\n but was: Text with value \"c\"")
).affirm();
}

Expand All @@ -113,7 +114,7 @@ void describeActual() {
new Assertion<>(
"describes the matcher",
description.toString(),
new IsEqual<>("\nExpected: Text with value \"e\"\n but was: Text is \"e\"")
new IsEqual<>("\nExpected: Text with value \"e\"\n but was: Text with value \"e\"")
).affirm();
}

Expand All @@ -125,7 +126,7 @@ void matches() {
new Mismatches<>(
new IsText("a"),
"Mismatches <a> with message <expected>",
"\nExpected: Text with value \"a\"\n but was: Text is \"a\""
"\nExpected: Text with value \"a\"\n but was: Text with value \"a\""
)
).affirm();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ void mismatches() {
new Mismatches<>(
new TextOf("The sentence."),
"Text starting with \"!\"",
"Text is \"The sentence.\""
"Text with value \"The sentence.\""
)
).affirm();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,7 @@ void matchesAReadOnceInput() {
final String input = "aaaa";
new Assertion<>(
"must match on an input that can be read only once",
new TextMatcher(
new IsEqual<>(input),
"Text equals to "
),
new TextMatcher(new IsEqual<>(input)),
new Matches<>(new TextOf(new StringReader(input)))
).affirm();
}
Expand All @@ -65,7 +62,7 @@ void mismatches() {
new Mismatches<>(
new TextOf("The sentence."),
"Text starting with \"!\"",
"Text is \"The sentence.\""
"Text with value \"The sentence.\""
)
).affirm();
}
Expand All @@ -74,9 +71,7 @@ void mismatches() {
void matchesFromMatcher() {
new Assertion<>(
"must match with matcher",
new TextMatcher(
new IsBlank()
),
new TextMatcher(new IsBlank()),
new Matches<>(new TextOf(""))
).affirm();
}
Expand Down

0 comments on commit ae8c090

Please sign in to comment.