From 56dad2617a86aeb83cf4f7f56519af621979851d Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Wed, 24 Feb 2016 21:56:31 +0000 Subject: Use Set instead of List for warnings --- .../mammoth/tests/DeepReflectionMatcher.java | 45 ++++++++++++++++++++-- 1 file changed, 41 insertions(+), 4 deletions(-) (limited to 'src/test/java/org/zwobble/mammoth/tests/DeepReflectionMatcher.java') 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 extends TypeSafeDiagnosingMatcher { 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 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 extends TypeSafeDiagnosingMatcher { return true; } + private static 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 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 extends TypeSafeDiagnosingMatcher { } 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( -- cgit v1.2.3