diff options
author | Carlo Zancanaro <carlo@zancanaro.id.au> | 2015-06-02 18:10:13 +1000 |
---|---|---|
committer | Carlo Zancanaro <carlo@zancanaro.id.au> | 2015-06-02 18:10:13 +1000 |
commit | 27fcf37206591c774d79ec60de8d404ed83378ac (patch) | |
tree | 26e71761085a282bc9f31dad4f9624c86ea3aac4 /src/main | |
parent | 140b4d41d695f32e75c785f6179430f677d244ae (diff) |
Fix up some static analysis warnings and improve @DataPoint detection
Diffstat (limited to 'src/main')
6 files changed, 54 insertions, 43 deletions
diff --git a/src/main/java/au/id/zancanaro/javacheck/Generator.java b/src/main/java/au/id/zancanaro/javacheck/Generator.java index 4b9113c..c28d967 100644 --- a/src/main/java/au/id/zancanaro/javacheck/Generator.java +++ b/src/main/java/au/id/zancanaro/javacheck/Generator.java @@ -19,6 +19,7 @@ import java.util.function.Function; * * @param <T> The type generated by this generator. */ +@SuppressWarnings("unused") public interface Generator<T> { /** * Return a {@link RoseTree} containing a new random value of the required @@ -115,9 +116,9 @@ public interface Generator<T> { /** * Produce a new generator relying on the value generated by this generator * - * Shrinking is a bit hard to predict under {@link #flatMap}, as it will - * first attempt to shrink this, resulting in the re-evaluation of action, - * and hence the re-generation of the subtree. + * Shrinking is a bit hard to predict under flatMap, as it will first + * attempt to shrink this, resulting in the re-evaluation of action, and + * hence the re-generation of the subtree. * * @param action A function to produce the new generator * @param <R> The type of the returned generator diff --git a/src/main/java/au/id/zancanaro/javacheck/Generators.java b/src/main/java/au/id/zancanaro/javacheck/Generators.java index eab3eed..548bd29 100644 --- a/src/main/java/au/id/zancanaro/javacheck/Generators.java +++ b/src/main/java/au/id/zancanaro/javacheck/Generators.java @@ -7,6 +7,7 @@ import java.util.List; import java.util.function.Function; import java.util.function.Predicate; +@SuppressWarnings("unused") public final class Generators { private Generators() { } @@ -26,6 +27,12 @@ public final class Generators { }; } + public static <T> Generator<T> noShrink(Generator<T> gen) { + return (random, size) -> new RoseTree<>( + gen.generate(random, size).getValue(), + Collections.emptyList()); + } + @SafeVarargs public static <T> Generator<T> oneOf(Generator<T>... gens) { return integer(0, gens.length).flatMap(index -> gens[index]); @@ -91,8 +98,6 @@ public final class Generators { }; } - ; - public static <T> Generator<List<T>> listOf(Generator<T> gen) { return (random, size) -> { Generator<Integer> countGen = sized(s -> integer(0, s)); diff --git a/src/main/java/au/id/zancanaro/javacheck/Iterators.java b/src/main/java/au/id/zancanaro/javacheck/Iterators.java index 33652b2..3f365eb 100644 --- a/src/main/java/au/id/zancanaro/javacheck/Iterators.java +++ b/src/main/java/au/id/zancanaro/javacheck/Iterators.java @@ -95,6 +95,7 @@ public final class Iterators { } + @SuppressWarnings("WeakerAccess") public static <T> EmptyIterator<T> emptyIterator() { return new EmptyIterator<>(); } diff --git a/src/main/java/au/id/zancanaro/javacheck/RoseTree.java b/src/main/java/au/id/zancanaro/javacheck/RoseTree.java index 5a54d6f..c735b46 100644 --- a/src/main/java/au/id/zancanaro/javacheck/RoseTree.java +++ b/src/main/java/au/id/zancanaro/javacheck/RoseTree.java @@ -6,6 +6,7 @@ import java.util.*; import java.util.function.Function; import java.util.function.Predicate; +@SuppressWarnings("unused") public class RoseTree<T> { private final T value; private final Iterable<RoseTree<T>> children; @@ -49,14 +50,17 @@ public class RoseTree<T> { )); } - public static <T, R> RoseTree<R> zip(Function<List<T>, R> fn, RoseTree<T>[] trees) { - @SuppressWarnings("unchecked") - List<T> heads = new ArrayList(trees.length); + private static <T> List<T> makeHeadList(RoseTree<T>[] trees) { + List<T> heads = new ArrayList<>(trees.length); for (RoseTree<T> tree : trees) { heads.add(tree.getValue()); } + return heads; + } + + public static <T, R> RoseTree<R> zip(Function<List<T>, R> fn, RoseTree<T>[] trees) { return new RoseTree<>( - fn.apply(heads), + fn.apply(makeHeadList(trees)), () -> Iterators.mappingIterator( roses -> RoseTree.zip(fn, roses), RoseTree.permutations(trees))); @@ -76,13 +80,8 @@ public class RoseTree<T> { } public static <T, R> RoseTree<R> shrink(Function<List<T>, R> fn, RoseTree<T>[] trees) { - @SuppressWarnings("unchecked") - List<T> heads = new ArrayList(trees.length); - for (RoseTree<T> tree : trees) { - heads.add(tree.getValue()); - } return new RoseTree<>( - fn.apply(heads), + fn.apply(makeHeadList(trees)), () -> Iterators.mappingIterator( roses -> RoseTree.shrink(fn, roses), RoseTree.removeEach(trees))); diff --git a/src/main/java/au/id/zancanaro/javacheck/junit/Properties.java b/src/main/java/au/id/zancanaro/javacheck/junit/Properties.java index 6ff0538..1e3f502 100644 --- a/src/main/java/au/id/zancanaro/javacheck/junit/Properties.java +++ b/src/main/java/au/id/zancanaro/javacheck/junit/Properties.java @@ -16,6 +16,7 @@ import org.junit.runners.model.TestClass; import java.lang.reflect.*; import java.util.*; +@SuppressWarnings("WeakerAccess") public class Properties extends BlockJUnit4ClassRunner { private final Map<Type, Generator<?>> generators = new HashMap<>(); @@ -35,32 +36,36 @@ public class Properties extends BlockJUnit4ClassRunner { Field[] fields = getTestClass().getJavaClass().getDeclaredFields(); for (Field field : fields) { - if (!field.isAnnotationPresent(DataSource.class)) { - continue; - } - Type type = field.getGenericType(); - if (!(type instanceof ParameterizedType)) { - continue; - } - ParameterizedType parameterizedType = (ParameterizedType) type; - if (!(parameterizedType.getRawType() instanceof Class)) { - continue; - } - Class<?> c = (Class) parameterizedType.getRawType(); - if (c != Generator.class) { - continue; - } - boolean error = false; - if (!Modifier.isStatic(field.getModifiers())) { - errors.add(new Error("Generator field " + field.getName() + " must be static")); - error = true; - } - if (!Modifier.isPublic(field.getModifiers())) { - errors.add(new Error("Generator field " + field.getName() + " must be public")); - error = true; - } - if (!error) { - result.add(parameterizedType.getActualTypeArguments()[0]); + if (field.isAnnotationPresent(DataSource.class)) { + boolean error = false; + if (!Modifier.isStatic(field.getModifiers())) { + errors.add(new Error("@DataSource field " + field.getName() + " must be static")); + error = true; + } + if (!Modifier.isPublic(field.getModifiers())) { + errors.add(new Error("@DataSource field " + field.getName() + " must be public")); + error = true; + } + + Type type = field.getGenericType(); + ParameterizedType parameterizedType;; + if (type instanceof ParameterizedType) { + parameterizedType = (ParameterizedType) type; + if (parameterizedType.getRawType() instanceof Class) { + Class<?> c = (Class) parameterizedType.getRawType(); + if (c == Generator.class) { + if (!error) { + result.add(parameterizedType.getActualTypeArguments()[0]); + } + } else { + errors.add(new Error("@DataSource fields must be of type Generator<T>")); + } + } else { + errors.add(new Error("@DataSource fields must be of type Generator<T>")); + } + } else { + errors.add(new Error("@DataSource fields must be of type Generator<T>")); + } } } return result; diff --git a/src/main/java/au/id/zancanaro/javacheck/junit/PropertyError.java b/src/main/java/au/id/zancanaro/javacheck/junit/PropertyError.java index 2cd1325..44c071b 100644 --- a/src/main/java/au/id/zancanaro/javacheck/junit/PropertyError.java +++ b/src/main/java/au/id/zancanaro/javacheck/junit/PropertyError.java @@ -5,6 +5,7 @@ import au.id.zancanaro.javacheck.ShrinkResult; import java.util.Arrays; import java.util.Iterator; +@SuppressWarnings("WeakerAccess") public class PropertyError extends AssertionError { public PropertyError(String methodName, long seed, ShrinkResult shrunk) { super(shrunk.thrown.getMessage() == null ? @@ -18,8 +19,7 @@ public class PropertyError extends AssertionError { initCause(shrunk.thrown); } - - public static String joinArgs(Object... params) { + private static String joinArgs(Object... params) { StringBuilder sb = new StringBuilder(); Iterator<Object> iterator = Arrays.asList(params).iterator(); while (iterator.hasNext()) { |