### Question:
PostOrderPartialReducerDriver { public <E, N extends OpenNode<E, N>, R> R reduce(N node, BiFunction<N, Collection<R>, ReductionResult<R>> reducer) { return tryReduce(node, reducer).result(); }  R reduce(N node, BiFunction<N, Collection<R>, ReductionResult<R>> reducer);  }### Answer:
@SuppressWarnings("unchecked") @Test public void reduceNoResult() { when(function.apply(child0, Collections.emptyList())).thenReturn(ReductionResults.noResult()); when(function.apply(child1, Collections.emptyList())).thenReturn(ReductionResults.noResult()); when(function.apply(eq(parent), isA(Collection.class))).thenReturn(ReductionResults.noResult()); boolean exception = false; final PostOrderPartialReducerDriver driver = new PostOrderPartialReducerDriver(); try { driver.reduce(parent, function); } catch (IllegalStateException e) { exception = true; } verify(function).apply(eq(parent), resultsCaptor.capture()); final Collection<String> childResults = resultsCaptor.getValue(); assertEquals(0, childResults.size()); assertTrue(exception); }
@SuppressWarnings("unchecked") @Test public void reduce() { when(function.apply(child0, Collections.emptyList())).thenReturn(ReductionResults.result("a")); when(function.apply(child1, Collections.emptyList())).thenReturn(ReductionResults.result("b")); when(function.apply(eq(parent), isA(Collection.class))).thenReturn(ReductionResults.result("c")); final PostOrderPartialReducerDriver driver = new PostOrderPartialReducerDriver(); final String result = driver.reduce(parent, function); verify(function).apply(eq(parent), resultsCaptor.capture()); final Collection<String> childResults = resultsCaptor.getValue(); assertEquals(2, childResults.size()); assertThat(childResults, IsCollectionContaining.hasItems("a", "b")); assertEquals(result, "c"); }
@SuppressWarnings("unchecked") @Test public void reduceShortcut() { when(function.apply(child0, Collections.emptyList())).thenReturn(ReductionResults.haltAndResult("a")); final PostOrderPartialReducerDriver driver = new PostOrderPartialReducerDriver(); final String result = driver.reduce(parent, function); verify(function, never()).apply(eq(parent), isA(Collection.class)); verify(function, never()).apply(eq(child1), isA(Collection.class)); assertEquals(result, "a"); }