-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathassert.go
274 lines (246 loc) · 8 KB
/
assert.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
package jsonassert
import (
"bytes"
"encoding/json"
"fmt"
"io"
"os"
"reflect"
"sort"
)
var nilVal = reflect.ValueOf(nil)
type Testing interface {
Error(args ...interface{})
Errorf(format string, args ...interface{})
Helper()
}
// StructCheck is a convenience function for calling Equal. It is useful for verifying that the
// struct(s) you've created to receive JSON data in your application can losslessly encode and
// decode that JSON data. StructCheck will:
// 1. Open and read the text from an input JSON file
// 2. Encode the text in the JSON file to the result map, struct or slice
// 3. Decode the result map, struct, or slice back to JSON
// 4. Compare the input JSON text with the output JSON text using the Equal function
func StructCheck(t Testing, filename string, result interface{}) {
t.Helper()
var originalText, encodedText bytes.Buffer
isMapType, err := resultArgCheck(result)
if err != nil {
t.Error(err)
return
}
f, err := os.Open(filename)
if err != nil {
t.Error(err)
return
}
defer f.Close()
r := io.TeeReader(f, &originalText) // save original text to buffer while decoding JSON to result
if err := json.NewDecoder(r).Decode(result); err != nil {
t.Errorf("error decoding json in %s: %v", filename, err)
return
}
json.NewEncoder(&encodedText).Encode(result)
if isMapType {
notifyErrors(t, filename, EqualMap(originalText.Bytes(), encodedText.Bytes()))
} else {
notifyErrors(t, filename, EqualSlice(originalText.Bytes(), encodedText.Bytes()))
}
}
// EqualMap takes as its input two JSON byte slices and causes tests to fail as appropriate
// if the JSON doesn't match. Values are considered to be equivalent if they are:
// 1. Both the same data type and values match
// 2. One value is nil and the other is the default value for its data type. This is done since Go may
// not serialize JSON the same way as other languages. Go is very strict and consistent, but the JSON
// you have may not be. To make that easier, these values are considered equal:
// a. "" and nil
// b. 0.0 and nil
// c. false and nil
// d. empty slice and nil
func EqualMap(json1, json2 []byte) []error {
json1Map, err1 := getJSONMap(json1)
json2Map, err2 := getJSONMap(json2)
if err1 != nil || err2 != nil {
var errors []error
if err1 != nil {
errors = append(errors, fmt.Errorf("error unmarshalling json1: %v", err1))
}
if err2 != nil {
errors = append(errors, fmt.Errorf("error unmarshalling json2: %v", err2))
}
return errors
}
return compareMaps("", json1Map, json2Map)
}
// EqualSlice takes as its input two JSON byte slices and causes tests to fail as appropriate
// if the JSON doesn't match. Values are considered to be equivalent if they are:
// 1. Both the same data type and values match
// 2. One value is nil and the other is the default value for its data type. This is done since Go may
// not serialize JSON the same way as other languages. Go is very strict and consistent, but the JSON
// you have may not be. To make that easier, these values are considered equal:
// a. "" and nil
// b. 0.0 and nil
// c. false and nil
// d. empty slice and nil
func EqualSlice(json1, json2 []byte) []error {
json1Slice, err1 := getJSONSlice(json1)
json2Slice, err2 := getJSONSlice(json2)
if err1 != nil || err2 != nil {
var errors []error
if err1 != nil {
errors = append(errors, fmt.Errorf("error unmarshalling json1: %v", err1))
}
if err2 != nil {
errors = append(errors, fmt.Errorf("error unmarshalling json2: %v", err2))
}
return errors
}
return compareSlices("", json1Slice, json2Slice)
}
func notifyErrors(t Testing, filename string, errors []error) {
if len(errors) > 0 {
t.Errorf("*** %d errors in %s", len(errors), filename)
}
for _, err := range errors {
t.Error(err)
}
}
func resultArgCheck(result interface{}) (bool, error) {
resT := reflect.TypeOf(result)
resKind := resT.Kind()
var elemKind reflect.Kind
if resKind == reflect.Ptr {
elemKind = resT.Elem().Kind()
}
isMapType := elemKind == reflect.Struct || elemKind == reflect.Map
if resKind != reflect.Ptr || elemKind != reflect.Struct && elemKind != reflect.Map && elemKind != reflect.Slice && elemKind != reflect.Array {
return isMapType, fmt.Errorf("invalid argument: result must be a pointer to a struct, slice, or map, but got %T", result)
}
return isMapType, nil
}
func getJSONMap(text []byte) (map[string]interface{}, error) {
jsonMap := make(map[string]interface{})
return jsonMap, json.Unmarshal(text, &jsonMap)
}
func getJSONSlice(text []byte) ([]interface{}, error) {
jsonSlice := []interface{}{}
return jsonSlice, json.Unmarshal(text, &jsonSlice)
}
func compareMaps(location string, map1, map2 map[string]interface{}) []error {
var errors []error
for _, key := range keys(map1) {
errors = append(errors, compareValues(getLocation(location, key), map1[key], map2[key])...)
}
for _, key := range keys(map2) {
value1, ok := map1[key]
if !ok { // matched values were checked in the first loop, so only check missing ones here
errors = append(errors, compareValues(getLocation(location, key), value1, map2[key])...)
}
}
return errors
}
func getLocation(location, key string) string {
if location == "" {
return key
}
return fmt.Sprintf("%s.%s", location, key)
}
func keys(v map[string]interface{}) []string {
keys := []string{}
for key := range v {
keys = append(keys, key)
}
sort.Strings(keys)
return keys
}
func compareValues(location string, value1, value2 interface{}) []error {
switch v1 := value1.(type) {
case bool:
if !boolEqual(v1, value2) {
return []error{notifyError(location, value1, value2)}
}
case float64:
if !floatEqual(v1, value2) {
return []error{notifyError(location, value1, value2)}
}
case map[string]interface{}:
v2, ok := value2.(map[string]interface{})
if value2 != nil && !ok {
return []error{notifyError(location, value1, value2)}
}
return compareMaps(location, v1, v2)
case string:
if !stringEqual(v1, value2) {
return []error{notifyError(location, value1, value2)}
}
case nil:
if !isEmpty(value2) {
return []error{notifyError(location, value1, value2)}
}
default:
return compareSlices(location, value1, value2)
}
return nil
}
func notifyError(location string, value1, value2 interface{}) error {
return fmt.Errorf("%s mismatch. %v vs. %v", location, quoteString(value1), quoteString(value2))
}
func quoteString(v interface{}) string {
strV, ok := v.(string)
if ok {
return fmt.Sprintf("%q", strV)
}
return fmt.Sprintf("%v", v)
}
func boolEqual(value1 bool, value2 interface{}) bool {
return value1 == value2 || !value1 && value2 == nil
}
func floatEqual(value1 float64, value2 interface{}) bool {
return value1 == value2 || value1 == 0.0 && value2 == nil
}
func isEmpty(value interface{}) bool {
if value == "" || value == nil || value == 0.0 || value == false {
return true
}
if mapV, ok := value.(map[string]interface{}); ok {
return isMapEmpty(mapV)
}
rv := reflect.ValueOf(value)
return rv.Kind() == reflect.Slice && rv.Len() == 0
}
func isMapEmpty(value map[string]interface{}) bool {
for _, key := range keys(value) {
if !isEmpty(value[key]) {
return false
}
}
return true
}
func stringEqual(value1 string, value2 interface{}) bool {
return value1 == value2 || value1 == "" && value2 == nil
}
func compareSlices(location string, value1, value2 interface{}) []error {
rv1 := reflect.ValueOf(value1)
rv2 := reflect.ValueOf(value2)
if rv1.Kind() != reflect.Slice || (rv2.Kind() != reflect.Slice && rv2 != nilVal) {
return []error{notifyError(location, value1, value2)}
}
len1 := sliceLen(rv1)
if len1 != sliceLen(rv2) {
return []error{notifyError(location, value1, value2)}
}
if len1 == 0 {
return nil
}
var errors []error
for i := 0; i < len1; i++ {
errors = append(errors, compareValues(fmt.Sprintf("%s[%d]", location, i), rv1.Index(i).Interface(), rv2.Index(i).Interface())...)
}
return errors
}
func sliceLen(v reflect.Value) int {
if v == nilVal || v.IsNil() || v.Kind() != reflect.Slice {
return 0
}
return v.Len()
}