forked from avakar/toml-test
-
Notifications
You must be signed in to change notification settings - Fork 1
/
json.go
216 lines (193 loc) · 5.44 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
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
package main
import (
"math"
"strconv"
"strings"
"time"
)
// compareJson consumes the recursive structure of both `expected` and `test`
// simultaneously. If anything is unequal, the result has failed and
// comparison stops.
//
// N.B. `reflect.DeepEqual` could work here, but it won't tell us how the
// two structures are different.
func (r result) cmpJson(expected, test interface{}) result {
switch e := expected.(type) {
case map[string]interface{}:
return r.cmpJsonMaps(e, test)
case []interface{}:
return r.cmpJsonArrays(e, test)
default:
return r.failedf("Key '%s' in expected output should be a map or a "+
"list of maps, but it's a %T.", r.key, expected)
}
panic("unreachable")
}
func (r result) cmpJsonMaps(
e map[string]interface{}, test interface{}) result {
t, ok := test.(map[string]interface{})
if !ok {
return r.mismatch("table", t)
}
// Check to make sure both or neither are values.
if isValue(e) && !isValue(t) {
return r.failedf("Key '%s' is supposed to be a value, but the "+
"parser reports it as a table.", r.key)
}
if !isValue(e) && isValue(t) {
return r.failedf("Key '%s' is supposed to be a table, but the "+
"parser reports it as a value.", r.key)
}
if isValue(e) && isValue(t) {
return r.cmpJsonValues(e, t)
}
// Check that the keys of each map are equivalent.
for k, _ := range e {
if _, ok := t[k]; !ok {
bunk := r.kjoin(k)
return bunk.failedf("Could not find key '%s' in parser output.",
bunk.key)
}
}
for k, _ := range t {
if _, ok := e[k]; !ok {
bunk := r.kjoin(k)
return bunk.failedf("Could not find key '%s' in expected output.",
bunk.key)
}
}
// Okay, now make sure that each value is equivalent.
for k, _ := range e {
if sub := r.kjoin(k).cmpJson(e[k], t[k]); sub.failed() {
return sub
}
}
return r
}
func (r result) cmpJsonArrays(e, t interface{}) result {
ea, ok := e.([]interface{})
if !ok {
return r.failedf("BUG in test case. 'value' should be a JSON array "+
"when 'type' indicates 'array', but it is a %T.", e)
}
ta, ok := t.([]interface{})
if !ok {
return r.failedf("Malformed parser output. 'value' should be a "+
"JSON array when 'type' indicates 'array', but it is a %T.", t)
}
if len(ea) != len(ta) {
return r.failedf("Array lengths differ for key '%s'. Expected a "+
"length of %d but got %d.", r.key, len(ea), len(ta))
}
for i := 0; i < len(ea); i++ {
if sub := r.cmpJson(ea[i], ta[i]); sub.failed() {
return sub
}
}
return r
}
func (r result) cmpJsonValues(e, t map[string]interface{}) result {
etype, ok := e["type"].(string)
if !ok {
return r.failedf("BUG in test case. 'type' should be a string, "+
"but it is a %T.", e["type"])
}
ttype, ok := t["type"].(string)
if !ok {
return r.failedf("Malformed parser output. 'type' should be a "+
"string, but it is a %T.", t["type"])
}
if etype != ttype {
return r.valMismatch(etype, ttype)
}
// If this is an array, then we've got to do some work to check
// equality.
if etype == "array" {
return r.cmpJsonArrays(e["value"], t["value"])
} else {
// Atomic values are always strings
evalue, ok := e["value"].(string)
if !ok {
return r.failedf("BUG in test case. 'value' "+
"should be a string, but it is a %T.",
e["value"])
}
tvalue, ok := t["value"].(string)
if !ok {
return r.failedf("Malformed parser output. 'value' "+
"should be a string but it is a %T.",
t["value"])
}
// Excepting floats and datetimes, other values can be
// compared as strings.
switch etype {
case "float":
return r.cmpFloats(evalue, tvalue);
case "datetime":
return r.cmpAsDatetimes(evalue, tvalue);
default:
return r.cmpAsStrings(evalue, tvalue);
}
}
}
func (r result) cmpAsStrings(e, t string) result {
if e != t {
return r.failedf("Values for key '%s' don't match. Expected a "+
"value of '%s' but got '%s'.", r.key, e, t)
}
return r
}
func (r result) cmpFloats(e, t string) result {
ef, err := strconv.ParseFloat(e, 64)
if err != nil {
return r.failedf("BUG in test case. Could not read '%s' as a "+
"float value for key '%s'.", e, r.key)
}
// Go lacks a -NaN representation, so trim the negation sign from any
// -nans potentially coming from the parser output
if math.IsNaN(ef) {
t = strings.TrimPrefix(t, "-")
}
tf, err := strconv.ParseFloat(t, 64)
if err != nil {
return r.failedf("Malformed parser output. Could not read '%s' "+
"as a float value for key '%s'.", t, r.key)
}
// all comparisons against NaN are false, so explicitly check that the
// values are not both NaN
if ef != tf && !(math.IsNaN(ef) && math.IsNaN(tf)) {
return r.failedf("Values for key '%s' don't match. Expected a "+
"value of '%v' but got '%v'.", r.key, ef, tf)
}
return r
}
func (r result) cmpAsDatetimes(e, t string) result {
var err error
ef, err := time.Parse(time.RFC3339Nano, e)
if err != nil {
return r.failedf("BUG in test case. Could not read '%s' as a "+
"datetime value for key '%s'.", e, r.key)
}
tf, err := time.Parse(time.RFC3339Nano, t)
if err != nil {
return r.failedf("Malformed parser output. Could not read '%s' "+
"as datetime value for key '%s'.", t, r.key)
}
if !ef.Equal(tf) {
return r.failedf("Values for key '%s' don't match. Expected a "+
"value of '%v' but got '%v'.", r.key, ef, tf)
}
return r
}
func isValue(m map[string]interface{}) bool {
if len(m) != 2 {
return false
}
if _, ok := m["type"]; !ok {
return false
}
if _, ok := m["value"]; !ok {
return false
}
return true
}