Skip to content
Permalink
Browse files

[Breaking change] Introduce first class Iterator assertions without r…

…emoving previous supported assertions that were for Iterable. Fixes #414.
  • Loading branch information...
stovocor authored and joel-costigliola committed Jul 1, 2018
1 parent b68e95c commit acafa142aa903afd611de812576e6e80e12e7964
Showing with 712 additions and 636 deletions.
  1. +0 −8 src/main/java/org/assertj/core/api/AbstractIterableAssert.java
  2. +96 −0 src/main/java/org/assertj/core/api/AbstractIteratorAssert.java
  3. +13 −5 src/main/java/org/assertj/core/api/Assertions.java
  4. +3 −7 src/main/java/org/assertj/core/api/AssertionsForInterfaceTypes.java
  5. +5 −3 src/main/java/org/assertj/core/api/Assumptions.java
  6. +15 −5 src/main/java/org/assertj/core/api/BDDAssertions.java
  7. +2 −169 src/main/java/org/assertj/core/api/IterableAssert.java
  8. +33 −0 src/main/java/org/assertj/core/api/IteratorAssert.java
  9. +5 −5 src/main/java/org/assertj/core/api/Java6AbstractBDDSoftAssertions.java
  10. +4 −5 src/main/java/org/assertj/core/api/Java6AbstractStandardSoftAssertions.java
  11. +15 −7 src/main/java/org/assertj/core/api/Java6Assertions.java
  12. +14 −5 src/main/java/org/assertj/core/api/Java6BDDAssertions.java
  13. +1 −0 src/main/java/org/assertj/core/api/ObjectEnumerableAssert.java
  14. +1 −6 src/main/java/org/assertj/core/api/ProxyableIterableAssert.java
  15. +13 −5 src/main/java/org/assertj/core/api/WithAssertions.java
  16. +5 −3 src/main/java/org/assertj/core/api/WithAssumptions.java
  17. +43 −0 src/main/java/org/assertj/core/error/ShouldBeExhausted.java
  18. +40 −0 src/main/java/org/assertj/core/error/ShouldHaveNext.java
  19. +3 −3 src/main/java/org/assertj/core/internal/FloatArrays.java
  20. +3 −3 src/main/java/org/assertj/core/internal/IntArrays.java
  21. +34 −14 src/main/java/org/assertj/core/internal/Iterables.java
  22. +57 −0 src/main/java/org/assertj/core/internal/Iterators.java
  23. +3 −3 src/main/java/org/assertj/core/internal/LongArrays.java
  24. +3 −3 src/main/java/org/assertj/core/internal/ShortArrays.java
  25. +3 −4 src/test/java/org/assertj/core/api/Assertions_assertThat_with_DoubleStream_Test.java
  26. +3 −4 src/test/java/org/assertj/core/api/Assertions_assertThat_with_IntStream_Test.java
  27. +7 −124 src/test/java/org/assertj/core/api/Assertions_assertThat_with_Iterator_Test.java
  28. +3 −4 src/test/java/org/assertj/core/api/Assertions_assertThat_with_LongStream_Test.java
  29. +3 −4 src/test/java/org/assertj/core/api/Assertions_assertThat_with_Stream_Test.java
  30. +6 −10 src/test/java/org/assertj/core/api/AutoCloseableBDDSoftAssertionsTest.java
  31. +2 −8 src/test/java/org/assertj/core/api/AutoCloseableSoftAssertionsTest.java
  32. +3 −2 src/test/java/org/assertj/core/api/BDDAssertions_then_Test.java
  33. +2 −22 src/test/java/org/assertj/core/api/BDDSoftAssertionsTest.java
  34. +47 −0 src/test/java/org/assertj/core/api/IteratorAssertBaseTest.java
  35. +5 −24 src/test/java/org/assertj/core/api/SoftAssertionsTest.java
  36. +2 −1 src/test/java/org/assertj/core/api/WithAssertions_delegation_Test.java
  37. +2 −2 src/test/java/org/assertj/core/api/assumptions/Assumptions_assumeThat_with_various_types_Test.java
  38. +0 −145 src/test/java/org/assertj/core/api/assumptions/Iterator_assumptions_Test.java
  39. +0 −5 src/test/java/org/assertj/core/api/iterable/IterableAssert_filteredOn_Test.java
  40. +2 −2 src/test/java/org/assertj/core/api/iterable/IterableAssert_filteredOn_condition_Test.java
  41. +0 −5 src/test/java/org/assertj/core/api/iterable/IterableAssert_filteredOn_in_Test.java
  42. +0 −5 src/test/java/org/assertj/core/api/iterable/IterableAssert_filteredOn_notIn_Test.java
  43. +0 −5 src/test/java/org/assertj/core/api/iterable/IterableAssert_filteredOn_not_Test.java
  44. +73 −0 src/test/java/org/assertj/core/api/iterator/IteratorAssert_hasNext_Test.java
  45. +73 −0 src/test/java/org/assertj/core/api/iterator/IteratorAssert_isExhausted_Test.java
  46. +32 −0 src/test/java/org/assertj/core/error/ShouldBeExhausted_create_Test.java
  47. +32 −0 src/test/java/org/assertj/core/error/ShouldHaveNext_create_Test.java
  48. +1 −1 src/test/java/org/assertj/core/perf/SoftAssertionsPerfTest.java
