-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathequal.go
More file actions
109 lines (94 loc) · 3.17 KB
/
equal.go
File metadata and controls
109 lines (94 loc) · 3.17 KB
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
97
98
99
100
101
102
103
104
105
106
107
108
109
package attest
import (
"testing"
"github.com/google/go-cmp/cmp"
)
// DeepEqualOption configures the behavior of DeepEqual and NotDeepEqual.
type DeepEqualOption func(*deepEqualConfig)
type deepEqualConfig struct {
cmpOpts []cmp.Option
msg string
msgArgs []any
}
// WithCmpOpts adds go-cmp options to control comparison behavior in DeepEqual
// and NotDeepEqual.
func WithCmpOpts(opts ...cmp.Option) DeepEqualOption {
return func(c *deepEqualConfig) {
c.cmpOpts = append(c.cmpOpts, opts...)
}
}
// WithMessage adds a custom failure message to DeepEqual and NotDeepEqual.
func WithMessage(format string, args ...any) DeepEqualOption {
return func(c *deepEqualConfig) {
c.msg = format
c.msgArgs = args
}
}
func equal[T comparable](t testing.TB, mode failMode, expected, actual T, msgAndArgs ...any) {
t.Helper()
if actual != expected {
msg := formatMsg("values are not equal", formatExpectedReceived(expected, actual))
report(t, mode, msg, msgAndArgs...)
}
}
// Equal asserts that expected and actual are equal using the == operator.
func Equal[T comparable](t testing.TB, expected, actual T, msgAndArgs ...any) {
t.Helper()
equal(t, modeError, expected, actual, msgAndArgs...)
}
func notEqual[T comparable](t testing.TB, mode failMode, expected, actual T, msgAndArgs ...any) {
t.Helper()
if actual == expected {
msg := formatMsg("values should not be equal", formatExpectedReceived(expected, actual))
report(t, mode, msg, msgAndArgs...)
}
}
// NotEqual asserts that expected and actual are not equal using the != operator.
func NotEqual[T comparable](t testing.TB, expected, actual T, msgAndArgs ...any) {
t.Helper()
notEqual(t, modeError, expected, actual, msgAndArgs...)
}
func applyDeepEqualOpts(opts []DeepEqualOption) deepEqualConfig {
var cfg deepEqualConfig
for _, opt := range opts {
opt(&cfg)
}
return cfg
}
func deepEqual[T any](t testing.TB, mode failMode, expected, actual T, opts ...DeepEqualOption) {
t.Helper()
cfg := applyDeepEqualOpts(opts)
if !cmp.Equal(expected, actual, cfg.cmpOpts...) {
diff := cmp.Diff(expected, actual, cfg.cmpOpts...)
msg := formatMsg("values are not deep equal", "diff (-expected +actual):\n"+diff)
if cfg.msg != "" {
args := append([]any{cfg.msg}, cfg.msgArgs...)
report(t, mode, msg, args...)
} else {
report(t, mode, msg)
}
}
}
// DeepEqual asserts that expected and actual are deeply equal using go-cmp.
func DeepEqual[T any](t testing.TB, expected, actual T, opts ...DeepEqualOption) {
t.Helper()
deepEqual(t, modeError, expected, actual, opts...)
}
func notDeepEqual[T any](t testing.TB, mode failMode, expected, actual T, opts ...DeepEqualOption) {
t.Helper()
cfg := applyDeepEqualOpts(opts)
if cmp.Equal(expected, actual, cfg.cmpOpts...) {
msg := formatMsg("values should not be deep equal", "both values are equal")
if cfg.msg != "" {
args := append([]any{cfg.msg}, cfg.msgArgs...)
report(t, mode, msg, args...)
} else {
report(t, mode, msg)
}
}
}
// NotDeepEqual asserts that expected and actual are not deeply equal using go-cmp.
func NotDeepEqual[T any](t testing.TB, expected, actual T, opts ...DeepEqualOption) {
t.Helper()
notDeepEqual(t, modeError, expected, actual, opts...)
}