code
stringlengths 23
201k
| docstring
stringlengths 17
96.2k
| func_name
stringlengths 0
235
| language
stringclasses 1
value | repo
stringlengths 8
72
| path
stringlengths 11
317
| url
stringlengths 57
377
| license
stringclasses 7
values |
|---|---|---|---|---|---|---|---|
public void containsAnyIn(Iterable<? extends E> expected) {
Collection<A> actual = iterableToCollection(getCastActual());
Correspondence.ExceptionStore exceptions = Correspondence.ExceptionStore.forIterable();
for (E expectedItem : expected) {
for (A actualItem : actual) {
if (correspondence.safeCompare(actualItem, expectedItem, exceptions)) {
// Found a match, but we still need to fail if we hit an exception along the way.
if (exceptions.hasCompareException()) {
subject.failWithoutActual(
ImmutableList.<Fact>builder()
.addAll(exceptions.describeAsMainCause())
.add(fact("expected to contain any of", expected))
.addAll(correspondence.describeForIterable())
.add(simpleFact("found match (but failing because of exception)"))
.add(subject.fullContents())
.build());
}
return;
}
}
}
// Found no match. Fail, reporting elements that have a correct key if there are any.
if (pairer != null) {
Pairing<A, E> pairing =
pairer.pair(iterableToList(expected), iterableToList(actual), exceptions);
if (pairing != null) {
if (!pairing.pairedKeysToExpectedValues.isEmpty()) {
subject.failWithoutActual(
ImmutableList.<Fact>builder()
.add(fact("expected to contain any of", expected))
.addAll(correspondence.describeForIterable())
.add(subject.butWas())
.addAll(describeAnyMatchesByKey(pairing, exceptions))
.addAll(exceptions.describeAsAdditionalInfo())
.build());
} else {
subject.failWithoutActual(
ImmutableList.<Fact>builder()
.add(fact("expected to contain any of", expected))
.addAll(correspondence.describeForIterable())
.add(subject.butWas())
.add(simpleFact("it does not contain any matches by key, either"))
.addAll(exceptions.describeAsAdditionalInfo())
.build());
}
} else {
subject.failWithoutActual(
ImmutableList.<Fact>builder()
.add(fact("expected to contain any of", expected))
.addAll(correspondence.describeForIterable())
.add(subject.butWas())
.add(
simpleFact(
"a key function which does not uniquely key the expected elements was"
+ " provided and has consequently been ignored"))
.addAll(exceptions.describeAsAdditionalInfo())
.build());
}
} else {
subject.failWithoutActual(
ImmutableList.<Fact>builder()
.add(fact("expected to contain any of", expected))
.addAll(correspondence.describeForIterable())
.add(subject.butWas())
.addAll(exceptions.describeAsAdditionalInfo())
.build());
}
}
|
Checks that the actual iterable contains at least one element that corresponds to at least
one of the expected elements.
|
containsAnyIn
|
java
|
google/truth
|
core/src/main/java/com/google/common/truth/IterableSubject.java
|
https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/IterableSubject.java
|
Apache-2.0
|
@SuppressWarnings("AvoidObjectArrays")
public void containsAnyIn(E[] expected) {
containsAnyIn(asList(expected));
}
|
Checks that the actual iterable contains at least one element that corresponds to at least
one of the expected elements.
|
containsAnyIn
|
java
|
google/truth
|
core/src/main/java/com/google/common/truth/IterableSubject.java
|
https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/IterableSubject.java
|
Apache-2.0
|
private ImmutableList<Fact> describeAnyMatchesByKey(
Pairing<A, E> pairing, Correspondence.ExceptionStore exceptions) {
ImmutableList.Builder<Fact> facts = ImmutableList.builder();
for (Object key : pairing.pairedKeysToExpectedValues.keySet()) {
E expected = pairing.pairedKeysToExpectedValues.get(key);
List<A> got = pairing.pairedKeysToActualValues.get(key);
facts.add(fact("for key", key));
facts.add(fact("expected any of", expected));
facts.addAll(formatExtras("but got", expected, got, exceptions));
facts.add(simpleFact("---"));
}
return facts.build();
}
|
Checks that the actual iterable contains at least one element that corresponds to at least
one of the expected elements.
|
describeAnyMatchesByKey
|
java
|
google/truth
|
core/src/main/java/com/google/common/truth/IterableSubject.java
|
https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/IterableSubject.java
|
Apache-2.0
|
@SafeVarargs
public final void containsNoneOf(
E firstExcluded, E secondExcluded, E @Nullable ... restOfExcluded) {
containsNoneIn(accumulate(firstExcluded, secondExcluded, restOfExcluded));
}
|
Checks that the actual iterable contains no elements that correspond to any of the given
elements.
|
containsNoneOf
|
java
|
google/truth
|
core/src/main/java/com/google/common/truth/IterableSubject.java
|
https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/IterableSubject.java
|
Apache-2.0
|
@SuppressWarnings("nullness") // TODO: b/423853632 - Remove after checker is fixed.
public void containsNoneIn(Iterable<? extends E> excluded) {
Collection<A> actual = iterableToCollection(getCastActual());
ListMultimap<E, A> present = LinkedListMultimap.create();
Correspondence.ExceptionStore exceptions = Correspondence.ExceptionStore.forIterable();
for (E excludedItem : Sets.newLinkedHashSet(excluded)) {
for (A actualItem : actual) {
if (correspondence.safeCompare(actualItem, excludedItem, exceptions)) {
present.put(excludedItem, actualItem);
}
}
}
// Fail if we found any matches.
if (!present.isEmpty()) {
ImmutableList.Builder<Fact> facts = ImmutableList.builder();
facts.add(fact("expected not to contain any of", annotateEmptyStrings(excluded)));
facts.addAll(correspondence.describeForIterable());
for (E excludedItem : present.keySet()) {
List<A> actualItems = present.get(excludedItem);
facts.add(fact("but contained", annotateEmptyStrings(actualItems)));
facts.add(fact("corresponding to", excludedItem));
facts.add(simpleFact("---"));
}
facts.add(subject.fullContents());
facts.addAll(exceptions.describeAsAdditionalInfo());
subject.failWithoutActual(facts.build());
return;
}
// Found no match, but we still need to fail if we hit an exception along the way.
if (exceptions.hasCompareException()) {
subject.failWithoutActual(
ImmutableList.<Fact>builder()
.addAll(exceptions.describeAsMainCause())
.add(fact("expected not to contain any of", annotateEmptyStrings(excluded)))
.addAll(correspondence.describeForIterable())
.add(simpleFact("found no matches (but failing because of exception)"))
.add(subject.fullContents())
.build());
}
}
|
Checks that the actual iterable contains no elements that correspond to any of the given
elements.
|
containsNoneIn
|
java
|
google/truth
|
core/src/main/java/com/google/common/truth/IterableSubject.java
|
https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/IterableSubject.java
|
Apache-2.0
|
@SuppressWarnings("AvoidObjectArrays")
public void containsNoneIn(E[] excluded) {
containsNoneIn(asList(excluded));
}
|
Checks that the subject contains no elements that correspond to any of the given elements.
|
containsNoneIn
|
java
|
google/truth
|
core/src/main/java/com/google/common/truth/IterableSubject.java
|
https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/IterableSubject.java
|
Apache-2.0
|
@SuppressWarnings("unchecked") // throwing ClassCastException is the correct behaviour
private Iterable<A> getCastActual() {
return (Iterable<A>) checkNotNull(subject.actual);
}
|
Checks that the subject contains no elements that correspond to any of the given elements.
|
getCastActual
|
java
|
google/truth
|
core/src/main/java/com/google/common/truth/IterableSubject.java
|
https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/IterableSubject.java
|
Apache-2.0
|
@Nullable Pairing<A, E> pair(
List<? extends E> expectedValues,
List<? extends A> actualValues,
Correspondence.ExceptionStore exceptions) {
Pairing<A, E> pairing = Pairing.create();
// Populate expectedKeys with the keys of the corresponding elements of expectedValues.
// We do this ahead of time to avoid invoking the key function twice for each element.
List<@Nullable Object> expectedKeys = new ArrayList<>(expectedValues.size());
for (E expected : expectedValues) {
expectedKeys.add(expectedKey(expected, exceptions));
}
// Populate pairedKeysToExpectedValues with *all* the expected values with non-null keys.
// We will remove the unpaired keys later. Return null if we find a duplicate key.
for (int i = 0; i < expectedValues.size(); i++) {
E expected = expectedValues.get(i);
Object key = expectedKeys.get(i);
if (key != null) {
if (pairing.pairedKeysToExpectedValues.containsKey(key)) {
return null;
} else {
pairing.pairedKeysToExpectedValues.put(key, expected);
}
}
}
// Populate pairedKeysToActualValues and unpairedActualValues.
for (A actual : actualValues) {
Object key = actualKey(actual, exceptions);
if (pairing.pairedKeysToExpectedValues.containsKey(key)) {
pairing.pairedKeysToActualValues.put(checkNotNull(key), actual);
} else {
pairing.unpairedActualValues.add(actual);
}
}
// Populate unpairedExpectedValues and remove unpaired keys from pairedKeysToExpectedValues.
for (int i = 0; i < expectedValues.size(); i++) {
E expected = expectedValues.get(i);
Object key = expectedKeys.get(i);
if (!pairing.pairedKeysToActualValues.containsKey(key)) {
pairing.unpairedExpectedValues.add(expected);
pairing.pairedKeysToExpectedValues.remove(key);
}
}
return pairing;
}
|
Returns a {@link Pairing} of the given expected and actual values, or {@code null} if the
expected values are not uniquely keyed.
|
pair
|
java
|
google/truth
|
core/src/main/java/com/google/common/truth/IterableSubject.java
|
https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/IterableSubject.java
|
Apache-2.0
|
List<A> pairOne(
E expectedValue,
Iterable<? extends A> actualValues,
Correspondence.ExceptionStore exceptions) {
Object key = expectedKey(expectedValue, exceptions);
List<A> matches = new ArrayList<>();
if (key != null) {
for (A actual : actualValues) {
if (key.equals(actualKey(actual, exceptions))) {
matches.add(actual);
}
}
}
return matches;
}
|
Returns a {@link Pairing} of the given expected and actual values, or {@code null} if the
expected values are not uniquely keyed.
|
pairOne
|
java
|
google/truth
|
core/src/main/java/com/google/common/truth/IterableSubject.java
|
https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/IterableSubject.java
|
Apache-2.0
|
private @Nullable Object actualKey(A actual, Correspondence.ExceptionStore exceptions) {
try {
return actualKeyFunction.apply(actual);
} catch (RuntimeException e) {
exceptions.addActualKeyFunctionException(
IterableSubject.UsingCorrespondence.Pairer.class, e, actual);
return null;
}
}
|
Returns a {@link Pairing} of the given expected and actual values, or {@code null} if the
expected values are not uniquely keyed.
|
actualKey
|
java
|
google/truth
|
core/src/main/java/com/google/common/truth/IterableSubject.java
|
https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/IterableSubject.java
|
Apache-2.0
|
private @Nullable Object expectedKey(E expected, Correspondence.ExceptionStore exceptions) {
try {
return expectedKeyFunction.apply(expected);
} catch (RuntimeException e) {
exceptions.addExpectedKeyFunctionException(
IterableSubject.UsingCorrespondence.Pairer.class, e, expected);
return null;
}
}
|
Returns a {@link Pairing} of the given expected and actual values, or {@code null} if the
expected values are not uniquely keyed.
|
expectedKey
|
java
|
google/truth
|
core/src/main/java/com/google/common/truth/IterableSubject.java
|
https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/IterableSubject.java
|
Apache-2.0
|
static <A extends @Nullable Object, E extends @Nullable Object> Pairer<A, E> create(
Function<? super A, ?> actualKeyFunction, Function<? super E, ?> expectedKeyFunction) {
return new Pairer<>(actualKeyFunction, expectedKeyFunction);
}
|
Returns a {@link Pairing} of the given expected and actual values, or {@code null} if the
expected values are not uniquely keyed.
|
create
|
java
|
google/truth
|
core/src/main/java/com/google/common/truth/IterableSubject.java
|
https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/IterableSubject.java
|
Apache-2.0
|
static <A extends @Nullable Object, E extends @Nullable Object> Pairing<A, E> create() {
return new Pairing<>();
}
|
List of the actual values not used in the pairing. Iterates in the order they appear in the
input.
|
create
|
java
|
google/truth
|
core/src/main/java/com/google/common/truth/IterableSubject.java
|
https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/IterableSubject.java
|
Apache-2.0
|
static Factory<IterableSubject, Iterable<?>> iterables() {
return IterableSubject::new;
}
|
List of the actual values not used in the pairing. Iterates in the order they appear in the
input.
|
iterables
|
java
|
google/truth
|
core/src/main/java/com/google/common/truth/IterableSubject.java
|
https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/IterableSubject.java
|
Apache-2.0
|
@Deprecated
@SuppressWarnings("InlineMeSuggester") // We want users to remove the surrounding call entirely.
public static Factory<LongStreamSubject, LongStream> longStreams() {
return LongStreamSubject::new;
}
|
Obsolete factory instance. This factory was previously necessary for assertions like {@code
assertWithMessage(...).about(longStreams()).that(stream)....}. Now, you can perform assertions
like that without the {@code about(...)} call.
@deprecated Instead of {@code about(longStreams()).that(...)}, use just {@code that(...)}.
Similarly, instead of {@code assertAbout(longStreams()).that(...)}, use just {@code
assertThat(...)}.
|
longStreams
|
java
|
google/truth
|
core/src/main/java/com/google/common/truth/LongStreamSubject.java
|
https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/LongStreamSubject.java
|
Apache-2.0
|
@SuppressWarnings("GoodTime") // false positive; b/122617528
public void containsAnyOf(long first, long second, long... rest) {
checkThatContentsList().containsAnyOf(first, second, box(rest));
}
|
Checks that the actual stream contains at least one of the given elements.
|
containsAnyOf
|
java
|
google/truth
|
core/src/main/java/com/google/common/truth/LongStreamSubject.java
|
https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/LongStreamSubject.java
|
Apache-2.0
|
@SuppressWarnings("GoodTime") // false positive; b/122617528
@CanIgnoreReturnValue
public Ordered containsAtLeast(long first, long second, long... rest) {
return checkThatContentsList().containsAtLeast(first, second, box(rest));
}
|
Checks that the actual stream contains all of the given elements. If an element appears more
than once in the given elements, then it must appear at least that number of times in the
actual elements.
<p>To also test that the contents appear in the given order, make a call to {@code inOrder()}
on the object returned by this method. The expected elements must appear in the given order
within the actual elements, but they are not required to be consecutive.
|
containsAtLeast
|
java
|
google/truth
|
core/src/main/java/com/google/common/truth/LongStreamSubject.java
|
https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/LongStreamSubject.java
|
Apache-2.0
|
@CanIgnoreReturnValue
public Ordered containsAtLeastElementsIn(@Nullable Iterable<?> expected) {
return checkThatContentsList().containsAtLeastElementsIn(expected);
}
|
Checks that the actual stream contains all of the given elements. If an element appears more
than once in the given elements, then it must appear at least that number of times in the
actual elements.
<p>To also test that the contents appear in the given order, make a call to {@code inOrder()}
on the object returned by this method. The expected elements must appear in the given order
within the actual elements, but they are not required to be consecutive.
|
containsAtLeastElementsIn
|
java
|
google/truth
|
core/src/main/java/com/google/common/truth/LongStreamSubject.java
|
https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/LongStreamSubject.java
|
Apache-2.0
|
@CanIgnoreReturnValue
public Ordered containsExactlyElementsIn(@Nullable Iterable<?> expected) {
return checkThatContentsList().containsExactlyElementsIn(expected);
}
|
Checks that the actual stream contains exactly the given elements.
<p>Multiplicity is respected. For example, an object duplicated exactly 3 times in the
parameters asserts that the object must likewise be duplicated exactly 3 times in the actual
stream.
<p>To also test that the contents appear in the given order, make a call to {@code inOrder()}
on the object returned by this method.
|
containsExactlyElementsIn
|
java
|
google/truth
|
core/src/main/java/com/google/common/truth/LongStreamSubject.java
|
https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/LongStreamSubject.java
|
Apache-2.0
|
@SuppressWarnings("GoodTime") // false positive; b/122617528
public void containsNoneOf(long first, long second, long... rest) {
checkThatContentsList().containsNoneOf(first, second, box(rest));
}
|
Checks that the actual stream does not contain any of the given elements.
|
containsNoneOf
|
java
|
google/truth
|
core/src/main/java/com/google/common/truth/LongStreamSubject.java
|
https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/LongStreamSubject.java
|
Apache-2.0
|
private IterableSubject checkThatContentsList() {
return substituteCheck().that(listSupplier.get());
}
|
Be careful with using this, as documented on {@link Subject#substituteCheck}.
|
checkThatContentsList
|
java
|
google/truth
|
core/src/main/java/com/google/common/truth/LongStreamSubject.java
|
https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/LongStreamSubject.java
|
Apache-2.0
|
private static Supplier<@Nullable List<?>> listCollector(@Nullable LongStream actual) {
return () -> actual == null ? null : actual.boxed().collect(toCollection(ArrayList::new));
}
|
Be careful with using this, as documented on {@link Subject#substituteCheck}.
|
listCollector
|
java
|
google/truth
|
core/src/main/java/com/google/common/truth/LongStreamSubject.java
|
https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/LongStreamSubject.java
|
Apache-2.0
|
private static Object[] box(long[] rest) {
return LongStream.of(rest).boxed().toArray(Long[]::new);
}
|
Be careful with using this, as documented on {@link Subject#substituteCheck}.
|
box
|
java
|
google/truth
|
core/src/main/java/com/google/common/truth/LongStreamSubject.java
|
https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/LongStreamSubject.java
|
Apache-2.0
|
@Deprecated
@Override
public boolean equals(@Nullable Object o) {
throw new UnsupportedOperationException(
"If you meant to compare longs, use .of(long) instead.");
}
|
@throws UnsupportedOperationException always
@deprecated {@link Object#equals(Object)} is not supported on TolerantLongComparison. If you
meant to compare longs, use {@link #of(long)} instead.
|
equals
|
java
|
google/truth
|
core/src/main/java/com/google/common/truth/LongSubject.java
|
https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/LongSubject.java
|
Apache-2.0
|
@Deprecated
@Override
public int hashCode() {
throw new UnsupportedOperationException("Subject.hashCode() is not supported.");
}
|
@throws UnsupportedOperationException always
@deprecated {@link Object#hashCode()} is not supported on TolerantLongComparison
|
hashCode
|
java
|
google/truth
|
core/src/main/java/com/google/common/truth/LongSubject.java
|
https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/LongSubject.java
|
Apache-2.0
|
static TolerantLongComparison create(LongComparer comparer) {
return new TolerantLongComparison(comparer);
}
|
@throws UnsupportedOperationException always
@deprecated {@link Object#hashCode()} is not supported on TolerantLongComparison
|
create
|
java
|
google/truth
|
core/src/main/java/com/google/common/truth/LongSubject.java
|
https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/LongSubject.java
|
Apache-2.0
|
public TolerantLongComparison isWithin(long tolerance) {
return TolerantLongComparison.create(
other -> {
if (tolerance < 0) {
failWithoutActual(
simpleFact(
"could not perform approximate-equality check because tolerance is negative"),
numericFact("expected", other),
numericFact("was", actual),
numericFact("tolerance", tolerance));
} else if (actual == null) {
failWithoutActual(
numericFact("expected a value near", other),
numericFact("but was", actual),
numericFact("tolerance", tolerance));
} else if (!equalWithinTolerance(actual, other, tolerance)) {
failWithoutActual(
numericFact("expected", other),
numericFact("but was", actual),
numericFact("outside tolerance", tolerance));
}
});
}
|
Prepares for a check that the actual value is a number within the given tolerance of an
expected value that will be provided in the next call in the fluent chain.
@param tolerance an inclusive upper bound on the difference between the actual value and
expected value allowed by the check, which must be a non-negative value.
@since 1.2
|
isWithin
|
java
|
google/truth
|
core/src/main/java/com/google/common/truth/LongSubject.java
|
https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/LongSubject.java
|
Apache-2.0
|
public TolerantLongComparison isNotWithin(long tolerance) {
return TolerantLongComparison.create(
other -> {
if (tolerance < 0) {
failWithoutActual(
simpleFact(
"could not perform approximate-equality check because tolerance is negative"),
numericFact("expected", other),
numericFact("was", actual),
numericFact("tolerance", tolerance));
} else if (actual == null) {
failWithoutActual(
numericFact("expected a value that is not near", other),
numericFact("but was", actual),
numericFact("tolerance", tolerance));
} else if (equalWithinTolerance(actual, other, tolerance)) {
failWithoutActual(
numericFact("expected not to be", other),
numericFact("but was", actual),
numericFact("within tolerance", tolerance));
}
});
}
|
Prepares for a check that the actual value is a number not within the given tolerance of an
expected value that will be provided in the next call in the fluent chain.
@param tolerance an exclusive lower bound on the difference between the actual value and
expected value allowed by the check, which must be a non-negative value.
@since 1.2
|
isNotWithin
|
java
|
google/truth
|
core/src/main/java/com/google/common/truth/LongSubject.java
|
https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/LongSubject.java
|
Apache-2.0
|
@Override
@Deprecated
public final void isEquivalentAccordingToCompareTo(@Nullable Long other) {
super.isEquivalentAccordingToCompareTo(other);
}
|
@deprecated Use {@link #isEqualTo} instead. Long comparison is consistent with equality.
|
isEquivalentAccordingToCompareTo
|
java
|
google/truth
|
core/src/main/java/com/google/common/truth/LongSubject.java
|
https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/LongSubject.java
|
Apache-2.0
|
public final void isGreaterThan(int other) {
isGreaterThan((long) other);
}
|
Checks that the actual value is greater than {@code other}.
<p>To check that the actual value is greater than <i>or equal to</i> {@code other}, use {@link
#isAtLeast}.
|
isGreaterThan
|
java
|
google/truth
|
core/src/main/java/com/google/common/truth/LongSubject.java
|
https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/LongSubject.java
|
Apache-2.0
|
public final void isLessThan(int other) {
isLessThan((long) other);
}
|
Checks that the actual value is less than {@code other}.
<p>To check that the actual value is less than <i>or equal to</i> {@code other}, use {@link
#isAtMost} .
|
isLessThan
|
java
|
google/truth
|
core/src/main/java/com/google/common/truth/LongSubject.java
|
https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/LongSubject.java
|
Apache-2.0
|
public final void isAtMost(int other) {
isAtMost((long) other);
}
|
Checks that the actual value is less than or equal to {@code other}.
<p>To check that the actual value is <i>strictly</i> less than {@code other}, use {@link
#isLessThan}.
|
isAtMost
|
java
|
google/truth
|
core/src/main/java/com/google/common/truth/LongSubject.java
|
https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/LongSubject.java
|
Apache-2.0
|
public final void isAtLeast(int other) {
isAtLeast((long) other);
}
|
Checks that the actual value is greater than or equal to {@code other}.
<p>To check that the actual value is <i>strictly</i> greater than {@code other}, use {@link
#isGreaterThan}.
|
isAtLeast
|
java
|
google/truth
|
core/src/main/java/com/google/common/truth/LongSubject.java
|
https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/LongSubject.java
|
Apache-2.0
|
static Factory<LongSubject, Long> longs() {
return LongSubject::new;
}
|
Checks that the actual value is greater than or equal to {@code other}.
<p>To check that the actual value is <i>strictly</i> greater than {@code other}, use {@link
#isGreaterThan}.
|
longs
|
java
|
google/truth
|
core/src/main/java/com/google/common/truth/LongSubject.java
|
https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/LongSubject.java
|
Apache-2.0
|
@Override
public final void isEqualTo(@Nullable Object other) {
if (Objects.equals(actual, other)) {
return;
}
// Fail but with a more descriptive message:
if (actual == null || !(other instanceof Map)) {
super.isEqualTo(other);
return;
}
containsEntriesInAnyOrder((Map<?, ?>) other, /* allowUnexpected= */ false);
}
|
Constructor for use by subclasses. If you want to create an instance of this class itself, call
{@link Subject#check(String, Object...) check(...)}{@code .that(actual)}.
|
isEqualTo
|
java
|
google/truth
|
core/src/main/java/com/google/common/truth/MapSubject.java
|
https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/MapSubject.java
|
Apache-2.0
|
public final void isEmpty() {
if (!checkNotNull(actual).isEmpty()) {
failWithActual(simpleFact("expected to be empty"));
}
}
|
Checks that the actual map is empty.
|
isEmpty
|
java
|
google/truth
|
core/src/main/java/com/google/common/truth/MapSubject.java
|
https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/MapSubject.java
|
Apache-2.0
|
public final void isNotEmpty() {
if (checkNotNull(actual).isEmpty()) {
failWithoutActual(simpleFact("expected not to be empty"));
}
}
|
Checks that the actual map is not empty.
|
isNotEmpty
|
java
|
google/truth
|
core/src/main/java/com/google/common/truth/MapSubject.java
|
https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/MapSubject.java
|
Apache-2.0
|
public final void hasSize(int expectedSize) {
checkArgument(expectedSize >= 0, "expectedSize (%s) must be >= 0", expectedSize);
check("size()").that(checkNotNull(actual).size()).isEqualTo(expectedSize);
}
|
Checks that the actual map has the given size.
|
hasSize
|
java
|
google/truth
|
core/src/main/java/com/google/common/truth/MapSubject.java
|
https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/MapSubject.java
|
Apache-2.0
|
public final void containsEntry(@Nullable Object key, @Nullable Object value) {
Map.Entry<@Nullable Object, @Nullable Object> entry = immutableEntry(key, value);
checkNotNull(actual);
if (!actual.entrySet().contains(entry)) {
List<@Nullable Object> keyList = singletonList(key);
List<@Nullable Object> valueList = singletonList(value);
if (actual.containsKey(key)) {
Object actualValue = actual.get(key);
if (Objects.equals(actualValue, value)) {
/*
* `contains(entry(key, value))` returned `false`, but `get(key)` returned a result equal
* to `value`. We're probably looking at an `IdentityHashMap`, which compares values (not
* just keys!) using `==`.
*
* `IdentityHashMap` isn't following the contract for `Map`, so we're within our rights to
* do whatever we want. But it's probably simplest for us and best for users if we just
* make the assertion pass: While users probably *do* want us to follow the
* `IdentityHashMap` behavior of comparing *keys* with `==`, they probably *don't* want us
* to follow the same behavior for *values*.
*/
return;
}
/*
* In the case of a null expected or actual map, clarify that the key *is* present and
* *is* expected to be present. That is, get() isn't returning null to indicate that the key
* is missing, and the user isn't making an assertion that the key is missing.
*/
StandardSubjectBuilder check = check("get(%s)", key);
if (value == null || actualValue == null) {
check = check.withMessage("key is present but with a different value");
}
// See the comment on IterableSubject's use of failEqualityCheckForEqualsWithoutDescription.
check.that(actualValue).failEqualityCheckForEqualsWithoutDescription(value);
} else if (hasMatchingToStringPair(actual.keySet(), keyList)) {
failWithoutActual(
fact("expected to contain entry", entry),
fact("an instance of", objectToTypeName(entry)),
simpleFact("but did not"),
fact(
"though it did contain keys",
countDuplicatesAndAddTypeInfo(
retainMatchingToString(actual.keySet(), /* itemsToCheck= */ keyList))),
fact("full contents", actualCustomStringRepresentationForPackageMembersToCall()));
} else if (actual.containsValue(value)) {
Set<@Nullable Object> keys = new LinkedHashSet<>();
for (Map.Entry<?, ?> actualEntry : actual.entrySet()) {
if (Objects.equals(actualEntry.getValue(), value)) {
keys.add(actualEntry.getKey());
}
}
failWithoutActual(
fact("expected to contain entry", entry),
simpleFact("but did not"),
fact("though it did contain keys with that value", keys),
fact("full contents", actualCustomStringRepresentationForPackageMembersToCall()));
} else if (hasMatchingToStringPair(actual.values(), valueList)) {
failWithoutActual(
fact("expected to contain entry", entry),
fact("an instance of", objectToTypeName(entry)),
simpleFact("but did not"),
fact(
"though it did contain values",
countDuplicatesAndAddTypeInfo(
retainMatchingToString(actual.values(), /* itemsToCheck= */ valueList))),
fact("full contents", actualCustomStringRepresentationForPackageMembersToCall()));
} else {
failWithActual("expected to contain entry", entry);
}
}
}
|
Checks that the actual map contains the given entry.
|
containsEntry
|
java
|
google/truth
|
core/src/main/java/com/google/common/truth/MapSubject.java
|
https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/MapSubject.java
|
Apache-2.0
|
public final void doesNotContainEntry(@Nullable Object key, @Nullable Object value) {
checkNoNeedToDisplayBothValues("entrySet()")
.that(checkNotNull(actual).entrySet())
.doesNotContain(immutableEntry(key, value));
}
|
Checks that the actual map does not contain the given entry.
|
doesNotContainEntry
|
java
|
google/truth
|
core/src/main/java/com/google/common/truth/MapSubject.java
|
https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/MapSubject.java
|
Apache-2.0
|
@CanIgnoreReturnValue
public final Ordered containsExactly() {
return containsExactlyEntriesIn(ImmutableMap.of());
}
|
Checks that the actual map is empty.
|
containsExactly
|
java
|
google/truth
|
core/src/main/java/com/google/common/truth/MapSubject.java
|
https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/MapSubject.java
|
Apache-2.0
|
@CanIgnoreReturnValue
public final Ordered containsExactly(
@Nullable Object k0, @Nullable Object v0, @Nullable Object... rest) {
return containsExactlyEntriesIn(accumulateMap("containsExactly", k0, v0, rest));
}
|
Checks that the actual map contains exactly the given set of key/value pairs.
<p><b>Warning:</b> the use of varargs means that we cannot guarantee an equal number of
key/value pairs at compile time. Please make sure you provide varargs in key/value pairs!
<p>The arguments must not contain duplicate keys.
|
containsExactly
|
java
|
google/truth
|
core/src/main/java/com/google/common/truth/MapSubject.java
|
https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/MapSubject.java
|
Apache-2.0
|
@CanIgnoreReturnValue
public final Ordered containsAtLeast(
@Nullable Object k0, @Nullable Object v0, @Nullable Object... rest) {
return containsAtLeastEntriesIn(accumulateMap("containsAtLeast", k0, v0, rest));
}
|
Checks that the actual map contains exactly the given set of key/value pairs.
<p><b>Warning:</b> the use of varargs means that we cannot guarantee an equal number of
key/value pairs at compile time. Please make sure you provide varargs in key/value pairs!
<p>The arguments must not contain duplicate keys.
|
containsAtLeast
|
java
|
google/truth
|
core/src/main/java/com/google/common/truth/MapSubject.java
|
https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/MapSubject.java
|
Apache-2.0
|
private static Map<@Nullable Object, @Nullable Object> accumulateMap(
String functionName, @Nullable Object k0, @Nullable Object v0, @Nullable Object... rest) {
checkArgument(
rest.length % 2 == 0,
"There must be an equal number of key/value pairs "
+ "(i.e., the number of key/value parameters (%s) must be even).",
rest.length + 2);
Map<@Nullable Object, @Nullable Object> expectedMap = new LinkedHashMap<>();
expectedMap.put(k0, v0);
Multiset<@Nullable Object> keys = LinkedHashMultiset.create();
keys.add(k0);
for (int i = 0; i < rest.length; i += 2) {
Object key = rest[i];
expectedMap.put(key, rest[i + 1]);
keys.add(key);
}
checkArgument(
keys.size() == expectedMap.size(),
"Duplicate keys (%s) cannot be passed to %s().",
keys,
functionName);
return expectedMap;
}
|
Checks that the actual map contains exactly the given set of key/value pairs.
<p><b>Warning:</b> the use of varargs means that we cannot guarantee an equal number of
key/value pairs at compile time. Please make sure you provide varargs in key/value pairs!
<p>The arguments must not contain duplicate keys.
|
accumulateMap
|
java
|
google/truth
|
core/src/main/java/com/google/common/truth/MapSubject.java
|
https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/MapSubject.java
|
Apache-2.0
|
@CanIgnoreReturnValue
public final Ordered containsExactlyEntriesIn(Map<?, ?> expectedMap) {
if (expectedMap.isEmpty()) {
if (checkNotNull(actual).isEmpty()) {
return IN_ORDER;
} else {
isEmpty(); // fails
return ALREADY_FAILED;
}
}
boolean containsAnyOrder = containsEntriesInAnyOrder(expectedMap, /* allowUnexpected= */ false);
if (containsAnyOrder) {
return MapInOrder.create(
this, expectedMap, /* allowUnexpected= */ false, /* correspondence= */ null);
} else {
return ALREADY_FAILED;
}
}
|
Checks that the actual map contains exactly the given set of entries in the given map.
|
containsExactlyEntriesIn
|
java
|
google/truth
|
core/src/main/java/com/google/common/truth/MapSubject.java
|
https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/MapSubject.java
|
Apache-2.0
|
@CanIgnoreReturnValue
public final Ordered containsAtLeastEntriesIn(Map<?, ?> expectedMap) {
if (expectedMap.isEmpty()) {
return IN_ORDER;
}
boolean containsAnyOrder = containsEntriesInAnyOrder(expectedMap, /* allowUnexpected= */ true);
if (containsAnyOrder) {
return MapInOrder.create(
this, expectedMap, /* allowUnexpected= */ true, /* correspondence= */ null);
} else {
return ALREADY_FAILED;
}
}
|
Checks that the actual map contains at least the given set of entries in the given map.
|
containsAtLeastEntriesIn
|
java
|
google/truth
|
core/src/main/java/com/google/common/truth/MapSubject.java
|
https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/MapSubject.java
|
Apache-2.0
|
@CanIgnoreReturnValue
private boolean containsEntriesInAnyOrder(Map<?, ?> expectedMap, boolean allowUnexpected) {
MapDifference<@Nullable Object, @Nullable Object, @Nullable Object> diff =
MapDifference.create(checkNotNull(actual), expectedMap, allowUnexpected, Objects::equals);
if (diff.isEmpty()) {
return true;
}
// TODO(cpovirk): Consider adding a special-case where the diff contains exactly one key which
// is present with the wrong value, doing an isEqualTo assertion on the values. Pro: This gives
// us all the extra power of isEqualTo, including maybe throwing a ComparisonFailure. Con: It
// might be misleading to report a single mismatched value when the assertion was on the whole
// map - this could be mitigated by adding extra info explaining that. (Would need to ensure
// that it still fails in cases where e.g. the value is 1 and it should be 1L, where isEqualTo
// succeeds: perhaps failEqualityCheckForEqualsWithoutDescription will do the right thing.)
// First, we need to decide whether this kind of cleverness is a line we want to cross.
// (See also containsEntry, which does do an isEqualTo-like assertion when the expected key is
// present with the wrong value, which may be the closest we currently get to this.)
failWithoutActual(
ImmutableList.<Fact>builder()
.addAll(diff.describe(/* differ= */ null))
.add(simpleFact("---"))
.add(fact(allowUnexpected ? "expected to contain at least" : "expected", expectedMap))
.add(butWas())
.build());
return false;
}
|
Checks that the actual map contains at least the given set of entries in the given map.
|
containsEntriesInAnyOrder
|
java
|
google/truth
|
core/src/main/java/com/google/common/truth/MapSubject.java
|
https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/MapSubject.java
|
Apache-2.0
|
static <K extends @Nullable Object, A extends @Nullable Object, E extends @Nullable Object>
MapDifference<K, A, E> create(
Map<? extends K, ? extends A> actual,
Map<? extends K, ? extends E> expected,
boolean allowUnexpected,
ValueTester<? super A, ? super E> valueTester) {
Map<K, A> unexpected = new LinkedHashMap<>(actual);
Map<K, E> missing = new LinkedHashMap<>();
Map<K, ValueDifference<A, E>> wrongValues = new LinkedHashMap<>();
for (Map.Entry<? extends K, ? extends E> expectedEntry : expected.entrySet()) {
K expectedKey = expectedEntry.getKey();
E expectedValue = expectedEntry.getValue();
if (actual.containsKey(expectedKey)) {
@SuppressWarnings("UnnecessaryCast") // needed by nullness checker
A actualValue = (A) unexpected.remove(expectedKey);
if (!valueTester.test(actualValue, expectedValue)) {
wrongValues.put(expectedKey, ValueDifference.create(actualValue, expectedValue));
}
} else {
missing.put(expectedKey, expectedValue);
}
}
if (allowUnexpected) {
unexpected.clear();
}
return new MapDifference<>(
missing, unexpected, wrongValues, Sets.union(actual.keySet(), expected.keySet()));
}
|
Checks that the actual map contains at least the given set of entries in the given map.
|
create
|
java
|
google/truth
|
core/src/main/java/com/google/common/truth/MapSubject.java
|
https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/MapSubject.java
|
Apache-2.0
|
boolean isEmpty() {
return missing.isEmpty() && unexpected.isEmpty() && wrongValues.isEmpty();
}
|
Checks that the actual map contains at least the given set of entries in the given map.
|
isEmpty
|
java
|
google/truth
|
core/src/main/java/com/google/common/truth/MapSubject.java
|
https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/MapSubject.java
|
Apache-2.0
|
ImmutableList<Fact> describe(@Nullable Differ<? super A, ? super E> differ) {
boolean includeKeyTypes = includeKeyTypes();
ImmutableList.Builder<Fact> facts = ImmutableList.builder();
if (!wrongValues.isEmpty()) {
facts.add(simpleFact("keys with wrong values"));
}
for (Map.Entry<K, ValueDifference<A, E>> entry : wrongValues.entrySet()) {
facts.add(fact("for key", maybeAddType(entry.getKey(), includeKeyTypes)));
facts.addAll(entry.getValue().describe(differ));
}
if (!missing.isEmpty()) {
facts.add(simpleFact("missing keys"));
}
for (Map.Entry<K, E> entry : missing.entrySet()) {
facts.add(fact("for key", maybeAddType(entry.getKey(), includeKeyTypes)));
facts.add(fact("expected value", entry.getValue()));
}
if (!unexpected.isEmpty()) {
facts.add(simpleFact("unexpected keys"));
}
for (Map.Entry<K, A> entry : unexpected.entrySet()) {
facts.add(fact("for key", maybeAddType(entry.getKey(), includeKeyTypes)));
facts.add(fact("unexpected value", entry.getValue()));
}
return facts.build();
}
|
Checks that the actual map contains at least the given set of entries in the given map.
|
describe
|
java
|
google/truth
|
core/src/main/java/com/google/common/truth/MapSubject.java
|
https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/MapSubject.java
|
Apache-2.0
|
private boolean includeKeyTypes() {
// We will annotate all the keys in the diff with their types if any of the keys involved have
// the same toString() without being equal.
Set<K> keys = new HashSet<>();
keys.addAll(missing.keySet());
keys.addAll(unexpected.keySet());
keys.addAll(wrongValues.keySet());
return hasMatchingToStringPair(keys, allKeys);
}
|
Checks that the actual map contains at least the given set of entries in the given map.
|
includeKeyTypes
|
java
|
google/truth
|
core/src/main/java/com/google/common/truth/MapSubject.java
|
https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/MapSubject.java
|
Apache-2.0
|
ImmutableList<Fact> describe(@Nullable Differ<? super A, ? super E> differ) {
boolean includeTypes =
differ == null && String.valueOf(actual).equals(String.valueOf(expected));
ImmutableList.Builder<Fact> facts =
ImmutableList.<Fact>builder()
.add(fact("expected value", maybeAddType(expected, includeTypes)))
.add(fact("but got value", maybeAddType(actual, includeTypes)));
if (differ != null) {
String diffString = differ.diff(actual, expected);
if (diffString != null) {
facts.add(fact("diff", diffString));
}
}
return facts.build();
}
|
Checks that the actual map contains at least the given set of entries in the given map.
|
describe
|
java
|
google/truth
|
core/src/main/java/com/google/common/truth/MapSubject.java
|
https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/MapSubject.java
|
Apache-2.0
|
static <A extends @Nullable Object, E extends @Nullable Object> ValueDifference<A, E> create(
A actual, E expected) {
return new ValueDifference<>(actual, expected);
}
|
Checks that the actual map contains at least the given set of entries in the given map.
|
create
|
java
|
google/truth
|
core/src/main/java/com/google/common/truth/MapSubject.java
|
https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/MapSubject.java
|
Apache-2.0
|
private static String maybeAddType(@Nullable Object o, boolean includeTypes) {
return includeTypes ? lenientFormat("%s (%s)", o, objectToTypeName(o)) : String.valueOf(o);
}
|
Checks that the actual map contains at least the given set of entries in the given map.
|
maybeAddType
|
java
|
google/truth
|
core/src/main/java/com/google/common/truth/MapSubject.java
|
https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/MapSubject.java
|
Apache-2.0
|
@Override
public void inOrder() {
// We're using the fact that Sets.intersection keeps the order of the first set.
checkNotNull(subject.actual);
List<?> expectedKeyOrder =
new ArrayList<>(Sets.intersection(expectedMap.keySet(), subject.actual.keySet()));
List<?> actualKeyOrder =
new ArrayList<>(Sets.intersection(subject.actual.keySet(), expectedMap.keySet()));
if (!actualKeyOrder.equals(expectedKeyOrder)) {
ImmutableList.Builder<Fact> facts =
ImmutableList.<Fact>builder()
.add(
simpleFact(
allowUnexpected
? "required entries were all found, but order was wrong"
: "entries match, but order was wrong"))
.add(
fact(
allowUnexpected ? "expected to contain at least" : "expected",
expectedMap));
if (correspondence != null) {
facts.addAll(correspondence.describeForMapValues());
}
subject.failWithActual(facts.build());
}
}
|
Checks whether the common elements between actual and expected are in the same order.
<p>This doesn't check whether the keys have the same values or whether all the required keys
are actually present. That was supposed to be done before the "in order" part.
|
inOrder
|
java
|
google/truth
|
core/src/main/java/com/google/common/truth/MapSubject.java
|
https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/MapSubject.java
|
Apache-2.0
|
static MapInOrder create(
MapSubject subject,
Map<?, ?> expectedMap,
boolean allowUnexpected,
@Nullable Correspondence<?, ?> correspondence) {
return new MapInOrder(subject, expectedMap, allowUnexpected, correspondence);
}
|
Checks whether the common elements between actual and expected are in the same order.
<p>This doesn't check whether the keys have the same values or whether all the required keys
are actually present. That was supposed to be done before the "in order" part.
|
create
|
java
|
google/truth
|
core/src/main/java/com/google/common/truth/MapSubject.java
|
https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/MapSubject.java
|
Apache-2.0
|
public final <A extends @Nullable Object, E extends @Nullable Object>
UsingCorrespondence<A, E> comparingValuesUsing(
Correspondence<? super A, ? super E> correspondence) {
return UsingCorrespondence.create(this, correspondence);
}
|
Starts a method chain for a check in which the actual values (i.e. the values of the {@link
Map} under test) are compared to expected values using the given {@link Correspondence}. The
actual values must be of type {@code A}, the expected values must be of type {@code E}. The
check is actually executed by continuing the method chain. For example:
<pre>{@code
assertThat(actualMap)
.comparingValuesUsing(correspondence)
.containsEntry(expectedKey, expectedValue);
}</pre>
where {@code actualMap} is a {@code Map<?, A>} (or, more generally, a {@code Map<?, ? extends
A>}), {@code correspondence} is a {@code Correspondence<A, E>}, and {@code expectedValue} is an
{@code E}.
<p>Note that keys will always be compared with regular object equality ({@link Object#equals}).
<p>Any of the methods on the returned object may throw {@link ClassCastException} if they
encounter an actual map that is not of type {@code A} or an expected value that is not of type
{@code E}.
|
comparingValuesUsing
|
java
|
google/truth
|
core/src/main/java/com/google/common/truth/MapSubject.java
|
https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/MapSubject.java
|
Apache-2.0
|
public final <V extends @Nullable Object> UsingCorrespondence<V, V> formattingDiffsUsing(
DiffFormatter<? super V, ? super V> formatter) {
return comparingValuesUsing(Correspondence.<V>equality().formattingDiffsUsing(formatter));
}
|
Starts a method chain for a check in which failure messages may use the given {@link
DiffFormatter} to describe the difference between an actual map (i.e. a value in the {@link
Map} under test) and the value it is expected to be equal to, but isn't. The actual and
expected values must be of type {@code V}. The check is actually executed by continuing the
method chain. For example:
<pre>{@code
assertThat(actualMap)
.formattingDiffsUsing(FooTestHelper::formatDiff)
.containsExactly(key1, foo1, key2, foo2, key3, foo3);
}</pre>
where {@code actualMap} is a {@code Map<?, Foo>} (or, more generally, a {@code Map<?, ? extends
Foo>}), {@code FooTestHelper.formatDiff} is a static method taking two {@code Foo} arguments
and returning a {@link String}, and {@code foo1}, {@code foo2}, and {@code foo3} are {@code
Foo} instances.
<p>Unlike when using {@link #comparingValuesUsing}, the values are still compared using object
equality, so this method does not affect whether a test passes or fails.
<p>Any of the methods on the returned object may throw {@link ClassCastException} if they
encounter a value that is not of type {@code V}.
@since 1.1
|
formattingDiffsUsing
|
java
|
google/truth
|
core/src/main/java/com/google/common/truth/MapSubject.java
|
https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/MapSubject.java
|
Apache-2.0
|
@SuppressWarnings("UnnecessaryCast") // needed by nullness checker
public void containsEntry(@Nullable Object expectedKey, E expectedValue) {
if (checkNotNull(actual).containsKey(expectedKey)) {
// Found matching key.
A actualValue = getCastSubject().get(expectedKey);
Correspondence.ExceptionStore exceptions = Correspondence.ExceptionStore.forMapValues();
if (correspondence.safeCompare((A) actualValue, expectedValue, exceptions)) {
// The expected key had the expected value. There's no need to check exceptions here,
// because if Correspondence.compare() threw then safeCompare() would return false.
return;
}
// Found matching key with non-matching value.
String diff = correspondence.safeFormatDiff((A) actualValue, expectedValue, exceptions);
if (diff != null) {
failWithoutActual(
ImmutableList.<Fact>builder()
.add(fact("for key", expectedKey))
.add(fact("expected value", expectedValue))
.addAll(correspondence.describeForMapValues())
.add(fact("but got value", actualValue))
.add(fact("diff", diff))
.add(fact("full map", actualCustomStringRepresentationForPackageMembersToCall()))
.addAll(exceptions.describeAsAdditionalInfo())
.build());
} else {
failWithoutActual(
ImmutableList.<Fact>builder()
.add(fact("for key", expectedKey))
.add(fact("expected value", expectedValue))
.addAll(correspondence.describeForMapValues())
.add(fact("but got value", actualValue))
.add(fact("full map", actualCustomStringRepresentationForPackageMembersToCall()))
.addAll(exceptions.describeAsAdditionalInfo())
.build());
}
} else {
// Did not find matching key. Look for the matching value with a different key.
Set<@Nullable Object> keys = new LinkedHashSet<>();
Correspondence.ExceptionStore exceptions = Correspondence.ExceptionStore.forMapValues();
for (Map.Entry<?, A> actualEntry : getCastSubject().entrySet()) {
if (correspondence.safeCompare(actualEntry.getValue(), expectedValue, exceptions)) {
keys.add(actualEntry.getKey());
}
}
if (!keys.isEmpty()) {
// Found matching values with non-matching keys.
failWithoutActual(
ImmutableList.<Fact>builder()
.add(fact("for key", expectedKey))
.add(fact("expected value", expectedValue))
.addAll(correspondence.describeForMapValues())
.add(simpleFact("but was missing"))
.add(fact("other keys with matching values", keys))
.add(fact("full map", actualCustomStringRepresentationForPackageMembersToCall()))
.addAll(exceptions.describeAsAdditionalInfo())
.build());
} else {
// Did not find matching key or value.
failWithoutActual(
ImmutableList.<Fact>builder()
.add(fact("for key", expectedKey))
.add(fact("expected value", expectedValue))
.addAll(correspondence.describeForMapValues())
.add(simpleFact("but was missing"))
.add(fact("full map", actualCustomStringRepresentationForPackageMembersToCall()))
.addAll(exceptions.describeAsAdditionalInfo())
.build());
}
}
}
|
Checks that the actual map contains an entry with the given key and a value that corresponds
to the given value.
|
containsEntry
|
java
|
google/truth
|
core/src/main/java/com/google/common/truth/MapSubject.java
|
https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/MapSubject.java
|
Apache-2.0
|
@SuppressWarnings("UnnecessaryCast") // needed by nullness checker
public void doesNotContainEntry(@Nullable Object excludedKey, E excludedValue) {
if (checkNotNull(actual).containsKey(excludedKey)) {
// Found matching key. Fail if the value matches, too.
A actualValue = getCastSubject().get(excludedKey);
Correspondence.ExceptionStore exceptions = Correspondence.ExceptionStore.forMapValues();
if (correspondence.safeCompare((A) actualValue, excludedValue, exceptions)) {
// The matching key had a matching value. There's no need to check exceptions here,
// because if Correspondence.compare() threw then safeCompare() would return false.
failWithoutActual(
ImmutableList.<Fact>builder()
.add(fact("expected not to contain", immutableEntry(excludedKey, excludedValue)))
.addAll(correspondence.describeForMapValues())
.add(
fact(
"but contained",
Maps.<@Nullable Object, @Nullable A>immutableEntry(
excludedKey, actualValue)))
.add(fact("full map", actualCustomStringRepresentationForPackageMembersToCall()))
.addAll(exceptions.describeAsAdditionalInfo())
.build());
}
// The value didn't match, but we still need to fail if we hit an exception along the way.
if (exceptions.hasCompareException()) {
failWithoutActual(
ImmutableList.<Fact>builder()
.addAll(exceptions.describeAsMainCause())
.add(fact("expected not to contain", immutableEntry(excludedKey, excludedValue)))
.addAll(correspondence.describeForMapValues())
.add(simpleFact("found no match (but failing because of exception)"))
.add(fact("full map", actualCustomStringRepresentationForPackageMembersToCall()))
.build());
}
}
}
|
Checks that the actual map does not contain an entry with the given key and a value that
corresponds to the given value.
|
doesNotContainEntry
|
java
|
google/truth
|
core/src/main/java/com/google/common/truth/MapSubject.java
|
https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/MapSubject.java
|
Apache-2.0
|
@CanIgnoreReturnValue
public Ordered containsExactly(@Nullable Object k0, @Nullable E v0, @Nullable Object... rest) {
@SuppressWarnings("unchecked") // throwing ClassCastException is the correct behaviour
Map<Object, E> expectedMap = (Map<Object, E>) accumulateMap("containsExactly", k0, v0, rest);
return containsExactlyEntriesIn(expectedMap);
}
|
Checks that the actual map contains exactly the given set of keys mapping to values that
correspond to the given values.
<p>The values must all be of type {@code E}, and a {@link ClassCastException} will be thrown
if any other type is encountered.
<p><b>Warning:</b> the use of varargs means that we cannot guarantee an equal number of
key/value pairs at compile time. Please make sure you provide varargs in key/value pairs!
|
containsExactly
|
java
|
google/truth
|
core/src/main/java/com/google/common/truth/MapSubject.java
|
https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/MapSubject.java
|
Apache-2.0
|
@CanIgnoreReturnValue
public Ordered containsAtLeast(@Nullable Object k0, @Nullable E v0, @Nullable Object... rest) {
@SuppressWarnings("unchecked") // throwing ClassCastException is the correct behaviour
Map<Object, E> expectedMap = (Map<Object, E>) accumulateMap("containsAtLeast", k0, v0, rest);
return containsAtLeastEntriesIn(expectedMap);
}
|
Checks that the actual map contains at least the given set of keys mapping to values that
correspond to the given values.
<p>The values must all be of type {@code E}, and a {@link ClassCastException} will be thrown
if any other type is encountered.
<p><b>Warning:</b> the use of varargs means that we cannot guarantee an equal number of
key/value pairs at compile time. Please make sure you provide varargs in key/value pairs!
|
containsAtLeast
|
java
|
google/truth
|
core/src/main/java/com/google/common/truth/MapSubject.java
|
https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/MapSubject.java
|
Apache-2.0
|
@CanIgnoreReturnValue
public Ordered containsExactlyEntriesIn(Map<?, ? extends E> expectedMap) {
if (expectedMap.isEmpty()) {
if (checkNotNull(actual).isEmpty()) {
return IN_ORDER;
} else {
subject.isEmpty(); // fails
return ALREADY_FAILED;
}
}
return internalContainsEntriesIn(expectedMap, /* allowUnexpected= */ false);
}
|
Checks that the actual map contains exactly the keys in the given map, mapping to values that
correspond to the values of the given map.
|
containsExactlyEntriesIn
|
java
|
google/truth
|
core/src/main/java/com/google/common/truth/MapSubject.java
|
https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/MapSubject.java
|
Apache-2.0
|
@CanIgnoreReturnValue
public Ordered containsAtLeastEntriesIn(Map<?, ? extends E> expectedMap) {
if (expectedMap.isEmpty()) {
return IN_ORDER;
}
return internalContainsEntriesIn(expectedMap, /* allowUnexpected= */ true);
}
|
Checks that the actual map contains at least the keys in the given map, mapping to values
that correspond to the values of the given map.
|
containsAtLeastEntriesIn
|
java
|
google/truth
|
core/src/main/java/com/google/common/truth/MapSubject.java
|
https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/MapSubject.java
|
Apache-2.0
|
private <K extends @Nullable Object, V extends E> Ordered internalContainsEntriesIn(
Map<K, V> expectedMap, boolean allowUnexpected) {
Correspondence.ExceptionStore exceptions = Correspondence.ExceptionStore.forMapValues();
MapDifference<@Nullable Object, A, V> diff =
MapDifference.create(
getCastSubject(),
expectedMap,
allowUnexpected,
(actualValue, expectedValue) ->
correspondence.safeCompare(actualValue, expectedValue, exceptions));
if (diff.isEmpty()) {
// The maps correspond exactly. There's no need to check exceptions here, because if
// Correspondence.compare() threw then safeCompare() would return false and the diff would
// record that we had the wrong value for that key.
return MapInOrder.create(subject, expectedMap, allowUnexpected, correspondence);
}
failWithoutActual(
ImmutableList.<Fact>builder()
.addAll(diff.describe(differ(exceptions)))
.add(simpleFact("---"))
.add(fact(allowUnexpected ? "expected to contain at least" : "expected", expectedMap))
.addAll(correspondence.describeForMapValues())
.add(butWas())
.addAll(exceptions.describeAsAdditionalInfo())
.build());
return ALREADY_FAILED;
}
|
Checks that the actual map contains at least the keys in the given map, mapping to values
that correspond to the values of the given map.
|
internalContainsEntriesIn
|
java
|
google/truth
|
core/src/main/java/com/google/common/truth/MapSubject.java
|
https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/MapSubject.java
|
Apache-2.0
|
private <V extends E> Differ<A, V> differ(Correspondence.ExceptionStore exceptions) {
return (actual, expected) -> correspondence.safeFormatDiff(actual, expected, exceptions);
}
|
Checks that the actual map contains at least the keys in the given map, mapping to values
that correspond to the values of the given map.
|
differ
|
java
|
google/truth
|
core/src/main/java/com/google/common/truth/MapSubject.java
|
https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/MapSubject.java
|
Apache-2.0
|
@SuppressWarnings("unchecked") // throwing ClassCastException is the correct behaviour
private Map<?, A> getCastSubject() {
return (Map<?, A>) checkNotNull(actual);
}
|
Checks that the actual map contains at least the keys in the given map, mapping to values
that correspond to the values of the given map.
|
getCastSubject
|
java
|
google/truth
|
core/src/main/java/com/google/common/truth/MapSubject.java
|
https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/MapSubject.java
|
Apache-2.0
|
private String actualCustomStringRepresentationForPackageMembersToCall() {
return subject.actualCustomStringRepresentationForPackageMembersToCall();
}
|
Checks that the actual map contains at least the keys in the given map, mapping to values
that correspond to the values of the given map.
|
actualCustomStringRepresentationForPackageMembersToCall
|
java
|
google/truth
|
core/src/main/java/com/google/common/truth/MapSubject.java
|
https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/MapSubject.java
|
Apache-2.0
|
private Fact butWas() {
return subject.butWas();
}
|
Checks that the actual map contains at least the keys in the given map, mapping to values
that correspond to the values of the given map.
|
butWas
|
java
|
google/truth
|
core/src/main/java/com/google/common/truth/MapSubject.java
|
https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/MapSubject.java
|
Apache-2.0
|
static <A extends @Nullable Object, E extends @Nullable Object>
UsingCorrespondence<A, E> create(
MapSubject subject, Correspondence<? super A, ? super E> correspondence) {
return new UsingCorrespondence<>(subject, correspondence);
}
|
Checks that the actual map contains at least the keys in the given map, mapping to values
that correspond to the values of the given map.
|
create
|
java
|
google/truth
|
core/src/main/java/com/google/common/truth/MapSubject.java
|
https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/MapSubject.java
|
Apache-2.0
|
static Factory<MapSubject, Map<?, ?>> maps() {
return MapSubject::new;
}
|
Checks that the actual map contains at least the keys in the given map, mapping to values
that correspond to the values of the given map.
|
maps
|
java
|
google/truth
|
core/src/main/java/com/google/common/truth/MapSubject.java
|
https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/MapSubject.java
|
Apache-2.0
|
static boolean equalWithinTolerance(long left, long right, long tolerance) {
try {
long absDiff = abs(subtractExact(left, right));
return 0 <= absDiff && absDiff <= abs(tolerance);
} catch (ArithmeticException e) {
// The numbers are so far apart their difference isn't even a long.
return false;
}
}
|
Returns true iff {@code left} and {@code right} are values within {@code tolerance} of each
other.
|
equalWithinTolerance
|
java
|
google/truth
|
core/src/main/java/com/google/common/truth/MathUtil.java
|
https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/MathUtil.java
|
Apache-2.0
|
static boolean equalWithinTolerance(int left, int right, int tolerance) {
try {
int absDiff = abs(subtractExact(left, right));
return 0 <= absDiff && absDiff <= abs(tolerance);
} catch (ArithmeticException e) {
// The numbers are so far apart their difference isn't even a int.
return false;
}
}
|
Returns true iff {@code left} and {@code right} are values within {@code tolerance} of each
other.
|
equalWithinTolerance
|
java
|
google/truth
|
core/src/main/java/com/google/common/truth/MathUtil.java
|
https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/MathUtil.java
|
Apache-2.0
|
public static boolean equalWithinTolerance(double left, double right, double tolerance) {
return abs(left - right) <= abs(tolerance);
}
|
Returns true iff {@code left} and {@code right} are finite values within {@code tolerance} of
each other. Note that both this method and {@link #notEqualWithinTolerance} returns false if
either {@code left} or {@code right} is infinite or NaN.
|
equalWithinTolerance
|
java
|
google/truth
|
core/src/main/java/com/google/common/truth/MathUtil.java
|
https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/MathUtil.java
|
Apache-2.0
|
public static boolean equalWithinTolerance(float left, float right, float tolerance) {
return equalWithinTolerance(left, right, (double) tolerance);
}
|
Returns true iff {@code left} and {@code right} are finite values within {@code tolerance} of
each other. Note that both this method and {@link #notEqualWithinTolerance} returns false if
either {@code left} or {@code right} is infinite or NaN.
|
equalWithinTolerance
|
java
|
google/truth
|
core/src/main/java/com/google/common/truth/MathUtil.java
|
https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/MathUtil.java
|
Apache-2.0
|
public static boolean notEqualWithinTolerance(double left, double right, double tolerance) {
return isFinite(left) && isFinite(right) && abs(left - right) > abs(tolerance);
}
|
Returns true iff {@code left} and {@code right} are finite values not within {@code tolerance}
of each other. Note that both this method and {@link #equalWithinTolerance} returns false if
either {@code left} or {@code right} is infinite or NaN.
|
notEqualWithinTolerance
|
java
|
google/truth
|
core/src/main/java/com/google/common/truth/MathUtil.java
|
https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/MathUtil.java
|
Apache-2.0
|
public static boolean notEqualWithinTolerance(float left, float right, float tolerance) {
return notEqualWithinTolerance(left, right, (double) tolerance);
}
|
Returns true iff {@code left} and {@code right} are finite values not within {@code tolerance}
of each other. Note that both this method and {@link #equalWithinTolerance} returns false if
either {@code left} or {@code right} is infinite or NaN.
|
notEqualWithinTolerance
|
java
|
google/truth
|
core/src/main/java/com/google/common/truth/MathUtil.java
|
https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/MathUtil.java
|
Apache-2.0
|
static void checkTolerance(double tolerance) {
checkArgument(!Double.isNaN(tolerance), "tolerance cannot be NaN");
checkArgument(
Double.compare(tolerance, 0.0) >= 0, "tolerance (%s) cannot be negative", tolerance);
checkArgument(tolerance != Double.POSITIVE_INFINITY, "tolerance cannot be POSITIVE_INFINITY");
}
|
Ensures that the given tolerance is a non-negative finite value, i.e. not {@code Double.NaN},
{@code Double.POSITIVE_INFINITY}, or negative, including {@code -0.0}.
|
checkTolerance
|
java
|
google/truth
|
core/src/main/java/com/google/common/truth/MathUtil.java
|
https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/MathUtil.java
|
Apache-2.0
|
public final void isEmpty() {
if (!checkNotNull(actual).isEmpty()) {
failWithActual(simpleFact("expected to be empty"));
}
}
|
Checks that the actual multimap is empty.
|
isEmpty
|
java
|
google/truth
|
core/src/main/java/com/google/common/truth/MultimapSubject.java
|
https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/MultimapSubject.java
|
Apache-2.0
|
public final void isNotEmpty() {
if (checkNotNull(actual).isEmpty()) {
failWithoutActual(simpleFact("expected not to be empty"));
}
}
|
Checks that the actual multimap is not empty.
|
isNotEmpty
|
java
|
google/truth
|
core/src/main/java/com/google/common/truth/MultimapSubject.java
|
https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/MultimapSubject.java
|
Apache-2.0
|
public final void hasSize(int expectedSize) {
checkArgument(expectedSize >= 0, "expectedSize(%s) must be >= 0", expectedSize);
check("size()").that(checkNotNull(actual).size()).isEqualTo(expectedSize);
}
|
Checks that the actual multimap has the given size.
|
hasSize
|
java
|
google/truth
|
core/src/main/java/com/google/common/truth/MultimapSubject.java
|
https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/MultimapSubject.java
|
Apache-2.0
|
public final void containsEntry(@Nullable Object key, @Nullable Object value) {
// TODO(kak): Can we share any of this logic w/ MapSubject.containsEntry()?
checkNotNull(actual);
if (!actual.containsEntry(key, value)) {
Map.Entry<@Nullable Object, @Nullable Object> entry = immutableEntry(key, value);
ImmutableList<Map.Entry<@Nullable Object, @Nullable Object>> entryList =
ImmutableList.of(entry);
// TODO(cpovirk): If the key is present but not with the right value, we could fail using
// something like valuesForKey(key).contains(value). Consider whether this is worthwhile.
if (hasMatchingToStringPair(actual.entries(), entryList)) {
failWithoutActual(
fact("expected to contain entry", entry),
fact("an instance of", objectToTypeName(entry)),
simpleFact("but did not"),
fact(
"though it did contain",
countDuplicatesAndAddTypeInfo(
retainMatchingToString(actual.entries(), /* itemsToCheck= */ entryList))),
fact("full contents", actualCustomStringRepresentationForPackageMembersToCall()));
} else if (actual.containsKey(key)) {
failWithoutActual(
fact("expected to contain entry", entry),
simpleFact("but did not"),
fact("though it did contain values with that key", actual.asMap().get(key)),
fact("full contents", actualCustomStringRepresentationForPackageMembersToCall()));
} else if (actual.containsValue(value)) {
Set<@Nullable Object> keys = new LinkedHashSet<>();
for (Map.Entry<?, ?> actualEntry : actual.entries()) {
if (Objects.equals(actualEntry.getValue(), value)) {
keys.add(actualEntry.getKey());
}
}
failWithoutActual(
fact("expected to contain entry", entry),
simpleFact("but did not"),
fact("though it did contain keys with that value", keys),
fact("full contents", actualCustomStringRepresentationForPackageMembersToCall()));
} else {
failWithActual("expected to contain entry", immutableEntry(key, value));
}
}
}
|
Checks that the actual multimap contains the given entry.
|
containsEntry
|
java
|
google/truth
|
core/src/main/java/com/google/common/truth/MultimapSubject.java
|
https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/MultimapSubject.java
|
Apache-2.0
|
public final void doesNotContainEntry(@Nullable Object key, @Nullable Object value) {
checkNoNeedToDisplayBothValues("entries()")
.that(checkNotNull(actual).entries())
.doesNotContain(immutableEntry(key, value));
}
|
Checks that the actual multimap does not contain the given entry.
|
doesNotContainEntry
|
java
|
google/truth
|
core/src/main/java/com/google/common/truth/MultimapSubject.java
|
https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/MultimapSubject.java
|
Apache-2.0
|
@SuppressWarnings("unchecked") // safe because we only read, not write
/*
* non-final because it's overridden by MultimapWithProtoValuesSubject.
*
* If we really, really wanted it to be final, we could investigate whether
* MultimapWithProtoValuesFluentAssertion could provide its own valuesForKey method. But that
* would force callers to perform any configuration _before_ the valuesForKey call, while
* currently they must perform it _after_.
*/
public IterableSubject valuesForKey(@Nullable Object key) {
return check("valuesForKey(%s)", key)
.that(((Multimap<@Nullable Object, @Nullable Object>) checkNotNull(actual)).get(key));
}
|
Returns a {@link Subject} for making assertions about the values for the given key within the
{@link Multimap}.
<p>This method performs no checks on its own and cannot cause test failures. Subsequent
assertions must be chained onto this method call to test properties of the {@link Multimap}.
|
valuesForKey
|
java
|
google/truth
|
core/src/main/java/com/google/common/truth/MultimapSubject.java
|
https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/MultimapSubject.java
|
Apache-2.0
|
@Override
public final void isEqualTo(@Nullable Object other) {
@SuppressWarnings("UndefinedEquals") // the contract of this method is to follow Multimap.equals
boolean isEqual = Objects.equals(actual, other);
if (isEqual) {
return;
}
// Fail but with a more descriptive message:
if ((actual instanceof ListMultimap && other instanceof SetMultimap)
|| (actual instanceof SetMultimap && other instanceof ListMultimap)) {
String actualType = (actual instanceof ListMultimap) ? "ListMultimap" : "SetMultimap";
String otherType = (other instanceof ListMultimap) ? "ListMultimap" : "SetMultimap";
failWithoutActual(
fact("expected", other),
fact("an instance of", otherType),
fact("but was", actualCustomStringRepresentationForPackageMembersToCall()),
fact("an instance of", actualType),
simpleFact(
lenientFormat(
"a %s cannot equal a %s if either is non-empty", actualType, otherType)));
} else if (actual instanceof ListMultimap) {
containsExactlyEntriesIn((Multimap<?, ?>) checkNotNull(other)).inOrder();
} else if (actual instanceof SetMultimap) {
containsExactlyEntriesIn((Multimap<?, ?>) checkNotNull(other));
} else {
super.isEqualTo(other);
}
}
|
Returns a {@link Subject} for making assertions about the values for the given key within the
{@link Multimap}.
<p>This method performs no checks on its own and cannot cause test failures. Subsequent
assertions must be chained onto this method call to test properties of the {@link Multimap}.
|
isEqualTo
|
java
|
google/truth
|
core/src/main/java/com/google/common/truth/MultimapSubject.java
|
https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/MultimapSubject.java
|
Apache-2.0
|
@CanIgnoreReturnValue
public final Ordered containsExactlyEntriesIn(Multimap<?, ?> expectedMultimap) {
checkNotNull(expectedMultimap, "expectedMultimap");
checkNotNull(actual);
ListMultimap<?, ?> missing = difference(expectedMultimap, actual);
ListMultimap<?, ?> extra = difference(actual, expectedMultimap);
// TODO(kak): Possible enhancement: Include "[1 copy]" if the element does appear in
// the actual multimap but not enough times. Similarly for unexpected extra items.
if (!missing.isEmpty()) {
if (!extra.isEmpty()) {
boolean addTypeInfo = hasMatchingToStringPair(missing.entries(), extra.entries());
// Note: The usage of countDuplicatesAndAddTypeInfo() below causes entries no longer to be
// grouped by key in the 'missing' and 'unexpected items' parts of the message (we still
// show the actual and expected multimaps in the standard format).
String missingDisplay =
addTypeInfo
? countDuplicatesAndAddTypeInfo(annotateEmptyStringsMultimap(missing).entries())
: countDuplicatesMultimap(annotateEmptyStringsMultimap(missing));
String extraDisplay =
addTypeInfo
? countDuplicatesAndAddTypeInfo(annotateEmptyStringsMultimap(extra).entries())
: countDuplicatesMultimap(annotateEmptyStringsMultimap(extra));
failWithActual(
fact("missing", missingDisplay),
fact("unexpected", extraDisplay),
simpleFact("---"),
fact("expected", annotateEmptyStringsMultimap(expectedMultimap)));
return ALREADY_FAILED;
} else {
failWithActual(
fact("missing", countDuplicatesMultimap(annotateEmptyStringsMultimap(missing))),
simpleFact("---"),
fact("expected", annotateEmptyStringsMultimap(expectedMultimap)));
return ALREADY_FAILED;
}
} else if (!extra.isEmpty()) {
failWithActual(
fact("unexpected", countDuplicatesMultimap(annotateEmptyStringsMultimap(extra))),
simpleFact("---"),
fact("expected", annotateEmptyStringsMultimap(expectedMultimap)));
return ALREADY_FAILED;
}
return MultimapInOrder.create(this, /* allowUnexpected= */ false, expectedMultimap);
}
|
Checks that the actual multimap contains precisely the same entries as the argument {@link
Multimap}.
<p>A subsequent call to {@link Ordered#inOrder} may be made if the caller wishes to verify that
the two multimaps iterate fully in the same order. That is, their key sets iterate in the same
order, and the value collections for each key iterate in the same order.
|
containsExactlyEntriesIn
|
java
|
google/truth
|
core/src/main/java/com/google/common/truth/MultimapSubject.java
|
https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/MultimapSubject.java
|
Apache-2.0
|
@CanIgnoreReturnValue
public final Ordered containsAtLeastEntriesIn(Multimap<?, ?> expectedMultimap) {
checkNotNull(expectedMultimap, "expectedMultimap");
checkNotNull(actual);
ListMultimap<?, ?> missing = difference(expectedMultimap, actual);
// TODO(kak): Possible enhancement: Include "[1 copy]" if the element does appear in
// the actual multimap but not enough times. Similarly for unexpected extra items.
if (!missing.isEmpty()) {
failWithActual(
fact("missing", countDuplicatesMultimap(annotateEmptyStringsMultimap(missing))),
simpleFact("---"),
fact("expected to contain at least", annotateEmptyStringsMultimap(expectedMultimap)));
return ALREADY_FAILED;
}
return MultimapInOrder.create(this, /* allowUnexpected= */ true, expectedMultimap);
}
|
Checks that the actual multimap contains at least the entries in the argument {@link Multimap}.
<p>A subsequent call to {@link Ordered#inOrder} may be made if the caller wishes to verify that
the entries are present in the same order as given. That is, the keys are present in the given
order in the key set, and the values for each key are present in the given order order in the
value collections.
|
containsAtLeastEntriesIn
|
java
|
google/truth
|
core/src/main/java/com/google/common/truth/MultimapSubject.java
|
https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/MultimapSubject.java
|
Apache-2.0
|
@CanIgnoreReturnValue
public final Ordered containsExactly() {
return substituteCheck()
.about(iterableEntries())
.that(checkNotNull(actual).entries())
.containsExactly();
}
|
Checks that the actual multimap is empty.
|
containsExactly
|
java
|
google/truth
|
core/src/main/java/com/google/common/truth/MultimapSubject.java
|
https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/MultimapSubject.java
|
Apache-2.0
|
@CanIgnoreReturnValue
public final Ordered containsExactly(
@Nullable Object k0, @Nullable Object v0, @Nullable Object... rest) {
return containsExactlyEntriesIn(accumulateMultimap(k0, v0, rest));
}
|
Checks that the actual multimap contains exactly the given set of key/value pairs.
<p><b>Warning:</b> the use of varargs means that we cannot guarantee an equal number of
key/value pairs at compile time. Please make sure you provide varargs in key/value pairs!
|
containsExactly
|
java
|
google/truth
|
core/src/main/java/com/google/common/truth/MultimapSubject.java
|
https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/MultimapSubject.java
|
Apache-2.0
|
@CanIgnoreReturnValue
public final Ordered containsAtLeast(
@Nullable Object k0, @Nullable Object v0, @Nullable Object... rest) {
return containsAtLeastEntriesIn(accumulateMultimap(k0, v0, rest));
}
|
Checks that the actual multimap contains at least the given key/value pairs.
<p><b>Warning:</b> the use of varargs means that we cannot guarantee an equal number of
key/value pairs at compile time. Please make sure you provide varargs in key/value pairs!
|
containsAtLeast
|
java
|
google/truth
|
core/src/main/java/com/google/common/truth/MultimapSubject.java
|
https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/MultimapSubject.java
|
Apache-2.0
|
private static ListMultimap<@Nullable Object, @Nullable Object> accumulateMultimap(
@Nullable Object k0, @Nullable Object v0, @Nullable Object... rest) {
checkArgument(
rest.length % 2 == 0,
"There must be an equal number of key/value pairs "
+ "(i.e., the number of key/value parameters (%s) must be even).",
rest.length + 2);
LinkedListMultimap<@Nullable Object, @Nullable Object> expectedMultimap =
LinkedListMultimap.create();
expectedMultimap.put(k0, v0);
for (int i = 0; i < rest.length; i += 2) {
expectedMultimap.put(rest[i], rest[i + 1]);
}
return expectedMultimap;
}
|
Checks that the actual multimap contains at least the given key/value pairs.
<p><b>Warning:</b> the use of varargs means that we cannot guarantee an equal number of
key/value pairs at compile time. Please make sure you provide varargs in key/value pairs!
|
accumulateMultimap
|
java
|
google/truth
|
core/src/main/java/com/google/common/truth/MultimapSubject.java
|
https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/MultimapSubject.java
|
Apache-2.0
|
private Factory<IterableSubject, Iterable<?>> iterableEntries() {
return (metadata, actual) ->
IterableEntries.create(metadata, checkNotNull(actual), MultimapSubject.this);
}
|
Checks that the actual multimap contains at least the given key/value pairs.
<p><b>Warning:</b> the use of varargs means that we cannot guarantee an equal number of
key/value pairs at compile time. Please make sure you provide varargs in key/value pairs!
|
iterableEntries
|
java
|
google/truth
|
core/src/main/java/com/google/common/truth/MultimapSubject.java
|
https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/MultimapSubject.java
|
Apache-2.0
|
@Override
protected String actualCustomStringRepresentation() {
// We want to use the multimap's toString() instead of the iterable of entries' toString():
return multimapSubject.actualCustomStringRepresentationForPackageMembersToCall();
}
|
Checks that the actual multimap contains at least the given key/value pairs.
<p><b>Warning:</b> the use of varargs means that we cannot guarantee an equal number of
key/value pairs at compile time. Please make sure you provide varargs in key/value pairs!
|
actualCustomStringRepresentation
|
java
|
google/truth
|
core/src/main/java/com/google/common/truth/MultimapSubject.java
|
https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/MultimapSubject.java
|
Apache-2.0
|
static IterableEntries create(
FailureMetadata metadata, Iterable<?> actual, MultimapSubject multimapSubject) {
return new IterableEntries(metadata, actual, multimapSubject);
}
|
Checks that the actual multimap contains at least the given key/value pairs.
<p><b>Warning:</b> the use of varargs means that we cannot guarantee an equal number of
key/value pairs at compile time. Please make sure you provide varargs in key/value pairs!
|
create
|
java
|
google/truth
|
core/src/main/java/com/google/common/truth/MultimapSubject.java
|
https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/MultimapSubject.java
|
Apache-2.0
|
@Override
public void inOrder() {
// We use the fact that Sets.intersection's result has the same order as the first parameter
checkNotNull(subject.actual);
@SuppressWarnings("nullness") // TODO: b/339070656: Remove suppression after fix.
boolean keysInOrder =
new ArrayList<>(Sets.intersection(subject.actual.keySet(), expectedMultimap.keySet()))
.equals(new ArrayList<>(expectedMultimap.keySet()));
LinkedHashSet<@Nullable Object> keysWithValuesOutOfOrder = new LinkedHashSet<>();
for (Object key : expectedMultimap.keySet()) {
List<?> actualVals = new ArrayList<@Nullable Object>(get(subject.actual, key));
List<?> expectedVals = new ArrayList<>(get(expectedMultimap, key));
Iterator<?> actualIterator = actualVals.iterator();
for (Object value : expectedVals) {
if (!advanceToFind(actualIterator, value)) {
boolean unused = keysWithValuesOutOfOrder.add(key);
break;
}
}
}
if (!keysInOrder) {
if (!keysWithValuesOutOfOrder.isEmpty()) {
subject.failWithActual(
simpleFact("contents match, but order was wrong"),
simpleFact("keys are not in order"),
fact("keys with out-of-order values", keysWithValuesOutOfOrder),
simpleFact("---"),
fact(
allowUnexpected ? "expected to contain at least" : "expected", expectedMultimap));
} else {
subject.failWithActual(
simpleFact("contents match, but order was wrong"),
simpleFact("keys are not in order"),
simpleFact("---"),
fact(
allowUnexpected ? "expected to contain at least" : "expected", expectedMultimap));
}
} else if (!keysWithValuesOutOfOrder.isEmpty()) {
subject.failWithActual(
simpleFact("contents match, but order was wrong"),
fact("keys with out-of-order values", keysWithValuesOutOfOrder),
simpleFact("---"),
fact(allowUnexpected ? "expected to contain at least" : "expected", expectedMultimap));
}
}
|
Checks whether entries in expected appear in the same order in actual.
<p>We allow for actual to have more items than the expected to support both {@link
#containsExactly} and {@link #containsAtLeast}.
|
inOrder
|
java
|
google/truth
|
core/src/main/java/com/google/common/truth/MultimapSubject.java
|
https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/MultimapSubject.java
|
Apache-2.0
|
static MultimapInOrder create(
MultimapSubject subject, boolean allowUnexpected, Multimap<?, ?> expectedMultimap) {
return new MultimapInOrder(subject, allowUnexpected, expectedMultimap);
}
|
Checks whether entries in expected appear in the same order in actual.
<p>We allow for actual to have more items than the expected to support both {@link
#containsExactly} and {@link #containsAtLeast}.
|
create
|
java
|
google/truth
|
core/src/main/java/com/google/common/truth/MultimapSubject.java
|
https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/MultimapSubject.java
|
Apache-2.0
|
private static boolean advanceToFind(Iterator<?> iterator, @Nullable Object value) {
while (iterator.hasNext()) {
if (Objects.equals(iterator.next(), value)) {
return true;
}
}
return false;
}
|
Advances the iterator until it either returns value, or has no more elements.
<p>Returns true if the value was found, false if the end was reached before finding it.
<p>This is basically the same as {@link com.google.common.collect.Iterables#contains}, but
where the contract explicitly states that the iterator isn't advanced beyond the value if the
value is found.
|
advanceToFind
|
java
|
google/truth
|
core/src/main/java/com/google/common/truth/MultimapSubject.java
|
https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/MultimapSubject.java
|
Apache-2.0
|
@SuppressWarnings("EmptyList") // ImmutableList doesn't support nullable types
private static <V extends @Nullable Object> Collection<V> get(
Multimap<?, V> multimap, @Nullable Object key) {
if (multimap.containsKey(key)) {
return checkNotNull(multimap.asMap().get(key));
} else {
return emptyList();
}
}
|
Advances the iterator until it either returns value, or has no more elements.
<p>Returns true if the value was found, false if the end was reached before finding it.
<p>This is basically the same as {@link com.google.common.collect.Iterables#contains}, but
where the contract explicitly states that the iterator isn't advanced beyond the value if the
value is found.
|
get
|
java
|
google/truth
|
core/src/main/java/com/google/common/truth/MultimapSubject.java
|
https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/MultimapSubject.java
|
Apache-2.0
|
private static ListMultimap<?, ?> difference(Multimap<?, ?> minuend, Multimap<?, ?> subtrahend) {
ListMultimap<@Nullable Object, @Nullable Object> difference = LinkedListMultimap.create();
for (Object key : minuend.keySet()) {
List<?> valDifference =
difference(new ArrayList<>(get(minuend, key)), new ArrayList<>(get(subtrahend, key)));
difference.putAll(key, valDifference);
}
return difference;
}
|
Advances the iterator until it either returns value, or has no more elements.
<p>Returns true if the value was found, false if the end was reached before finding it.
<p>This is basically the same as {@link com.google.common.collect.Iterables#contains}, but
where the contract explicitly states that the iterator isn't advanced beyond the value if the
value is found.
|
difference
|
java
|
google/truth
|
core/src/main/java/com/google/common/truth/MultimapSubject.java
|
https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/MultimapSubject.java
|
Apache-2.0
|
private static List<?> difference(List<?> minuend, List<?> subtrahend) {
LinkedHashMultiset<@Nullable Object> remaining =
LinkedHashMultiset.<@Nullable Object>create(subtrahend);
List<@Nullable Object> difference = new ArrayList<>();
for (Object elem : minuend) {
if (!remaining.remove(elem)) {
difference.add(elem);
}
}
return difference;
}
|
Advances the iterator until it either returns value, or has no more elements.
<p>Returns true if the value was found, false if the end was reached before finding it.
<p>This is basically the same as {@link com.google.common.collect.Iterables#contains}, but
where the contract explicitly states that the iterator isn't advanced beyond the value if the
value is found.
|
difference
|
java
|
google/truth
|
core/src/main/java/com/google/common/truth/MultimapSubject.java
|
https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/MultimapSubject.java
|
Apache-2.0
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.