From 9495214f1f1b8339511efdc8e344234b47586c15 Mon Sep 17 00:00:00 2001 From: Samuel Liang <47774610+Harlan1997@users.noreply.github.com> Date: Sun, 25 Apr 2021 19:13:22 +0800 Subject: [PATCH 1/3] enhancement on isuue #196 extend this class with specific whitespace --- .../text/IsEqualCompressingWhiteSpace.java | 64 +++++++++++++++++-- 1 file changed, 59 insertions(+), 5 deletions(-) diff --git a/hamcrest/src/main/java/org/hamcrest/text/IsEqualCompressingWhiteSpace.java b/hamcrest/src/main/java/org/hamcrest/text/IsEqualCompressingWhiteSpace.java index 15d7a877..3c77fd70 100644 --- a/hamcrest/src/main/java/org/hamcrest/text/IsEqualCompressingWhiteSpace.java +++ b/hamcrest/src/main/java/org/hamcrest/text/IsEqualCompressingWhiteSpace.java @@ -7,18 +7,31 @@ /** * Tests if a string is equal to another string, compressing any changes in whitespace. */ + public class IsEqualCompressingWhiteSpace extends TypeSafeMatcher { // TODO: Replace String with CharSequence to allow for easy interoperability between // String, StringBuffer, StringBuilder, CharBuffer, etc (joe). + enum whiteSpaceType + { + SPACE, // \s + TAB,// \t + LINEFEED,// \n + FORMFEED, // \f + CARRIAGERETURN, // \r + MIX; + } + private final String string; + private final whiteSpaceType type; - public IsEqualCompressingWhiteSpace(String string) { + public IsEqualCompressingWhiteSpace(String string, whiteSpaceType type) { if (string == null) { throw new IllegalArgumentException("Non-null value required"); } this.string = string; + this.type = type; } @Override @@ -39,7 +52,24 @@ public void describeTo(Description description) { } public String stripSpaces(String toBeStripped) { - return toBeStripped.replaceAll("[\\p{Z}\\p{C}]+", " ").trim(); + if (this.type == whiteSpaceType.TAB){ + return toBeStripped.replaceAll("[\\p{Z}\\t]+", " ").trim(); + } + else if (this.type == whiteSpaceType.LINEFEED){ + return toBeStripped.replaceAll("[\\p{Z}\\n]+", " ").trim(); + } + else if (this.type == whiteSpaceType.FORMFEED){ + return toBeStripped.replaceAll("[\\p{Z}\\f]+", " ").trim(); + } + else if (this.type == whiteSpaceType.CARRIAGERETURN){ + return toBeStripped.replaceAll("[\\p{Z}\\r]+", " ").trim(); + } + else if (this.type == whiteSpaceType.SPACE){ + return toBeStripped.replaceAll("[\\p{Z}]+", " ").trim(); + } + else{ + return toBeStripped.replaceAll("[\\p{Z}\\t\\n\\f\\r]+", " ").trim(); + } } /** @@ -47,8 +77,8 @@ public String stripSpaces(String toBeStripped) { * @param expectedString * the expected value of matched strings */ - public static Matcher equalToIgnoringWhiteSpace(String expectedString) { - return new IsEqualCompressingWhiteSpace(expectedString); + public static Matcher equalToIgnoringWhiteSpace(String expectedString, whiteSpaceType type) { + return new IsEqualCompressingWhiteSpace(expectedString, type); } /** @@ -66,7 +96,31 @@ public static Matcher equalToIgnoringWhiteSpace(String expectedString) { * the expected value of matched strings */ public static Matcher equalToCompressingWhiteSpace(String expectedString) { - return new IsEqualCompressingWhiteSpace(expectedString); + return new IsEqualCompressingWhiteSpace(expectedString, whiteSpaceType.MIX); + } + + public static Matcher equalToCompressingSPACE(String expectedString) { + return new IsEqualCompressingWhiteSpace(expectedString, whiteSpaceType.SPACE); + } + + public static Matcher equalToCompressingTAB(String expectedString) { + return new IsEqualCompressingWhiteSpace(expectedString, whiteSpaceType.TAB); + } + + public static Matcher equalToCompressingLINEFEED(String expectedString) { + return new IsEqualCompressingWhiteSpace(expectedString, whiteSpaceType.LINEFEED); + } + + public static Matcher equalToCompressingFORMFEED(String expectedString) { + return new IsEqualCompressingWhiteSpace(expectedString, whiteSpaceType.FORMFEED); + } + + public static Matcher equalToCompressingCARRIAGERETURN(String expectedString) { + return new IsEqualCompressingWhiteSpace(expectedString, whiteSpaceType.CARRIAGERETURN); + } + + public static Matcher equalToCompressingMIX(String expectedString) { + return new IsEqualCompressingWhiteSpace(expectedString, whiteSpaceType.MIX); } } From 683a956cde09ebeecd04c1f0233651044637bb72 Mon Sep 17 00:00:00 2001 From: = <=> Date: Mon, 26 Apr 2021 13:59:40 +0800 Subject: [PATCH 2/3] enhancement on issue #196 --- .../text/IsEqualCompressingWhiteSpace.java | 65 +++++++++++++------ .../IsEqualCompressingWhiteSpaceTest.java | 30 ++++++++- 2 files changed, 73 insertions(+), 22 deletions(-) diff --git a/hamcrest/src/main/java/org/hamcrest/text/IsEqualCompressingWhiteSpace.java b/hamcrest/src/main/java/org/hamcrest/text/IsEqualCompressingWhiteSpace.java index 3c77fd70..b445a96f 100644 --- a/hamcrest/src/main/java/org/hamcrest/text/IsEqualCompressingWhiteSpace.java +++ b/hamcrest/src/main/java/org/hamcrest/text/IsEqualCompressingWhiteSpace.java @@ -13,20 +13,27 @@ public class IsEqualCompressingWhiteSpace extends TypeSafeMatcher { // TODO: Replace String with CharSequence to allow for easy interoperability between // String, StringBuffer, StringBuilder, CharBuffer, etc (joe). + /** + * enum the whitespace type where SPACE is '\s', TAB is '\t', LINE_FEED is '\n', + * FORM_FEED is '\f', CARRIAGE_RETURN is '\r', MIX is combined type above. + * @author Koko + * @version 0.0.1 + * @date 2021/04/26 + */ enum whiteSpaceType { - SPACE, // \s - TAB,// \t - LINEFEED,// \n - FORMFEED, // \f - CARRIAGERETURN, // \r - MIX; + SPACE, + TAB, + LINE_FEED, + FORM_FEED, + CARRIAGE_RETURN, + MIX } private final String string; private final whiteSpaceType type; - public IsEqualCompressingWhiteSpace(String string, whiteSpaceType type) { + private IsEqualCompressingWhiteSpace(String string, whiteSpaceType type) { if (string == null) { throw new IllegalArgumentException("Non-null value required"); } @@ -51,17 +58,27 @@ public void describeTo(Description description) { .appendText(" compressing white space"); } - public String stripSpaces(String toBeStripped) { + /** + * strip space on the head and tail of the string; + * besides that replace all whitespace specified by this.type with " " + * strip redundant space between words + * @author Koko + * @version 0.0.1 + * @date 2021/04/26 + * @Param string to be stripped + * @return: string after stripped + */ + private String stripSpaces(String toBeStripped) { if (this.type == whiteSpaceType.TAB){ return toBeStripped.replaceAll("[\\p{Z}\\t]+", " ").trim(); } - else if (this.type == whiteSpaceType.LINEFEED){ + else if (this.type == whiteSpaceType.LINE_FEED){ return toBeStripped.replaceAll("[\\p{Z}\\n]+", " ").trim(); } - else if (this.type == whiteSpaceType.FORMFEED){ + else if (this.type == whiteSpaceType.FORM_FEED){ return toBeStripped.replaceAll("[\\p{Z}\\f]+", " ").trim(); } - else if (this.type == whiteSpaceType.CARRIAGERETURN){ + else if (this.type == whiteSpaceType.CARRIAGE_RETURN){ return toBeStripped.replaceAll("[\\p{Z}\\r]+", " ").trim(); } else if (this.type == whiteSpaceType.SPACE){ @@ -99,27 +116,35 @@ public static Matcher equalToCompressingWhiteSpace(String expectedString return new IsEqualCompressingWhiteSpace(expectedString, whiteSpaceType.MIX); } - public static Matcher equalToCompressingSPACE(String expectedString) { + /** + * different types of generate string matcher according to whitespace type + * @author Koko + * @version 0.0.1 + * @date 2021/04/26 + * @Param string to generate matcher + * @return: string matcher + */ + public static Matcher equalToCompressingSpace(String expectedString) { return new IsEqualCompressingWhiteSpace(expectedString, whiteSpaceType.SPACE); } - public static Matcher equalToCompressingTAB(String expectedString) { + public static Matcher equalToCompressingTab(String expectedString) { return new IsEqualCompressingWhiteSpace(expectedString, whiteSpaceType.TAB); } - public static Matcher equalToCompressingLINEFEED(String expectedString) { - return new IsEqualCompressingWhiteSpace(expectedString, whiteSpaceType.LINEFEED); + public static Matcher equalToCompressingLineFeed(String expectedString) { + return new IsEqualCompressingWhiteSpace(expectedString, whiteSpaceType.LINE_FEED); } - public static Matcher equalToCompressingFORMFEED(String expectedString) { - return new IsEqualCompressingWhiteSpace(expectedString, whiteSpaceType.FORMFEED); + public static Matcher equalToCompressingFormFeed(String expectedString) { + return new IsEqualCompressingWhiteSpace(expectedString, whiteSpaceType.FORM_FEED); } - public static Matcher equalToCompressingCARRIAGERETURN(String expectedString) { - return new IsEqualCompressingWhiteSpace(expectedString, whiteSpaceType.CARRIAGERETURN); + public static Matcher equalToCompressingCarriageReturn(String expectedString) { + return new IsEqualCompressingWhiteSpace(expectedString, whiteSpaceType.CARRIAGE_RETURN); } - public static Matcher equalToCompressingMIX(String expectedString) { + public static Matcher equalToCompressingMix(String expectedString) { return new IsEqualCompressingWhiteSpace(expectedString, whiteSpaceType.MIX); } diff --git a/hamcrest/src/test/java/org/hamcrest/text/IsEqualCompressingWhiteSpaceTest.java b/hamcrest/src/test/java/org/hamcrest/text/IsEqualCompressingWhiteSpaceTest.java index e85e54f6..ac3a5c7e 100644 --- a/hamcrest/src/test/java/org/hamcrest/text/IsEqualCompressingWhiteSpaceTest.java +++ b/hamcrest/src/test/java/org/hamcrest/text/IsEqualCompressingWhiteSpaceTest.java @@ -3,11 +3,16 @@ import org.hamcrest.AbstractMatcherTest; import org.hamcrest.Matcher; -import static org.hamcrest.text.IsEqualCompressingWhiteSpace.equalToCompressingWhiteSpace; +import static org.hamcrest.text.IsEqualCompressingWhiteSpace.*; public class IsEqualCompressingWhiteSpaceTest extends AbstractMatcherTest { - private final Matcher matcher = equalToCompressingWhiteSpace(" Hello World how\n are we? "); + private final Matcher matcher = equalToCompressingMix(" Hello World how\n are we? "); + private final Matcher SpaceMatcher = equalToCompressingSpace(" Hello World how are we? "); + private final Matcher TabMatcher = equalToCompressingTab(" Hello World how\t are\t\t we? "); + private final Matcher LineFeedMatcher = equalToCompressingLineFeed(" Hello World how\n are we? "); + private final Matcher FormFeedMatcher = equalToCompressingFormFeed(" Hello World how\f are\f\f we? "); + private final Matcher CarriageReturnMatcher = equalToCompressingCarriageReturn(" Hello \r World how \r\r are we? "); @Override protected Matcher createMatcher() { @@ -45,4 +50,25 @@ public void testHasAReadableDescription() { public void testPassesIfWhitespacesContainsNoBreakSpace() { assertMatches(matcher, "Hello" + ((char)160) + "World how are we?"); } + + public void testFailsIfwordsAreSameButWhiteSpaceDiffers() + { + assertDoesNotMatch(SpaceMatcher, " Hello World how\n are we? "); + assertDoesNotMatch(TabMatcher, " Hello World how\n are we? "); + assertDoesNotMatch(LineFeedMatcher, " Hello World how\r are we? "); + } + + public void testPassesIfwordsAreSameButMixWhiteSpace() + { + assertMatches(matcher, "Hello\f\f World\t how are\r we?\n\n"); + } + + public void testUnitWhiteSpace() + { + assertMatches(SpaceMatcher, " Hello World how are we? "); + assertMatches(TabMatcher, " Hello World how \t are we? \t"); + assertMatches(LineFeedMatcher, " Hello World how\n are \n we? "); + assertMatches(FormFeedMatcher, " Hello World\f how are we? "); + assertMatches(CarriageReturnMatcher, "Hello World how are we?"); + } } From 7d7b85d44fe21762e59345e091cb8a50e9c5801e Mon Sep 17 00:00:00 2001 From: = <=> Date: Mon, 26 Apr 2021 14:15:35 +0800 Subject: [PATCH 3/3] modify javadoc comments --- .../text/IsEqualCompressingWhiteSpace.java | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/hamcrest/src/main/java/org/hamcrest/text/IsEqualCompressingWhiteSpace.java b/hamcrest/src/main/java/org/hamcrest/text/IsEqualCompressingWhiteSpace.java index b445a96f..2c982da6 100644 --- a/hamcrest/src/main/java/org/hamcrest/text/IsEqualCompressingWhiteSpace.java +++ b/hamcrest/src/main/java/org/hamcrest/text/IsEqualCompressingWhiteSpace.java @@ -17,8 +17,6 @@ public class IsEqualCompressingWhiteSpace extends TypeSafeMatcher { * enum the whitespace type where SPACE is '\s', TAB is '\t', LINE_FEED is '\n', * FORM_FEED is '\f', CARRIAGE_RETURN is '\r', MIX is combined type above. * @author Koko - * @version 0.0.1 - * @date 2021/04/26 */ enum whiteSpaceType { @@ -63,10 +61,8 @@ public void describeTo(Description description) { * besides that replace all whitespace specified by this.type with " " * strip redundant space between words * @author Koko - * @version 0.0.1 - * @date 2021/04/26 - * @Param string to be stripped - * @return: string after stripped + * @param toBeStripped + * string to be stripped */ private String stripSpaces(String toBeStripped) { if (this.type == whiteSpaceType.TAB){ @@ -119,10 +115,8 @@ public static Matcher equalToCompressingWhiteSpace(String expectedString /** * different types of generate string matcher according to whitespace type * @author Koko - * @version 0.0.1 - * @date 2021/04/26 - * @Param string to generate matcher - * @return: string matcher + * @param expectedString + * string used to generate matcher */ public static Matcher equalToCompressingSpace(String expectedString) { return new IsEqualCompressingWhiteSpace(expectedString, whiteSpaceType.SPACE);