null_spec.lua 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. local helpers = require('test.functional.helpers')(after_each)
  2. local curbufmeths = helpers.curbufmeths
  3. local exc_exec = helpers.exc_exec
  4. local command = helpers.command
  5. local clear = helpers.clear
  6. local meths = helpers.meths
  7. local funcs = helpers.funcs
  8. local eq = helpers.eq
  9. local function redir_exec(cmd)
  10. meths.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 = meths.get_var('__redir_exec_output')
  17. meths.del_var('__redir_exec_output')
  18. meths.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),
  41. redir_exec('let g:_var = ' .. expr))
  42. if val == nil then
  43. eq(0, funcs.exists('g:_var'))
  44. else
  45. eq(val, meths.get_var('_var'))
  46. end
  47. if after ~= nil then
  48. after()
  49. end
  50. end)
  51. end
  52. describe('list', function()
  53. -- Incorrect behaviour
  54. -- FIXME Should error out with different message
  55. null_test('makes :unlet act as if it is not a list', ':unlet L[0]',
  56. 'Vim(unlet):E689: Can only index a List, Dictionary or Blob')
  57. -- Subjectable behaviour
  58. null_expr_test('is equal to empty list', 'L == []', 0, 1)
  59. null_expr_test('is equal to empty list (reverse order)', '[] == L', 0, 1)
  60. -- Correct behaviour
  61. null_expr_test('can be indexed with error message for empty list', 'L[0]',
  62. 'E684: list index out of range: 0', nil)
  63. null_expr_test('can be splice-indexed', 'L[:]', 0, {})
  64. null_expr_test('is not locked', 'islocked("v:_null_list")', 0, 0)
  65. null_test('is accepted by :for', 'for x in L|throw x|endfor', 0)
  66. null_expr_test('does not crash append()', 'append(1, L)', 0, 0, function()
  67. eq({''}, curbufmeths.get_lines(0, -1, false))
  68. end)
  69. null_expr_test('does not crash setline()', 'setline(1, L)', 0, 0, function()
  70. eq({''}, curbufmeths.get_lines(0, -1, false))
  71. end)
  72. null_expr_test('is identical to itself', 'L is L', 0, 1)
  73. null_expr_test('can be sliced', 'L[:]', 0, {})
  74. null_expr_test('can be copied', 'copy(L)', 0, {})
  75. null_expr_test('can be deepcopied', 'deepcopy(L)', 0, {})
  76. null_expr_test('does not crash when indexed', 'L[1]',
  77. 'E684: list index out of range: 1', nil)
  78. null_expr_test('does not crash call()', 'call("arglistid", L)', 0, 0)
  79. null_expr_test('does not crash col()', 'col(L)', 0, 0)
  80. null_expr_test('does not crash virtcol()', 'virtcol(L)', 0, 0)
  81. null_expr_test('does not crash line()', 'line(L)', 0, 0)
  82. null_expr_test('does not crash line() with window id', 'line(L, 1000)', 0, 0)
  83. null_expr_test('does not crash count()', 'count(L, 1)', 0, 0)
  84. null_expr_test('does not crash cursor()', 'cursor(L)', 'E474: Invalid argument', -1)
  85. null_expr_test('does not crash map()', 'map(L, "v:val")', 0, {})
  86. null_expr_test('does not crash filter()', 'filter(L, "1")', 0, {})
  87. null_expr_test('is empty', 'empty(L)', 0, 1)
  88. null_expr_test('does not crash get()', 'get(L, 1, 10)', 0, 10)
  89. null_expr_test('has zero length', 'len(L)', 0, 0)
  90. null_expr_test('is accepted as an empty list by max()', 'max(L)', 0, 0)
  91. null_expr_test('is accepted as an empty list by min()', 'min(L)', 0, 0)
  92. null_expr_test('is stringified correctly', 'string(L)', 0, '[]')
  93. null_expr_test('is JSON encoded correctly', 'json_encode(L)', 0, '[]')
  94. null_test('does not crash lockvar', 'lockvar! L', 0)
  95. null_expr_test('can be added to itself', '(L + L)', 0, {})
  96. null_expr_test('can be added to itself', '(L + L) is L', 0, 1)
  97. null_expr_test('can be added to non-empty list', '([1] + L)', 0, {1})
  98. null_expr_test('can be added to non-empty list (reversed)', '(L + [1])', 0, {1})
  99. null_expr_test('is equal to itself', 'L == L', 0, 1)
  100. null_expr_test('is not not equal to itself', 'L != L', 0, 0)
  101. null_expr_test('counts correctly', 'count([L], L)', 0, 1)
  102. null_expr_test('makes map() return v:_null_list', 'map(L, "v:val") is# L', 0, 1)
  103. null_expr_test('makes filter() return v:_null_list', 'filter(L, "1") is# L', 0, 1)
  104. null_test('is treated by :let as empty list', ':let [l] = L', 'Vim(let):E688: More targets than List items')
  105. null_expr_test('is accepted as an empty list by inputlist()', '[feedkeys("\\n"), inputlist(L)]',
  106. 'Type number and <Enter> or click with the mouse (q or empty cancels): ', {0, 0})
  107. null_expr_test('is accepted as an empty list by writefile()',
  108. ('[writefile(L, "%s"), readfile("%s")]'):format(tmpfname, tmpfname),
  109. 0, {0, {}})
  110. null_expr_test('makes add() error out', 'add(L, 0)',
  111. 'E742: Cannot change value of add() argument', 1)
  112. null_expr_test('makes insert() error out', 'insert(L, 1)',
  113. 'E742: Cannot change value of insert() argument', 0)
  114. null_expr_test('does not crash remove()', 'remove(L, 0)',
  115. 'E742: Cannot change value of remove() argument', 0)
  116. null_expr_test('makes reverse() error out', 'reverse(L)',
  117. 'E742: Cannot change value of reverse() argument', 0)
  118. null_expr_test('makes sort() error out', 'sort(L)',
  119. 'E742: Cannot change value of sort() argument', 0)
  120. null_expr_test('makes uniq() error out', 'uniq(L)',
  121. 'E742: Cannot change value of uniq() argument', 0)
  122. null_expr_test('does not crash extend()', 'extend(L, [1])', 'E742: Cannot change value of extend() argument', 0)
  123. null_expr_test('does not crash extend() (second position)', 'extend([1], L)', 0, {1})
  124. null_expr_test('makes join() return empty string', 'join(L, "")', 0, '')
  125. null_expr_test('makes msgpackdump() return empty list', 'msgpackdump(L)', 0, {})
  126. null_expr_test('does not crash system()', 'system("cat", L)', 0, '')
  127. null_expr_test('does not crash setreg', 'setreg("x", L)', 0, 0)
  128. null_expr_test('does not crash systemlist()', 'systemlist("cat", L)', 0, {})
  129. null_test('does not make Neovim crash when v:oldfiles gets assigned to that', ':let v:oldfiles = L|oldfiles', 0)
  130. null_expr_test('does not make complete() crash or error out',
  131. 'execute(":normal i\\<C-r>=complete(1, L)[-1]\\n")',
  132. '', '\n', function()
  133. eq({''}, curbufmeths.get_lines(0, -1, false))
  134. end)
  135. null_expr_test('is accepted by setmatches()', 'setmatches(L)', 0, 0)
  136. null_expr_test('is accepted by setqflist()', 'setqflist(L)', 0, 0)
  137. null_expr_test('is accepted by setloclist()', 'setloclist(1, L)', 0, 0)
  138. null_test('is accepted by :cexpr', 'cexpr L', 0)
  139. null_test('is accepted by :lexpr', 'lexpr L', 0)
  140. null_expr_test('does not crash execute()', 'execute(L)', 0, '')
  141. end)
  142. describe('dict', function()
  143. it('does not crash when indexing NULL dict', function()
  144. eq('\nE716: Key not present in Dictionary: "test"',
  145. redir_exec('echo v:_null_dict.test'))
  146. end)
  147. null_expr_test('makes extend error out', 'extend(D, {})', 'E742: Cannot change value of extend() argument', 0)
  148. null_expr_test('makes extend do nothing', 'extend({1: 2}, D)', 0, {['1']=2})
  149. null_expr_test('does not crash map()', 'map(D, "v:val")', 0, {})
  150. null_expr_test('does not crash filter()', 'filter(D, "1")', 0, {})
  151. null_expr_test('makes map() return v:_null_dict', 'map(D, "v:val") is# D', 0, 1)
  152. null_expr_test('makes filter() return v:_null_dict', 'filter(D, "1") is# D', 0, 1)
  153. end)
  154. describe('string', function()
  155. null_test('does not crash :echomsg', 'echomsg S', 0)
  156. null_test('does not crash :execute', 'execute S', 0)
  157. null_expr_test('does not crash execute()', 'execute(S)', 0, '')
  158. null_expr_test('does not crash executable()', 'executable(S)', 0, 0)
  159. null_expr_test('makes timer_start() error out', 'timer_start(0, S)', 'E921: Invalid callback argument', -1)
  160. null_expr_test('does not crash filereadable()', 'filereadable(S)', 0, 0)
  161. null_expr_test('does not crash filewritable()', 'filewritable(S)', 0, 0)
  162. null_expr_test('does not crash fnamemodify()', 'fnamemodify(S, S)', 0, '')
  163. null_expr_test('does not crash getfperm()', 'getfperm(S)', 0, '')
  164. null_expr_test('does not crash getfsize()', 'getfsize(S)', 0, -1)
  165. null_expr_test('does not crash getftime()', 'getftime(S)', 0, -1)
  166. null_expr_test('does not crash getftype()', 'getftype(S)', 0, '')
  167. null_expr_test('does not crash glob()', 'glob(S)', 0, '')
  168. null_expr_test('does not crash globpath()', 'globpath(S, S)', 0, '')
  169. null_expr_test('does not crash mkdir()', 'mkdir(S)', 0, 0)
  170. null_expr_test('does not crash sort()', 'sort(["b", S, "a"])', 0, {'', 'a', 'b'})
  171. null_expr_test('does not crash split()', 'split(S)', 0, {})
  172. null_test('can be used to set an option', 'let &grepprg = S', 0)
  173. null_expr_test('is equal to non-existent variable', 'S == V', 0, 1)
  174. end)
  175. end)