-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmatches.go
92 lines (73 loc) · 2.98 KB
/
matches.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
package assertjson
import (
"strings"
"github.com/stretchr/testify/assert"
)
// Matches compares two JSON payloads.
// It ignores added fields in actual JSON payload.
func (c Comparer) Matches(t TestingT, expected, actual []byte, msgAndArgs ...interface{}) bool {
if h, ok := t.(tHelper); ok {
h.Helper()
}
err := c.FailMismatch(expected, actual)
if err == nil {
return true
}
msg := err.Error()
msg = strings.ToUpper(msg[0:1]) + msg[1:]
assert.Fail(t, msg, msgAndArgs...)
return false
}
// MatchesMarshal marshals actual JSON payload and compares it with expected payload.
// It ignores added fields in actual JSON payload.
func (c Comparer) MatchesMarshal(t TestingT, expected []byte, actualValue interface{}, msgAndArgs ...interface{}) bool {
if h, ok := t.(tHelper); ok {
h.Helper()
}
actual, err := MarshalIndentCompact(actualValue, "", " ", 80)
assert.NoError(t, err, "failed to marshal actual value")
if len(msgAndArgs) == 0 {
msgAndArgs = append(msgAndArgs, string(actual))
}
return c.Matches(t, expected, actual, msgAndArgs...)
}
// FailMismatch returns error if expected JSON payload does not match actual JSON payload, nil otherwise.
// It ignores added fields in actual JSON payload.
func FailMismatch(expected, actual []byte) error {
return defaultComparer.FailMismatch(expected, actual)
}
// FailMismatchMarshal returns error if expected JSON payload does not match marshaled actual value, nil otherwise.
// It ignores added fields in actual JSON payload.
func FailMismatchMarshal(expected []byte, actualValue interface{}) error {
return defaultComparer.FailMismatchMarshal(expected, actualValue)
}
// FailMismatchMarshal returns error if expected JSON payload does not match marshaled actual value, nil otherwise.
// It ignores added fields in actual JSON payload.
func (c Comparer) FailMismatchMarshal(expected []byte, actualValue interface{}) error {
actual, err := MarshalIndentCompact(actualValue, "", " ", 80)
if err != nil {
return err
}
return c.FailMismatch(expected, actual)
}
// FailMismatch returns error if expected JSON payload does not match actual JSON payload, nil otherwise.
// It ignores added fields in actual JSON payload.
func (c Comparer) FailMismatch(expected, actual []byte) error {
return c.fail(expected, actual, true)
}
// Matches compares two JSON documents ignoring string values "<ignore-diff>".
// It ignores added fields in actual JSON payload.
func Matches(t TestingT, expected, actual []byte, msgAndArgs ...interface{}) bool {
if h, ok := t.(tHelper); ok {
h.Helper()
}
return defaultComparer.Matches(t, expected, actual, msgAndArgs...)
}
// MatchesMarshal marshals actual value and compares two JSON documents ignoring string values "<ignore-diff>".
// It ignores added fields in actual JSON payload.
func MatchesMarshal(t TestingT, expected []byte, actualValue interface{}, msgAndArgs ...interface{}) bool {
if h, ok := t.(tHelper); ok {
h.Helper()
}
return defaultComparer.MatchesMarshal(t, expected, actualValue, msgAndArgs...)
}