robin/laufey

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

robinmerge: 1dc9728daa1d8a2702

master
8.1 KiB228 linesraw
1local t = require('test.testutil')
2local n = require('test.functional.testnvim')()
3local Screen = require('test.functional.ui.screen')
4
5local describe, it, before_each = t.describe, t.it, t.before_each
6local neq, eq, command = t.neq, t.eq, n.command
7local clear = n.clear
8local expect, eval = n.expect, n.eval
9local exec_lua = n.exec_lua
10local insert, pcall_err = n.insert, t.pcall_err
11local matches = t.matches
12local api = n.api
13local feed = n.feed
14
15describe('eval-API', function()
16  before_each(clear)
17
18  it('work', function()
19    command("call nvim_command('let g:test = 1')")
20    eq(1, eval("nvim_get_var('test')"))
21
22    local buf = eval('nvim_get_current_buf()')
23    command('call nvim_buf_set_lines(' .. buf .. ", 0, -1, v:true, ['aa', 'bb'])")
24    expect([[
25      aa
26      bb]])
27
28    command('call nvim_win_set_cursor(0, [1, 1])')
29    command("call nvim_input('ax<esc>')")
30    expect([[
31      aax
32      bb]])
33  end)
34
35  it('throw errors for invalid arguments', function()
36    local err = pcall_err(command, 'call nvim_get_current_buf("foo")')
37    eq('Vim(call):E118: Too many arguments for function: nvim_get_current_buf', err)
38
39    err = pcall_err(command, 'call nvim_set_option_value("hlsearch")')
40    eq('Vim(call):E119: Not enough arguments for function: nvim_set_option_value', err)
41
42    err = pcall_err(command, 'call nvim_buf_set_lines(1, 0, -1, [], ["list"])')
43    eq(
44      'Vim(call):E5555: API call: Wrong type for argument 4 when calling nvim_buf_set_lines, expecting Boolean',
45      err
46    )
47
48    err = pcall_err(command, 'call nvim_buf_set_lines(0, 0, -1, v:true, "string")')
49    eq(
50      'Vim(call):E5555: API call: Wrong type for argument 5 when calling nvim_buf_set_lines, expecting ArrayOf(String)',
51      err
52    )
53
54    err = pcall_err(command, 'call nvim_buf_get_number("0")')
55    eq(
56      'Vim(call):E5555: API call: Wrong type for argument 1 when calling nvim_buf_get_number, expecting Buffer',
57      err
58    )
59
60    err = pcall_err(command, 'call nvim_buf_line_count(17)')
61    eq('Vim(call):E5555: API call: Invalid buffer id: 17', err)
62  end)
63
64  it('cannot change text or window if textlock', function()
65    command('autocmd TextYankPost <buffer> ++once call nvim_buf_set_lines(0, 0, -1, v:false, [])')
66    matches(
67      'Vim%(call%):E5555: API call: E565: Not allowed to change text or change window$',
68      pcall_err(command, 'normal! yy')
69    )
70
71    command('autocmd TextYankPost <buffer> ++once call nvim_open_term(0, {})')
72    matches(
73      'Vim%(call%):E5555: API call: E565: Not allowed to change text or change window$',
74      pcall_err(command, 'normal! yy')
75    )
76
77    -- Functions checking textlock should also not be usable from <expr> mappings.
78    command('inoremap <expr> <f2> nvim_win_close(0, 1)')
79    eq(
80      'Vim(normal):E5555: API call: E565: Not allowed to change text or change window',
81      pcall_err(command, [[execute "normal i\<f2>"]])
82    )
83
84    -- Text-changing functions gave a "Failed to save undo information" error when called from an
85    -- <expr> mapping outside do_cmdline() (msg_list == NULL), so use feed() to test this.
86    command("inoremap <expr> <f2> nvim_buf_set_text(0, 0, 0, 0, 0, ['hi'])")
87    api.nvim_set_vvar('errmsg', '')
88    feed('i<f2><esc>')
89    eq(
90      'E5555: API call: E565: Not allowed to change text or change window',
91      api.nvim_get_vvar('errmsg')
92    )
93
94    -- cmdwin (#40312, #40484): E11 "Invalid in command-line window" restriction was removed.
95    local old_win = api.nvim_get_current_win()
96    feed('q:')
97    local cmdwin_win = api.nvim_get_current_win()
98    neq(old_win, cmdwin_win)
99    -- Switching to another window from the cmdwin now works (previously E11).
100    api.nvim_set_current_win(old_win)
101    eq(old_win, api.nvim_get_current_win())
102    -- ...and back into the cmdwin.
103    api.nvim_set_current_win(cmdwin_win)
104    eq(cmdwin_win, api.nvim_get_current_win())
105
106    -- nvim_buf_set_lines() in the cmdwin buffer is OK.
107    api.nvim_buf_set_lines(0, 0, -1, 1, { 'wow!' })
108    eq({ 'wow!' }, api.nvim_buf_get_lines(0, 0, -1, 1))
109
110    -- Turning the cmdwin buffer into a terminal buffer also works (#41199)
111    api.nvim_open_term(0, {})
112    api.nvim_buf_delete(0, { force = true })
113
114    feed('q:')
115    exec_lua([[
116      local cmdwin_buf = vim.api.nvim_get_current_buf()
117      vim._with({buf = vim.api.nvim_create_buf(false, true)}, function()
118        vim.api.nvim_open_term(cmdwin_buf, {})
119      end)
120    ]])
121
122    -- And turning a different buffer into a terminal from the cmdwin is OK.
123    local term_buf = api.nvim_create_buf(false, true)
124    api.nvim_open_term(term_buf, {})
125    eq('terminal', api.nvim_get_option_value('buftype', { buf = term_buf }))
126  end)
127
128  it('use buffer numbers and windows ids as handles', function()
129    local bnr = eval("bufnr('')")
130    local bhnd = eval('nvim_get_current_buf()')
131    local wid = eval('win_getid()')
132    local whnd = eval('nvim_get_current_win()')
133    eq(bnr, bhnd)
134    eq(wid, whnd)
135
136    command('new') -- creates new buffer and new window
137    local bnr2 = eval("bufnr('')")
138    local bhnd2 = eval('nvim_get_current_buf()')
139    local wid2 = eval('win_getid()')
140    local whnd2 = eval('nvim_get_current_win()')
141    eq(bnr2, bhnd2)
142    eq(wid2, whnd2)
143    neq(bnr, bnr2)
144    neq(wid, wid2)
145    -- 0 is synonymous to the current buffer
146    eq(bnr2, eval('nvim_buf_get_number(0)'))
147
148    command('bn') -- show old buffer in new window
149    eq(bnr, eval('nvim_get_current_buf()'))
150    eq(bnr, eval("bufnr('')"))
151    eq(bnr, eval('nvim_buf_get_number(0)'))
152    eq(wid2, eval('win_getid()'))
153    eq(whnd2, eval('nvim_get_current_win()'))
154  end)
155
156  it('get_lines and set_lines use NL to represent NUL', function()
157    api.nvim_buf_set_lines(0, 0, -1, true, { 'aa\0', 'b\0b' })
158    eq({ 'aa\n', 'b\nb' }, eval('nvim_buf_get_lines(0, 0, -1, 1)'))
159
160    command('call nvim_buf_set_lines(0, 1, 2, v:true, ["xx", "\\nyy"])')
161    eq({ 'aa\0', 'xx', '\0yy' }, api.nvim_buf_get_lines(0, 0, -1, 1))
162  end)
163
164  it('that are FUNC_ATTR_NOEVAL cannot be called', function()
165    -- Deprecated vim_ prefix is not exported.
166    local err = pcall_err(command, 'call vim_get_current_buffer("foo")')
167    eq('Vim(call):E117: Unknown function: vim_get_current_buffer', err)
168
169    -- Deprecated buffer_ prefix is not exported.
170    err = pcall_err(command, 'call buffer_line_count(0)')
171    eq('Vim(call):E117: Unknown function: buffer_line_count', err)
172
173    -- Functions deprecated before the api functions became available
174    -- in vimscript are not exported.
175    err = pcall_err(command, 'call buffer_get_line(0, 1)')
176    eq('Vim(call):E117: Unknown function: buffer_get_line', err)
177
178    -- some api functions are only useful from a msgpack-rpc channel
179    err = pcall_err(command, 'call nvim_set_client_info()')
180    eq('Vim(call):E117: Unknown function: nvim_set_client_info', err)
181  end)
182
183  it('have metadata accessible with api_info()', function()
184    local api_keys = eval('sort(keys(api_info()))')
185    eq({ 'error_types', 'functions', 'types', 'ui_events', 'ui_options', 'version' }, api_keys)
186  end)
187
188  it('are highlighted by vim.vim syntax file', function()
189    local screen = Screen.new(40, 8)
190
191    command('set ft=vim')
192    n.add_builddir_to_rtp()
193    command('syntax on')
194    insert([[
195      call bufnr('%')
196      call nvim_input('typing...')
197      call not_a_function(42)]])
198
199    screen:expect([[
200      {15:call} {25:bufnr}{16:(}{26:'%'}{16:)}                         |
201      {15:call} {25:nvim_input}{16:(}{26:'typing...'}{16:)}            |
202      {15:call} {9:not_a_function}{16:(}{26:42}{16:^)}                 |
203      {1:~                                       }|*4
204                                              |
205    ]])
206  end)
207
208  it('cannot be called from sandbox', function()
209    eq(
210      'Vim(call):E48: Not allowed in sandbox',
211      pcall_err(command, "sandbox call nvim_input('ievil')")
212    )
213    eq({ '' }, api.nvim_buf_get_lines(0, 0, -1, true))
214  end)
215
216  it('converts blobs to API strings', function()
217    command('let g:v1 = nvim__id(0z68656c6c6f)')
218    command('let g:v2 = nvim__id(v:_null_blob)')
219    eq(1, eval('type(g:v1)'))
220    eq(1, eval('type(g:v2)'))
221    eq('hello', eval('g:v1'))
222    eq('', eval('g:v2'))
223  end)
224
225  it('supports omitting optional trailing opts', function()
226    eq(0, eval('nvim_get_hl_ns()'))
227  end)
228end)