From 0d32564189601b8dc8ce0babc4f944d6e7b0f52e Mon Sep 17 00:00:00 2001 From: Frederic BIDON Date: Sat, 27 Dec 2025 19:13:36 +0100 Subject: [PATCH] fix: Display uint values in decimal instead of hex in diffs Merged from https://github.com/stretchr/testify/pull/1223 The issue is described at https://github.com/stretchr/testify/issues/400 Thanks to the original contributor @mnotti Signed-off-by: Frederic BIDON --- README.md | 7 +-- internal/assertions/equal.go | 4 +- internal/assertions/equal_impl_test.go | 63 +++++++++++++++++--------- internal/assertions/equal_test.go | 1 + 4 files changed, 49 insertions(+), 26 deletions(-) diff --git a/README.md b/README.md index c77b21b73..f6a68eef4 100644 --- a/README.md +++ b/README.md @@ -63,7 +63,7 @@ However, at `go-openapi` we would like to address the well-known issues in `test 1. [x] The first release comes with zero dependencies and an unstable API (see below [our use case](#usage-at-go-openapi)) 2. |x] This project is going to be injected as the main and sole test dependency of the `go-openapi` libraries 2. [ ] ... and the `go-swagger` tool -3. [ ) Valuable pending pull requests from the original project could be merged (e.g. `JSONEqBytes`) or transformed as "enable" modules (e.g. colorized output) +3. [x] Valuable pending pull requests from the original project could be merged (e.g. `JSONEqBytes`) or transformed as "enable" modules (e.g. colorized output) 4. [ ] Unclear assertions may be provided an alternative verb (e.g. `InDelta`) 5. [ ] Since we have leveled the go requirements to the rest of the go-openapi (currently go1.24) there is quite a bit of relinting lying ahead. @@ -200,6 +200,7 @@ some adaptations into this fork: * github.com/stretchr/testify#1356 - panic(nil) handling for Go 1.21+ * github.com/stretchr/testify#1825 - Fix panic when using EqualValues with uncomparable types [merged] * github.com/stretchr/testify#1818 - Fix panic on invalid regex in Regexp/NotRegexp assertions [merged] +* github.com/stretchr/testify#1223 - Display uint values in decimal instead of hex in diffs [merged] ### Planned merges @@ -207,7 +208,7 @@ some adaptations into this fork: * Follow / adapt https://github.com/stretchr/testify/pull/1824 -Not PRs, but reported issues in the original repo: +Not PRs, but reported issues in the original repo (need to investigate): * https://github.com/stretchr/testify/issues/1826 * https://github.com/stretchr/testify/issues/1611 @@ -223,7 +224,7 @@ These improvements apply to the internalized and modernized copies of dependenci #### UX improvements -* github.com/stretchr/testify#1223 - Display uint values in decimal instead of hex in diffs +* diff rendering ### Under consideration diff --git a/internal/assertions/equal.go b/internal/assertions/equal.go index 700f1ab4d..77c14d552 100644 --- a/internal/assertions/equal.go +++ b/internal/assertions/equal.go @@ -148,8 +148,8 @@ func formatUnequalValues(expected, actual any) (e string, a string) { fmt.Sprintf("%T(%s)", actual, truncatingFormat("%#v", actual)) } switch expected.(type) { - case time.Duration: - return fmt.Sprintf("%v", expected), fmt.Sprintf("%v", actual) + case time.Duration, uint, uint8, uint16, uint32, uint64: + return fmt.Sprint(expected), fmt.Sprint(actual) default: return truncatingFormat("%#v", expected), truncatingFormat("%#v", actual) } diff --git a/internal/assertions/equal_impl_test.go b/internal/assertions/equal_impl_test.go index f4c24cd3c..263155a36 100644 --- a/internal/assertions/equal_impl_test.go +++ b/internal/assertions/equal_impl_test.go @@ -6,6 +6,8 @@ package assertions import ( "errors" "fmt" + "iter" + "slices" "testing" "time" ) @@ -39,29 +41,15 @@ func testFormatUnequalValues() func(*testing.T) { return func(t *testing.T) { t.Parallel() - expected, actual := formatUnequalValues("foo", "bar") - Equal(t, `"foo"`, expected, "value should not include type") - Equal(t, `"bar"`, actual, "value should not include type") - - expected, actual = formatUnequalValues(123, 123) - Equal(t, `123`, expected, "value should not include type") - Equal(t, `123`, actual, "value should not include type") - - expected, actual = formatUnequalValues(int64(123), int32(123)) - Equal(t, `int64(123)`, expected, "value should include type") - Equal(t, `int32(123)`, actual, "value should include type") - - expected, actual = formatUnequalValues(int64(123), nil) - Equal(t, `int64(123)`, expected, "value should include type") - Equal(t, `()`, actual, "value should include type") + for tt := range formatUnequalCases() { + t.Run(tt.testName, func(t *testing.T) { + t.Parallel() - type testStructType struct { - Val string + expected, actual := formatUnequalValues(tt.unequalExpected, tt.unequalActual) + Equal(t, tt.expectedExpected, expected, tt.testName) + Equal(t, tt.expectedActual, actual, tt.testName) + }) } - - expected, actual = formatUnequalValues(&testStructType{Val: "test"}, &testStructType{Val: "test"}) - Equal(t, fmt.Sprintf(`&%s.testStructType{Val:"test"}`, shortpkg), expected, "value should not include type annotation") - Equal(t, fmt.Sprintf(`&%s.testStructType{Val:"test"}`, shortpkg), actual, "value should not include type annotation") } } @@ -207,3 +195,36 @@ func testValidateEqualArgs() func(*testing.T) { } } } + +type formatUnequalCase struct { + unequalExpected any + unequalActual any + expectedExpected string + expectedActual string + testName string +} + +func formatUnequalCases() iter.Seq[formatUnequalCase] { + type testStructType struct { + Val string + } + + return slices.Values([]formatUnequalCase{ + {"foo", "bar", `"foo"`, `"bar"`, "value should not include type"}, + {123, 123, `123`, `123`, "value should not include type"}, + {int64(123), int32(123), `int64(123)`, `int32(123)`, "value should include type"}, + {int64(123), nil, `int64(123)`, `()`, "value should include type"}, + { + unequalExpected: &testStructType{Val: "test"}, + unequalActual: &testStructType{Val: "test"}, + expectedExpected: fmt.Sprintf(`&%s.testStructType{Val:"test"}`, shortpkg), + expectedActual: fmt.Sprintf(`&%s.testStructType{Val:"test"}`, shortpkg), + testName: "value should not include type annotation", + }, + {uint(123), uint(124), `123`, `124`, "uint should print clean"}, + {uint8(123), uint8(124), `123`, `124`, "uint8 should print clean"}, + {uint16(123), uint16(124), `123`, `124`, "uint16 should print clean"}, + {uint32(123), uint32(124), `123`, `124`, "uint32 should print clean"}, + {uint64(123), uint64(124), `123`, `124`, "uint64 should print clean"}, + }) +} diff --git a/internal/assertions/equal_test.go b/internal/assertions/equal_test.go index bc40958fe..a8172692c 100644 --- a/internal/assertions/equal_test.go +++ b/internal/assertions/equal_test.go @@ -372,6 +372,7 @@ func equalCases() iter.Seq[equalCase] { // A case that might be confusing, especially with numeric literals {10, uint(10), false, ""}, + {int(1), uint(1), false, ""}, }) }