Skip to content
This repository

assertThat() should accept an Iterator #118

Open
wants to merge 1 commit into from

2 participants

MEDDAH Julien Joel Costigliola
MEDDAH Julien

In reference to http://code.google.com/p/fest/issues/detail?id=101
There is no way currently to call Assertions.assertThat with an Iterator<T> as parameter.
An user can still convert himself the iterator to an iterable, but he might as well use one more assertThat overload.

Joel Costigliola
Collaborator

Done in last Paris Hackergarten, thanks Julien.

Joel Costigliola joel-costigliola referenced this pull request in joel-costigliola/assertj-core March 20, 2013
Closed

assertThat() should accept an Iterator #16

Joel Costigliola
Collaborator

For your information, this issue has been fixed in AssertJ a fork of Fest Assert 2.0M10.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 1 unique commit by 1 author.

Oct 23, 2012
MEDDAH Julien Assertions.assertThat can now take an Iterator parameter 15b2238
This page is out of date. Refresh to see the latest.
12  src/main/java/org/fest/assertions/api/Assertions.java
@@ -19,6 +19,7 @@
19 19
 import java.math.BigDecimal;
20 20
 import java.nio.charset.Charset;
21 21
 import java.util.Date;
  22
+import java.util.Iterator;
22 23
 import java.util.List;
23 24
 import java.util.Map;
24 25
 
@@ -58,6 +59,7 @@
58 59
  * @author Matthieu Baechler
59 60
  * @author Mikhail Mazursky
60 61
  * @author Nicolas François
  62
+ * @author Julien Meddah
61 63
  */
62 64
 public class Assertions {
63 65
 
@@ -161,6 +163,16 @@ public static CharacterAssert assertThat(Character actual) {
161 163
   }
162 164
 
163 165
   /**
  166
+   * Creates a new instance of <code>{@link IterableAssert}</code>.
  167
+   * The <code>{@link Iterator}</code> is first converted into an <code>{@link Iterable}</code>
  168
+   * @param actual the actual value.
  169
+   * @return the created assertion object.
  170
+   */
  171
+  public static <T> IterableAssert<T> assertThat(Iterator<T> actual) {
  172
+    return new IterableAssert<T>(actual);
  173
+  }
  174
+
  175
+  /**
164 176
    * Creates a new instance of <code>{@link DoubleAssert}</code>.
165 177
    * @param actual the actual value.
166 178
    * @return the created assertion object.
18  src/main/java/org/fest/assertions/api/IterableAssert.java
@@ -14,6 +14,11 @@
14 14
  */
15 15
 package org.fest.assertions.api;
16 16
 
  17
+import java.util.ArrayList;
  18
+import java.util.Collection;
  19
+import java.util.Iterator;
  20
+
  21
+
17 22
 /**
18 23
  * Assertion methods for {@link Iterable}.
19 24
  * <p>
@@ -26,10 +31,23 @@
26 31
  * @author Matthieu Baechler
27 32
  * @author Joel Costigliola
28 33
  * @author Mikhail Mazursky
  34
+ * @author Julien Meddah
29 35
  */
30 36
 public class IterableAssert<T> extends AbstractIterableAssert<IterableAssert<T>, Iterable<T>, T> {
31 37
 
32 38
   protected IterableAssert(Iterable<T> actual) {
33 39
     super(actual, IterableAssert.class);
34 40
   }
  41
+  
  42
+  protected IterableAssert(Iterator<T> actual) {
  43
+    this(toIterable(actual));
  44
+  }
  45
+
  46
+  private static <T> Iterable<T> toIterable(Iterator<T> actual) {
  47
+    Collection<T> actualList = new ArrayList<T>();
  48
+    while(actual.hasNext()) {
  49
+      actualList.add(actual.next());
  50
+    }
  51
+    return actualList;
  52
+  }
35 53
 }
48  src/test/java/org/fest/assertions/api/Assertions_assertThat_with_Iterator_Test.java
... ...
@@ -0,0 +1,48 @@
  1
+/*
  2
+ * Created on Oct 18, 2010
  3
+ * 
  4
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the
  5
+ * License. You may obtain a copy of the License at
  6
+ * 
  7
+ * http://www.apache.org/licenses/LICENSE-2.0
  8
+ * 
  9
+ * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS"
  10
+ * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language
  11
+ * governing permissions and limitations under the License.
  12
+ * 
  13
+ * Copyright @2010-2011 the original author or authors.
  14
+ */
  15
+package org.fest.assertions.api;
  16
+
  17
+import static java.util.Arrays.asList;
  18
+import static org.fest.assertions.api.Assertions.assertThat;
  19
+import static org.fest.util.Sets.newLinkedHashSet;
  20
+import static org.junit.Assert.assertNotNull;
  21
+import static org.junit.Assert.assertThat;
  22
+import static org.junit.matchers.JUnitMatchers.hasItems;
  23
+
  24
+import java.util.Iterator;
  25
+
  26
+import org.junit.Test;
  27
+
  28
+/**
  29
+ * Tests for <code>{@link Assertions#assertThat(Iterator)}</code>.
  30
+ * 
  31
+ * @author Julien Meddah
  32
+ * @author Joel Costigliola
  33
+ */
  34
+public class Assertions_assertThat_with_Iterator_Test {
  35
+
  36
+  @Test
  37
+  public void should_create_Assert() {
  38
+    IterableAssert<Object> assertions = Assertions.assertThat(newLinkedHashSet());
  39
+    assertNotNull(assertions);
  40
+  }
  41
+
  42
+  @Test
  43
+  public void should_initialise_actual() {
  44
+    Iterator<String> names = asList("Luke", "Leia").iterator();
  45
+    IterableAssert<String> assertions = assertThat(names);
  46
+    assertThat(assertions.actual, hasItems("Leia", "Luke"));
  47
+  }
  48
+}
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.