FilteredGenerator implements Gen<T> { @Override public T generate(RandomnessSource in) { while(true) { DetatchedRandomnessSource detached = in.detach(); T t = child.generate(detached); if (assumption.test(t)) { detached.commit(); return t; } else { detached.registerFailedAssumption(); } } } FilteredGenerator(Gen<T> child, Predicate<T> assumption); @Override T generate(RandomnessSource in); String asString(T t);  }
@Test public void limitsValuesByAssumptions() { Gen<Integer> ints = Sequence.of(1,2,3,4) .assuming(i -> i != 3); Stream<Integer> actual = generate(ints); assertThat(actual.limit(3)).containsExactly(1,2,4); }
@Test public void reportsAssumptionFailure() { Gen<Integer> ints = Sequence.of(1,2,3,4) .assuming(i -> i != 3); generate(ints).limit(3).forEach(consume()); verify(source).registerFailedAssumption(); }
@Test public void reportsNoAssumptionFailureWhenNoneOccurs() { Gen<Integer> ints = Sequence.of(1,2,3,4); generate(ints).limit(3).forEach(consume()); verify(source, never()).registerFailedAssumption(); }
@Test public void mapsContentsWithFunction() { testee = Sequence.of(1,2,3,4,5) .map(i -> "" + (i + 1)); Stream<String> actual = generate(testee); assertThat(actual.limit(5)).containsExactly("2","3","4","5","6"); }
@Test public void mutatesContentsWithMod() { when(this.source.next(Constraint.none())).thenReturn(11L); Mod<String,String> m = (i,r) -> i + r.next(Constraint.none()); testee = Sequence.of("1","2","3").mutate(m); Stream<String> actual = generate(testee); assertThat(actual.limit(3)).containsExactly("111", "211","311"); }
@Test public void mapsContentsWithBiFunction() { testee = Sequence.of(1,2,3,4,5,6) .map( (a,b) -> "" + (a + b)); Stream<String> actual = generate(testee); assertThat(actual.limit(3)).containsExactly("3","7","11"); }
@Test public void mapsContentsWithFunction3() { testee = Sequence.of(1,2,3,4,5,6,7,8,9) .map( (a,b,c) -> "" + (a + b + c)); Stream<String> actual = generate(testee); assertThat(actual.limit(3)).containsExactly("6","15","24"); }
@Test public void combinesWithOtherGenUsingBiFunction() { Gen<Integer> as = Sequence.of(1,2,3,4); Gen<Integer> bs = Sequence.of(2,4,6,8); Gen<Integer> combined = as.zip(bs, (a,b) -> a + b); Stream<Integer> actual = generate(combined).limit(4); assertThat(actual).containsExactly(3,6,9,12); }
@Test public void combinesWithOtherGenUsingFunction3() { Gen<Integer> as = Sequence.of(1,2,3,4); Gen<Integer> bs = Sequence.of(2,4,6,8); Gen<Integer> cs = Sequence.of(3,6,9,12); Gen<Integer> combined = as.zip(bs, cs, (a,b,c) -> a + b + c); Stream<Integer> actual = generate(combined).limit(4); assertThat(actual).containsExactly(6,12,18,24); }
@Test public void combinesWithOtherGenUsingFunction4() { Gen<Integer> as = Sequence.of(1,2,3,4); Gen<Integer> bs = Sequence.of(2,4,6,8); Gen<Integer> cs = Sequence.of(3,6,9,12); Gen<Integer> ds = Sequence.of(1,3,5,7); Gen<Integer> combined = as.zip(bs, cs, ds, (a,b,c,d) -> a + b + c + d); Stream<Integer> actual = generate(combined).limit(4); assertThat(actual).containsExactly(7,15,23,31); }
@Test public void combinesWithOtherGenUsingFunction5() { Gen<Integer> as = Sequence.of(1,2,3,4); Gen<Integer> bs = Sequence.of(1,2,3,4); Gen<Integer> cs = Sequence.of(1,2,3,4); Gen<Integer> ds = Sequence.of(1,2,3,4); Gen<Integer> es = Sequence.of(1,2,3,4); Gen<Integer> combined = as.zip(bs, cs, ds, es, (a,b,c,d, e) -> a + b + c + d + e); Stream<Integer> actual = generate(combined).limit(4); assertThat(actual).containsExactly(5,10,15,20); }
@Test public void mixesValuesRandomlyWithOtherGens() { Gen<Integer> as = Sequence.of(1,2,3); Gen<Integer> bs = Sequence.of(2,4,6); Gen<Integer> mixed = as.mix(bs); when(source.next(any(Constraint.class))) .thenReturn(0L, 99L, 0L); Stream<Integer> actual = generate(mixed).limit(3); assertThat(actual).containsExactly(2,1,4); }
@SuppressWarnings("unchecked") @Test public void mapsToOptionalsWithoutEmptyWhenPercentageIs0() { Gen<Optional<Integer>> testee = Sequence.of(1,2,3).toOptionals(0); Stream<Optional<Integer>> actual = generate(testee).limit(3); assertThat(actual).containsExactly(Optional.of(1), Optional.of(2), Optional.of(3)); }
@SuppressWarnings("unchecked") @Test public void mapsToOnlyEmptyOptionalsWhenPercentageIs100() { Gen<Optional<Integer>> testee = Sequence.of(1,2,3).toOptionals(100); Stream<Optional<Integer>> actual = generate(testee).limit(3); assertThat(actual).containsExactly(Optional.empty(), Optional.empty(), Optional.empty()); }