Commit 567f2d3b authored by Christian Wulf's avatar Christian Wulf

#359: added producesNothing()

parent b8ea9454
package teetime.framework.test;
import java.util.List;
import org.hamcrest.BaseMatcher;
import org.hamcrest.Description;
import org.hamcrest.Matcher;
......@@ -10,15 +12,17 @@ import teetime.stage.CollectorSink;
class Produces<T, O extends OutputPort<T>> extends BaseMatcher<O> {
private final T[] expectedElements;
private final Matcher<Iterable<? extends T>> matcher;
@SafeVarargs
public Produces(final T... expectedElements) {
this.expectedElements = expectedElements;
this.matcher = Matchers.contains(expectedElements);
}
public Produces() {
this.matcher = Matchers.emptyIterable();
}
@Override
public boolean matches(final Object item) {
if (!(item instanceof OutputPort)) {
......@@ -36,7 +40,8 @@ class Produces<T, O extends OutputPort<T>> extends BaseMatcher<O> {
@Override
public void describeTo(final Description description) {
description.appendText("to produce ").appendValueList("", ", ", "", expectedElements);
description.appendText("to produce ");
matcher.describeTo(description);
}
@Override
......@@ -49,7 +54,8 @@ class Produces<T, O extends OutputPort<T>> extends BaseMatcher<O> {
OutputPort<T> outputPort = (OutputPort<T>) item;
CollectorSink<T> collectorSink = StageFactory.getSinkFromOutputPort(outputPort);
description.appendText("has produced ").appendValueList("", ", ", "", collectorSink.getElements());
List<T> actualElements = collectorSink.getElements();
description.appendText("has produced ").appendValueList("[", ", ", "]", actualElements);
}
}
......@@ -163,4 +163,9 @@ public class StageTester {
return new Produces<T, OutputPort<T>>(values);
}
@Factory
public static <T> Matcher<OutputPort<T>> producesNothing() {
return new Produces<T, OutputPort<T>>();
}
}
......@@ -16,9 +16,6 @@
package teetime.stage;
import static org.hamcrest.collection.IsCollectionWithSize.*;
import static org.hamcrest.collection.IsEmptyCollection.*;
import static org.hamcrest.collection.IsIterableContainingInOrder.*;
import static org.hamcrest.core.Is.*;
import static org.junit.Assert.*;
import static teetime.framework.test.StageTester.*;
......@@ -45,59 +42,48 @@ public class InstanceOfFilterTest {
@Test
public void filterShouldForwardCorrectTypes() {
final List<Clazz> results = new ArrayList<InstanceOfFilterTest.Clazz>();
final Object clazz = new Clazz();
final Clazz clazz = new Clazz();
test(filter)
.and().send(clazz).to(filter.getInputPort())
.and().receive(results).from(filter.getMatchedOutputPort())
.start();
assertThat(results, contains(clazz));
assertThat(filter.getMatchedOutputPort(), produces(clazz));
}
@Test
public void outputMatchedAndMismatchedElements() {
final List<Clazz> matchedElements = new ArrayList<InstanceOfFilterTest.Clazz>();
final List<Object> mismatchedElements = new ArrayList<>();
final Clazz clazz = new Clazz();
final Integer number = 42;
test(filter)
.and().send(clazz, number, clazz).to(filter.getInputPort())
.and().receive(matchedElements).from(filter.getMatchedOutputPort())
.and().receive(mismatchedElements).from(filter.getMismatchedOutputPort())
test(filter).and()
.send(clazz, number, clazz).to(filter.getInputPort())
.start();
assertThat(matchedElements, contains(clazz, clazz));
assertThat(mismatchedElements, contains(number));
assertThat(filter.getMatchedOutputPort(), produces(clazz, clazz));
assertThat(filter.getMismatchedOutputPort(), produces(number));
}
@Test
public void filterShouldForwardSubTypes() {
final List<Clazz> results = new ArrayList<InstanceOfFilterTest.Clazz>();
final Object clazz = new SubClazz();
final SubClazz clazz = new SubClazz();
test(filter)
.and().send(clazz).to(filter.getInputPort())
.and().receive(results).from(filter.getMatchedOutputPort())
test(filter).and()
.send(clazz).to(filter.getInputPort()).and()
.start();
assertThat(results, contains(clazz));
assertThat(filter.getMatchedOutputPort(), produces(clazz));
}
@Test
public void filterShouldDropInvalidTypes() {
final List<Clazz> results = new ArrayList<InstanceOfFilterTest.Clazz>();
final Object object = new Object();
test(filter)
.and().send(object).to(filter.getInputPort())
.and().receive(results).from(filter.getMatchedOutputPort())
.start();
assertThat(results, is(empty()));
assertThat(filter.getMatchedOutputPort(), producesNothing());
}
@Test
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment