Class IterableSubject
- Direct Known Subclasses:
MultimapSubject.IterableEntries,MultisetSubject,PrimitiveDoubleArraySubject.IterableSubjectWithInheritedToString,PrimitiveFloatArraySubject.IterableSubjectWithInheritedToString
Iterable subjects.
Note:
- Assertions may iterate through the given
Iterablemore than once. If you have an unusual implementation ofIterablewhich does not support multiple iterations (sometimes known as a "one-shot iterable"), you must copy your iterable into a collection which does (e.g.ImmutableList.copyOf(iterable)or, if your iterable may contain null,newArrayList(iterable)). If you don't, you may see surprising failures. - Assertions may also require that the elements in the given
IterableimplementObject.hashCode()correctly.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescription(package private) static enumWhether to output each missing/unexpected item as its ownFactor to group all those items together into a singleFact.private static interfacestatic classA partially specified check in which the actual elements (normally the elements of theIterableunder test) are compared to expected elements using aCorrespondence.Nested classes/interfaces inherited from class com.google.common.truth.Subject
Subject.EqualityCheck, Subject.Factory<SubjectT extends Subject,ActualT> -
Field Summary
Fields -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedIterableSubject(FailureMetadata metadata, Iterable<?> iterable) Constructor for use by subclasses. -
Method Summary
Modifier and TypeMethodDescriptionprotected StringSupplies the direct string representation of the actual value to other methods which may prefix or otherwise position it in an error message.private static booleananyContainsCommaOrNewline(Iterable<com.google.common.collect.Multiset.Entry<?>>... lists) <A,E> IterableSubject.UsingCorrespondence <A, E> comparingElementsUsing(Correspondence<? super A, ? super E> correspondence) Starts a method chain for a check in which the actual elements (i.e.final voidChecks (with a side-effect failure) that the subject contains the supplied item.final voidcontainsAnyIn(Iterable<?> expected) Checks that the subject contains at least one of the objects contained in the provided collection or fails.final voidcontainsAnyIn(Object[] expected) Checks that the subject contains at least one of the objects contained in the provided array or fails.final voidcontainsAnyOf(Object first, Object second, Object... rest) Checks that the subject contains at least one of the provided objects or fails.final OrderedcontainsAtLeast(Object firstExpected, Object secondExpected, Object... restOfExpected) Checks that the actual iterable contains at least all of the expected elements or fails.final OrderedcontainsAtLeastElementsIn(Iterable<?> expectedIterable) Checks that the actual iterable contains at least all of the expected elements or fails.final OrderedcontainsAtLeastElementsIn(Object[] expected) Checks that the actual iterable contains at least all of the expected elements or fails.private static booleancontainsEmptyOrLong(Iterable<com.google.common.collect.Multiset.Entry<?>> entries) final OrderedcontainsExactly(Object... varargs) Checks that a subject contains exactly the provided objects or fails.final OrderedcontainsExactlyElementsIn(Iterable<?> expected) Checks that a subject contains exactly the provided objects or fails.private OrderedcontainsExactlyElementsIn(Iterable<?> required, boolean addElementsInWarning) final OrderedcontainsExactlyElementsIn(Object[] expected) Checks that a subject contains exactly the provided objects or fails.final voidChecks that the subject does not contain duplicate elements.final voidcontainsNoneIn(Iterable<?> excluded) Checks that the actual iterable contains none of the elements contained in the excluded iterable or fails.final voidcontainsNoneIn(Object[] excluded) Checks that the actual iterable contains none of the elements contained in the excluded array or fails.final voidcontainsNoneOf(Object firstExcluded, Object secondExcluded, Object... restOfExcluded) Checks that a actual iterable contains none of the excluded objects or fails.final voiddoesNotContain(Object element) Checks (with a side-effect failure) that the subject does not contain the supplied item.private OrderedfailAtLeast(Collection<?> expected, Collection<?> missingRawObjects) private OrderedfailExactly(Iterable<?> required, boolean addElementsInWarning, Collection<?> missingRawObjects, Collection<?> extraRawObjects) private Factprivate static booleanhasMultiple(Iterable<com.google.common.collect.Multiset.Entry<?>> entries) final voidhasSize(int expectedSize) Fails if the subject does not have the given size.final voidisEmpty()Fails if the subject is not empty.voidFails if the subject is not equal to the given object.voidFails if the iterable is not ordered, according to the natural ordering of its elements.final voidisInOrder(Comparator<?> comparator) Fails if the iterable is not ordered, according to the given comparator.voidFails if the iterable is not strictly ordered, according to the natural ordering of its elements.final voidisInStrictOrder(Comparator<?> comparator) Fails if the iterable is not strictly ordered, according to the given comparator.voidDeprecated.final voidFails if the subject is empty.voidDeprecated.You probably meant to callcontainsNoneIn(java.lang.Iterable<?>)instead.private static StringkeyToGoWithElementsString(String label, SubjectUtils.DuplicateGroupedAndTyped elements) private static StringkeyToServeAsHeader(String label, SubjectUtils.DuplicateGroupedAndTyped elements) private static com.google.common.collect.ImmutableList<Fact> makeElementFacts(String label, SubjectUtils.DuplicateGroupedAndTyped elements, IterableSubject.ElementFactGrouping grouping) Returns a list of facts (zero, one, or many, depending on the number of elements and the grouping policy) describing the given missing, unexpected, or near-miss elements.private static com.google.common.collect.ImmutableList<Fact> makeElementFactsForBoth(String firstKey, Collection<?> firstCollection, String secondKey, Collection<?> secondCollection) private static voidmoveElements(List<?> input, Collection<Object> output, int maxElements) Removes at most the given number of available elements from the input list and adds them to the given output collection.private static StringnumberString(int n, int count) private voidpairwiseCheck(String expectedFact, IterableSubject.PairwiseChecker checker) private static IterableSubject.ElementFactGroupingpickGrouping(Iterable<com.google.common.collect.Multiset.Entry<?>> first, Iterable<com.google.common.collect.Multiset.Entry<?>> second) Methods inherited from class com.google.common.truth.Subject
actual, actualCustomStringRepresentationForPackageMembersToCall, butWas, check, check, checkNoNeedToDisplayBothValues, equals, fail, fail, fail, failEqualityCheckForEqualsWithoutDescription, failWithActual, failWithActual, failWithActual, failWithActual, failWithBadResults, failWithCustomSubject, failWithoutActual, failWithoutActual, failWithoutActual, failWithoutActual, failWithoutSubject, hashCode, ignoreCheck, isAnyOf, isIn, isInstanceOf, isNotEqualTo, isNotInstanceOf, isNotNull, isNotSameInstanceAs, isNull, isSameInstanceAs, toString, typeDescription
-
Field Details
-
actual
-
IN_ORDER
Ordered implementation that does nothing because it's already known to be true. -
ALREADY_FAILED
Ordered implementation that does nothing because an earlier check already caused a failure.
-
-
Constructor Details
-
IterableSubject
Constructor for use by subclasses. If you want to create an instance of this class itself, callcheck(...).that(actual).
-
-
Method Details
-
actualCustomStringRepresentation
Description copied from class:SubjectSupplies the direct string representation of the actual value to other methods which may prefix or otherwise position it in an error message. This should only be overridden to provide an improved string representation of the value under test, as it would appear in any given error message, and should not be used for additional prefixing.Subjects should override this with care.
By default, this returns
String.ValueOf(getActualValue()).- Overrides:
actualCustomStringRepresentationin classSubject
-
isEqualTo
Description copied from class:SubjectFails if the subject is not equal to the given object. For the purposes of this comparison, two objects are equal if any of the following is true:- they are equal according to
Objects.equal(java.lang.Object, java.lang.Object) - they are arrays and are considered equal by the appropriate
Arrays.equals(long[], long[])overload - they are boxed integer types (
Byte,Short,Character,Integer, orLong) and they are numerically equal when converted toLong. - the actual value is a boxed floating-point type (
DoubleorFloat), the expected value is anInteger, and the two are numerically equal when converted toDouble. (This allowsassertThat(someDouble).isEqualTo(0)to pass.)
Note: This method does not test the
Object.equals(java.lang.Object)implementation itself; it assumes that method is functioning correctly according to its contract. Testing anequalsimplementation requires a utility such as guava-testlib's EqualsTester.In some cases, this method might not even call
equals. It may instead perform other tests that will return the same result as long asequalsis implemented according to the contract for its type. - they are equal according to
-
isEmpty
public final void isEmpty()Fails if the subject is not empty. -
isNotEmpty
public final void isNotEmpty()Fails if the subject is empty. -
hasSize
public final void hasSize(int expectedSize) Fails if the subject does not have the given size. -
contains
Checks (with a side-effect failure) that the subject contains the supplied item. -
doesNotContain
Checks (with a side-effect failure) that the subject does not contain the supplied item. -
containsNoDuplicates
public final void containsNoDuplicates()Checks that the subject does not contain duplicate elements. -
containsAnyOf
Checks that the subject contains at least one of the provided objects or fails. -
containsAnyIn
Checks that the subject contains at least one of the objects contained in the provided collection or fails. -
containsAnyIn
Checks that the subject contains at least one of the objects contained in the provided array or fails. -
containsAtLeast
public final Ordered containsAtLeast(Object firstExpected, Object secondExpected, Object... restOfExpected) Checks that the actual iterable contains at least all of the expected elements or fails. If an element appears more than once in the expected elements to this call then it must appear at least that number of times in the actual elements.To also test that the contents appear in the given order, make a call to
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
Checks that the actual iterable contains at least all of the expected elements or fails. If an element appears more than once in the expected elements then it must appear at least that number of times in the actual elements.To also test that the contents appear in the given order, make a call to
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
Checks that the actual iterable contains at least all of the expected elements or fails. If an element appears more than once in the expected elements then it must appear at least that number of times in the actual elements.To also test that the contents appear in the given order, make a call to
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. -
failAtLeast
-
moveElements
Removes at most the given number of available elements from the input list and adds them to the given output collection. -
containsExactly
Checks that a subject contains exactly the provided objects or fails.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 subject.
To also test that the contents appear in the given order, make a call to
inOrder()on the object returned by this method.To test that the iterable contains the same elements as an array, prefer
containsExactlyElementsIn(Object[]). It makes clear that the given array is a list of elements, not an element itself. This helps human readers and avoids a compiler warning. -
containsExactlyElementsIn
Checks that a subject contains exactly the provided objects or fails.Multiplicity is respected. For example, an object duplicated exactly 3 times in the
Iterableparameter asserts that the object must likewise be duplicated exactly 3 times in the subject.To also test that the contents appear in the given order, make a call to
inOrder()on the object returned by this method. -
containsExactlyElementsIn
Checks that a subject contains exactly the provided objects or fails.Multiplicity is respected. For example, an object duplicated exactly 3 times in the array parameter asserts that the object must likewise be duplicated exactly 3 times in the subject.
To also test that the contents appear in the given order, make a call to
inOrder()on the object returned by this method. -
containsExactlyElementsIn
-
failExactly
private Ordered failExactly(Iterable<?> required, boolean addElementsInWarning, Collection<?> missingRawObjects, Collection<?> extraRawObjects) -
makeElementFactsForBoth
private static com.google.common.collect.ImmutableList<Fact> makeElementFactsForBoth(String firstKey, Collection<?> firstCollection, String secondKey, Collection<?> secondCollection) -
makeElementFacts
private static com.google.common.collect.ImmutableList<Fact> makeElementFacts(String label, SubjectUtils.DuplicateGroupedAndTyped elements, IterableSubject.ElementFactGrouping grouping) Returns a list of facts (zero, one, or many, depending on the number of elements and the grouping policy) describing the given missing, unexpected, or near-miss elements. -
keyToGoWithElementsString
private static String keyToGoWithElementsString(String label, SubjectUtils.DuplicateGroupedAndTyped elements) -
keyToServeAsHeader
private static String keyToServeAsHeader(String label, SubjectUtils.DuplicateGroupedAndTyped elements) -
numberString
-
pickGrouping
private static IterableSubject.ElementFactGrouping pickGrouping(Iterable<com.google.common.collect.Multiset.Entry<?>> first, Iterable<com.google.common.collect.Multiset.Entry<?>> second) -
anyContainsCommaOrNewline
private static boolean anyContainsCommaOrNewline(Iterable<com.google.common.collect.Multiset.Entry<?>>... lists) -
hasMultiple
-
containsEmptyOrLong
private static boolean containsEmptyOrLong(Iterable<com.google.common.collect.Multiset.Entry<?>> entries) -
containsNoneOf
public final void containsNoneOf(Object firstExcluded, Object secondExcluded, Object... restOfExcluded) Checks that a actual iterable contains none of the excluded objects or fails. (Duplicates are irrelevant to this test, which fails if any of the actual elements equal any of the excluded.) -
containsNoneIn
Checks that the actual iterable contains none of the elements contained in the excluded iterable or fails. (Duplicates are irrelevant to this test, which fails if any of the actual elements equal any of the excluded.) -
containsNoneIn
Checks that the actual iterable contains none of the elements contained in the excluded array or fails. (Duplicates are irrelevant to this test, which fails if any of the actual elements equal any of the excluded.) -
isInStrictOrder
public void isInStrictOrder()Fails if the iterable is not strictly ordered, according to the natural ordering of its elements. Strictly ordered means that each element in the iterable is strictly greater than the element that preceded it.- Throws:
ClassCastException- if any pair of elements is not mutually ComparableNullPointerException- if any element is null
-
isInStrictOrder
Fails if the iterable is not strictly ordered, according to the given comparator. Strictly ordered means that each element in the iterable is strictly greater than the element that preceded it.- Throws:
ClassCastException- if any pair of elements is not mutually Comparable
-
isInOrder
public void isInOrder()Fails if the iterable is not ordered, according to the natural ordering of its elements. Ordered means that each element in the iterable is greater than or equal to the element that preceded it.- Throws:
ClassCastException- if any pair of elements is not mutually ComparableNullPointerException- if any element is null
-
isInOrder
Fails if the iterable is not ordered, according to the given comparator. Ordered means that each element in the iterable is greater than or equal to the element that preceded it.- Throws:
ClassCastException- if any pair of elements is not mutually Comparable
-
pairwiseCheck
-
isNoneOf
Deprecated.You probably meant to callcontainsNoneOf(java.lang.Object, java.lang.Object, java.lang.Object...)instead.Description copied from class:SubjectFails if the subject is equal to any of the given elements. -
isNotIn
Deprecated.You probably meant to callcontainsNoneIn(java.lang.Iterable<?>)instead.Description copied from class:SubjectFails if the subject is equal to any element in the given iterable. -
fullContents
-
comparingElementsUsing
public <A,E> IterableSubject.UsingCorrespondence<A,E> comparingElementsUsing(Correspondence<? super A, ? super E> correspondence) Starts a method chain for a check in which the actual elements (i.e. the elements of theIterableunder test) are compared to expected elements using the givenCorrespondence. The actual elements must be of typeA, the expected elements must be of typeE. The check is actually executed by continuing the method chain. For example:
whereassertThat(actualIterable).comparingElementsUsing(correspondence).contains(expected);actualIterableis anIterable<A>(or, more generally, anIterable<? extends A>),correspondenceis aCorrespondence<A, E>, andexpectedis anE.Any of the methods on the returned object may throw
ClassCastExceptionif they encounter an actual element that is not of typeA.
-
containsNoneOf(java.lang.Object, java.lang.Object, java.lang.Object...)instead.