Skip to content

Commit 78fcf0a

Browse files
authored
Merge pull request #51 from pendo-io/improve-tests-more
Tests distinguish between error and key-not-found
2 parents 146479e + 81804e1 commit 78fcf0a

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`,
@@ -300,10 +313,10 @@ var getTests = []GetTest{
300313
},
301314

302315
GetTest{ // This test returns not found instead of a parse error, as checking for the malformed JSON would reduce performance
303-
desc: "malformed key (followed by comma followed by colon)",
304-
json: `{"a",:1}`,
305-
path: []string{"a"},
306-
isErr: true,
316+
desc: "malformed key (followed by comma followed by colon)",
317+
json: `{"a",:1}`,
318+
path: []string{"a"},
319+
isFound: false,
307320
},
308321
GetTest{ // This test returns a match instead of a parse error, as checking for the malformed JSON would reduce performance (this is not ideal)
309322
desc: "malformed 'colon chain', lookup first string",
@@ -477,8 +490,8 @@ var getArrayTests = []GetTest{
477490
// checkFoundAndNoError checks the dataType and error return from Get*() against the test case expectations.
478491
// Returns true the test should proceed to checking the actual data returned from Get*(), or false if the test is finished.
479492
func getTestCheckFoundAndNoError(t *testing.T, testKind string, test GetTest, jtype ValueType, value interface{}, err error) bool {
480-
isFound := (jtype != NotExist) && (err != KeyPathNotFoundError)
481-
isErr := (err != nil)
493+
isFound := (err != KeyPathNotFoundError)
494+
isErr := (err != nil && err != KeyPathNotFoundError)
482495

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

0 commit comments

Comments
 (0)