Skip to content

Commit

Permalink
(llorllale#94) Inherit MatcherEnvelope and fix a test
Browse files Browse the repository at this point in the history
  • Loading branch information
vzurauskas committed Apr 30, 2019
1 parent ef46413 commit 7fff8ac
Show file tree
Hide file tree
Showing 7 changed files with 131 additions and 104 deletions.
43 changes: 13 additions & 30 deletions src/main/java/org/llorllale/cactoos/matchers/FuncApplies.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@

import org.cactoos.Func;
import org.cactoos.func.UncheckedFunc;
import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.hamcrest.TypeSafeMatcher;
import org.hamcrest.core.IsEqual;

/**
Expand All @@ -40,17 +38,8 @@
* @param <Y> Type of output
* @since 0.2
*/
public final class FuncApplies<X, Y> extends TypeSafeMatcher<Func<X, Y>> {

/**
* Input of the function.
*/
private final X input;

/**
* Matcher of the result.
*/
private final Matcher<Y> matcher;
@SuppressWarnings("PMD.AvoidDuplicateLiterals")
public final class FuncApplies<X, Y> extends MatcherEnvelope<Func<X, Y>> {

/**
* Ctor.
Expand All @@ -63,25 +52,19 @@ public FuncApplies(final X inpt, final Y result) {

/**
* Ctor.
* @param inpt Input for the function
* @param input Input for the function
* @param mtr Matcher of the text
*/
public FuncApplies(final X inpt, final Matcher<Y> mtr) {
super();
this.input = inpt;
this.matcher = mtr;
}

@Override
public boolean matchesSafely(final Func<X, Y> func) {
return this.matcher.matches(
new UncheckedFunc<>(func).apply(this.input)
public FuncApplies(final X input, final Matcher<Y> mtr) {
super(
// @checkstyle IndentationCheck (7 line)
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))
);
}

@Override
public void describeTo(final Description description) {
description.appendText("Func with ");
description.appendDescriptionOf(this.matcher);
}
}
69 changes: 29 additions & 40 deletions src/main/java/org/llorllale/cactoos/matchers/HasLines.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,14 @@

import java.util.Collection;
import org.cactoos.BiFunc;
import org.cactoos.Func;
import org.cactoos.Scalar;
import org.cactoos.Text;
import org.cactoos.collection.CollectionOf;
import org.cactoos.collection.Mapped;
import org.cactoos.func.UncheckedBiFunc;
import org.cactoos.func.UncheckedFunc;
import org.cactoos.iterable.IterableOf;
import org.cactoos.list.ListOf;
import org.hamcrest.Description;
import org.hamcrest.TypeSafeMatcher;
import org.cactoos.scalar.UncheckedScalar;

/**
* Allows to check that text has lines considering platform-dependent line
Expand All @@ -46,22 +44,7 @@
* @since 1.0.0
* @checkstyle ProtectedMethodInFinalClassCheck (200 lines)
*/
public final class HasLines extends TypeSafeMatcher<String> {

/**
* The function to split the text which came for testing.
*/
private final UncheckedFunc<String, Collection<String>> split;

/**
* The expected lines.
*/
private final Collection<String> expected;

/**
* The function to match the actual/expected lines.
*/
private final BiFunc<Collection<String>, Collection<String>, Boolean> fnc;
public final class HasLines extends MatcherEnvelope<String> {

/**
* Ctor.
Expand Down Expand Up @@ -98,30 +81,36 @@ public HasLines(
final Scalar<String> sep,
final Collection<String> lns
) {
super();
this.fnc = fnc;
this.expected = lns;
this.split = new UncheckedFunc<>(
text -> new CollectionOf<>(text.split(sep.value()))
this(
fnc,
lns,
text -> new CollectionOf<>(
text.split(
new UncheckedScalar<>(sep).value()
)
)
);
}

@Override
public void describeTo(final Description desc) {
desc.appendText("Lines are ").appendValue(this.expected);
}

@Override
protected boolean matchesSafely(final String text) {
return new UncheckedBiFunc<>(this.fnc).apply(
this.split.apply(text), this.expected
/**
* Ctor.
* @param match The function to match the actual/expected lines.
* @param expected The expected lines to be present.
* @param split The function to split the text which came for testing.
*/
private HasLines(
final BiFunc<Collection<String>, Collection<String>, Boolean> match,
final Collection<String> expected,
final Func<String, Collection<String>> split
) {
super(
// @checkstyle IndentationCheck (5 line)
actual -> match.apply(
split.apply(actual), expected
),
desc -> desc.appendText("Lines are ").appendValue(expected),
(actual, desc) -> desc.appendValue(split.apply(actual))
);
}

@Override
protected void describeMismatchSafely(
final String text, final Description description
) {
description.appendValue(this.split.apply(text));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,14 @@
* Matcher Envelope.
* @param <T> The type of the Matcher.
* @since 1.0.0
* @todo #75:30min Refactor other matchers to extend MatcherEnvelope.
* @todo #94:30min Refactor other matchers to extend MatcherEnvelope.
* If you do not know how to do it please refer to InputHasContent
* class as the example.
*
* @todo #94:30min Write unit tests for MatcherEnvelope.
* Create MatcherEnvelopeTest class and write tests to cover the whole
* functionality. Since MatcherEnvelope is abstract, create a private
* nested child class and use it for tests.
*/
public abstract class MatcherEnvelope<T> extends TypeSafeMatcher<T> {

Expand All @@ -58,7 +63,7 @@ public abstract class MatcherEnvelope<T> extends TypeSafeMatcher<T> {
* @param mismatch BiProcedure generates a description for situation when an
* actual object does not match to the expected one
*/
public MatcherEnvelope(
protected MatcherEnvelope(
final Func<T, Boolean> match,
final Proc<Description> description,
final BiProc<T, Description> mismatch
Expand Down Expand Up @@ -90,7 +95,7 @@ protected boolean matchesSafely(final T item) {
* Ctor.
* @param origin Encapsulated matcher.
*/
public MatcherEnvelope(final Matcher<T> origin) {
protected MatcherEnvelope(final Matcher<T> origin) {
super();
this.origin = origin;
}
Expand Down
36 changes: 8 additions & 28 deletions src/main/java/org/llorllale/cactoos/matchers/MatcherOf.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,8 @@
import org.cactoos.Proc;
import org.cactoos.Text;
import org.cactoos.func.FuncOf;
import org.cactoos.func.UncheckedFunc;
import org.cactoos.text.FormattedText;
import org.cactoos.text.UncheckedText;
import org.hamcrest.Description;
import org.hamcrest.TypeSafeMatcher;

/**
* Func as Matcher.
Expand All @@ -44,17 +41,7 @@
* @param <T> Type of object to match
* @since 0.12
*/
public final class MatcherOf<T> extends TypeSafeMatcher<T> {

/**
* The func.
*/
private final Func<T, Boolean> func;

/**
* Matcher description.
*/
private final UncheckedText desc;
public final class MatcherOf<T> extends MatcherEnvelope<T> {

/**
* Ctor.
Expand All @@ -78,20 +65,13 @@ public MatcherOf(final Func<T, Boolean> fnc) {
* @param description The description
*/
public MatcherOf(final Func<T, Boolean> fnc, final Text description) {
super();
this.func = fnc;
this.desc = new UncheckedText(
new FormattedText("\"%s\"", description)
super(
// @checkstyle IndentationCheck (5 line)
fnc,
desc -> desc.appendText(
new FormattedText("\"%s\"", description).asString()
),
(actual, desc) -> desc.appendValue(actual)
);
}

@Override
public boolean matchesSafely(final T item) {
return new UncheckedFunc<>(this.func).apply(item);
}

@Override
public void describeTo(final Description description) {
description.appendText(this.desc.asString());
}
}
13 changes: 10 additions & 3 deletions src/test/java/org/llorllale/cactoos/matchers/AssertionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@
*/
package org.llorllale.cactoos.matchers;

import java.io.IOException;
import java.util.concurrent.atomic.AtomicInteger;
import org.cactoos.text.JoinedText;
import org.cactoos.text.TextOf;
import org.hamcrest.core.IsEqual;
import org.junit.Rule;
Expand All @@ -35,7 +37,6 @@

/**
* Tests for {@link Assertion}.
*
* @since 1.0.0
*/
@SuppressWarnings("PMD.AvoidDuplicateLiterals")
Expand All @@ -62,12 +63,18 @@ public void affirmIfResultMatches() {
/**
* Assertion must be refuted if the operation being tested does not
* match.
*
* @throws IOException if something goes wrong.
*/
@Test
public void refuteIfResultDoesNotMatch() {
public void refuteIfResultDoesNotMatch() throws IOException {
this.exception.expect(AssertionError.class);
this.exception.expectMessage(
"Text with value \"no match\"\n but was: Text is \"test\""
new JoinedText(
System.lineSeparator(),
"Text with value \"no match\"",
" but was: Text is \"test\""
).asString()
);
new Assertion<>(
"must refute the assertion if the test's result is not as expected",
Expand Down
25 changes: 25 additions & 0 deletions src/test/java/org/llorllale/cactoos/matchers/FuncAppliesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@
package org.llorllale.cactoos.matchers;

import org.hamcrest.core.IsNot;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

/**
* Test case for {@link FuncApplies}.
Expand All @@ -36,6 +38,13 @@
* @checkstyle JavadocMethodCheck (500 lines)
*/
public final class FuncAppliesTest {

/**
* A rule for handling an exception.
*/
@Rule
public final ExpectedException exception = ExpectedException.none();

@Test
public void matchFuncs() {
new Assertion<>(
Expand All @@ -55,4 +64,20 @@ public void mismatchFuncs() {
new IsNot<>(new Matches<>(x -> 3 * x))
).affirm();
}

@Test
public void describesMismatch() {
this.exception.expect(AssertionError.class);
this.exception.expectMessage(
String.format(
"Expected: Func with <1>%n but was: Func with <3>"
)
);
new Assertion<>(
"describes mismatch",
// @checkstyle MagicNumber (1 line)
x -> 3 * x,
new FuncApplies<>(1, 1)
).affirm();
}
}
Loading

0 comments on commit 7fff8ac

Please sign in to comment.