...
1
2
3
4 package testutil
5
6 import (
7 "testing"
8
9 "github.com/google/go-cmp/cmp"
10 "github.com/google/go-cmp/cmp/cmpopts"
11 "github.com/stretchr/testify/assert"
12 )
13
14
15
16 type Asserter struct {
17 Options cmp.Options
18 }
19
20
21 func NewAsserter(opts ...cmp.Option) *Asserter {
22 return &Asserter{
23 Options: opts,
24 }
25 }
26
27
28
29 var DefaultAsserter = NewAsserter(cmpopts.EquateErrors())
30
31
32
33 func (a *Asserter) EqualMatcher(expected interface{}) *EqualMatcher {
34 return &EqualMatcher{
35 Expected: expected,
36 Options: a.Options,
37 }
38 }
39
40
41
42 func (a *Asserter) Equal(t *testing.T, expected, actual interface{}, msgAndArgs ...interface{}) {
43 t.Helper()
44 matcher := a.EqualMatcher(expected)
45 match, err := matcher.Match(actual)
46 if err != nil {
47 t.Errorf("errored testing equality: %v", err)
48 return
49 }
50 if !match {
51 assert.Fail(t, matcher.FailureMessage(actual), msgAndArgs...)
52 }
53 }
54
55
56
57 func AssertEqual(t *testing.T, expected, actual interface{}, msgAndArgs ...interface{}) {
58 t.Helper()
59 DefaultAsserter.Equal(t, expected, actual, msgAndArgs...)
60 }
61
62
63
64 func (a *Asserter) NotEqual(t *testing.T, expected, actual interface{}, msgAndArgs ...interface{}) {
65 t.Helper()
66 matcher := a.EqualMatcher(expected)
67 match, err := matcher.Match(actual)
68 if err != nil {
69 t.Errorf("errored testing equality: %v", err)
70 return
71 }
72 if match {
73 assert.Fail(t, matcher.NegatedFailureMessage(actual), msgAndArgs...)
74 }
75 }
76
77
78
79 func AssertNotEqual(t *testing.T, expected, actual interface{}, msgAndArgs ...interface{}) {
80 t.Helper()
81 DefaultAsserter.NotEqual(t, expected, actual, msgAndArgs...)
82 }
83
View as plain text