Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enhancement on issue #196 #341

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,36 @@
/**
* Tests if a string is equal to another string, compressing any changes in whitespace.
*/

public class IsEqualCompressingWhiteSpace extends TypeSafeMatcher<String> {

// 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
Expand All @@ -38,17 +56,42 @@ 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();
}
}

/**
* @deprecated {@link #equalToCompressingWhiteSpace(String)}
* @param expectedString
* the expected value of matched strings
*/
public static Matcher<String> equalToIgnoringWhiteSpace(String expectedString) {
return new IsEqualCompressingWhiteSpace(expectedString);
public static Matcher<String> equalToIgnoringWhiteSpace(String expectedString, whiteSpaceType type) {
return new IsEqualCompressingWhiteSpace(expectedString, type);
}

/**
Expand All @@ -66,7 +109,37 @@ public static Matcher<String> equalToIgnoringWhiteSpace(String expectedString) {
* the expected value of matched strings
*/
public static Matcher<String> 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<String> equalToCompressingSpace(String expectedString) {
return new IsEqualCompressingWhiteSpace(expectedString, whiteSpaceType.SPACE);
}

public static Matcher<String> equalToCompressingTab(String expectedString) {
return new IsEqualCompressingWhiteSpace(expectedString, whiteSpaceType.TAB);
}

public static Matcher<String> equalToCompressingLineFeed(String expectedString) {
return new IsEqualCompressingWhiteSpace(expectedString, whiteSpaceType.LINE_FEED);
}

public static Matcher<String> equalToCompressingFormFeed(String expectedString) {
return new IsEqualCompressingWhiteSpace(expectedString, whiteSpaceType.FORM_FEED);
}

public static Matcher<String> equalToCompressingCarriageReturn(String expectedString) {
return new IsEqualCompressingWhiteSpace(expectedString, whiteSpaceType.CARRIAGE_RETURN);
}

public static Matcher<String> equalToCompressingMix(String expectedString) {
return new IsEqualCompressingWhiteSpace(expectedString, whiteSpaceType.MIX);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> matcher = equalToCompressingWhiteSpace(" Hello World how\n are we? ");
private final Matcher<String> matcher = equalToCompressingMix(" Hello World how\n are we? ");
private final Matcher<String> SpaceMatcher = equalToCompressingSpace(" Hello World how are we? ");
private final Matcher<String> TabMatcher = equalToCompressingTab(" Hello World how\t are\t\t we? ");
private final Matcher<String> LineFeedMatcher = equalToCompressingLineFeed(" Hello World how\n are we? ");
private final Matcher<String> FormFeedMatcher = equalToCompressingFormFeed(" Hello World how\f are\f\f we? ");
private final Matcher<String> CarriageReturnMatcher = equalToCompressingCarriageReturn(" Hello \r World how \r\r are we? ");

@Override
protected Matcher<?> createMatcher() {
Expand Down Expand Up @@ -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?");
}
}