Skip to content

Commit

Permalink
Add unit test for Bug Report in #7. Fix it according to the new appro…
Browse files Browse the repository at this point in the history
…ach based on tryAdvance().
  • Loading branch information
fmcarvalho committed Oct 30, 2020
1 parent 77e2391 commit cdea6ca
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 11 deletions.
16 changes: 5 additions & 11 deletions src/main/java/org/jayield/advs/AdvancerList.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,29 +20,23 @@
import org.jayield.Traverser;
import org.jayield.Yield;

import java.util.Iterator;
import java.util.List;
import java.util.Spliterator;

public class AdvancerList<U> implements Advancer<U>, Traverser<U> {
private final List<U> data;
private final Iterator<U> current;
private final Spliterator<U> current;

public AdvancerList(List<U> data) {
this.data = data;
this.current = data.iterator();
this.current = data.spliterator();
}

@Override
public void traverse(Yield<? super U> yield) {
if(!current.hasNext())
throw new IllegalStateException("Traverser has already been operated on or closed!");
data.forEach(yield::ret);
current.forEachRemaining(yield::ret);
}

@Override
public boolean tryAdvance(Yield<? super U> yield) {
if(!current.hasNext()) return false;
yield.ret(current.next());
return true;
return current.tryAdvance(yield::ret);
}
}
22 changes: 22 additions & 0 deletions src/test/java/org/jayield/QueryTraverseTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,28 @@ public void testReduce() {
assertEquals(actual, expected);
}

@Test
public void testFlatMapAndReduce() {
List<Query<String>> input = new ArrayList<>();
input.add(Query.of("a"));
input.add(Query.of("b"));
input.add(Query.of("c"));
String expected = "abc";
String actual = fromList(input).flatMap(s -> s).reduce((p, c) -> p + c).orElseThrow();
assertEquals(actual, expected);
}

@Test
public void testFromListFlatMapAndReduce() {
List<Query<String>> input = new ArrayList<>();
input.add(fromList(List.of("a")));
input.add(fromList(List.of("b")));
input.add(fromList(List.of("c")));
String expected = "abc";
String actual = fromList(input).flatMap(s -> s).reduce((p, c) -> p + c).orElseThrow();
assertEquals(actual, expected);
}

@Test
public void testReduceOnEmpty() {
String[] input = {};
Expand Down

0 comments on commit cdea6ca

Please sign in to comment.