Skip to content

Commit

Permalink
(#20) Cleanup matcher
Browse files Browse the repository at this point in the history
  • Loading branch information
victornoel committed Feb 14, 2021
1 parent 7364167 commit ce70d22
Show file tree
Hide file tree
Showing 18 changed files with 141 additions and 205 deletions.
5 changes: 0 additions & 5 deletions src/main/java/org/llorllale/cactoos/matchers/HasContent.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,6 @@
/**
* 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.
*/
@SuppressWarnings("PMD.AvoidDuplicateLiterals")
public final class HasContent extends MatcherEnvelope<Input> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public final class HasProperty extends MatcherEnvelope<Properties> {
* @param key Literal key.
* @param value Literal value.
*/
HasProperty(final String key, final String value) {
public HasProperty(final String key, final String value) {
this(new IsEqual<>(key), new IsEqual<>(value));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,16 @@
*/
package org.llorllale.cactoos.matchers;

import org.hamcrest.BaseMatcher;
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
*/
public abstract class MatcherEnvelope<T> extends TypeSafeMatcher<T> {
public abstract class MatcherEnvelope<T> extends BaseMatcher<T> {

/**
* The matcher to test.
Expand All @@ -57,13 +57,12 @@ public final void describeTo(final Description desc) {
}

@Override
protected final void describeMismatchSafely(final T item,
final Description desc) {
this.origin.describeMismatch(item, desc);
public final boolean matches(final Object actual) {
return this.origin.matches(actual);
}

@Override
protected final boolean matchesSafely(final T item) {
return this.origin.matches(item);
public final void describeMismatch(final Object item, final Description description) {
this.origin.describeMismatch(item, description);
}
}
13 changes: 8 additions & 5 deletions src/main/java/org/llorllale/cactoos/matchers/Matches.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.hamcrest.TypeSafeDiagnosingMatcher;
import org.hamcrest.TypeSafeMatcher;

/**
* Matcher to test {@link org.hamcrest.Matcher} objects.
Expand All @@ -52,8 +52,7 @@
* @since 1.0.0
* @checkstyle ProtectedMethodInFinalClassCheck (200 lines)
*/
public final class Matches<X, M extends Matcher<X>> extends
TypeSafeDiagnosingMatcher<M> {
public final class Matches<X, M extends Matcher<X>> extends TypeSafeMatcher<M> {

/**
* The testing arguments for the target matcher.
Expand All @@ -75,8 +74,12 @@ public void describeTo(final Description desc) {
}

@Override
protected boolean matchesSafely(final M matcher, final Description dsc) {
matcher.describeTo(dsc);
protected boolean matchesSafely(final M matcher) {
return matcher.matches(this.args);
}

@Override
protected void describeMismatchSafely(final M matcher, final Description dsc) {
matcher.describeTo(dsc);
}
}
21 changes: 8 additions & 13 deletions src/main/java/org/llorllale/cactoos/matchers/MatchesBefore.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
package org.llorllale.cactoos.matchers;

import java.util.concurrent.TimeoutException;
import org.cactoos.Func;
import org.cactoos.func.Timed;
import org.hamcrest.Description;
import org.hamcrest.Matcher;
Expand All @@ -53,10 +52,7 @@
* @param <T> Type of the scalar's value
* @since 1.0.0
*/
@SuppressWarnings({
"PMD.AvoidCatchingGenericException",
"PMD.ProtectedMethodInFinalClassCheck"
})
@SuppressWarnings("PMD.AvoidCatchingGenericException")
public final class MatchesBefore<T> extends TypeSafeDiagnosingMatcher<T> {
/**
* Time unit.
Expand Down Expand Up @@ -99,22 +95,21 @@ public void describeTo(final Description desc) {
protected boolean matchesSafely(
final T item, final Description desc
) {
boolean result = false;
final Func<T, Boolean> func =
new Timed<>(this.matcher::matches, this.millisec);
boolean matches = false;
try {
result = func.apply(item);
this.matcher.describeMismatch(item, desc);
matches = new Timed<>(this.matcher::matches, this.millisec).apply(item);
if (!matches) {
this.matcher.describeMismatch(item, desc);
}
} catch (final TimeoutException texc) {
desc.appendText("Timeout after ")
.appendValue(this.millisec)
.appendText(" ")
.appendText(MatchesBefore.TIME_UNIT);
// @checkstyle IllegalCatchCheck (1 line)
} catch (final Exception ex) {
desc.appendText("Thrown ")
.appendValue(ex);
throw new IllegalStateException(ex);
}
return result;
return matches;
}
}
39 changes: 26 additions & 13 deletions src/main/java/org/llorllale/cactoos/matchers/RunsInThreads.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,18 @@
package org.llorllale.cactoos.matchers;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import org.cactoos.Func;
import org.cactoos.scalar.And;
import org.cactoos.scalar.Unchecked;
import org.cactoos.iterable.Mapped;
import org.cactoos.scalar.SumOf;
import org.cactoos.scalar.Ternary;
import org.hamcrest.Description;
import org.hamcrest.TypeSafeMatcher;
import org.hamcrest.TypeSafeDiagnosingMatcher;

/**
* Matcher for {@link Func} that must run in multiple threads.
Expand All @@ -46,7 +47,7 @@
* @since 0.24
* @checkstyle JavadocMethodCheck (500 lines)
*/
public final class RunsInThreads<T> extends TypeSafeMatcher<Func<T, Boolean>> {
public final class RunsInThreads<T> extends TypeSafeDiagnosingMatcher<Func<T, Boolean>> {

/**
* Input.
Expand Down Expand Up @@ -86,12 +87,15 @@ public RunsInThreads(final T object, final int threads) {
}

@Override
public boolean matchesSafely(final Func<T, Boolean> func) {
public boolean matchesSafely(
final Func<T, Boolean> func,
final Description desc
) {
final ExecutorService service = Executors.newFixedThreadPool(
this.total
);
final CountDownLatch latch = new CountDownLatch(1);
final Collection<Future<Boolean>> futures = new ArrayList<>(this.total);
final List<Future<Boolean>> futures = new ArrayList<>(this.total);
final Callable<Boolean> task = () -> {
latch.await();
return func.apply(this.input);
Expand All @@ -100,18 +104,27 @@ public boolean matchesSafely(final Func<T, Boolean> func) {
futures.add(service.submit(task));
}
latch.countDown();
final boolean matches = new Unchecked<>(
new And(
(Func<Future<Boolean>, Boolean>) Future::get,
final int matching = new SumOf(
new Mapped<>(
f -> new Ternary<>(f.get(), 1, 0).value(),
futures
)
).value();
).intValue();
service.shutdown();
return matches;
if (matching != this.total) {
desc
.appendText("Ran successfuly in ")
.appendValue(matching)
.appendText(" threads");
}
return matching == this.total;
}

@Override
public void describeTo(final Description description) {
description.appendText("failed");
description
.appendText("Runs in ")
.appendValue(this.total)
.appendText(" threads successfuly");
}
}
18 changes: 10 additions & 8 deletions src/main/java/org/llorllale/cactoos/matchers/Throws.java
Original file line number Diff line number Diff line change
Expand Up @@ -108,25 +108,27 @@ public void describeTo(final Description dsc) {
@SuppressWarnings(
{ "PMD.AvoidCatchingGenericException", "PMD.AvoidCatchingThrowable" }
)
protected boolean matchesSafely(final Scalar<T> obj,
final Description dsc) {
protected boolean matchesSafely(
final Scalar<T> obj,
final Description dsc
) {
// @checkstyle IllegalCatchCheck (20 lines)
boolean matches;
try {
obj.value();
matches = false;
dsc.appendText("The exception wasn't thrown.");
} catch (final Throwable cause) {
dsc
.appendText("Exception has type '")
.appendText(cause.getClass().getName())
.appendText("' and message '")
.appendText(cause.getMessage())
.appendText("'");
if (this.type.isAssignableFrom(cause.getClass())
&& this.msg.matches(cause.getMessage())) {
matches = true;
} else {
dsc
.appendText("Exception has type '")
.appendText(cause.getClass().getName())
.appendText("' and message '")
.appendText(cause.getMessage())
.appendText("'");
matches = false;
}
}
Expand Down
29 changes: 0 additions & 29 deletions src/test/java/org/llorllale/cactoos/matchers/HasContentTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,13 @@
package org.llorllale.cactoos.matchers;

import org.cactoos.io.InputOf;
import org.hamcrest.Description;
import org.hamcrest.StringDescription;
import org.hamcrest.core.IsEqual;
import org.junit.jupiter.api.Test;

/**
* Test case for {@link HasContent}.
*
* @since 1.0.0
*/
@SuppressWarnings("PMD.AvoidDuplicateLiterals")
public final class HasContentTest {

@Test
Expand All @@ -62,29 +58,4 @@ void mismatchInputContent() {
)
).affirm();
}

@Test
void describesMismatch() {
final Description description = new StringDescription();
new HasContent("world").describeMismatchSafely(
new InputOf("hello"), description
);
new Assertion<>(
"includes the specified contents when describing a mismatch",
description.toString(),
new IsEqual<>("has content \"hello\"")
).affirm();
}

@Test
void describesExpectedValues() {
final Description description = new StringDescription();
new HasContent("world").describeTo(description);
new Assertion<>(
"includes the specified contents when describing itself",
description.toString(),
new IsEqual<>("has content \"world\"")
).affirm();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ void positiveMatch() {
* Simple positive case only for key.
*/
@Test
void positiveMachKey() {
void positiveMatchKey() {
new Assertion<>(
"must match 'b=...'",
new HasValue<>(new HasProperty("b")),
Expand Down
10 changes: 0 additions & 10 deletions src/test/java/org/llorllale/cactoos/matchers/HasStringTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
package org.llorllale.cactoos.matchers;

import org.cactoos.text.TextOf;
import org.hamcrest.core.IsNot;
import org.junit.jupiter.api.Test;

/**
Expand Down Expand Up @@ -68,15 +67,6 @@ void matchesInTheMiddle() {

@Test
void mismatch() {
new Assertion<>(
"does not match text not containing the given string",
new HasString("xyz"),
new IsNot<>(new Matches<>(new TextOf("abc")))
).affirm();
}

@Test
void describesMismatch() {
new Assertion<>(
"describes mismatch correctly",
new HasString("xyz456"),
Expand Down
22 changes: 6 additions & 16 deletions src/test/java/org/llorllale/cactoos/matchers/IsApplicableTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
*/
package org.llorllale.cactoos.matchers;

import org.hamcrest.core.IsNot;
import org.cactoos.func.FuncOf;
import org.junit.jupiter.api.Test;

/**
Expand All @@ -35,34 +35,24 @@
* @since 1.0
* @checkstyle MagicNumber (100 line)
*/
public final class IsApplicableTest {
final class IsApplicableTest {

@Test
void matchFuncs() {
void matches() {
new Assertion<>(
"matches function that produces same output from the given input",
new IsApplicable<>(1, 1),
new Matches<>(x -> x)
new Matches<>(new FuncOf<>(x -> x))
).affirm();
}

@Test
void mismatchFuncs() {
new Assertion<>(
// @checkstyle LineLength (1 line)
"does not match function that produces different output from the given input",
new IsApplicable<>(1, 1),
new IsNot<>(new Matches<>(x -> 3 * x))
).affirm();
}

@Test
void describesMismatch() {
void mismatches() {
new Assertion<>(
"describes mismatch",
new IsApplicable<>(1, 1),
new Mismatches<>(
x -> 3 * x,
new FuncOf<>(x -> 3 * x),
"Func with <1>",
"Func with <3>"
)
Expand Down
Loading

1 comment on commit ce70d22

@0pdd
Copy link
Collaborator

@0pdd 0pdd commented on ce70d22 Feb 14, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Puzzle 20-e1bad078 discovered in src/test/java/org/llorllale/cactoos/matchers/ThrowsTest.java and submitted as #230. Please, remember that the puzzle was not necessarily added in this particular commit. Maybe it was added earlier, but we discovered it only now.

Please sign in to comment.