Skip to content

Commit

Permalink
(#135) - MatcherEnvelope is a real envelope, MatcherOf is complete
Browse files Browse the repository at this point in the history
  • Loading branch information
victornoel committed Jul 14, 2020
1 parent 4daa27a commit c7bc081
Show file tree
Hide file tree
Showing 17 changed files with 195 additions and 305 deletions.
16 changes: 9 additions & 7 deletions src/main/java/org/llorllale/cactoos/matchers/FuncApplies.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,15 @@ public FuncApplies(final X inpt, final Y result) {
*/
public FuncApplies(final X input, final Matcher<Y> mtr) {
super(
func -> mtr.matches(
new UncheckedFunc<>(func).apply(input)
),
desc -> desc.appendText("Func with ")
.appendDescriptionOf(mtr),
(func, desc) -> desc.appendText("Func with ")
.appendValue(func.apply(input))
new MatcherOf<>(
func -> mtr.matches(
new UncheckedFunc<>(func).apply(input)
),
desc -> desc.appendText("Func with ")
.appendDescriptionOf(mtr),
(func, desc) -> desc.appendText("Func with ")
.appendValue(func.apply(input))
)
);
}
}
32 changes: 17 additions & 15 deletions src/main/java/org/llorllale/cactoos/matchers/HasEntry.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,23 +57,25 @@ public final class HasEntry<K, V> extends MatcherEnvelope<Map<K, V>> {
@SuppressWarnings("PMD.AvoidDuplicateLiterals")
public HasEntry(final K key, final V value) {
super(
input -> Optional.ofNullable(input.get(key))
.map(vl -> vl.equals(value))
.orElse(false),
desc -> desc
.appendText("has entry ")
.appendValue(key)
.appendText("=")
.appendValue(value),
(input, desc) -> new Ternary<>(
() -> input.containsKey(key),
() -> desc.appendText("has entry ")
new MatcherOf<>(
input -> Optional.ofNullable(input.get(key))
.map(vl -> vl.equals(value))
.orElse(false),
desc -> desc
.appendText("has entry ")
.appendValue(key)
.appendText("=")
.appendValue(input.get(key)),
() -> desc.appendText("has no entry for ")
.appendValue(key)
).value()
.appendValue(value),
(input, desc) -> new Ternary<>(
() -> input.containsKey(key),
() -> desc.appendText("has entry ")
.appendValue(key)
.appendText("=")
.appendValue(input.get(key)),
() -> desc.appendText("has no entry for ")
.appendValue(key)
).value()
)
);
}
}
8 changes: 5 additions & 3 deletions src/main/java/org/llorllale/cactoos/matchers/HasLines.java
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,11 @@ private HasLines(
final Func<String, Collection<String>> split
) {
super(
actual -> match.apply(split.apply(actual), expected),
desc -> desc.appendText("Lines are ").appendValue(expected),
(actual, desc) -> desc.appendValue(split.apply(actual))
new MatcherOf<>(
actual -> match.apply(split.apply(actual), expected),
desc -> desc.appendText("Lines are ").appendValue(expected),
(actual, desc) -> desc.appendValue(split.apply(actual))
)
);
}

Expand Down
21 changes: 11 additions & 10 deletions src/main/java/org/llorllale/cactoos/matchers/HasProperty.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,16 +84,17 @@ public HasProperty(final String key) {
*/
private HasProperty(final Matcher<Map.Entry<String, String>> entr) {
super(
// @checkstyle IndentationCheck (10 line)
properties -> new HasValuesMatching<>(
entr::matches
).matches(properties.entrySet()),
desc -> desc
.appendText("has property ")
.appendDescriptionOf(entr),
(properties, desc) -> desc
.appendText("has properties ")
.appendValue(properties)
new MatcherOf<>(
properties -> new HasValuesMatching<>(
entr::matches
).matches(properties.entrySet()),
desc -> desc
.appendText("has property ")
.appendDescriptionOf(entr),
(properties, desc) -> desc
.appendText("has properties ")
.appendValue(properties)
)
);
}

Expand Down
12 changes: 7 additions & 5 deletions src/main/java/org/llorllale/cactoos/matchers/HasSize.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,13 @@ public final class HasSize extends MatcherEnvelope<Iterable<?>> {
*/
public HasSize(final Integer size) {
super(
input -> new LengthOf(input).intValue() == size,
desc -> desc.appendText("has size ")
.appendValue(size),
(input, desc) -> desc.appendText("has size ")
.appendValue(new LengthOf(input).intValue())
new MatcherOf<>(
input -> new LengthOf(input).intValue() == size,
desc -> desc.appendText("has size ")
.appendValue(size),
(input, desc) -> desc.appendText("has size ")
.appendValue(new LengthOf(input).intValue())
)
);
}
}
10 changes: 6 additions & 4 deletions src/main/java/org/llorllale/cactoos/matchers/HasValues.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,12 @@ public HasValues(final X... expected) {
*/
public HasValues(final Iterable<X> expected) {
super(
actual -> new ListOf<>(actual).containsAll(new ListOf<>(expected)),
desc -> desc.appendText("contains ")
.appendValue(new TextOf(expected)),
(actual, desc) -> desc.appendValue(new TextOf(actual))
new MatcherOf<>(
actual -> new ListOf<>(actual).containsAll(new ListOf<>(expected)),
desc -> desc.appendText("contains ")
.appendValue(new TextOf(expected)),
(actual, desc) -> desc.appendValue(new TextOf(actual))
)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,15 @@ public final class HasValuesMatching<X> extends MatcherEnvelope<Iterable<X>> {
*/
public HasValuesMatching(final Func<X, Boolean> fnc) {
super(
actual -> new Or(fnc, actual).value(),
desc -> desc.appendText("The function matches at least 1 element."),
(actual, desc) -> desc.appendText(
new FormattedText(
"No any elements from [%s] matches by the function",
new TextOf(actual)
).asString()
new MatcherOf<>(
actual -> new Or(fnc, actual).value(),
desc -> desc.appendText("The function matches at least 1 element."),
(actual, desc) -> desc.appendText(
new FormattedText(
"No any elements from [%s] matches by the function",
new TextOf(actual)
).asString()
)
)
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,15 @@ public InputHasContent(final Text text) {
*/
public InputHasContent(final Matcher<String> mtr) {
super(
input -> mtr.matches(
new TextOf(input).asString()
),
desc -> desc.appendText("has content ")
.appendDescriptionOf(mtr),
(input, desc) -> desc.appendText("has content ")
.appendValue(
new TextOf(input).asString()
)
new MatcherOf<>(
input -> mtr.matches(new TextOf(input).asString()),
desc -> desc
.appendText("has content ")
.appendDescriptionOf(mtr),
(input, desc) -> desc
.appendText("has content ")
.appendValue(new TextOf(input).asString())
)
);
}
}
8 changes: 5 additions & 3 deletions src/main/java/org/llorllale/cactoos/matchers/IsBlank.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,11 @@ public final class IsBlank extends MatcherEnvelope<String> {
*/
public IsBlank() {
super(
text -> text.trim().isEmpty(),
desc -> desc.appendText("is blank"),
(text, desc) -> desc.appendValue(text)
new MatcherOf<>(
text -> text.trim().isEmpty(),
desc -> desc.appendText("is blank"),
(text, desc) -> desc.appendValue(text)
)
);
}
}
31 changes: 16 additions & 15 deletions src/main/java/org/llorllale/cactoos/matchers/IsEntry.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,21 +60,22 @@ public IsEntry(final K key, final V value) {
*/
public IsEntry(final Matcher<K> key, final Matcher<V> value) {
super(
// @checkstyle IndentationCheck (20 line)
entry -> new And(
() -> key.matches(entry.getKey()),
() -> value.matches(entry.getValue())
).value(),
desc -> {
IsEntry.descriptionOfKey(desc).appendDescriptionOf(key);
IsEntry.descriptionOfValue(desc).appendDescriptionOf(value);
},
(entry, desc) -> {
IsEntry.descriptionOfKey(desc);
key.describeMismatch(entry.getKey(), desc);
IsEntry.descriptionOfValue(desc);
value.describeMismatch(entry.getValue(), desc);
}
new MatcherOf<>(
entry -> new And(
() -> key.matches(entry.getKey()),
() -> value.matches(entry.getValue())
).value(),
desc -> {
IsEntry.descriptionOfKey(desc).appendDescriptionOf(key);
IsEntry.descriptionOfValue(desc).appendDescriptionOf(value);
},
(entry, desc) -> {
IsEntry.descriptionOfKey(desc);
key.describeMismatch(entry.getKey(), desc);
IsEntry.descriptionOfValue(desc);
value.describeMismatch(entry.getValue(), desc);
}
)
);
}

Expand Down
8 changes: 5 additions & 3 deletions src/main/java/org/llorllale/cactoos/matchers/IsTrue.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,11 @@ public final class IsTrue extends MatcherEnvelope<Boolean> {
*/
public IsTrue() {
super(
bool -> bool,
desc -> desc.appendValue(true),
(bool, desc) -> desc.appendValue(bool)
new MatcherOf<>(
bool -> bool,
desc -> desc.appendValue(true),
(bool, desc) -> desc.appendValue(bool)
)
);
}
}
44 changes: 0 additions & 44 deletions src/main/java/org/llorllale/cactoos/matchers/MatcherEnvelope.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,6 @@
*/
package org.llorllale.cactoos.matchers;

import org.cactoos.BiProc;
import org.cactoos.Func;
import org.cactoos.Proc;
import org.cactoos.func.UncheckedBiProc;
import org.cactoos.func.UncheckedFunc;
import org.cactoos.func.UncheckedProc;
import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.hamcrest.TypeSafeMatcher;
Expand All @@ -40,9 +34,6 @@
* Matcher Envelope.
* @param <T> The type of the Matcher.
* @since 1.0.0
* @todo #129:30min Refactor other matchers to extend MatcherEnvelope.
* If you do not know how to do it please refer to InputHasContent
* class as the example.
*/
public abstract class MatcherEnvelope<T> extends TypeSafeMatcher<T> {

Expand All @@ -51,41 +42,6 @@ public abstract class MatcherEnvelope<T> extends TypeSafeMatcher<T> {
*/
private final Matcher<T> origin;

/**
* Ctor.
* @param match Function matches an actual object with expected one
* @param description Procedure generates a description of the object
* @param mismatch BiProcedure generates a description for situation when an
* actual object does not match to the expected one
*/
protected MatcherEnvelope(
final Func<T, Boolean> match,
final Proc<Description> description,
final BiProc<T, Description> mismatch
) {
this(
new TypeSafeMatcher<T>() {
@Override
public void describeTo(final Description desc) {
new UncheckedProc<>(description).exec(desc);
}

@Override
protected void describeMismatchSafely(
final T item,
final Description desc
) {
new UncheckedBiProc<>(mismatch).exec(item, desc);
}

@Override
protected boolean matchesSafely(final T item) {
return new UncheckedFunc<>(match).apply(item);
}
}
);
}

/**
* Ctor.
* @param origin Encapsulated matcher.
Expand Down
Loading

0 comments on commit c7bc081

Please sign in to comment.