Skip to content

Commit

Permalink
Remove all MatcherOf ctors except last one #165
Browse files Browse the repository at this point in the history
  • Loading branch information
Olivier B. OURA committed Jan 23, 2021
1 parent 81bdf65 commit aff8dc6
Show file tree
Hide file tree
Showing 12 changed files with 118 additions and 156 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
19 changes: 15 additions & 4 deletions 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 All @@ -54,11 +55,20 @@ public HasContent(final String text) {
/**
* Ctor.
* @param text The text to match against
* @todo #165:30min We need an InputMatcher similar to {@link TextMatcher} that would
* be in charge of caching the value of the {@link Input} so that it is ready only once
* per invocation of the {@link Matcher}.
*/
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()
),
(act, desc) -> desc
.appendText("has content ")
.appendValue(act)
)
);
}
Expand All @@ -74,9 +84,10 @@ public HasContent(final Matcher<String> mtr) {
desc -> desc
.appendText("has content ")
.appendDescriptionOf(mtr),
(input, desc) -> desc
.appendText("has content ")
.appendValue(new TextOf(input).asString())
(input, desc) -> mtr.describeMismatch(
new TextOf(input).toString(),
desc
)
)
);
}
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"
)
);
}

}
27 changes: 21 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,34 @@
*/
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
* @todo #165:30min Introduce a ComparatorMatcher that encapsulates comparison logic here.
* It would only take a {@code Comparator<X>} and an expected X for example.
*/
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
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,9 @@ public MatchesRegex(final String regex) {
public MatchesRegex(final Text regex) {
super(
new TextMatcher(
new MatcherOf<>(
(String act) -> act.matches(regex.asString()),
regex
),
"Text matches "
regex,
(act, txt) -> act.matches(txt),
"Text matches"
)
);
}
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);
this.matcher.describeTo(desc);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import org.hamcrest.Description;
import org.hamcrest.StringDescription;
import org.hamcrest.core.IsEqual;
import org.hamcrest.core.IsNot;
import org.junit.jupiter.api.Test;

/**
Expand All @@ -56,7 +55,11 @@ void mismatchInputContent() {
new Assertion<>(
"does not match input with different contents",
new HasContent("hello"),
new IsNot<>(new Matches<>(new InputOf("world")))
new Mismatches<>(
new InputOf("world"),
"has content \"hello\"",
"has content \"world\""
)
).affirm();
}

Expand Down

0 comments on commit aff8dc6

Please sign in to comment.