From 88eb534435e4a4a2d84be1684045014074ec37d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Victor=20No=C3=ABl?= Date: Sun, 13 Sep 2020 17:20:17 +0200 Subject: [PATCH] (#135) TextMatcherEnvelope is now TextMatcher --- .../llorllale/cactoos/matchers/EndsWith.java | 14 +++-- .../cactoos/matchers/MatchesRegex.java | 14 +++-- .../cactoos/matchers/StartsWith.java | 14 +++-- .../cactoos/matchers/TextHasString.java | 14 +++-- .../llorllale/cactoos/matchers/TextIs.java | 14 +++-- ...tMatcherEnvelope.java => TextMatcher.java} | 13 ++-- .../cactoos/matchers/EndsWithTest.java | 59 ++++--------------- .../cactoos/matchers/MatchesRegexTest.java | 51 +++------------- .../cactoos/matchers/StartsWithTest.java | 51 +++------------- .../cactoos/matchers/TextHasStringTest.java | 6 +- ...EnvelopeTest.java => TextMatcherTest.java} | 16 ++--- 11 files changed, 78 insertions(+), 188 deletions(-) rename src/main/java/org/llorllale/cactoos/matchers/{TextMatcherEnvelope.java => TextMatcher.java} (91%) rename src/test/java/org/llorllale/cactoos/matchers/{TextMatcherEnvelopeTest.java => TextMatcherTest.java} (85%) diff --git a/src/main/java/org/llorllale/cactoos/matchers/EndsWith.java b/src/main/java/org/llorllale/cactoos/matchers/EndsWith.java index e9b3ceca..8f77947c 100644 --- a/src/main/java/org/llorllale/cactoos/matchers/EndsWith.java +++ b/src/main/java/org/llorllale/cactoos/matchers/EndsWith.java @@ -34,7 +34,7 @@ * * @since 1.0.0 */ -public final class EndsWith extends TextMatcherEnvelope { +public final class EndsWith extends MatcherEnvelope { /** * Ctor. @@ -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 " + ) ); } } diff --git a/src/main/java/org/llorllale/cactoos/matchers/MatchesRegex.java b/src/main/java/org/llorllale/cactoos/matchers/MatchesRegex.java index ca6df65f..e0aa52b5 100644 --- a/src/main/java/org/llorllale/cactoos/matchers/MatchesRegex.java +++ b/src/main/java/org/llorllale/cactoos/matchers/MatchesRegex.java @@ -34,7 +34,7 @@ * * @since 1.0.0 */ -public final class MatchesRegex extends TextMatcherEnvelope { +public final class MatchesRegex extends MatcherEnvelope { /** * Ctor. @@ -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 " + ) ); } } diff --git a/src/main/java/org/llorllale/cactoos/matchers/StartsWith.java b/src/main/java/org/llorllale/cactoos/matchers/StartsWith.java index ca5a2823..21519b38 100644 --- a/src/main/java/org/llorllale/cactoos/matchers/StartsWith.java +++ b/src/main/java/org/llorllale/cactoos/matchers/StartsWith.java @@ -34,7 +34,7 @@ * * @since 1.0.0 */ -public final class StartsWith extends TextMatcherEnvelope { +public final class StartsWith extends MatcherEnvelope { /** * Ctor. @@ -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 " + ) ); } } diff --git a/src/main/java/org/llorllale/cactoos/matchers/TextHasString.java b/src/main/java/org/llorllale/cactoos/matchers/TextHasString.java index 7cec7ba6..63b59c89 100644 --- a/src/main/java/org/llorllale/cactoos/matchers/TextHasString.java +++ b/src/main/java/org/llorllale/cactoos/matchers/TextHasString.java @@ -34,7 +34,7 @@ * * @since 0.2 */ -public final class TextHasString extends TextMatcherEnvelope { +public final class TextHasString extends MatcherEnvelope { /** * Ctor. @@ -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 " + ) ); } diff --git a/src/main/java/org/llorllale/cactoos/matchers/TextIs.java b/src/main/java/org/llorllale/cactoos/matchers/TextIs.java index cd93238e..9d527a3d 100644 --- a/src/main/java/org/llorllale/cactoos/matchers/TextIs.java +++ b/src/main/java/org/llorllale/cactoos/matchers/TextIs.java @@ -34,7 +34,7 @@ * * @since 1.0.0 */ -public final class TextIs extends TextMatcherEnvelope { +public final class TextIs extends MatcherEnvelope { /** * Ctor. @@ -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 " + ) ); } diff --git a/src/main/java/org/llorllale/cactoos/matchers/TextMatcherEnvelope.java b/src/main/java/org/llorllale/cactoos/matchers/TextMatcher.java similarity index 91% rename from src/main/java/org/llorllale/cactoos/matchers/TextMatcherEnvelope.java rename to src/main/java/org/llorllale/cactoos/matchers/TextMatcher.java index db48de22..4b55007f 100644 --- a/src/main/java/org/llorllale/cactoos/matchers/TextMatcherEnvelope.java +++ b/src/main/java/org/llorllale/cactoos/matchers/TextMatcher.java @@ -37,9 +37,9 @@ * The Text-based {@link TypeSafeDiagnosingMatcher} envelope. * * @since 1.0.0 + * @checkstyle ProtectedMethodInFinalClassCheck (200 lines) */ -public abstract class TextMatcherEnvelope extends - TypeSafeDiagnosingMatcher { +public final class TextMatcher extends TypeSafeDiagnosingMatcher { /** * The matcher to test. @@ -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 mtchr, final String expected ) { this(mtchr, expected, "Text is "); @@ -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 mtchr, final String expected, final String actual ) { super(); @@ -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)); } - } diff --git a/src/test/java/org/llorllale/cactoos/matchers/EndsWithTest.java b/src/test/java/org/llorllale/cactoos/matchers/EndsWithTest.java index 167842f7..d16f7a78 100644 --- a/src/test/java/org/llorllale/cactoos/matchers/EndsWithTest.java +++ b/src/test/java/org/llorllale/cactoos/matchers/EndsWithTest.java @@ -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; /** @@ -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(); } } diff --git a/src/test/java/org/llorllale/cactoos/matchers/MatchesRegexTest.java b/src/test/java/org/llorllale/cactoos/matchers/MatchesRegexTest.java index c8878c8d..0573ce9f 100644 --- a/src/test/java/org/llorllale/cactoos/matchers/MatchesRegexTest.java +++ b/src/test/java/org/llorllale/cactoos/matchers/MatchesRegexTest.java @@ -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; /** @@ -41,11 +37,8 @@ */ 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\\.$"), @@ -53,46 +46,16 @@ public void matchPositive() { ).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(); } } diff --git a/src/test/java/org/llorllale/cactoos/matchers/StartsWithTest.java b/src/test/java/org/llorllale/cactoos/matchers/StartsWithTest.java index 3119ac7e..a74cf2d0 100644 --- a/src/test/java/org/llorllale/cactoos/matchers/StartsWithTest.java +++ b/src/test/java/org/llorllale/cactoos/matchers/StartsWithTest.java @@ -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; /** @@ -41,11 +37,8 @@ */ public final class StartsWithTest { - /** - * Example of {@link StartsWith} usage. - */ @Test - public void matchPositive() { + public void matches() { new Assertion<>( "matches text with prefix", new StartsWith("I'm simple"), @@ -53,46 +46,16 @@ public void matchPositive() { ).affirm(); } - /** - * Give the negative testing result for the invalid arguments. - */ @Test - public void matchNegative() { + public void mismatches() { new Assertion<>( "mismatches text without the prefix", new StartsWith("!"), - new IsNot<>(new Matches<>(new TextOf("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 StartsWith("").matchesSafely(new TextOf("ABC"), desc); - new Assertion<>( - "describes the test arg", - 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 StartsWith("!").describeTo(desc); - new Assertion<>( - "describes the expected prefix", - desc.toString(), - new IsEqual<>("Text starting with \"!\"") + new Mismatches<>( + new TextOf("The sentence."), + "Text starting with \"!\"", + "Text is \"The sentence.\"" + ) ).affirm(); } diff --git a/src/test/java/org/llorllale/cactoos/matchers/TextHasStringTest.java b/src/test/java/org/llorllale/cactoos/matchers/TextHasStringTest.java index 90a742f7..37efd453 100644 --- a/src/test/java/org/llorllale/cactoos/matchers/TextHasStringTest.java +++ b/src/test/java/org/llorllale/cactoos/matchers/TextHasStringTest.java @@ -45,7 +45,7 @@ public void matchesPrefix() { new Assertion<>( "matches prefix", new TextHasString("123"), - new Matches<>(() -> "12345") + new Matches<>(new TextOf("12345")) ).affirm(); } @@ -54,7 +54,7 @@ public void matchesSuffix() { new Assertion<>( "matches suffix", new TextHasString("345"), - new Matches<>(() -> "12345") + new Matches<>(new TextOf("12345")) ).affirm(); } @@ -63,7 +63,7 @@ public void matchesInTheMiddle() { new Assertion<>( "matches substring", new TextHasString("234"), - new Matches<>(() -> "12345") + new Matches<>(new TextOf("12345")) ).affirm(); } diff --git a/src/test/java/org/llorllale/cactoos/matchers/TextMatcherEnvelopeTest.java b/src/test/java/org/llorllale/cactoos/matchers/TextMatcherTest.java similarity index 85% rename from src/test/java/org/llorllale/cactoos/matchers/TextMatcherEnvelopeTest.java rename to src/test/java/org/llorllale/cactoos/matchers/TextMatcherTest.java index 29aedd28..56fbb776 100644 --- a/src/test/java/org/llorllale/cactoos/matchers/TextMatcherEnvelopeTest.java +++ b/src/test/java/org/llorllale/cactoos/matchers/TextMatcherTest.java @@ -37,24 +37,18 @@ * @checkstyle JavadocMethodCheck (500 lines) * @checkstyle JavadocTypeCheck (500 lines) */ -public final class TextMatcherEnvelopeTest { +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 TextEquals(input), + new TextMatcher( + new MatcherOf<>((Text text) -> input.equals(text.asString())), + "Text equals to " + ), new Matches<>(new TextOf(new StringReader(input))) ).affirm(); } - - private static final class TextEquals extends TextMatcherEnvelope { - TextEquals(final String txt) { - super( - new MatcherOf<>((Text text) -> txt.equals(text.asString())), - "Text equals to " - ); - } - } }