null_spec.lua 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. local helpers = require('test.functional.helpers')(after_each)
  2. local curbufmeths = helpers.curbufmeths
  3. local redir_exec = helpers.redir_exec
  4. local exc_exec = helpers.exc_exec
  5. local command = helpers.command
  6. local clear = helpers.clear
  7. local meths = helpers.meths
  8. local funcs = helpers.funcs
  9. local eq = helpers.eq
  10. describe('NULL', function()
  11. before_each(function()
  12. clear()
  13. command('let L = v:_null_list')
  14. command('let D = v:_null_dict')
  15. command('let S = $XXX_NONEXISTENT_VAR_XXX')
  16. end)
  17. local tmpfname = 'Xtest-functional-viml-null'
  18. after_each(function()
  19. os.remove(tmpfname)
  20. end)
  21. local null_test = function(name, cmd, err)
  22. it(name, function()
  23. eq(err, exc_exec(cmd))
  24. end)
  25. end
  26. local null_expr_test = function(name, expr, err, val, after)
  27. it(name, function()
  28. eq((err == 0) and ('') or ('\n' .. err),
  29. redir_exec('let g:_var = ' .. expr))
  30. if val == nil then
  31. eq(0, funcs.exists('g:_var'))
  32. else
  33. eq(val, meths.get_var('_var'))
  34. end
  35. if after ~= nil then
  36. after()
  37. end
  38. end)
  39. end
  40. describe('list', function()
  41. -- Incorrect behaviour
  42. -- FIXME Should error out with different message
  43. null_test('makes :unlet act as if it is not a list', ':unlet L[0]',
  44. 'Vim(unlet):E689: Can only index a List or Dictionary')
  45. -- Subjectable behaviour
  46. -- FIXME Should return 1
  47. null_expr_test('is equal to empty list', 'L == []', 0, 0)
  48. -- FIXME Should return 1
  49. null_expr_test('is equal to empty list (reverse order)', '[] == L', 0, 0)
  50. -- Correct behaviour
  51. null_expr_test('can be indexed with error message for empty list', 'L[0]',
  52. 'E684: list index out of range: 0\nE15: Invalid expression: L[0]', nil)
  53. null_expr_test('can be splice-indexed', 'L[:]', 0, {})
  54. null_expr_test('is not locked', 'islocked("v:_null_list")', 0, 0)
  55. null_test('is accepted by :for', 'for x in L|throw x|endfor', 0)
  56. null_expr_test('does not crash append()', 'append(1, L)', 0, 0, function()
  57. eq({''}, curbufmeths.get_lines(0, -1, false))
  58. end)
  59. null_expr_test('does not crash setline()', 'setline(1, L)', 0, 0, function()
  60. eq({''}, curbufmeths.get_lines(0, -1, false))
  61. end)
  62. null_expr_test('is identical to itself', 'L is L', 0, 1)
  63. null_expr_test('can be sliced', 'L[:]', 0, {})
  64. null_expr_test('can be copied', 'copy(L)', 0, {})
  65. null_expr_test('can be deepcopied', 'deepcopy(L)', 0, {})
  66. null_expr_test('does not crash when indexed', 'L[1]',
  67. 'E684: list index out of range: 1\nE15: Invalid expression: L[1]', nil)
  68. null_expr_test('does not crash call()', 'call("arglistid", L)', 0, 0)
  69. null_expr_test('does not crash col()', 'col(L)', 0, 0)
  70. null_expr_test('does not crash virtcol()', 'virtcol(L)', 0, 0)
  71. null_expr_test('does not crash line()', 'line(L)', 0, 0)
  72. null_expr_test('does not crash count()', 'count(L, 1)', 0, 0)
  73. null_expr_test('does not crash cursor()', 'cursor(L)', 'E474: Invalid argument', -1)
  74. null_expr_test('does not crash map()', 'map(L, "v:val")', 0, {})
  75. null_expr_test('does not crash filter()', 'filter(L, "1")', 0, {})
  76. null_expr_test('is empty', 'empty(L)', 0, 1)
  77. null_expr_test('does not crash get()', 'get(L, 1, 10)', 0, 10)
  78. null_expr_test('has zero length', 'len(L)', 0, 0)
  79. null_expr_test('is accepted as an empty list by max()', 'max(L)', 0, 0)
  80. null_expr_test('is accepted as an empty list by min()', 'min(L)', 0, 0)
  81. null_expr_test('is stringified correctly', 'string(L)', 0, '[]')
  82. null_expr_test('is JSON encoded correctly', 'json_encode(L)', 0, '[]')
  83. null_test('does not crash lockvar', 'lockvar! L', 0)
  84. null_expr_test('can be added to itself', '(L + L)', 0, {})
  85. null_expr_test('can be added to itself', '(L + L) is L', 0, 1)
  86. null_expr_test('can be added to non-empty list', '([1] + L)', 0, {1})
  87. null_expr_test('can be added to non-empty list (reversed)', '(L + [1])', 0, {1})
  88. null_expr_test('is equal to itself', 'L == L', 0, 1)
  89. null_expr_test('is not not equal to itself', 'L != L', 0, 0)
  90. null_expr_test('counts correctly', 'count([L], L)', 0, 1)
  91. null_expr_test('makes map() return v:_null_list', 'map(L, "v:val") is# L', 0, 1)
  92. null_expr_test('makes filter() return v:_null_list', 'filter(L, "1") is# L', 0, 1)
  93. null_test('is treated by :let as empty list', ':let [l] = L', 'Vim(let):E688: More targets than List items')
  94. null_expr_test('is accepted as an empty list by inputlist()', '[feedkeys("\\n"), inputlist(L)]',
  95. 'Type number and <Enter> or click with mouse (empty cancels): ', {0, 0})
  96. null_expr_test('is accepted as an empty list by writefile()',
  97. ('[writefile(L, "%s"), readfile("%s")]'):format(tmpfname, tmpfname),
  98. 0, {0, {}})
  99. null_expr_test('makes add() error out', 'add(L, 0)',
  100. 'E742: Cannot change value of add() argument', 1)
  101. null_expr_test('makes insert() error out', 'insert(L, 1)',
  102. 'E742: Cannot change value of insert() argument', 0)
  103. null_expr_test('does not crash remove()', 'remove(L, 0)',
  104. 'E742: Cannot change value of remove() argument', 0)
  105. null_expr_test('makes reverse() error out', 'reverse(L)',
  106. 'E742: Cannot change value of reverse() argument', 0)
  107. null_expr_test('makes sort() error out', 'sort(L)',
  108. 'E742: Cannot change value of sort() argument', 0)
  109. null_expr_test('makes uniq() error out', 'uniq(L)',
  110. 'E742: Cannot change value of uniq() argument', 0)
  111. null_expr_test('does not crash extend()', 'extend(L, [1])', 'E742: Cannot change value of extend() argument', 0)
  112. null_expr_test('does not crash extend() (second position)', 'extend([1], L)', 0, {1})
  113. null_expr_test('makes join() return empty string', 'join(L, "")', 0, '')
  114. null_expr_test('makes msgpackdump() return empty list', 'msgpackdump(L)', 0, {})
  115. null_expr_test('does not crash system()', 'system("cat", L)', 0, '')
  116. null_expr_test('does not crash setreg', 'setreg("x", L)', 0, 0)
  117. null_expr_test('does not crash systemlist()', 'systemlist("cat", L)', 0, {})
  118. null_test('does not make Neovim crash when v:oldfiles gets assigned to that', ':let v:oldfiles = L|oldfiles', 0)
  119. null_expr_test('does not make complete() crash or error out',
  120. 'execute(":normal i\\<C-r>=complete(1, L)[-1]\\n")',
  121. '', '\n', function()
  122. eq({''}, curbufmeths.get_lines(0, -1, false))
  123. end)
  124. null_expr_test('is accepted by setmatches()', 'setmatches(L)', 0, 0)
  125. null_expr_test('is accepted by setqflist()', 'setqflist(L)', 0, 0)
  126. null_expr_test('is accepted by setloclist()', 'setloclist(1, L)', 0, 0)
  127. null_test('is accepted by :cexpr', 'cexpr L', 0)
  128. null_test('is accepted by :lexpr', 'lexpr L', 0)
  129. end)
  130. describe('dict', function()
  131. it('does not crash when indexing NULL dict', function()
  132. eq('\nE716: Key not present in Dictionary: test\nE15: Invalid expression: v:_null_dict.test',
  133. redir_exec('echo v:_null_dict.test'))
  134. end)
  135. null_expr_test('makes extend error out', 'extend(D, {})', 'E742: Cannot change value of extend() argument', 0)
  136. null_expr_test('makes extend do nothing', 'extend({1: 2}, D)', 0, {['1']=2})
  137. null_expr_test('does not crash map()', 'map(D, "v:val")', 0, {})
  138. null_expr_test('does not crash filter()', 'filter(D, "1")', 0, {})
  139. null_expr_test('makes map() return v:_null_dict', 'map(D, "v:val") is# D', 0, 1)
  140. null_expr_test('makes filter() return v:_null_dict', 'filter(D, "1") is# D', 0, 1)
  141. end)
  142. end)