Class Iterables


  • public class Iterables
    extends java.lang.Object
    Reusable assertions for Iterables.
    Author:
    Alex Ruiz, Yvonne Wang, Maciej Jaskowski, Nicolas François, Joel Costigliola
    • Method Detail

      • getComparator

        public java.util.Comparator<?> getComparator()
      • assertNullOrEmpty

        public void assertNullOrEmpty​(AssertionInfo info,
                                      java.lang.Iterable<?> actual)
        Asserts that the given Iterable is null or empty.
        Parameters:
        info - contains information about the assertion.
        actual - the given Iterable.
        Throws:
        java.lang.AssertionError - if the given Iterable is not null *and* contains one or more elements.
      • assertEmpty

        public void assertEmpty​(AssertionInfo info,
                                java.lang.Iterable<?> actual)
        Asserts that the given Iterable is empty.
        Parameters:
        info - contains information about the assertion.
        actual - the given Iterable.
        Throws:
        java.lang.AssertionError - if the given Iterable is null.
        java.lang.AssertionError - if the given Iterable is not empty.
      • assertNotEmpty

        public void assertNotEmpty​(AssertionInfo info,
                                   java.lang.Iterable<?> actual)
        Asserts that the given Iterable is not empty.
        Parameters:
        info - contains information about the assertion.
        actual - the given Iterable.
        Throws:
        java.lang.AssertionError - if the given Iterable is null.
        java.lang.AssertionError - if the given Iterable is empty.
      • assertHasSize

        public void assertHasSize​(AssertionInfo info,
                                  java.lang.Iterable<?> actual,
                                  int expectedSize)
        Asserts that the number of elements in the given Iterable is equal to the expected one.
        Parameters:
        info - contains information about the assertion.
        actual - the given Iterable.
        expectedSize - the expected size of actual.
        Throws:
        java.lang.AssertionError - if the given Iterable is null.
        java.lang.AssertionError - if the number of elements in the given Iterable is different than the expected one.
      • assertHasSameSizeAs

        public void assertHasSameSizeAs​(AssertionInfo info,
                                        java.lang.Iterable<?> actual,
                                        java.lang.Object[] other)
        Assert that the actual Iterable has the same size as the other array.
        Parameters:
        info - contains information about the assertion.
        actual - the given Iterable.
        other - the given array to compare.
        Throws:
        java.lang.AssertionError - if the actual group is null.
        java.lang.AssertionError - if the other group is null.
        java.lang.AssertionError - if actual Iterable and other array don't have the same size.
      • assertHasSameSizeAs

        public void assertHasSameSizeAs​(AssertionInfo info,
                                        java.lang.Iterable<?> actual,
                                        java.lang.Iterable<?> other)
        Assert that the actual Iterable has the same size as the other Iterable.
        Parameters:
        info - contains information about the assertion.
        actual - the given Iterable.
        other - the given Iterable.
        Throws:
        java.lang.AssertionError - if the actual group is null.
        java.lang.AssertionError - if the other group is null.
        java.lang.AssertionError - if actual and other Iterable don't have the same size.
      • assertContains

        public void assertContains​(AssertionInfo info,
                                   java.lang.Iterable<?> actual,
                                   java.lang.Object[] values)
        Asserts that the given Iterable contains the given values, in any order.
        Parameters:
        info - contains information about the assertion.
        actual - the given Iterable.
        values - the values that are expected to be in the given Iterable.
        Throws:
        java.lang.NullPointerException - if the array of values is null.
        java.lang.IllegalArgumentException - if the array of values is empty.
        java.lang.AssertionError - if the given Iterable is null.
        java.lang.AssertionError - if the given Iterable does not contain the given values.
      • assertContainsOnly

        public void assertContainsOnly​(AssertionInfo info,
                                       java.lang.Iterable<?> actual,
                                       java.lang.Object[] values)
        Asserts that the given Iterable contains only the given values and nothing else, in any order.
        Parameters:
        info - contains information about the assertion.
        actual - the given Iterable.
        values - the values that are expected to be in the given Iterable.
        Throws:
        java.lang.NullPointerException - if the array of values is null.
        java.lang.IllegalArgumentException - if the array of values is empty.
        java.lang.AssertionError - if the given Iterable is null.
        java.lang.AssertionError - if the given Iterable does not contain the given values or if the given Iterable contains values that are not in the given array.
      • assertContainsSequence

        public void assertContainsSequence​(AssertionInfo info,
                                           java.lang.Iterable<?> actual,
                                           java.lang.Object[] sequence)
        Verifies that the given Iterable contains the given sequence of objects, without any other objects between them.
        Parameters:
        info - contains information about the assertion.
        actual - the given Iterable.
        sequence - the sequence of objects to look for.
        Throws:
        java.lang.AssertionError - if the given Iterable is null.
        java.lang.NullPointerException - if the given sequence is null.
        java.lang.IllegalArgumentException - if the given sequence is empty.
        java.lang.AssertionError - if the given Iterable does not contain the given sequence of objects.
      • assertIsSubsetOf

        public void assertIsSubsetOf​(AssertionInfo info,
                                     java.lang.Iterable<?> actual,
                                     java.lang.Iterable<?> values)
        Verifies that the actual Iterable is a subset of values Iterable.
        Both actual and given iterable are treated as sets, therefore duplicates on either of them are ignored.
        Parameters:
        info - contains information about the assertion.
        actual - the actual Iterable.
        values - the Iterable that should contain all actual elements.
        Throws:
        java.lang.AssertionError - if the actual Iterable is null.
        java.lang.NullPointerException - if the given Iterable is null.
        java.lang.AssertionError - if the actual Iterable is not subset of set Iterable
      • assertDoesNotContain

        public void assertDoesNotContain​(AssertionInfo info,
                                         java.lang.Iterable<?> actual,
                                         java.lang.Object[] values)
        Asserts that the given Iterable does not contain the given values.
        Parameters:
        info - contains information about the assertion.
        actual - the given Iterable.
        values - the values that are expected not to be in the given Iterable.
        Throws:
        java.lang.NullPointerException - if the array of values is null.
        java.lang.IllegalArgumentException - if the array of values is empty.
        java.lang.AssertionError - if the given Iterable is null.
        java.lang.AssertionError - if the given Iterable contains any of given values.
      • assertDoesNotHaveDuplicates

        public void assertDoesNotHaveDuplicates​(AssertionInfo info,
                                                java.lang.Iterable<?> actual)
        Asserts that the given Iterable does not have duplicate values.
        Parameters:
        info - contains information about the assertion.
        actual - the given Iterable.
        Throws:
        java.lang.NullPointerException - if the array of values is null.
        java.lang.IllegalArgumentException - if the array of values is empty.
        java.lang.AssertionError - if the given Iterable is null.
        java.lang.AssertionError - if the given Iterable contains duplicate values.
      • assertStartsWith

        public void assertStartsWith​(AssertionInfo info,
                                     java.lang.Iterable<?> actual,
                                     java.lang.Object[] sequence)
        Verifies that the given Iterable starts with the given sequence of objects, without any other objects between them. Similar to assertContainsSequence(AssertionInfo, Iterable, Object[]), but it also verifies that the first element in the sequence is also the first element of the given Iterable.
        Parameters:
        info - contains information about the assertion.
        actual - the given Iterable.
        sequence - the sequence of objects to look for.
        Throws:
        java.lang.NullPointerException - if the given argument is null.
        java.lang.IllegalArgumentException - if the given argument is an empty array.
        java.lang.AssertionError - if the given Iterable is null.
        java.lang.AssertionError - if the given Iterable does not start with the given sequence of objects.
      • assertEndsWith

        public void assertEndsWith​(AssertionInfo info,
                                   java.lang.Iterable<?> actual,
                                   java.lang.Object[] sequence)
        Verifies that the given Iterable ends with the given sequence of objects, without any other objects between them. Similar to assertContainsSequence(AssertionInfo, Iterable, Object[]), but it also verifies that the last element in the sequence is also the last element of the given Iterable.
        Parameters:
        info - contains information about the assertion.
        actual - the given Iterable.
        sequence - the sequence of objects to look for.
        Throws:
        java.lang.NullPointerException - if the given argument is null.
        java.lang.IllegalArgumentException - if the given argument is an empty array.
        java.lang.AssertionError - if the given Iterable is null.
        java.lang.AssertionError - if the given Iterable does not end with the given sequence of objects.
      • assertContainsNull

        public void assertContainsNull​(AssertionInfo info,
                                       java.lang.Iterable<?> actual)
        Asserts that the given Iterable contains at least a null element.
        Parameters:
        info - contains information about the assertion.
        actual - the given Iterable.
        Throws:
        java.lang.AssertionError - if the given Iterable is null.
        java.lang.AssertionError - if the given Iterable does not contain at least a null element.
      • assertDoesNotContainNull

        public void assertDoesNotContainNull​(AssertionInfo info,
                                             java.lang.Iterable<?> actual)
        Asserts that the given Iterable does not contain null elements.
        Parameters:
        info - contains information about the assertion.
        actual - the given Iterable.
        Throws:
        java.lang.AssertionError - if the given Iterable is null.
        java.lang.AssertionError - if the given Iterable contains a null element.
      • assertAre

        public <E> void assertAre​(AssertionInfo info,
                                  java.lang.Iterable<? extends E> actual,
                                  Condition<? super E> condition)
        Assert that each element of given Iterable satisfies the given condition.
        Parameters:
        info - contains information about the assertion.
        actual - the given Iterable.
        condition - the given Condition.
        Throws:
        java.lang.NullPointerException - if the given condition is null.
        java.lang.AssertionError - if a element cannot be cast to E.
        java.lang.AssertionError - if one or more element not satisfy the given condition.
      • assertAreNot

        public <E> void assertAreNot​(AssertionInfo info,
                                     java.lang.Iterable<? extends E> actual,
                                     Condition<? super E> condition)
        Assert that each element of given Iterable not satisfies the given condition.
        Parameters:
        info - contains information about the assertion.
        actual - the given Iterable.
        condition - the given Condition.
        Throws:
        java.lang.NullPointerException - if the given condition is null.
        java.lang.AssertionError - if a element cannot be cast to E.
        java.lang.AssertionError - if one or more element satisfy the given condition.
      • assertHave

        public <E> void assertHave​(AssertionInfo info,
                                   java.lang.Iterable<? extends E> actual,
                                   Condition<? super E> condition)
        Assert that each element of given Iterable satisfies the given condition.
        Parameters:
        info - contains information about the assertion.
        actual - the given Iterable.
        condition - the given Condition.
        Throws:
        java.lang.NullPointerException - if the given condition is null.
        java.lang.AssertionError - if a element cannot be cast to E.
        java.lang.AssertionError - if one or more element not satisfy the given condition.
      • assertDoNotHave

        public <E> void assertDoNotHave​(AssertionInfo info,
                                        java.lang.Iterable<? extends E> actual,
                                        Condition<? super E> condition)
        Assert that each element of given Iterable not satisfies the given condition.
        Parameters:
        info - contains information about the assertion.
        actual - the given Iterable.
        condition - the given Condition.
        Throws:
        java.lang.NullPointerException - if the given condition is null.
        java.lang.AssertionError - if a element cannot be cast to E.
        java.lang.AssertionError - if one or more element satisfy the given condition.
      • assertAreAtLeast

        public <E> void assertAreAtLeast​(AssertionInfo info,
                                         java.lang.Iterable<? extends E> actual,
                                         int n,
                                         Condition<? super E> condition)
        Assert that there is at least n elements in the actual Iterable satisfying the given condition.
        Parameters:
        info - contains information about the assertion.
        actual - the given Iterable.
        n - the minimum number of times the condition should be verified.
        condition - the given Condition.
        Throws:
        java.lang.NullPointerException - if the given condition is null.
        java.lang.AssertionError - if a element cannot be cast to E.
        java.lang.AssertionError - if the number of elements satisfying the given condition is < n.
      • assertAreNotAtLeast

        public <E> void assertAreNotAtLeast​(AssertionInfo info,
                                            java.lang.Iterable<? extends E> actual,
                                            int n,
                                            Condition<? super E> condition)
        Assert that there is at least n elements in the actual Iterable not satisfying the given condition.
        Parameters:
        info - contains information about the assertion.
        actual - the given Iterable.
        n - the number of times the condition should not be verified at least.
        condition - the given Condition.
        Throws:
        java.lang.NullPointerException - if the given condition is null.
        java.lang.AssertionError - if a element cannot be cast to E.
        java.lang.AssertionError - if the number of elements not satisfying the given condition is < n.
      • assertAreAtMost

        public <E> void assertAreAtMost​(AssertionInfo info,
                                        java.lang.Iterable<? extends E> actual,
                                        int n,
                                        Condition<? super E> condition)
        Assert that there is at most n elements in the actual Iterable satisfying the given condition.
        Parameters:
        info - contains information about the assertion.
        actual - the given Iterable.
        n - the number of times the condition should be at most verified.
        condition - the given Condition.
        Throws:
        java.lang.NullPointerException - if the given condition is null.
        java.lang.AssertionError - if a element cannot be cast to E.
        java.lang.AssertionError - if the number of elements satisfying the given condition is > n.
      • assertAreNotAtMost

        public <E> void assertAreNotAtMost​(AssertionInfo info,
                                           java.lang.Iterable<? extends E> actual,
                                           int n,
                                           Condition<? super E> condition)
        Verifies that there is at most n elements in the actual Iterable not satisfying the given condition.
        Parameters:
        info - contains information about the assertion.
        actual - the given Iterable.
        n - the number of times the condition should not be verified at most.
        condition - the given Condition.
        Throws:
        java.lang.NullPointerException - if the given condition is null.
        java.lang.AssertionError - if a element cannot be cast to E.
        java.lang.AssertionError - if the number of elements not satisfying the given condition is > n.
      • assertAreExactly

        public <E> void assertAreExactly​(AssertionInfo info,
                                         java.lang.Iterable<? extends E> actual,
                                         int n,
                                         Condition<? super E> condition)
        Verifies that there is exactly n elements in the actual Iterable satisfying the given condition.
        Parameters:
        info - contains information about the assertion.
        actual - the given Iterable.
        n - the exact number of times the condition should be verified.
        condition - the given Condition.
        Throws:
        java.lang.NullPointerException - if the given condition is null.
        java.lang.AssertionError - if a element cannot be cast to E.
        java.lang.AssertionError - if the number of elements satisfying the given condition is ≠ n.
      • assertAreNotExactly

        public <E> void assertAreNotExactly​(AssertionInfo info,
                                            java.lang.Iterable<? extends E> actual,
                                            int n,
                                            Condition<? super E> condition)
        Verifies that there is exactly n elements in the actual Iterable not satisfying the given condition.
        Parameters:
        info - contains information about the assertion.
        actual - the given Iterable.
        n - the exact number of times the condition should not be verified.
        condition - the given Condition.
        Throws:
        java.lang.NullPointerException - if the given condition is null.
        java.lang.AssertionError - if a element cannot be cast to E.
        java.lang.AssertionError - if the number of elements not satisfying the given condition is ≠ n.
      • assertContainsAll

        public void assertContainsAll​(AssertionInfo info,
                                      java.lang.Iterable<?> actual,
                                      java.lang.Iterable<?> other)
        Asserts that the given Iterable contains all the elements of the other Iterable, in any order.
        Parameters:
        info - contains information about the assertion.
        actual - the given Iterable.
        other - the other Iterable.
        Throws:
        java.lang.NullPointerException - if Iterable is null.
        java.lang.AssertionError - if the given Iterable is null.
        java.lang.AssertionError - if the given Iterable does not contain all the elements of the other Iterable, in any order.
      • assertContainsExactly

        public void assertContainsExactly​(AssertionInfo info,
                                          java.lang.Iterable<?> actual,
                                          java.lang.Object[] values)
        Asserts that the given Iterable contains exactly the given values and nothing else, in order.
        Parameters:
        info - contains information about the assertion.
        actual - the given Iterable.
        values - the values that are expected to be in the given Iterable in order.
        Throws:
        java.lang.NullPointerException - if the array of values is null.
        java.lang.IllegalArgumentException - if the array of values is empty.
        java.lang.AssertionError - if the given Iterable is null.
        java.lang.AssertionError - if the given Iterable does not contain the given values or if the given Iterable contains values that are not in the given array, in order.
      • iterableToLookForIsNull

        public static java.lang.NullPointerException iterableToLookForIsNull()
      • iterableToLookForIsEmpty

        public static java.lang.IllegalArgumentException iterableToLookForIsEmpty()