@@ -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
190
214
var setTests = []SetTest {
@@ -380,6 +404,19 @@ var setTests = []SetTest{
380
404
isFound : true ,
381
405
data : `{"top":["one", "two", "value"]}` ,
382
406
},
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
+ },
383
420
}
384
421
385
422
var getTests = []GetTest {
@@ -831,6 +868,12 @@ var getIntTests = []GetTest{
831
868
path : []string {"p" },
832
869
isErr : true ,
833
870
},
871
+ {
872
+ desc : `read non-numeric value as integer` ,
873
+ json : `{"a": "b", "c": "d"}` ,
874
+ path : []string {"c" },
875
+ isErr : true ,
876
+ },
834
877
}
835
878
836
879
var getFloatTests = []GetTest {
@@ -848,6 +891,12 @@ var getFloatTests = []GetTest{
848
891
isFound : true ,
849
892
data : float64 (23.41323 ),
850
893
},
894
+ {
895
+ desc : `read non-numeric value as float` ,
896
+ json : `{"a": "b", "c": "d"}` ,
897
+ path : []string {"c" },
898
+ isErr : true ,
899
+ },
851
900
}
852
901
853
902
var getStringTests = []GetTest {
@@ -900,6 +949,43 @@ var getStringTests = []GetTest{
900
949
isFound : false ,
901
950
data : `` ,
902
951
},
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
+ },
903
989
}
904
990
905
991
var getBoolTests = []GetTest {
@@ -1175,6 +1261,19 @@ func TestGetString(t *testing.T) {
1175
1261
)
1176
1262
}
1177
1263
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
+
1178
1277
func TestGetInt (t * testing.T ) {
1179
1278
runGetTests (t , "GetInt()" , getIntTests ,
1180
1279
func (test GetTest ) (value interface {}, dataType ValueType , err error ) {
0 commit comments