Skip to content

Commit 2e826ed

Browse files
committed
add more test case
1 parent 1a29609 commit 2e826ed

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

190214
var setTests = []SetTest{
@@ -380,6 +404,19 @@ var setTests = []SetTest{
380404
isFound: true,
381405
data: `{"top":["one", "two", "value"]}`,
382406
},
407+
{
408+
desc: "set non-exist key",
409+
json: `{"test":"input"}`,
410+
setData: `"new value"`,
411+
isFound: false,
412+
},
413+
{
414+
desc: "set key in invalid json",
415+
json: `{"test"::"input"}`,
416+
path: []string{"test"},
417+
setData: "new value",
418+
isErr: true,
419+
},
383420
}
384421

385422
var getTests = []GetTest{
@@ -831,6 +868,12 @@ var getIntTests = []GetTest{
831868
path: []string{"p"},
832869
isErr: true,
833870
},
871+
{
872+
desc: `read non-numeric value as integer`,
873+
json: `{"a": "b", "c": "d"}`,
874+
path: []string{"c"},
875+
isErr: true,
876+
},
834877
}
835878

836879
var getFloatTests = []GetTest{
@@ -848,6 +891,12 @@ var getFloatTests = []GetTest{
848891
isFound: true,
849892
data: float64(23.41323),
850893
},
894+
{
895+
desc: `read non-numeric value as float`,
896+
json: `{"a": "b", "c": "d"}`,
897+
path: []string{"c"},
898+
isErr: true,
899+
},
851900
}
852901

853902
var getStringTests = []GetTest{
@@ -900,6 +949,43 @@ var getStringTests = []GetTest{
900949
isFound: false,
901950
data: ``,
902951
},
952+
{
953+
desc: `read non-string as string`,
954+
json: `{"c": true}`,
955+
path: []string{"c"},
956+
isErr: true,
957+
},
958+
}
959+
960+
var getUnsafeStringTests = []GetTest{
961+
{
962+
desc: `Do not translate Unicode symbols`,
963+
json: `{"c": "test"}`,
964+
path: []string{"c"},
965+
isFound: true,
966+
data: `test`,
967+
},
968+
{
969+
desc: `Do not translate Unicode symbols`,
970+
json: `{"c": "15\u00b0C"}`,
971+
path: []string{"c"},
972+
isFound: true,
973+
data: `15\u00b0C`,
974+
},
975+
{
976+
desc: `Do not translate supplementary Unicode symbols`,
977+
json: `{"c": "\uD83D\uDE03"}`, // Smiley face (UTF16 surrogate pair)
978+
path: []string{"c"},
979+
isFound: true,
980+
data: `\uD83D\uDE03`, // Smiley face
981+
},
982+
{
983+
desc: `Do not translate escape symbols`,
984+
json: `{"c": "\\\""}`,
985+
path: []string{"c"},
986+
isFound: true,
987+
data: `\\\"`,
988+
},
903989
}
904990

905991
var getBoolTests = []GetTest{
@@ -1175,6 +1261,19 @@ func TestGetString(t *testing.T) {
11751261
)
11761262
}
11771263

1264+
func TestGetUnsafeString(t *testing.T) {
1265+
runGetTests(t, "GetUnsafeString()", getUnsafeStringTests,
1266+
func(test GetTest) (value interface{}, dataType ValueType, err error) {
1267+
value, err = GetUnsafeString([]byte(test.json), test.path...)
1268+
return value, String, err
1269+
},
1270+
func(test GetTest, value interface{}) (bool, interface{}) {
1271+
expected := test.data.(string)
1272+
return expected == value.(string), expected
1273+
},
1274+
)
1275+
}
1276+
11781277
func TestGetInt(t *testing.T) {
11791278
runGetTests(t, "GetInt()", getIntTests,
11801279
func(test GetTest) (value interface{}, dataType ValueType, err error) {

0 commit comments

Comments
 (0)