cweill / gotests

Automatically generate Go test boilerplate from your source code.
Apache License 2.0
4.93k stars 346 forks source link

use go-cmp instead of reflect.DeepEqual #99

Open mekegi opened 5 years ago

mekegi commented 5 years ago

https://github.com/cweill/gotests/issues/98

https://github.com/google/go-cmp/ is more usefull then reflect.DeepEqual

I changed reflect.DeepEqual to cmp.Equal and add to t.Errorf cmp.Diff for more detailed view of diff between got and want for exmaple how it looks like

=== RUN   TestAddress2addressResultSlice/two_diff_addr
    --- FAIL: TestAddress2addressResultSlice/two_diff_addr (0.00s)
        helpers_test.go:161: Address2addressResultSlice() = [address:<UID:"ololo1" >  address:<UID:"trololo" > ], want [address:<UID:"ololo" >  address:<UID:"trololo" > ]
             diff =   []*address_api.AddressResult{
                &{
                    Precision:            s"other",
            -       Address:              s`UID:"ololo1" `,
            +       Address:              s`UID:"ololo" `,
                    Distance:             0,
                    ... // 2 identical fields
                },
                &{Address: s`UID:"trololo" `},
              }
FAIL
coveralls commented 5 years ago

Coverage Status

Coverage increased (+0.05%) to 96.411% when pulling 3cb5a24c56f023d36f92f3dc3b486066ff315279 on mekegi:go-cmp into afa0a378663a63a98287714c3f3359e22a4ab29e on cweill:develop.

coveralls commented 5 years ago

Coverage Status

Coverage increased (+0.05%) to 96.411% when pulling cec4af4cabb0ab2fb33dcc38ea56327b577587f2 on mekegi:go-cmp into afa0a378663a63a98287714c3f3359e22a4ab29e on cweill:develop.

cweill commented 5 years ago

@mekegi: This looks good to me, except can you please add a flag use_go_cmp=true. That way people who want the old behavior (minus a dependency) can use it.

smitt04 commented 4 years ago

I see it has been a year since this PR was created. I would love to use cmp instead of reflect. Is going to get merged or is it dead?

sljeff commented 4 years ago

+1

Now gopls checks DeepEqual with errors.

17:03:20 ➜ gopls check app/grpc/handlers_test.go
/Users/jeff/proj/learning/app/grpc/handlers_test.go:2073:9-49: avoid using reflect.DeepEqual with errors
/Users/jeff/proj/learning/app/grpc/handlers_test.go:3163:7-55: avoid using reflect.DeepEqual with errors
/Users/jeff/proj/learning/app/grpc/handlers_test.go:4498:9-49: avoid using reflect.DeepEqual with errors
/Users/jeff/proj/learning/app/grpc/handlers_test.go:5076:9-49: avoid using reflect.DeepEqual with errors
/Users/jeff/proj/learning/app/grpc/handlers_test.go:5291:9-49: avoid using reflect.DeepEqual with errors
cweill commented 3 years ago

If OP or someone else can update this PR to resolve branch conflicts, I will approve it.