Created
November 30, 2015 11:47
-
-
Save spuklo/660c4504d088a1d7f38f to your computer and use it in GitHub Desktop.
Case insensitive substring hamcrest matcher. I've found myself needing such matcher at least 3 times recently, so maybe someone else will also find it useful.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.hamcrest.Description; | |
import org.hamcrest.Factory; | |
import org.hamcrest.Matcher; | |
import org.hamcrest.TypeSafeMatcher; | |
public class CaseInsensitiveSubstringMatcher extends TypeSafeMatcher<String> { | |
private final String subString; | |
private CaseInsensitiveSubstringMatcher(final String subString) { | |
this.subString = subString; | |
} | |
@Override | |
protected boolean matchesSafely(final String actualString) { | |
return actualString.toLowerCase().contains(this.subString.toLowerCase()); | |
} | |
@Override | |
public void describeTo(final Description description) { | |
description.appendText("containing substring \"" + this.subString + "\""); | |
} | |
@Factory | |
public static Matcher<String> containsIgnoringCase(final String subString) { | |
return new CaseInsensitiveSubstringMatcher(subString); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
very nice!