@@ -48,7 +48,6 @@
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.assertj.core.api.IterableAssert.LazyIterable;
import org.assertj.core.api.filter.FilterOperator;
import org.assertj.core.api.filter.Filters;
import org.assertj.core.api.iterable.Extractor;
@@ -120,13 +119,6 @@ public AbstractIterableAssert(ACTUAL actual, Class<?> selfType) {
super(actual, selfType);
}

protected static <T> Iterable<T> toLazyIterable(Iterator<T> actual) {
if (actual == null) {
return null;
}
return new LazyIterable<>(actual);
}

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

import java.util.Iterator;

import org.assertj.core.internal.Iterators;
import org.assertj.core.util.VisibleForTesting;

/**
* <p>Base class for all implementations of assertions for {@link Iterator}s.</p>
* <p>Note that none of the assertions modify the actual iterator, i.e. they do not consume any elements.
* In order to use consuming assertions, use {@link #toIterable()}.</p>
*
* @param <SELF> the "self" type of this assertion class.
* @param <ELEMENT> the type of elements.
*
* @author Stephan Windmüller
* @since 3.12.0
*/
public abstract class AbstractIteratorAssert<SELF extends AbstractIteratorAssert<SELF, ELEMENT>, ELEMENT>
extends AbstractAssert<SELF, Iterator<? extends ELEMENT>> {

@VisibleForTesting
Iterators iterators = Iterators.instance();

/**
* Creates a new <code>{@link org.assertj.core.api.AbstractIteratorAssert}</code>.
*
* @param actual the actual value to verify
* @param selfType the "self type"
*/
public AbstractIteratorAssert(Iterator<? extends ELEMENT> actual, Class<?> selfType) {
super(actual, selfType);
}

/**
* <p>Verifies that the actual {@code Iterator} has at least one more element.</p>
*
* Example:
* <pre><code class='java'> Iterator&lt;TolkienCharacter&gt; elvesRingBearers = list(galadriel, elrond, gandalf).iterator();
*
* assertThat(elvesRingBearers).hasNext();</code></pre>
*
* @throws AssertionError if the actual {@code Iterator} is {@code null} or does not have another element.
* @return this assertion object.
* @since 3.12.0
*/
public SELF hasNext() {
iterators.assertHasNext(info, actual);
return myself;
}

/**
* <p>Verifies that the actual {@code Iterator} has no more elements.</p>
*
* Example:
* <pre><code class='java'> Iterator&lt;String&gt; result = Collections.emptyList().iterator();
*
* assertThat(result).isExhausted();</code></pre>
*
* @throws AssertionError if the actual {@code Iterator} is {@code null} or has another element.
* @return this assertion object.
* @since 3.12.0
*/
public SELF isExhausted() {
iterators.assertIsExhausted(info, actual);
return myself;
}

/**
* <p>Creates a new {@link IterableAssert} from this {@link IteratorAssert} which allows for
* using any Iterable assertions like {@link IterableAssert#contains(Object[])}.</p>
* Example:
* <pre><code class='java'> Iterator&lt;String&gt; bestBasketBallPlayers = getBestBasketBallPlayers();
*
* assertThat(bestBasketBallPlayers).toIterable().contains("Jordan", "Magic", "Lebron");</code></pre>
*
* @return the new {@link IterableAssert}.
* @since 3.12.0
*/
public IterableAssert<ELEMENT> toIterable() {
return new IterableAssert<>(IterableAssert.toIterable(actual));
}

}
@@ -2635,18 +2635,26 @@ public static void useDefaultDateFormatsOnly() {
}

