robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

Justin M. KeyesMerge #40865 from justinmk/fixcif907614088

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