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

Simplify TextMatcher #204

Merged
merged 2 commits into from
Sep 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 21 additions & 19 deletions src/main/java/org/llorllale/cactoos/matchers/Assertion.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
*/
package org.llorllale.cactoos.matchers;

import org.cactoos.scalar.Unchecked;
import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.hamcrest.StringDescription;
Expand Down Expand Up @@ -73,15 +72,21 @@
* Matchers derived from BaseMatcher, such as IsEqual.
*/
public final class Assertion<T> {

/**
* Message.
*/
private final String msg;

/**
* Whether this assertion is refuted.
* Tested value.
*/
private final Unchecked<Boolean> refuted;
private final T test;

/**
* Refutation error.
* Matcher.
*/
private final Unchecked<AssertionError> error;
private final Matcher<T> matcher;

/**
* Ctor.
Expand All @@ -92,27 +97,24 @@ public final class Assertion<T> {
public Assertion(
final String msg, final T test, final Matcher<T> matcher
) {
this.refuted = new Unchecked<>(() -> !matcher.matches(test));
this.error = new Unchecked<>(
() -> {
final Description text = new StringDescription();
text.appendText(msg)
.appendText(String.format("%nExpected: "))
.appendDescriptionOf(matcher)
.appendText(String.format("%n but was: "));
matcher.describeMismatch(test, text);
return new AssertionError(text.toString());
}
);
this.msg = msg;
Copy link
Contributor

Choose a reason for hiding this comment

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

@victornoel Shouldn't the names of fields and ctor parameters be different?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@andreoss some people seems to think it is important, but since I am ARC, I've never enforced that for anybody because I don't think it is useful or has any advantage, the opposite actually, it forces you to invent new names for the same thing...
Why do you think it should be the case? I could change my mind

Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@andreoss good find!! thanks 👍 :)

this.test = test;
this.matcher = matcher;
}

/**
* Affirm this assertion.
* @throws AssertionError if this assertion is refuted
*/
public void affirm() throws AssertionError {
if (this.refuted.value()) {
throw this.error.value();
if (!this.matcher.matches(this.test)) {
final Description text = new StringDescription();
text.appendText(this.msg)
.appendText(String.format("%nExpected: "))
.appendDescriptionOf(this.matcher)
.appendText(String.format("%n but was: "));
this.matcher.describeMismatch(this.test, text);
throw new AssertionError(text.toString());
}
}
}
2 changes: 1 addition & 1 deletion src/main/java/org/llorllale/cactoos/matchers/EndsWith.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public EndsWith(final Text text) {
super(
new TextMatcher(
new MatcherOf<>(
(Text act) -> act.asString().endsWith(text.asString()),
(String act) -> act.endsWith(text.asString()),
text
),
"Text ending with "
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public MatchesRegex(final Text regex) {
super(
new TextMatcher(
new MatcherOf<>(
(Text act) -> act.asString().matches(regex.asString()),
(String act) -> act.matches(regex.asString()),
regex
),
"Text matches "
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public StartsWith(final Text text) {
super(
new TextMatcher(
new MatcherOf<>(
(Text act) -> act.asString().startsWith(text.asString()),
(String act) -> act.startsWith(text.asString()),
text
),
"Text starting with "
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public TextHasString(final Text text) {
super(
new TextMatcher(
new MatcherOf<>(
(Text actual) -> actual.asString().contains(text.asString()),
(String actual) -> actual.contains(text.asString()),
text
),
"Text with "
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/llorllale/cactoos/matchers/TextIs.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public TextIs(final Text text) {
super(
new TextMatcher(
new MatcherOf<>(
(Text actual) -> actual.asString().equals(text.asString()),
(String actual) -> actual.equals(text.asString()),
text
),
"Text with value "
Expand Down
14 changes: 6 additions & 8 deletions src/main/java/org/llorllale/cactoos/matchers/TextMatcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,13 @@
package org.llorllale.cactoos.matchers;

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.TypeSafeDiagnosingMatcher;

/**
* The Text-based {@link TypeSafeDiagnosingMatcher} envelope.
* Generic {@link Matcher} of {@link Text}.
*
* @since 1.0.0
* @checkstyle ProtectedMethodInFinalClassCheck (200 lines)
Expand All @@ -44,7 +43,7 @@ public final class TextMatcher extends TypeSafeDiagnosingMatcher<Text> {
/**
* The matcher to test.
*/
private final Matcher<Text> matcher;
private final Matcher<String> matcher;

/**
* The description of the matcher's expected text.
Expand All @@ -62,7 +61,7 @@ public final class TextMatcher extends TypeSafeDiagnosingMatcher<Text> {
* @param expected The description of the matcher's expected text.
*/
public TextMatcher(
final Matcher<Text> mtchr, final String expected
final Matcher<String> mtchr, final String expected
) {
this(mtchr, expected, "Text is ");
}
Expand All @@ -74,7 +73,7 @@ public TextMatcher(
* @param actual The description of the matcher's actual text.
*/
public TextMatcher(
final Matcher<Text> mtchr, final String expected, final String actual
final Matcher<String> mtchr, final String expected, final String actual
) {
super();
this.matcher = mtchr;
Expand All @@ -88,10 +87,9 @@ public void describeTo(final Description desc) {
}

@Override
protected boolean matchesSafely(final Text text,
final Description desc) {
protected boolean matchesSafely(final Text text, final Description desc) {
final String txt = new UncheckedText(text).asString();
desc.appendText(this.actual).appendValue(txt);
return this.matcher.matches(new TextOf(txt));
return this.matcher.matches(txt);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,25 +27,25 @@
package org.llorllale.cactoos.matchers;

import java.io.StringReader;
import org.cactoos.Text;
import org.cactoos.text.TextOf;
import org.hamcrest.core.IsEqual;
import org.junit.Test;

/**
* Tests for {@link TextMatcherEnvelope}.
* Tests for {@link TextMatcher}.
*
* @since 1.0.0
* @checkstyle JavadocMethodCheck (500 lines)
* @checkstyle JavadocTypeCheck (500 lines)
*/
public final class TextMatcherTest {

@Test
public void matchesAReadOnceInput() {
final String input = "aaaa";
new Assertion<>(
"must match on an input that can be read only once",
new TextMatcher(
new MatcherOf<>((Text text) -> input.equals(text.asString())),
new IsEqual<>(input),
"Text equals to "
),
new Matches<>(new TextOf(new StringReader(input)))
Expand Down