robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
aa1d8a2702
master
1local t =require ( 'test.testutil' ) 2local n =require ( 'test.functional.testnvim' )() 3 4local describe , it , setup =t . describe , t . it , t . setup 5local clear =n . clear 6local fn =n . fn 7local api =n . api 8local eq =t . eq 9local matches =t . matches 10local eval =n . eval 11local command =n . command 12local pcall_err =t . pcall_err 13local NIL =vim . NIL 14local source =n . source 15 16describe ( 'json_decode() function' , function () 17setup ( function () 18clear () 19source ( [[ 20language C 21function Eq(exp, act) 22let act = a:act 23let exp = a:exp 24if type(exp) != type(act) 25return 0 26endif 27if type(exp) == type({}) 28if sort(keys(exp)) !=# sort(keys(act)) 29return 0 30endif 31if sort(keys(exp)) ==# ['_TYPE', '_VAL'] 32let exp_typ = v:msgpack_types[exp._TYPE] 33let act_typ = act._TYPE 34if exp_typ isnot act_typ 35return 0 36endif 37return Eq(exp._VAL, act._VAL) 38else 39return empty(filter(copy(exp), '!Eq(v:val, act[v:key])')) 40endif 41else 42if type(exp) == type([]) 43if len(exp) != len(act) 44return 0 45endif 46return empty(filter(copy(exp), '!Eq(v:val, act[v:key])')) 47endif 48return exp ==# act 49endif 50return 1 51endfunction 52function EvalEq(exp, act_expr) 53let act = eval(a:act_expr) 54if Eq(a:exp, act) 55return 1 56else 57return string(act) 58endif 59endfunction 60]] ) 61end ) 62 63local speq =function ( expected , actual_expr ) 64eq ( 1 , fn . EvalEq ( expected , actual_expr )) 65end 66 67it ( 'accepts readfile()-style list' , function () 68eq ( 69{ Test =1 } , 70fn . json_decode ( { 71'{' , 72'\t"Test": 1' , 73'}' , 74} ) 75) 76end ) 77 78it ( 'accepts strings with newlines' , function () 79eq ( 80{ Test =1 } , 81fn . json_decode ( [[ 82{ 83"Test": 1 84} 85]] ) 86) 87end ) 88 89it ( 'parses null, true, false' , function () 90eq ( NIL , fn . json_decode ( 'null' )) 91eq ( true , fn . json_decode ( 'true' )) 92eq ( false , fn . json_decode ( 'false' )) 93end ) 94 95it ( 'fails to parse incomplete null, true, false' , function () 96eq ( 'Vim(call):E474: Expected null: n' , pcall_err ( command , 'call json_decode("n")' )) 97eq ( 'Vim(call):E474: Expected null: nu' , pcall_err ( command , 'call json_decode("nu")' )) 98eq ( 'Vim(call):E474: Expected null: nul' , pcall_err ( command , 'call json_decode("nul")' )) 99matches ( 100'Vim%(call%):E474: Expected null: nul' , 101pcall_err ( command , 'call json_decode("nul\\n\\t")' ) 102) 103 104eq ( 'Vim(call):E474: Expected true: t' , pcall_err ( command , 'call json_decode("t")' )) 105eq ( 'Vim(call):E474: Expected true: tr' , pcall_err ( command , 'call json_decode("tr")' )) 106eq ( 'Vim(call):E474: Expected true: tru' , pcall_err ( command , 'call json_decode("tru")' )) 107matches ( 108'Vim%(call%):E474: Expected true: tru' , 109pcall_err ( command , 'call json_decode("tru\\t\\n")' ) 110) 111 112eq ( 'Vim(call):E474: Expected false: f' , pcall_err ( command , 'call json_decode("f")' )) 113eq ( 'Vim(call):E474: Expected false: fa' , pcall_err ( command , 'call json_decode("fa")' )) 114eq ( 'Vim(call):E474: Expected false: fal' , pcall_err ( command , 'call json_decode("fal")' )) 115eq ( 116'Vim(call):E474: Expected false: fal <' , 117pcall_err ( command , 'call json_decode(" fal <")' ) 118) 119eq ( 'Vim(call):E474: Expected false: fals' , pcall_err ( command , 'call json_decode("fals")' )) 120end ) 121 122it ( 'parses integer numbers' , function () 123eq ( 100000 , fn . json_decode ( '100000' )) 124eq ( - 100000 , fn . json_decode ( '-100000' )) 125eq ( 100000 , fn . json_decode ( ' 100000 ' )) 126eq ( - 100000 , fn . json_decode ( ' -100000 ' )) 127eq ( 0 , fn . json_decode ( '0' )) 128eq ( 0 , fn . json_decode ( '-0' )) 129end ) 130 131it ( 'fails to parse +numbers and .number' , function () 132eq ( 'Vim(call):E474: Unidentified byte: +1000' , pcall_err ( command , 'call json_decode("+1000")' )) 133eq ( 'Vim(call):E474: Unidentified byte: .1000' , pcall_err ( command , 'call json_decode(".1000")' )) 134end ) 135 136it ( 'fails to parse numbers with leading zeroes' , function () 137eq ( 138'Vim(call):E474: Leading zeroes are not allowed: 00.1' , 139pcall_err ( command , 'call json_decode("00.1")' ) 140) 141eq ( 142'Vim(call):E474: Leading zeroes are not allowed: 01' , 143pcall_err ( command , 'call json_decode("01")' ) 144) 145eq ( 146'Vim(call):E474: Leading zeroes are not allowed: -01' , 147pcall_err ( command , 'call json_decode("-01")' ) 148) 149eq ( 150'Vim(call):E474: Leading zeroes are not allowed: -001.0' , 151pcall_err ( command , 'call json_decode("-001.0")' ) 152) 153end ) 154 155it ( 'fails to parse incomplete numbers' , function () 156eq ( 157'Vim(call):E474: Missing number after minus sign: -.1' , 158pcall_err ( command , 'call json_decode("-.1")' ) 159) 160eq ( 161'Vim(call):E474: Missing number after minus sign: -' , 162pcall_err ( command , 'call json_decode("-")' ) 163) 164eq ( 165'Vim(call):E474: Missing number after decimal dot: -1.' , 166pcall_err ( command , 'call json_decode("-1.")' ) 167) 168eq ( 169'Vim(call):E474: Missing number after decimal dot: 0.' , 170pcall_err ( command , 'call json_decode("0.")' ) 171) 172eq ( 'Vim(call):E474: Missing exponent: 0.0e' , pcall_err ( command , 'call json_decode("0.0e")' )) 173eq ( 'Vim(call):E474: Missing exponent: 0.0e+' , pcall_err ( command , 'call json_decode("0.0e+")' )) 174eq ( 'Vim(call):E474: Missing exponent: 0.0e-' , pcall_err ( command , 'call json_decode("0.0e-")' )) 175eq ( 'Vim(call):E474: Missing exponent: 0.0e-' , pcall_err ( command , 'call json_decode("0.0e-")' )) 176eq ( 177'Vim(call):E474: Missing number after decimal dot: 1.e5' , 178pcall_err ( command , 'call json_decode("1.e5")' ) 179) 180eq ( 181'Vim(call):E474: Missing number after decimal dot: 1.e+5' , 182pcall_err ( command , 'call json_decode("1.e+5")' ) 183) 184eq ( 185'Vim(call):E474: Missing number after decimal dot: 1.e+' , 186pcall_err ( command , 'call json_decode("1.e+")' ) 187) 188end ) 189 190it ( 'parses floating-point numbers' , function () 191-- Also test method call (->) syntax 192eq ( '100000.0' , eval ( '"100000.0"->json_decode()->string()' )) 193eq ( 100000.5 , fn . json_decode ( '100000.5' )) 194eq ( - 100000.5 , fn . json_decode ( '-100000.5' )) 195eq ( - 100000.5e50 , fn . json_decode ( '-100000.5e50' )) 196eq ( 100000.5e50 , fn . json_decode ( '100000.5e50' )) 197eq ( 100000.5e50 , fn . json_decode ( '100000.5e+50' )) 198eq ( - 100000.5e-50 , fn . json_decode ( '-100000.5e-50' )) 199eq ( 100000.5e-50 , fn . json_decode ( '100000.5e-50' )) 200eq ( 100000e-50 , fn . json_decode ( '100000e-50' )) 201eq ( 0.5 , fn . json_decode ( '0.5' )) 202eq ( 0.005 , fn . json_decode ( '0.005' )) 203eq ( 0.005 , fn . json_decode ( '0.00500' )) 204eq ( 0.5 , fn . json_decode ( '0.00500e+002' )) 205eq ( 0.00005 , fn . json_decode ( '0.00500e-002' )) 206 207eq ( - 0.0 , fn . json_decode ( '-0.0' )) 208eq ( - 0.0 , fn . json_decode ( '-0.0e0' )) 209eq ( - 0.0 , fn . json_decode ( '-0.0e+0' )) 210eq ( - 0.0 , fn . json_decode ( '-0.0e-0' )) 211eq ( - 0.0 , fn . json_decode ( '-0e-0' )) 212eq ( - 0.0 , fn . json_decode ( '-0e-2' )) 213eq ( - 0.0 , fn . json_decode ( '-0e+2' )) 214 215eq ( 0.0 , fn . json_decode ( '0.0' )) 216eq ( 0.0 , fn . json_decode ( '0.0e0' )) 217eq ( 0.0 , fn . json_decode ( '0.0e+0' )) 218eq ( 0.0 , fn . json_decode ( '0.0e-0' )) 219eq ( 0.0 , fn . json_decode ( '0e-0' )) 220eq ( 0.0 , fn . json_decode ( '0e-2' )) 221eq ( 0.0 , fn . json_decode ( '0e+2' )) 222end ) 223 224it ( 'fails to parse numbers with spaces inside' , function () 225eq ( 226'Vim(call):E474: Missing number after minus sign: - 1000' , 227pcall_err ( command , 'call json_decode("- 1000")' ) 228) 229eq ( 230'Vim(call):E474: Missing number after decimal dot: 0. ' , 231pcall_err ( command , 'call json_decode("0. ")' ) 232) 233eq ( 234'Vim(call):E474: Missing number after decimal dot: 0. 0' , 235pcall_err ( command , 'call json_decode("0. 0")' ) 236) 237eq ( 'Vim(call):E474: Missing exponent: 0.0e 1' , pcall_err ( command , 'call json_decode("0.0e 1")' )) 238eq ( 239'Vim(call):E474: Missing exponent: 0.0e+ 1' , 240pcall_err ( command , 'call json_decode("0.0e+ 1")' ) 241) 242eq ( 243'Vim(call):E474: Missing exponent: 0.0e- 1' , 244pcall_err ( command , 'call json_decode("0.0e- 1")' ) 245) 246end ) 247 248it ( 'fails to parse "," and ":"' , function () 249eq ( 250'Vim(call):E474: Comma not inside container: , ' , 251pcall_err ( command , 'call json_decode(" , ")' ) 252) 253eq ( 254'Vim(call):E474: Colon not inside container: : ' , 255pcall_err ( command , 'call json_decode(" : ")' ) 256) 257end ) 258 259it ( 'parses empty containers' , function () 260eq ( {} , fn . json_decode ( '[]' )) 261eq ( '[]' , eval ( 'string(json_decode("[]"))' )) 262end ) 263 264it ( 'fails to parse "[" and "{"' , function () 265eq ( 'Vim(call):E474: Unexpected end of input: {' , pcall_err ( command , 'call json_decode("{")' )) 266eq ( 'Vim(call):E474: Unexpected end of input: [' , pcall_err ( command , 'call json_decode("[")' )) 267end ) 268 269it ( 'fails to parse "}" and "]"' , function () 270eq ( 'Vim(call):E474: No container to close: ]' , pcall_err ( command , 'call json_decode("]")' )) 271eq ( 'Vim(call):E474: No container to close: }' , pcall_err ( command , 'call json_decode("}")' )) 272end ) 273 274it ( 'fails to parse containers which are closed by different brackets' , function () 275eq ( 276'Vim(call):E474: Closing dictionary with square bracket: ]' , 277pcall_err ( command , 'call json_decode("{]")' ) 278) 279eq ( 280'Vim(call):E474: Closing list with curly bracket: }' , 281pcall_err ( command , 'call json_decode("[}")' ) 282) 283end ) 284 285it ( 'fails to parse concat inside container' , function () 286eq ( 287'Vim(call):E474: Expected comma before list item: []]' , 288pcall_err ( command , 'call json_decode("[[][]]")' ) 289) 290eq ( 291'Vim(call):E474: Expected comma before list item: {}]' , 292pcall_err ( command , 'call json_decode("[{}{}]")' ) 293) 294eq ( 295'Vim(call):E474: Expected comma before list item: ]' , 296pcall_err ( command , 'call json_decode("[1 2]")' ) 297) 298eq ( 299'Vim(call):E474: Expected comma before dictionary key: ": 4}' , 300pcall_err ( command , 'call json_decode("{\\"1\\": 2 \\"3\\": 4}")' ) 301) 302eq ( 303'Vim(call):E474: Expected colon before dictionary value: , "3" 4}' , 304pcall_err ( command , 'call json_decode("{\\"1\\" 2, \\"3\\" 4}")' ) 305) 306end ) 307 308it ( 'fails to parse containers with leading comma or colon' , function () 309eq ( 'Vim(call):E474: Leading comma: ,}' , pcall_err ( command , 'call json_decode("{,}")' )) 310eq ( 'Vim(call):E474: Leading comma: ,]' , pcall_err ( command , 'call json_decode("[,]")' )) 311eq ( 312'Vim(call):E474: Using colon not in dictionary: :]' , 313pcall_err ( command , 'call json_decode("[:]")' ) 314) 315eq ( 'Vim(call):E474: Unexpected colon: :}' , pcall_err ( command , 'call json_decode("{:}")' )) 316end ) 317 318it ( 'fails to parse containers with trailing comma' , function () 319eq ( 'Vim(call):E474: Trailing comma: ]' , pcall_err ( command , 'call json_decode("[1,]")' )) 320eq ( 'Vim(call):E474: Trailing comma: }' , pcall_err ( command , 'call json_decode("{\\"1\\": 2,}")' )) 321end ) 322 323it ( 'fails to parse dictionaries with missing value' , function () 324eq ( 325'Vim(call):E474: Expected value after colon: }' , 326pcall_err ( command , 'call json_decode("{\\"1\\":}")' ) 327) 328eq ( 'Vim(call):E474: Expected value: }' , pcall_err ( command , 'call json_decode("{\\"1\\"}")' )) 329end ) 330 331it ( 'fails to parse containers with two commas or colons' , function () 332eq ( 333'Vim(call):E474: Duplicate comma: , "2": 2}' , 334pcall_err ( command , 'call json_decode("{\\"1\\": 1,, \\"2\\": 2}")' ) 335) 336eq ( 337'Vim(call):E474: Duplicate comma: , "2", 2]' , 338pcall_err ( command , 'call json_decode("[\\"1\\", 1,, \\"2\\", 2]")' ) 339) 340eq ( 341'Vim(call):E474: Duplicate colon: : 2}' , 342pcall_err ( command , 'call json_decode("{\\"1\\": 1, \\"2\\":: 2}")' ) 343) 344eq ( 345'Vim(call):E474: Comma after colon: , 2}' , 346pcall_err ( command , 'call json_decode("{\\"1\\": 1, \\"2\\":, 2}")' ) 347) 348eq ( 349'Vim(call):E474: Unexpected colon: : "2": 2}' , 350pcall_err ( command , 'call json_decode("{\\"1\\": 1,: \\"2\\": 2}")' ) 351) 352eq ( 353'Vim(call):E474: Unexpected colon: :, "2": 2}' , 354pcall_err ( command , 'call json_decode("{\\"1\\": 1:, \\"2\\": 2}")' ) 355) 356end ) 357 358it ( 'fails to parse concat of two values' , function () 359eq ( 'Vim(call):E474: Trailing characters: []' , pcall_err ( command , 'call json_decode("{}[]")' )) 360end ) 361 362it ( 'parses containers' , function () 363eq ( { 1 } , fn . json_decode ( '[1]' )) 364eq ( { NIL , 1 } , fn . json_decode ( '[null, 1]' )) 365eq ( { [ '1' ] =2 } , fn . json_decode ( '{"1": 2}' )) 366eq ( 367{ [ '1' ] =2 , [ '3' ] ={ { [ '4' ] ={ [ '5' ] ={ {} , 1 } } } } } , 368fn . json_decode ( '{"1": 2, "3": [{"4": {"5": [[], 1]}}]}' ) 369) 370end ) 371 372it ( 'fails to parse incomplete strings' , function () 373matches ( 374'Vim%(call%):E474: Expected string end:' , 375pcall_err ( command , 'call json_decode("\\t\\"")' ) 376) 377matches ( 378'Vim%(call%):E474: Expected string end:' , 379pcall_err ( command , 'call json_decode("\\t\\"abc")' ) 380) 381matches ( 382'Vim%(call%):E474: Unfinished escape sequence:' , 383pcall_err ( command , 'call json_decode("\\t\\"abc\\\\")' ) 384) 385matches ( 386'Vim%(call%):E474: Unfinished unicode escape sequence:' , 387pcall_err ( command , 'call json_decode("\\t\\"abc\\\\u")' ) 388) 389matches ( 390'Vim%(call%):E474: Unfinished unicode escape sequence:' , 391pcall_err ( command , 'call json_decode("\\t\\"abc\\\\u0")' ) 392) 393matches ( 394'Vim%(call%):E474: Unfinished unicode escape sequence:' , 395pcall_err ( command , 'call json_decode("\\t\\"abc\\\\u00")' ) 396) 397matches ( 398'Vim%(call%):E474: Unfinished unicode escape sequence:' , 399pcall_err ( command , 'call json_decode("\\t\\"abc\\\\u000")' ) 400) 401eq ( 402'Vim(call):E474: Expected four hex digits after \\u: \\u" ' , 403pcall_err ( command , 'call json_decode("\\t\\"abc\\\\u\\" ")' ) 404) 405eq ( 406'Vim(call):E474: Expected four hex digits after \\u: \\u0" ' , 407pcall_err ( command , 'call json_decode("\\t\\"abc\\\\u0\\" ")' ) 408) 409eq ( 410'Vim(call):E474: Expected four hex digits after \\u: \\u00" ' , 411pcall_err ( command , 'call json_decode("\\t\\"abc\\\\u00\\" ")' ) 412) 413eq ( 414'Vim(call):E474: Expected four hex digits after \\u: \\u000" ' , 415pcall_err ( command , 'call json_decode("\\t\\"abc\\\\u000\\" ")' ) 416) 417matches ( 418'Vim%(call%):E474: Expected string end:' , 419pcall_err ( command , 'call json_decode("\\t\\"abc\\\\u0000")' ) 420) 421end ) 422 423it ( 'fails to parse unknown escape sequences' , function () 424eq ( 425'Vim(call):E474: Unknown escape sequence: \\a"' , 426pcall_err ( command , 'call json_decode("\\t\\"\\\\a\\"")' ) 427) 428end ) 429 430it ( 'parses strings properly' , function () 431eq ( '\n' , fn . json_decode ( '"\\n"' )) 432eq ( '' , fn . json_decode ( '""' )) 433eq ( '\\/"\t\b\n\r\f' , fn . json_decode ( [["\\\/\"\t\b\n\r\f"]] )) 434eq ( '/a' , fn . json_decode ( [["\/a"]] )) 435-- Unicode characters: 2-byte, 3-byte, 4-byte 436eq ( 437{ 438'«' , 439'ફ' , 440'\240\144\128\128' , 441} , 442fn . json_decode ( { 443'[' , 444'"«",' , 445'"ફ",' , 446'"\240\144\128\128"' , 447']' , 448} ) 449) 450end ) 451 452it ( 'fails on strings with invalid bytes' , function () 453eq ( 454'Vim(call):E474: Only UTF-8 strings allowed: \255"' , 455pcall_err ( command , 'call json_decode("\\t\\"\\xFF\\"")' ) 456) 457eq ( 458'Vim(call):E474: ASCII control characters cannot be present inside string: ' , 459pcall_err ( command , 'call json_decode(["\\"\\n\\""])' ) 460) 461-- 0xC2 starts 2-byte unicode character 462eq ( 463'Vim(call):E474: Only UTF-8 strings allowed: \194"' , 464pcall_err ( command , 'call json_decode("\\t\\"\\xC2\\"")' ) 465) 466-- 0xE0 0xAA starts 3-byte unicode character 467eq ( 468'Vim(call):E474: Only UTF-8 strings allowed: \224"' , 469pcall_err ( command , 'call json_decode("\\t\\"\\xE0\\"")' ) 470) 471eq ( 472'Vim(call):E474: Only UTF-8 strings allowed: \224\170"' , 473pcall_err ( command , 'call json_decode("\\t\\"\\xE0\\xAA\\"")' ) 474) 475-- 0xF0 0x90 0x80 starts 4-byte unicode character 476eq ( 477'Vim(call):E474: Only UTF-8 strings allowed: \240"' , 478pcall_err ( command , 'call json_decode("\\t\\"\\xF0\\"")' ) 479) 480eq ( 481'Vim(call):E474: Only UTF-8 strings allowed: \240\144"' , 482pcall_err ( command , 'call json_decode("\\t\\"\\xF0\\x90\\"")' ) 483) 484eq ( 485'Vim(call):E474: Only UTF-8 strings allowed: \240\144\128"' , 486pcall_err ( command , 'call json_decode("\\t\\"\\xF0\\x90\\x80\\"")' ) 487) 488-- 0xF9 0x80 0x80 0x80 starts 5-byte unicode character 489eq ( 490'Vim(call):E474: Only UTF-8 strings allowed: \249"' , 491pcall_err ( command , 'call json_decode("\\t\\"\\xF9\\"")' ) 492) 493eq ( 494'Vim(call):E474: Only UTF-8 strings allowed: \249\128"' , 495pcall_err ( command , 'call json_decode("\\t\\"\\xF9\\x80\\"")' ) 496) 497eq ( 498'Vim(call):E474: Only UTF-8 strings allowed: \249\128\128"' , 499pcall_err ( command , 'call json_decode("\\t\\"\\xF9\\x80\\x80\\"")' ) 500) 501eq ( 502'Vim(call):E474: Only UTF-8 strings allowed: \249\128\128\128"' , 503pcall_err ( command , 'call json_decode("\\t\\"\\xF9\\x80\\x80\\x80\\"")' ) 504) 505-- 0xFC 0x90 0x80 0x80 0x80 starts 6-byte unicode character 506eq ( 507'Vim(call):E474: Only UTF-8 strings allowed: \252"' , 508pcall_err ( command , 'call json_decode("\\t\\"\\xFC\\"")' ) 509) 510eq ( 511'Vim(call):E474: Only UTF-8 strings allowed: \252\144"' , 512pcall_err ( command , 'call json_decode("\\t\\"\\xFC\\x90\\"")' ) 513) 514eq ( 515'Vim(call):E474: Only UTF-8 strings allowed: \252\144\128"' , 516pcall_err ( command , 'call json_decode("\\t\\"\\xFC\\x90\\x80\\"")' ) 517) 518eq ( 519'Vim(call):E474: Only UTF-8 strings allowed: \252\144\128\128"' , 520pcall_err ( command , 'call json_decode("\\t\\"\\xFC\\x90\\x80\\x80\\"")' ) 521) 522eq ( 523'Vim(call):E474: Only UTF-8 strings allowed: \252\144\128\128\128"' , 524pcall_err ( command , 'call json_decode("\\t\\"\\xFC\\x90\\x80\\x80\\x80\\"")' ) 525) 526-- Specification does not allow unquoted characters above 0x10FFFF 527eq ( 528'Vim(call):E474: Only UTF-8 code points up to U+10FFFF are allowed to appear unescaped: \249\128\128\128\128"' , 529pcall_err ( command , 'call json_decode("\\t\\"\\xF9\\x80\\x80\\x80\\x80\\"")' ) 530) 531eq ( 532'Vim(call):E474: Only UTF-8 code points up to U+10FFFF are allowed to appear unescaped: \252\144\128\128\128\128"' , 533pcall_err ( command , 'call json_decode("\\t\\"\\xFC\\x90\\x80\\x80\\x80\\x80\\"")' ) 534) 535-- '"\249\128\128\128\128"', 536-- '"\252\144\128\128\128\128"', 537end ) 538 539it ( 'parses surrogate pairs properly' , function () 540eq ( '\240\144\128\128' , fn . json_decode ( '"\\uD800\\uDC00"' )) 541eq ( '\237\160\128a\237\176\128' , fn . json_decode ( '"\\uD800a\\uDC00"' )) 542eq ( '\237\160\128\t\237\176\128' , fn . json_decode ( '"\\uD800\\t\\uDC00"' )) 543 544eq ( '\237\160\128' , fn . json_decode ( '"\\uD800"' )) 545eq ( '\237\160\128a' , fn . json_decode ( '"\\uD800a"' )) 546eq ( '\237\160\128\t' , fn . json_decode ( '"\\uD800\\t"' )) 547 548eq ( '\237\176\128' , fn . json_decode ( '"\\uDC00"' )) 549eq ( '\237\176\128a' , fn . json_decode ( '"\\uDC00a"' )) 550eq ( '\237\176\128\t' , fn . json_decode ( '"\\uDC00\\t"' )) 551 552eq ( '\237\176\128' , fn . json_decode ( '"\\uDC00"' )) 553eq ( 'a\237\176\128' , fn . json_decode ( '"a\\uDC00"' )) 554eq ( '\t\237\176\128' , fn . json_decode ( '"\\t\\uDC00"' )) 555 556eq ( '\237\160\128¬' , fn . json_decode ( '"\\uD800\\u00AC"' )) 557 558eq ( '\237\160\128\237\160\128' , fn . json_decode ( '"\\uD800\\uD800"' )) 559end ) 560 561local sp_decode_eq =function ( expected , json ) 562api . nvim_set_var ( '__json' , json ) 563speq ( expected , 'json_decode(g:__json)' ) 564command ( 'unlet! g:__json' ) 565end 566 567it ( 'parses strings with NUL properly' , function () 568sp_decode_eq ( '\000' , '"\\u0000"' ) 569sp_decode_eq ( '\000\n\000' , '"\\u0000\\n\\u0000"' ) 570sp_decode_eq ( '\000«\000' , '"\\u0000\\u00AB\\u0000"' ) 571end ) 572 573it ( 'parses dictionaries with duplicate keys to special maps' , function () 574sp_decode_eq ( { _TYPE ='map' , _VAL ={ { 'a' , 1 } , { 'a' , 2 } } } , '{"a": 1, "a": 2}' ) 575sp_decode_eq ( 576{ _TYPE ='map' , _VAL ={ { 'b' , 3 } , { 'a' , 1 } , { 'a' , 2 } } } , 577'{"b": 3, "a": 1, "a": 2}' 578) 579sp_decode_eq ( 580{ _TYPE ='map' , _VAL ={ { 'b' , 3 } , { 'a' , 1 } , { 'c' , 4 } , { 'a' , 2 } } } , 581'{"b": 3, "a": 1, "c": 4, "a": 2}' 582) 583sp_decode_eq ( 584{ _TYPE ='map' , _VAL ={ { 'b' , 3 } , { 'a' , 1 } , { 'c' , 4 } , { 'a' , 2 } , { 'c' , 4 } } } , 585'{"b": 3, "a": 1, "c": 4, "a": 2, "c": 4}' 586) 587sp_decode_eq ( 588{ { _TYPE ='map' , _VAL ={ { 'b' , 3 } , { 'a' , 1 } , { 'c' , 4 } , { 'a' , 2 } , { 'c' , 4 } } } } , 589'[{"b": 3, "a": 1, "c": 4, "a": 2, "c": 4}]' 590) 591sp_decode_eq ( { 592{ 593d ={ 594_TYPE ='map' , 595_VAL ={ { 'b' , 3 } , { 'a' , 1 } , { 'c' , 4 } , { 'a' , 2 } , { 'c' , 4 } } , 596} , 597} , 598} , '[{"d": {"b": 3, "a": 1, "c": 4, "a": 2, "c": 4}}]' ) 599sp_decode_eq ( { 6001 , 601{ 602d ={ 603_TYPE ='map' , 604_VAL ={ { 'b' , 3 } , { 'a' , 1 } , { 'c' , 4 } , { 'a' , 2 } , { 'c' , 4 } } , 605} , 606} , 607} , '[1, {"d": {"b": 3, "a": 1, "c": 4, "a": 2, "c": 4}}]' ) 608sp_decode_eq ( { 6091 , 610{ 611a ={} , 612d ={ 613_TYPE ='map' , 614_VAL ={ 615{ 'b' , 3 } , 616{ 'a' , 1 } , 617{ 'c' , 4 } , 618{ 'a' , 2 } , 619{ 620'c' , 6214 , 622} , 623} , 624} , 625} , 626} , '[1, {"a": [], "d": {"b": 3, "a": 1, "c": 4, "a": 2, "c": 4}}]' ) 627sp_decode_eq ( 628{ _TYPE ='map' , _VAL ={ { '' , 3 } , { 'a' , 1 } , { 'c' , 4 } , { 'd' , 2 } , { '' , 4 } } } , 629'{"": 3, "a": 1, "c": 4, "d": 2, "": 4}' 630) 631sp_decode_eq ( 632{ { _TYPE ='map' , _VAL ={ { '' , 3 } , { 'a' , 1 } , { 'c' , 4 } , { 'd' , 2 } , { '' , 4 } } } } , 633'[{"": 3, "a": 1, "c": 4, "d": 2, "": 4}]' 634) 635end ) 636 637it ( 'parses dictionaries with empty keys' , function () 638eq ( { [ '' ] =4 } , fn . json_decode ( '{"": 4}' )) 639eq ( 640{ b =3 , a =1 , c =4 , d =2 , [ '' ] =4 } , 641fn . json_decode ( '{"b": 3, "a": 1, "c": 4, "d": 2, "": 4}' ) 642) 643end ) 644 645it ( 'parses dictionaries with keys with NUL bytes to special maps' , function () 646sp_decode_eq ( { _TYPE ='map' , _VAL ={ { 'a\000\nb' , 4 } } } , '{"a\\u0000\\nb": 4}' ) 647sp_decode_eq ( { _TYPE ='map' , _VAL ={ { 'a\000\nb\n' , 4 } } } , '{"a\\u0000\\nb\\n": 4}' ) 648sp_decode_eq ( { 649_TYPE ='map' , 650_VAL ={ 651{ 'b' , 3 } , 652{ 'a' , 1 } , 653{ 'c' , 4 } , 654{ 'd' , 2 } , 655{ '\000' , 4 } , 656} , 657} , '{"b": 3, "a": 1, "c": 4, "d": 2, "\\u0000": 4}' ) 658end ) 659 660it ( 'parses U+00C3 correctly' , function () 661eq ( '\195\131' , fn . json_decode ( '"\195\131"' )) 662end ) 663 664it ( 'fails to parse empty string' , function () 665eq ( 666'Vim(call):E474: Attempt to decode a blank string' , 667pcall_err ( command , 'call json_decode("")' ) 668) 669eq ( 670'Vim(call):E474: Attempt to decode a blank string' , 671pcall_err ( command , 'call json_decode([])' ) 672) 673eq ( 674'Vim(call):E474: Attempt to decode a blank string' , 675pcall_err ( command , 'call json_decode([""])' ) 676) 677eq ( 678'Vim(call):E474: Attempt to decode a blank string' , 679pcall_err ( command , 'call json_decode(" ")' ) 680) 681eq ( 682'Vim(call):E474: Attempt to decode a blank string' , 683pcall_err ( command , 'call json_decode("\\t")' ) 684) 685eq ( 686'Vim(call):E474: Attempt to decode a blank string' , 687pcall_err ( command , 'call json_decode("\\n")' ) 688) 689eq ( 690'Vim(call):E474: Attempt to decode a blank string' , 691pcall_err ( command , 'call json_decode(" \\t\\n \\n\\t\\t \\n\\t\\n \\n \\t\\n\\t ")' ) 692) 693end ) 694 695it ( 'accepts all spaces in every position where space may be put' , function () 696local s = 697' \t\n\r \t\r\n \n\t\r \n\r\t \r\t\n \r\n\t\t \n\r\t \r\n\t\n \r\t\n\r \t\r \n\t\r\n \n \t\r\n \r\t\n\t \r\n\t\r \n\r \t\n\r\t \r \t\n\r \n\t\r\t \n\r\t\n \r\n \t\r\n\t' 698local str =( '%s{%s"key"%s:%s[%s"val"%s,%s"val2"%s]%s,%s"key2"%s:%s1%s}%s' ): gsub ( '%%s' , s ) 699eq ( { key ={ 'val' , 'val2' } , key2 =1 } , fn . json_decode ( str )) 700end ) 701 702it ( 'does not overflow when writing error message about decoding ["", ""]' , function () 703eq ( 704'Vim(call):E474: Attempt to decode a blank string' , 705pcall_err ( command , 'call json_decode(["", ""])' ) 706) 707end ) 708end ) 709 710describe ( 'json_encode() function' , function () 711setup ( function () 712clear () 713command ( 'language C' ) 714end ) 715 716it ( 'dumps strings' , function () 717eq ( '"Test"' , fn . json_encode ( 'Test' )) 718eq ( '""' , fn . json_encode ( '' )) 719eq ( '"\\t"' , fn . json_encode ( '\t' )) 720eq ( '"\\n"' , fn . json_encode ( '\n' )) 721eq ( '"\\u001B"' , fn . json_encode ( '\27' )) 722eq ( '"þÿþ"' , fn . json_encode ( 'þÿþ' )) 723end ) 724 725it ( 'dumps blobs' , function () 726eq ( '[]' , eval ( 'json_encode(0z)' )) 727eq ( '[222, 173, 190, 239]' , eval ( 'json_encode(0zDEADBEEF)' )) 728end ) 729 730it ( 'dumps numbers' , function () 731eq ( '0' , fn . json_encode ( 0 )) 732eq ( '10' , fn . json_encode ( 10 )) 733eq ( '-10' , fn . json_encode ( - 10 )) 734end ) 735 736it ( 'dumps floats' , function () 737-- Also test method call (->) syntax 738eq ( '0.0' , eval ( '0.0->json_encode()' )) 739eq ( '10.5' , fn . json_encode ( 10.5 )) 740eq ( '-10.5' , fn . json_encode ( - 10.5 )) 741eq ( '-1.0e-5' , fn . json_encode ( - 1e-5 )) 742eq ( '1.0e50' , eval ( '1.0e50->json_encode()' )) 743end ) 744 745it ( 'fails to dump NaN and infinite values' , function () 746eq ( 747'Vim(call):E474: Unable to represent NaN value in JSON' , 748pcall_err ( command , 'call json_encode(str2float("nan"))' ) 749) 750eq ( 751'Vim(call):E474: Unable to represent infinity in JSON' , 752pcall_err ( command , 'call json_encode(str2float("inf"))' ) 753) 754eq ( 755'Vim(call):E474: Unable to represent infinity in JSON' , 756pcall_err ( command , 'call json_encode(-str2float("inf"))' ) 757) 758end ) 759 760it ( 'dumps lists' , function () 761eq ( '[]' , fn . json_encode ( {} )) 762eq ( '[[]]' , fn . json_encode ( { {} } )) 763eq ( '[[], []]' , fn . json_encode ( { {} , {} } )) 764end ) 765 766it ( 'dumps dictionaries' , function () 767eq ( '{}' , eval ( 'json_encode({})' )) 768eq ( '{"d": []}' , fn . json_encode ( { d ={} } )) 769eq ( '{"d": [], "e": []}' , fn . json_encode ( { d ={} , e ={} } )) 770-- Empty keys are allowed per JSON spec (and Vim dicts, and msgpack). 771eq ( '{"": []}' , fn . json_encode ( { [ '' ] ={} } )) 772end ) 773 774it ( 'cannot dump generic mapping with generic mapping keys and values' , function () 775command ( 'let todump = {"_TYPE": v:msgpack_types.map, "_VAL": []}' ) 776command ( 'let todumpv1 = {"_TYPE": v:msgpack_types.map, "_VAL": []}' ) 777command ( 'let todumpv2 = {"_TYPE": v:msgpack_types.map, "_VAL": []}' ) 778command ( 'call add(todump._VAL, [todumpv1, todumpv2])' ) 779eq ( 780'Vim(call):E474: Invalid key in special dictionary' , 781pcall_err ( command , 'call json_encode(todump)' ) 782) 783end ) 784 785it ( 'cannot dump generic mapping with ext key' , function () 786command ( 'let todump = {"_TYPE": v:msgpack_types.ext, "_VAL": [5, ["",""]]}' ) 787command ( 'let todump = {"_TYPE": v:msgpack_types.map, "_VAL": [[todump, 1]]}' ) 788eq ( 789'Vim(call):E474: Invalid key in special dictionary' , 790pcall_err ( command , 'call json_encode(todump)' ) 791) 792end ) 793 794it ( 'cannot dump generic mapping with array key' , function () 795command ( 'let todump = {"_TYPE": v:msgpack_types.array, "_VAL": [5, [""]]}' ) 796command ( 'let todump = {"_TYPE": v:msgpack_types.map, "_VAL": [[todump, 1]]}' ) 797eq ( 798'Vim(call):E474: Invalid key in special dictionary' , 799pcall_err ( command , 'call json_encode(todump)' ) 800) 801end ) 802 803it ( 'cannot dump generic mapping with UINT64_MAX key' , function () 804command ( 'let todump = {"_TYPE": v:msgpack_types.integer}' ) 805command ( 'let todump._VAL = [1, 3, 0x7FFFFFFF, 0x7FFFFFFF]' ) 806command ( 'let todump = {"_TYPE": v:msgpack_types.map, "_VAL": [[todump, 1]]}' ) 807eq ( 808'Vim(call):E474: Invalid key in special dictionary' , 809pcall_err ( command , 'call json_encode(todump)' ) 810) 811end ) 812 813it ( 'cannot dump generic mapping with floating-point key' , function () 814command ( 'let todump = {"_TYPE": v:msgpack_types.float, "_VAL": 0.125}' ) 815command ( 'let todump = {"_TYPE": v:msgpack_types.map, "_VAL": [[todump, 1]]}' ) 816eq ( 817'Vim(call):E474: Invalid key in special dictionary' , 818pcall_err ( command , 'call json_encode(todump)' ) 819) 820end ) 821 822it ( 'can dump generic mapping with STR special key and NUL' , function () 823command ( 'let todump = {"_TYPE": v:msgpack_types.string, "_VAL": ["\\n"]}' ) 824command ( 'let todump = {"_TYPE": v:msgpack_types.map, "_VAL": [[todump, 1]]}' ) 825eq ( '{"\\u0000": 1}' , eval ( 'json_encode(todump)' )) 826end ) 827 828it ( 'can dump STR special mapping with NUL and NL' , function () 829command ( 'let todump = {"_TYPE": v:msgpack_types.string, "_VAL": ["\\n", ""]}' ) 830eq ( '"\\u0000\\n"' , eval ( 'json_encode(todump)' )) 831end ) 832 833it ( 'cannot dump special ext mapping' , function () 834command ( 'let todump = {"_TYPE": v:msgpack_types.ext, "_VAL": [5, ["",""]]}' ) 835eq ( 836'Vim(call):E474: Unable to convert EXT string to JSON' , 837pcall_err ( command , 'call json_encode(todump)' ) 838) 839end ) 840 841it ( 'can dump special array mapping' , function () 842command ( 'let todump = {"_TYPE": v:msgpack_types.array, "_VAL": [5, [""]]}' ) 843eq ( '[5, [""]]' , eval ( 'json_encode(todump)' )) 844end ) 845 846it ( 'can dump special UINT64_MAX mapping' , function () 847command ( 'let todump = {"_TYPE": v:msgpack_types.integer}' ) 848command ( 'let todump._VAL = [1, 3, 0x7FFFFFFF, 0x7FFFFFFF]' ) 849eq ( '18446744073709551615' , eval ( 'json_encode(todump)' )) 850end ) 851 852it ( 'can dump special INT64_MIN mapping' , function () 853command ( 'let todump = {"_TYPE": v:msgpack_types.integer}' ) 854command ( 'let todump._VAL = [-1, 2, 0, 0]' ) 855eq ( '-9223372036854775808' , eval ( 'json_encode(todump)' )) 856end ) 857 858it ( 'can dump special BOOLEAN true mapping' , function () 859command ( 'let todump = {"_TYPE": v:msgpack_types.boolean, "_VAL": 1}' ) 860eq ( 'true' , eval ( 'json_encode(todump)' )) 861end ) 862 863it ( 'can dump special BOOLEAN false mapping' , function () 864command ( 'let todump = {"_TYPE": v:msgpack_types.boolean, "_VAL": 0}' ) 865eq ( 'false' , eval ( 'json_encode(todump)' )) 866end ) 867 868it ( 'can dump special NIL mapping' , function () 869command ( 'let todump = {"_TYPE": v:msgpack_types.nil, "_VAL": 0}' ) 870eq ( 'null' , eval ( 'json_encode(todump)' )) 871end ) 872 873it ( 'fails to dump a function reference' , function () 874eq ( 875'Vim(call):E474: Error while dumping encode_tv2json() argument, itself: attempt to dump function reference' , 876pcall_err ( command , 'call json_encode(function("tr"))' ) 877) 878end ) 879 880it ( 'fails to dump a partial' , function () 881command ( 'function T() dict\nendfunction' ) 882eq ( 883'Vim(call):E474: Error while dumping encode_tv2json() argument, itself: attempt to dump function reference' , 884pcall_err ( command , 'call json_encode(function("T", [1, 2], {}))' ) 885) 886end ) 887 888it ( 'fails to dump a function reference in a list' , function () 889eq ( 890'Vim(call):E474: Error while dumping encode_tv2json() argument, index 0: attempt to dump function reference' , 891pcall_err ( command , 'call json_encode([function("tr")])' ) 892) 893end ) 894 895it ( 'fails to dump a recursive list' , function () 896command ( 'let todump = [[[]]]' ) 897command ( 'call add(todump[0][0], todump)' ) 898eq ( 899'Vim(call):E724: unable to correctly dump variable with self-referencing container' , 900pcall_err ( command , 'call json_encode(todump)' ) 901) 902end ) 903 904it ( 'fails to dump a recursive dict' , function () 905command ( 'let todump = {"d": {"d": {}}}' ) 906command ( 'call extend(todump.d.d, {"d": todump})' ) 907eq ( 908'Vim(call):E724: unable to correctly dump variable with self-referencing container' , 909pcall_err ( command , 'call json_encode([todump])' ) 910) 911end ) 912 913it ( 'can dump dict with two same dicts inside' , function () 914command ( 'let inter = {}' ) 915command ( 'let todump = {"a": inter, "b": inter}' ) 916eq ( '{"a": {}, "b": {}}' , eval ( 'json_encode(todump)' )) 917end ) 918 919it ( 'can dump list with two same lists inside' , function () 920command ( 'let inter = []' ) 921command ( 'let todump = [inter, inter]' ) 922eq ( '[[], []]' , eval ( 'json_encode(todump)' )) 923end ) 924 925it ( 'fails to dump a recursive list in a special dict' , function () 926command ( 'let todump = {"_TYPE": v:msgpack_types.array, "_VAL": []}' ) 927command ( 'call add(todump._VAL, todump)' ) 928eq ( 929'Vim(call):E724: unable to correctly dump variable with self-referencing container' , 930pcall_err ( command , 'call json_encode(todump)' ) 931) 932end ) 933 934it ( 'fails to dump a recursive (val) map in a special dict' , function () 935command ( 'let todump = {"_TYPE": v:msgpack_types.map, "_VAL": []}' ) 936command ( 'call add(todump._VAL, ["", todump])' ) 937eq ( 938'Vim(call):E724: unable to correctly dump variable with self-referencing container' , 939pcall_err ( command , 'call json_encode([todump])' ) 940) 941end ) 942 943it ( 'fails to dump a recursive (val) map in a special dict, _VAL reference' , function () 944command ( 'let todump = {"_TYPE": v:msgpack_types.map, "_VAL": [["", []]]}' ) 945command ( 'call add(todump._VAL[0][1], todump._VAL)' ) 946eq ( 947'Vim(call):E724: unable to correctly dump variable with self-referencing container' , 948pcall_err ( command , 'call json_encode(todump)' ) 949) 950end ) 951 952it ( 'fails to dump a recursive (val) special list in a special dict' , function () 953command ( 'let todump = {"_TYPE": v:msgpack_types.array, "_VAL": []}' ) 954command ( 'call add(todump._VAL, ["", todump._VAL])' ) 955eq ( 956'Vim(call):E724: unable to correctly dump variable with self-referencing container' , 957pcall_err ( command , 'call json_encode(todump)' ) 958) 959end ) 960 961it ( 'fails when called with no arguments' , function () 962eq ( 963'Vim(call):E119: Not enough arguments for function: json_encode' , 964pcall_err ( command , 'call json_encode()' ) 965) 966end ) 967 968it ( 'fails when called with two arguments' , function () 969eq ( 970'Vim(call):E118: Too many arguments for function: json_encode' , 971pcall_err ( command , 'call json_encode(["", ""], 1)' ) 972) 973end ) 974 975it ( 'fails when using surrogate character in a UTF-8 string' , function () 976eq ( 977'Vim(call):E474: UTF-8 string contains code point which belongs to a surrogate pair: \237\160\128' , 978pcall_err ( command , 'call json_encode("\237\160\128")' ) 979) 980eq ( 981'Vim(call):E474: UTF-8 string contains code point which belongs to a surrogate pair: \237\175\191' , 982pcall_err ( command , 'call json_encode("\237\175\191")' ) 983) 984end ) 985 986it ( 'dumps control characters as expected' , function () 987eq ( 988[["\u0000\u0001\u0002\u0003\u0004\u0005\u0006\u0007\b\t\n\u000B\f\r\u000E\u000F\u0010\u0011\u0012\u0013"]] , 989eval ( 990'json_encode({"_TYPE": v:msgpack_types.string, "_VAL": ["\n\1\2\3\4\5\6\7\8\9", "\11\12\13\14\15\16\17\18\19"]})' 991) 992) 993end ) 994 995it ( 'can dump NULL string' , function () 996eq ( '""' , eval ( 'json_encode($XXX_UNEXISTENT_VAR_XXX)' )) 997end ) 998 999it ( 'can dump NULL blob' , function () 1000eq ( '[]' , eval ( 'json_encode(v:_null_blob)' )) 1001end ) 1002 1003it ( 'can dump NULL list' , function () 1004eq ( '[]' , eval ( 'json_encode(v:_null_list)' )) 1005end ) 1006 1007it ( 'can dump NULL dict' , function () 1008eq ( '{}' , eval ( 'json_encode(v:_null_dict)' )) 1009end ) 1010 1011it ( 'fails to parse NULL strings and lists' , function () 1012eq ( 1013'Vim(call):E474: Attempt to decode a blank string' , 1014pcall_err ( command , 'call json_decode($XXX_UNEXISTENT_VAR_XXX)' ) 1015) 1016eq ( 1017'Vim(call):E474: Attempt to decode a blank string' , 1018pcall_err ( command , 'call json_decode(v:_null_list)' ) 1019) 1020end ) 1021end )