aboutsummaryrefslogtreecommitdiff
path: root/cmp/report_slices.go
diff options
context:
space:
mode:
authorJoe Tsai <joetsai@digital-static.net>2020-06-10 16:51:30 -0700
committerGitHub <noreply@github.com>2020-06-10 16:51:30 -0700
commit0d296f9f534978cc25de69216b23b74bbc10fad9 (patch)
treefd8ce943ae7021a0cb6930782d985f2d17f148c9 /cmp/report_slices.go
parent7c9a834557ca73ca54b2f367316f4bd747217741 (diff)
downloadgo-cmp-0d296f9f534978cc25de69216b23b74bbc10fad9.tar.gz
Limit number of printed differences for variable-length composites (#213)
For large slices, arrays, and maps, the reporter can be unreadable if there are many differences. Limit the number of results to some reasonable maximum.
Diffstat (limited to 'cmp/report_slices.go')
-rw-r--r--cmp/report_slices.go16
1 files changed, 15 insertions, 1 deletions
diff --git a/cmp/report_slices.go b/cmp/report_slices.go
index 3da92bc..7a35f39 100644
--- a/cmp/report_slices.go
+++ b/cmp/report_slices.go
@@ -16,6 +16,10 @@ import (
"github.com/google/go-cmp/cmp/internal/diff"
)
+// maxDiffElements is the maximum number of difference elements to format
+// before the remaining differences are coalesced together.
+const maxDiffElements = 32
+
// CanFormatDiffSlice reports whether we support custom formatting for nodes
// that are slices of primitive kinds or strings.
func (opts formatOptions) CanFormatDiffSlice(v *valueNode) bool {
@@ -335,7 +339,13 @@ func (opts formatOptions) formatDiffSlice(
groups := coalesceAdjacentEdits(name, es)
groups = coalesceInterveningIdentical(groups, chunkSize/4)
+ maxGroup := diffStats{Name: name}
for i, ds := range groups {
+ if len(list) >= maxDiffElements {
+ maxGroup = maxGroup.Append(ds)
+ continue
+ }
+
// Print equal.
if ds.NumDiff() == 0 {
// Compute the number of leading and trailing equal bytes to print.
@@ -369,7 +379,11 @@ func (opts formatOptions) formatDiffSlice(
ny := appendChunks(vy.Slice(0, ds.NumIdentical+ds.NumInserted+ds.NumModified), diffInserted)
vy = vy.Slice(ny, vy.Len())
}
- assert(vx.Len() == 0 && vy.Len() == 0)
+ if maxGroup.IsZero() {
+ assert(vx.Len() == 0 && vy.Len() == 0)
+ } else {
+ list.AppendEllipsis(maxGroup)
+ }
return list
}