io.github.olib963.javatest.matchers.CollectionMatchers Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of javatest-matchers Show documentation
Show all versions of javatest-matchers Show documentation
Matchers to create assertions from common conditions
package io.github.olib963.javatest.matchers;
import io.github.olib963.javatest.matchers.internal.ElementThatMatcher;
import io.github.olib963.javatest.matchers.internal.PredicateMatcher;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
public class CollectionMatchers {
private CollectionMatchers() {}
public static Matcher> hasSize(final int size) {
return PredicateMatcher.of(c -> c.size() == size, "have size {" + size + "}", c -> "had size {" + c.size() + "}");
}
public static Matcher> isEmpty() {
return PredicateMatcher.of(Collection::isEmpty, "be empty");
}
public static Matcher> contains(final T element) {
return PredicateMatcher.of(c -> c.contains(element), "contain {" + element + "}");
}
@SafeVarargs
public static Matcher> containsAll(final T first, T... remaining) {
var elements = new ArrayList<>(remaining.length + 1);
elements.add(first);
elements.addAll(Arrays.asList(remaining));
return PredicateMatcher.of(c -> c.containsAll(elements), "contain all of {" + elements + "}");
}
public static Matcher> containsElementThat(Matcher matcher) {
return new ElementThatMatcher<>(matcher);
}
}