json_functions_spec.lua 33 KB

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