google / google/truth

Descendants of AbstractArraySubject have poor error reporting

Open
#395 4 comments 0 reactions 0 assignees View on GitHub
P3 type=enhancement
Dominant language
Java
Stars
2.8k
Forks
275
Avg merge
7m
Merged PRs (30d)
4

Description

The current code of `PrimitiveByteArraySubject` (as well as other primitive array subjects) has several major flaws:

1. The Throwable message is generated by concatenating string representation of arrays. If arrays are sufficiently long, those messages become unreadable and sometimes even cause OOM errors because of excessive length of resulting string;
2. There is no automatic check for array length, so one have to perform that check every time by calling `hasLength`. I believe, that `isEqualTo` on AbstractArraySubject should automatically perform that check;
3. The message shows array bodies, but does not tell which elements differ.

These are the changes I'd like to see:

1. Always put array contents after the rest of error message: even if IDE or something else truncates the message, the most informative part should still be visible;
2. If array lengths differ, the exception message should start from saying so. I am unable to fathom a situation when someone would like to know if arrays are equal but wouldn't care about their lengths;
3. If total length of arrays is sufficiently big (say, bigger than 200 elements), *do not* add their full string representation to the Throwable message. Instead override the `printStackTrace` so that information is printed to stream (you can even keep the same format while doing so). This improvement might also be relevant for ListSubject
4. Add the same difference-finding logic currently present in ListSubject to the descendants of AbstractArraySubject.

The resulting error message woukd like like this:

Arrays byte[99] and byte[100] differ at position 4 (5 more differences) *details about differences* [optional array contents]

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.