Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#75 MatcherEnvelope for TypeSafeMatcher #87

Merged
merged 12 commits into from
Jan 27, 2019
53 changes: 21 additions & 32 deletions src/main/java/org/llorllale/cactoos/matchers/InputHasContent.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,27 +29,17 @@
import org.cactoos.Input;
import org.cactoos.Text;
import org.cactoos.text.TextOf;
import org.cactoos.text.UncheckedText;
import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.hamcrest.TypeSafeMatcher;

/**
* Matcher for the input.
*
* @since 0.11
* @todo #7:30min All the matchers should implement describeMismatchSafely and
* their tests must verify that the implementation of the descriptions
* are satisfactory. The matchers should not expose publicly the xxxSafely
* method and the tests should rely on actual real use with assertThat.
* See ScalarHasValueTest for an example of a satisfactory result.
* @todo #75:30min Remove checkstyle suppression when qulice will fix this issue
* See https://github.com/teamed/qulice/issues/985.
* When new version will be released, update qulice and remove this puzzle.
*/
public final class InputHasContent extends TypeSafeMatcher<Input> {

/**
* Matcher of the value.
*/
private final Matcher<String> matcher;
@SuppressWarnings("PMD.AvoidDuplicateLiterals")
public final class InputHasContent extends MatcherEnvelope<Input> {

/**
* Ctor.
Expand All @@ -64,30 +54,29 @@ public InputHasContent(final String text) {
* @param text The text to match against
*/
public InputHasContent(final Text text) {
this(new MatcherOf<>((String input) -> text.asString().equals(input)));
this(
new MatcherOf<>(
(String input) -> text.asString().equals(input), text
)
);
}

/**
* Ctor.
* @param mtr Matcher of the text
*/
public InputHasContent(final Matcher<String> mtr) {
super();
this.matcher = mtr;
}

@Override
public boolean matchesSafely(final Input item) {
return this.matcher.matches(
new UncheckedText(
new TextOf(item)
).asString()
super(
// @checkstyle IndentationCheck (9 line)
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()
)
);
}

@Override
public void describeTo(final Description description) {
description.appendText("Input with ");
description.appendDescriptionOf(this.matcher);
}
}
113 changes: 113 additions & 0 deletions src/main/java/org/llorllale/cactoos/matchers/MatcherEnvelope.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/*
* The MIT License (MIT)
*
* Copyright (c) for portions of project cactoos-matchers are held by
* Yegor Bugayenko, 2017-2018, as part of project cactoos.
* All other copyright for project cactoos-matchers are held by
* George Aristy, 2018.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
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;

/**
* Matcher Envelope.
* @param <T> The type of the Matcher.
* @since 1.0.0
* @todo #75: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> {

/**
* The matcher to test.
*/
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
*/
public 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.
*/
public MatcherEnvelope(final Matcher<T> origin) {
super();
this.origin = origin;
}

@Override
public final void describeTo(final Description desc) {
this.origin.describeTo(desc);
}

@Override
protected final void describeMismatchSafely(final T item,
final Description desc) {
this.origin.describeMismatch(item, desc);
}

@Override
protected final boolean matchesSafely(final T item) {
return this.origin.matches(item);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,20 @@
package org.llorllale.cactoos.matchers;

import org.cactoos.io.InputOf;
import org.hamcrest.core.IsNot;
import org.cactoos.text.JoinedText;
import org.cactoos.text.UncheckedText;
import org.hamcrest.Description;
import org.hamcrest.StringDescription;
import org.junit.Test;

/**
* Test case for {@link org.llorllale.cactoos.matchers.InputHasContent}.
*
* @since 1.0.0
* @checkstyle JavadocMethodCheck (500 lines)
* @checkstyle StringLiteralsConcatenationCheck (500 lines)
*/
@SuppressWarnings("PMD.AvoidDuplicateLiterals")
public final class InputHasContentTest {

@Test
Expand All @@ -52,8 +57,34 @@ public void matchesInputContent() {
public void failsIfContentDoesNotMatch() {
new Assertion<>(
"Matcher matches values that are not equal",
() -> new InputOf("hello"),
new IsNot<>(new InputHasContent("world"))
() -> {
final Description description = new StringDescription();
new InputHasContent("world").describeMismatchSafely(
new InputOf("hello"), description
);
return new UncheckedText(description.toString());
},
new TextIs("has content \"hello\"")
).affirm();
}

@Test
public void describesExpectedValues() {
new Assertion<>(
new UncheckedText(
new JoinedText(
" ",
"The matcher print the value which should be present",
"in the target iterable"
)
).asString(),
() -> {
final Description description = new StringDescription();
new InputHasContent("world").describeTo(description);
return new UncheckedText(description.toString());
},
new TextIs("has content \"world\"")
).affirm();
}

}