json_spec.lua 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. local t = require('test.testutil')
  2. local n = require('test.functional.testnvim')()
  3. local clear = n.clear
  4. local exec_lua = n.exec_lua
  5. local eq = t.eq
  6. local pcall_err = t.pcall_err
  7. describe('vim.json.decode()', function()
  8. before_each(function()
  9. clear()
  10. end)
  11. it('parses null, true, false', function()
  12. eq(vim.NIL, exec_lua([[return vim.json.decode('null')]]))
  13. eq(true, exec_lua([[return vim.json.decode('true')]]))
  14. eq(false, exec_lua([[return vim.json.decode('false')]]))
  15. end)
  16. it('validation', function()
  17. eq(
  18. 'Expected object key string but found invalid token at character 2',
  19. pcall_err(exec_lua, [[return vim.json.decode('{a:"b"}')]])
  20. )
  21. end)
  22. it('options', function()
  23. local jsonstr = '{"arr":[1,2,null],"bar":[3,7],"foo":{"a":"b"},"baz":null}'
  24. eq({
  25. arr = { 1, 2, vim.NIL },
  26. bar = { 3, 7 },
  27. baz = vim.NIL,
  28. foo = { a = 'b' },
  29. }, exec_lua([[return vim.json.decode(..., {})]], jsonstr))
  30. eq(
  31. {
  32. arr = { 1, 2, vim.NIL },
  33. bar = { 3, 7 },
  34. baz = vim.NIL,
  35. foo = { a = 'b' },
  36. },
  37. exec_lua(
  38. [[return vim.json.decode(..., { luanil = { array = false, object = false } })]],
  39. jsonstr
  40. )
  41. )
  42. eq({
  43. arr = { 1, 2, vim.NIL },
  44. bar = { 3, 7 },
  45. -- baz = nil,
  46. foo = { a = 'b' },
  47. }, exec_lua([[return vim.json.decode(..., { luanil = { object = true } })]], jsonstr))
  48. eq({
  49. arr = { 1, 2 },
  50. bar = { 3, 7 },
  51. baz = vim.NIL,
  52. foo = { a = 'b' },
  53. }, exec_lua([[return vim.json.decode(..., { luanil = { array = true } })]], jsonstr))
  54. eq(
  55. {
  56. arr = { 1, 2 },
  57. bar = { 3, 7 },
  58. -- baz = nil,
  59. foo = { a = 'b' },
  60. },
  61. exec_lua(
  62. [[return vim.json.decode(..., { luanil = { array = true, object = true } })]],
  63. jsonstr
  64. )
  65. )
  66. end)
  67. it('parses integer numbers', function()
  68. eq(100000, exec_lua([[return vim.json.decode('100000')]]))
  69. eq(-100000, exec_lua([[return vim.json.decode('-100000')]]))
  70. eq(100000, exec_lua([[return vim.json.decode(' 100000 ')]]))
  71. eq(-100000, exec_lua([[return vim.json.decode(' -100000 ')]]))
  72. eq(0, exec_lua([[return vim.json.decode('0')]]))
  73. eq(0, exec_lua([[return vim.json.decode('-0')]]))
  74. end)
  75. it('parses floating-point numbers', function()
  76. -- This behavior differs from vim.fn.json_decode, which return '100000.0'
  77. eq('100000', exec_lua([[return tostring(vim.json.decode('100000.0'))]]))
  78. eq(100000.5, exec_lua([[return vim.json.decode('100000.5')]]))
  79. eq(-100000.5, exec_lua([[return vim.json.decode('-100000.5')]]))
  80. eq(-100000.5e50, exec_lua([[return vim.json.decode('-100000.5e50')]]))
  81. eq(100000.5e50, exec_lua([[return vim.json.decode('100000.5e50')]]))
  82. eq(100000.5e50, exec_lua([[return vim.json.decode('100000.5e+50')]]))
  83. eq(-100000.5e-50, exec_lua([[return vim.json.decode('-100000.5e-50')]]))
  84. eq(100000.5e-50, exec_lua([[return vim.json.decode('100000.5e-50')]]))
  85. eq(100000e-50, exec_lua([[return vim.json.decode('100000e-50')]]))
  86. eq(0.5, exec_lua([[return vim.json.decode('0.5')]]))
  87. eq(0.005, exec_lua([[return vim.json.decode('0.005')]]))
  88. eq(0.005, exec_lua([[return vim.json.decode('0.00500')]]))
  89. eq(0.5, exec_lua([[return vim.json.decode('0.00500e+002')]]))
  90. eq(0.00005, exec_lua([[return vim.json.decode('0.00500e-002')]]))
  91. eq(-0.0, exec_lua([[return vim.json.decode('-0.0')]]))
  92. eq(-0.0, exec_lua([[return vim.json.decode('-0.0e0')]]))
  93. eq(-0.0, exec_lua([[return vim.json.decode('-0.0e+0')]]))
  94. eq(-0.0, exec_lua([[return vim.json.decode('-0.0e-0')]]))
  95. eq(-0.0, exec_lua([[return vim.json.decode('-0e-0')]]))
  96. eq(-0.0, exec_lua([[return vim.json.decode('-0e-2')]]))
  97. eq(-0.0, exec_lua([[return vim.json.decode('-0e+2')]]))
  98. eq(0.0, exec_lua([[return vim.json.decode('0.0')]]))
  99. eq(0.0, exec_lua([[return vim.json.decode('0.0e0')]]))
  100. eq(0.0, exec_lua([[return vim.json.decode('0.0e+0')]]))
  101. eq(0.0, exec_lua([[return vim.json.decode('0.0e-0')]]))
  102. eq(0.0, exec_lua([[return vim.json.decode('0e-0')]]))
  103. eq(0.0, exec_lua([[return vim.json.decode('0e-2')]]))
  104. eq(0.0, exec_lua([[return vim.json.decode('0e+2')]]))
  105. end)
  106. it('parses containers', function()
  107. eq({ 1 }, exec_lua([[return vim.json.decode('[1]')]]))
  108. eq({ vim.NIL, 1 }, exec_lua([[return vim.json.decode('[null, 1]')]]))
  109. eq({ ['1'] = 2 }, exec_lua([[return vim.json.decode('{"1": 2}')]]))
  110. eq(
  111. { ['1'] = 2, ['3'] = { { ['4'] = { ['5'] = { {}, 1 } } } } },
  112. exec_lua([[return vim.json.decode('{"1": 2, "3": [{"4": {"5": [ [], 1]}}]}')]])
  113. )
  114. -- Empty string is a valid key. #20757
  115. eq({ [''] = 42 }, exec_lua([[return vim.json.decode('{"": 42}')]]))
  116. end)
  117. it('parses strings properly', function()
  118. eq('\n', exec_lua([=[return vim.json.decode([["\n"]])]=]))
  119. eq('', exec_lua([=[return vim.json.decode([[""]])]=]))
  120. eq('\\/"\t\b\n\r\f', exec_lua([=[return vim.json.decode([["\\\/\"\t\b\n\r\f"]])]=]))
  121. eq('/a', exec_lua([=[return vim.json.decode([["\/a"]])]=]))
  122. -- Unicode characters: 2-byte, 3-byte
  123. eq('«', exec_lua([=[return vim.json.decode([["«"]])]=]))
  124. eq('ફ', exec_lua([=[return vim.json.decode([["ફ"]])]=]))
  125. end)
  126. it('parses surrogate pairs properly', function()
  127. eq('\240\144\128\128', exec_lua([[return vim.json.decode('"\\uD800\\uDC00"')]]))
  128. end)
  129. it('accepts all spaces in every position where space may be put', function()
  130. local s =
  131. ' \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'
  132. local str = ('%s{%s"key"%s:%s[%s"val"%s,%s"val2"%s]%s,%s"key2"%s:%s1%s}%s'):gsub('%%s', s)
  133. eq({ key = { 'val', 'val2' }, key2 = 1 }, exec_lua([[return vim.json.decode(...)]], str))
  134. end)
  135. end)
  136. describe('vim.json.encode()', function()
  137. before_each(function()
  138. clear()
  139. end)
  140. it('dumps strings', function()
  141. eq('"Test"', exec_lua([[return vim.json.encode('Test')]]))
  142. eq('""', exec_lua([[return vim.json.encode('')]]))
  143. eq('"\\t"', exec_lua([[return vim.json.encode('\t')]]))
  144. eq('"\\n"', exec_lua([[return vim.json.encode('\n')]]))
  145. -- vim.fn.json_encode return \\u001B
  146. eq('"\\u001b"', exec_lua([[return vim.json.encode('\27')]]))
  147. eq('"þÿþ"', exec_lua([[return vim.json.encode('þÿþ')]]))
  148. end)
  149. it('dumps numbers', function()
  150. eq('0', exec_lua([[return vim.json.encode(0)]]))
  151. eq('10', exec_lua([[return vim.json.encode(10)]]))
  152. eq('-10', exec_lua([[return vim.json.encode(-10)]]))
  153. end)
  154. it('dumps floats', function()
  155. eq('10.5', exec_lua([[return vim.json.encode(10.5)]]))
  156. eq('-10.5', exec_lua([[return vim.json.encode(-10.5)]]))
  157. eq('-1e-05', exec_lua([[return vim.json.encode(-1e-5)]]))
  158. end)
  159. it('dumps lists', function()
  160. eq('[]', exec_lua([[return vim.json.encode({})]]))
  161. eq('[[]]', exec_lua([[return vim.json.encode({{}})]]))
  162. eq('[[],[]]', exec_lua([[return vim.json.encode({{}, {}})]]))
  163. end)
  164. it('dumps dictionaries', function()
  165. eq('{}', exec_lua([[return vim.json.encode(vim.empty_dict())]]))
  166. eq('{"d":[]}', exec_lua([[return vim.json.encode({d={}})]]))
  167. -- Empty string is a valid key. #20757
  168. eq('{"":42}', exec_lua([[return vim.json.encode({['']=42})]]))
  169. end)
  170. it('dumps vim.NIL', function()
  171. eq('null', exec_lua([[return vim.json.encode(vim.NIL)]]))
  172. end)
  173. end)