Skip to content

Commit

Permalink
Remove all MatcherOf ctors except last one llorllale#165
Browse files Browse the repository at this point in the history
  • Loading branch information
Olivier B. OURA committed Jan 19, 2021
1 parent 81bdf65 commit 25ad01e
Show file tree
Hide file tree
Showing 12 changed files with 99 additions and 152 deletions.
8 changes: 3 additions & 5 deletions src/main/java/org/llorllale/cactoos/matchers/EndsWith.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,9 @@ public EndsWith(final String suffix) {
public EndsWith(final Text text) {
super(
new TextMatcher(
new MatcherOf<>(
(String act) -> act.endsWith(text.asString()),
text
),
"Text ending with "
text,
(act, txt) -> act.endsWith(txt),
"Text ending with"
)
);
}
Expand Down
7 changes: 6 additions & 1 deletion src/main/java/org/llorllale/cactoos/matchers/HasContent.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

import org.cactoos.Input;
import org.cactoos.Text;
import org.cactoos.text.FormattedText;
import org.cactoos.text.TextOf;
import org.hamcrest.Matcher;

Expand Down Expand Up @@ -58,7 +59,11 @@ public HasContent(final String text) {
public HasContent(final Text text) {
this(
new MatcherOf<>(
(String input) -> text.asString().equals(input), text
input -> text.asString().equals(input),
desc -> desc.appendText(
new FormattedText("\"%s\"", text).asString()
),
(actual, desc) -> desc.appendValue(actual)
)
);
}
Expand Down
9 changes: 3 additions & 6 deletions src/main/java/org/llorllale/cactoos/matchers/HasString.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,10 @@ public HasString(final String text) {
public HasString(final Text text) {
super(
new TextMatcher(
new MatcherOf<>(
(String actual) -> actual.contains(text.asString()),
text
),
"Text with "
text,
(act, txt) -> act.contains(txt),
"Text with"
)
);
}

}
25 changes: 19 additions & 6 deletions src/main/java/org/llorllale/cactoos/matchers/IsNumber.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,32 @@
*/
public final class IsNumber extends MatcherEnvelope<Number> {

/**
* Comparator of numbers.
*/
private static final Comparator<Number> FNC =
Comparator
.comparing(Number::doubleValue)
.thenComparing(Number::intValue)
.thenComparing(Number::longValue)
.thenComparing(Number::floatValue);

/**
* Ctor.
* @param expected The expected value
*/
public IsNumber(final Number expected) {
super(
new MatcherOf<>(
expected,
Comparator
.comparing(Number::doubleValue)
.thenComparing(Number::intValue)
.thenComparing(Number::longValue)
.thenComparing(Number::floatValue)
actual -> IsNumber.FNC.compare(actual, expected) == 0,
desc -> desc.appendText("equals ").appendValue(expected),
(actual, desc) -> desc
.appendText("comparator returns ")
.appendValue(IsNumber.FNC.compare(actual, expected))
.appendText(" when ")
.appendValue(expected)
.appendText(" compared to ")
.appendValue(actual)
)
);
}
Expand Down
8 changes: 3 additions & 5 deletions src/main/java/org/llorllale/cactoos/matchers/IsText.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,9 @@ public IsText(final String text) {
public IsText(final Text text) {
super(
new TextMatcher(
new MatcherOf<>(
(String actual) -> actual.equals(text.asString()),
text
),
"Text with value "
text,
(act, txt) -> act.equals(txt),
"Text with value"
)
);
}
Expand Down
60 changes: 0 additions & 60 deletions src/main/java/org/llorllale/cactoos/matchers/MatcherOf.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,12 @@
*/
package org.llorllale.cactoos.matchers;

import java.util.Comparator;
import org.cactoos.BiProc;
import org.cactoos.Func;
import org.cactoos.Proc;
import org.cactoos.Text;
import org.cactoos.func.FuncOf;
import org.cactoos.func.UncheckedFunc;
import org.cactoos.proc.UncheckedBiProc;
import org.cactoos.proc.UncheckedProc;
import org.cactoos.text.FormattedText;
import org.cactoos.text.UncheckedText;
import org.hamcrest.Description;
import org.hamcrest.TypeSafeMatcher;

Expand All @@ -47,9 +42,6 @@
*
* @param <T> Type of object to match
* @since 0.12
* @todo #135:30min Remove all constructors except the last one so that
* every matcher implemented using MatcherOf take care of properly
* describe itself and the mismatch.
*/
public final class MatcherOf<T> extends TypeSafeMatcher<T> {

Expand All @@ -69,37 +61,6 @@ public final class MatcherOf<T> extends TypeSafeMatcher<T> {
*/
private final BiProc<T, Description> mismatch;

/**
* Ctor.
* @param proc The func
*/
public MatcherOf(final Proc<T> proc) {
this(new FuncOf<>(proc, true));
}

/**
* Ctor.
* @param fnc The func
*/
public MatcherOf(final Func<T, Boolean> fnc) {
this(fnc, new UncheckedText(fnc.toString()));
}

/**
* Ctor.
* @param fnc The func
* @param description The description
*/
public MatcherOf(final Func<T, Boolean> fnc, final Text description) {
this(
fnc,
desc -> desc.appendText(
new FormattedText("\"%s\"", description).asString()
),
(actual, desc) -> desc.appendValue(actual)
);
}

/**
* Ctor.
* @param match Matches an actual object with expected one
Expand All @@ -117,27 +78,6 @@ public MatcherOf(
this.mismatch = mismatch;
}

/**
* Ctor.
* @param expected Expected value.
* @param comp Comparator.
*/
public MatcherOf(final T expected, final Comparator<? super T> comp) {
this(
(T x) -> comp.compare(x, expected) == 0,
(Description desc) -> desc
.appendText("equals ")
.appendValue(expected),
(T actual, Description desc) -> desc
.appendText("comparator returns ")
.appendValue(comp.compare(actual, expected))
.appendText(" when ")
.appendValue(expected)
.appendText(" compared to ")
.appendValue(actual)
);
}

@Override
public void describeTo(final Description desc) {
new UncheckedProc<>(this.description).exec(desc);
Expand Down
17 changes: 12 additions & 5 deletions src/main/java/org/llorllale/cactoos/matchers/MatchesRegex.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@
*/
package org.llorllale.cactoos.matchers;

import java.util.regex.Pattern;
import org.cactoos.Text;
import org.cactoos.text.FormattedText;
import org.cactoos.text.TextOf;

/**
Expand All @@ -50,12 +52,17 @@ public MatchesRegex(final String regex) {
*/
public MatchesRegex(final Text regex) {
super(
new TextMatcher(
new MatcherOf<>(
(String act) -> act.matches(regex.asString()),
regex
new MatcherOf<>(
act -> act.asString().matches(regex.asString()),
desc -> desc.appendText(
new FormattedText(
"Text matches /%s/",
Pattern.compile(regex.asString()).toString()
).asString()
),
"Text matches "
(actual, desc) -> desc.appendText(
new FormattedText("Text is \"%s\"", actual).asString()
)
)
);
}
Expand Down
8 changes: 3 additions & 5 deletions src/main/java/org/llorllale/cactoos/matchers/StartsWith.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,9 @@ public StartsWith(final String prefix) {
public StartsWith(final Text text) {
super(
new TextMatcher(
new MatcherOf<>(
(String act) -> act.startsWith(text.asString()),
text
),
"Text starting with "
text,
(act, txt) -> act.startsWith(txt),
"Text starting with"
)
);
}
Expand Down
40 changes: 26 additions & 14 deletions src/main/java/org/llorllale/cactoos/matchers/TextMatcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@
*/
package org.llorllale.cactoos.matchers;

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;
Expand All @@ -45,45 +47,55 @@ public final class TextMatcher extends TypeSafeDiagnosingMatcher<Text> {
*/
private final Matcher<String> matcher;

/**
* The description of the matcher's expected text.
*/
private final String expected;

/**
* The description of the matcher's actual text.
*/
private final String actual;

/**
* Ctor.
* @param mtchr The matcher to test.
* @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.
*/
public TextMatcher(
final Matcher<String> mtchr, final String expected
final Text text,
final BiFunc<String, String, Boolean> func,
final String expected
) {
this(mtchr, expected, "Text is ");
this(
new MatcherOf<>(
act -> func.apply(act, text.asString()),
desc -> desc.appendText(
new FormattedText("%s \"%s\"", expected, text).asString()
),
(act, desc) -> desc.appendValue(act)
)
);
}

/**
* Ctor.
* @param mtchr The matcher to test.
*/
public TextMatcher(final Matcher<String> mtchr) {
this(mtchr, "Text is ");
}

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

@Override
public void describeTo(final Description desc) {
desc.appendText(this.expected).appendDescriptionOf(this.matcher);
desc.appendDescriptionOf(this.matcher);
}

@Override
Expand Down
Loading

0 comments on commit 25ad01e

Please sign in to comment.