org.hamcrest.text.StringContainsInOrder Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of hamcrest-library Show documentation
Show all versions of hamcrest-library Show documentation
A library of Hamcrest matchers - deprecated, please use "hamcrest" instead
package org.hamcrest.text;
import org.hamcrest.Description;
import org.hamcrest.Factory;
import org.hamcrest.Matcher;
import org.hamcrest.TypeSafeMatcher;
public class StringContainsInOrder extends TypeSafeMatcher {
private final Iterable substrings;
public StringContainsInOrder(Iterable substrings) {
this.substrings = substrings;
}
@Override
public boolean matchesSafely(String s) {
int fromIndex = 0;
for (String substring : substrings) {
fromIndex = s.indexOf(substring, fromIndex);
if (fromIndex == -1) {
return false;
}
}
return true;
}
@Override
public void describeMismatchSafely(String item, Description mismatchDescription) {
mismatchDescription.appendText("was \"").appendText(item).appendText("\"");
}
@Override
public void describeTo(Description description) {
description.appendText("a string containing ")
.appendValueList("", ", ", "", substrings)
.appendText(" in order");
}
/**
* Creates a matcher of {@link String} that matches when the examined string contains all of
* the specified substrings, regardless of the order of their appearance.
*
* For example:
* assertThat("myfoobarbaz", stringContainsInOrder(Arrays.asList("bar", "foo")))
*
* @param substrings
* the substrings that must be contained within matching strings
*/
@Factory
public static Matcher stringContainsInOrder(Iterable substrings) {
return new StringContainsInOrder(substrings);
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy