diff options
author | Carlo Zancanaro <carlo@zancanaro.id.au> | 2015-06-09 17:33:56 +1000 |
---|---|---|
committer | Carlo Zancanaro <carlo@zancanaro.id.au> | 2015-06-09 17:33:56 +1000 |
commit | dd9f72b94eb7b2c37061c80457e74e8d7ac3e18f (patch) | |
tree | 17ac650c0c4a5045b1cbf0ef5c194b0ea7f7acd3 /src/test | |
parent | 813e523e9e57dc38f81afc53340e216b948d87cf (diff) |
Add an ObjectGenerator<>, and related machinery (also a mapOf generator)
Diffstat (limited to 'src/test')
4 files changed, 123 insertions, 1 deletions
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<Integer> subObject; + + public MyObject(String string, int value, SubObject<Integer> subObject) { + this.string = string; + this.value = value; + this.subObject = subObject; + } + + @UseForGeneration + public MyObject(String string, SubObject<Integer> 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<MyObject> 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<T> { + public final Map<String, List<T>> obj; + + public SubObject(Map<String, List<T>> obj) { + this.obj = obj; + } + + public SubObject<T> add(SubObject<T> other) { + Map<String, List<T>> values = new HashMap<>(obj); + for (Map.Entry<String, List<T>> entry : other.obj.entrySet()) { + String key = entry.getKey(); + if (values.containsKey(key)) { + List<T> 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(); + } +} |