gotests icon indicating copy to clipboard operation
gotests copied to clipboard

use go-cmp instead of reflect.DeepEqual

Open mekegi opened this issue 6 years ago • 6 comments

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

mekegi avatar May 13 '19 15:05 mekegi

Coverage Status

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

coveralls avatar May 13 '19 15:05 coveralls

Coverage Status

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

coveralls avatar May 13 '19 15:05 coveralls

@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.

cweill avatar Jun 30 '19 17:06 cweill

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?

smitt04 avatar Jun 05 '20 16:06 smitt04

+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

sljeff avatar Jun 19 '20 09:06 sljeff

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

cweill avatar Dec 31 '20 14:12 cweill