alexruiz / fest-assert-2.x

FEST Fluent Assertions 2.x
http://fest.easytesting.org
Apache License 2.0
402 stars 69 forks source link

org.fest.util.Iterables#sizeOf cause and endless loop #142

Closed laszlohordos closed 11 years ago

laszlohordos commented 11 years ago

I use in Maven the org.easytesting:fest-assert-core:2.0M8 and the Iterable assertion hangs.

assertThat(MyClass implements Iterable<Object>).hasSize(2);

The cause is in fest-util-123 where the iterator is not moved to the next.

package org.fest.util;

// TODO(alexRuiz): Get rid of this class.
public final class Iterables {

/**
 * Returns the size of the given {@link Iterable}.
 * 
 * @param iterable the {@link Iterable} to get size.
 * @return the size of the given {@link Iterable}.
 * @throws NullPointerException if given {@link Iterable} is null.
 */
public static int sizeOf(Iterable<?> iterable) {
  if (iterable == null) {
    throw new NullPointerException("Iterable must not be null");
  }
  if (iterable instanceof Collection) {
    return ((Collection<?>) iterable).size();
  }
  int size = 0;
  Iterator<?> iterator = iterable.iterator();
  while (iterator.hasNext()) {
    size++;

HERE the iterator is not shifted!!! iterator.next(); is missing.

  }
  return size;
}
joel-costigliola commented 11 years ago

This has already been fixed, see https://github.com/alexruiz/fest-util/issues/13 Next release of fest-assert-core will use the fixed fest-util version.

Regards