Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow "normal" objects to describe themself, e.g. in case of a mismatch #244

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions hamcrest/src/main/java/org/hamcrest/BaseDescription.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -29,6 +31,19 @@ public Description appendDescriptionOf(SelfDescribing value) {
public Description appendValue(Object value) {
if (value == null) {
append("null");
} else if (value instanceof SelfDescribing){

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a fundamental problem with this approach: normally hamcrest would be a test dependency.

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) {
Expand Down