From 15b22381b8a164ab69f17ff9585f2502b1dbb0c0 Mon Sep 17 00:00:00 2001 From: MEDDAH Julien Date: Tue, 23 Oct 2012 17:57:48 +0200 Subject: [PATCH] Assertions.assertThat can now take an Iterator parameter --- .../org/fest/assertions/api/Assertions.java | 12 +++++ .../fest/assertions/api/IterableAssert.java | 18 +++++++ ...ertions_assertThat_with_Iterator_Test.java | 48 +++++++++++++++++++ 3 files changed, 78 insertions(+) create mode 100644 src/test/java/org/fest/assertions/api/Assertions_assertThat_with_Iterator_Test.java diff --git a/src/main/java/org/fest/assertions/api/Assertions.java b/src/main/java/org/fest/assertions/api/Assertions.java index cd07b946..56b04de4 100644 --- a/src/main/java/org/fest/assertions/api/Assertions.java +++ b/src/main/java/org/fest/assertions/api/Assertions.java @@ -19,6 +19,7 @@ import java.math.BigDecimal; import java.nio.charset.Charset; import java.util.Date; +import java.util.Iterator; import java.util.List; import java.util.Map; @@ -58,6 +59,7 @@ * @author Matthieu Baechler * @author Mikhail Mazursky * @author Nicolas François + * @author Julien Meddah */ public class Assertions { @@ -160,6 +162,16 @@ public static IterableAssert assertThat(Iterable actual) { return new IterableAssert(actual); } + /** + * Creates a new instance of {@link IterableAssert}. + * The {@link Iterator} is first converted into an {@link Iterable} + * @param actual the actual value. + * @return the created assertion object. + */ + public static IterableAssert assertThat(Iterator actual) { + return new IterableAssert(actual); + } + /** * Creates a new instance of {@link DoubleAssert}. * @param actual the actual value. diff --git a/src/main/java/org/fest/assertions/api/IterableAssert.java b/src/main/java/org/fest/assertions/api/IterableAssert.java index 38c9260e..892e4ee2 100644 --- a/src/main/java/org/fest/assertions/api/IterableAssert.java +++ b/src/main/java/org/fest/assertions/api/IterableAssert.java @@ -14,6 +14,11 @@ */ package org.fest.assertions.api; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Iterator; + + /** * Assertion methods for {@link Iterable}. *

@@ -26,10 +31,23 @@ * @author Matthieu Baechler * @author Joel Costigliola * @author Mikhail Mazursky + * @author Julien Meddah */ public class IterableAssert extends AbstractIterableAssert, Iterable, T> { protected IterableAssert(Iterable actual) { super(actual, IterableAssert.class); } + + protected IterableAssert(Iterator actual) { + this(toIterable(actual)); + } + + private static Iterable toIterable(Iterator actual) { + Collection actualList = new ArrayList(); + while(actual.hasNext()) { + actualList.add(actual.next()); + } + return actualList; + } } diff --git a/src/test/java/org/fest/assertions/api/Assertions_assertThat_with_Iterator_Test.java b/src/test/java/org/fest/assertions/api/Assertions_assertThat_with_Iterator_Test.java new file mode 100644 index 00000000..e8e6cf85 --- /dev/null +++ b/src/test/java/org/fest/assertions/api/Assertions_assertThat_with_Iterator_Test.java @@ -0,0 +1,48 @@ +/* + * Created on Oct 18, 2010 + * + * 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 @2010-2011 the original author or authors. + */ +package org.fest.assertions.api; + +import static java.util.Arrays.asList; +import static org.fest.assertions.api.Assertions.assertThat; +import static org.fest.util.Sets.newLinkedHashSet; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertThat; +import static org.junit.matchers.JUnitMatchers.hasItems; + +import java.util.Iterator; + +import org.junit.Test; + +/** + * Tests for {@link Assertions#assertThat(Iterator)}. + * + * @author Julien Meddah + * @author Joel Costigliola + */ +public class Assertions_assertThat_with_Iterator_Test { + + @Test + public void should_create_Assert() { + IterableAssert assertions = Assertions.assertThat(newLinkedHashSet()); + assertNotNull(assertions); + } + + @Test + public void should_initialise_actual() { + Iterator names = asList("Luke", "Leia").iterator(); + IterableAssert assertions = assertThat(names); + assertThat(assertions.actual, hasItems("Leia", "Luke")); + } +}