
ext.test4j.hamcrest.core.CombinableMatcher Maven / Gradle / Ivy
package ext.test4j.hamcrest.core;
import ext.test4j.hamcrest.BaseMatcher;
import ext.test4j.hamcrest.Description;
import ext.test4j.hamcrest.Factory;
import ext.test4j.hamcrest.Matcher;
import java.util.ArrayList;
@SuppressWarnings({ "rawtypes", "unchecked" })
public class CombinableMatcher extends BaseMatcher {
private final Matcher super T> matcher;
public CombinableMatcher(Matcher super T> matcher) {
this.matcher = matcher;
}
public boolean matches(Object item) {
return matcher.matches(item);
}
public void describeTo(Description description) {
description.appendDescriptionOf(matcher);
}
public CombinableMatcher and(Matcher other) {
return new CombinableMatcher(new AllOf(templatedListWith(other)));
}
public CombinableMatcher or(Matcher other) {
return new CombinableMatcher(new AnyOf(templatedListWith(other)));
}
private ArrayList templatedListWith(Matcher other) {
ArrayList matchers = new ArrayList();
matchers.add(matcher);
matchers.add(other);
return matchers;
}
/**
* This is useful for fluently combining matchers that must both pass. For
* example:
*
*
* assertThat(string, both(containsString("a")).and(containsString("b")));
*
*/
@Factory
public static CombinableMatcher both(Matcher super LHS> matcher) {
return new CombinableMatcher(matcher);
}
/**
* This is useful for fluently combining matchers where either may pass, for
* example:
*
*
* assertThat(string, both(containsString("a")).and(containsString("b")));
*
*/
@Factory
public static CombinableMatcher either(Matcher super LHS> matcher) {
return new CombinableMatcher(matcher);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy