stretchr / testify

A toolkit with common assertions and mocks that plays nicely with the standard library
MIT License
22.57k stars 1.57k forks source link

Formatting unsigned integers (in formatUnequalValue or truncatingFormat) with %v instead of %#v #1116

Open tjanez opened 2 years ago

tjanez commented 2 years ago

Example:

package main

import (
    "github.com/stretchr/testify/require"
    "testing"
)

func TestUnsignedInt(t *testing.T) {

    var a uint64 = 1234567
    var b uint64 = 98765523

    require.Equal(t, a, b, "The two numbers should be the same.")

}

This results in hex-formatted integers:

=== RUN   TestUnsignedInt
    prog.go:13: 
            Error Trace:    prog.go:13
            Error:          Not equal: 
                            expected: 0x12d687
                            actual  : 0x5e30ad3
            Test:           TestUnsignedInt
            Messages:       The two numbers should be the same.
--- FAIL: TestUnsignedInt (0.00s)
FAIL

Replacing %#v with %v in truncatingFormat(): https://github.com/stretchr/testify/blob/c26b7f39f88ecc339b622fcbe6531ac5fdccd799/assert/assertions.go#L447

would result in decimal-formatted integers:

=== RUN   TestUnsignedInt
    prog.go:13: 
            Error Trace:    prog.go:13
            Error:          Not equal: 
                            expected: 1234567
                            actual  : 98765523
            Test:           TestUnsignedInt
            Messages:       The two numbers should be the same.
--- FAIL: TestUnsignedInt (0.00s)
FAIL

There was a similar issue for time.Duration type (#626) which was solved with special casing in formatUnequalValue(): https://github.com/stretchr/testify/blob/c26b7f39f88ecc339b622fcbe6531ac5fdccd799/assert/assertions.go#L435-L438


A similar broader thing is tracked in #390.

mrdengbo commented 2 years ago

Replacing %#v with %T(%v) in truncatingFormat(), The formatted output parameters are as follows: image