From 6faefd0594fae82639a62c23f0aed1451509dcc0 Mon Sep 17 00:00:00 2001 From: Joe Tsai Date: Tue, 12 Oct 2021 11:15:02 -0700 Subject: [PATCH] Reduce minimum length for specialize string diffing (#275) 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. Co-authored-by: Damien Neil --- 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 5f02a82..c7a409d 100644 --- a/cmp/compare_test.go +++ b/cmp/compare_test.go @@ -1338,6 +1338,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 59afe6d..d207803 100644 --- a/cmp/testdata/diffs +++ b/cmp/testdata/diffs @@ -1122,6 +1122,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{