robin/laufey

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

Justin M. KeyesMerge #40865 from justinmk/fixcif907614088

master
8.4 KiB280 linesraw
1-- Tests for core Vimscript "eval" behavior.
2--
3-- See also:
4--    let_spec.lua
5--    null_spec.lua
6--    operators_spec.lua
7--
8-- Tests for the Vimscript |vimscript-functions| library should live in:
9--    test/functional/vimscript/<funcname>_spec.lua
10--    test/functional/vimscript/functions_spec.lua
11
12local t = require('test.testutil')
13local n = require('test.functional.testnvim')()
14local Screen = require('test.functional.ui.screen')
15
16local describe, it, before_each, setup, teardown, pending, finally =
17  t.describe, t.it, t.before_each, t.setup, t.teardown, t.pending, t.finally
18local mkdir = t.mkdir
19local pcall_err = t.pcall_err
20local clear = n.clear
21local eq = t.eq
22local exec = n.exec
23local exec_lua = n.exec_lua
24local exec_capture = n.exec_capture
25local eval = n.eval
26local command = n.command
27local write_file = t.write_file
28local api = n.api
29local fn = n.fn
30local sleep = vim.uv.sleep
31local assert_alive = n.assert_alive
32local poke_eventloop = n.poke_eventloop
33local feed = n.feed
34local expect_exit = n.expect_exit
35
36describe('Up to MAX_FUNC_ARGS arguments are handled by', function()
37  local max_func_args = 20 -- from eval.h
38  local range = n.fn.range
39
40  before_each(clear)
41
42  it('printf()', function()
43    local printf = n.fn.printf
44    local rep = n.fn['repeat']
45    local expected = '2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,'
46    eq(expected, printf(rep('%d,', max_func_args - 1), unpack(range(2, max_func_args))))
47    local ret =
48      pcall_err(command, 'call printf("", 2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21)')
49    eq('Vim(call):E740: Too many arguments for function printf', ret)
50  end)
51
52  it('rpcnotify()', function()
53    local rpcnotify = n.fn.rpcnotify
54    local ret = rpcnotify(0, 'foo', unpack(range(3, max_func_args)))
55    eq(1, ret)
56    ret = pcall_err(
57      command,
58      'call rpcnotify(0, "foo", 3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21)'
59    )
60    eq('Vim(call):E740: Too many arguments for function rpcnotify', ret)
61  end)
62end)
63
64describe('backtick expansion', function()
65  setup(function()
66    clear()
67    mkdir('test-backticks')
68    write_file('test-backticks/file1', 'test file 1')
69    write_file('test-backticks/file2', 'test file 2')
70    write_file('test-backticks/file3', 'test file 3')
71    mkdir('test-backticks/subdir')
72    write_file('test-backticks/subdir/file4', 'test file 4')
73    -- Long path might cause "Press ENTER" prompt; use :silent to avoid it.
74    command('silent cd test-backticks')
75  end)
76
77  teardown(function()
78    n.rmdir('test-backticks')
79  end)
80
81  it("with default 'shell'", function()
82    if t.is_os('win') then
83      command(':silent args `dir /b *2`')
84    else
85      command(':silent args `echo ***2`')
86    end
87    eq({ 'file2' }, eval('argv()'))
88    if t.is_os('win') then
89      command(':silent args `dir /s/b *4`')
90      eq({ 'subdir/file4' }, eval('map(argv(), \'fnamemodify(v:val, ":.")\')'))
91    else
92      command(':silent args `echo */*4`')
93      eq({ 'subdir/file4' }, eval('argv()'))
94    end
95  end)
96
97  it('with shell=fish', function()
98    t.skip(fn.executable('fish') == 0, 'N/A: missing "fish" command')
99
100    command('set shell=fish')
101    command(':silent args `echo ***2`')
102    eq({ 'file2' }, eval('argv()'))
103    command(':silent args `echo */*4`')
104    eq({ 'subdir/file4' }, eval('argv()'))
105  end)
106end)
107
108describe('List support code', function()
109  local dur
110  local min_dur = 8
111  local len = 131072
112
113  if not pending('does not actually allows interrupting with just got_int', function() end) then
114    return
115  end
116  -- The following tests are confirmed to work with os_breakcheck() just before
117  -- `if (got_int) {break;}` in tv_list_copy and list_join_inner() and not to
118  -- work without.
119  setup(function()
120    clear()
121    dur = 0
122    while true do
123      command(([[
124        let rt = reltime()
125        let bl = range(%u)
126        let dur = reltimestr(reltime(rt))
127      ]]):format(len))
128      dur = tonumber(api.nvim_get_var('dur'))
129      if dur >= min_dur then
130        -- print(('Using len %u, dur %g'):format(len, dur))
131        break
132      else
133        len = len * 2
134      end
135    end
136  end)
137  it('allows interrupting copy', function()
138    feed(':let t_rt = reltime()<CR>:let t_bl = copy(bl)<CR>')
139    sleep(min_dur / 16 * 1000)
140    feed('<C-c>')
141    poke_eventloop()
142    command('let t_dur = reltimestr(reltime(t_rt))')
143    local t_dur = tonumber(api.nvim_get_var('t_dur'))
144    if t_dur >= dur / 8 then
145      eq(nil, ('Took too long to cancel: %g >= %g'):format(t_dur, dur / 8))
146    end
147  end)
148  it('allows interrupting join', function()
149    feed(':let t_rt = reltime()<CR>:let t_j = join(bl)<CR>')
150    sleep(min_dur / 16 * 1000)
151    feed('<C-c>')
152    poke_eventloop()
153    command('let t_dur = reltimestr(reltime(t_rt))')
154    local t_dur = tonumber(api.nvim_get_var('t_dur'))
155    print(('t_dur: %g'):format(t_dur))
156    if t_dur >= dur / 8 then
157      eq(nil, ('Took too long to cancel: %g >= %g'):format(t_dur, dur / 8))
158    end
159  end)
160end)
161
162describe('uncaught exception', function()
163  before_each(clear)
164
165  it('is not forgotten #13490', function()
166    command('autocmd BufWinEnter * throw "i am error"')
167    t.matches('i am error', pcall_err(command, 'try | new | endtry'))
168
169    -- Like Vim, throwing here aborts the processing of the script, but does not stop :runtime!
170    -- from processing the others.
171    -- Only the first thrown exception should be rethrown from the :try below, though.
172    for i = 1, 3 do
173      write_file(
174        'throw' .. i .. '.vim',
175        ([[
176        let result ..= '%d'
177        throw 'throw%d'
178        let result ..= 'X'
179      ]]):format(i, i)
180      )
181    end
182    finally(function()
183      for i = 1, 3 do
184        os.remove('throw' .. i .. '.vim')
185      end
186    end)
187
188    command('set runtimepath+=. | let result = ""')
189    t.matches('throw1', pcall_err(command, 'try | runtime! throw*.vim | endtry'))
190    eq('123', eval('result'))
191  end)
192
193  it('multiline exception remains multiline #25350', function()
194    local screen = Screen.new(80, 11)
195    exec_lua([[
196      function _G.Oops()
197        error("oops")
198      end
199    ]])
200    feed(':try\rlua _G.Oops()\rendtry\r')
201    screen:expect([[
202      {3:                                                                                }|
203      :try                                                                            |
204      :  lua _G.Oops()                                                                |
205      :  endtry                                                                       |
206      {9:Error in :}                                                                      |
207      {9:E5108: Lua: [string "<nvim>"]:2: oops}                                           |
208      {9:stack traceback:}                                                                |
209      {9:        [C]: in function 'error'}                                                |
210      {9:        [string "<nvim>"]:2: in function 'Oops'}                                 |
211      {9:        [string ":lua"]:1: in main chunk}                                        |
212      {6:Press ENTER or type command to continue}^                                         |
213    ]])
214  end)
215end)
216
217describe('listing functions using :function', function()
218  before_each(clear)
219
220  it('works for lambda functions with <lambda> #20466', function()
221    command('let A = {-> 1}')
222    local num = exec_capture('echo A'):match("function%('<lambda>(%d+)'%)")
223    eq(
224      t.dedent([[
225           function <lambda>%s(...)
226        1  return 1
227           endfunction]]):format(num),
228      exec_capture(('function <lambda>%s'):format(num))
229    )
230  end)
231end)
232
233it('no double-free in garbage collection #16287', function()
234  clear()
235  -- Don't use exec() here as using a named script reproduces the issue better.
236  write_file(
237    'Xgarbagecollect.vim',
238    [[
239    func Foo() abort
240      let s:args = [a:000]
241      let foo0 = ""
242      let foo1 = ""
243      let foo2 = ""
244      let foo3 = ""
245      let foo4 = ""
246      let foo5 = ""
247      let foo6 = ""
248      let foo7 = ""
249      let foo8 = ""
250      let foo9 = ""
251      let foo10 = ""
252      let foo11 = ""
253      let foo12 = ""
254      let foo13 = ""
255      let foo14 = ""
256    endfunc
257
258    set updatetime=1
259    call Foo()
260    call Foo()
261  ]]
262  )
263  finally(function()
264    os.remove('Xgarbagecollect.vim')
265  end)
266  command('source Xgarbagecollect.vim')
267  sleep(10)
268  assert_alive()
269end)
270
271it('no heap-use-after-free with EXITFREE and partial as prompt callback', function()
272  clear()
273  exec([[
274    func PromptCallback(text)
275    endfunc
276    setlocal buftype=prompt
277    call prompt_setcallback('', funcref('PromptCallback'))
278  ]])
279  expect_exit(command, 'qall!')
280end)