com.exasol.matcher.EnhancedAllOfMatcher Maven / Gradle / Ivy
package com.exasol.matcher;
import java.util.Arrays;
import org.hamcrest.*;
/**
* This Hamcrest matcher improves upon the error reporting of the {@link org.hamcrest.core.AllOf} matcher.
*
* @param matcher type
*/
public class EnhancedAllOfMatcher extends DiagnosingMatcher {
private final Iterable> matchers;
private EnhancedAllOfMatcher(final Iterable> matchers) {
this.matchers = matchers;
}
/**
* Get an {@link EnhancedAllOfMatcher} for multiple other matchers.
*
* @param matchers matchers to combine
* @param matcher type
* @return built {@link EnhancedAllOfMatcher}
*/
@SafeVarargs
@SuppressWarnings("varargs")
public static Matcher enhancedAllOf(final Matcher super T>... matchers) {
return new EnhancedAllOfMatcher(Arrays.asList(matchers));
}
@Override
protected boolean matches(final Object item, final Description mismatchDescription) {
for (final Matcher super T> matcher : this.matchers) {
if (!matcher.matches(item)) {
matcher.describeMismatch(item, mismatchDescription);
return false;
}
}
return true;
}
@Override
public void describeTo(final Description description) {
description.appendList("(", " and ", ")", this.matchers);
}
}