generated from WillAbides/goproject-tmpl
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtestutil.go
210 lines (195 loc) · 5.12 KB
/
testutil.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
package rjson
import (
"errors"
"fmt"
"strings"
"unicode/utf8"
)
var arrayHandlers = map[string]ArrayValueHandlerFunc{
"alwaysZero": func(data []byte) (p int, err error) { return 0, nil },
"skipValue": func(data []byte) (p int, err error) { return SkipValue(data, nil) },
"alwaysError": func(data []byte) (p int, err error) { return 0, fmt.Errorf("error") },
"skipHalf": func(data []byte) (p int, err error) {
value, err := SkipValue(data, nil)
return value / 2, err
},
"skipDouble": func(data []byte) (p int, err error) {
value, err := SkipValue(data, nil)
return value * 2, err
},
"skipAll": func(data []byte) (p int, err error) { return len(data), nil },
"skipAllTimes2": func(data []byte) (p int, err error) { return len(data) * 2, nil },
"skipAllPlus1": func(data []byte) (p int, err error) { return len(data) + 1, nil },
"neg1": func(data []byte) (p int, err error) { return -1, nil },
"neg100_000": func(data []byte) (p int, err error) { return -100_000, nil },
}
type nCallArrayValueHandler struct {
handler ArrayValueHandler
callCount int
n int
}
func (h *nCallArrayValueHandler) HandleArrayValue(data []byte) (p int, err error) {
h.callCount++
if h.callCount == h.n {
return h.handler.HandleArrayValue(data)
}
return 0, nil
}
func stdLibCompatibleValue(rjsonVal interface{}) interface{} {
switch v := rjsonVal.(type) {
case string:
return StdLibCompatibleString(v)
case map[string]interface{}:
return StdLibCompatibleMap(v)
case []interface{}:
return StdLibCompatibleSlice(v)
default:
return v
}
}
type multiPathErr []*pathErr
func (m multiPathErr) Error() string {
msg := ""
for _, err := range m {
msg += err.Error() + "\n"
}
return msg
}
type pathErr struct {
path []string
msg string
}
func (p *pathErr) Error() string {
return strings.Join(p.path, ".") + ": " + p.msg
}
func newPathErr(path []string, msg string, args ...interface{}) *pathErr {
return &pathErr{
path: path,
msg: fmt.Sprintf(msg, args...),
}
}
func wrongValErr(path []string, a, b interface{}) *pathErr {
return newPathErr(path, "wrong value. wanted %v but got %v", a, b)
}
func wrongTypeErr(path []string, a, b interface{}) *pathErr {
return newPathErr(path, "wrong type. wanted %T but got %T", a, b)
}
func fuzzCompare(want, got interface{}) error {
switch want.(type) {
case map[string]interface{}, []interface{}:
return ifaceCompare(want, got, []string{"ROOT"})
}
if got != want {
return fmt.Errorf("expected %v but got %v", want, got)
}
return nil
}
//nolint:gocyclo // oh the complexity
func ifaceCompare(want, got interface{}, path []string) error {
var err error
switch wantVal := want.(type) {
case string:
gotVal, ok := got.(string)
if !ok {
return wrongTypeErr(path, wantVal, got)
}
if wantVal != gotVal {
return wrongValErr(path, wantVal, gotVal)
}
return nil
case float64:
bVal, ok := got.(float64)
if !ok {
return wrongTypeErr(path, wantVal, got)
}
if wantVal != bVal {
return wrongValErr(path, wantVal, bVal)
}
return nil
case bool:
bVal, ok := got.(bool)
if !ok {
return wrongTypeErr(path, wantVal, got)
}
if wantVal != bVal {
return wrongValErr(path, wantVal, bVal)
}
return nil
case nil:
if got != nil {
return newPathErr(path, "wrong value. wanted nil but got %v", got)
}
return nil
case map[string]interface{}:
gotVal, ok := got.(map[string]interface{})
if !ok {
return wrongTypeErr(path, wantVal, got)
}
var multiErr multiPathErr
for k, wv := range wantVal {
var gv interface{}
gv, ok = gotVal[k]
if !ok {
multiErr = append(multiErr, newPathErr(append(path, k), "missing map key"))
continue
}
if strings.ContainsRune(k, utf8.RuneError) {
continue
}
err = ifaceCompare(wv, gv, append(path, k))
var pe *pathErr
if errors.As(err, &pe) {
multiErr = append(multiErr, pe)
}
var multiErr2 multiPathErr
if errors.As(err, &multiErr2) {
multiErr = append(multiErr, multiErr2...)
}
}
for k := range gotVal {
_, ok = wantVal[k]
if !ok {
multiErr = append(multiErr, newPathErr(append(path, k), "extra map key"))
}
}
if len(multiErr) > 0 {
return multiErr
}
return nil
case []interface{}:
gotVal, ok := got.([]interface{})
if !ok {
return wrongTypeErr(path, wantVal, got)
}
var multiErr multiPathErr
for i := range wantVal {
pathElem := fmt.Sprintf(`[%d]`, i)
if i >= len(gotVal) {
multiErr = append(multiErr, newPathErr(append(path, pathElem), "missing value"))
}
err = ifaceCompare(wantVal[i], gotVal[i], append(path, pathElem))
var pe *pathErr
if errors.As(err, &pe) {
multiErr = append(multiErr, pe)
}
var multiErr2 multiPathErr
if errors.As(err, &multiErr2) {
multiErr = append(multiErr, multiErr2...)
}
}
for i := len(gotVal); i < len(wantVal); i++ {
pathElem := fmt.Sprintf(`[%d]`, i)
multiErr = append(multiErr, newPathErr(append(path, pathElem), "extra value"))
}
if len(multiErr) > 0 {
return multiErr
}
return nil
default:
return newPathErr(path, "unhandled type %T", wantVal)
}
}
func dirtyStringBuffer() *[]byte {
buf := []byte(`this is a dirty buffer`)
return &buf
}