Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

assertThat() should accept an Iterator #118

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/main/java/org/fest/assertions/api/Assertions.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -58,6 +59,7 @@
* @author Matthieu Baechler
* @author Mikhail Mazursky
* @author Nicolas François
* @author Julien Meddah
*/
public class Assertions {

Expand Down Expand Up @@ -160,6 +162,16 @@ public static <T> IterableAssert<T> assertThat(Iterable<T> actual) {
return new IterableAssert<T>(actual);
}

/**
* Creates a new instance of <code>{@link IterableAssert}</code>.
* The <code>{@link Iterator}</code> is first converted into an <code>{@link Iterable}</code>
* @param actual the actual value.
* @return the created assertion object.
*/
public static <T> IterableAssert<T> assertThat(Iterator<T> actual) {
return new IterableAssert<T>(actual);
}

/**
* Creates a new instance of <code>{@link DoubleAssert}</code>.
* @param actual the actual value.
Expand Down
18 changes: 18 additions & 0 deletions src/main/java/org/fest/assertions/api/IterableAssert.java
Original file line number Diff line number Diff line change
Expand Up @@ -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}.
* <p>
Expand All @@ -26,10 +31,23 @@
* @author Matthieu Baechler
* @author Joel Costigliola
* @author Mikhail Mazursky
* @author Julien Meddah
*/
public class IterableAssert<T> extends AbstractIterableAssert<IterableAssert<T>, Iterable<T>, T> {

protected IterableAssert(Iterable<T> actual) {
super(actual, IterableAssert.class);
}

protected IterableAssert(Iterator<T> actual) {
this(toIterable(actual));
}

private static <T> Iterable<T> toIterable(Iterator<T> actual) {
Collection<T> actualList = new ArrayList<T>();
while(actual.hasNext()) {
actualList.add(actual.next());
}
return actualList;
}
}
Original file line number Diff line number Diff line change
@@ -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 <code>{@link Assertions#assertThat(Iterator)}</code>.
*
* @author Julien Meddah
* @author Joel Costigliola
*/
public class Assertions_assertThat_with_Iterator_Test {

@Test
public void should_create_Assert() {
IterableAssert<Object> assertions = Assertions.assertThat(newLinkedHashSet());
assertNotNull(assertions);
}

@Test
public void should_initialise_actual() {
Iterator<String> names = asList("Luke", "Leia").iterator();
IterableAssert<String> assertions = assertThat(names);
assertThat(assertions.actual, hasItems("Leia", "Luke"));
}
}