Class IterableSubject
- java.lang.Object
-
- com.google.common.truth.Subject
-
- com.google.common.truth.IterableSubject
-
- Direct Known Subclasses:
IterableOfProtosSubject,MultisetSubject
public class IterableSubject extends Subject
A subject forIterablevalues.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 Classes Modifier and Type Class Description static classIterableSubject.UsingCorrespondence<A extends @Nullable Object,E extends @Nullable Object>A 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.Factory<SubjectT extends Subject,ActualT>
-
-
Constructor Summary
Constructors Modifier Constructor Description protectedIterableSubject(FailureMetadata metadata, @Nullable Iterable<?> actual)The constructor is for use by subclasses only.
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description protected StringactualCustomStringRepresentation()Returns a string representation of the actual value for inclusion in failure messages.<A extends @Nullable Object,E extends @Nullable Object>
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.voidcontains(@Nullable Object element)Checks that the actual iterable contains the supplied item.voidcontainsAnyIn(@Nullable Iterable<?> expected)Checks that the actual iterable contains at least one of the objects contained in the provided collection.voidcontainsAnyIn(@Nullable Object @Nullable [] expected)Checks that the actual iterable contains at least one of the objects contained in the provided array.voidcontainsAnyOf(@Nullable Object first, @Nullable Object second, @Nullable Object @Nullable ... rest)Checks that the actual iterable contains at least one of the provided objects.OrderedcontainsAtLeast(@Nullable Object first, @Nullable Object second, @Nullable Object @Nullable ... rest)Checks that the actual iterable contains at least all the expected elements.OrderedcontainsAtLeastElementsIn(@Nullable Iterable<?> expected)Checks that the actual iterable contains at least all the expected elements.OrderedcontainsAtLeastElementsIn(@Nullable Object @Nullable [] expected)Checks that the actual iterable contains at least all the expected elements.OrderedcontainsExactly(@Nullable Object @Nullable ... expected)Checks that the actual iterable contains exactly the provided objects.OrderedcontainsExactlyElementsIn(@Nullable Iterable<?> expected)Checks that the actual iterable contains exactly the provided objects.OrderedcontainsExactlyElementsIn(@Nullable Object @Nullable [] expected)Checks that the actual iterable contains exactly the provided objects.voidcontainsNoDuplicates()Checks that the actual iterable does not contain duplicate elements.voidcontainsNoneIn(@Nullable Iterable<?> excluded)Checks that the actual iterable contains none of the elements contained in the excluded iterable.voidcontainsNoneIn(@Nullable Object @Nullable [] excluded)Checks that the actual iterable contains none of the elements contained in the excluded array.voidcontainsNoneOf(@Nullable Object first, @Nullable Object second, @Nullable Object @Nullable ... rest)Checks that the actual iterable contains none of the excluded objects.voiddoesNotContain(@Nullable Object element)Checks that the actual iterable does not contain the supplied item.<T extends @Nullable Object>
IterableSubject.UsingCorrespondence<T,T>formattingDiffsUsing(Correspondence.DiffFormatter<? super T,? super T> formatter)Starts a method chain for a check in which failure messages may use the givenCorrespondence.DiffFormatterto describe the difference between an actual element (i.e. an element of theIterableunder test) and the element it is expected to be equal to, but isn't.voidhasSize(int size)Checks that the actual iterable has the given size.voidisEmpty()Checks that the actual iterable is empty.voidisEqualTo(@Nullable Object expected)Checks that the value under test is equal to the given object.voidisInOrder()Checks that the actual iterable is ordered, according to the natural ordering of its elements.voidisInOrder(@Nullable Comparator<?> comparator)Checks that the actual iterable is ordered, according to the given comparator.voidisInStrictOrder()Checks that the actual iterable is strictly ordered, according to the natural ordering of its elements.voidisInStrictOrder(@Nullable Comparator<?> comparator)Checks that the actual iterable is strictly ordered, according to the given comparator.voidisNoneOf(@Nullable Object first, @Nullable Object second, @Nullable Object @Nullable ... rest)Deprecated.You probably meant to callcontainsNoneOf(java.lang.Object, java.lang.Object, java.lang.Object...)instead.voidisNotEmpty()Checks that the actual iterable is not empty.voidisNotIn(@Nullable Iterable<?> iterable)Deprecated.You probably meant to callcontainsNoneIn(java.lang.Iterable<?>)instead.-
Methods inherited from class com.google.common.truth.Subject
check, equals, failWithActual, failWithActual, failWithoutActual, hashCode, ignoreCheck, isAnyOf, isIn, isInstanceOf, isNotEqualTo, isNotInstanceOf, isNotNull, isNotSameInstanceAs, isNull, isSameInstanceAs, toString
-
-
-
-
Constructor Detail
-
IterableSubject
protected IterableSubject(FailureMetadata metadata, @Nullable Iterable<?> actual)
The constructor is for use by subclasses only. If you want to create an instance of this class itself, callcheck(...).that(actual).
-
-
Method Detail
-
actualCustomStringRepresentation
protected String actualCustomStringRepresentation()
Description copied from class:SubjectReturns a string representation of the actual value for inclusion in failure messages.Subjects should override this with care.
By default, this method returns
String.valueOf(getActualValue())for most types. It does have some special logic for a few cases, like arrays.- Overrides:
actualCustomStringRepresentationin classSubject
-
isEqualTo
public void isEqualTo(@Nullable Object expected)
Description copied from class:SubjectChecks that the value under test is 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.equals(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()
Checks that the actual iterable is empty.
-
isNotEmpty
public final void isNotEmpty()
Checks that the actual iterable is not empty.
-
hasSize
public final void hasSize(int size)
Checks that the actual iterable has the given size.
-
contains
public final void contains(@Nullable Object element)
Checks that the actual iterable contains the supplied item.
-
doesNotContain
public final void doesNotContain(@Nullable Object element)
Checks that the actual iterable does not contain the supplied item.
-
containsNoDuplicates
public final void containsNoDuplicates()
Checks that the actual iterable does not contain duplicate elements.
-
containsAnyOf
public final void containsAnyOf(@Nullable Object first, @Nullable Object second, @Nullable Object @Nullable ... rest)
Checks that the actual iterable contains at least one of the provided objects.
-
containsAnyIn
public final void containsAnyIn(@Nullable Iterable<?> expected)
Checks that the actual iterable contains at least one of the objects contained in the provided collection.
-
containsAnyIn
public final void containsAnyIn(@Nullable Object @Nullable [] expected)
Checks that the actual iterable contains at least one of the objects contained in the provided array.
-
containsAtLeast
@CanIgnoreReturnValue public final Ordered containsAtLeast(@Nullable Object first, @Nullable Object second, @Nullable Object @Nullable ... rest)
Checks that the actual iterable contains at least all the expected elements. 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
@CanIgnoreReturnValue public final Ordered containsAtLeastElementsIn(@Nullable Iterable<?> expected)
Checks that the actual iterable contains at least all the expected elements. 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
@CanIgnoreReturnValue public final Ordered containsAtLeastElementsIn(@Nullable Object @Nullable [] expected)
Checks that the actual iterable contains at least all the expected elements. 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.
-
containsExactly
@CanIgnoreReturnValue public final Ordered containsExactly(@Nullable Object @Nullable ... expected)
Checks that the actual iterable contains exactly the provided objects.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 iterable.
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
@CanIgnoreReturnValue public final Ordered containsExactlyElementsIn(@Nullable Iterable<?> expected)
Checks that the actual iterable contains exactly the provided objects.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 actual iterable.To also test that the contents appear in the given order, make a call to
inOrder()on the object returned by this method.
-
containsExactlyElementsIn
@CanIgnoreReturnValue public final Ordered containsExactlyElementsIn(@Nullable Object @Nullable [] expected)
Checks that the actual iterable contains exactly the provided objects.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 actual iterable.
To also test that the contents appear in the given order, make a call to
inOrder()on the object returned by this method.
-
containsNoneOf
public final void containsNoneOf(@Nullable Object first, @Nullable Object second, @Nullable Object @Nullable ... rest)
Checks that the actual iterable contains none of the excluded objects.
-
containsNoneIn
public final void containsNoneIn(@Nullable Iterable<?> excluded)
Checks that the actual iterable contains none of the elements contained in the excluded iterable.
-
containsNoneIn
public final void containsNoneIn(@Nullable Object @Nullable [] excluded)
Checks that the actual iterable contains none of the elements contained in the excluded array.
-
isInStrictOrder
public void isInStrictOrder()
Checks that the actual iterable is 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
public final void isInStrictOrder(@Nullable Comparator<?> comparator)
Checks that the actual iterable is 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()
Checks that the actual iterable is 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
public final void isInOrder(@Nullable Comparator<?> comparator)
Checks that the actual iterable is 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
-
isNoneOf
@Deprecated public void isNoneOf(@Nullable Object first, @Nullable Object second, @Nullable Object @Nullable ... rest)
Deprecated.You probably meant to callcontainsNoneOf(java.lang.Object, java.lang.Object, java.lang.Object...)instead.Description copied from class:SubjectChecks that the value under test is not equal to any of the given elements.
-
isNotIn
@Deprecated public void isNotIn(@Nullable Iterable<?> iterable)
Deprecated.You probably meant to callcontainsNoneIn(java.lang.Iterable<?>)instead.Description copied from class:SubjectChecks that the value under test is not equal to any element in the given iterable.
-
comparingElementsUsing
public <A extends @Nullable Object,E extends @Nullable Object> 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.
-
formattingDiffsUsing
public <T extends @Nullable Object> IterableSubject.UsingCorrespondence<T,T> formattingDiffsUsing(Correspondence.DiffFormatter<? super T,? super T> formatter)
Starts a method chain for a check in which failure messages may use the givenCorrespondence.DiffFormatterto describe the difference between an actual element (i.e. an element of theIterableunder test) and the element it is expected to be equal to, but isn't. The actual and expected elements must be of typeT. The check is actually executed by continuing the method chain. You may well want to useIterableSubject.UsingCorrespondence.displayingDiffsPairedBy(com.google.common.base.Function<? super E, ?>)to specify how the elements should be paired up for diffing. For example:
whereassertThat(actualFoos) .formattingDiffsUsing(FooTestHelper::formatDiff) .displayingDiffsPairedBy(Foo::getId) .containsExactly(foo1, foo2, foo3);actualFoosis anIterable<Foo>,FooTestHelper.formatDiffis a static method taking twoFooarguments and returning aString,Foo.getIdis a no-arg instance method returning some kind of ID, andfoo1, {code foo2}, andfoo3areFooinstances.Unlike when using
comparingElementsUsing(com.google.common.truth.Correspondence<? super A, ? super E>), the elements are still compared using object equality, so this method does not affect whether a test passes or fails.Any of the methods on the returned object may throw
ClassCastExceptionif they encounter an actual element that is not of typeT.- Since:
- 1.1
-
-