aboutsummaryrefslogtreecommitdiff
path: root/cmp/report_slices.go
diff options
context:
space:
mode:
authorJoe Tsai <joetsai@digital-static.net>2020-06-17 17:25:47 -0700
committerGitHub <noreply@github.com>2020-06-17 17:25:47 -0700
commit77ae86f624cb174e21763cffcbbf070eb06cb016 (patch)
treedd32bd59eb58b50d44dee2efa06ac36f8daa92a1 /cmp/report_slices.go
parentc49bfce0ac9115b09320b47c3b9534cc5afd4579 (diff)
downloadgo-cmp-77ae86f624cb174e21763cffcbbf070eb06cb016.tar.gz
Improve reporting of values with cycles (#217)
Previously, the reporter could handle formatting values with cycles in that it did not crash with a stack overflow. However, the output was not particularly understandable as it did not surface to the user why a particular value was truncated, and if it was truncated due to a cyclic reference, what was the referent. This change annotates the reporter tree with pointer information so that a later pass can inject reference information if it is needed to produce more understandable output. Consider the following example: map[string]*cmp_test.CycleAlpha{ "Foo": &⟪ref#0⟫{ Name: "Foo", Bravos: map[string]*cmp_test.CycleBravo{ "FooBravo": &{ - ID: 101, + ID: 0, Name: "FooBravo", Mods: 100, Alphas: {"Foo": &⟪ref#0⟫(...)}, }, }, }, } This graph contains a cycle. To ensure that a graph can be formatted, the cycle is truncated as indicated with: &⟪ref#0⟫(...). The referent was identified earlier with: &⟪ref#0⟫{...}.
Diffstat (limited to 'cmp/report_slices.go')
-rw-r--r--cmp/report_slices.go10
1 files changed, 5 insertions, 5 deletions
diff --git a/cmp/report_slices.go b/cmp/report_slices.go
index 49fc5ec..35315da 100644
--- a/cmp/report_slices.go
+++ b/cmp/report_slices.go
@@ -112,7 +112,7 @@ func (opts formatOptions) FormatDiffSlice(v *valueNode) textNode {
}
}
isText = !isBinary
- isLinedText = isText && numLines >= 4 && maxLineLen <= 256
+ isLinedText = isText && numLines >= 4 && maxLineLen <= 1024
}
// Format the string into printable records.
@@ -194,7 +194,7 @@ func (opts formatOptions) FormatDiffSlice(v *valueNode) textNode {
}
list2 = append(list2, textRecord{Value: textLine(`"""`), ElideComma: true})
if isTripleQuoted {
- var out textNode = textWrap{"(", list2, ")"}
+ var out textNode = &textWrap{Prefix: "(", Value: list2, Suffix: ")"}
switch t.Kind() {
case reflect.String:
if t != reflect.TypeOf(string("")) {
@@ -281,7 +281,7 @@ func (opts formatOptions) FormatDiffSlice(v *valueNode) textNode {
}
// Wrap the output with appropriate type information.
- var out textNode = textWrap{"{", list, "}"}
+ var out textNode = &textWrap{Prefix: "{", Value: list, Suffix: "}"}
if !isText {
// The "{...}" byte-sequence literal is not valid Go syntax for strings.
// Emit the type for extra clarity (e.g. "string{...}").
@@ -292,12 +292,12 @@ func (opts formatOptions) FormatDiffSlice(v *valueNode) textNode {
}
switch t.Kind() {
case reflect.String:
- out = textWrap{"strings.Join(", out, fmt.Sprintf(", %q)", delim)}
+ out = &textWrap{Prefix: "strings.Join(", Value: out, Suffix: fmt.Sprintf(", %q)", delim)}
if t != reflect.TypeOf(string("")) {
out = opts.FormatType(t, out)
}
case reflect.Slice:
- out = textWrap{"bytes.Join(", out, fmt.Sprintf(", %q)", delim)}
+ out = &textWrap{Prefix: "bytes.Join(", Value: out, Suffix: fmt.Sprintf(", %q)", delim)}
if t != reflect.TypeOf([]byte(nil)) {
out = opts.FormatType(t, out)
}