-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatchers_equal_to.go
60 lines (52 loc) · 1.38 KB
/
matchers_equal_to.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
package justest
import (
"strings"
"github.com/google/go-cmp/cmp"
)
type Comparator func(t T, expected any, actual any)
type EqualToMatcher interface {
Matcher
Using(comparator Comparator) EqualToMatcher
}
type equalTo struct {
expected []any
comparator Comparator
}
//go:noinline
func (m *equalTo) Assert(t T, actuals ...any) {
GetHelper(t).Helper()
if len(m.expected) != len(actuals) {
t.Fatalf("Unexpected difference: received %d actual values and %d expected values", len(actuals), len(m.expected))
} else {
for i := 0; i < len(m.expected); i++ {
expected := m.expected[i]
actual := actuals[i]
m.comparator(t, expected, actual)
}
}
}
func (m *equalTo) Using(comparator Comparator) EqualToMatcher {
m.comparator = comparator
return m
}
//go:noinline
func EqualTo(expected ...any) EqualToMatcher {
var opts []cmp.Option
var expectedWithoutOpts []any
for _, e := range expected {
if _, ok := e.(cmp.Option); ok {
opts = append(opts, e.(cmp.Option))
} else {
expectedWithoutOpts = append(expectedWithoutOpts, e)
}
}
return &equalTo{
expected: expectedWithoutOpts,
comparator: func(t T, expected, actual any) {
GetHelper(t).Helper()
if !cmp.Equal(expected, actual, opts...) {
t.Fatalf("Unexpected difference (\"-\" lines are expected values; \"+\" lines are actual values):\n%s", strings.TrimSpace(cmp.Diff(expected, actual, opts...)))
}
},
}
}