From 1835615ea6157784ef83b1b6865020c5cf7e0cd2 Mon Sep 17 00:00:00 2001 From: Joe Tsai Date: Thu, 16 Sep 2021 16:39:07 -0700 Subject: [PATCH] Reduce minimum length for specialize string diffing The original threshold of 64 was chosen without much thought. Lower it to 32 now that we have some concrete examples that it is aesthetically better. --- cmp/compare_test.go | 13 +++++++++++++ cmp/report_slices.go | 2 +- cmp/testdata/diffs | 12 ++++++++++++ 3 files changed, 26 insertions(+), 1 deletion(-) diff --git a/cmp/compare_test.go b/cmp/compare_test.go index a435209..49f439a 100644 --- a/cmp/compare_test.go +++ b/cmp/compare_test.go @@ -1337,6 +1337,19 @@ using the AllowUnexported option.`, "\n"), x: struct{ X MyBytes }{MyBytes("\xde\xad\xbe\xef")}, y: struct{ X MyBytes }{}, reason: "MyBytes should not be printed as text since it is binary data", + }, { + label: label + "/ShortJSON", + x: `{ + "id": 1, + "foo": true, + "bar": true, +}`, + y: `{ + "id": 1434180, + "foo": true, + "bar": true, +}`, + reason: "short multiline JSON should prefer triple-quoted string diff as it is more readable", }} } diff --git a/cmp/report_slices.go b/cmp/report_slices.go index 9809228..68b5c1a 100644 --- a/cmp/report_slices.go +++ b/cmp/report_slices.go @@ -80,7 +80,7 @@ func (opts formatOptions) CanFormatDiffSlice(v *valueNode) bool { } // Use specialized string diffing for longer slices or strings. - const minLength = 64 + const minLength = 32 return vx.Len() >= minLength && vy.Len() >= minLength } diff --git a/cmp/testdata/diffs b/cmp/testdata/diffs index 96ea191..cc18337 100644 --- a/cmp/testdata/diffs +++ b/cmp/testdata/diffs @@ -1120,6 +1120,18 @@ + X: nil, } >>> TestDiff/Reporter/NonStringifiedNamedBytes +<<< TestDiff/Reporter/ShortJSON + ( + """ + { +- "id": 1, ++ "id": 1434180, + "foo": true, + "bar": true, + } + """ + ) +>>> TestDiff/Reporter/ShortJSON <<< TestDiff/EmbeddedStruct/ParentStructA/Inequal teststructs.ParentStructA{ privateStruct: teststructs.privateStruct{