From 8376ed30f83c9193025da5962de5f5d78edfa185 Mon Sep 17 00:00:00 2001 From: Carlo Zancanaro Date: Tue, 24 Nov 2015 12:57:27 +1100 Subject: Add an implicit generator for Sets --- src/main/java/au/id/zancanaro/javacheck/Generators.java | 11 +++++++++++ .../au/id/zancanaro/javacheck/object/GeneratorProvider.java | 6 ++++++ 2 files changed, 17 insertions(+) (limited to 'src/main/java/au') diff --git a/src/main/java/au/id/zancanaro/javacheck/Generators.java b/src/main/java/au/id/zancanaro/javacheck/Generators.java index af8f8ad..631e14a 100644 --- a/src/main/java/au/id/zancanaro/javacheck/Generators.java +++ b/src/main/java/au/id/zancanaro/javacheck/Generators.java @@ -179,6 +179,17 @@ public final class Generators { }; } + public static Generator> setOf(Generator gen) { + return (random, size) -> { + Generator countGen = sized(s -> integer(0, s)); + int count = countGen.generate(random, size).getValue(); + return Generator.list(count, gen) + .generate(random, size) + .map(HashSet::new) + .map(Collections::unmodifiableSet); + }; + } + @SuppressWarnings("unchecked") public static Generator> mapOf(Generator keyGen, Generator valueGen) { return (random, size) -> { diff --git a/src/main/java/au/id/zancanaro/javacheck/object/GeneratorProvider.java b/src/main/java/au/id/zancanaro/javacheck/object/GeneratorProvider.java index 827ba3f..c975933 100644 --- a/src/main/java/au/id/zancanaro/javacheck/object/GeneratorProvider.java +++ b/src/main/java/au/id/zancanaro/javacheck/object/GeneratorProvider.java @@ -11,6 +11,7 @@ import java.lang.reflect.TypeVariable; import java.util.List; import java.util.Map; import java.util.Objects; +import java.util.Set; import static au.id.zancanaro.javacheck.Generators.*; @@ -81,6 +82,11 @@ public interface GeneratorProvider { List.class.getTypeParameters()[0], new Annotation[0], provider)); + } else if (type == Set.class) { + return setOf(provider.getGenerator( + Set.class.getTypeParameters()[0], + new Annotation[0], + provider)); } else if (type == Map.class) { TypeVariable[] params = Map.class.getTypeParameters(); TypeVariable keyType = params[0], valueType = params[1]; -- cgit v1.2.3