jjnair / fest

Automatically exported from code.google.com/p/fest
0 stars 0 forks source link

assertThat() should accept an Iterator #101

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
Currently assertThat() has signatures for arrays and collections, but none
for Iterator. There should be a method that looks something like this:

public CollectionAssert assertThat(Iterator actual) {
...
}

An easy way to do this is to wrap the Iterator into a Collection and then
just return a CollectionAssert. A better way would be to implement an
IteratorAssert that extends GroupAssert, or possibly that extends
CollectionAssert. The goal would be to fail as quickly as possible since
iterators tend to be used when the cost of accessing all of the elements is
expensive.

Original issue reported on code.google.com by tedyo...@gmail.com on 17 Jan 2008 at 9:24

GoogleCodeExporter commented 8 years ago
So here's my "easy way" to implement assertThat(Iterator):

package org.fest.assertions;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

/**
 * Assertions for iterators.
 *
 * @author tyoung
 */
public class IteratorAssert<T> extends CollectionAssert {

  IteratorAssert(Iterator<T> actual) {
    super(toList(actual));
  }

  private static <T> Collection<T> toList(Iterator<T> iterator) {
    List<T> list = new ArrayList<T>();
    while (iterator.hasNext()) {
      list.add(iterator.next());
    }
    return list;
  }

}

I can generate a patch for the Assertions.java class and I have the
IteratorAssertTest, but since this code is fairly simple, I think the following 
is
good enough:

package org.fest.assertions;

import static org.fest.util.Collections.list;
import org.testng.annotations.Test;

import java.util.ArrayList;
import java.util.Iterator;

/**
 * Tests for <code>{@link org.fest.assertions.IteratorAssert}</code>.
 *
 * @author Ted Young
 */
public class IteratorAssertTest {

  @Test public void shouldPassIfGivenObjectIsInIterator() {
    new IteratorAssert<String>(iterator("Luke", "Leia")).contains("Luke");
    new IteratorAssert<String>(iterator("Luke", "Leia")).contains("Leia");
  }

  public static <T> Iterator<T> iterator(T... elements) {
    return list(elements).iterator();
  }

  @Test(dependsOnMethods = "shouldPassIfGivenObjectIsInIterator")
  public void shouldPassIfGivenObjectsAreInIterator() {
    new IteratorAssert<String>(iterator("Luke", "Leia", "Anakin")).contains("Luke",
"Leia");
  }

  @Test(dependsOnMethods = "shouldPassIfGivenObjectIsInIterator", expectedExceptions
= AssertionError.class)
  public void shouldFailIfGivenObjectIsNotInIterator() {
    new IteratorAssert<String>(new ArrayList<String>().iterator()).contains("Luke");
  }

  @Test public void shouldPassIfGivenObjectIsNotInCollection() {
    new IteratorAssert<String>(iterator("Luke", "Leia")).excludes("Anakin");
  }
}

Of course, the static iterator() convenience class should move to the 
appropriate
utils class.

Thoughts?

;ted

Original comment by tedyo...@gmail.com on 18 Jan 2008 at 2:39

GoogleCodeExporter commented 8 years ago
Very nice! No need to create a patch. Thanks a lot Ted! :)

-Alex.

Original comment by Alex.Rui...@gmail.com on 18 Jan 2008 at 6:06

GoogleCodeExporter commented 8 years ago
[deleted comment]
GoogleCodeExporter commented 8 years ago
As I mentioned in an email, since the IteratorAssert class ends up not doing 
very
much, we can instead just do:

  public static <T> CollectionAssert assertThat(Iterator<T> actual) {
    return new CollectionAssert<T>(toList(actual));
  }

  private static <T> Collection<T> toList(Iterator<T> iterator) {
    List<T> list = new ArrayList<T>();
    while (iterator.hasNext()) {
      list.add(iterator.next());
    }
    return list;
  }

With the toList() in the Collections class.

Original comment by tedyo...@gmail.com on 18 Jan 2008 at 6:12

GoogleCodeExporter commented 8 years ago
Thanks Ted for the clarification :)

-Alex

Original comment by Alex.Rui...@gmail.com on 18 Jan 2008 at 6:19

GoogleCodeExporter commented 8 years ago
Added method 'assertThat(Iterator)' to Assertions. This method returns a 
CollectionAssert.

Original comment by wanghy1...@gmail.com on 25 Jan 2008 at 3:36

GoogleCodeExporter commented 8 years ago
Set the module as a label, instead of being part of the title.

Original comment by Alex.Rui...@gmail.com on 1 Dec 2008 at 1:48