-
Notifications
You must be signed in to change notification settings - Fork 0
/
assert.go
96 lines (84 loc) · 1.97 KB
/
assert.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package assert
import (
"context"
"reflect"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
)
// CommonOptions is common options which applied for all types.
var CommonOptions = []cmp.Option{
cmpopts.IgnoreInterfaces(struct{ context.Context }{}),
}
// AddCommon adds common cmp.Options for all types.
func AddCommon(opts ...cmp.Option) {
CommonOptions = append(CommonOptions, opts...)
}
var repos = map[reflect.Type][]cmp.Option{}
// Add adds options for the type of value to option repository.
func Add(v interface{}, opts ...cmp.Option) {
if v == nil || len(opts) == 0 {
return
}
typ, ok := v.(reflect.Type)
if !ok {
typ = reflect.TypeOf(v)
}
curr, ok := repos[typ]
if !ok {
curr = make([]cmp.Option, 0, len(opts))
}
repos[typ] = append(curr, opts...)
}
func get(typ reflect.Type) []cmp.Option {
if typ == nil {
return nil
}
return repos[typ]
}
func join(oo ...[]cmp.Option) []cmp.Option {
n := 0
for _, o := range oo {
n += len(o)
}
opts := make([]cmp.Option, 0, n)
for _, o := range oo {
if len(o) == 0 {
continue
}
opts = append(opts, o...)
}
return opts
}
func check(t testing.TB, expected, actual interface{}) bool {
t.Helper()
var opts2 []cmp.Option
texp, tact := reflect.TypeOf(expected), reflect.TypeOf(actual)
opts := get(texp)
if texp != tact {
opts2 = get(tact)
}
diff := cmp.Diff(expected, actual, join(CommonOptions, opts, opts2)...)
if diff == "" {
return true
}
t.Logf("not equal\nName: %s\nDiff:\n--- Expected\n+++ Actual\n%s", t.Name(), diff)
return false
}
// Equal checks equality between "expected" and "actual".
// Test will be aborted soon if it wasn't match.
func Equal(tb testing.TB, expected, actual interface{}) {
tb.Helper()
if check(tb, expected, actual) {
return
}
tb.FailNow()
}
// FatalEqual checks equality between expected and actual.
func FatalEqual(tb testing.TB, expected, actual interface{}) {
tb.Helper()
if check(tb, expected, actual) {
return
}
tb.FailNow()
}