aboutsummaryrefslogtreecommitdiff
path: root/src/test/java/org/zwobble/mammoth/tests/DeepReflectionMatcher.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/test/java/org/zwobble/mammoth/tests/DeepReflectionMatcher.java')
-rw-r--r--src/test/java/org/zwobble/mammoth/tests/DeepReflectionMatcher.java24
1 files changed, 24 insertions, 0 deletions
diff --git a/src/test/java/org/zwobble/mammoth/tests/DeepReflectionMatcher.java b/src/test/java/org/zwobble/mammoth/tests/DeepReflectionMatcher.java
index 8ee0d8f..54b74ee 100644
--- a/src/test/java/org/zwobble/mammoth/tests/DeepReflectionMatcher.java
+++ b/src/test/java/org/zwobble/mammoth/tests/DeepReflectionMatcher.java
@@ -11,6 +11,7 @@ import org.hamcrest.TypeSafeDiagnosingMatcher;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.List;
+import java.util.Optional;
import static com.google.common.collect.Iterables.filter;
import static com.google.common.collect.Iterables.transform;
@@ -37,6 +38,9 @@ public class DeepReflectionMatcher<T> extends TypeSafeDiagnosingMatcher<T> {
mismatchDescription.appendText("was " + actual.getClass().getName());
return false;
}
+ if (expected instanceof Optional && actual instanceof Optional) {
+ return matchesOptional(path, (Optional)expected, (Optional)actual, mismatchDescription);
+ }
if (expected instanceof String) {
return matchesString(path, expected, actual, mismatchDescription);
@@ -75,6 +79,26 @@ public class DeepReflectionMatcher<T> extends TypeSafeDiagnosingMatcher<T> {
return true;
}
+ private static boolean matchesOptional(String path, Optional expected, Optional actual, Description mismatchDescription) {
+ if (actual.isPresent() && !expected.isPresent()) {
+ appendPath(mismatchDescription, path);
+ mismatchDescription.appendText("had value " + generateDescriptionOfValue(actual.get()));
+ return false;
+ }
+
+ if (expected.isPresent() && !actual.isPresent()) {
+ appendPath(mismatchDescription, path);
+ mismatchDescription.appendText("was empty");
+ return false;
+ }
+
+ if (actual.isPresent() && expected.isPresent()) {
+ return matchesSafely(path, expected.get(), actual.get(), mismatchDescription);
+ }
+
+ return true;
+ }
+
private static <T> boolean matchesString(String path, T expected, T actual, Description mismatchDescription) {
Matcher<Object> matcher = Matchers.equalTo(expected);
if (!matcher.matches(actual)) {