All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.hamcrest.text.StringContainsInOrder Maven / Gradle / Ivy

There is a newer version: 3.0
Show newest version
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