Skip to content

Commit

Permalink
(llorllale#203) Simplify TextMatcher
Browse files Browse the repository at this point in the history
  • Loading branch information
victornoel committed Sep 27, 2020
1 parent c5404b8 commit f26f6b6
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 19 deletions.
2 changes: 1 addition & 1 deletion src/main/java/org/llorllale/cactoos/matchers/EndsWith.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public EndsWith(final Text text) {
super(
new TextMatcher(
new MatcherOf<>(
(Text act) -> act.asString().endsWith(text.asString()),
(String act) -> act.endsWith(text.asString()),
text
),
"Text ending with "
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public MatchesRegex(final Text regex) {
super(
new TextMatcher(
new MatcherOf<>(
(Text act) -> act.asString().matches(regex.asString()),
(String act) -> act.matches(regex.asString()),
regex
),
"Text matches "
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public StartsWith(final Text text) {
super(
new TextMatcher(
new MatcherOf<>(
(Text act) -> act.asString().startsWith(text.asString()),
(String act) -> act.startsWith(text.asString()),
text
),
"Text starting with "
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public TextHasString(final Text text) {
super(
new TextMatcher(
new MatcherOf<>(
(Text actual) -> actual.asString().contains(text.asString()),
(String actual) -> actual.contains(text.asString()),
text
),
"Text with "
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/llorllale/cactoos/matchers/TextIs.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public TextIs(final Text text) {
super(
new TextMatcher(
new MatcherOf<>(
(Text actual) -> actual.asString().equals(text.asString()),
(String actual) -> actual.equals(text.asString()),
text
),
"Text with value "
Expand Down
14 changes: 6 additions & 8 deletions src/main/java/org/llorllale/cactoos/matchers/TextMatcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,13 @@
package org.llorllale.cactoos.matchers;

import org.cactoos.Text;
import org.cactoos.text.TextOf;
import org.cactoos.text.UncheckedText;
import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.hamcrest.TypeSafeDiagnosingMatcher;

/**
* The Text-based {@link TypeSafeDiagnosingMatcher} envelope.
* Generic {@link Matcher} of {@link Text}.
*
* @since 1.0.0
* @checkstyle ProtectedMethodInFinalClassCheck (200 lines)
Expand All @@ -44,7 +43,7 @@ public final class TextMatcher extends TypeSafeDiagnosingMatcher<Text> {
/**
* The matcher to test.
*/
private final Matcher<Text> matcher;
private final Matcher<String> matcher;

/**
* The description of the matcher's expected text.
Expand All @@ -62,7 +61,7 @@ public final class TextMatcher extends TypeSafeDiagnosingMatcher<Text> {
* @param expected The description of the matcher's expected text.
*/
public TextMatcher(
final Matcher<Text> mtchr, final String expected
final Matcher<String> mtchr, final String expected
) {
this(mtchr, expected, "Text is ");
}
Expand All @@ -74,7 +73,7 @@ public TextMatcher(
* @param actual The description of the matcher's actual text.
*/
public TextMatcher(
final Matcher<Text> mtchr, final String expected, final String actual
final Matcher<String> mtchr, final String expected, final String actual
) {
super();
this.matcher = mtchr;
Expand All @@ -88,10 +87,9 @@ public void describeTo(final Description desc) {
}

@Override
protected boolean matchesSafely(final Text text,
final Description desc) {
protected boolean matchesSafely(final Text text, final Description desc) {
final String txt = new UncheckedText(text).asString();
desc.appendText(this.actual).appendValue(txt);
return this.matcher.matches(new TextOf(txt));
return this.matcher.matches(txt);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,25 +27,25 @@
package org.llorllale.cactoos.matchers;

import java.io.StringReader;
import org.cactoos.Text;
import org.cactoos.text.TextOf;
import org.hamcrest.core.IsEqual;
import org.junit.Test;

/**
* Tests for {@link TextMatcherEnvelope}.
* Tests for {@link TextMatcher}.
*
* @since 1.0.0
* @checkstyle JavadocMethodCheck (500 lines)
* @checkstyle JavadocTypeCheck (500 lines)
*/
public final class TextMatcherTest {

final class TextMatcherTest {
@Test
public void matchesAReadOnceInput() {
void matchesAReadOnceInput() {
final String input = "aaaa";
new Assertion<>(
"must match on an input that can be read only once",
new TextMatcher(
new MatcherOf<>((Text text) -> input.equals(text.asString())),
new IsEqual<>(input),
"Text equals to "
),
new Matches<>(new TextOf(new StringReader(input)))
Expand Down

0 comments on commit f26f6b6

Please sign in to comment.