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

io.github.olib963.javatest.matchers.MapMatchers 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.EntryMatcher;
import io.github.olib963.javatest.matchers.internal.MapSetMatcher;
import io.github.olib963.javatest.matchers.internal.PredicateMatcher;

import java.util.Map;

public class MapMatchers {

    private MapMatchers() {}

    public static  Matcher> isEmptyMap() {
        return PredicateMatcher.of(Map::isEmpty, "be an empty map");
    }

    public static  Matcher> hasMapSize(int size) {
        return PredicateMatcher.of(m -> m.size() == size, "have size {" + size + "}", m -> "had size {" + m.size() + "}");
    }

    public static  Matcher> hasKey(K key) {
        return PredicateMatcher.of(m -> m.containsKey(key), "contain the key {" + key + "}");
    }

    public static  Matcher> hasKeyThat(Matcher keyMatcher) {
        return new MapSetMatcher<>(Map::keySet, keyMatcher, "key");
    }

    public static  Matcher> hasValue(V value) {
        return PredicateMatcher.of(m -> m.containsValue(value), "contain the value {" + value + "}");
    }

    public static  Matcher> hasValueThat(Matcher valueMatcher) {
        return new MapSetMatcher<>(Map::values, valueMatcher, "value");
    }

    public static  Matcher> hasEntry(K key, V value) {
        var entry = Map.entry(key, value);
        return PredicateMatcher.of(
                m -> m.entrySet().contains(entry),
                "contain an entry with key {" + key + "} and value {" + value + "}"
        );
    }

    public static  Matcher> hasEntryThat(Matcher keyMatcher, Matcher valueMatcher) {
        return new MapSetMatcher<>(Map::entrySet, new EntryMatcher<>(keyMatcher, valueMatcher), "entry");
    }

}





© 2015 - 2024 Weber Informatics LLC | Privacy Policy