Closed GoogleCodeExporter closed 9 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
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
[deleted comment]
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
Thanks Ted for the clarification :)
-Alex
Original comment by Alex.Rui...@gmail.com
on 18 Jan 2008 at 6:19
Added method 'assertThat(Iterator)' to Assertions. This method returns a
CollectionAssert.
Original comment by wanghy1...@gmail.com
on 25 Jan 2008 at 3:36
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
Original issue reported on code.google.com by
tedyo...@gmail.com
on 17 Jan 2008 at 9:24