From 43556c3c8dca6582ca2339e1ba2f143055d4711b Mon Sep 17 00:00:00 2001 From: Robbie McMichael <2044464+robbiemcmichael@users.noreply.github.com> Date: Sat, 15 Nov 2025 12:34:34 +0800 Subject: [PATCH] Ensure deterministic order when diffing maps When generating the diff for two maps where the map key is not a basic type, the order is non-deterministic which makes the diffs unreadable. By enabling the SpewKeys setting, the map keys will be spewed to strings for the purpose of sorting them. This results in a deterministic order and makes the diffs readable. --- assert/assertions.go | 2 ++ assert/assertions_test.go | 22 ++++++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/assert/assertions.go b/assert/assertions.go index a27e70546..dd0a71e2e 100644 --- a/assert/assertions.go +++ b/assert/assertions.go @@ -1985,6 +1985,7 @@ var spewConfig = spew.ConfigState{ DisablePointerAddresses: true, DisableCapacities: true, SortKeys: true, + SpewKeys: true, DisableMethods: true, MaxDepth: 10, } @@ -1994,6 +1995,7 @@ var spewConfigStringerEnabled = spew.ConfigState{ DisablePointerAddresses: true, DisableCapacities: true, SortKeys: true, + SpewKeys: true, MaxDepth: 10, } diff --git a/assert/assertions_test.go b/assert/assertions_test.go index 4975f5e41..d1e57d6b3 100644 --- a/assert/assertions_test.go +++ b/assert/assertions_test.go @@ -2986,6 +2986,28 @@ Diff: expected = ` +Diff: +--- Expected ++++ Actual +@@ -12,3 +12,3 @@ + x: (int) 4 +- }: (int) 4 ++ }: (int) 999 + } +` + + type Key struct { + x int + } + + actual = diff( + map[Key]int{Key{1}: 1, Key{2}: 2, Key{3}: 3, Key{4}: 4}, + map[Key]int{Key{1}: 1, Key{2}: 2, Key{3}: 3, Key{4}: 999}, + ) + Equal(t, expected, actual) + + expected = ` + Diff: --- Expected +++ Actual