Skip to content

Commit

Permalink
(llorllale#135) TextMatcherEnvelope is now TextMatcher
Browse files Browse the repository at this point in the history
  • Loading branch information
victornoel committed Sep 13, 2020
1 parent 961d37f commit 88eb534
Show file tree
Hide file tree
Showing 11 changed files with 78 additions and 188 deletions.
14 changes: 8 additions & 6 deletions src/main/java/org/llorllale/cactoos/matchers/EndsWith.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
*
* @since 1.0.0
*/
public final class EndsWith extends TextMatcherEnvelope {
public final class EndsWith extends MatcherEnvelope<Text> {

/**
* Ctor.
Expand All @@ -50,11 +50,13 @@ public EndsWith(final String suffix) {
*/
public EndsWith(final Text text) {
super(
new MatcherOf<>(
(Text act) -> act.asString().endsWith(text.asString()),
text
),
"Text ending with "
new TextMatcher(
new MatcherOf<>(
(Text act) -> act.asString().endsWith(text.asString()),
text
),
"Text ending with "
)
);
}
}
Expand Down
14 changes: 8 additions & 6 deletions src/main/java/org/llorllale/cactoos/matchers/MatchesRegex.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
*
* @since 1.0.0
*/
public final class MatchesRegex extends TextMatcherEnvelope {
public final class MatchesRegex extends MatcherEnvelope<Text> {

/**
* Ctor.
Expand All @@ -50,11 +50,13 @@ public MatchesRegex(final String regex) {
*/
public MatchesRegex(final Text regex) {
super(
new MatcherOf<>(
(Text act) -> act.asString().matches(regex.asString()),
regex
),
"Text matches "
new TextMatcher(
new MatcherOf<>(
(Text act) -> act.asString().matches(regex.asString()),
regex
),
"Text matches "
)
);
}
}
Expand Down
14 changes: 8 additions & 6 deletions src/main/java/org/llorllale/cactoos/matchers/StartsWith.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
*
* @since 1.0.0
*/
public final class StartsWith extends TextMatcherEnvelope {
public final class StartsWith extends MatcherEnvelope<Text> {

/**
* Ctor.
Expand All @@ -50,11 +50,13 @@ public StartsWith(final String prefix) {
*/
public StartsWith(final Text text) {
super(
new MatcherOf<>(
(Text act) -> act.asString().startsWith(text.asString()),
text
),
"Text starting with "
new TextMatcher(
new MatcherOf<>(
(Text act) -> act.asString().startsWith(text.asString()),
text
),
"Text starting with "
)
);
}
}
Expand Down
14 changes: 8 additions & 6 deletions src/main/java/org/llorllale/cactoos/matchers/TextHasString.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
*
* @since 0.2
*/
public final class TextHasString extends TextMatcherEnvelope {
public final class TextHasString extends MatcherEnvelope<Text> {

/**
* Ctor.
Expand All @@ -50,11 +50,13 @@ public TextHasString(final String text) {
*/
public TextHasString(final Text text) {
super(
new MatcherOf<>(
(Text actual) -> actual.asString().contains(text.asString()),
text
),
"Text with "
new TextMatcher(
new MatcherOf<>(
(Text actual) -> actual.asString().contains(text.asString()),
text
),
"Text with "
)
);
}

Expand Down
14 changes: 8 additions & 6 deletions src/main/java/org/llorllale/cactoos/matchers/TextIs.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
*
* @since 1.0.0
*/
public final class TextIs extends TextMatcherEnvelope {
public final class TextIs extends MatcherEnvelope<Text> {

/**
* Ctor.
Expand All @@ -50,11 +50,13 @@ public TextIs(final String text) {
*/
public TextIs(final Text text) {
super(
new MatcherOf<>(
(Text actual) -> actual.asString().equals(text.asString()),
text
),
"Text with value "
new TextMatcher(
new MatcherOf<>(
(Text actual) -> actual.asString().equals(text.asString()),
text
),
"Text with value "
)
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@
* The Text-based {@link TypeSafeDiagnosingMatcher} envelope.
*
* @since 1.0.0
* @checkstyle ProtectedMethodInFinalClassCheck (200 lines)
*/
public abstract class TextMatcherEnvelope extends
TypeSafeDiagnosingMatcher<Text> {
public final class TextMatcher extends TypeSafeDiagnosingMatcher<Text> {

/**
* The matcher to test.
Expand All @@ -61,7 +61,7 @@ public abstract class TextMatcherEnvelope extends
* @param mtchr The matcher to test.
* @param expected The description of the matcher's expected text.
*/
public TextMatcherEnvelope(
public TextMatcher(
final Matcher<Text> mtchr, final String expected
) {
this(mtchr, expected, "Text is ");
Expand All @@ -73,7 +73,7 @@ public TextMatcherEnvelope(
* @param expected The description of the matcher's expected text.
* @param actual The description of the matcher's actual text.
*/
public TextMatcherEnvelope(
public TextMatcher(
final Matcher<Text> mtchr, final String expected, final String actual
) {
super();
Expand All @@ -83,16 +83,15 @@ public TextMatcherEnvelope(
}

@Override
public final void describeTo(final Description desc) {
public void describeTo(final Description desc) {
desc.appendText(this.expected).appendDescriptionOf(this.matcher);
}

@Override
protected final boolean matchesSafely(final Text text,
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));
}

}
59 changes: 10 additions & 49 deletions src/test/java/org/llorllale/cactoos/matchers/EndsWithTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,6 @@
package org.llorllale.cactoos.matchers;

import org.cactoos.text.TextOf;
import org.hamcrest.Description;
import org.hamcrest.StringDescription;
import org.hamcrest.core.IsEqual;
import org.junit.Test;

/**
Expand All @@ -40,61 +37,25 @@
*/
public final class EndsWithTest {

/**
* Example of {@link EndsWith} usage.
*/
@Test
public void matchPositive() {
public void matches() {
new Assertion<>(
"The matcher gives positive result for the valid arguments",
new TextOf("I'm simple and I know it."),
new EndsWith("know it.")
new EndsWith("know it."),
new Matches<>(new TextOf("I'm simple and I know it."))
).affirm();
}

/**
* Give the negative testing result for the invalid arguments.
*/
@Test
public void matchNegative() {
public void mismatches() {
new Assertion<>(
"The matcher gives negative result for the invalid arguments",
new EndsWith("!").matchesSafely(
() -> "The sentence.",
new StringDescription()
),
new IsEqual<>(false)
).affirm();
}

/**
* Matcher prints the actual value(s) properly in case of errors.
* The actual/expected section are using only when testing is failed and
* we need to explain what exactly went wrong.
*/
@Test
public void describeActualValues() {
final Description desc = new StringDescription();
new EndsWith("").matchesSafely(new TextOf("ABC"), desc);
new Assertion<>(
"The matcher print the value which came for testing",
desc.toString(),
new IsEqual<>("Text is \"ABC\"")
).affirm();
}

/**
* Matcher prints the expected value(s) properly.
* The user has the ability to specify the description for the function.
*/
@Test
public void describeExpectedValues() {
final Description desc = new StringDescription();
new EndsWith("!").describeTo(desc);
new Assertion<>(
"The matcher print the description of the scenario",
desc.toString(),
new IsEqual<>("Text ending with \"!\"")
new EndsWith("!"),
new Mismatches<>(
new TextOf("The sentence."),
"Text ending with \"!\"",
"Text is \"The sentence.\""
)
).affirm();
}
}
51 changes: 7 additions & 44 deletions src/test/java/org/llorllale/cactoos/matchers/MatchesRegexTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,6 @@
package org.llorllale.cactoos.matchers;

import org.cactoos.text.TextOf;
import org.hamcrest.Description;
import org.hamcrest.StringDescription;
import org.hamcrest.core.IsEqual;
import org.hamcrest.core.IsNot;
import org.junit.Test;

/**
Expand All @@ -41,58 +37,25 @@
*/
public final class MatchesRegexTest {

/**
* Example of {@link MatchesRegex} usage.
*/
@Test
public void matchPositive() {
public void matches() {
new Assertion<>(
"matches text that satisfies regex",
new MatchesRegex("^.*know\\sit\\.$"),
new Matches<>(new TextOf("I'm simple and I know it."))
).affirm();
}

/**
* Give the negative testing result for the invalid arguments.
*/
@Test
public void matchNegative() {
public void mismatches() {
new Assertion<>(
"does not match text that does not conform to the regex",
new MatchesRegex("^.*!$"),
new IsNot<>(new Matches<>(() -> "The sentence."))
).affirm();
}

/**
* Matcher prints the actual value(s) properly in case of errors.
* The actual/expected section are using only when testing is failed and
* we need to explain what exactly went wrong.
*/
@Test
public void describeActualValues() {
final Description desc = new StringDescription();
new MatchesRegex("").matchesSafely(new TextOf("ABC"), desc);
new Assertion<>(
"includes the test object in the description",
desc.toString(),
new IsEqual<>("Text is \"ABC\"")
).affirm();
}

/**
* Matcher prints the expected value(s) properly.
* The user has the ability to specify the description for the function.
*/
@Test
public void describeExpectedValues() {
final Description desc = new StringDescription();
new MatchesRegex("^.*\\.$").describeTo(desc);
new Assertion<>(
"describes the scenario",
desc.toString(),
new IsEqual<>("Text matches \"^.*\\.$\"")
new Mismatches<>(
new TextOf("The sentence."),
"Text matches \"^.*!$\"",
"Text is \"The sentence.\""
)
).affirm();
}
}
Loading

0 comments on commit 88eb534

Please sign in to comment.