-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser_test.go
401 lines (377 loc) · 9.16 KB
/
parser_test.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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
package djson
import (
"fmt"
"reflect"
"strings"
"testing"
)
type parserTestCase struct {
desc string // Description
input string // Input string
expected map[string]interface{} // The expected result
}
func newParserTestCase(desc, input string, expected map[string]interface{}) parserTestCase {
return parserTestCase{
desc: desc,
input: input,
expected: expected,
}
}
var (
commonParserTestCases = []parserTestCase{
newParserTestCase(
"an simple key value pair", "key=val",
map[string]interface{}{
"key": "val",
},
),
newParserTestCase(
"an nested key", "key1.key2=val",
map[string]interface{}{
"key1": map[string]interface{}{
"key2": "val",
},
},
),
newParserTestCase(
"a simple list", "foo[0]=bar",
map[string]interface{}{
"foo": []interface{}{"bar"},
},
),
newParserTestCase(
"a list with an empty value", "foo[0]=",
map[string]interface{}{
"foo": []interface{}{""},
},
),
newParserTestCase(
"a map in an array", "foo[0].key=bar",
map[string]interface{}{
"foo": []interface{}{
map[string]interface{}{
"key": "bar",
},
},
},
),
newParserTestCase(
"a nested array with missing values", "foo[1][1]=bar",
map[string]interface{}{
"foo": []interface{}{
nil,
[]interface{}{
nil,
"bar",
},
},
},
),
}
)
func Test_Value_Parser_Succeeds(t *testing.T) {
testCases := []parserTestCase{
newParserTestCase(
"an boolean value true", "key=true",
map[string]interface{}{
"key": true,
},
),
newParserTestCase(
"an boolean value false", "key=false",
map[string]interface{}{
"key": false,
},
),
newParserTestCase(
"an integer value 1000", "key=1000",
map[string]interface{}{
"key": int64(1000),
},
),
newParserTestCase(
"a floating point value 10.01", "key=10.01",
map[string]interface{}{
"key": float64(10.01),
},
),
newParserTestCase(
"a floating point value 0.01", "key=0.01",
map[string]interface{}{
"key": float64(0.01),
},
),
}
testCases = append(testCases, commonParserTestCases...)
for _, test := range testCases {
m := map[string]interface{}{}
err := MergeValue(m, test.input)
assertNoError(t, err, test, m)
}
}
func Test_String_Parser_Succeeds(t *testing.T) {
testCases := []parserTestCase{
newParserTestCase(
"an boolean value true", "key=true",
map[string]interface{}{
"key": "true",
},
),
newParserTestCase(
"an boolean value false", "key=false",
map[string]interface{}{
"key": "false",
},
),
newParserTestCase(
"an integer value 1000", "key=1000",
map[string]interface{}{
"key": "1000",
},
),
newParserTestCase(
"a floating point value 10.01", "key=10.01",
map[string]interface{}{
"key": "10.01",
},
),
newParserTestCase(
"a floating point value 0.01", "key=0.01",
map[string]interface{}{
"key": "0.01",
},
),
}
testCases = append(testCases, commonParserTestCases...)
for _, test := range testCases {
m := map[string]interface{}{}
err := MergeString(m, test.input)
assertNoError(t, err, test, m)
}
}
func assertNoError(t *testing.T, err error, test parserTestCase, m map[string]interface{}) {
if err != nil {
t.Errorf("\nIn the case of %s \"%s\"\nexpected:\n\tsuccess\ngot:\n\t%+v",
test.desc, test.input, err.Error())
} else {
if !reflect.DeepEqual(m, test.expected) {
t.Errorf("\nIn the case of %s \"%s\"\nexpected:\n\t%+v\ngot:\n\t%+v",
test.desc, test.input, test.expected, m)
}
}
}
func Test_Parser_Accumulates_In_Input_Map(t *testing.T) {
testCases := []parserTestCase{
newParserTestCase(
"merging two root keys", "key1=val1,key2=val2",
map[string]interface{}{
"key1": "val1",
"key2": "val2",
},
),
newParserTestCase(
"merging with a shared root key", "foo.key1=val1,foo.key2=val2",
map[string]interface{}{
"foo": map[string]interface{}{
"key1": "val1",
"key2": "val2",
},
},
),
newParserTestCase(
"merging with jagged keys", "foo.key1=val1,foo.bar.key2=val2",
map[string]interface{}{
"foo": map[string]interface{}{
"key1": "val1",
"bar": map[string]interface{}{
"key2": "val2",
},
},
},
),
newParserTestCase(
"merging with collided keys", "foo=val1,foo=val2",
map[string]interface{}{
"foo": "val2",
},
),
newParserTestCase(
"merging arrays with extending", "foo[0]=val1,foo[1]=val2",
map[string]interface{}{
"foo": []interface{}{
"val1",
"val2",
},
},
),
newParserTestCase(
"merging arrays with filling", "foo[1]=val2,foo[0]=val1",
map[string]interface{}{
"foo": []interface{}{
"val1",
"val2",
},
},
),
newParserTestCase(
"merging arrays with rewrite", "foo[0]=val1,foo[0]=val2",
map[string]interface{}{
"foo": []interface{}{
"val2",
},
},
),
newParserTestCase(
"merging nested arrays with rewrite", "foo[0][0]=val1,foo[0][0]=val2",
map[string]interface{}{
"foo": []interface{}{
[]interface{}{
"val2",
},
},
},
),
newParserTestCase(
"merging a map in an array", "foo[0].key1=val1,foo[0].key2=val2",
map[string]interface{}{
"foo": []interface{}{
map[string]interface{}{
"key1": "val1",
"key2": "val2",
},
},
},
),
newParserTestCase(
"a value overridden by a null value", "foo[0]=val,foo[0]=null",
map[string]interface{}{
"foo": []interface{}{
nil,
},
},
),
}
for _, test := range testCases {
m := map[string]interface{}{}
parts := strings.Split(test.input, ",")
for _, input := range parts {
err := MergeValue(m, input)
if err != nil {
t.Errorf("\nIn the case of %s \"%s\"\nexpected:\n\tsuccess\ngot:\n\t%+v",
test.desc, test.input, err.Error())
}
}
if !reflect.DeepEqual(m, test.expected) {
t.Errorf("\nIn the case of %s \"%s\"\nexpected:\n\t%+v\ngot:\n\t%+v",
test.desc, test.input, test.expected, m)
}
}
}
type parserErrorTestCase struct {
desc string // Description
input string // Input string
expected string // The expected error message
}
func newParserErrorTestCase(desc, input, expected string) parserErrorTestCase {
return parserErrorTestCase{
desc: desc,
input: input,
expected: expected,
}
}
func Test_Parser_Fails(t *testing.T) {
testCases := []parserErrorTestCase{
newParserErrorTestCase(
"an empty string", "",
"unable to parse \"\", unexpected end, expecting a map key",
),
newParserErrorTestCase(
"key with no value", "foo",
"unable to parse \"foo\", unexpected end, expecting '.', '=' or '['",
),
newParserErrorTestCase(
"an array with no value", "foo[0]",
"unable to parse \"foo[0]\", unexpected end, expecting '.', '=' or '['",
),
newParserErrorTestCase(
"an array index is not defined", "foo[",
"unable to parse \"foo[\", unexpected end, expecting an array index",
),
newParserErrorTestCase(
"an array index is not complete", "foo[0",
"unable to parse \"foo[0\", unexpected end, expecting ']'",
),
newParserErrorTestCase(
"an array index is out of range", "foo[99999999999999999999]",
"unable to parse \"foo[99999999999999999999]\", strconv.Atoi: parsing \"99999999999999999999\": value out of range",
),
}
for _, test := range testCases {
m := map[string]interface{}{}
err := MergeValue(m, test.input)
if err == nil {
t.Errorf("\nIn the case of %s \"%s\"\nexpected error:\n\t%+v\ngot:\n\tsuccess",
test.desc, test.input, test.expected)
} else {
if err.Error() != test.expected {
t.Errorf("\nIn the case of %s \"%s\"\nexpected:\n\t%+v\ngot:\n\t%+v",
test.desc, test.input, test.expected, err)
}
}
}
}
func Test_tokenToError(t *testing.T) {
i := 0
for tType := tokenType(i); i < int(tokenUnknown); i++ {
tok := token{
TokenType: tType,
value: tokenType(i).String(),
}
err := tokenToError(tok)
if tok.TokenType == tokenError {
if err.Error() != tokenError.String() {
t.Errorf("Expected \"%s\", got \"%s\"", tokenError.String(), err.Error())
}
} else {
msg := fmt.Sprintf("unexpected \"%s\"", tok.value)
if err.Error() != msg {
t.Errorf("Expected \"%s\", got \"%s\"", msg, err.Error())
}
}
}
}
type fakeLexer struct {
index int
tokens []token
}
func (l *fakeLexer) drain() {
}
func (l *fakeLexer) nextToken() token {
t := l.tokens[l.index]
l.index++
return t
}
func (l *fakeLexer) reset() {
l.index = 0
}
func Test_readLeftValue(t *testing.T) {
lexer := &fakeLexer{}
lexer.tokens = append(lexer.tokens, token{TokenType: tokenAssignment})
lexer.tokens = append(lexer.tokens, token{TokenType: tokenError, value: "error"})
parser := &parser{
lex: lexer,
}
// When readRightValue fails readLeftValue should return an error.
parser.rightValueReader = parser.readRightValue
err := parser.readLeftValue(nil)
if err.Error() != "error" {
t.Errorf("Expected \"%s\", got \"%s\"", "error", err.Error())
}
// When readRightString fails readLeftValue should return an error.
lexer.reset()
parser.rightValueReader = parser.readRightString
err = parser.readLeftValue(nil)
if err.Error() != "error" {
t.Errorf("Expected \"%s\", got \"%s\"", "error", err.Error())
}
}