1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586
|
package jsonparser
import (
"bytes"
"fmt"
_ "fmt"
"reflect"
"testing"
)
// Set it to non-empty value if want to run only specific test
var activeTest = ""
func toArray(data []byte) (result [][]byte) {
ArrayEach(data, func(value []byte, dataType ValueType, offset int, err error) {
result = append(result, value)
})
return
}
func toStringArray(data []byte) (result []string) {
ArrayEach(data, func(value []byte, dataType ValueType, offset int, err error) {
result = append(result, string(value))
})
return
}
type GetTest struct {
desc string
json string
path []string
isErr bool
isFound bool
data interface{}
}
type SetTest struct {
desc string
json string
setData string
path []string
isErr bool
isFound bool
data interface{}
}
type DeleteTest struct {
desc string
json string
path []string
data interface{}
}
var deleteTests = []DeleteTest{
{
desc: "Delete test key",
json: `{"test":"input"}`,
path: []string{"test"},
data: `{}`,
},
{
desc: "Delete object",
json: `{"test":"input"}`,
path: []string{},
data: ``,
},
{
desc: "Delete a nested object",
json: `{"test":"input","new.field":{"key": "new object"}}`,
path: []string{"new.field", "key"},
data: `{"test":"input","new.field":{}}`,
},
{
desc: "Deleting a key that doesn't exist should return the same object",
json: `{"test":"input"}`,
path: []string{"test2"},
data: `{"test":"input"}`,
},
{
desc: "Delete object in an array",
json: `{"test":[{"key":"val-obj1"}]}`,
path: []string{"test", "[0]"},
data: `{"test":[]}`,
},
{
desc: "Deleting a object in an array that doesn't exists should return the same object",
json: `{"test":[{"key":"val-obj1"}]}`,
path: []string{"test", "[1]"},
data: `{"test":[{"key":"val-obj1"}]}`,
},
{
desc: "Delete a complex object in a nested array",
json: `{"test":[{"key":[{"innerKey":"innerKeyValue"}]}]}`,
path: []string{"test", "[0]", "key", "[0]"},
data: `{"test":[{"key":[]}]}`,
},
{
desc: "Delete known key (simple type within nested array)",
json: `{"test":[{"key":["innerKey"]}]}`,
path: []string{"test", "[0]", "key", "[0]"},
data: `{"test":[{"key":[]}]}`,
},
{
desc: "Delete in empty json",
json: `{}`,
path: []string{},
data: ``,
},
{
desc: "Delete empty array",
json: `[]`,
path: []string{},
data: ``,
},
{
desc: "Deleting non json should return the same value",
json: `1.323`,
path: []string{"foo"},
data: `1.323`,
},
{
desc: "Delete known key (top level array)",
json: `[{"key":"val-obj1"}]`,
path: []string{"[0]"},
data: `[]`,
},
{ // This test deletes the key instead of returning a parse error, as checking for the malformed JSON would reduce performance (this is not ideal)
desc: `malformed with trailing whitespace`,
json: `{"a":1 `,
path: []string{"a"},
data: `{ `,
},
{ // This test dels the key instead of returning a parse error, as checking for the malformed JSON would reduce performance (this is not ideal)
desc: "malformed 'colon chain', delete b",
json: `{"a":"b":"c"}`,
path: []string{"b"},
data: `{"a":}`,
},
}
var setTests = []SetTest{
{
desc: "set unknown key (string)",
json: `{"test":"input"}`,
isFound: true,
path: []string{"new.field"},
setData: `"new value"`,
data: `{"test":"input","new.field":"new value"}`,
},
{
desc: "set known key (string)",
json: `{"test":"input"}`,
isFound: true,
path: []string{"test"},
setData: `"new value"`,
data: `{"test":"new value"}`,
},
{
desc: "set unknown key (object)",
json: `{"test":"input"}`,
isFound: true,
path: []string{"new.field"},
setData: `{"key": "new object"}`,
data: `{"test":"input","new.field":{"key": "new object"}}`,
},
{
desc: "set known key (object)",
json: `{"test":"input"}`,
isFound: true,
path: []string{"test"},
setData: `{"key": "new object"}`,
data: `{"test":{"key": "new object"}}`,
},
{
desc: "set known key (object within array)",
json: `{"test":[{"key":"val-obj1"}]}`,
isFound: true,
path: []string{"test", "[0]"},
setData: `{"key":"new object"}`,
data: `{"test":[{"key":"new object"}]}`,
},
{
desc: "set unknown key (replace object)",
json: `{"test":[{"key":"val-obj1"}]}`,
isFound: true,
path: []string{"test", "newKey"},
setData: `"new object"`,
data: `{"test":{"newKey":"new object"}}`,
},
{
desc: "set unknown key (complex object within nested array)",
json: `{"test":[{"key":[{"innerKey":"innerKeyValue"}]}]}`,
isFound: true,
path: []string{"test", "[0]", "key", "[0]", "newInnerKey"},
setData: `{"key":"new object"}`,
data: `{"test":[{"key":[{"innerKey":"innerKeyValue","newInnerKey":{"key":"new object"}}]}]}`,
},
{
desc: "set known key (complex object within nested array)",
json: `{"test":[{"key":[{"innerKey":"innerKeyValue"}]}]}`,
isFound: true,
path: []string{"test", "[0]", "key", "[0]", "innerKey"},
setData: `{"key":"new object"}`,
data: `{"test":[{"key":[{"innerKey":{"key":"new object"}}]}]}`,
},
{
desc: "set unknown key (object, partial subtree exists)",
json: `{"test":{"input":"output"}}`,
isFound: true,
path: []string{"test", "new.field"},
setData: `{"key":"new object"}`,
data: `{"test":{"input":"output","new.field":{"key":"new object"}}}`,
},
{
desc: "set unknown key (object, empty partial subtree exists)",
json: `{"test":{}}`,
isFound: true,
path: []string{"test", "new.field"},
setData: `{"key":"new object"}`,
data: `{"test":{"new.field":{"key":"new object"}}}`,
},
{
desc: "set unknown key (object, no subtree exists)",
json: `{"test":"input"}`,
isFound: true,
path: []string{"new.field", "nested", "value"},
setData: `{"key": "new object"}`,
data: `{"test":"input","new.field":{"nested":{"value":{"key": "new object"}}}}`,
},
{
desc: "set in empty json",
json: `{}`,
isFound: true,
path: []string{"foo"},
setData: `"null"`,
data: `{"foo":"null"}`,
},
{
desc: "set subtree in empty json",
json: `{}`,
isFound: true,
path: []string{"foo", "bar"},
setData: `"null"`,
data: `{"foo":{"bar":"null"}}`,
},
{
desc: "set in empty string - not found",
json: ``,
isFound: false,
path: []string{"foo"},
setData: `"null"`,
data: ``,
},
{
desc: "set in Number - not found",
json: `1.323`,
isFound: false,
path: []string{"foo"},
setData: `"null"`,
data: `1.323`,
},
{
desc: "set known key (top level array)",
json: `[{"key":"val-obj1"}]`,
isFound: true,
path: []string{"[0]", "key"},
setData: `"new object"`,
data: `[{"key":"new object"}]`,
},
{
desc: "set unknown key (trailing whitespace)",
json: `{"key":"val-obj1"} `,
isFound: true,
path: []string{"alt-key"},
setData: `"new object"`,
data: `{"key":"val-obj1","alt-key":"new object"} `,
},
{ // This test sets the key instead of returning a parse error, as checking for the malformed JSON would reduce performance (this is not ideal)
desc: `malformed with trailing whitespace`,
json: `{"a":1 `,
path: []string{"a"},
setData: `2`,
isFound: true,
data: `{"a":2 `,
},
{ // This test sets the key instead of returning a parse error, as checking for the malformed JSON would reduce performance (this is not ideal)
desc: "malformed 'colon chain', set second string",
json: `{"a":"b":"c"}`,
path: []string{"b"},
setData: `"d"`,
isFound: true,
data: `{"a":"b":"d"}`,
},
}
var getTests = []GetTest{
// Trivial tests
{
desc: "read string",
json: `""`,
isFound: true,
data: ``,
},
{
desc: "read number",
json: `0`,
isFound: true,
data: `0`,
},
{
desc: "read object",
json: `{}`,
isFound: true,
data: `{}`,
},
{
desc: "read array",
json: `[]`,
isFound: true,
data: `[]`,
},
{
desc: "read boolean",
json: `true`,
isFound: true,
data: `true`,
},
// Found key tests
{
desc: "handling multiple nested keys with same name",
json: `{"a":[{"b":1},{"b":2},3],"c":{"c":[1,2]}} }`,
path: []string{"c", "c"},
isFound: true,
data: `[1,2]`,
},
{
desc: "read basic key",
json: `{"a":"b"}`,
path: []string{"a"},
isFound: true,
data: `b`,
},
{
desc: "read basic key with space",
json: `{"a": "b"}`,
path: []string{"a"},
isFound: true,
data: `b`,
},
{
desc: "read composite key",
json: `{"a": { "b":{"c":"d" }}}`,
path: []string{"a", "b", "c"},
isFound: true,
data: `d`,
},
{
desc: `read numberic value as string`,
json: `{"a": "b", "c": 1}`,
path: []string{"c"},
isFound: true,
data: `1`,
},
{
desc: `handle multiple nested keys with same name`,
json: `{"a":[{"b":1},{"b":2},3],"c":{"c":[1,2]}} }`,
path: []string{"c", "c"},
isFound: true,
data: `[1,2]`,
},
{
desc: `read string values with quotes`,
json: `{"a": "string\"with\"quotes"}`,
path: []string{"a"},
isFound: true,
data: `string\"with\"quotes`,
},
{
desc: `read object`,
json: `{"a": { "b":{"c":"d" }}}`,
path: []string{"a", "b"},
isFound: true,
data: `{"c":"d" }`,
},
{
desc: `empty path`,
json: `{"c":"d" }`,
path: []string{},
isFound: true,
data: `{"c":"d" }`,
},
{
desc: `formatted JSON value`,
json: "{\n \"a\": \"b\"\n}",
path: []string{"a"},
isFound: true,
data: `b`,
},
{
desc: `formatted JSON value 2`,
json: "{\n \"a\":\n {\n\"b\":\n {\"c\":\"d\",\n\"e\": \"f\"}\n}\n}",
path: []string{"a", "b"},
isFound: true,
data: "{\"c\":\"d\",\n\"e\": \"f\"}",
},
{
desc: `whitespace`,
json: " \n\r\t{ \n\r\t\"whitespace\" \n\r\t: \n\r\t333 \n\r\t} \n\r\t",
path: []string{"whitespace"},
isFound: true,
data: "333",
},
{
desc: `escaped backslash quote`,
json: `{"a": "\\\""}`,
path: []string{"a"},
isFound: true,
data: `\\\"`,
},
{
desc: `unescaped backslash quote`,
json: `{"a": "\\"}`,
path: []string{"a"},
isFound: true,
data: `\\`,
},
{
desc: `unicode in JSON`,
json: `{"a": "15°C"}`,
path: []string{"a"},
isFound: true,
data: `15°C`,
},
{
desc: `no padding + nested`,
json: `{"a":{"a":"1"},"b":2}`,
path: []string{"b"},
isFound: true,
data: `2`,
},
{
desc: `no padding + nested + array`,
json: `{"a":{"b":[1,2]},"c":3}`,
path: []string{"c"},
isFound: true,
data: `3`,
},
{
desc: `empty key`,
json: `{"":{"":{"":true}}}`,
path: []string{"", "", ""},
isFound: true,
data: `true`,
},
// Escaped key tests
{
desc: `key with simple escape`,
json: `{"a\\b":1}`,
path: []string{"a\\b"},
isFound: true,
data: `1`,
},
{
desc: `key and value with whitespace escapes`,
json: `{"key\b\f\n\r\tkey":"value\b\f\n\r\tvalue"}`,
path: []string{"key\b\f\n\r\tkey"},
isFound: true,
data: `value\b\f\n\r\tvalue`, // value is not unescaped since this is Get(), but the key should work correctly
},
{
desc: `key with Unicode escape`,
json: `{"a\u00B0b":1}`,
path: []string{"a\u00B0b"},
isFound: true,
data: `1`,
},
{
desc: `key with complex escape`,
json: `{"a\uD83D\uDE03b":1}`,
path: []string{"a\U0001F603b"},
isFound: true,
data: `1`,
},
{ // This test returns a match instead of a parse error, as checking for the malformed JSON would reduce performance
desc: `malformed with trailing whitespace`,
json: `{"a":1 `,
path: []string{"a"},
isFound: true,
data: `1`,
},
{ // This test returns a match instead of a parse error, as checking for the malformed JSON would reduce performance
desc: `malformed with wrong closing bracket`,
json: `{"a":1]`,
path: []string{"a"},
isFound: true,
data: `1`,
},
// Not found key tests
{
desc: `empty input`,
json: ``,
path: []string{"a"},
isFound: false,
},
{
desc: "non-existent key 1",
json: `{"a":"b"}`,
path: []string{"c"},
isFound: false,
},
{
desc: "non-existent key 2",
json: `{"a":"b"}`,
path: []string{"b"},
isFound: false,
},
{
desc: "non-existent key 3",
json: `{"aa":"b"}`,
path: []string{"a"},
isFound: false,
},
{
desc: "apply scope of parent when search for nested key",
json: `{"a": { "b": 1}, "c": 2 }`,
path: []string{"a", "b", "c"},
isFound: false,
},
{
desc: `apply scope to key level`,
json: `{"a": { "b": 1}, "c": 2 }`,
path: []string{"b"},
isFound: false,
},
{
desc: `handle escaped quote in key name in JSON`,
json: `{"key\"key": 1}`,
path: []string{"key"},
isFound: false,
},
{
desc: "handling multiple keys with different name",
json: `{"a":{"a":1},"b":{"a":3,"c":[1,2]}}`,
path: []string{"a", "c"},
isFound: false,
},
{
desc: "handling nested json",
json: `{"a":{"b":{"c":1},"d":4}}`,
path: []string{"a", "d"},
isFound: true,
data: `4`,
},
// Error/invalid tests
{
desc: `handle escaped quote in key name in JSON`,
json: `{"key\"key": 1}`,
path: []string{"key"},
isFound: false,
},
{
desc: `missing closing brace, but can still find key`,
json: `{"a":"b"`,
path: []string{"a"},
isFound: true,
data: `b`,
},
{
desc: `missing value closing quote`,
json: `{"a":"b`,
path: []string{"a"},
isErr: true,
},
{
desc: `missing value closing curly brace`,
json: `{"a": { "b": "c"`,
path: []string{"a"},
isErr: true,
},
{
desc: `missing value closing square bracket`,
json: `{"a": [1, 2, 3 }`,
path: []string{"a"},
isErr: true,
},
{
desc: `missing value 1`,
json: `{"a":`,
path: []string{"a"},
isErr: true,
},
{
desc: `missing value 2`,
json: `{"a": `,
path: []string{"a"},
isErr: true,
},
{
desc: `missing value 3`,
json: `{"a":}`,
path: []string{"a"},
isErr: true,
},
{
desc: `malformed array (no closing brace)`,
json: `{"a":[, "b":123}`,
path: []string{"b"},
isFound: false,
},
{ // Issue #81
desc: `missing key in object in array`,
json: `{"p":{"a":[{"u":"abc","t":"th"}]}}`,
path: []string{"p", "a", "[0]", "x"},
isFound: false,
},
{ // Issue #81 counter test
desc: `existing key in object in array`,
json: `{"p":{"a":[{"u":"abc","t":"th"}]}}`,
path: []string{"p", "a", "[0]", "u"},
isFound: true,
data: "abc",
},
{ // This test returns not found instead of a parse error, as checking for the malformed JSON would reduce performance
desc: "malformed key (followed by comma followed by colon)",
json: `{"a",:1}`,
path: []string{"a"},
isFound: false,
},
{ // This test returns a match instead of a parse error, as checking for the malformed JSON would reduce performance (this is not ideal)
desc: "malformed 'colon chain', lookup first string",
json: `{"a":"b":"c"}`,
path: []string{"a"},
isFound: true,
data: "b",
},
{ // This test returns a match instead of a parse error, as checking for the malformed JSON would reduce performance (this is not ideal)
desc: "malformed 'colon chain', lookup second string",
json: `{"a":"b":"c"}`,
path: []string{"b"},
isFound: true,
data: "c",
},
// Array index paths
{
desc: "last key in path is index",
json: `{"a":[{"b":1},{"b":"2"}, 3],"c":{"c":[1,2]}}`,
path: []string{"a", "[1]"},
isFound: true,
data: `{"b":"2"}`,
},
{
desc: "get string from array",
json: `{"a":[{"b":1},"foo", 3],"c":{"c":[1,2]}}`,
path: []string{"a", "[1]"},
isFound: true,
data: "foo",
},
{
desc: "key in path is index",
json: `{"a":[{"b":"1"},{"b":"2"},3],"c":{"c":[1,2]}}`,
path: []string{"a", "[0]", "b"},
isFound: true,
data: `1`,
},
{
desc: "last key in path is an index to value in array (formatted json)",
json: `{
"a": [
{
"b": 1
},
{"b":"2"},
3
],
"c": {
"c": [
1,
2
]
}
}`,
path: []string{"a", "[1]"},
isFound: true,
data: `{"b":"2"}`,
},
{
desc: "key in path is index (formatted json)",
json: `{
"a": [
{"b": 1},
{"b": "2"},
3
],
"c": {
"c": [
1,
2
]
}
}`,
path: []string{"a", "[0]", "b"},
isFound: true,
data: `1`,
},
}
var getIntTests = []GetTest{
{
desc: `read numeric value as number`,
json: `{"a": "b", "c": 1}`,
path: []string{"c"},
isFound: true,
data: int64(1),
},
{
desc: `read numeric value as number in formatted JSON`,
json: "{\"a\": \"b\", \"c\": 1 \n}",
path: []string{"c"},
isFound: true,
data: int64(1),
},
}
var getFloatTests = []GetTest{
{
desc: `read numeric value as number`,
json: `{"a": "b", "c": 1.123}`,
path: []string{"c"},
isFound: true,
data: float64(1.123),
},
{
desc: `read numeric value as number in formatted JSON`,
json: "{\"a\": \"b\", \"c\": 23.41323 \n}",
path: []string{"c"},
isFound: true,
data: float64(23.41323),
},
}
var getStringTests = []GetTest{
{
desc: `Translate Unicode symbols`,
json: `{"c": "test"}`,
path: []string{"c"},
isFound: true,
data: `test`,
},
{
desc: `Translate Unicode symbols`,
json: `{"c": "15\u00b0C"}`,
path: []string{"c"},
isFound: true,
data: `15°C`,
},
{
desc: `Translate supplementary Unicode symbols`,
json: `{"c": "\uD83D\uDE03"}`, // Smiley face (UTF16 surrogate pair)
path: []string{"c"},
isFound: true,
data: "\U0001F603", // Smiley face
},
{
desc: `Translate escape symbols`,
json: `{"c": "\\\""}`,
path: []string{"c"},
isFound: true,
data: `\"`,
},
{
desc: `key and value with whitespace escapes`,
json: `{"key\b\f\n\r\tkey":"value\b\f\n\r\tvalue"}`,
path: []string{"key\b\f\n\r\tkey"},
isFound: true,
data: "value\b\f\n\r\tvalue", // value is unescaped since this is GetString()
},
}
var getBoolTests = []GetTest{
{
desc: `read boolean true as boolean`,
json: `{"a": "b", "c": true}`,
path: []string{"c"},
isFound: true,
data: true,
},
{
desc: `boolean true in formatted JSON`,
json: "{\"a\": \"b\", \"c\": true \n}",
path: []string{"c"},
isFound: true,
data: true,
},
{
desc: `read boolean false as boolean`,
json: `{"a": "b", "c": false}`,
path: []string{"c"},
isFound: true,
data: false,
},
{
desc: `boolean true in formatted JSON`,
json: "{\"a\": \"b\", \"c\": false \n}",
path: []string{"c"},
isFound: true,
data: false,
},
{
desc: `read fake boolean true`,
json: `{"a": txyz}`,
path: []string{"a"},
isErr: true,
},
{
desc: `read fake boolean false`,
json: `{"a": fwxyz}`,
path: []string{"a"},
isErr: true,
},
{
desc: `read boolean true with whitespace and another key`,
json: "{\r\t\n \"a\"\r\t\n :\r\t\n true\r\t\n ,\r\t\n \"b\": 1}",
path: []string{"a"},
isFound: true,
data: true,
},
}
var getArrayTests = []GetTest{
{
desc: `read array of simple values`,
json: `{"a": { "b":[1,2,3,4]}}`,
path: []string{"a", "b"},
isFound: true,
data: []string{`1`, `2`, `3`, `4`},
},
{
desc: `read array via empty path`,
json: `[1,2,3,4]`,
path: []string{},
isFound: true,
data: []string{`1`, `2`, `3`, `4`},
},
{
desc: `read array of objects`,
json: `{"a": { "b":[{"x":1},{"x":2},{"x":3},{"x":4}]}}`,
path: []string{"a", "b"},
isFound: true,
data: []string{`{"x":1}`, `{"x":2}`, `{"x":3}`, `{"x":4}`},
},
{
desc: `read nested array`,
json: `{"a": [[[1]],[[2]]]}`,
path: []string{"a"},
isFound: true,
data: []string{`[[1]]`, `[[2]]`},
},
}
// checkFoundAndNoError checks the dataType and error return from Get*() against the test case expectations.
// Returns true the test should proceed to checking the actual data returned from Get*(), or false if the test is finished.
func getTestCheckFoundAndNoError(t *testing.T, testKind string, test GetTest, jtype ValueType, value interface{}, err error) bool {
isFound := (err != KeyPathNotFoundError)
isErr := (err != nil && err != KeyPathNotFoundError)
if test.isErr != isErr {
// If the call didn't match the error expectation, fail
t.Errorf("%s test '%s' isErr mismatch: expected %t, obtained %t (err %v). Value: %v", testKind, test.desc, test.isErr, isErr, err, value)
return false
} else if isErr {
// Else, if there was an error, don't fail and don't check isFound or the value
return false
} else if test.isFound != isFound {
// Else, if the call didn't match the is-found expectation, fail
t.Errorf("%s test '%s' isFound mismatch: expected %t, obtained %t", testKind, test.desc, test.isFound, isFound)
return false
} else if !isFound {
// Else, if no value was found, don't fail and don't check the value
return false
} else {
// Else, there was no error and a value was found, so check the value
return true
}
}
func runGetTests(t *testing.T, testKind string, tests []GetTest, runner func(GetTest) (interface{}, ValueType, error), resultChecker func(GetTest, interface{}) (bool, interface{})) {
for _, test := range tests {
if activeTest != "" && test.desc != activeTest {
continue
}
fmt.Println("Running:", test.desc)
value, dataType, err := runner(test)
if getTestCheckFoundAndNoError(t, testKind, test, dataType, value, err) {
if test.data == nil {
t.Errorf("MALFORMED TEST: %v", test)
continue
}
if ok, expected := resultChecker(test, value); !ok {
if expectedBytes, ok := expected.([]byte); ok {
expected = string(expectedBytes)
}
if valueBytes, ok := value.([]byte); ok {
value = string(valueBytes)
}
t.Errorf("%s test '%s' expected to return value %v, but did returned %v instead", testKind, test.desc, expected, value)
}
}
}
}
func setTestCheckFoundAndNoError(t *testing.T, testKind string, test SetTest, value interface{}, err error) bool {
isFound := (err != KeyPathNotFoundError)
isErr := (err != nil && err != KeyPathNotFoundError)
if test.isErr != isErr {
// If the call didn't match the error expectation, fail
t.Errorf("%s test '%s' isErr mismatch: expected %t, obtained %t (err %v). Value: %v", testKind, test.desc, test.isErr, isErr, err, value)
return false
} else if isErr {
// Else, if there was an error, don't fail and don't check isFound or the value
return false
} else if test.isFound != isFound {
// Else, if the call didn't match the is-found expectation, fail
t.Errorf("%s test '%s' isFound mismatch: expected %t, obtained %t", testKind, test.desc, test.isFound, isFound)
return false
} else if !isFound {
// Else, if no value was found, don't fail and don't check the value
return false
} else {
// Else, there was no error and a value was found, so check the value
return true
}
}
func runSetTests(t *testing.T, testKind string, tests []SetTest, runner func(SetTest) (interface{}, ValueType, error), resultChecker func(SetTest, interface{}) (bool, interface{})) {
for _, test := range tests {
if activeTest != "" && test.desc != activeTest {
continue
}
fmt.Println("Running:", test.desc)
value, _, err := runner(test)
if setTestCheckFoundAndNoError(t, testKind, test, value, err) {
if test.data == nil {
t.Errorf("MALFORMED TEST: %v", test)
continue
}
if string(value.([]byte)) != test.data {
t.Errorf("Unexpected result on %s test '%s'", testKind, test.desc)
t.Log("Got: ", string(value.([]byte)))
t.Log("Expected:", test.data)
t.Log("Error: ", err)
}
}
}
}
func runDeleteTests(t *testing.T, testKind string, tests []DeleteTest, runner func(DeleteTest) interface{}, resultChecker func(DeleteTest, interface{}) (bool, interface{})) {
for _, test := range tests {
if activeTest != "" && test.desc != activeTest {
continue
}
fmt.Println("Running:", test.desc)
value := runner(test)
if test.data == nil {
t.Errorf("MALFORMED TEST: %v", test)
continue
}
if ok, expected := resultChecker(test, value); !ok {
if expectedBytes, ok := expected.([]byte); ok {
expected = string(expectedBytes)
}
if valueBytes, ok := value.([]byte); ok {
value = string(valueBytes)
}
t.Errorf("%s test '%s' expected to return value %v, but did returned %v instead", testKind, test.desc, expected, value)
}
}
}
func TestSet(t *testing.T) {
runSetTests(t, "Set()", setTests,
func(test SetTest) (value interface{}, dataType ValueType, err error) {
value, err = Set([]byte(test.json), []byte(test.setData), test.path...)
return
},
func(test SetTest, value interface{}) (bool, interface{}) {
expected := []byte(test.data.(string))
return bytes.Equal(expected, value.([]byte)), expected
},
)
}
func TestDelete(t *testing.T) {
runDeleteTests(t, "Delete()", deleteTests,
func(test DeleteTest) interface{} {
return Delete([]byte(test.json), test.path...)
},
func(test DeleteTest, value interface{}) (bool, interface{}) {
expected := []byte(test.data.(string))
return bytes.Equal(expected, value.([]byte)), expected
},
)
}
func TestGet(t *testing.T) {
runGetTests(t, "Get()", getTests,
func(test GetTest) (value interface{}, dataType ValueType, err error) {
value, dataType, _, err = Get([]byte(test.json), test.path...)
return
},
func(test GetTest, value interface{}) (bool, interface{}) {
expected := []byte(test.data.(string))
return bytes.Equal(expected, value.([]byte)), expected
},
)
}
func TestGetString(t *testing.T) {
runGetTests(t, "GetString()", getStringTests,
func(test GetTest) (value interface{}, dataType ValueType, err error) {
value, err = GetString([]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) {
value, err = GetInt([]byte(test.json), test.path...)
return value, Number, err
},
func(test GetTest, value interface{}) (bool, interface{}) {
expected := test.data.(int64)
return expected == value.(int64), expected
},
)
}
func TestGetFloat(t *testing.T) {
runGetTests(t, "GetFloat()", getFloatTests,
func(test GetTest) (value interface{}, dataType ValueType, err error) {
value, err = GetFloat([]byte(test.json), test.path...)
return value, Number, err
},
func(test GetTest, value interface{}) (bool, interface{}) {
expected := test.data.(float64)
return expected == value.(float64), expected
},
)
}
func TestGetBoolean(t *testing.T) {
runGetTests(t, "GetBoolean()", getBoolTests,
func(test GetTest) (value interface{}, dataType ValueType, err error) {
value, err = GetBoolean([]byte(test.json), test.path...)
return value, Boolean, err
},
func(test GetTest, value interface{}) (bool, interface{}) {
expected := test.data.(bool)
return expected == value.(bool), expected
},
)
}
func TestGetSlice(t *testing.T) {
runGetTests(t, "Get()-for-arrays", getArrayTests,
func(test GetTest) (value interface{}, dataType ValueType, err error) {
value, dataType, _, err = Get([]byte(test.json), test.path...)
return
},
func(test GetTest, value interface{}) (bool, interface{}) {
expected := test.data.([]string)
return reflect.DeepEqual(expected, toStringArray(value.([]byte))), expected
},
)
}
func TestArrayEach(t *testing.T) {
mock := []byte(`{"a": { "b":[{"x": 1} ,{"x":2},{ "x":3}, {"x":4} ]}}`)
count := 0
ArrayEach(mock, func(value []byte, dataType ValueType, offset int, err error) {
count++
switch count {
case 1:
if string(value) != `{"x": 1}` {
t.Errorf("Wrong first item: %s", string(value))
}
case 2:
if string(value) != `{"x":2}` {
t.Errorf("Wrong second item: %s", string(value))
}
case 3:
if string(value) != `{ "x":3}` {
t.Errorf("Wrong third item: %s", string(value))
}
case 4:
if string(value) != `{"x":4}` {
t.Errorf("Wrong forth item: %s", string(value))
}
default:
t.Errorf("Should process only 4 items")
}
}, "a", "b")
}
func TestArrayEachEmpty(t *testing.T) {
funcError := func([]byte, ValueType, int, error) { t.Errorf("Run func not allow") }
type args struct {
data []byte
cb func(value []byte, dataType ValueType, offset int, err error)
keys []string
}
tests := []struct {
name string
args args
wantOffset int
wantErr bool
}{
{"Empty array", args{[]byte("[]"), funcError, []string{}}, 1, false},
{"Empty array with space", args{[]byte("[ ]"), funcError, []string{}}, 2, false},
{"Empty array with \n", args{[]byte("[\n]"), funcError, []string{}}, 2, false},
{"Empty field array", args{[]byte("{\"data\": []}"), funcError, []string{"data"}}, 10, false},
{"Empty field array with space", args{[]byte("{\"data\": [ ]}"), funcError, []string{"data"}}, 11, false},
{"Empty field array with \n", args{[]byte("{\"data\": [\n]}"), funcError, []string{"data"}}, 11, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gotOffset, err := ArrayEach(tt.args.data, tt.args.cb, tt.args.keys...)
if (err != nil) != tt.wantErr {
t.Errorf("ArrayEach() error = %v, wantErr %v", err, tt.wantErr)
return
}
if gotOffset != tt.wantOffset {
t.Errorf("ArrayEach() = %v, want %v", gotOffset, tt.wantOffset)
}
})
}
}
type keyValueEntry struct {
key string
value string
valueType ValueType
}
func (kv keyValueEntry) String() string {
return fmt.Sprintf("[%s: %s (%s)]", kv.key, kv.value, kv.valueType)
}
type ObjectEachTest struct {
desc string
json string
isErr bool
entries []keyValueEntry
}
var objectEachTests = []ObjectEachTest{
{
desc: "empty object",
json: `{}`,
entries: []keyValueEntry{},
},
{
desc: "single key-value object",
json: `{"key": "value"}`,
entries: []keyValueEntry{
{"key", "value", String},
},
},
{
desc: "multiple key-value object with many value types",
json: `{
"key1": null,
"key2": true,
"key3": 1.23,
"key4": "string value",
"key5": [1,2,3],
"key6": {"a":"b"}
}`,
entries: []keyValueEntry{
{"key1", "null", Null},
{"key2", "true", Boolean},
{"key3", "1.23", Number},
{"key4", "string value", String},
{"key5", "[1,2,3]", Array},
{"key6", `{"a":"b"}`, Object},
},
},
{
desc: "escaped key",
json: `{"key\"\\\/\b\f\n\r\t\u00B0": "value"}`,
entries: []keyValueEntry{
{"key\"\\/\b\f\n\r\t\u00B0", "value", String},
},
},
// Error cases
{
desc: "no object present",
json: ` \t\n\r`,
isErr: true,
},
{
desc: "unmatched braces 1",
json: `{`,
isErr: true,
},
{
desc: "unmatched braces 2",
json: `}`,
isErr: true,
},
{
desc: "unmatched braces 3",
json: `}{}`,
isErr: true,
},
{
desc: "bad key (number)",
json: `{123: "value"}`,
isErr: true,
},
{
desc: "bad key (unclosed quote)",
json: `{"key: 123}`,
isErr: true,
},
{
desc: "bad value (no value)",
json: `{"key":}`,
isErr: true,
},
{
desc: "bad value (bogus value)",
json: `{"key": notavalue}`,
isErr: true,
},
{
desc: "bad entry (missing colon)",
json: `{"key" "value"}`,
isErr: true,
},
{
desc: "bad entry (no trailing comma)",
json: `{"key": "value" "key2": "value2"}`,
isErr: true,
},
{
desc: "bad entry (two commas)",
json: `{"key": "value",, "key2": "value2"}`,
isErr: true,
},
}
func TestObjectEach(t *testing.T) {
for _, test := range objectEachTests {
if activeTest != "" && test.desc != activeTest {
continue
}
// Execute ObjectEach and capture all of the entries visited, in order
var entries []keyValueEntry
err := ObjectEach([]byte(test.json), func(key, value []byte, valueType ValueType, off int) error {
entries = append(entries, keyValueEntry{
key: string(key),
value: string(value),
valueType: valueType,
})
return nil
})
// Check the correctness of the result
isErr := (err != nil)
if test.isErr != isErr {
// If the call didn't match the error expectation, fail
t.Errorf("ObjectEach test '%s' isErr mismatch: expected %t, obtained %t (err %v)", test.desc, test.isErr, isErr, err)
} else if isErr {
// Else, if there was an expected error, don't fail and don't check anything further
} else if len(test.entries) != len(entries) {
t.Errorf("ObjectEach test '%s' mismatch in number of key-value entries: expected %d, obtained %d (entries found: %s)", test.desc, len(test.entries), len(entries), entries)
} else {
for i, entry := range entries {
expectedEntry := test.entries[i]
if expectedEntry.key != entry.key {
t.Errorf("ObjectEach test '%s' key mismatch at entry %d: expected %s, obtained %s", test.desc, i, expectedEntry.key, entry.key)
break
} else if expectedEntry.value != entry.value {
t.Errorf("ObjectEach test '%s' value mismatch at entry %d: expected %s, obtained %s", test.desc, i, expectedEntry.value, entry.value)
break
} else if expectedEntry.valueType != entry.valueType {
t.Errorf("ObjectEach test '%s' value type mismatch at entry %d: expected %s, obtained %s", test.desc, i, expectedEntry.valueType, entry.valueType)
break
} else {
// Success for this entry
}
}
}
}
}
var testJson = []byte(`{"name": "Name", "order": "Order", "sum": 100, "len": 12, "isPaid": true, "nested": {"a":"test", "b":2, "nested3":{"a":"test3","b":4}, "c": "unknown"}, "nested2": {"a":"test2", "b":3}, "arr": [{"a":"zxc", "b": 1}, {"a":"123", "b":2}], "arrInt": [1,2,3,4], "intPtr": 10}`)
func TestEachKey(t *testing.T) {
paths := [][]string{
{"name"},
{"order"},
{"nested", "a"},
{"nested", "b"},
{"nested2", "a"},
{"nested", "nested3", "b"},
{"arr", "[1]", "b"},
{"arrInt", "[3]"},
{"arrInt", "[5]"}, // Should not find last key
}
keysFound := 0
EachKey(testJson, func(idx int, value []byte, vt ValueType, err error) {
keysFound++
switch idx {
case 0:
if string(value) != "Name" {
t.Error("Should find 1 key", string(value))
}
case 1:
if string(value) != "Order" {
t.Errorf("Should find 2 key")
}
case 2:
if string(value) != "test" {
t.Errorf("Should find 3 key")
}
case 3:
if string(value) != "2" {
t.Errorf("Should find 4 key")
}
case 4:
if string(value) != "test2" {
t.Error("Should find 5 key", string(value))
}
case 5:
if string(value) != "4" {
t.Errorf("Should find 6 key")
}
case 6:
if string(value) != "2" {
t.Errorf("Should find 7 key")
}
case 7:
if string(value) != "4" {
t.Error("Should find 8 key", string(value))
}
default:
t.Errorf("Should found only 8 keys")
}
}, paths...)
if keysFound != 8 {
t.Errorf("Should find 8 keys: %d", keysFound)
}
}
type ParseTest struct {
in string
intype ValueType
out interface{}
isErr bool
}
var parseBoolTests = []ParseTest{
{
in: "true",
intype: Boolean,
out: true,
},
{
in: "false",
intype: Boolean,
out: false,
},
{
in: "foo",
intype: Boolean,
isErr: true,
},
{
in: "trux",
intype: Boolean,
isErr: true,
},
{
in: "truex",
intype: Boolean,
isErr: true,
},
{
in: "",
intype: Boolean,
isErr: true,
},
}
var parseFloatTest = []ParseTest{
{
in: "0",
intype: Number,
out: float64(0),
},
{
in: "0.0",
intype: Number,
out: float64(0.0),
},
{
in: "1",
intype: Number,
out: float64(1),
},
{
in: "1.234",
intype: Number,
out: float64(1.234),
},
{
in: "1.234e5",
intype: Number,
out: float64(1.234e5),
},
{
in: "-1.234e5",
intype: Number,
out: float64(-1.234e5),
},
{
in: "+1.234e5", // Note: + sign not allowed under RFC7159, but our parser accepts it since it uses strconv.ParseFloat
intype: Number,
out: float64(1.234e5),
},
{
in: "1.2.3",
intype: Number,
isErr: true,
},
{
in: "1..1",
intype: Number,
isErr: true,
},
{
in: "1a",
intype: Number,
isErr: true,
},
{
in: "",
intype: Number,
isErr: true,
},
}
// parseTestCheckNoError checks the error return from Parse*() against the test case expectations.
// Returns true the test should proceed to checking the actual data returned from Parse*(), or false if the test is finished.
func parseTestCheckNoError(t *testing.T, testKind string, test ParseTest, value interface{}, err error) bool {
if isErr := (err != nil); test.isErr != isErr {
// If the call didn't match the error expectation, fail
t.Errorf("%s test '%s' isErr mismatch: expected %t, obtained %t (err %v). Obtained value: %v", testKind, test.in, test.isErr, isErr, err, value)
return false
} else if isErr {
// Else, if there was an error, don't fail and don't check isFound or the value
return false
} else {
// Else, there was no error and a value was found, so check the value
return true
}
}
func runParseTests(t *testing.T, testKind string, tests []ParseTest, runner func(ParseTest) (interface{}, error), resultChecker func(ParseTest, interface{}) (bool, interface{})) {
for _, test := range tests {
value, err := runner(test)
if parseTestCheckNoError(t, testKind, test, value, err) {
if test.out == nil {
t.Errorf("MALFORMED TEST: %v", test)
continue
}
if ok, expected := resultChecker(test, value); !ok {
if expectedBytes, ok := expected.([]byte); ok {
expected = string(expectedBytes)
}
if valueBytes, ok := value.([]byte); ok {
value = string(valueBytes)
}
t.Errorf("%s test '%s' expected to return value %v, but did returned %v instead", testKind, test.in, expected, value)
}
}
}
}
func TestParseBoolean(t *testing.T) {
runParseTests(t, "ParseBoolean()", parseBoolTests,
func(test ParseTest) (value interface{}, err error) {
return ParseBoolean([]byte(test.in))
},
func(test ParseTest, obtained interface{}) (bool, interface{}) {
expected := test.out.(bool)
return obtained.(bool) == expected, expected
},
)
}
func TestParseFloat(t *testing.T) {
runParseTests(t, "ParseFloat()", parseFloatTest,
func(test ParseTest) (value interface{}, err error) {
return ParseFloat([]byte(test.in))
},
func(test ParseTest, obtained interface{}) (bool, interface{}) {
expected := test.out.(float64)
return obtained.(float64) == expected, expected
},
)
}
var parseStringTest = []ParseTest{
{
in: `\uFF11`,
intype: String,
out: "\uFF11",
},
{
in: `\uFFFF`,
intype: String,
out: "\uFFFF",
},
{
in: `\uDF00`,
intype: String,
isErr: true,
},
}
func TestParseString(t *testing.T) {
runParseTests(t, "ParseString()", parseStringTest,
func(test ParseTest) (value interface{}, err error) {
return ParseString([]byte(test.in))
},
func(test ParseTest, obtained interface{}) (bool, interface{}) {
expected := test.out.(string)
return obtained.(string) == expected, expected
},
)
}
|