diff --git a/assert/assertions.go b/assert/assertions.go index 166f63726..021aeafd2 100644 --- a/assert/assertions.go +++ b/assert/assertions.go @@ -1955,8 +1955,10 @@ func diff(expected interface{}, actual interface{}) string { e = spewConfigStringerEnabled.Sdump(expected) a = spewConfigStringerEnabled.Sdump(actual) default: - e = spewConfig.Sdump(expected) - a = spewConfig.Sdump(actual) + config := spewConfig + config.TrailingMapComma = true + e = config.Sdump(expected) + a = config.Sdump(actual) } diff, _ := difflib.GetUnifiedDiffString(difflib.UnifiedDiff{ diff --git a/assert/assertions_test.go b/assert/assertions_test.go index 11642e096..99aaa5e1e 100644 --- a/assert/assertions_test.go +++ b/assert/assertions_test.go @@ -2971,10 +2971,9 @@ Diff: - (string) (len=4) "four": (int) 4, + (string) (len=4) "five": (int) 5, (string) (len=3) "one": (int) 1, -- (string) (len=5) "three": (int) 3, -- (string) (len=3) "two": (int) 2 + (string) (len=5) "seven": (int) 7, -+ (string) (len=5) "three": (int) 3 + (string) (len=5) "three": (int) 3, +- (string) (len=3) "two": (int) 2, } ` diff --git a/internal/spew/config.go b/internal/spew/config.go index 161895fc6..fa4e3b068 100644 --- a/internal/spew/config.go +++ b/internal/spew/config.go @@ -98,6 +98,10 @@ type ConfigState struct { // be spewed to strings and sorted by those strings. This is only // considered if SortKeys is true. SpewKeys bool + + // TrailingMapComma adds a comma after the last entry when dumping a map. + // This keeps unchanged entries identical when comparing two map dumps. + TrailingMapComma bool } // Config is the active configuration of the top-level functions. diff --git a/internal/spew/dump.go b/internal/spew/dump.go index 8323041a4..f141dd7b9 100644 --- a/internal/spew/dump.go +++ b/internal/spew/dump.go @@ -392,7 +392,7 @@ func (d *dumpState) dump(v reflect.Value) { d.w.Write(colonSpaceBytes) d.ignoreNextIndent = true d.dump(d.unpackValue(v.MapIndex(key))) - if i < (numEntries - 1) { + if d.cs.TrailingMapComma || i < numEntries-1 { d.w.Write(commaNewlineBytes) } else { d.w.Write(newlineBytes)