summaryrefslogtreecommitdiff
path: root/src/test/java/au/id/zancanaro/javacheck/object
diff options
context:
space:
mode:
authorCarlo Zancanaro <carlo@zancanaro.id.au>2015-06-09 23:31:54 +1000
committerCarlo Zancanaro <carlo@zancanaro.id.au>2015-06-09 23:31:54 +1000
commit40961d4950c40643d5d71721a7e024e3951323ce (patch)
tree330af94a98468ab0c10cff76c2bb36eb28a64ef0 /src/test/java/au/id/zancanaro/javacheck/object
parentdd9f72b94eb7b2c37061c80457e74e8d7ac3e18f (diff)
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.
Diffstat (limited to 'src/test/java/au/id/zancanaro/javacheck/object')
-rw-r--r--src/test/java/au/id/zancanaro/javacheck/object/MyObject.java21
-rw-r--r--src/test/java/au/id/zancanaro/javacheck/object/MyObjectAddTest.java8
2 files changed, 20 insertions, 9 deletions
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<Integer> subObject) {
+ public MyObject(
+ @UseGenerator(StringGenerator.class) String string,
+ SubObject<Integer> subObject) {
this(string, string.length(), subObject);
}
+ public static class StringGenerator implements Generator<String> {
+ public final Generator<String> gen = stringOf(asciiCharacter());
+
+ @Override
+ public ShrinkTree<String> 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<MyObject> source = ofType(MyObject.class);
-
@Property
public void testAdd(MyObject a, MyObject b) {
MyObject added = a.add(b);