go-test / deep

Golang deep variable equality test that returns human-readable differences
MIT License
743 stars 54 forks source link

internal struct don't work #63

Open lougxing opened 2 months ago

lougxing commented 2 months ago

`import ( "fmt" "testing"

"github.com/go-test/deep"

. "github.com/smartystreets/goconvey/convey"

)

func TestStructCompare(t *testing.T) {

type BB struct {
    i_BB int
    I_BB int
    S_BB string
    M_BB map[string]string
    A_BB []int
}

type AA struct {
    i_AA int
    I_AA int
    S_AA string
    M_AA map[string]string
    A_AA []int
    P_AA *BB
    B_AA BB
}

Convey("test struct compare", t, func() {
    a1 := AA{}
    a2 := AA{}

    a1.I_AA = 1
    a1.S_AA = "cc"
    a1.M_AA = map[string]string{"1": "2"}
    a1.A_AA = []int{3, 4}
    a1.P_AA = &BB{I_BB: 21, M_BB: map[string]string{"15": "6"}}
    a1.B_AA = BB{I_BB: 21, M_BB: map[string]string{"15": "6"}, A_BB: []int{1, 2}}

    a2.I_AA = 2
    a2.S_AA = "dd"
    a2.M_AA = map[string]string{"5": "6"}
    a2.A_AA = []int{7, 8}
    a2.P_AA = &BB{I_BB: 21, M_BB: map[string]string{"25": "6"}}

    a2.B_AA = BB{I_BB: 21, M_BB: map[string]string{"25": "6"}, A_BB: []int{3, 4}}

    fmt.Println()
    fmt.Printf("%+v \n", a1)
    fmt.Printf("%+v \n", a2)

    if diff := deep.Equal(a1, a2); diff != nil {
        for i, s := range diff {
            fmt.Println(i, s)

        }
        t.Error(diff)
    }
})

}` output:

{i_AA:0 I_AA:1 S_AA:cc M_AA:map[1:2] A_AA:[3 4] P_AA:0x140009aaf00 B_AA:{i_BB:0 I_BB:21 S_BB: M_BB:map[15:6] A_BB:[1 2]}} {i_AA:0 I_AA:2 S_AA:dd M_AA:map[5:6] A_AA:[7 8] P_AA:0x140009aaf40 B_AA:{i_BB:0 I_BB:21 S_BB: M_BB:map[25:6] A_BB:[3 4]}} 0 I_AA: 1 != 2 1 S_AA: cc != dd 2 M_AA.map[1]: 2 != 3 M_AA.map[5]: != 6 4 A_AA.slice[0]: 3 != 7 5 A_AA.slice[1]: 4 != 8 6 P_AA.M_BB.map[15]: 6 != 7 P_AA.M_BB.map[25]: != 6 8 B_AA.M_BB.map[15]: 6 != 9 B_AA.M_BB.map[25]: != 6

BB.A_BB is diff,but no.

daniel-nichter commented 1 week ago

Simplifying this, I don't get a failure, I get a diff: https://go.dev/play/p/fTEZVqbQUMi

If you can make a simpler test case that reproduces the problem, it should be easy to fix.