Skip to content

Commit

Permalink
started introducing ignore case
Browse files Browse the repository at this point in the history
  • Loading branch information
sf105 committed May 28, 2014
1 parent 5466739 commit 639b99e
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@
* Tests if the argument is a string that contains a substring.
*/
public class StringContains extends SubstringMatcher {
public StringContains(String substring) {
super("containing", substring);
public StringContains(boolean ignoringCase, String substring) {
super("containing", ignoringCase, substring);
}

@Override
protected boolean evalSubstringOf(String s) {
return s.contains(substring);
return s.toLowerCase().contains(substring.toLowerCase());
}

/**
Expand All @@ -31,7 +31,11 @@ protected boolean evalSubstringOf(String s) {
*/
@Factory
public static Matcher<String> containsString(String substring) {
return new StringContains(substring);
return new StringContains(false, substring);
}
@Factory
public static Matcher<String> containsStringIgnoringCase(String substring) {
return new StringContains(true, substring);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* Tests if the argument is a string that contains a substring.
*/
public class StringEndsWith extends SubstringMatcher {
public StringEndsWith(String substring) { super("ending with", substring); }
public StringEndsWith(boolean ignoringCase, String substring) { super("ending with", ignoringCase, substring); }

@Override
protected boolean evalSubstringOf(String s) {
Expand All @@ -28,7 +28,7 @@ protected boolean evalSubstringOf(String s) {
*/
@Factory
public static Matcher<String> endsWith(String suffix) {
return new StringEndsWith(suffix);
return new StringEndsWith(false, suffix);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* Tests if the argument is a string that contains a substring.
*/
public class StringStartsWith extends SubstringMatcher {
public StringStartsWith(String substring) { super("starting with", substring); }
public StringStartsWith(boolean ignoringCase, String substring) { super("starting with", ignoringCase, substring); }

@Override
protected boolean evalSubstringOf(String s) {
Expand All @@ -27,8 +27,6 @@ protected boolean evalSubstringOf(String s) {
* the substring that the returned matcher will expect at the start of any examined string
*/
@Factory
public static Matcher<String> startsWith(String prefix) {
return new StringStartsWith(prefix);
}
public static Matcher<String> startsWith(String prefix) { return new StringStartsWith(false, prefix); }

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,18 @@ public abstract class SubstringMatcher extends TypeSafeMatcher<String> {
// String, StringBuffer, StringBuilder, CharBuffer, etc (joe).

private final String relationship;
private final boolean ignoringCase;
protected final String substring;

protected SubstringMatcher(String relationship, String substring) {
protected SubstringMatcher(String relationship, boolean ignoringCase, String substring) {
this.relationship = relationship;
this.ignoringCase = ignoringCase;
this.substring = substring;
}

@Override
public boolean matchesSafely(String item) {
return evalSubstringOf(item);
return evalSubstringOf(ignoringCase ? item.toLowerCase() :item);
}
@Override
public void describeMismatchSafely(String item, Description mismatchDescription) {
Expand All @@ -31,6 +33,9 @@ public void describeTo(Description description) {
.appendText(relationship)
.appendText(" ")
.appendValue(substring);
if (ignoringCase) {
description.appendText(" ignoring case");
}
}

protected abstract boolean evalSubstringOf(String string);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package org.hamcrest.core;

import static org.hamcrest.core.StringContains.containsString;
import static org.hamcrest.core.StringContains.containsStringIgnoringCase;

import org.hamcrest.AbstractMatcherTest;
import org.hamcrest.Matcher;
Expand All @@ -23,12 +24,21 @@ public void testMatchesSubstrings() {
assertMatches(stringContains, "START" + EXCERPT);
assertMatches(stringContains, "START" + EXCERPT + "END");
assertMatches(stringContains, EXCERPT + EXCERPT);
assertDoesNotMatch(stringContains, "XC");

assertMismatchDescription("was \"Something else\"", stringContains, "Something else");
assertMismatchDescription("was \"XC\"", stringContains, "XC");
assertDescription("a string containing \"EXCERPT\"", stringContains);
}

public void testMatchesSubstringsIgnoringCase() {
final Matcher<String> ignoringCase = containsStringIgnoringCase("ExCert");
assertMatches(ignoringCase, "eXcERT" + "END");
assertMatches(ignoringCase, "START" + "EXCert");
assertMatches(ignoringCase, "START" + "excERT" + "END");
assertMatches(ignoringCase, "eXCert" + "excErt");
assertDoesNotMatch(ignoringCase, "xc");

public void testHasAReadableDescription() {
assertDescription("a string containing \"EXCERPT\"", stringContains);
assertMismatchDescription("was \"Something else\"", ignoringCase, "Something else");
assertDescription("a string containing \"ExCert\" ignoring case", ignoringCase);
}
}

0 comments on commit 639b99e

Please sign in to comment.