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

org.assertj.guava.api.MultisetAssert Maven / Gradle / Ivy

There is a newer version: 3.26.3
Show newest version
/*
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations under the License.
 *
 * Copyright 2012-2018 the original author or authors.
 */
package org.assertj.guava.api;

import static com.google.common.base.Preconditions.checkArgument;
import static org.assertj.guava.error.MultisetShouldContainAtLeastTimes.shouldContainAtLeastTimes;
import static org.assertj.guava.error.MultisetShouldContainAtMostTimes.shouldContainAtMostTimes;
import static org.assertj.guava.error.MultisetShouldContainTimes.shouldContainTimes;

import org.assertj.core.api.AbstractIterableAssert;
import org.assertj.core.api.ObjectAssert;
import org.assertj.core.internal.Failures;
import org.assertj.core.internal.Objects;
import org.assertj.core.util.VisibleForTesting;

import com.google.common.collect.HashMultiset;
import com.google.common.collect.Multiset;

/**
 *
 * Assertions for guava {@link Multiset}.
 * 

* To create an instance of this class, invoke {@link Assertions#assertThat(Multiset)} *

* * @param the type of elements contained in the tested Multiset value * * @author Max Daniline */ public class MultisetAssert extends AbstractIterableAssert, Multiset, T, ObjectAssert> { @VisibleForTesting Failures failures = Failures.instance(); protected MultisetAssert(Multiset actual) { super(actual, MultisetAssert.class); } /** * Verifies the actual {@link Multiset} contains the given value exactly the given number of times. *

* Example : * *

 Multiset<String> actual = HashMultiset.create();
   * actual.add("shoes", 2);
   *
   * // assertion succeeds
   * assertThat(actual).contains(2, "shoes");
   *
   * // assertions fail
   * assertThat(actual).contains(1, "shoes");
   * assertThat(actual).contains(3, "shoes");
* * @param expectedCount the exact number of times the given value should appear in the set * @param expected the value which to expect * * @return this {@link MultisetAssert} for fluent chaining * * @throws AssertionError if the actual {@link Multiset} is null * @throws AssertionError if the actual {@link Multiset} contains the given value a number of times different to the given count */ public MultisetAssert contains(int expectedCount, T expected) { Objects.instance().assertNotNull(info, actual); checkArgument(expectedCount >= 0, "The expected count should not be negative."); int actualCount = actual.count(expected); if (actualCount != expectedCount) { throw failures.failure(info, shouldContainTimes(actual, expected, expectedCount, actualCount)); } return myself; } /** * Verifies the actual {@link Multiset} contains the given value at least the given number of times. *

* Example : * *

 Multiset<String> actual = HashMultiset.create();
   * actual.add("shoes", 2);
   *
   * // assertions succeed
   * assertThat(actual).containsAtLeast(1, "shoes");
   * assertThat(actual).containsAtLeast(2, "shoes");
   *
   * // assertion fails
   * assertThat(actual).containsAtLeast(3, "shoes");
* * @param minimumCount the minimum number of times the given value should appear in the set * @param expected the value which to expect * * @return this {@link MultisetAssert} for fluent chaining * * @throws AssertionError if the actual {@link Multiset} is null * @throws AssertionError if the actual {@link Multiset} contains the given value fewer times than the given count */ public MultisetAssert containsAtLeast(int minimumCount, T expected) { Objects.instance().assertNotNull(info, actual); checkArgument(minimumCount >= 0, "The minimum count should not be negative."); int actualCount = actual.count(expected); if (actualCount < minimumCount) { throw failures.failure(info, shouldContainAtLeastTimes(actual, expected, minimumCount, actualCount)); } return myself; } /** * Verifies the actual {@link Multiset} contains the given value at most the given number of times. *

* Example : * *

 Multiset<String> actual = HashMultiset.create();
   * actual.add("shoes", 2);
   *
   * // assertions succeed
   * assertThat(actual).containsAtMost(3, "shoes");
   * assertThat(actual).containsAtMost(2, "shoes");
   *
   * // assertion fails
   * assertThat(actual).containsAtMost(1, "shoes");
* * * @param maximumCount the maximum number of times the given value should appear in the set * * @param expected the value which to expect * @return this {@link MultisetAssert} for fluent chaining * * @throws AssertionError if the actual {@link Multiset} is null * @throws AssertionError if the actual {@link Multiset} contains the given value more times than the given count */ public MultisetAssert containsAtMost(int maximumCount, T expected) { Objects.instance().assertNotNull(info, actual); checkArgument(maximumCount >= 0, "The maximum count should not be negative."); int actualCount = actual.count(expected); if (actualCount > maximumCount) { throw failures.failure(info, shouldContainAtMostTimes(actual, expected, maximumCount, actualCount)); } return myself; } @Override protected ObjectAssert toAssert(T value, String description) { return null; } @Override protected MultisetAssert newAbstractIterableAssert(Iterable iterable) { // actual may not have been a HashMultiset but there is no easy elegant to build the same Multiset subtype. Multiset filtered = HashMultiset.create(); iterable.forEach(filtered::add); return new MultisetAssert<>(filtered); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy