From 044161281d393187f1ab326f00c857f1708e6a18 Mon Sep 17 00:00:00 2001 From: PirolA <---> Date: Mon, 18 Jun 2018 14:52:17 +0200 Subject: [PATCH] allow "normal" objects to describe themself --- .../main/java/org/hamcrest/BaseDescription.java | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/hamcrest/src/main/java/org/hamcrest/BaseDescription.java b/hamcrest/src/main/java/org/hamcrest/BaseDescription.java index 7b540c50..bf5e0662 100644 --- a/hamcrest/src/main/java/org/hamcrest/BaseDescription.java +++ b/hamcrest/src/main/java/org/hamcrest/BaseDescription.java @@ -3,6 +3,8 @@ import org.hamcrest.internal.ArrayIterator; import org.hamcrest.internal.SelfDescribingValueIterator; +import java.io.PrintWriter; +import java.io.StringWriter; import java.util.Arrays; import java.util.Iterator; @@ -29,6 +31,19 @@ public Description appendDescriptionOf(SelfDescribing value) { public Description appendValue(Object value) { if (value == null) { append("null"); + } else if (value instanceof SelfDescribing){ + append('<'); + try { + appendDescriptionOf( (SelfDescribing) value); + } catch (Exception ex){ + // generating an error message should not throw another exception. + // process exception into mismatch description to preserve "nice" error messages + StringWriter stringWriter = new StringWriter(); + PrintWriter printWriter = new PrintWriter(stringWriter); + ex.printStackTrace(printWriter); + append("Exception while resolving self description: " + stringWriter.toString()); + } + append('>'); } else if (value instanceof String) { toJavaSyntax((String) value); } else if (value instanceof Character) {