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

io.github.olib963.javatest.matchers.CollectionMatchers Maven / Gradle / Ivy

There is a newer version: 0.2.0
Show newest version
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);
    }
}





© 2015 - 2024 Weber Informatics LLC | Privacy Policy