diff --git a/hamcrest/src/main/java/org/hamcrest/text/IsEqualCompressingWhiteSpace.java b/hamcrest/src/main/java/org/hamcrest/text/IsEqualCompressingWhiteSpace.java index 15d7a877..2c982da6 100644 --- a/hamcrest/src/main/java/org/hamcrest/text/IsEqualCompressingWhiteSpace.java +++ b/hamcrest/src/main/java/org/hamcrest/text/IsEqualCompressingWhiteSpace.java @@ -7,18 +7,36 @@ /** * 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 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 + */ + enum whiteSpaceType + { + SPACE, + TAB, + LINE_FEED, + FORM_FEED, + CARRIAGE_RETURN, + MIX + } + private final String string; + private final whiteSpaceType type; - public IsEqualCompressingWhiteSpace(String string) { + private IsEqualCompressingWhiteSpace(String string, whiteSpaceType type) { if (string == null) { throw new IllegalArgumentException("Non-null value required"); } this.string = string; + this.type = type; } @Override @@ -38,8 +56,33 @@ public void describeTo(Description description) { .appendText(" compressing white space"); } - public String stripSpaces(String toBeStripped) { - return toBeStripped.replaceAll("[\\p{Z}\\p{C}]+", " ").trim(); + /** + * 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 + * @param toBeStripped + * string to be stripped + */ + private String stripSpaces(String toBeStripped) { + if (this.type == whiteSpaceType.TAB){ + return toBeStripped.replaceAll("[\\p{Z}\\t]+", " ").trim(); + } + else if (this.type == whiteSpaceType.LINE_FEED){ + return toBeStripped.replaceAll("[\\p{Z}\\n]+", " ").trim(); + } + else if (this.type == whiteSpaceType.FORM_FEED){ + return toBeStripped.replaceAll("[\\p{Z}\\f]+", " ").trim(); + } + else if (this.type == whiteSpaceType.CARRIAGE_RETURN){ + 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 +90,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 +109,37 @@ 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); + } + + /** + * different types of generate string matcher according to whitespace type + * @author Koko + * @param expectedString + * string used to generate matcher + */ + 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.LINE_FEED); + } + + public static Matcher equalToCompressingFormFeed(String expectedString) { + return new IsEqualCompressingWhiteSpace(expectedString, whiteSpaceType.FORM_FEED); + } + + public static Matcher equalToCompressingCarriageReturn(String expectedString) { + return new IsEqualCompressingWhiteSpace(expectedString, whiteSpaceType.CARRIAGE_RETURN); + } + + 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?"); + } }