
uk.org.lidalia.test.CombinableMatcher Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of lidalia-test Show documentation
Show all versions of lidalia-test Show documentation
A collection of useful common assertions & matchers for unit testing.
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 super T> matcher;
CombinableMatcher(final Matcher super T> 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 super T> other) {
return new CombinableMatcher(new AllOf(templatedListWith(other)));
}
private List> templatedListWith(final Matcher super T> 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 super LHS> matcher) {
return new CombinableMatcher(matcher);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy