-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjson.go
68 lines (57 loc) · 2.03 KB
/
json.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
package equals
import (
"encoding/json"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"reflect"
)
/* AssertJson is a function that checks two dereferenced proto objects because require.Equal infinite loops */
func AssertJson(t require.TestingT, want interface{}, got interface{}) bool {
expectJson, err := json.Marshal(want)
if !assert.NoError(t, err) {
return false
}
gotJson, err := json.Marshal(got)
if !assert.NoError(t, err) {
return false
}
return assert.JSONEq(t, string(expectJson), string(gotJson))
}
// jsoneq asserts that two JSON strings are equivalent.
//
// require.JSONEq(t, `{"hello": "world", "foo": "bar"}`, `{"foo": "bar", "hello": "world"}`)
func jsoneq(expected string, actual string, msgAndArgs ...interface{}) bool {
var expectedJSONAsInterface, actualJSONAsInterface interface{}
if err := json.Unmarshal([]byte(expected), &expectedJSONAsInterface); err != nil {
return false
}
if err := json.Unmarshal([]byte(actual), &actualJSONAsInterface); err != nil {
return false
}
return reflect.DeepEqual(expectedJSONAsInterface, actualJSONAsInterface)
}
/* equalJson is a function that checks two dereferenced proto objects because require.Equal infinite loops */
func equalJson(want interface{}, got interface{}) bool {
expectJson, err := json.Marshal(want)
if err != nil {
return false
}
gotJson, err := json.Marshal(got)
if err != nil {
return false
}
return jsoneq(string(expectJson), string(gotJson))
}
/* equalJson is a function that checks two dereferenced proto objects because require.Equal infinite loops */
func interfaceToJson(want interface{}, got interface{}) (interface{}, interface{}) {
expectJson, _ := json.Marshal(want)
gotJson, _ := json.Marshal(got)
var expectedJSONAsInterface, actualJSONAsInterface interface{}
if err := json.Unmarshal(expectJson, &expectedJSONAsInterface); err != nil {
return nil, nil
}
if err := json.Unmarshal(gotJson, &actualJSONAsInterface); err != nil {
return nil, nil
}
return expectedJSONAsInterface, actualJSONAsInterface
}