From 60bc9218d0872e40a6857706d51955ee3d058717 Mon Sep 17 00:00:00 2001 From: Carlo Zancanaro Date: Wed, 10 Jun 2015 00:21:35 +1000 Subject: Support enums in the ObjectGeneration stuff --- .../id/zancanaro/javacheck/object/GeneratorProvider.java | 7 ++++++- .../id/zancanaro/javacheck/object/MyObjectAddTest.java | 1 + .../java/au/id/zancanaro/javacheck/object/SubObject.java | 16 +++++++++++----- 3 files changed, 18 insertions(+), 6 deletions(-) (limited to 'src') diff --git a/src/main/java/au/id/zancanaro/javacheck/object/GeneratorProvider.java b/src/main/java/au/id/zancanaro/javacheck/object/GeneratorProvider.java index 24b73d5..827ba3f 100644 --- a/src/main/java/au/id/zancanaro/javacheck/object/GeneratorProvider.java +++ b/src/main/java/au/id/zancanaro/javacheck/object/GeneratorProvider.java @@ -88,7 +88,12 @@ public interface GeneratorProvider { provider.getGenerator(keyType, new Annotation[0], provider), provider.getGenerator(valueType, new Annotation[0], provider)); } else if (type instanceof Class) { - return ofType((Class) type, provider); + Class clazz = (Class) type; + if (Enum.class.isAssignableFrom(clazz)) { + return elements(clazz.getEnumConstants()); + } else { + return ofType((Class) type, provider); + } } else if (type instanceof ParameterizedType) { ParameterizedType param = (ParameterizedType) type; if (param.getRawType() instanceof Class) { 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 9321acb..884114c 100644 --- a/src/test/java/au/id/zancanaro/javacheck/object/MyObjectAddTest.java +++ b/src/test/java/au/id/zancanaro/javacheck/object/MyObjectAddTest.java @@ -16,6 +16,7 @@ public class MyObjectAddTest { 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)); + assertEquals(added.subObject.value, a.subObject.value); } } diff --git a/src/test/java/au/id/zancanaro/javacheck/object/SubObject.java b/src/test/java/au/id/zancanaro/javacheck/object/SubObject.java index bdf2ce8..1e49cfe 100644 --- a/src/test/java/au/id/zancanaro/javacheck/object/SubObject.java +++ b/src/test/java/au/id/zancanaro/javacheck/object/SubObject.java @@ -4,9 +4,11 @@ import java.util.*; public class SubObject { public final Map> obj; + public final EnumValues value; - public SubObject(Map> obj) { + public SubObject(Map> obj, EnumValues value) { this.obj = obj; + this.value = value; } public SubObject add(SubObject other) { @@ -21,12 +23,12 @@ public class SubObject { values.put(key, entry.getValue()); } } - return new SubObject<>(Collections.unmodifiableMap(values)); + return new SubObject<>(Collections.unmodifiableMap(values), this.value); } @Override public String toString() { - return obj.toString(); + return "{" + obj.toString() + ", " + this.value + "}"; } @Override @@ -34,11 +36,15 @@ public class SubObject { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; SubObject subObject = (SubObject) o; - return obj.equals(subObject.obj); + return obj.equals(subObject.obj) && value.equals(subObject.value); } @Override public int hashCode() { - return obj.hashCode(); + return obj.hashCode() + 37 * value.hashCode(); + } + + public static enum EnumValues { + A, B, C } } -- cgit v1.2.3