/
matchers.go
52 lines (46 loc) · 1.36 KB
/
matchers.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package testcommon
import (
"context"
"reflect"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/stretchr/testify/mock"
)
func MatchByCmpDiff(t *testing.T, i any, opts ...cmp.Option) any {
opts = append(opts, IgnoreUnexported())
return mock.MatchedBy(func(j any) bool {
diff := cmp.Diff(i, j, opts...)
if diff != "" {
if t != nil {
t.Log(diff)
}
return false
}
return true
})
}
// MatchIgnoreContext is a mock matcher that ignores contexts inside the given object.
//
// interfaces generated by swagger bury the ctx into the params, which makes it impossible to mock
// with this matcher you can use the mocks quite adequately.
func MatchIgnoreContext(t *testing.T, i any, opts ...cmp.Option) any {
opts = append(opts, IgnoreContexts(), IgnoreUnexported())
return mock.MatchedBy(func(j any) bool {
// the exporter opt allows all unexported fields: https://github.com/google/go-cmp/pull/176
if diff := cmp.Diff(i, j, opts...); diff != "" {
if t != nil {
t.Log(diff)
}
return false
}
return true
})
}
func IgnoreContexts() cmp.Option {
return cmpopts.IgnoreInterfaces(struct{ context.Context }{})
}
func IgnoreUnexported() cmp.Option {
// the exporter opt allows all unexported fields: https://github.com/google/go-cmp/pull/176
return cmp.Exporter(func(reflect.Type) bool { return true })
}