Skip to content

Commit 81804e1

Browse files
committed
Tests now discriminate between KeyNotFoundErrors and other errors
1 parent 183ade1 commit 81804e1

File tree

1 file changed

+47
-34
lines changed

1 file changed

+47
-34
lines changed

parser_test.go

Lines changed: 47 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,13 @@ var getTests = []GetTest{
158158
isFound: true,
159159
data: `3`,
160160
},
161+
GetTest{
162+
desc: `empty key`,
163+
json: `{"":{"":{"":true}}}`,
164+
path: []string{"", "", ""},
165+
isFound: true,
166+
data: `true`,
167+
},
161168

162169
// Escaped key tests
163170
GetTest{
@@ -206,48 +213,54 @@ var getTests = []GetTest{
206213

207214
// Not found key tests
208215
GetTest{
209-
desc: "non-existent key 1",
210-
json: `{"a":"b"}`,
211-
path: []string{"c"},
212-
isErr: true,
216+
desc: `empty input`,
217+
json: ``,
218+
path: []string{"a"},
219+
isFound: false,
213220
},
214221
GetTest{
215-
desc: "non-existent key 2",
216-
json: `{"a":"b"}`,
217-
path: []string{"b"},
218-
isErr: true,
222+
desc: "non-existent key 1",
223+
json: `{"a":"b"}`,
224+
path: []string{"c"},
225+
isFound: false,
219226
},
220227
GetTest{
221-
desc: "non-existent key 3",
222-
json: `{"aa":"b"}`,
223-
path: []string{"a"},
224-
isErr: true,
228+
desc: "non-existent key 2",
229+
json: `{"a":"b"}`,
230+
path: []string{"b"},
231+
isFound: false,
225232
},
226233
GetTest{
227-
desc: "apply scope of parent when search for nested key",
228-
json: `{"a": { "b": 1}, "c": 2 }`,
229-
path: []string{"a", "b", "c"},
230-
isErr: true,
234+
desc: "non-existent key 3",
235+
json: `{"aa":"b"}`,
236+
path: []string{"a"},
237+
isFound: false,
231238
},
232239
GetTest{
233-
desc: `apply scope to key level`,
234-
json: `{"a": { "b": 1}, "c": 2 }`,
235-
path: []string{"b"},
236-
isErr: true,
240+
desc: "apply scope of parent when search for nested key",
241+
json: `{"a": { "b": 1}, "c": 2 }`,
242+
path: []string{"a", "b", "c"},
243+
isFound: false,
237244
},
238245
GetTest{
239-
desc: `handle escaped quote in key name in JSON`,
240-
json: `{"key\"key": 1}`,
241-
path: []string{"key"},
242-
isErr: true,
246+
desc: `apply scope to key level`,
247+
json: `{"a": { "b": 1}, "c": 2 }`,
248+
path: []string{"b"},
249+
isFound: false,
250+
},
251+
GetTest{
252+
desc: `handle escaped quote in key name in JSON`,
253+
json: `{"key\"key": 1}`,
254+
path: []string{"key"},
255+
isFound: false,
243256
},
244257

245258
// Error/invalid tests
246259
GetTest{
247-
desc: `handle escaped quote in key name in JSON`,
248-
json: `{"key\"key": 1}`,
249-
path: []string{"key"},
250-
isErr: true,
260+
desc: `handle escaped quote in key name in JSON`,
261+
json: `{"key\"key": 1}`,
262+
path: []string{"key"},
263+
isFound: false,
251264
},
252265
GetTest{
253266
desc: `missing closing brace, but can still find key`,
@@ -294,10 +307,10 @@ var getTests = []GetTest{
294307
},
295308

296309
GetTest{ // This test returns not found instead of a parse error, as checking for the malformed JSON would reduce performance
297-
desc: "malformed key (followed by comma followed by colon)",
298-
json: `{"a",:1}`,
299-
path: []string{"a"},
300-
isErr: true,
310+
desc: "malformed key (followed by comma followed by colon)",
311+
json: `{"a",:1}`,
312+
path: []string{"a"},
313+
isFound: false,
301314
},
302315
GetTest{ // This test returns a match instead of a parse error, as checking for the malformed JSON would reduce performance (this is not ideal)
303316
desc: "malformed 'colon chain', lookup first string",
@@ -471,8 +484,8 @@ var getArrayTests = []GetTest{
471484
// checkFoundAndNoError checks the dataType and error return from Get*() against the test case expectations.
472485
// Returns true the test should proceed to checking the actual data returned from Get*(), or false if the test is finished.
473486
func getTestCheckFoundAndNoError(t *testing.T, testKind string, test GetTest, jtype ValueType, value interface{}, err error) bool {
474-
isFound := (jtype != NotExist) && (err != KeyPathNotFoundError)
475-
isErr := (err != nil)
487+
isFound := (err != KeyPathNotFoundError)
488+
isErr := (err != nil && err != KeyPathNotFoundError)
476489

477490
if test.isErr != isErr {
478491
// If the call didn't match the error expectation, fail

0 commit comments

Comments
 (0)