Skip to content

Commit b42051d

Browse files
authored
Add matching checker (#20)
1 parent 04c2462 commit b42051d

File tree

3 files changed

+147
-4
lines changed

3 files changed

+147
-4
lines changed

equal.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ func (c Comparer) varCollected(s string, v interface{}) bool {
122122
return false
123123
}
124124

125-
func (c Comparer) filterDeltas(deltas []gojsondiff.Delta) []gojsondiff.Delta {
125+
func (c Comparer) filterDeltas(deltas []gojsondiff.Delta, ignoreAdded bool) []gojsondiff.Delta {
126126
result := make([]gojsondiff.Delta, 0, len(deltas))
127127

128128
for _, delta := range deltas {
@@ -142,19 +142,24 @@ func (c Comparer) filterDeltas(deltas []gojsondiff.Delta) []gojsondiff.Delta {
142142
}
143143
}
144144
case *gojsondiff.Object:
145-
v.Deltas = c.filterDeltas(v.Deltas)
145+
v.Deltas = c.filterDeltas(v.Deltas, ignoreAdded)
146146
if len(v.Deltas) == 0 {
147147
continue
148148
}
149149

150150
delta = v
151151
case *gojsondiff.Array:
152-
v.Deltas = c.filterDeltas(v.Deltas)
152+
v.Deltas = c.filterDeltas(v.Deltas, ignoreAdded)
153153
if len(v.Deltas) == 0 {
154154
continue
155155
}
156156

157157
delta = v
158+
159+
case *gojsondiff.Added:
160+
if ignoreAdded {
161+
continue
162+
}
158163
}
159164

160165
result = append(result, delta)
@@ -237,6 +242,10 @@ func (c Comparer) FailNotEqualMarshal(expected []byte, actualValue interface{})
237242

238243
// FailNotEqual returns error if JSON payloads are different, nil otherwise.
239244
func (c Comparer) FailNotEqual(expected, actual []byte) error {
245+
return c.fail(expected, actual, false)
246+
}
247+
248+
func (c Comparer) fail(expected, actual []byte, ignoreAdded bool) error {
240249
var expDecoded, actDecoded interface{}
241250

242251
expected, err := c.filterExpected(expected)
@@ -277,7 +286,7 @@ func (c Comparer) FailNotEqual(expected, actual []byte) error {
277286
return nil
278287
}
279288

280-
diffValue = &diff{deltas: c.filterDeltas(diffValue.Deltas())}
289+
diffValue = &diff{deltas: c.filterDeltas(diffValue.Deltas(), ignoreAdded)}
281290
if !diffValue.Modified() {
282291
return nil
283292
}

matches.go

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package assertjson
2+
3+
import (
4+
"strings"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
// Matches compares two JSON payloads.
10+
// It ignores added fields in actual JSON payload.
11+
func (c Comparer) Matches(t TestingT, expected, actual []byte, msgAndArgs ...interface{}) bool {
12+
if h, ok := t.(tHelper); ok {
13+
h.Helper()
14+
}
15+
16+
err := c.FailMismatch(expected, actual)
17+
if err == nil {
18+
return true
19+
}
20+
21+
msg := err.Error()
22+
msg = strings.ToUpper(msg[0:1]) + msg[1:]
23+
assert.Fail(t, msg, msgAndArgs...)
24+
25+
return false
26+
}
27+
28+
// MatchesMarshal marshals actual JSON payload and compares it with expected payload.
29+
// It ignores added fields in actual JSON payload.
30+
func (c Comparer) MatchesMarshal(t TestingT, expected []byte, actualValue interface{}, msgAndArgs ...interface{}) bool {
31+
if h, ok := t.(tHelper); ok {
32+
h.Helper()
33+
}
34+
35+
actual, err := MarshalIndentCompact(actualValue, "", " ", 80)
36+
assert.NoError(t, err, "failed to marshal actual value")
37+
38+
if len(msgAndArgs) == 0 {
39+
msgAndArgs = append(msgAndArgs, string(actual))
40+
}
41+
42+
return c.Matches(t, expected, actual, msgAndArgs...)
43+
}
44+
45+
// FailMismatch returns error if expected JSON payload does not match actual JSON payload, nil otherwise.
46+
// It ignores added fields in actual JSON payload.
47+
func FailMismatch(expected, actual []byte) error {
48+
return defaultComparer.FailMismatch(expected, actual)
49+
}
50+
51+
// FailMismatchMarshal returns error if expected JSON payload does not match marshaled actual value, nil otherwise.
52+
// It ignores added fields in actual JSON payload.
53+
func FailMismatchMarshal(expected []byte, actualValue interface{}) error {
54+
return defaultComparer.FailMismatchMarshal(expected, actualValue)
55+
}
56+
57+
// FailMismatchMarshal returns error if expected JSON payload does not match marshaled actual value, nil otherwise.
58+
// It ignores added fields in actual JSON payload.
59+
func (c Comparer) FailMismatchMarshal(expected []byte, actualValue interface{}) error {
60+
actual, err := MarshalIndentCompact(actualValue, "", " ", 80)
61+
if err != nil {
62+
return err
63+
}
64+
65+
return c.FailMismatch(expected, actual)
66+
}
67+
68+
// FailMismatch returns error if expected JSON payload does not match actual JSON payload, nil otherwise.
69+
// It ignores added fields in actual JSON payload.
70+
func (c Comparer) FailMismatch(expected, actual []byte) error {
71+
return c.fail(expected, actual, true)
72+
}
73+
74+
// Matches compares two JSON documents ignoring string values "<ignore-diff>".
75+
// It ignores added fields in actual JSON payload.
76+
func Matches(t TestingT, expected, actual []byte, msgAndArgs ...interface{}) bool {
77+
if h, ok := t.(tHelper); ok {
78+
h.Helper()
79+
}
80+
81+
return defaultComparer.Matches(t, expected, actual, msgAndArgs...)
82+
}
83+
84+
// MatchesMarshal marshals actual value and compares two JSON documents ignoring string values "<ignore-diff>".
85+
// It ignores added fields in actual JSON payload.
86+
func MatchesMarshal(t TestingT, expected []byte, actualValue interface{}, msgAndArgs ...interface{}) bool {
87+
if h, ok := t.(tHelper); ok {
88+
h.Helper()
89+
}
90+
91+
return defaultComparer.MatchesMarshal(t, expected, actualValue, msgAndArgs...)
92+
}

matches_test.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package assertjson_test
2+
3+
import (
4+
"encoding/json"
5+
"testing"
6+
7+
"github.com/stretchr/testify/assert"
8+
"github.com/swaggest/assertjson"
9+
)
10+
11+
func TestFailMismatch(t *testing.T) {
12+
act := json.RawMessage(`{"a": 1, "b": 2, "c": {"d": 1, "e": 2}}`)
13+
exp := []byte(`{"a": 1, "c": {"d": 1}}`)
14+
expFail := []byte(`{"a": 1, "c": {"d": 2}}`)
15+
16+
assert.EqualError(t, assertjson.FailNotEqualMarshal(exp, act), `not equal:
17+
{
18+
"a": 1,
19+
"c": {
20+
"d": 1
21+
+ "e": 2
22+
}
23+
+ "b": 2
24+
}
25+
`)
26+
27+
assert.NoError(t, assertjson.FailMismatchMarshal(exp, act))
28+
assert.NoError(t, assertjson.FailMismatch(exp, act))
29+
assert.EqualError(t, assertjson.FailMismatchMarshal(expFail, act), `not equal:
30+
{
31+
"a": 1,
32+
"c": {
33+
- "d": 2
34+
+ "d": 1
35+
}
36+
}
37+
`)
38+
39+
assert.False(t, assertjson.MatchesMarshal(testingT(func(format string, args ...interface{}) {}), expFail, act))
40+
assertjson.MatchesMarshal(t, exp, act)
41+
assertjson.Matches(t, exp, act)
42+
}

0 commit comments

Comments
 (0)