From 40961d4950c40643d5d71721a7e024e3951323ce Mon Sep 17 00:00:00 2001 From: Carlo Zancanaro Date: Tue, 9 Jun 2015 23:31:54 +1000 Subject: Generalise the ObjectGeneration stuff The new ObjectGeneration stuff is now used to generate everything for a test case, which means it's all unified and "nice" now. Add a @UseGenerator annotation to be used to specify how to generate specific field values. Obviously, not everything can be generated magically, so if you specify a @DataSource in your test then it will be used in preference to any magically generated value. --- .../zancanaro/javacheck/ListFunctorRulesTest.java | 6 ++---- .../javacheck/SimpleListOperationsTest.java | 8 -------- .../au/id/zancanaro/javacheck/object/MyObject.java | 21 ++++++++++++++++++++- .../zancanaro/javacheck/object/MyObjectAddTest.java | 8 -------- .../javacheck/state/queue/commands/Offer.java | 1 - 5 files changed, 22 insertions(+), 22 deletions(-) (limited to 'src/test/java/au/id') diff --git a/src/test/java/au/id/zancanaro/javacheck/ListFunctorRulesTest.java b/src/test/java/au/id/zancanaro/javacheck/ListFunctorRulesTest.java index b757bcd..4100378 100644 --- a/src/test/java/au/id/zancanaro/javacheck/ListFunctorRulesTest.java +++ b/src/test/java/au/id/zancanaro/javacheck/ListFunctorRulesTest.java @@ -9,7 +9,8 @@ import java.util.List; import java.util.function.Function; import java.util.stream.Collectors; -import static au.id.zancanaro.javacheck.Generators.*; +import static au.id.zancanaro.javacheck.Generators.longInteger; +import static au.id.zancanaro.javacheck.Generators.oneOf; import static org.junit.Assert.assertEquals; @RunWith(Properties.class) @@ -18,9 +19,6 @@ public class ListFunctorRulesTest { private final static int runs = 1000; private final static int maxSize = 1000; - @DataSource - public static Generator> listOfIntegers = listOf(longInteger()); - @DataSource public static Generator> integerFunction = oneOf( diff --git a/src/test/java/au/id/zancanaro/javacheck/SimpleListOperationsTest.java b/src/test/java/au/id/zancanaro/javacheck/SimpleListOperationsTest.java index fcc8baf..3f4d58b 100644 --- a/src/test/java/au/id/zancanaro/javacheck/SimpleListOperationsTest.java +++ b/src/test/java/au/id/zancanaro/javacheck/SimpleListOperationsTest.java @@ -1,6 +1,5 @@ package au.id.zancanaro.javacheck; -import au.id.zancanaro.javacheck.annotations.DataSource; import au.id.zancanaro.javacheck.annotations.Property; import au.id.zancanaro.javacheck.junit.Properties; import org.junit.runner.RunWith; @@ -8,7 +7,6 @@ import org.junit.runner.RunWith; import java.util.ArrayList; import java.util.List; -import static au.id.zancanaro.javacheck.Generators.*; import static java.util.Collections.reverse; import static java.util.Collections.sort; import static org.junit.Assert.assertEquals; @@ -19,12 +17,6 @@ public class SimpleListOperationsTest { private final static int runs = 1000; private final static int maxSize = 1000; - @DataSource - public static Generator> listOfIntegers = listOf(integer()); - - @DataSource - public static Generator> listOfStrings = listOf(stringOf(alphaNumericCharacter())); - @Property(maxSize = maxSize, runs = runs) public void sortingIsIdempotent(List list) { List left = new ArrayList<>(list); diff --git a/src/test/java/au/id/zancanaro/javacheck/object/MyObject.java b/src/test/java/au/id/zancanaro/javacheck/object/MyObject.java index ac0c370..824fd8d 100644 --- a/src/test/java/au/id/zancanaro/javacheck/object/MyObject.java +++ b/src/test/java/au/id/zancanaro/javacheck/object/MyObject.java @@ -1,5 +1,13 @@ package au.id.zancanaro.javacheck.object; +import au.id.zancanaro.javacheck.Generator; +import au.id.zancanaro.javacheck.ShrinkTree; + +import java.util.Random; + +import static au.id.zancanaro.javacheck.Generators.asciiCharacter; +import static au.id.zancanaro.javacheck.Generators.stringOf; + public class MyObject { public final String string; public final int value; @@ -12,10 +20,21 @@ public class MyObject { } @UseForGeneration - public MyObject(String string, SubObject subObject) { + public MyObject( + @UseGenerator(StringGenerator.class) String string, + SubObject subObject) { this(string, string.length(), subObject); } + public static class StringGenerator implements Generator { + public final Generator gen = stringOf(asciiCharacter()); + + @Override + public ShrinkTree generate(Random random, int size) { + return gen.generate(random, size); + } + } + public MyObject add(MyObject other) { return new MyObject( this.string + other.string, diff --git a/src/test/java/au/id/zancanaro/javacheck/object/MyObjectAddTest.java b/src/test/java/au/id/zancanaro/javacheck/object/MyObjectAddTest.java index 7965449..9321acb 100644 --- a/src/test/java/au/id/zancanaro/javacheck/object/MyObjectAddTest.java +++ b/src/test/java/au/id/zancanaro/javacheck/object/MyObjectAddTest.java @@ -1,23 +1,15 @@ package au.id.zancanaro.javacheck.object; -import au.id.zancanaro.javacheck.Generator; -import au.id.zancanaro.javacheck.annotations.DataSource; import au.id.zancanaro.javacheck.annotations.Property; import au.id.zancanaro.javacheck.junit.Properties; import org.junit.runner.RunWith; -import java.util.Collections; - -import static au.id.zancanaro.javacheck.Generators.ofType; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; @RunWith(Properties.class) public class MyObjectAddTest { - @DataSource - public static Generator source = ofType(MyObject.class); - @Property public void testAdd(MyObject a, MyObject b) { MyObject added = a.add(b); diff --git a/src/test/java/au/id/zancanaro/javacheck/state/queue/commands/Offer.java b/src/test/java/au/id/zancanaro/javacheck/state/queue/commands/Offer.java index 301efeb..366e974 100644 --- a/src/test/java/au/id/zancanaro/javacheck/state/queue/commands/Offer.java +++ b/src/test/java/au/id/zancanaro/javacheck/state/queue/commands/Offer.java @@ -7,7 +7,6 @@ import au.id.zancanaro.javacheck.state.queue.QueueState; import java.util.ArrayList; import java.util.List; -import java.util.Queue; public class Offer extends Command, T, Void> { public final Generator generator; -- cgit v1.2.3