From dd9f72b94eb7b2c37061c80457e74e8d7ac3e18f Mon Sep 17 00:00:00 2001 From: Carlo Zancanaro Date: Tue, 9 Jun 2015 17:33:56 +1000 Subject: Add an ObjectGenerator<>, and related machinery (also a mapOf generator) --- .../javacheck/SimpleListOperationsTest.java | 1 - .../au/id/zancanaro/javacheck/object/MyObject.java | 50 ++++++++++++++++++++++ .../javacheck/object/MyObjectAddTest.java | 29 +++++++++++++ .../id/zancanaro/javacheck/object/SubObject.java | 44 +++++++++++++++++++ 4 files changed, 123 insertions(+), 1 deletion(-) create mode 100644 src/test/java/au/id/zancanaro/javacheck/object/MyObject.java create mode 100644 src/test/java/au/id/zancanaro/javacheck/object/MyObjectAddTest.java create mode 100644 src/test/java/au/id/zancanaro/javacheck/object/SubObject.java (limited to 'src/test') diff --git a/src/test/java/au/id/zancanaro/javacheck/SimpleListOperationsTest.java b/src/test/java/au/id/zancanaro/javacheck/SimpleListOperationsTest.java index 70b2425..fcc8baf 100644 --- a/src/test/java/au/id/zancanaro/javacheck/SimpleListOperationsTest.java +++ b/src/test/java/au/id/zancanaro/javacheck/SimpleListOperationsTest.java @@ -6,7 +6,6 @@ import au.id.zancanaro.javacheck.junit.Properties; import org.junit.runner.RunWith; import java.util.ArrayList; -import java.util.Collections; import java.util.List; import static au.id.zancanaro.javacheck.Generators.*; diff --git a/src/test/java/au/id/zancanaro/javacheck/object/MyObject.java b/src/test/java/au/id/zancanaro/javacheck/object/MyObject.java new file mode 100644 index 0000000..ac0c370 --- /dev/null +++ b/src/test/java/au/id/zancanaro/javacheck/object/MyObject.java @@ -0,0 +1,50 @@ +package au.id.zancanaro.javacheck.object; + +public class MyObject { + public final String string; + public final int value; + public final SubObject subObject; + + public MyObject(String string, int value, SubObject subObject) { + this.string = string; + this.value = value; + this.subObject = subObject; + } + + @UseForGeneration + public MyObject(String string, SubObject subObject) { + this(string, string.length(), subObject); + } + + public MyObject add(MyObject other) { + return new MyObject( + this.string + other.string, + this.value + other.value, + this.subObject.add(other.subObject)); + } + + @Override + public String toString() { + return "{" + string + ", " + value + ", " + subObject + "}"; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + MyObject myObject = (MyObject) o; + + return value == myObject.value + && string.equals(myObject.string) + && subObject.equals(myObject.subObject); + } + + @Override + public int hashCode() { + int result = string.hashCode(); + result = 31 * result + value; + result = 31 * result + subObject.hashCode(); + return result; + } +} diff --git a/src/test/java/au/id/zancanaro/javacheck/object/MyObjectAddTest.java b/src/test/java/au/id/zancanaro/javacheck/object/MyObjectAddTest.java new file mode 100644 index 0000000..7965449 --- /dev/null +++ b/src/test/java/au/id/zancanaro/javacheck/object/MyObjectAddTest.java @@ -0,0 +1,29 @@ +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); + assertTrue(added.string.length() == a.string.length() + b.string.length()); + assertTrue(added.value == a.value + b.value); + assertEquals(added.subObject, a.subObject.add(b.subObject)); + } + +} diff --git a/src/test/java/au/id/zancanaro/javacheck/object/SubObject.java b/src/test/java/au/id/zancanaro/javacheck/object/SubObject.java new file mode 100644 index 0000000..bdf2ce8 --- /dev/null +++ b/src/test/java/au/id/zancanaro/javacheck/object/SubObject.java @@ -0,0 +1,44 @@ +package au.id.zancanaro.javacheck.object; + +import java.util.*; + +public class SubObject { + public final Map> obj; + + public SubObject(Map> obj) { + this.obj = obj; + } + + public SubObject add(SubObject other) { + Map> values = new HashMap<>(obj); + for (Map.Entry> entry : other.obj.entrySet()) { + String key = entry.getKey(); + if (values.containsKey(key)) { + List result = new ArrayList<>(values.get(key)); + result.addAll(entry.getValue()); + values.put(key, Collections.unmodifiableList(result)); + } else { + values.put(key, entry.getValue()); + } + } + return new SubObject<>(Collections.unmodifiableMap(values)); + } + + @Override + public String toString() { + return obj.toString(); + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + SubObject subObject = (SubObject) o; + return obj.equals(subObject.obj); + } + + @Override + public int hashCode() { + return obj.hashCode(); + } +} -- cgit v1.2.3