Created
December 2, 2016 12:13
-
-
Save takawitter/faff1bdd72dd87eea1a64e02b4a30607 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import static org.junit.Assert.assertEquals; | |
import static org.junit.Assert.assertFalse; | |
import static org.junit.Assert.assertTrue; | |
import java.util.Collection; | |
import java.util.Iterator; | |
import java.util.function.Consumer; | |
public class CollectionFixture<T> { | |
public CollectionFixture(Collection<T> collection){ | |
this.collection = collection; | |
this.it = collection.iterator(); | |
} | |
public CollectionFixture<T> assertHasNext(){ | |
assertTrue(it.hasNext()); | |
return this; | |
} | |
public CollectionFixture<T> assertNotHasNext(){ | |
assertFalse(it.hasNext()); | |
return this; | |
} | |
public CollectionFixture<T> assertSize(int expected){ | |
assertEquals(expected, collection.size()); | |
return this; | |
} | |
@SuppressWarnings("unchecked") | |
public <U extends T> CollectionFixture<T> next(Consumer<U> c){ | |
c.accept((U)it.next()); | |
return this; | |
} | |
private Collection<T> collection; | |
private Iterator<T> it; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import static org.junit.Assert.assertEquals; | |
import static org.junit.Assert.assertFalse; | |
public class SampleTest{ | |
static class Fruit{} | |
static class Apple extends Fruit{boolean bitten;} | |
static class Orange extends Fruit{enum Type{Tangerine, Mandarin} Type type = Type.Tangerine;} | |
@Test | |
public void t() throws Throwable{ | |
new CollectionFixture<Fruit>(Arrays.asList(new Apple(), new Orange())) | |
.next((Apple f) -> { | |
assertFalse(f.bitten); | |
}) | |
.next((Orange f) -> { | |
assertEquals(f.type, Orange.Type.Tangerine); | |
}) | |
.assertNotHasNext(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment