Skip to content

Commit 683a956

Browse files
author
=
committed
enhancement on issue #196
1 parent 9495214 commit 683a956

2 files changed

Lines changed: 73 additions & 22 deletions

File tree

hamcrest/src/main/java/org/hamcrest/text/IsEqualCompressingWhiteSpace.java

Lines changed: 45 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,27 @@ public class IsEqualCompressingWhiteSpace extends TypeSafeMatcher<String> {
1313
// TODO: Replace String with CharSequence to allow for easy interoperability between
1414
// String, StringBuffer, StringBuilder, CharBuffer, etc (joe).
1515

16+
/**
17+
* enum the whitespace type where SPACE is '\s', TAB is '\t', LINE_FEED is '\n',
18+
* FORM_FEED is '\f', CARRIAGE_RETURN is '\r', MIX is combined type above.
19+
* @author Koko
20+
* @version 0.0.1
21+
* @date 2021/04/26
22+
*/
1623
enum whiteSpaceType
1724
{
18-
SPACE, // \s
19-
TAB,// \t
20-
LINEFEED,// \n
21-
FORMFEED, // \f
22-
CARRIAGERETURN, // \r
23-
MIX;
25+
SPACE,
26+
TAB,
27+
LINE_FEED,
28+
FORM_FEED,
29+
CARRIAGE_RETURN,
30+
MIX
2431
}
2532

2633
private final String string;
2734
private final whiteSpaceType type;
2835

29-
public IsEqualCompressingWhiteSpace(String string, whiteSpaceType type) {
36+
private IsEqualCompressingWhiteSpace(String string, whiteSpaceType type) {
3037
if (string == null) {
3138
throw new IllegalArgumentException("Non-null value required");
3239
}
@@ -51,17 +58,27 @@ public void describeTo(Description description) {
5158
.appendText(" compressing white space");
5259
}
5360

54-
public String stripSpaces(String toBeStripped) {
61+
/**
62+
* strip space on the head and tail of the string;
63+
* besides that replace all whitespace specified by this.type with " "
64+
* strip redundant space between words
65+
* @author Koko
66+
* @version 0.0.1
67+
* @date 2021/04/26
68+
* @Param string to be stripped
69+
* @return: string after stripped
70+
*/
71+
private String stripSpaces(String toBeStripped) {
5572
if (this.type == whiteSpaceType.TAB){
5673
return toBeStripped.replaceAll("[\\p{Z}\\t]+", " ").trim();
5774
}
58-
else if (this.type == whiteSpaceType.LINEFEED){
75+
else if (this.type == whiteSpaceType.LINE_FEED){
5976
return toBeStripped.replaceAll("[\\p{Z}\\n]+", " ").trim();
6077
}
61-
else if (this.type == whiteSpaceType.FORMFEED){
78+
else if (this.type == whiteSpaceType.FORM_FEED){
6279
return toBeStripped.replaceAll("[\\p{Z}\\f]+", " ").trim();
6380
}
64-
else if (this.type == whiteSpaceType.CARRIAGERETURN){
81+
else if (this.type == whiteSpaceType.CARRIAGE_RETURN){
6582
return toBeStripped.replaceAll("[\\p{Z}\\r]+", " ").trim();
6683
}
6784
else if (this.type == whiteSpaceType.SPACE){
@@ -99,27 +116,35 @@ public static Matcher<String> equalToCompressingWhiteSpace(String expectedString
99116
return new IsEqualCompressingWhiteSpace(expectedString, whiteSpaceType.MIX);
100117
}
101118

102-
public static Matcher<String> equalToCompressingSPACE(String expectedString) {
119+
/**
120+
* different types of generate string matcher according to whitespace type
121+
* @author Koko
122+
* @version 0.0.1
123+
* @date 2021/04/26
124+
* @Param string to generate matcher
125+
* @return: string matcher
126+
*/
127+
public static Matcher<String> equalToCompressingSpace(String expectedString) {
103128
return new IsEqualCompressingWhiteSpace(expectedString, whiteSpaceType.SPACE);
104129
}
105130

106-
public static Matcher<String> equalToCompressingTAB(String expectedString) {
131+
public static Matcher<String> equalToCompressingTab(String expectedString) {
107132
return new IsEqualCompressingWhiteSpace(expectedString, whiteSpaceType.TAB);
108133
}
109134

110-
public static Matcher<String> equalToCompressingLINEFEED(String expectedString) {
111-
return new IsEqualCompressingWhiteSpace(expectedString, whiteSpaceType.LINEFEED);
135+
public static Matcher<String> equalToCompressingLineFeed(String expectedString) {
136+
return new IsEqualCompressingWhiteSpace(expectedString, whiteSpaceType.LINE_FEED);
112137
}
113138

114-
public static Matcher<String> equalToCompressingFORMFEED(String expectedString) {
115-
return new IsEqualCompressingWhiteSpace(expectedString, whiteSpaceType.FORMFEED);
139+
public static Matcher<String> equalToCompressingFormFeed(String expectedString) {
140+
return new IsEqualCompressingWhiteSpace(expectedString, whiteSpaceType.FORM_FEED);
116141
}
117142

118-
public static Matcher<String> equalToCompressingCARRIAGERETURN(String expectedString) {
119-
return new IsEqualCompressingWhiteSpace(expectedString, whiteSpaceType.CARRIAGERETURN);
143+
public static Matcher<String> equalToCompressingCarriageReturn(String expectedString) {
144+
return new IsEqualCompressingWhiteSpace(expectedString, whiteSpaceType.CARRIAGE_RETURN);
120145
}
121146

122-
public static Matcher<String> equalToCompressingMIX(String expectedString) {
147+
public static Matcher<String> equalToCompressingMix(String expectedString) {
123148
return new IsEqualCompressingWhiteSpace(expectedString, whiteSpaceType.MIX);
124149
}
125150

hamcrest/src/test/java/org/hamcrest/text/IsEqualCompressingWhiteSpaceTest.java

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,16 @@
33
import org.hamcrest.AbstractMatcherTest;
44
import org.hamcrest.Matcher;
55

6-
import static org.hamcrest.text.IsEqualCompressingWhiteSpace.equalToCompressingWhiteSpace;
6+
import static org.hamcrest.text.IsEqualCompressingWhiteSpace.*;
77

88
public class IsEqualCompressingWhiteSpaceTest extends AbstractMatcherTest {
99

10-
private final Matcher<String> matcher = equalToCompressingWhiteSpace(" Hello World how\n are we? ");
10+
private final Matcher<String> matcher = equalToCompressingMix(" Hello World how\n are we? ");
11+
private final Matcher<String> SpaceMatcher = equalToCompressingSpace(" Hello World how are we? ");
12+
private final Matcher<String> TabMatcher = equalToCompressingTab(" Hello World how\t are\t\t we? ");
13+
private final Matcher<String> LineFeedMatcher = equalToCompressingLineFeed(" Hello World how\n are we? ");
14+
private final Matcher<String> FormFeedMatcher = equalToCompressingFormFeed(" Hello World how\f are\f\f we? ");
15+
private final Matcher<String> CarriageReturnMatcher = equalToCompressingCarriageReturn(" Hello \r World how \r\r are we? ");
1116

1217
@Override
1318
protected Matcher<?> createMatcher() {
@@ -45,4 +50,25 @@ public void testHasAReadableDescription() {
4550
public void testPassesIfWhitespacesContainsNoBreakSpace() {
4651
assertMatches(matcher, "Hello" + ((char)160) + "World how are we?");
4752
}
53+
54+
public void testFailsIfwordsAreSameButWhiteSpaceDiffers()
55+
{
56+
assertDoesNotMatch(SpaceMatcher, " Hello World how\n are we? ");
57+
assertDoesNotMatch(TabMatcher, " Hello World how\n are we? ");
58+
assertDoesNotMatch(LineFeedMatcher, " Hello World how\r are we? ");
59+
}
60+
61+
public void testPassesIfwordsAreSameButMixWhiteSpace()
62+
{
63+
assertMatches(matcher, "Hello\f\f World\t how are\r we?\n\n");
64+
}
65+
66+
public void testUnitWhiteSpace()
67+
{
68+
assertMatches(SpaceMatcher, " Hello World how are we? ");
69+
assertMatches(TabMatcher, " Hello World how \t are we? \t");
70+
assertMatches(LineFeedMatcher, " Hello World how\n are \n we? ");
71+
assertMatches(FormFeedMatcher, " Hello World\f how are we? ");
72+
assertMatches(CarriageReturnMatcher, "Hello World how are we?");
73+
}
4874
}

0 commit comments

Comments
 (0)