diff --git a/hamcrest-core/src/main/java/org/hamcrest/core/StringContains.java b/hamcrest-core/src/main/java/org/hamcrest/core/StringContains.java index e1dd7627..913e229b 100644 --- a/hamcrest-core/src/main/java/org/hamcrest/core/StringContains.java +++ b/hamcrest-core/src/main/java/org/hamcrest/core/StringContains.java @@ -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()); } /** @@ -31,7 +31,11 @@ protected boolean evalSubstringOf(String s) { */ @Factory public static Matcher containsString(String substring) { - return new StringContains(substring); + return new StringContains(false, substring); + } + @Factory + public static Matcher containsStringIgnoringCase(String substring) { + return new StringContains(true, substring); } } diff --git a/hamcrest-core/src/main/java/org/hamcrest/core/StringEndsWith.java b/hamcrest-core/src/main/java/org/hamcrest/core/StringEndsWith.java index 7638f48d..5d1fe249 100644 --- a/hamcrest-core/src/main/java/org/hamcrest/core/StringEndsWith.java +++ b/hamcrest-core/src/main/java/org/hamcrest/core/StringEndsWith.java @@ -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) { @@ -28,7 +28,7 @@ protected boolean evalSubstringOf(String s) { */ @Factory public static Matcher endsWith(String suffix) { - return new StringEndsWith(suffix); + return new StringEndsWith(false, suffix); } } diff --git a/hamcrest-core/src/main/java/org/hamcrest/core/StringStartsWith.java b/hamcrest-core/src/main/java/org/hamcrest/core/StringStartsWith.java index af3d5a92..5f518ae8 100644 --- a/hamcrest-core/src/main/java/org/hamcrest/core/StringStartsWith.java +++ b/hamcrest-core/src/main/java/org/hamcrest/core/StringStartsWith.java @@ -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) { @@ -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 startsWith(String prefix) { - return new StringStartsWith(prefix); - } + public static Matcher startsWith(String prefix) { return new StringStartsWith(false, prefix); } } diff --git a/hamcrest-core/src/main/java/org/hamcrest/core/SubstringMatcher.java b/hamcrest-core/src/main/java/org/hamcrest/core/SubstringMatcher.java index 25644d4c..4ebc153a 100644 --- a/hamcrest-core/src/main/java/org/hamcrest/core/SubstringMatcher.java +++ b/hamcrest-core/src/main/java/org/hamcrest/core/SubstringMatcher.java @@ -9,16 +9,18 @@ public abstract class SubstringMatcher extends TypeSafeMatcher { // 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) { @@ -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); diff --git a/hamcrest-core/src/test/java/org/hamcrest/core/StringContainsTest.java b/hamcrest-core/src/test/java/org/hamcrest/core/StringContainsTest.java index 8ad31c27..d455508a 100644 --- a/hamcrest-core/src/test/java/org/hamcrest/core/StringContainsTest.java +++ b/hamcrest-core/src/test/java/org/hamcrest/core/StringContainsTest.java @@ -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; @@ -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 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); } }