null_spec.lua 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. local t = require('test.testutil')
  2. local n = require('test.functional.testnvim')()
  3. local exc_exec = n.exc_exec
  4. local command = n.command
  5. local clear = n.clear
  6. local api = n.api
  7. local fn = n.fn
  8. local eq = t.eq
  9. local function redir_exec(cmd)
  10. api.nvim_set_var('__redir_exec_cmd', cmd)
  11. command([[
  12. redir => g:__redir_exec_output
  13. silent! execute g:__redir_exec_cmd
  14. redir END
  15. ]])
  16. local ret = api.nvim_get_var('__redir_exec_output')
  17. api.nvim_del_var('__redir_exec_output')
  18. api.nvim_del_var('__redir_exec_cmd')
  19. return ret
  20. end
  21. describe('NULL', function()
  22. before_each(function()
  23. clear()
  24. command('let L = v:_null_list')
  25. command('let D = v:_null_dict')
  26. command('let S = v:_null_string')
  27. command('let V = $XXX_NONEXISTENT_VAR_XXX')
  28. end)
  29. local tmpfname = 'Xtest-functional-viml-null'
  30. after_each(function()
  31. os.remove(tmpfname)
  32. end)
  33. local null_test = function(name, cmd, err)
  34. it(name, function()
  35. eq(err, exc_exec(cmd))
  36. end)
  37. end
  38. local null_expr_test = function(name, expr, err, val, after)
  39. it(name, function()
  40. eq((err == 0) and '' or ('\n' .. err), redir_exec('let g:_var = ' .. expr))
  41. if val == nil then
  42. eq(0, fn.exists('g:_var'))
  43. else
  44. eq(val, api.nvim_get_var('_var'))
  45. end
  46. if after ~= nil then
  47. after()
  48. end
  49. end)
  50. end
  51. describe('list', function()
  52. -- Subjectable behaviour
  53. null_expr_test('is equal to empty list', 'L == []', 0, 1)
  54. null_expr_test('is equal to empty list (reverse order)', '[] == L', 0, 1)
  55. -- Correct behaviour
  56. null_test(
  57. 'can be :unlet item with error message for empty list',
  58. ':unlet L[0]',
  59. 'Vim(unlet):E684: List index out of range: 0'
  60. )
  61. null_expr_test(
  62. 'can be indexed with error message for empty list',
  63. 'L[0]',
  64. 'E684: List index out of range: 0',
  65. nil
  66. )
  67. null_expr_test('can be splice-indexed', 'L[:]', 0, {})
  68. null_expr_test('is not locked', 'islocked("v:_null_list")', 0, 0)
  69. null_test('is accepted by :for', 'for x in L|throw x|endfor', 0)
  70. null_expr_test('does not crash append()', 'append(0, L)', 0, 0, function()
  71. eq({ '' }, api.nvim_buf_get_lines(0, 0, -1, false))
  72. end)
  73. null_expr_test('does not crash setline()', 'setline(1, L)', 0, 0, function()
  74. eq({ '' }, api.nvim_buf_get_lines(0, 0, -1, false))
  75. end)
  76. null_expr_test('is identical to itself', 'L is L', 0, 1)
  77. null_expr_test('can be sliced', 'L[:]', 0, {})
  78. null_expr_test('can be copied', 'copy(L)', 0, {})
  79. null_expr_test('can be deepcopied', 'deepcopy(L)', 0, {})
  80. null_expr_test('does not crash when indexed', 'L[1]', 'E684: List index out of range: 1', nil)
  81. null_expr_test('does not crash call()', 'call("arglistid", L)', 0, 0)
  82. null_expr_test('does not crash col()', 'col(L)', 0, 0)
  83. null_expr_test('does not crash virtcol()', 'virtcol(L)', 0, 0)
  84. null_expr_test('does not crash line()', 'line(L)', 0, 0)
  85. null_expr_test('does not crash line() with window id', 'line(L, 1000)', 0, 0)
  86. null_expr_test('does not crash count()', 'count(L, 1)', 0, 0)
  87. null_expr_test('does not crash cursor()', 'cursor(L)', 'E474: Invalid argument', -1)
  88. null_expr_test('does not crash map()', 'map(L, "v:val")', 0, {})
  89. null_expr_test('does not crash filter()', 'filter(L, "1")', 0, {})
  90. null_expr_test('is empty', 'empty(L)', 0, 1)
  91. null_expr_test('does not crash get()', 'get(L, 1, 10)', 0, 10)
  92. null_expr_test('has zero length', 'len(L)', 0, 0)
  93. null_expr_test('is accepted as an empty list by max()', 'max(L)', 0, 0)
  94. null_expr_test('is accepted as an empty list by min()', 'min(L)', 0, 0)
  95. null_expr_test('is stringified correctly', 'string(L)', 0, '[]')
  96. null_expr_test('is JSON encoded correctly', 'json_encode(L)', 0, '[]')
  97. null_test('does not crash lockvar', 'lockvar! L', 0)
  98. null_expr_test('can be added to itself', '(L + L)', 0, {})
  99. null_expr_test('can be added to itself', '(L + L) is L', 0, 1)
  100. null_expr_test('can be added to non-empty list', '([1] + L)', 0, { 1 })
  101. null_expr_test('can be added to non-empty list (reversed)', '(L + [1])', 0, { 1 })
  102. null_expr_test('is equal to itself', 'L == L', 0, 1)
  103. null_expr_test('is not not equal to itself', 'L != L', 0, 0)
  104. null_expr_test('counts correctly', 'count([L], L)', 0, 1)
  105. null_expr_test('makes map() return v:_null_list', 'map(L, "v:val") is# L', 0, 1)
  106. null_expr_test('makes filter() return v:_null_list', 'filter(L, "1") is# L', 0, 1)
  107. null_test(
  108. 'is treated by :let as empty list',
  109. ':let [l] = L',
  110. 'Vim(let):E688: More targets than List items'
  111. )
  112. null_expr_test(
  113. 'is accepted as an empty list by inputlist()',
  114. '[feedkeys("\\n"), inputlist(L)]',
  115. 'Type number and <Enter> or click with the mouse (q or empty cancels): ',
  116. { 0, 0 }
  117. )
  118. null_expr_test(
  119. 'is accepted as an empty list by writefile()',
  120. ('[writefile(L, "%s"), readfile("%s")]'):format(tmpfname, tmpfname),
  121. 0,
  122. { 0, {} }
  123. )
  124. null_expr_test(
  125. 'makes add() error out',
  126. 'add(L, 0)',
  127. 'E742: Cannot change value of add() argument',
  128. 1
  129. )
  130. null_expr_test(
  131. 'makes insert() error out',
  132. 'insert(L, 1)',
  133. 'E742: Cannot change value of insert() argument',
  134. 0
  135. )
  136. null_expr_test(
  137. 'does not crash remove()',
  138. 'remove(L, 0)',
  139. 'E742: Cannot change value of remove() argument',
  140. 0
  141. )
  142. null_expr_test(
  143. 'makes reverse() error out',
  144. 'reverse(L)',
  145. 'E742: Cannot change value of reverse() argument',
  146. 0
  147. )
  148. null_expr_test(
  149. 'makes sort() error out',
  150. 'sort(L)',
  151. 'E742: Cannot change value of sort() argument',
  152. 0
  153. )
  154. null_expr_test(
  155. 'makes uniq() error out',
  156. 'uniq(L)',
  157. 'E742: Cannot change value of uniq() argument',
  158. 0
  159. )
  160. null_expr_test(
  161. 'does not crash extend()',
  162. 'extend(L, [1])',
  163. 'E742: Cannot change value of extend() argument',
  164. 0
  165. )
  166. null_expr_test('does not crash extend() (second position)', 'extend([1], L)', 0, { 1 })
  167. null_expr_test('makes join() return empty string', 'join(L, "")', 0, '')
  168. null_expr_test('makes msgpackdump() return empty list', 'msgpackdump(L)', 0, {})
  169. null_expr_test('does not crash system()', 'system("cat", L)', 0, '')
  170. null_expr_test('does not crash setreg', 'setreg("x", L)', 0, 0)
  171. null_expr_test('does not crash systemlist()', 'systemlist("cat", L)', 0, {})
  172. null_test(
  173. 'does not make Neovim crash when v:oldfiles gets assigned to that',
  174. ':let v:oldfiles = L|oldfiles',
  175. 0
  176. )
  177. null_expr_test(
  178. 'does not make complete() crash or error out',
  179. 'execute(":normal i\\<C-r>=complete(1, L)[-1]\\n")',
  180. 0,
  181. '',
  182. function()
  183. eq({ '' }, api.nvim_buf_get_lines(0, 0, -1, false))
  184. end
  185. )
  186. null_expr_test('is accepted by setmatches()', 'setmatches(L)', 0, 0)
  187. null_expr_test('is accepted by setqflist()', 'setqflist(L)', 0, 0)
  188. null_expr_test('is accepted by setloclist()', 'setloclist(1, L)', 0, 0)
  189. null_test('is accepted by :cexpr', 'cexpr L', 0)
  190. null_test('is accepted by :lexpr', 'lexpr L', 0)
  191. null_expr_test('does not crash execute()', 'execute(L)', 0, '')
  192. end)
  193. describe('dict', function()
  194. it('does not crash when indexing NULL dict', function()
  195. eq('\nE716: Key not present in Dictionary: "test"', redir_exec('echo v:_null_dict.test'))
  196. end)
  197. null_expr_test(
  198. 'makes extend error out',
  199. 'extend(D, {})',
  200. 'E742: Cannot change value of extend() argument',
  201. 0
  202. )
  203. null_expr_test('makes extend do nothing', 'extend({1: 2}, D)', 0, { ['1'] = 2 })
  204. null_expr_test('does not crash map()', 'map(D, "v:val")', 0, {})
  205. null_expr_test('does not crash filter()', 'filter(D, "1")', 0, {})
  206. null_expr_test('makes map() return v:_null_dict', 'map(D, "v:val") is# D', 0, 1)
  207. null_expr_test('makes filter() return v:_null_dict', 'filter(D, "1") is# D', 0, 1)
  208. end)
  209. describe('string', function()
  210. null_test('does not crash :echomsg', 'echomsg S', 0)
  211. null_test('does not crash :execute', 'execute S', 0)
  212. null_expr_test('does not crash execute()', 'execute(S)', 0, '')
  213. null_expr_test('does not crash executable()', 'executable(S)', 0, 0)
  214. null_expr_test(
  215. 'makes timer_start() error out',
  216. 'timer_start(0, S)',
  217. 'E921: Invalid callback argument',
  218. -1
  219. )
  220. null_expr_test('does not crash filereadable()', 'filereadable(S)', 0, 0)
  221. null_expr_test('does not crash filewritable()', 'filewritable(S)', 0, 0)
  222. null_expr_test('does not crash fnamemodify()', 'fnamemodify(S, S)', 0, '')
  223. null_expr_test('does not crash getfperm()', 'getfperm(S)', 0, '')
  224. null_expr_test('does not crash getfsize()', 'getfsize(S)', 0, -1)
  225. null_expr_test('does not crash getftime()', 'getftime(S)', 0, -1)
  226. null_expr_test('does not crash getftype()', 'getftype(S)', 0, '')
  227. null_expr_test('does not crash glob()', 'glob(S)', 0, '')
  228. null_expr_test('does not crash globpath()', 'globpath(S, S)', 0, '')
  229. null_expr_test('does not crash mkdir()', 'mkdir(S)', 0, 0)
  230. null_expr_test('does not crash sort()', 'sort(["b", S, "a"])', 0, { '', 'a', 'b' })
  231. null_expr_test('does not crash split()', 'split(S)', 0, {})
  232. null_test('can be used to set an option', 'let &grepprg = S', 0)
  233. null_expr_test('is equal to non-existent variable', 'S == V', 0, 1)
  234. end)
  235. end)