From 7db38e2e4f2c24c2a21d1c19c12bfdad4727ebad Mon Sep 17 00:00:00 2001 From: Carlo Zancanaro Date: Mon, 1 Jun 2015 16:41:07 +1000 Subject: Make listOf able to take a min/max length of list --- src/main/java/au/id/zancanaro/javacheck/Generators.java | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'src/main') diff --git a/src/main/java/au/id/zancanaro/javacheck/Generators.java b/src/main/java/au/id/zancanaro/javacheck/Generators.java index bf1b1b3..eab3eed 100644 --- a/src/main/java/au/id/zancanaro/javacheck/Generators.java +++ b/src/main/java/au/id/zancanaro/javacheck/Generators.java @@ -80,6 +80,19 @@ public final class Generators { }; } + public static Generator> listOf(Generator gen, int minElements, int maxElements) { + return (random, size) -> { + Generator countGen = sized(s -> integer(minElements, maxElements)); + int count = countGen.generate(random, size).getValue(); + return Generator.list(count, gen).generate(random, size) + .filter(list -> + minElements <= list.size() + && list.size() < maxElements); + }; + } + + ; + public static Generator> listOf(Generator gen) { return (random, size) -> { Generator countGen = sized(s -> integer(0, s)); -- cgit v1.2.3