/**
* Creates a new instance of <code>{@link IterableAssert}</code>.
* Creates a new instance of <code>{@link IteratorAssert}</code>.
* <p>
* <b>Breaking change in version 3.12.0:</b> this method does not return anymore an {@link IterableAssert} but an {@link IteratorAssert}.<br>
* In order to access assertions from {@link IterableAssert}, use {@link IteratorAssert#toIterable()}.
* <p>
* <b>Be aware that calls to most methods on returned IterableAssert will consume Iterator so it won't be possible to
* iterate over it again.</b> Calling multiple methods on returned IterableAssert is safe as Iterator's elements are
* cached by IterableAssert first time Iterator is consumed.
* {@link IteratorAssert} instances have limited assertions because it does not consume iterator's elements.
* <p>
* Examples:
* <pre><code class='java'> Iterator&lt;String&gt; bestBasketBallPlayers = getBestBasketBallPlayers();
*
* assertThat(bestBasketBallPlayers).hasNext() // Iterator assertion
* .toIterable() // switch to Iterable assertions
* .contains("Jordan", "Magic", "Lebron"); // Iterable assertion </code></pre>
*
* @param <ELEMENT> the type of elements.
* @param actual the actual value.
* @return the created assertion object.
*/
@CheckReturnValue
public static <ELEMENT> IterableAssert<ELEMENT> assertThat(Iterator<? extends ELEMENT> actual) {
public static <ELEMENT> IteratorAssert<ELEMENT> assertThat(Iterator<? extends ELEMENT> actual) {
return AssertionsForInterfaceTypes.assertThat(actual);
}

@@ -107,19 +107,15 @@ protected AssertionsForInterfaceTypes() {}
}

/**
* Creates a new instance of <code>{@link IterableAssert}</code>.
* <p>
* <b>Be aware that calls to most methods on returned IterableAssert will consume Iterator so it won't be possible to
* iterate over it again.</b> Calling multiple methods on returned IterableAssert is safe as Iterator's elements are
* cached by IterableAssert first time Iterator is consumed.
* Creates a new instance of <code>{@link IteratorAssert}</code>.
*
* @param <ELEMENT> the type of elements.
* @param actual the actual value.
* @return the created assertion object.
*/
@CheckReturnValue
public static <ELEMENT> IterableAssert<ELEMENT> assertThat(Iterator<? extends ELEMENT> actual) {
return new IterableAssert<>(actual);
public static <ELEMENT> IteratorAssert<ELEMENT> assertThat(Iterator<? extends ELEMENT> actual) {
return new IteratorAssert<>(actual);
}

/**
@@ -763,7 +763,9 @@ public static AtomicLongArrayAssert assumeThat(AtomicLongArray actual) {
}

/**
* Creates a new instance of <code>{@link IterableAssert}</code> assumption.
* Creates a new instance of <code>{@link IteratorAssert}</code> assumption.
* <p>
* <b>Breaking change in version 3.12.0:</b> this method does not return anymore an {@link ProxyableIterableAssert} but an {@link IteratorAssert}.<br>
*
* @param <ELEMENT> the type of elements.
* @param actual the actual value.
@@ -772,8 +774,8 @@ public static AtomicLongArrayAssert assumeThat(AtomicLongArray actual) {
*/
@CheckReturnValue
@SuppressWarnings("unchecked")
public static <ELEMENT> ProxyableIterableAssert<ELEMENT> assumeThat(Iterator<? extends ELEMENT> actual) {
return asAssumption(ProxyableIterableAssert.class, Iterator.class, actual);
public static <ELEMENT> IteratorAssert<ELEMENT> assumeThat(Iterator<? extends ELEMENT> actual) {
return asAssumption(IteratorAssert.class, Iterator.class, actual);
}

/**
@@ -356,17 +356,27 @@ public static ClassAssert then(Class<?> actual) {
}

/**
* Creates a new instance of <code>{@link org.assertj.core.api.IterableAssert}</code>. The <code>{@link
* java.util.Iterator}</code> is first
* converted
* into an <code>{@link Iterable}</code>
* Creates a new instance of <code>{@link IteratorAssert}</code>.
* <p>
* <b>Breaking change in version 3.12.0:</b> this method does not return anymore an {@link IterableAssert} but an {@link IteratorAssert}.<br>
* In order to access assertions from {@link IterableAssert}, use {@link IteratorAssert#toIterable()}.
* <p>
* {@link IteratorAssert} instances have limited assertions because it does not consume iterator's elements.
* <p>
* Examples:
* <pre><code class='java'> Iterator&lt;String&gt; bestBasketBallPlayers = getBestBasketBallPlayers();
*
* then(bestBasketBallPlayers).hasNext() // Iterator assertion
* .toIterable() // switch to Iterable assertions
* .contains("Jordan", "Magic", "Lebron"); // Iterable assertion </code></pre>
*
*
* @param <T> the actual elements type
* @param actual the actual value.
* @return the created assertion object.
*/
@CheckReturnValue
public static <T> IterableAssert<T> then(Iterator<? extends T> actual) {
public static <T> IteratorAssert<T> then(Iterator<? extends T> actual) {
return assertThat(actual);
}

0 comments on commit acafa14

Please sign in to comment.
You can’t perform that action at this time.