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

uk.org.lidalia.test.CombinableMatcher Maven / Gradle / Ivy

The newest version!
package uk.org.lidalia.test;

import java.util.ArrayList;
import java.util.List;

import org.hamcrest.Description;
import org.hamcrest.Factory;
import org.hamcrest.Matcher;
import org.hamcrest.TypeSafeDiagnosingMatcher;

class CombinableMatcher extends TypeSafeDiagnosingMatcher {
    private final Matcher matcher;

    CombinableMatcher(final Matcher matcher) {
        super();
        this.matcher = matcher;
    }

    @Override
    protected boolean matchesSafely(final T item, final Description mismatch) {
        if (!matcher.matches(item)) {
            matcher.describeMismatch(item, mismatch);
            return false;
        }
        return true;
    }

    @Override
    public void describeTo(final Description description) {
        description.appendDescriptionOf(matcher);
    }

    CombinableMatcher and(final Matcher other) {
        return new CombinableMatcher(new AllOf(templatedListWith(other)));
    }

    private List> templatedListWith(final Matcher other) {
        final List> matchers = new ArrayList>();
        matchers.add(matcher);
        matchers.add(other);
        return matchers;
    }

    /**
     * Creates a matcher that matches when both of the specified matchers match the examined object.
     * 

* For example: *

assertThat("fab", both(containsString("a")).and(containsString("b")))
*/ @Factory static CombinableMatcher both(final Matcher matcher) { return new CombinableMatcher(matcher); } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy