Skip to content

Commit

Permalink
added stream and iterable support
Browse files Browse the repository at this point in the history
  • Loading branch information
musketyr committed May 28, 2024
1 parent eafa357 commit b4484b1
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,15 @@ public Where3(Headers3 headers, Row3<A, B, C> first, Collection<Row3<A, B, C>> r
this.data.addAll(rest);
}

public Where3(Headers3 headers, Iterable<Row3<A, B, C>> rows) {
this.headers = headers;
rows.forEach(data::add);

if (data.isEmpty()) {
throw new IllegalArgumentException("No data provided. " + rows + " must contain at least one row.");
}
}

public Expectations expect(String template, Assertion3<A, B, C> verification) {
return new Expectations3<>(this, template, verification);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.agorapulse.testing.where4j.dsl;

import java.util.Iterator;
import java.util.stream.Stream;

public class Zip3<A, B, C> implements Iterable<Row3<A, B, C>>{

private final Iterator<A> iteratorA;
private final Iterator<B> iteratorB;
private final Iterator<C> iteratorC;

public Zip3(Iterator<A> valuesA, Iterator<B> valuesB, Iterator<C> valuesC) {
this.iteratorA = valuesA;
this.iteratorB = valuesB;
this.iteratorC = valuesC;
}

@Override
public Iterator<Row3<A, B, C>> iterator() {
return new Iterator<Row3<A, B, C>>() {
@Override
public boolean hasNext() {
return iteratorA.hasNext() && iteratorB.hasNext() && iteratorC.hasNext();
}

@Override
public Row3<A, B, C> next() {
return new Row3<>(iteratorA.next(), iteratorB.next(), iteratorC.next());
}
};
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

import org.junit.jupiter.api.TestFactory;

import java.util.Arrays;
import java.util.Collections;
import java.util.Objects;
import java.util.stream.Stream;

import static com.agorapulse.testing.where4j.Expectations.*;
import static org.junit.jupiter.api.Assertions.*;

Expand All @@ -21,6 +26,32 @@ Expectations basicTestWithThreeVariablesAndExplicitEvaluation() {
);
}

@TestFactory
Expectations basicTestWithStreams() {
Calculator calculator = new Calculator();

return forEach(
"a", Stream.of(2, 3, 4),
"b", Stream.of(3, 5, 7),
"c", Stream.of(5, 8, 11)
).expect("#a + #b = #c", (a, b, c) ->
calculator.add(a, b) == c
);
}

@TestFactory
Expectations basicTestWithIterables() {
Calculator calculator = new Calculator();

return forEach(
"a", Arrays.asList(2, 3, 4),
"b", Arrays.asList(3, 5, 7),
"c", Arrays.asList(5, 8, 11)
).expect("#a + #b = #c", (a, b, c) ->
calculator.add(a, b) == c
);
}

@TestFactory
Expectations basicTestWithThreeVariablesAndExplicitEvaluationWithMultipleLines() {
Calculator calculator = new Calculator();
Expand Down Expand Up @@ -51,4 +82,25 @@ Expectations basicTestWithThreeVariablesAndAssertion() {
);
}

@TestFactory
Expectations basicTestWithList() {
return forEach(
header("list", "a", "b"),
row(Arrays.asList(1, 2, 3), 1, 5),
row(Arrays.asList(1, 2, 3), 1, 1L)
).expect("#list contains #a but not #b", (list, a, b) ->
list.contains(a) && !list.contains(b)
);
}

@TestFactory
Expectations basicTestWithMap() {
return forEach(
header("map", "key", "value"),
row(Collections.singletonMap("foo", "bar"), "foo", "bar")
).expect("#map contains #key with value #value", (map, key, value) ->
map.containsKey(key) && Objects.equals(map.get(key), value)
);
}

}

0 comments on commit b4484b1

Please sign in to comment.