diff options
| author | Michael Williamson <mike@zwobble.org> | 2016-02-24 21:56:31 +0000 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2016-02-24 23:24:20 +0000 |
| commit | 56dad2617a86aeb83cf4f7f56519af621979851d (patch) | |
| tree | 618e572381fa70f868a03e020c61b38fee4b8028 /src/test/java/org/zwobble/mammoth/tests/DeepReflectionMatcher.java | |
| parent | cc66aa555d68eb33dfceb052e5dc37284e5080bd (diff) | |
Use Set instead of List for warnings
Diffstat (limited to 'src/test/java/org/zwobble/mammoth/tests/DeepReflectionMatcher.java')
| -rw-r--r-- | src/test/java/org/zwobble/mammoth/tests/DeepReflectionMatcher.java | 45 |
1 files changed, 41 insertions, 4 deletions
diff --git a/src/test/java/org/zwobble/mammoth/tests/DeepReflectionMatcher.java b/src/test/java/org/zwobble/mammoth/tests/DeepReflectionMatcher.java index 72a6529..8538534 100644 --- a/src/test/java/org/zwobble/mammoth/tests/DeepReflectionMatcher.java +++ b/src/test/java/org/zwobble/mammoth/tests/DeepReflectionMatcher.java @@ -4,17 +4,16 @@ import com.google.common.base.Joiner; import com.google.common.collect.ImmutableList; import com.google.common.collect.Iterables; import com.google.common.collect.Sets; -import org.hamcrest.Description; -import org.hamcrest.Matcher; -import org.hamcrest.Matchers; -import org.hamcrest.TypeSafeDiagnosingMatcher; +import org.hamcrest.*; import java.lang.reflect.Field; import java.lang.reflect.Modifier; import java.util.List; import java.util.Map; import java.util.Optional; +import java.util.Set; +import static com.google.common.collect.Iterables.any; import static com.google.common.collect.Iterables.filter; import static com.google.common.collect.Iterables.transform; import static java.util.Arrays.asList; @@ -35,11 +34,20 @@ public class DeepReflectionMatcher<T> extends TypeSafeDiagnosingMatcher<T> { return matchesSafely("", expected, item, mismatchDescription); } + private static boolean deepEquals(Object expected, Object actual) { + Description description = new Description.NullDescription(); + return matchesSafely("", expected, actual, description); + } + private static <T> boolean matchesSafely(String path, T expected, T actual, Description mismatchDescription) { if (expected instanceof List && actual instanceof List) { return matchesList(path, (List)expected, (List)actual, mismatchDescription); } + if (expected instanceof Set && actual instanceof Set) { + return matchesSet(path, (Set)expected, (Set)actual, mismatchDescription); + } + if (expected instanceof Map && actual instanceof Map) { return matchesMap(path, (Map)expected, (Map)actual, mismatchDescription); } @@ -89,6 +97,32 @@ public class DeepReflectionMatcher<T> extends TypeSafeDiagnosingMatcher<T> { return true; } + private static <T> boolean matchesSet(String path, Set<?> expected, Set<?> actual, Description mismatchDescription) { + List<?> missing = ImmutableList.copyOf(filter( + expected, + expectedElement -> !any(actual, actualElement -> deepEquals(expectedElement, actualElement)))); + + if (!missing.isEmpty()) { + appendPath(mismatchDescription, path); + mismatchDescription.appendText("missing elements:" + + indentedList(transform(missing, DeepReflectionMatcher::describeValue))); + return false; + } + + List<?> extra = ImmutableList.copyOf(filter( + actual, + actualElement -> !any(expected, expectedElement -> deepEquals(expectedElement, actualElement)))); + + if (!extra.isEmpty()) { + appendPath(mismatchDescription, path); + mismatchDescription.appendText("extra elements:" + + indentedList(transform(extra, DeepReflectionMatcher::describeValue))); + return false; + } + + return true; + } + private static <T> boolean matchesMap(String path, Map<?, ?> expected, Map<?, ?> actual, Description mismatchDescription) { if (!handleExtraElements(path, expected, actual, mismatchDescription, "extra elements:")) { return false; @@ -172,6 +206,9 @@ public class DeepReflectionMatcher<T> extends TypeSafeDiagnosingMatcher<T> { } else if (value instanceof List) { List<?> list = (List)value; return "[" + indentedList(transform(list, DeepReflectionMatcher::describeValue)) + "]"; + } else if (value instanceof Set) { + Set<?> list = (Set)value; + return "{" + indentedList(transform(list, DeepReflectionMatcher::describeValue)) + "}"; } else if (value instanceof Map) { Map<?, ?> map = (Map)value; String entries = indentedList( |
