blob: 7de4574530ada9f75c76851ea879d48670019bfd [file] [log] [blame]
Bram Moolenaard58f03b2017-01-29 22:48:45 +01001" Tests for Lua.
Bram Moolenaard58f03b2017-01-29 22:48:45 +01002
Bram Moolenaarb46fecd2019-06-15 17:58:09 +02003source check.vim
Bram Moolenaarc8970b92020-10-26 20:18:08 +01004
5" This test also works without the lua feature.
6func Test_skip_lua()
7 if 0
8 lua print("Not executed")
9 endif
10endfunc
11
Bram Moolenaarb46fecd2019-06-15 17:58:09 +020012CheckFeature lua
Bram Moolenaar5feabe02020-01-30 18:24:53 +010013CheckFeature float
Bram Moolenaard58f03b2017-01-29 22:48:45 +010014
Bram Moolenaare49b8e82020-07-01 13:52:55 +020015let s:luaver = split(split(luaeval('_VERSION'), ' ')[1], '\.')
16let s:major = str2nr(s:luaver[0])
17let s:minor = str2nr(s:luaver[1])
18if s:major < 5 || (s:major == 5 && s:minor < 3)
19 let s:lua_53_or_later = 0
20else
21 let s:lua_53_or_later = 1
22endif
23
Bram Moolenaare165f632019-03-10 09:48:59 +010024func TearDown()
25 " Run garbage collection after each test to exercise luaV_setref().
26 call test_garbagecollect_now()
27endfunc
28
Bram Moolenaar4ff48142018-06-30 21:50:25 +020029" Check that switching to another buffer does not trigger ml_get error.
Bram Moolenaar5feabe02020-01-30 18:24:53 +010030func Test_lua_command_new_no_ml_get_error()
Bram Moolenaard58f03b2017-01-29 22:48:45 +010031 new
32 let wincount = winnr('$')
33 call setline(1, ['one', 'two', 'three'])
34 luado vim.command("new")
35 call assert_equal(wincount + 1, winnr('$'))
Bram Moolenaar4ff48142018-06-30 21:50:25 +020036 %bwipe!
37endfunc
38
39" Test vim.command()
Bram Moolenaar5feabe02020-01-30 18:24:53 +010040func Test_lua_command()
Bram Moolenaar4ff48142018-06-30 21:50:25 +020041 new
42 call setline(1, ['one', 'two', 'three'])
43 luado vim.command("1,2d_")
44 call assert_equal(['three'], getline(1, '$'))
Bram Moolenaard58f03b2017-01-29 22:48:45 +010045 bwipe!
Bram Moolenaar4ff48142018-06-30 21:50:25 +020046endfunc
47
Bram Moolenaare49b8e82020-07-01 13:52:55 +020048func Test_lua_luado()
49 new
50 call setline(1, ['one', 'two'])
51 luado return(linenr)
52 call assert_equal(['1', '2'], getline(1, '$'))
53 close!
54
55 " Error cases
56 call assert_fails('luado string.format()',
57 \ "[string \"vim chunk\"]:1: bad argument #1 to 'format' (string expected, got no value)")
58 call assert_fails('luado func()',
59 \ s:lua_53_or_later
60 \ ? "[string \"vim chunk\"]:1: attempt to call a nil value (global 'func')"
61 \ : "[string \"vim chunk\"]:1: attempt to call global 'func' (a nil value)")
62 call assert_fails('luado error("failed")', "[string \"vim chunk\"]:1: failed")
63endfunc
64
Bram Moolenaar4ff48142018-06-30 21:50:25 +020065" Test vim.eval()
Bram Moolenaar5feabe02020-01-30 18:24:53 +010066func Test_lua_eval()
Bram Moolenaar4ff48142018-06-30 21:50:25 +020067 " lua.eval with a number
68 lua v = vim.eval('123')
69 call assert_equal('number', luaeval('vim.type(v)'))
Bram Moolenaareb04f082020-05-17 14:32:35 +020070 call assert_equal(123, luaeval('v'))
Bram Moolenaar4ff48142018-06-30 21:50:25 +020071
72 " lua.eval with a string
73 lua v = vim.eval('"abc"')
Bram Moolenaar02b31112019-08-31 22:16:38 +020074 call assert_equal('string', 'vim.type(v)'->luaeval())
Bram Moolenaar4ff48142018-06-30 21:50:25 +020075 call assert_equal('abc', luaeval('v'))
76
77 " lua.eval with a list
78 lua v = vim.eval("['a']")
79 call assert_equal('list', luaeval('vim.type(v)'))
80 call assert_equal(['a'], luaeval('v'))
81
82 " lua.eval with a dict
83 lua v = vim.eval("{'a':'b'}")
84 call assert_equal('dict', luaeval('vim.type(v)'))
85 call assert_equal({'a':'b'}, luaeval('v'))
86
Bram Moolenaarb7828692019-03-23 13:57:02 +010087 " lua.eval with a blob
88 lua v = vim.eval("0z00112233.deadbeef")
89 call assert_equal('blob', luaeval('vim.type(v)'))
90 call assert_equal(0z00112233.deadbeef, luaeval('v'))
91
Bram Moolenaare49b8e82020-07-01 13:52:55 +020092 " lua.eval with a float
93 lua v = vim.eval('3.14')
94 call assert_equal('number', luaeval('vim.type(v)'))
95 call assert_equal(3.14, luaeval('v'))
96
97 " lua.eval with a bool
98 lua v = vim.eval('v:true')
99 call assert_equal('number', luaeval('vim.type(v)'))
100 call assert_equal(1, luaeval('v'))
101 lua v = vim.eval('v:false')
102 call assert_equal('number', luaeval('vim.type(v)'))
103 call assert_equal(0, luaeval('v'))
104
105 " lua.eval with a null
106 lua v = vim.eval('v:null')
107 call assert_equal('nil', luaeval('vim.type(v)'))
108 call assert_equal(v:null, luaeval('v'))
109
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200110 call assert_fails('lua v = vim.eval(nil)',
111 \ "[string \"vim chunk\"]:1: bad argument #1 to 'eval' (string expected, got nil)")
112 call assert_fails('lua v = vim.eval(true)',
113 \ "[string \"vim chunk\"]:1: bad argument #1 to 'eval' (string expected, got boolean)")
114 call assert_fails('lua v = vim.eval({})',
115 \ "[string \"vim chunk\"]:1: bad argument #1 to 'eval' (string expected, got table)")
116 call assert_fails('lua v = vim.eval(print)',
117 \ "[string \"vim chunk\"]:1: bad argument #1 to 'eval' (string expected, got function)")
118 call assert_fails('lua v = vim.eval(vim.buffer())',
119 \ "[string \"vim chunk\"]:1: bad argument #1 to 'eval' (string expected, got userdata)")
120
121 lua v = nil
122endfunc
123
Bram Moolenaar86c3a212021-03-08 19:50:24 +0100124" Test luaeval() with lambda
125func Test_luaeval_with_lambda()
126 lua function hello_luaeval_lambda(a, cb) return a .. cb() end
127 call assert_equal('helloworld',
128 \ luaeval('hello_luaeval_lambda(_A[1], _A[2])',
129 \ ['hello', {->'world'}]))
130 lua hello_luaeval_lambda = nil
131endfunc
132
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200133" Test vim.window()
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100134func Test_lua_window()
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200135 e Xfoo2
136 new Xfoo1
137
138 " Window 1 (top window) contains Xfoo1
139 " Window 2 (bottom window) contains Xfoo2
140 call assert_equal('Xfoo1', luaeval('vim.window(1):buffer().name'))
141 call assert_equal('Xfoo2', luaeval('vim.window(2):buffer().name'))
142
143 " Window 3 does not exist so vim.window(3) should return nil
144 call assert_equal('nil', luaeval('tostring(vim.window(3))'))
145
Bram Moolenaare49b8e82020-07-01 13:52:55 +0200146 call assert_fails("let n = luaeval('vim.window().xyz()')",
147 \ s:lua_53_or_later
148 \ ? "[string \"luaeval\"]:1: attempt to call a nil value (field 'xyz')"
149 \ : "[string \"luaeval\"]:1: attempt to call field 'xyz' (a nil value)")
150 call assert_fails('lua vim.window().xyz = 1',
151 \ "[string \"vim chunk\"]:1: invalid window property: `xyz'")
152
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200153 %bwipe!
154endfunc
155
156" Test vim.window().height
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100157func Test_lua_window_height()
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200158 new
159 lua vim.window().height = 2
160 call assert_equal(2, winheight(0))
161 lua vim.window().height = vim.window().height + 1
162 call assert_equal(3, winheight(0))
163 bwipe!
164endfunc
165
166" Test vim.window().width
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100167func Test_lua_window_width()
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200168 vert new
169 lua vim.window().width = 2
170 call assert_equal(2, winwidth(0))
171 lua vim.window().width = vim.window().width + 1
172 call assert_equal(3, winwidth(0))
173 bwipe!
174endfunc
175
176" Test vim.window().line and vim.window.col
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100177func Test_lua_window_line_col()
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200178 new
179 call setline(1, ['line1', 'line2', 'line3'])
180 lua vim.window().line = 2
181 lua vim.window().col = 4
182 call assert_equal([0, 2, 4, 0], getpos('.'))
183 lua vim.window().line = vim.window().line + 1
184 lua vim.window().col = vim.window().col - 1
185 call assert_equal([0, 3, 3, 0], getpos('.'))
186
187 call assert_fails('lua vim.window().line = 10',
188 \ '[string "vim chunk"]:1: line out of range')
189 bwipe!
190endfunc
191
Bram Moolenaareb04f082020-05-17 14:32:35 +0200192" Test vim.call
193func Test_lua_call()
194 call assert_equal(has('lua'), luaeval('vim.call("has", "lua")'))
195 call assert_equal(printf("Hello %s", "vim"), luaeval('vim.call("printf", "Hello %s", "vim")'))
Bram Moolenaare49b8e82020-07-01 13:52:55 +0200196
197 " Error cases
198 call assert_fails("call luaeval('vim.call(\"min\", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21)')",
Bram Moolenaarb898a022020-07-12 18:33:53 +0200199 \ s:lua_53_or_later
200 \ ? '[string "luaeval"]:1: Function called with too many arguments'
201 \ : 'Function called with too many arguments')
Bram Moolenaare49b8e82020-07-01 13:52:55 +0200202 lua co = coroutine.create(function () print("hi") end)
203 call assert_fails("call luaeval('vim.call(\"type\", co)')",
Bram Moolenaarb898a022020-07-12 18:33:53 +0200204 \ s:lua_53_or_later
205 \ ? '[string "luaeval"]:1: lua: cannot convert value'
206 \ : 'lua: cannot convert value')
Bram Moolenaare49b8e82020-07-01 13:52:55 +0200207 lua co = nil
Bram Moolenaarb898a022020-07-12 18:33:53 +0200208 call assert_fails("call luaeval('vim.call(\"abc\")')",
209 \ ['E117:', s:lua_53_or_later ? '\[string "luaeval"]:1: lua: call_vim_function failed'
210 \ : 'lua: call_vim_function failed'])
Bram Moolenaareb04f082020-05-17 14:32:35 +0200211endfunc
212
213" Test vim.fn.*
214func Test_lua_fn()
215 call assert_equal(has('lua'), luaeval('vim.fn.has("lua")'))
216 call assert_equal(printf("Hello %s", "vim"), luaeval('vim.fn.printf("Hello %s", "vim")'))
217endfunc
218
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200219" Test setting the current window
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100220func Test_lua_window_set_current()
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200221 new Xfoo1
222 lua w1 = vim.window()
223 new Xfoo2
224 lua w2 = vim.window()
225
226 call assert_equal('Xfoo2', bufname('%'))
227 lua w1()
228 call assert_equal('Xfoo1', bufname('%'))
229 lua w2()
230 call assert_equal('Xfoo2', bufname('%'))
231
Bram Moolenaar2f362bf2018-07-01 19:49:27 +0200232 lua w1, w2 = nil
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200233 %bwipe!
234endfunc
235
236" Test vim.window().buffer
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100237func Test_lua_window_buffer()
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200238 new Xfoo1
239 lua w1 = vim.window()
240 lua b1 = w1.buffer()
241 new Xfoo2
242 lua w2 = vim.window()
243 lua b2 = w2.buffer()
244
245 lua b1()
246 call assert_equal('Xfoo1', bufname('%'))
247 lua b2()
248 call assert_equal('Xfoo2', bufname('%'))
249
Bram Moolenaar2f362bf2018-07-01 19:49:27 +0200250 lua b1, b2, w1, w2 = nil
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200251 %bwipe!
252endfunc
253
254" Test vim.window():previous() and vim.window():next()
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100255func Test_lua_window_next_previous()
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200256 new Xfoo1
257 new Xfoo2
258 new Xfoo3
259 wincmd j
260
261 call assert_equal('Xfoo2', luaeval('vim.window().buffer().name'))
262 call assert_equal('Xfoo1', luaeval('vim.window():next():buffer().name'))
263 call assert_equal('Xfoo3', luaeval('vim.window():previous():buffer().name'))
264
265 %bwipe!
266endfunc
267
268" Test vim.window():isvalid()
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100269func Test_lua_window_isvalid()
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200270 new Xfoo
271 lua w = vim.window()
272 call assert_true(luaeval('w:isvalid()'))
273
274 " FIXME: how to test the case when isvalid() returns v:false?
275 " isvalid() gives errors when the window is deleted. Is it a bug?
276
277 lua w = nil
278 bwipe!
279endfunc
280
281" Test vim.buffer() with and without argument
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100282func Test_lua_buffer()
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200283 new Xfoo1
284 let bn1 = bufnr('%')
285 new Xfoo2
286 let bn2 = bufnr('%')
287
288 " Test vim.buffer() without argument.
289 call assert_equal('Xfoo2', luaeval("vim.buffer().name"))
290
291 " Test vim.buffer() with string argument.
292 call assert_equal('Xfoo1', luaeval("vim.buffer('Xfoo1').name"))
293 call assert_equal('Xfoo2', luaeval("vim.buffer('Xfoo2').name"))
294
295 " Test vim.buffer() with integer argument.
296 call assert_equal('Xfoo1', luaeval("vim.buffer(" . bn1 . ").name"))
297 call assert_equal('Xfoo2', luaeval("vim.buffer(" . bn2 . ").name"))
298
Bram Moolenaar2f362bf2018-07-01 19:49:27 +0200299 lua bn1, bn2 = nil
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200300 %bwipe!
301endfunc
302
303" Test vim.buffer().name and vim.buffer().fname
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100304func Test_lua_buffer_name()
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200305 new
Bram Moolenaarfe08df42018-07-07 23:07:41 +0200306 call assert_equal('', luaeval('vim.buffer().name'))
307 call assert_equal('', luaeval('vim.buffer().fname'))
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200308 bwipe!
309
310 new Xfoo
311 call assert_equal('Xfoo', luaeval('vim.buffer().name'))
312 call assert_equal(expand('%:p'), luaeval('vim.buffer().fname'))
313 bwipe!
314endfunc
315
316" Test vim.buffer().number
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100317func Test_lua_buffer_number()
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200318 " All numbers in Lua are floating points number (no integers).
319 call assert_equal(bufnr('%'), float2nr(luaeval('vim.buffer().number')))
320endfunc
321
322" Test inserting lines in buffer.
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100323func Test_lua_buffer_insert()
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200324 new
325 lua vim.buffer()[1] = '3'
326 lua vim.buffer():insert('1', 0)
327 lua vim.buffer():insert('2', 1)
328 lua vim.buffer():insert('4', 10)
329
330 call assert_equal(['1', '2', '3', '4'], getline(1, '$'))
Bram Moolenaare49b8e82020-07-01 13:52:55 +0200331 call assert_equal('4', luaeval('vim.buffer()[4]'))
332 call assert_equal(v:null, luaeval('vim.buffer()[5]'))
333 call assert_equal(v:null, luaeval('vim.buffer()[{}]'))
334 call assert_fails('lua vim.buffer():xyz()',
335 \ s:lua_53_or_later
336 \ ? "[string \"vim chunk\"]:1: attempt to call a nil value (method 'xyz')"
337 \ : "[string \"vim chunk\"]:1: attempt to call method 'xyz' (a nil value)")
338 call assert_fails('lua vim.buffer()[1] = {}',
339 \ '[string "vim chunk"]:1: wrong argument to change')
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200340 bwipe!
341endfunc
342
343" Test deleting line in buffer
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100344func Test_lua_buffer_delete()
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200345 new
346 call setline(1, ['1', '2', '3'])
Bram Moolenaare49b8e82020-07-01 13:52:55 +0200347 call cursor(3, 1)
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200348 lua vim.buffer()[2] = nil
349 call assert_equal(['1', '3'], getline(1, '$'))
350
351 call assert_fails('lua vim.buffer()[3] = nil',
352 \ '[string "vim chunk"]:1: invalid line number')
353 bwipe!
354endfunc
355
356" Test #vim.buffer() i.e. number of lines in buffer
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100357func Test_lua_buffer_number_lines()
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200358 new
359 call setline(1, ['a', 'b', 'c'])
Bram Moolenaareb04f082020-05-17 14:32:35 +0200360 call assert_equal(3, luaeval('#vim.buffer()'))
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200361 bwipe!
362endfunc
363
364" Test vim.buffer():next() and vim.buffer():previous()
365" Note that these functions get the next or previous buffers
366" but do not switch buffer.
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100367func Test_lua_buffer_next_previous()
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200368 new Xfoo1
369 new Xfoo2
370 new Xfoo3
371 b Xfoo2
372
373 lua bn = vim.buffer():next()
374 lua bp = vim.buffer():previous()
375
376 call assert_equal('Xfoo2', luaeval('vim.buffer().name'))
377 call assert_equal('Xfoo1', luaeval('bp.name'))
378 call assert_equal('Xfoo3', luaeval('bn.name'))
379
380 call assert_equal('Xfoo2', bufname('%'))
381
382 lua bn()
383 call assert_equal('Xfoo3', luaeval('vim.buffer().name'))
384 call assert_equal('Xfoo3', bufname('%'))
385
386 lua bp()
387 call assert_equal('Xfoo1', luaeval('vim.buffer().name'))
388 call assert_equal('Xfoo1', bufname('%'))
389
Bram Moolenaar2f362bf2018-07-01 19:49:27 +0200390 lua bn, bp = nil
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200391 %bwipe!
392endfunc
393
394" Test vim.buffer():isvalid()
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100395func Test_lua_buffer_isvalid()
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200396 new Xfoo
397 lua b = vim.buffer()
398 call assert_true(luaeval('b:isvalid()'))
399
400 " FIXME: how to test the case when isvalid() returns v:false?
401 " isvalid() gives errors when the buffer is wiped. Is it a bug?
402
403 lua b = nil
404 bwipe!
405endfunc
406
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100407func Test_lua_list()
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200408 call assert_equal([], luaeval('vim.list()'))
409
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200410 let l = []
411 lua l = vim.eval('l')
412 lua l:add(123)
413 lua l:add('abc')
414 lua l:add(true)
415 lua l:add(false)
Bram Moolenaar9067cd62019-01-01 00:41:54 +0100416 lua l:add(nil)
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200417 lua l:add(vim.eval("[1, 2, 3]"))
418 lua l:add(vim.eval("{'a':1, 'b':2, 'c':3}"))
Bram Moolenaareb04f082020-05-17 14:32:35 +0200419 call assert_equal([123, 'abc', v:true, v:false, v:null, [1, 2, 3], {'a': 1, 'b': 2, 'c': 3}], l)
420 call assert_equal(7, luaeval('#l'))
Bram Moolenaara8a60d02018-07-02 22:54:36 +0200421 call assert_match('^list: \%(0x\)\?\x\+$', luaeval('tostring(l)'))
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200422
Bram Moolenaarbd846172020-06-27 12:32:57 +0200423 lua l[1] = 124
424 lua l[6] = nil
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200425 lua l:insert('first')
426 lua l:insert('xx', 3)
Bram Moolenaare49b8e82020-07-01 13:52:55 +0200427 call assert_fails('lua l:insert("xx", -20)',
428 \ '[string "vim chunk"]:1: invalid position')
Bram Moolenaareb04f082020-05-17 14:32:35 +0200429 call assert_equal(['first', 124, 'abc', 'xx', v:true, v:false, v:null, {'a': 1, 'b': 2, 'c': 3}], l)
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200430
Bram Moolenaar2f362bf2018-07-01 19:49:27 +0200431 lockvar 1 l
432 call assert_fails('lua l:add("x")', '[string "vim chunk"]:1: list is locked')
Bram Moolenaare49b8e82020-07-01 13:52:55 +0200433 call assert_fails('lua l:insert(2)', '[string "vim chunk"]:1: list is locked')
434 call assert_fails('lua l[9] = 1', '[string "vim chunk"]:1: list is locked')
435
436 unlockvar l
437 let l = [1, 2]
438 lua ll = vim.eval('l')
439 let x = luaeval("ll[3]")
440 call assert_equal(v:null, x)
441 call assert_fails('let x = luaeval("ll:xyz(3)")',
442 \ s:lua_53_or_later
443 \ ? "[string \"luaeval\"]:1: attempt to call a nil value (method 'xyz')"
444 \ : "[string \"luaeval\"]:1: attempt to call method 'xyz' (a nil value)")
445 let y = luaeval("ll[{}]")
446 call assert_equal(v:null, y)
Bram Moolenaar2f362bf2018-07-01 19:49:27 +0200447
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200448 lua l = nil
449endfunc
450
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100451func Test_lua_list_table()
Bram Moolenaar2f362bf2018-07-01 19:49:27 +0200452 " See :help lua-vim
453 " Non-numeric keys should not be used to initialize the list
454 " so say = 'hi' should be ignored.
455 lua t = {3.14, 'hello', false, true, say = 'hi'}
456 call assert_equal([3.14, 'hello', v:false, v:true], luaeval('vim.list(t)'))
457 lua t = nil
458
459 call assert_fails('lua vim.list(1)', '[string "vim chunk"]:1: table expected, got number')
460 call assert_fails('lua vim.list("x")', '[string "vim chunk"]:1: table expected, got string')
461 call assert_fails('lua vim.list(print)', '[string "vim chunk"]:1: table expected, got function')
462 call assert_fails('lua vim.list(true)', '[string "vim chunk"]:1: table expected, got boolean')
463endfunc
464
Bram Moolenaara1f9f862020-06-28 22:41:26 +0200465func Test_lua_list_table_insert_remove()
Bram Moolenaare49b8e82020-07-01 13:52:55 +0200466 if !s:lua_53_or_later
Bram Moolenaara1f9f862020-06-28 22:41:26 +0200467 throw 'Skipped: Lua version < 5.3'
468 endif
469
470 let l = [1, 2]
471 lua t = vim.eval('l')
472 lua table.insert(t, 10)
473 lua t[#t + 1] = 20
474 lua table.insert(t, 2, 30)
475 call assert_equal(l, [1, 30, 2, 10, 20])
476 lua table.remove(t, 2)
477 call assert_equal(l, [1, 2, 10, 20])
478 lua t[3] = nil
479 call assert_equal(l, [1, 2, 20])
480 lua removed_value = table.remove(t, 3)
481 call assert_equal(luaeval('removed_value'), 20)
482 lua t = nil
483 lua removed_value = nil
484 unlet l
485endfunc
486
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200487" Test l() i.e. iterator on list
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100488func Test_lua_list_iter()
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200489 lua l = vim.list():add('foo'):add('bar')
490 lua str = ''
491 lua for v in l() do str = str .. v end
492 call assert_equal('foobar', luaeval('str'))
493
Bram Moolenaar2f362bf2018-07-01 19:49:27 +0200494 lua str, l = nil
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200495endfunc
496
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100497func Test_lua_recursive_list()
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200498 lua l = vim.list():add(1):add(2)
499 lua l = l:add(l)
500
Bram Moolenaarbd846172020-06-27 12:32:57 +0200501 call assert_equal(1, luaeval('l[1]'))
502 call assert_equal(2, luaeval('l[2]'))
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200503
Bram Moolenaarbd846172020-06-27 12:32:57 +0200504 call assert_equal(1, luaeval('l[3][1]'))
505 call assert_equal(2, luaeval('l[3][2]'))
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200506
Bram Moolenaarbd846172020-06-27 12:32:57 +0200507 call assert_equal(1, luaeval('l[3][3][1]'))
508 call assert_equal(2, luaeval('l[3][3][2]'))
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200509
Bram Moolenaareb04f082020-05-17 14:32:35 +0200510 call assert_equal('[1, 2, [...]]', string(luaeval('l')))
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200511
Bram Moolenaara8a60d02018-07-02 22:54:36 +0200512 call assert_match('^list: \%(0x\)\?\x\+$', luaeval('tostring(l)'))
Bram Moolenaarbd846172020-06-27 12:32:57 +0200513 call assert_equal(luaeval('tostring(l)'), luaeval('tostring(l[3])'))
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200514
Bram Moolenaarbd846172020-06-27 12:32:57 +0200515 call assert_equal(luaeval('l'), luaeval('l[3]'))
516 call assert_equal(luaeval('l'), luaeval('l[3][3]'))
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200517
518 lua l = nil
519endfunc
520
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100521func Test_lua_dict()
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200522 call assert_equal({}, luaeval('vim.dict()'))
523
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200524 let d = {}
525 lua d = vim.eval('d')
526 lua d[0] = 123
527 lua d[1] = "abc"
528 lua d[2] = true
529 lua d[3] = false
530 lua d[4] = vim.eval("[1, 2, 3]")
531 lua d[5] = vim.eval("{'a':1, 'b':2, 'c':3}")
Bram Moolenaareb04f082020-05-17 14:32:35 +0200532 call assert_equal({'0':123, '1':'abc', '2':v:true, '3':v:false, '4': [1, 2, 3], '5': {'a':1, 'b':2, 'c':3}}, d)
533 call assert_equal(6, luaeval('#d'))
Bram Moolenaara8a60d02018-07-02 22:54:36 +0200534 call assert_match('^dict: \%(0x\)\?\x\+$', luaeval('tostring(d)'))
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200535
536 call assert_equal('abc', luaeval('d[1]'))
Bram Moolenaare49b8e82020-07-01 13:52:55 +0200537 call assert_equal(v:null, luaeval('d[22]'))
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200538
539 lua d[0] = 124
540 lua d[4] = nil
Bram Moolenaareb04f082020-05-17 14:32:35 +0200541 call assert_equal({'0':124, '1':'abc', '2':v:true, '3':v:false, '5': {'a':1, 'b':2, 'c':3}}, d)
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200542
Bram Moolenaar2f362bf2018-07-01 19:49:27 +0200543 lockvar 1 d
544 call assert_fails('lua d[6] = 1', '[string "vim chunk"]:1: dict is locked')
Bram Moolenaare49b8e82020-07-01 13:52:55 +0200545 unlockvar d
546
547 " Error case
548 lua d = {}
549 lua d[''] = 10
550 call assert_fails("let t = luaeval('vim.dict(d)')",
Bram Moolenaarb898a022020-07-12 18:33:53 +0200551 \ s:lua_53_or_later
552 \ ? '[string "luaeval"]:1: table has empty key'
553 \ : 'table has empty key')
Bram Moolenaare49b8e82020-07-01 13:52:55 +0200554 let d = {}
555 lua x = vim.eval('d')
556 call assert_fails("lua x[''] = 10", '[string "vim chunk"]:1: empty key')
557 lua x['a'] = nil
558 call assert_equal({}, d)
559
560 " cannot assign funcrefs in the global scope
561 lua x = vim.eval('g:')
562 call assert_fails("lua x['min'] = vim.funcref('max')",
563 \ '[string "vim chunk"]:1: cannot assign funcref to builtin scope')
Bram Moolenaar2f362bf2018-07-01 19:49:27 +0200564
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200565 lua d = nil
566endfunc
567
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100568func Test_lua_dict_table()
Bram Moolenaar2f362bf2018-07-01 19:49:27 +0200569 lua t = {key1 = 'x', key2 = 3.14, key3 = true, key4 = false}
570 call assert_equal({'key1': 'x', 'key2': 3.14, 'key3': v:true, 'key4': v:false},
571 \ luaeval('vim.dict(t)'))
572
573 " Same example as in :help lua-vim.
574 lua t = {math.pi, false, say = 'hi'}
575 " FIXME: commented out as it currently does not work as documented:
576 " Expected {'say': 'hi'}
577 " but got {'1': 3.141593, '2': v:false, 'say': 'hi'}
578 " Is the documentation or the code wrong?
579 "call assert_equal({'say' : 'hi'}, luaeval('vim.dict(t)'))
580 lua t = nil
581
582 call assert_fails('lua vim.dict(1)', '[string "vim chunk"]:1: table expected, got number')
583 call assert_fails('lua vim.dict("x")', '[string "vim chunk"]:1: table expected, got string')
584 call assert_fails('lua vim.dict(print)', '[string "vim chunk"]:1: table expected, got function')
585 call assert_fails('lua vim.dict(true)', '[string "vim chunk"]:1: table expected, got boolean')
586endfunc
587
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200588" Test d() i.e. iterator on dictionary
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100589func Test_lua_dict_iter()
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200590 let d = {'a': 1, 'b':2}
591 lua d = vim.eval('d')
592 lua str = ''
593 lua for k,v in d() do str = str .. k ..':' .. v .. ',' end
594 call assert_equal('a:1,b:2,', luaeval('str'))
595
Bram Moolenaar2f362bf2018-07-01 19:49:27 +0200596 lua str, d = nil
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200597endfunc
598
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100599func Test_lua_blob()
Bram Moolenaarb7828692019-03-23 13:57:02 +0100600 call assert_equal(0z, luaeval('vim.blob("")'))
601 call assert_equal(0z31326162, luaeval('vim.blob("12ab")'))
602 call assert_equal(0z00010203, luaeval('vim.blob("\x00\x01\x02\x03")'))
603 call assert_equal(0z8081FEFF, luaeval('vim.blob("\x80\x81\xfe\xff")'))
604
605 lua b = vim.blob("\x00\x00\x00\x00")
606 call assert_equal(0z00000000, luaeval('b'))
Bram Moolenaareb04f082020-05-17 14:32:35 +0200607 call assert_equal(4, luaeval('#b'))
Bram Moolenaarb7828692019-03-23 13:57:02 +0100608 lua b[0], b[1], b[2], b[3] = 1, 32, 256, 0xff
609 call assert_equal(0z012000ff, luaeval('b'))
610 lua b[4] = string.byte("z", 1)
611 call assert_equal(0z012000ff.7a, luaeval('b'))
Bram Moolenaareb04f082020-05-17 14:32:35 +0200612 call assert_equal(5, luaeval('#b'))
Bram Moolenaarb7828692019-03-23 13:57:02 +0100613 call assert_fails('lua b[#b+1] = 0x80', '[string "vim chunk"]:1: index out of range')
614 lua b:add("12ab")
615 call assert_equal(0z012000ff.7a313261.62, luaeval('b'))
Bram Moolenaareb04f082020-05-17 14:32:35 +0200616 call assert_equal(9, luaeval('#b'))
Bram Moolenaarb7828692019-03-23 13:57:02 +0100617 call assert_fails('lua b:add(nil)', '[string "vim chunk"]:1: string expected, got nil')
618 call assert_fails('lua b:add(true)', '[string "vim chunk"]:1: string expected, got boolean')
619 call assert_fails('lua b:add({})', '[string "vim chunk"]:1: string expected, got table')
620 lua b = nil
Bram Moolenaare49b8e82020-07-01 13:52:55 +0200621
622 let b = 0z0102
623 lua lb = vim.eval('b')
624 let n = luaeval('lb[1]')
625 call assert_equal(2, n)
626 let n = luaeval('lb[6]')
627 call assert_equal(v:null, n)
628 call assert_fails('let x = luaeval("lb:xyz(3)")',
629 \ s:lua_53_or_later
630 \ ? "[string \"luaeval\"]:1: attempt to call a nil value (method 'xyz')"
631 \ : "[string \"luaeval\"]:1: attempt to call method 'xyz' (a nil value)")
632 let y = luaeval("lb[{}]")
633 call assert_equal(v:null, y)
634
635 lockvar b
636 call assert_fails('lua lb[1] = 2', '[string "vim chunk"]:1: blob is locked')
637 call assert_fails('lua lb:add("12")', '[string "vim chunk"]:1: blob is locked')
638
639 " Error cases
640 lua t = {}
641 call assert_fails('lua b = vim.blob(t)',
642 \ '[string "vim chunk"]:1: string expected, got table')
Bram Moolenaarb7828692019-03-23 13:57:02 +0100643endfunc
644
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100645func Test_lua_funcref()
Bram Moolenaarca06da92018-07-01 15:12:05 +0200646 function I(x)
647 return a:x
648 endfunction
649 let R = function('I')
650 lua i1 = vim.funcref"I"
651 lua i2 = vim.eval"R"
652 lua msg = "funcref|test|" .. (#i2(i1) == #i1(i2) and "OK" or "FAIL")
653 lua msg = vim.funcref"tr"(msg, "|", " ")
654 call assert_equal("funcref test OK", luaeval('msg'))
655
Bram Moolenaare49b8e82020-07-01 13:52:55 +0200656 " Error cases
657 call assert_fails('lua f1 = vim.funcref("")',
658 \ '[string "vim chunk"]:1: invalid function name: ')
659 call assert_fails('lua f1 = vim.funcref("10")',
660 \ '[string "vim chunk"]:1: invalid function name: 10')
661 let fname = test_null_string()
662 call assert_fails('lua f1 = vim.funcref(fname)',
663 \ "[string \"vim chunk\"]:1: bad argument #1 to 'funcref' (string expected, got nil)")
664 call assert_fails('lua vim.funcref("abc")()',
Bram Moolenaarecdd14a2020-07-11 22:49:59 +0200665 \ ['E117:', '\[string "vim chunk"]:1: cannot call funcref'])
Bram Moolenaare49b8e82020-07-01 13:52:55 +0200666
Bram Moolenaarca06da92018-07-01 15:12:05 +0200667 " dict funcref
668 function Mylen() dict
669 return len(self.data)
670 endfunction
671 let l = [0, 1, 2, 3]
672 let mydict = {'data': l}
673 lua d = vim.eval"mydict"
674 lua d.len = vim.funcref"Mylen" -- assign d as 'self'
675 lua res = (d.len() == vim.funcref"len"(vim.eval"l")) and "OK" or "FAIL"
676 call assert_equal("OK", luaeval('res'))
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100677 call assert_equal(function('Mylen', {'data': l, 'len': function('Mylen')}), mydict.len)
Bram Moolenaar2f362bf2018-07-01 19:49:27 +0200678
679 lua i1, i2, msg, d, res = nil
Bram Moolenaarca06da92018-07-01 15:12:05 +0200680endfunc
681
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200682" Test vim.type()
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100683func Test_lua_type()
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200684 " The following values are identical to Lua's type function.
685 call assert_equal('string', luaeval('vim.type("foo")'))
686 call assert_equal('number', luaeval('vim.type(1)'))
687 call assert_equal('number', luaeval('vim.type(1.2)'))
688 call assert_equal('function', luaeval('vim.type(print)'))
689 call assert_equal('table', luaeval('vim.type({})'))
690 call assert_equal('boolean', luaeval('vim.type(true)'))
691 call assert_equal('boolean', luaeval('vim.type(false)'))
692 call assert_equal('nil', luaeval('vim.type(nil)'))
693
694 " The following values are specific to Vim.
695 call assert_equal('window', luaeval('vim.type(vim.window())'))
696 call assert_equal('buffer', luaeval('vim.type(vim.buffer())'))
697 call assert_equal('list', luaeval('vim.type(vim.list())'))
698 call assert_equal('dict', luaeval('vim.type(vim.dict())'))
Bram Moolenaarca06da92018-07-01 15:12:05 +0200699 call assert_equal('funcref', luaeval('vim.type(vim.funcref("Test_type"))'))
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200700endfunc
701
702" Test vim.open()
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100703func Test_lua_open()
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200704 call assert_notmatch('XOpen', execute('ls'))
705
706 " Open a buffer XOpen1, but do not jump to it.
707 lua b = vim.open('XOpen1')
708 call assert_equal('XOpen1', luaeval('b.name'))
709 call assert_equal('', bufname('%'))
710
711 call assert_match('XOpen1', execute('ls'))
712 call assert_notequal('XOpen2', bufname('%'))
713
714 " Open a buffer XOpen2 and jump to it.
715 lua b = vim.open('XOpen2')()
716 call assert_equal('XOpen2', luaeval('b.name'))
717 call assert_equal('XOpen2', bufname('%'))
718
719 lua b = nil
720 %bwipe!
721endfunc
722
Bram Moolenaar788fbb42020-05-31 14:08:12 +0200723func Test_update_package_paths()
724 set runtimepath+=./testluaplugin
725 call assert_equal("hello from lua", luaeval("require('testluaplugin').hello()"))
726endfunc
727
Bram Moolenaar801ab062020-06-25 19:27:56 +0200728func Vim_func_call_lua_callback(Concat, Cb)
729 let l:message = a:Concat("hello", "vim")
730 call a:Cb(l:message)
731endfunc
732
733func Test_pass_lua_callback_to_vim_from_lua()
734 lua pass_lua_callback_to_vim_from_lua_result = ""
735 call assert_equal("", luaeval("pass_lua_callback_to_vim_from_lua_result"))
736 lua <<EOF
737 vim.funcref('Vim_func_call_lua_callback')(
738 function(greeting, message)
739 return greeting .. " " .. message
740 end,
741 function(message)
742 pass_lua_callback_to_vim_from_lua_result = message
743 end)
744EOF
745 call assert_equal("hello vim", luaeval("pass_lua_callback_to_vim_from_lua_result"))
746endfunc
747
748func Vim_func_call_metatable_lua_callback(Greet)
749 return a:Greet("world")
750endfunc
751
752func Test_pass_lua_metatable_callback_to_vim_from_lua()
753 let result = luaeval("vim.funcref('Vim_func_call_metatable_lua_callback')(setmetatable({ space = ' '}, { __call = function(tbl, msg) return 'hello' .. tbl.space .. msg end }) )")
754 call assert_equal("hello world", result)
755endfunc
756
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200757" Test vim.line()
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100758func Test_lua_line()
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200759 new
760 call setline(1, ['first line', 'second line'])
761 1
762 call assert_equal('first line', luaeval('vim.line()'))
763 2
764 call assert_equal('second line', luaeval('vim.line()'))
765 bwipe!
766endfunc
767
768" Test vim.beep()
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100769func Test_lua_beep()
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200770 call assert_beeps('lua vim.beep()')
771endfunc
772
773" Test errors in luaeval()
774func Test_luaeval_error()
775 " Compile error
776 call assert_fails("call luaeval('-nil')",
777 \ '[string "luaeval"]:1: attempt to perform arithmetic on a nil value')
778 call assert_fails("call luaeval(']')",
779 \ "[string \"luaeval\"]:1: unexpected symbol near ']'")
Bram Moolenaare49b8e82020-07-01 13:52:55 +0200780 lua co = coroutine.create(function () print("hi") end)
781 call assert_fails('let i = luaeval("co")', 'luaeval: cannot convert value')
782 lua co = nil
783 call assert_fails('let m = luaeval("{}")', 'luaeval: cannot convert value')
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200784endfunc
785
786" Test :luafile foo.lua
787func Test_luafile()
788 call delete('Xlua_file')
Bram Moolenaareb04f082020-05-17 14:32:35 +0200789 call writefile(["str = 'hello'", "num = 123" ], 'Xlua_file')
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200790 call setfperm('Xlua_file', 'r-xr-xr-x')
791
792 luafile Xlua_file
793 call assert_equal('hello', luaeval('str'))
Bram Moolenaareb04f082020-05-17 14:32:35 +0200794 call assert_equal(123, luaeval('num'))
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200795
Bram Moolenaar2f362bf2018-07-01 19:49:27 +0200796 lua str, num = nil
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200797 call delete('Xlua_file')
798endfunc
799
800" Test :luafile %
801func Test_luafile_percent()
802 new Xlua_file
803 append
804 str, num = 'foo', 321.0
805 print(string.format('str=%s, num=%d', str, num))
806.
807 w!
808 luafile %
809 let msg = split(execute('message'), "\n")[-1]
810 call assert_equal('str=foo, num=321', msg)
811
Bram Moolenaar2f362bf2018-07-01 19:49:27 +0200812 lua str, num = nil
813 call delete('Xlua_file')
814 bwipe!
815endfunc
816
817" Test :luafile with syntax error
818func Test_luafile_error()
819 new Xlua_file
820 call writefile(['nil = 0' ], 'Xlua_file')
821 call setfperm('Xlua_file', 'r-xr-xr-x')
822
823 call assert_fails('luafile Xlua_file', "Xlua_file:1: unexpected symbol near 'nil'")
824
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200825 call delete('Xlua_file')
Bram Moolenaard58f03b2017-01-29 22:48:45 +0100826 bwipe!
827endfunc
Bram Moolenaar53901442018-07-25 22:02:36 +0200828
Bram Moolenaare49b8e82020-07-01 13:52:55 +0200829" Test for dealing with strings containing newlines and null character
830func Test_lua_string_with_newline()
831 let x = execute('lua print("Hello\nWorld")')
832 call assert_equal("\nHello\nWorld", x)
833 new
834 lua k = vim.buffer(vim.eval('bufnr()'))
835 lua k:insert("Hello\0World", 0)
836 call assert_equal(["Hello\nWorld", ''], getline(1, '$'))
837 close!
838endfunc
839
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100840func Test_lua_set_cursor()
Bram Moolenaar53901442018-07-25 22:02:36 +0200841 " Check that setting the cursor position works.
842 new
843 call setline(1, ['first line', 'second line'])
844 normal gg
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200845 lua << trim EOF
846 w = vim.window()
847 w.line = 1
848 w.col = 5
849 EOF
Bram Moolenaar53901442018-07-25 22:02:36 +0200850 call assert_equal([1, 5], [line('.'), col('.')])
851
852 " Check that movement after setting cursor position keeps current column.
853 normal j
854 call assert_equal([2, 5], [line('.'), col('.')])
855endfunc
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200856
857" Test for various heredoc syntax
858func Test_lua_heredoc()
859 lua << END
860vim.command('let s = "A"')
861END
862 lua <<
863vim.command('let s ..= "B"')
864.
865 lua << trim END
866 vim.command('let s ..= "C"')
867 END
868 lua << trim
869 vim.command('let s ..= "D"')
870 .
Bram Moolenaar6ab09532020-05-01 14:10:13 +0200871 lua << trim eof
872 vim.command('let s ..= "E"')
873 eof
874 call assert_equal('ABCDE', s)
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200875endfunc
876
877" vim: shiftwidth=2 sts=2 expandtab