Skip to content

Instantly share code, notes, and snippets.

@spuklo
Created November 30, 2015 11:47
Show Gist options
  • Save spuklo/660c4504d088a1d7f38f to your computer and use it in GitHub Desktop.
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.
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);
}
}
@nightscape
Copy link

I do 😄 👍

@whiskerz
Copy link

works like a charm! thanks a bunch 👍

@mlapierre
Copy link

Indeed, very useful, thanks!

@id23cat
Copy link

id23cat commented Sep 13, 2017

very nice!

@AlexBroadbent
Copy link

👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment