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 17, 2021
1 parent 81bdf65 commit f2bda3c
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 121 deletions.
8 changes: 6 additions & 2 deletions src/main/java/org/llorllale/cactoos/matchers/EndsWith.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
package org.llorllale.cactoos.matchers;

import org.cactoos.Text;
import org.cactoos.text.FormattedText;
import org.cactoos.text.TextOf;

/**
Expand All @@ -52,8 +53,11 @@ public EndsWith(final Text text) {
super(
new TextMatcher(
new MatcherOf<>(
(String act) -> act.endsWith(text.asString()),
text
act -> act.endsWith(text.asString()),
desc -> desc.appendText(
new FormattedText("\"%s\"", text).asString()
),
(actual, desc) -> desc.appendValue(actual)
),
"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
8 changes: 6 additions & 2 deletions src/main/java/org/llorllale/cactoos/matchers/HasString.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
package org.llorllale.cactoos.matchers;

import org.cactoos.Text;
import org.cactoos.text.FormattedText;
import org.cactoos.text.TextOf;

/**
Expand All @@ -52,8 +53,11 @@ public HasString(final Text text) {
super(
new TextMatcher(
new MatcherOf<>(
(String actual) -> actual.contains(text.asString()),
text
actual -> actual.contains(text.asString()),
desc -> desc.appendText(
new FormattedText("\"%s\"", text).asString()
),
(actual, desc) -> desc.appendValue(actual)
),
"Text with "
)
Expand Down
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: 6 additions & 2 deletions src/main/java/org/llorllale/cactoos/matchers/IsText.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
package org.llorllale.cactoos.matchers;

import org.cactoos.Text;
import org.cactoos.text.FormattedText;
import org.cactoos.text.TextOf;

/**
Expand All @@ -52,8 +53,11 @@ public IsText(final Text text) {
super(
new TextMatcher(
new MatcherOf<>(
(String actual) -> actual.equals(text.asString()),
text
actual -> actual.equals(text.asString()),
desc -> desc.appendText(
new FormattedText("\"%s\"", text).asString()
),
(actual, desc) -> desc.appendValue(actual)
),
"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 @@ -27,6 +27,7 @@
package org.llorllale.cactoos.matchers;

import org.cactoos.Text;
import org.cactoos.text.FormattedText;
import org.cactoos.text.TextOf;

/**
Expand All @@ -52,8 +53,11 @@ public MatchesRegex(final Text regex) {
super(
new TextMatcher(
new MatcherOf<>(
(String act) -> act.matches(regex.asString()),
regex
act -> act.matches(regex.asString()),
desc -> desc.appendText(
new FormattedText("\"%s\"", regex).asString()
),
(actual, desc) -> desc.appendValue(actual)
),
"Text matches "
)
Expand Down
8 changes: 6 additions & 2 deletions src/main/java/org/llorllale/cactoos/matchers/StartsWith.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
package org.llorllale.cactoos.matchers;

import org.cactoos.Text;
import org.cactoos.text.FormattedText;
import org.cactoos.text.TextOf;

/**
Expand All @@ -52,8 +53,11 @@ public StartsWith(final Text text) {
super(
new TextMatcher(
new MatcherOf<>(
(String act) -> act.startsWith(text.asString()),
text
act -> act.startsWith(text.asString()),
desc -> desc.appendText(
new FormattedText("\"%s\"", text).asString()
),
(actual, desc) -> desc.appendValue(actual)
),
"Text starting with "
)
Expand Down
56 changes: 12 additions & 44 deletions src/test/java/org/llorllale/cactoos/matchers/MatcherOfTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,8 @@
*/
package org.llorllale.cactoos.matchers;

import java.util.Comparator;
import org.cactoos.text.FormattedText;
import org.cactoos.text.Joined;
import org.cactoos.text.TextOf;
import org.junit.jupiter.api.Test;

/**
Expand All @@ -44,7 +43,11 @@ final class MatcherOfTest {
void matchesFunc() {
new Assertion<>(
"matches when arg satisfies the predicate",
new MatcherOf<>(x -> x > 5),
new MatcherOf<>(
x -> x > 5,
desc -> desc.appendText("Must be > 5"),
(actual, desc) -> desc.appendValue(actual)
),
new Matches<>(10)
).affirm();
}
Expand All @@ -54,26 +57,20 @@ void mismatchesFunc() {
new Assertion<>(
"mismatches when arg does not satisfy the predicate",
new MatcherOf<>(
x -> x > 5,
new TextOf("Must be > 5")
x -> x > 7,
desc -> desc.appendText(
new FormattedText("\"%s\"", "Must be > 7").asString()
),
(actual, desc) -> desc.appendValue(actual)
),
new Mismatches<>(
1,
"\"Must be > 5\"",
"\"Must be > 7\"",
"<1>"
)
).affirm();
}

@Test
void matcherOfProcMatchesAnyArguments() {
new Assertion<>(
"matches any arguments when constructed from a Proc",
new MatcherOf<>(String::trim),
new Matches<>("a")
).affirm();
}

@Test
void mismatches() {
final Integer expected = 42;
Expand All @@ -92,33 +89,4 @@ void mismatches() {
)
).affirm();
}

@Test
void matchesByComparator() {
new Assertion<>(
"matches when comparator returns 0",
new MatcherOf<>(
10,
Comparator.comparingInt(x -> Math.abs(x.intValue()))
),
new Matches<>(-10)
).affirm();
}

@Test
void mismatchesByComparator() {
new Assertion<>(
"mismatches when arg does not satisfy comparator",
new MatcherOf<>(
123,
Comparator.comparingInt(x -> Math.abs(x.intValue()))
),
new Mismatches<>(
1234,
"equals <123>",
"comparator returns <1> when <123> compared to <1234>"
)
).affirm();
}

}

0 comments on commit f2bda3c

Please sign in to comment.