Skip to content

Commit 7abe00e

Browse files
authored
fix: Display uint values in decimal instead of hex in diffs (#10)
Merged from stretchr#1223 The issue is described at stretchr#400 Thanks to the original contributor @mnotti Signed-off-by: Frederic BIDON <fredbi@yahoo.com>
1 parent 54b53a7 commit 7abe00e

4 files changed

Lines changed: 49 additions & 26 deletions

File tree

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ However, at `go-openapi` we would like to address the well-known issues in `test
6363
1. [x] The first release comes with zero dependencies and an unstable API (see below [our use case](#usage-at-go-openapi))
6464
2. |x] This project is going to be injected as the main and sole test dependency of the `go-openapi` libraries
6565
2. [ ] ... and the `go-swagger` tool
66-
3. [ ) Valuable pending pull requests from the original project could be merged (e.g. `JSONEqBytes`) or transformed as "enable" modules (e.g. colorized output)
66+
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)
6767
4. [ ] Unclear assertions may be provided an alternative verb (e.g. `InDelta`)
6868
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.
6969

@@ -200,14 +200,15 @@ some adaptations into this fork:
200200
* github.com/stretchr/testify#1356 - panic(nil) handling for Go 1.21+
201201
* github.com/stretchr/testify#1825 - Fix panic when using EqualValues with uncomparable types [merged]
202202
* github.com/stretchr/testify#1818 - Fix panic on invalid regex in Regexp/NotRegexp assertions [merged]
203+
* github.com/stretchr/testify#1223 - Display uint values in decimal instead of hex in diffs [merged]
203204

204205
### Planned merges
205206

206207
#### Critical safety fixes (high priority)
207208

208209
* Follow / adapt https://github.com/stretchr/testify/pull/1824
209210

210-
Not PRs, but reported issues in the original repo:
211+
Not PRs, but reported issues in the original repo (need to investigate):
211212

212213
* https://github.com/stretchr/testify/issues/1826
213214
* https://github.com/stretchr/testify/issues/1611
@@ -223,7 +224,7 @@ These improvements apply to the internalized and modernized copies of dependenci
223224

224225
#### UX improvements
225226

226-
* github.com/stretchr/testify#1223 - Display uint values in decimal instead of hex in diffs
227+
* diff rendering
227228

228229
### Under consideration
229230

internal/assertions/equal.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,8 @@ func formatUnequalValues(expected, actual any) (e string, a string) {
148148
fmt.Sprintf("%T(%s)", actual, truncatingFormat("%#v", actual))
149149
}
150150
switch expected.(type) {
151-
case time.Duration:
152-
return fmt.Sprintf("%v", expected), fmt.Sprintf("%v", actual)
151+
case time.Duration, uint, uint8, uint16, uint32, uint64:
152+
return fmt.Sprint(expected), fmt.Sprint(actual)
153153
default:
154154
return truncatingFormat("%#v", expected), truncatingFormat("%#v", actual)
155155
}

internal/assertions/equal_impl_test.go

Lines changed: 42 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ package assertions
66
import (
77
"errors"
88
"fmt"
9+
"iter"
10+
"slices"
911
"testing"
1012
"time"
1113
)
@@ -39,29 +41,15 @@ func testFormatUnequalValues() func(*testing.T) {
3941
return func(t *testing.T) {
4042
t.Parallel()
4143

42-
expected, actual := formatUnequalValues("foo", "bar")
43-
Equal(t, `"foo"`, expected, "value should not include type")
44-
Equal(t, `"bar"`, actual, "value should not include type")
45-
46-
expected, actual = formatUnequalValues(123, 123)
47-
Equal(t, `123`, expected, "value should not include type")
48-
Equal(t, `123`, actual, "value should not include type")
49-
50-
expected, actual = formatUnequalValues(int64(123), int32(123))
51-
Equal(t, `int64(123)`, expected, "value should include type")
52-
Equal(t, `int32(123)`, actual, "value should include type")
53-
54-
expected, actual = formatUnequalValues(int64(123), nil)
55-
Equal(t, `int64(123)`, expected, "value should include type")
56-
Equal(t, `<nil>(<nil>)`, actual, "value should include type")
44+
for tt := range formatUnequalCases() {
45+
t.Run(tt.testName, func(t *testing.T) {
46+
t.Parallel()
5747

58-
type testStructType struct {
59-
Val string
48+
expected, actual := formatUnequalValues(tt.unequalExpected, tt.unequalActual)
49+
Equal(t, tt.expectedExpected, expected, tt.testName)
50+
Equal(t, tt.expectedActual, actual, tt.testName)
51+
})
6052
}
61-
62-
expected, actual = formatUnequalValues(&testStructType{Val: "test"}, &testStructType{Val: "test"})
63-
Equal(t, fmt.Sprintf(`&%s.testStructType{Val:"test"}`, shortpkg), expected, "value should not include type annotation")
64-
Equal(t, fmt.Sprintf(`&%s.testStructType{Val:"test"}`, shortpkg), actual, "value should not include type annotation")
6553
}
6654
}
6755

@@ -207,3 +195,36 @@ func testValidateEqualArgs() func(*testing.T) {
207195
}
208196
}
209197
}
198+
199+
type formatUnequalCase struct {
200+
unequalExpected any
201+
unequalActual any
202+
expectedExpected string
203+
expectedActual string
204+
testName string
205+
}
206+
207+
func formatUnequalCases() iter.Seq[formatUnequalCase] {
208+
type testStructType struct {
209+
Val string
210+
}
211+
212+
return slices.Values([]formatUnequalCase{
213+
{"foo", "bar", `"foo"`, `"bar"`, "value should not include type"},
214+
{123, 123, `123`, `123`, "value should not include type"},
215+
{int64(123), int32(123), `int64(123)`, `int32(123)`, "value should include type"},
216+
{int64(123), nil, `int64(123)`, `<nil>(<nil>)`, "value should include type"},
217+
{
218+
unequalExpected: &testStructType{Val: "test"},
219+
unequalActual: &testStructType{Val: "test"},
220+
expectedExpected: fmt.Sprintf(`&%s.testStructType{Val:"test"}`, shortpkg),
221+
expectedActual: fmt.Sprintf(`&%s.testStructType{Val:"test"}`, shortpkg),
222+
testName: "value should not include type annotation",
223+
},
224+
{uint(123), uint(124), `123`, `124`, "uint should print clean"},
225+
{uint8(123), uint8(124), `123`, `124`, "uint8 should print clean"},
226+
{uint16(123), uint16(124), `123`, `124`, "uint16 should print clean"},
227+
{uint32(123), uint32(124), `123`, `124`, "uint32 should print clean"},
228+
{uint64(123), uint64(124), `123`, `124`, "uint64 should print clean"},
229+
})
230+
}

internal/assertions/equal_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,7 @@ func equalCases() iter.Seq[equalCase] {
372372

373373
// A case that might be confusing, especially with numeric literals
374374
{10, uint(10), false, ""},
375+
{int(1), uint(1), false, ""},
375376
})
376377
}
377378

0 commit comments

Comments
 (0)