Skip to content

Commit

Permalink
add more test case (#189)
Browse files Browse the repository at this point in the history
**Description**: Add more test case

Add more testcase to improve the coverage(88.2% to 90%)
  • Loading branch information
buger committed Mar 22, 2020
2 parents cc7baf7 + 86818d0 commit f7e751e
Showing 1 changed file with 99 additions and 0 deletions.
99 changes: 99 additions & 0 deletions parser_test.go
Expand Up @@ -185,6 +185,30 @@ var deleteTests = []DeleteTest{
path: []string{"b"},
data: `{"a": "1" , "c": 3}`,
},
{
desc: "Delete non-last key",
json: `{"test":"input","test1":"input1"}`,
path: []string{"test"},
data: `{"test1":"input1"}`,
},
{
desc: "Delete non-exist key",
json: `{"test:":"input"}`,
path: []string{"test", "test1"},
data: `{"test:":"input"}`,
},
{
desc: "Delete non-last object in an array",
json: `[{"key":"val-obj1"},{"key2":"val-obj2"}]`,
path: []string{"[0]"},
data: `[{"key2":"val-obj2"}]`,
},
{
desc: "Delete non-first object in an array",
json: `[{"key":"val-obj1"},{"key2":"val-obj2"}]`,
path: []string{"[1]"},
data: `[{"key":"val-obj1"}]`,
},
{
desc: "Issue #188: infinite loop in Delete",
json: `^_�^C^A^@[`,
Expand Down Expand Up @@ -392,6 +416,19 @@ var setTests = []SetTest{
isFound: true,
data: `{"top":["one", "two", "value"]}`,
},
{
desc: "set non-exist key",
json: `{"test":"input"}`,
setData: `"new value"`,
isFound: false,
},
{
desc: "set key in invalid json",
json: `{"test"::"input"}`,
path: []string{"test"},
setData: "new value",
isErr: true,
},
{
desc: "set unknown key (simple object within nested array)",
json: `{"test":{"key":[{"innerKey":"innerKeyValue", "innerKey2":"innerKeyValue2"}]}}`,
Expand Down Expand Up @@ -858,6 +895,12 @@ var getIntTests = []GetTest{
path: []string{"p"},
isErr: true,
},
{
desc: `read non-numeric value as integer`,
json: `{"a": "b", "c": "d"}`,
path: []string{"c"},
isErr: true,
},
}

var getFloatTests = []GetTest{
Expand All @@ -875,6 +918,12 @@ var getFloatTests = []GetTest{
isFound: true,
data: float64(23.41323),
},
{
desc: `read non-numeric value as float`,
json: `{"a": "b", "c": "d"}`,
path: []string{"c"},
isErr: true,
},
}

var getStringTests = []GetTest{
Expand Down Expand Up @@ -927,6 +976,43 @@ var getStringTests = []GetTest{
isFound: false,
data: ``,
},
{
desc: `read non-string as string`,
json: `{"c": true}`,
path: []string{"c"},
isErr: true,
},
}

var getUnsafeStringTests = []GetTest{
{
desc: `Do not translate Unicode symbols`,
json: `{"c": "test"}`,
path: []string{"c"},
isFound: true,
data: `test`,
},
{
desc: `Do not translate Unicode symbols`,
json: `{"c": "15\u00b0C"}`,
path: []string{"c"},
isFound: true,
data: `15\u00b0C`,
},
{
desc: `Do not translate supplementary Unicode symbols`,
json: `{"c": "\uD83D\uDE03"}`, // Smiley face (UTF16 surrogate pair)
path: []string{"c"},
isFound: true,
data: `\uD83D\uDE03`, // Smiley face
},
{
desc: `Do not translate escape symbols`,
json: `{"c": "\\\""}`,
path: []string{"c"},
isFound: true,
data: `\\\"`,
},
}

var getBoolTests = []GetTest{
Expand Down Expand Up @@ -1202,6 +1288,19 @@ func TestGetString(t *testing.T) {
)
}

func TestGetUnsafeString(t *testing.T) {
runGetTests(t, "GetUnsafeString()", getUnsafeStringTests,
func(test GetTest) (value interface{}, dataType ValueType, err error) {
value, err = GetUnsafeString([]byte(test.json), test.path...)
return value, String, err
},
func(test GetTest, value interface{}) (bool, interface{}) {
expected := test.data.(string)
return expected == value.(string), expected
},
)
}

func TestGetInt(t *testing.T) {
runGetTests(t, "GetInt()", getIntTests,
func(test GetTest) (value interface{}, dataType ValueType, err error) {
Expand Down

0 comments on commit f7e751e

Please sign in to comment.