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. --- .../au/id/zancanaro/javacheck/object/MyObject.java | 21 ++++++++++++++++++++- .../zancanaro/javacheck/object/MyObjectAddTest.java | 8 -------- 2 files changed, 20 insertions(+), 9 deletions(-) (limited to 'src/test/java/au/id/zancanaro/javacheck/object') 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); -- cgit v1.2.3