@@ -185,6 +185,30 @@ var deleteTests = []DeleteTest{
185
185
path : []string {"b" },
186
186
data : `{"a": "1" , "c": 3}` ,
187
187
},
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
+ },
188
212
{
189
213
desc : "Issue #188: infinite loop in Delete" ,
190
214
json : `^_�^C^A^@[` ,
@@ -392,6 +416,19 @@ var setTests = []SetTest{
392
416
isFound : true ,
393
417
data : `{"top":["one", "two", "value"]}` ,
394
418
},
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
+ },
395
432
{
396
433
desc : "set unknown key (simple object within nested array)" ,
397
434
json : `{"test":{"key":[{"innerKey":"innerKeyValue", "innerKey2":"innerKeyValue2"}]}}` ,
@@ -858,6 +895,12 @@ var getIntTests = []GetTest{
858
895
path : []string {"p" },
859
896
isErr : true ,
860
897
},
898
+ {
899
+ desc : `read non-numeric value as integer` ,
900
+ json : `{"a": "b", "c": "d"}` ,
901
+ path : []string {"c" },
902
+ isErr : true ,
903
+ },
861
904
}
862
905
863
906
var getFloatTests = []GetTest {
@@ -875,6 +918,12 @@ var getFloatTests = []GetTest{
875
918
isFound : true ,
876
919
data : float64 (23.41323 ),
877
920
},
921
+ {
922
+ desc : `read non-numeric value as float` ,
923
+ json : `{"a": "b", "c": "d"}` ,
924
+ path : []string {"c" },
925
+ isErr : true ,
926
+ },
878
927
}
879
928
880
929
var getStringTests = []GetTest {
@@ -927,6 +976,43 @@ var getStringTests = []GetTest{
927
976
isFound : false ,
928
977
data : `` ,
929
978
},
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
+ },
930
1016
}
931
1017
932
1018
var getBoolTests = []GetTest {
@@ -1202,6 +1288,19 @@ func TestGetString(t *testing.T) {
1202
1288
)
1203
1289
}
1204
1290
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
+
1205
1304
func TestGetInt (t * testing.T ) {
1206
1305
runGetTests (t , "GetInt()" , getIntTests ,
1207
1306
func (test GetTest ) (value interface {}, dataType ValueType , err error ) {
0 commit comments