-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathequal.go
79 lines (68 loc) · 2.32 KB
/
equal.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
package jsontools
import (
"encoding/json"
"fmt"
"reflect"
"github.com/stretchr/testify/assert"
)
func JsonEqual(a, b []byte) (bool, error) {
filter := NewJsonNullFilter(false)
a, err := filter.Filter(a)
if err != nil {
return false, err
}
b, err = filter.Filter(b)
if err != nil {
return false, err
}
var o1, o2 any
err = json.Unmarshal(a, &o1)
if err != nil {
return false, err
}
err = json.Unmarshal(b, &o2)
if err != nil {
return false, err
}
return reflect.DeepEqual(o1, o2), nil
}
type tHelper interface {
Helper()
}
// AssertJSONEq asserts that two JSON strings are equivalent.
//
// AssertJSONEq(t, `{"hello": "world", "foo": "bar"}`, `{"foo": "bar", "hello": "world"}`)
func AssertJSONEq(t assert.TestingT, expected string, actual string, msgAndArgs ...interface{}) bool {
if h, ok := t.(tHelper); ok {
h.Helper()
}
filter := NewJsonNullFilter(true)
expectedBytes, err := filter.Filter([]byte(expected))
if err != nil {
return assert.Fail(t, fmt.Sprintf("Expected value ('%s') is not valid json.\nJSON parsing error: '%s'", expected, err.Error()), msgAndArgs...)
}
actualBytes, err := filter.Filter([]byte(actual))
if err != nil {
return assert.Fail(t, fmt.Sprintf("Input ('%s') needs to be valid json.\nJSON parsing error: '%s'", actual, err.Error()), msgAndArgs...)
}
var expectedJSONAsInterface, actualJSONAsInterface interface{}
if err := json.Unmarshal(expectedBytes, &expectedJSONAsInterface); err != nil {
return assert.Fail(t, fmt.Sprintf("Expected value ('%s') is not valid json.\nJSON parsing error: '%s'", expected, err.Error()), msgAndArgs...)
}
if err := json.Unmarshal(actualBytes, &actualJSONAsInterface); err != nil {
return assert.Fail(t, fmt.Sprintf("Input ('%s') needs to be valid json.\nJSON parsing error: '%s'", actual, err.Error()), msgAndArgs...)
}
return assert.Equal(t, expectedJSONAsInterface, actualJSONAsInterface, msgAndArgs...)
}
// RequireJSONEq asserts that two JSON strings are equivalent.
//
// RequireJSONEq(t, `{"hello": "world", "foo": "bar"}`, `{"foo": "bar", "hello": "world"}`)
func RequireJSONEq(t assert.TestingT, expected string, actual string, msgAndArgs ...interface{}) {
if h, ok := t.(tHelper); ok {
h.Helper()
}
if AssertJSONEq(t, expected, actual, msgAndArgs...) {
return
}
assert.FailNow(t, "JSON strings are not equal", msgAndArgs...)
}