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

org.assertj.vavr.internal.Maps Maven / Gradle / Ivy

package org.assertj.vavr.internal;

import io.vavr.Tuple2;
import io.vavr.collection.Array;
import io.vavr.collection.HashSet;
import io.vavr.collection.Map;
import io.vavr.collection.Set;
import org.assertj.core.api.AssertionInfo;
import org.assertj.core.error.ShouldContainAnyOf;
import org.assertj.core.internal.Failures;
import org.assertj.core.internal.Objects;

import static org.assertj.core.error.ShouldContain.shouldContain;
import static org.assertj.core.error.ShouldNotContain.shouldNotContain;
import static org.assertj.core.internal.CommonValidations.failIfEmptySinceActualIsNotEmpty;
import static org.assertj.core.util.Objects.areEqual;
import static org.assertj.core.util.Preconditions.checkArgument;
import static org.assertj.core.util.Preconditions.checkNotNull;

public final class Maps {

    private static final Maps INSTANCE = new Maps();

    private Failures failures = Failures.instance();

    private Maps() {}

    public static Maps instance() {
        return INSTANCE;
    }

    public  void assertContainsAnyOf(AssertionInfo info, Map actual,
                                           Tuple2[] entries) {
        failIfNull(entries);
        assertNotNull(info, actual);
        // if both actual and values are empty, then assertion passes.
        if (actual.isEmpty() && entries.length == 0) return;
        failIfEmptySinceActualIsNotEmpty(entries);
        for (Tuple2 entry : entries) {
            if (containsEntry(actual, entry)) return;
        }
        throw failures.failure(info, ShouldContainAnyOf.shouldContainAnyOf(actual, entries));
    }

    /**
     * Asserts that the given {@code Map} contains the given entries, in any order.
     *
     * @param      key type
     * @param      value type
     * @param info    contains information about the assertion.
     * @param actual  the given {@code Map}.
     * @param entries the entries that are expected to be in the given {@code Map}.
     * @throws NullPointerException     if the array of entries is {@code null}.
     * @throws IllegalArgumentException if the array of entries is empty.
     * @throws NullPointerException     if any of the entries in the given array is {@code null}.
     * @throws AssertionError           if the given {@code Map} is {@code null}.
     * @throws AssertionError           if the given {@code Map} does not contain the given entries.
     */
    public  void assertContains(AssertionInfo info, Map actual,
                                      Tuple2[] entries) {
        failIfNull(entries);
        assertNotNull(info, actual);
        // if both actual and values are empty, then assertion passes.
        if (actual.isEmpty() && entries.length == 0) return;
        failIfEmptySinceActualIsNotEmpty(entries);
        final Set> notFound = Array
                .of(entries)
                .foldLeft(HashSet.empty(), (set, tuple) -> {
                    if (actual.contains(tuple)) {
                        return set;
                    } else {
                        return set.add(tuple);
                    }
                });
        if (!notFound.isEmpty()) {
            throw failures.failure(info, shouldContain(actual, entries, notFound));
        }
    }

    /**
     * Asserts that the given {@code Map} does not contain the given entries.
     *
     * @param      key type
     * @param      value type
     * @param info    contains information about the assertion.
     * @param actual  the given {@code Map}.
     * @param entries the entries that are expected to be in the given {@code Map}.
     * @throws NullPointerException     if the array of entries is {@code null}.
     * @throws IllegalArgumentException if the array of entries is empty.
     * @throws NullPointerException     if any of the entries in the given array is {@code null}.
     * @throws AssertionError           if the given {@code Map} is {@code null}.
     * @throws AssertionError           if the given {@code Map} contains any of the given entries.
     */
    public  void assertDoesNotContain(AssertionInfo info, Map actual,
                                            Tuple2[] entries) {
        failIfNullOrEmpty(entries);
        assertNotNull(info, actual);
        failIfEmptySinceActualIsNotEmpty(entries);
        final Set> found = Array
                .of(entries)
                .foldLeft(HashSet.empty(), (set, tuple) -> {
                    if (actual.contains(tuple)) {
                        return set.add(tuple);
                    } else {
                        return set;
                    }
                });
        if (!found.isEmpty()) {
            throw failures.failure(info, shouldNotContain(actual, entries, found));
        }
    }

    private  boolean containsEntry(Map actual, Tuple2 entry) {
        checkNotNull(entry, "Entries to look for should not be null");
        return actual.containsKey(entry._1) && areEqual(actual.get(entry._1).get(), entry._2);
    }

    private static  void failIfEmpty(Tuple2[] entries) {
        checkArgument(entries.length > 0, "The array of entries to look for should not be empty");
    }

    private static  void failIfNullOrEmpty(Tuple2[] entries) {
        failIfNull(entries);
        failIfEmpty(entries);
    }

    private static  void failIfNull(Tuple2[] entries) {
        checkNotNull(entries, "The array of entries to look for should not be null");
    }

    private void assertNotNull(AssertionInfo info, Map actual) {
        Objects.instance().assertNotNull(info, actual);
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy