diff options
Diffstat (limited to 'src/main/java/au')
-rw-r--r-- | src/main/java/au/id/zancanaro/javacheck/Generators.java | 11 | ||||
-rw-r--r-- | src/main/java/au/id/zancanaro/javacheck/object/GeneratorProvider.java | 6 |
2 files changed, 17 insertions, 0 deletions
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 <T> Generator<Set<T>> setOf(Generator<T> gen) { + return (random, size) -> { + Generator<Integer> 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 <K, V> Generator<Map<K, V>> mapOf(Generator<K> keyGen, Generator<V> 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]; |