Skip to content

Commit ff1ab0b

Browse files
committed
Renamed IgnoringWhiteSpace to CompressingWhiteSpace
1 parent e94100b commit ff1ab0b

File tree

4 files changed

+94
-82
lines changed

4 files changed

+94
-82
lines changed

hamcrest-library/src/main/java/org/hamcrest/Matchers.java

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import org.hamcrest.collection.ArrayMatching;
44
import org.hamcrest.core.IsIterableContaining;
55
import org.hamcrest.core.StringRegularExpression;
6+
import org.hamcrest.text.IsEqualCompressingWhiteSpace;
67

78
import java.util.regex.Pattern;
89

@@ -467,7 +468,7 @@ public static <T> org.hamcrest.Matcher<T> theInstance(T target) {
467468
* @param substring
468469
* the substring that the returned matcher will expect to find within any examined string
469470
*/
470-
public static org.hamcrest.Matcher<java.lang.String> containsString(java.lang.String substring) {
471+
public static Matcher<java.lang.String> containsString(java.lang.String substring) {
471472
return org.hamcrest.core.StringContains.containsString(substring);
472473
}
473474

@@ -480,7 +481,7 @@ public static org.hamcrest.Matcher<java.lang.String> containsString(java.lang.St
480481
* @param substring
481482
* the substring that the returned matcher will expect to find within any examined string
482483
*/
483-
public static org.hamcrest.Matcher<java.lang.String> containsStringIgnoringCase(java.lang.String substring) {
484+
public static Matcher<java.lang.String> containsStringIgnoringCase(java.lang.String substring) {
484485
return org.hamcrest.core.StringContains.containsStringIgnoringCase(substring);
485486
}
486487

@@ -495,7 +496,7 @@ public static org.hamcrest.Matcher<java.lang.String> containsStringIgnoringCase(
495496
* @param prefix
496497
* the substring that the returned matcher will expect at the start of any examined string
497498
*/
498-
public static org.hamcrest.Matcher<java.lang.String> startsWith(java.lang.String prefix) {
499+
public static Matcher<java.lang.String> startsWith(java.lang.String prefix) {
499500
return org.hamcrest.core.StringStartsWith.startsWith(prefix);
500501
}
501502

@@ -510,7 +511,7 @@ public static org.hamcrest.Matcher<java.lang.String> startsWith(java.lang.String
510511
* @param prefix
511512
* the substring that the returned matcher will expect at the start of any examined string
512513
*/
513-
public static org.hamcrest.Matcher<java.lang.String> startsWithIgnoringCase(java.lang.String prefix) {
514+
public static Matcher<java.lang.String> startsWithIgnoringCase(java.lang.String prefix) {
514515
return org.hamcrest.core.StringStartsWith.startsWithIgnoringCase(prefix);
515516
}
516517

@@ -523,7 +524,7 @@ public static org.hamcrest.Matcher<java.lang.String> startsWithIgnoringCase(java
523524
* @param suffix
524525
* the substring that the returned matcher will expect at the end of any examined string
525526
*/
526-
public static org.hamcrest.Matcher<java.lang.String> endsWith(java.lang.String suffix) {
527+
public static Matcher<java.lang.String> endsWith(java.lang.String suffix) {
527528
return org.hamcrest.core.StringEndsWith.endsWith(suffix);
528529
}
529530

@@ -536,7 +537,7 @@ public static org.hamcrest.Matcher<java.lang.String> endsWith(java.lang.String s
536537
* @param suffix
537538
* the substring that the returned matcher will expect at the end of any examined string
538539
*/
539-
public static org.hamcrest.Matcher<java.lang.String> endsWithIgnoringCase(java.lang.String suffix) {
540+
public static Matcher<java.lang.String> endsWithIgnoringCase(java.lang.String suffix) {
540541
return org.hamcrest.core.StringEndsWith.endsWithIgnoringCase(suffix);
541542
}
542543

@@ -1367,10 +1368,19 @@ public static <T extends java.lang.Comparable<T>> org.hamcrest.Matcher<T> lessTh
13671368
* @param expectedString
13681369
* the expected value of matched strings
13691370
*/
1370-
public static org.hamcrest.Matcher<java.lang.String> equalToIgnoringCase(java.lang.String expectedString) {
1371+
public static Matcher<java.lang.String> equalToIgnoringCase(java.lang.String expectedString) {
13711372
return org.hamcrest.text.IsEqualIgnoringCase.equalToIgnoringCase(expectedString);
13721373
}
13731374

1375+
/**
1376+
* @deprecated {@link #equalToCompressingWhiteSpace(String)}
1377+
* @param expectedString
1378+
* the expected value of matched strings
1379+
*/
1380+
public static Matcher<java.lang.String> equalToIgnoringWhiteSpace(java.lang.String expectedString) {
1381+
return equalToCompressingWhiteSpace(expectedString);
1382+
}
1383+
13741384
/**
13751385
* Creates a matcher of {@link String} that matches when the examined string is equal to
13761386
* the specified expectedString, when whitespace differences are (mostly) ignored. To be
@@ -1381,12 +1391,12 @@ public static org.hamcrest.Matcher<java.lang.String> equalToIgnoringCase(java.la
13811391
* </ul>
13821392
* For example:
13831393
* <pre>assertThat(" my\tfoo bar ", equalToIgnoringWhiteSpace(" my foo bar"))</pre>
1384-
*
1394+
*
13851395
* @param expectedString
13861396
* the expected value of matched strings
13871397
*/
1388-
public static org.hamcrest.Matcher<java.lang.String> equalToIgnoringWhiteSpace(java.lang.String expectedString) {
1389-
return org.hamcrest.text.IsEqualIgnoringWhiteSpace.equalToIgnoringWhiteSpace(expectedString);
1398+
public static Matcher<java.lang.String> equalToCompressingWhiteSpace(java.lang.String expectedString) {
1399+
return IsEqualCompressingWhiteSpace.equalToCompressingWhiteSpace(expectedString);
13901400
}
13911401

13921402
/**
@@ -1395,7 +1405,7 @@ public static org.hamcrest.Matcher<java.lang.String> equalToIgnoringWhiteSpace(j
13951405
* For example:
13961406
* <pre>assertThat(((String)null), is(emptyOrNullString()))</pre>
13971407
*/
1398-
public static org.hamcrest.Matcher<java.lang.String> emptyOrNullString() {
1408+
public static Matcher<java.lang.String> emptyOrNullString() {
13991409
return org.hamcrest.text.IsEmptyString.emptyOrNullString();
14001410
}
14011411

@@ -1404,7 +1414,7 @@ public static org.hamcrest.Matcher<java.lang.String> emptyOrNullString() {
14041414
* For example:
14051415
* <pre>assertThat("", is(emptyString()))</pre>
14061416
*/
1407-
public static org.hamcrest.Matcher<java.lang.String> emptyString() {
1417+
public static Matcher<java.lang.String> emptyString() {
14081418
return org.hamcrest.text.IsEmptyString.emptyString();
14091419
}
14101420

@@ -1417,7 +1427,7 @@ public static org.hamcrest.Matcher<java.lang.String> emptyString() {
14171427
* @deprecated use is(emptyOrNullString()) instead
14181428
*/
14191429
@SuppressWarnings("deprecation")
1420-
public static org.hamcrest.Matcher<java.lang.String> isEmptyOrNullString() {
1430+
public static Matcher<java.lang.String> isEmptyOrNullString() {
14211431
return org.hamcrest.text.IsEmptyString.isEmptyOrNullString();
14221432
}
14231433

@@ -1429,7 +1439,7 @@ public static org.hamcrest.Matcher<java.lang.String> isEmptyOrNullString() {
14291439
* @deprecated use is(emptyString()) instead
14301440
*/
14311441
@SuppressWarnings("deprecation")
1432-
public static org.hamcrest.Matcher<java.lang.String> isEmptyString() {
1442+
public static Matcher<java.lang.String> isEmptyString() {
14331443
return org.hamcrest.text.IsEmptyString.isEmptyString();
14341444
}
14351445

@@ -1439,7 +1449,7 @@ public static org.hamcrest.Matcher<java.lang.String> isEmptyString() {
14391449
* For example:
14401450
* <pre>assertThat(((String)null), is(blankOrNullString()))</pre>
14411451
*/
1442-
public static org.hamcrest.Matcher<java.lang.String> blankOrNullString() {
1452+
public static Matcher<java.lang.String> blankOrNullString() {
14431453
return org.hamcrest.text.IsBlankString.blankOrNullString();
14441454
}
14451455

@@ -1449,23 +1459,23 @@ public static org.hamcrest.Matcher<java.lang.String> blankOrNullString() {
14491459
* For example:
14501460
* <pre>assertThat(" ", is(blankString()))</pre>
14511461
*/
1452-
public static org.hamcrest.Matcher<java.lang.String> blankString() {
1462+
public static Matcher<java.lang.String> blankString() {
14531463
return org.hamcrest.text.IsBlankString.blankString();
14541464
}
14551465

14561466
/**
14571467
* Creates a matcher of {@link java.lang.String} that matches when the examined string
14581468
* exactly matches the given {@link java.util.regex.Pattern}.
14591469
*/
1460-
public static org.hamcrest.Matcher<java.lang.String> matchesPattern(java.util.regex.Pattern pattern) {
1470+
public static Matcher<java.lang.String> matchesPattern(java.util.regex.Pattern pattern) {
14611471
return org.hamcrest.text.MatchesPattern.matchesPattern(pattern);
14621472
}
14631473

14641474
/**
14651475
* Creates a matcher of {@link java.lang.String} that matches when the examined string
14661476
* exactly matches the given regular expression, treated as a {@link java.util.regex.Pattern}.
14671477
*/
1468-
public static org.hamcrest.Matcher<java.lang.String> matchesPattern(java.lang.String regex) {
1478+
public static Matcher<java.lang.String> matchesPattern(java.lang.String regex) {
14691479
return org.hamcrest.text.MatchesPattern.matchesPattern(regex);
14701480
}
14711481

@@ -1479,7 +1489,7 @@ public static org.hamcrest.Matcher<java.lang.String> matchesPattern(java.lang.St
14791489
* @param substrings
14801490
* the substrings that must be contained within matching strings
14811491
*/
1482-
public static org.hamcrest.Matcher<java.lang.String> stringContainsInOrder(java.lang.Iterable<java.lang.String> substrings) {
1492+
public static Matcher<java.lang.String> stringContainsInOrder(java.lang.Iterable<java.lang.String> substrings) {
14831493
return org.hamcrest.text.StringContainsInOrder.stringContainsInOrder(substrings);
14841494
}
14851495

@@ -1493,7 +1503,7 @@ public static org.hamcrest.Matcher<java.lang.String> stringContainsInOrder(java.
14931503
* @param substrings
14941504
* the substrings that must be contained within matching strings
14951505
*/
1496-
public static org.hamcrest.Matcher<java.lang.String> stringContainsInOrder(java.lang.String... substrings) {
1506+
public static Matcher<java.lang.String> stringContainsInOrder(java.lang.String... substrings) {
14971507
return org.hamcrest.text.StringContainsInOrder.stringContainsInOrder(substrings);
14981508
}
14991509

@@ -1617,7 +1627,7 @@ public static <T> org.hamcrest.Matcher<T> samePropertyValuesAs(T expectedBean) {
16171627
* @param valueMatcher
16181628
* matcher for the value at the specified xpath
16191629
*/
1620-
public static org.hamcrest.Matcher<org.w3c.dom.Node> hasXPath(java.lang.String xPath, org.hamcrest.Matcher<java.lang.String> valueMatcher) {
1630+
public static org.hamcrest.Matcher<org.w3c.dom.Node> hasXPath(java.lang.String xPath, Matcher<java.lang.String> valueMatcher) {
16211631
return org.hamcrest.xml.HasXPath.hasXPath(xPath, valueMatcher);
16221632
}
16231633

@@ -1635,7 +1645,7 @@ public static org.hamcrest.Matcher<org.w3c.dom.Node> hasXPath(java.lang.String x
16351645
* @param valueMatcher
16361646
* matcher for the value at the specified xpath
16371647
*/
1638-
public static org.hamcrest.Matcher<org.w3c.dom.Node> hasXPath(java.lang.String xPath, javax.xml.namespace.NamespaceContext namespaceContext, org.hamcrest.Matcher<java.lang.String> valueMatcher) {
1648+
public static org.hamcrest.Matcher<org.w3c.dom.Node> hasXPath(java.lang.String xPath, javax.xml.namespace.NamespaceContext namespaceContext, Matcher<java.lang.String> valueMatcher) {
16391649
return org.hamcrest.xml.HasXPath.hasXPath(xPath, namespaceContext, valueMatcher);
16401650
}
16411651

hamcrest-library/src/main/java/org/hamcrest/text/IsEqualIgnoringWhiteSpace.java renamed to hamcrest-library/src/main/java/org/hamcrest/text/IsEqualCompressingWhiteSpace.java

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,16 @@
77
import static java.lang.Character.isWhitespace;
88

99
/**
10-
* Tests if a string is equal to another string, ignoring any changes in whitespace.
10+
* Tests if a string is equal to another string, compressing any changes in whitespace.
1111
*/
12-
public class IsEqualIgnoringWhiteSpace extends TypeSafeMatcher<String> {
12+
public class IsEqualCompressingWhiteSpace extends TypeSafeMatcher<String> {
1313

1414
// TODO: Replace String with CharSequence to allow for easy interoperability between
1515
// String, StringBuffer, StringBuilder, CharBuffer, etc (joe).
1616

1717
private final String string;
1818

19-
public IsEqualIgnoringWhiteSpace(String string) {
19+
public IsEqualCompressingWhiteSpace(String string) {
2020
if (string == null) {
2121
throw new IllegalArgumentException("Non-null value required");
2222
}
@@ -25,19 +25,19 @@ public IsEqualIgnoringWhiteSpace(String string) {
2525

2626
@Override
2727
public boolean matchesSafely(String item) {
28-
return stripSpace(string).equalsIgnoreCase(stripSpace(item));
28+
return stripSpace(string).equals(stripSpace(item));
2929
}
3030

3131
@Override
3232
public void describeMismatchSafely(String item, Description mismatchDescription) {
33-
mismatchDescription.appendText("was ").appendText(stripSpace(item));
33+
mismatchDescription.appendText("was ").appendValue(item);
3434
}
3535

3636
@Override
3737
public void describeTo(Description description) {
3838
description.appendText("a string equal to ")
3939
.appendValue(string)
40-
.appendText(" ignoring white space");
40+
.appendText(" compressing white space");
4141
}
4242

4343
public String stripSpace(String toBeStripped) {
@@ -58,6 +58,15 @@ public String stripSpace(String toBeStripped) {
5858
return result.toString().trim();
5959
}
6060

61+
/**
62+
* @deprecated {@link #equalToCompressingWhiteSpace(String)}
63+
* @param expectedString
64+
* the expected value of matched strings
65+
*/
66+
public static Matcher<String> equalToIgnoringWhiteSpace(String expectedString) {
67+
return new IsEqualCompressingWhiteSpace(expectedString);
68+
}
69+
6170
/**
6271
* Creates a matcher of {@link String} that matches when the examined string is equal to
6372
* the specified expectedString, when whitespace differences are (mostly) ignored. To be
@@ -68,12 +77,12 @@ public String stripSpace(String toBeStripped) {
6877
* </ul>
6978
* For example:
7079
* <pre>assertThat(" my\tfoo bar ", equalToIgnoringWhiteSpace(" my foo bar"))</pre>
71-
*
80+
*
7281
* @param expectedString
7382
* the expected value of matched strings
7483
*/
75-
public static Matcher<String> equalToIgnoringWhiteSpace(String expectedString) {
76-
return new IsEqualIgnoringWhiteSpace(expectedString);
84+
public static Matcher<String> equalToCompressingWhiteSpace(String expectedString) {
85+
return new IsEqualCompressingWhiteSpace(expectedString);
7786
}
7887

7988
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package org.hamcrest.text;
2+
3+
import org.hamcrest.AbstractMatcherTest;
4+
import org.hamcrest.Matcher;
5+
6+
import static org.hamcrest.text.IsEqualCompressingWhiteSpace.equalToCompressingWhiteSpace;
7+
8+
public class IsEqualCompressingWhiteSpaceTest extends AbstractMatcherTest {
9+
10+
private final Matcher<String> matcher = equalToCompressingWhiteSpace("Hello World how\n are we? ");
11+
12+
@Override
13+
protected Matcher<?> createMatcher() {
14+
return matcher;
15+
}
16+
17+
public void testPassesIfWordsAreSameButWhitespaceDiffers() {
18+
assertMatches(matcher, "Hello World how are we?");
19+
assertMatches(matcher, " Hello World how are \n\n\twe?");
20+
}
21+
22+
public void testFailsIfTextOtherThanWhitespaceDiffers() {
23+
assertDoesNotMatch(matcher, "Hello PLANET how are we?");
24+
assertDoesNotMatch(matcher, "Hello World how are we");
25+
}
26+
27+
public void testFailsIfWhitespaceIsAddedOrRemovedInMidWord() {
28+
assertDoesNotMatch(matcher, "HelloWorld how are we?");
29+
assertDoesNotMatch(matcher, "Hello Wo rld how are we?");
30+
}
31+
32+
public void test_has_a_readable_mismatch() {
33+
assertMismatchDescription("was \"Hello World how are we \"", matcher, "Hello World how are we ");
34+
}
35+
36+
public void testFailsIfMatchingAgainstNull() {
37+
assertDoesNotMatch(matcher, null);
38+
}
39+
40+
public void testHasAReadableDescription() {
41+
assertDescription("a string equal to \"Hello World how\\n are we? \" compressing white space",
42+
matcher);
43+
}
44+
}

hamcrest-library/src/test/java/org/hamcrest/text/IsEqualIgnoringWhiteSpaceTest.java

Lines changed: 0 additions & 51 deletions
This file was deleted.

0 commit comments

Comments
 (0)