Skip to content

Commit f7e751e

Browse files
authored
add more test case (#189)
**Description**: Add more test case Add more testcase to improve the coverage(88.2% to 90%)
2 parents cc7baf7 + 86818d0 commit f7e751e

File tree

1 file changed

+99
-0
lines changed

1 file changed

+99
-0
lines changed

parser_test.go

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,30 @@ var deleteTests = []DeleteTest{
185185
path: []string{"b"},
186186
data: `{"a": "1" , "c": 3}`,
187187
},
188+
{
189+
desc: "Delete non-last key",
190+
json: `{"test":"input","test1":"input1"}`,
191+
path: []string{"test"},
192+
data: `{"test1":"input1"}`,
193+
},
194+
{
195+
desc: "Delete non-exist key",
196+
json: `{"test:":"input"}`,
197+
path: []string{"test", "test1"},
198+
data: `{"test:":"input"}`,
199+
},
200+
{
201+
desc: "Delete non-last object in an array",
202+
json: `[{"key":"val-obj1"},{"key2":"val-obj2"}]`,
203+
path: []string{"[0]"},
204+
data: `[{"key2":"val-obj2"}]`,
205+
},
206+
{
207+
desc: "Delete non-first object in an array",
208+
json: `[{"key":"val-obj1"},{"key2":"val-obj2"}]`,
209+
path: []string{"[1]"},
210+
data: `[{"key":"val-obj1"}]`,
211+
},
188212
{
189213
desc: "Issue #188: infinite loop in Delete",
190214
json: `^_�^C^A^@[`,
@@ -392,6 +416,19 @@ var setTests = []SetTest{
392416
isFound: true,
393417
data: `{"top":["one", "two", "value"]}`,
394418
},
419+
{
420+
desc: "set non-exist key",
421+
json: `{"test":"input"}`,
422+
setData: `"new value"`,
423+
isFound: false,
424+
},
425+
{
426+
desc: "set key in invalid json",
427+
json: `{"test"::"input"}`,
428+
path: []string{"test"},
429+
setData: "new value",
430+
isErr: true,
431+
},
395432
{
396433
desc: "set unknown key (simple object within nested array)",
397434
json: `{"test":{"key":[{"innerKey":"innerKeyValue", "innerKey2":"innerKeyValue2"}]}}`,
@@ -858,6 +895,12 @@ var getIntTests = []GetTest{
858895
path: []string{"p"},
859896
isErr: true,
860897
},
898+
{
899+
desc: `read non-numeric value as integer`,
900+
json: `{"a": "b", "c": "d"}`,
901+
path: []string{"c"},
902+
isErr: true,
903+
},
861904
}
862905

863906
var getFloatTests = []GetTest{
@@ -875,6 +918,12 @@ var getFloatTests = []GetTest{
875918
isFound: true,
876919
data: float64(23.41323),
877920
},
921+
{
922+
desc: `read non-numeric value as float`,
923+
json: `{"a": "b", "c": "d"}`,
924+
path: []string{"c"},
925+
isErr: true,
926+
},
878927
}
879928

880929
var getStringTests = []GetTest{
@@ -927,6 +976,43 @@ var getStringTests = []GetTest{
927976
isFound: false,
928977
data: ``,
929978
},
979+
{
980+
desc: `read non-string as string`,
981+
json: `{"c": true}`,
982+
path: []string{"c"},
983+
isErr: true,
984+
},
985+
}
986+
987+
var getUnsafeStringTests = []GetTest{
988+
{
989+
desc: `Do not translate Unicode symbols`,
990+
json: `{"c": "test"}`,
991+
path: []string{"c"},
992+
isFound: true,
993+
data: `test`,
994+
},
995+
{
996+
desc: `Do not translate Unicode symbols`,
997+
json: `{"c": "15\u00b0C"}`,
998+
path: []string{"c"},
999+
isFound: true,
1000+
data: `15\u00b0C`,
1001+
},
1002+
{
1003+
desc: `Do not translate supplementary Unicode symbols`,
1004+
json: `{"c": "\uD83D\uDE03"}`, // Smiley face (UTF16 surrogate pair)
1005+
path: []string{"c"},
1006+
isFound: true,
1007+
data: `\uD83D\uDE03`, // Smiley face
1008+
},
1009+
{
1010+
desc: `Do not translate escape symbols`,
1011+
json: `{"c": "\\\""}`,
1012+
path: []string{"c"},
1013+
isFound: true,
1014+
data: `\\\"`,
1015+
},
9301016
}
9311017

9321018
var getBoolTests = []GetTest{
@@ -1202,6 +1288,19 @@ func TestGetString(t *testing.T) {
12021288
)
12031289
}
12041290

1291+
func TestGetUnsafeString(t *testing.T) {
1292+
runGetTests(t, "GetUnsafeString()", getUnsafeStringTests,
1293+
func(test GetTest) (value interface{}, dataType ValueType, err error) {
1294+
value, err = GetUnsafeString([]byte(test.json), test.path...)
1295+
return value, String, err
1296+
},
1297+
func(test GetTest, value interface{}) (bool, interface{}) {
1298+
expected := test.data.(string)
1299+
return expected == value.(string), expected
1300+
},
1301+
)
1302+
}
1303+
12051304
func TestGetInt(t *testing.T) {
12061305
runGetTests(t, "GetInt()", getIntTests,
12071306
func(test GetTest) (value interface{}, dataType ValueType, err error) {

0 commit comments

Comments
 (0)