blob: 43b4aa30ff39b1dad788410609034183b9463cb8 [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
124" Test vim.window()
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100125func Test_lua_window()
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200126 e Xfoo2
127 new Xfoo1
128
129 " Window 1 (top window) contains Xfoo1
130 " Window 2 (bottom window) contains Xfoo2
131 call assert_equal('Xfoo1', luaeval('vim.window(1):buffer().name'))
132 call assert_equal('Xfoo2', luaeval('vim.window(2):buffer().name'))
133
134 " Window 3 does not exist so vim.window(3) should return nil
135 call assert_equal('nil', luaeval('tostring(vim.window(3))'))
136
Bram Moolenaare49b8e82020-07-01 13:52:55 +0200137 call assert_fails("let n = luaeval('vim.window().xyz()')",
138 \ s:lua_53_or_later
139 \ ? "[string \"luaeval\"]:1: attempt to call a nil value (field 'xyz')"
140 \ : "[string \"luaeval\"]:1: attempt to call field 'xyz' (a nil value)")
141 call assert_fails('lua vim.window().xyz = 1',
142 \ "[string \"vim chunk\"]:1: invalid window property: `xyz'")
143
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200144 %bwipe!
145endfunc
146
147" Test vim.window().height
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100148func Test_lua_window_height()
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200149 new
150 lua vim.window().height = 2
151 call assert_equal(2, winheight(0))
152 lua vim.window().height = vim.window().height + 1
153 call assert_equal(3, winheight(0))
154 bwipe!
155endfunc
156
157" Test vim.window().width
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100158func Test_lua_window_width()
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200159 vert new
160 lua vim.window().width = 2
161 call assert_equal(2, winwidth(0))
162 lua vim.window().width = vim.window().width + 1
163 call assert_equal(3, winwidth(0))
164 bwipe!
165endfunc
166
167" Test vim.window().line and vim.window.col
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100168func Test_lua_window_line_col()
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200169 new
170 call setline(1, ['line1', 'line2', 'line3'])
171 lua vim.window().line = 2
172 lua vim.window().col = 4
173 call assert_equal([0, 2, 4, 0], getpos('.'))
174 lua vim.window().line = vim.window().line + 1
175 lua vim.window().col = vim.window().col - 1
176 call assert_equal([0, 3, 3, 0], getpos('.'))
177
178 call assert_fails('lua vim.window().line = 10',
179 \ '[string "vim chunk"]:1: line out of range')
180 bwipe!
181endfunc
182
Bram Moolenaareb04f082020-05-17 14:32:35 +0200183" Test vim.call
184func Test_lua_call()
185 call assert_equal(has('lua'), luaeval('vim.call("has", "lua")'))
186 call assert_equal(printf("Hello %s", "vim"), luaeval('vim.call("printf", "Hello %s", "vim")'))
Bram Moolenaare49b8e82020-07-01 13:52:55 +0200187
188 " Error cases
189 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 +0200190 \ s:lua_53_or_later
191 \ ? '[string "luaeval"]:1: Function called with too many arguments'
192 \ : 'Function called with too many arguments')
Bram Moolenaare49b8e82020-07-01 13:52:55 +0200193 lua co = coroutine.create(function () print("hi") end)
194 call assert_fails("call luaeval('vim.call(\"type\", co)')",
Bram Moolenaarb898a022020-07-12 18:33:53 +0200195 \ s:lua_53_or_later
196 \ ? '[string "luaeval"]:1: lua: cannot convert value'
197 \ : 'lua: cannot convert value')
Bram Moolenaare49b8e82020-07-01 13:52:55 +0200198 lua co = nil
Bram Moolenaarb898a022020-07-12 18:33:53 +0200199 call assert_fails("call luaeval('vim.call(\"abc\")')",
200 \ ['E117:', s:lua_53_or_later ? '\[string "luaeval"]:1: lua: call_vim_function failed'
201 \ : 'lua: call_vim_function failed'])
Bram Moolenaareb04f082020-05-17 14:32:35 +0200202endfunc
203
204" Test vim.fn.*
205func Test_lua_fn()
206 call assert_equal(has('lua'), luaeval('vim.fn.has("lua")'))
207 call assert_equal(printf("Hello %s", "vim"), luaeval('vim.fn.printf("Hello %s", "vim")'))
208endfunc
209
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200210" Test setting the current window
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100211func Test_lua_window_set_current()
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200212 new Xfoo1
213 lua w1 = vim.window()
214 new Xfoo2
215 lua w2 = vim.window()
216
217 call assert_equal('Xfoo2', bufname('%'))
218 lua w1()
219 call assert_equal('Xfoo1', bufname('%'))
220 lua w2()
221 call assert_equal('Xfoo2', bufname('%'))
222
Bram Moolenaar2f362bf2018-07-01 19:49:27 +0200223 lua w1, w2 = nil
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200224 %bwipe!
225endfunc
226
227" Test vim.window().buffer
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100228func Test_lua_window_buffer()
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200229 new Xfoo1
230 lua w1 = vim.window()
231 lua b1 = w1.buffer()
232 new Xfoo2
233 lua w2 = vim.window()
234 lua b2 = w2.buffer()
235
236 lua b1()
237 call assert_equal('Xfoo1', bufname('%'))
238 lua b2()
239 call assert_equal('Xfoo2', bufname('%'))
240
Bram Moolenaar2f362bf2018-07-01 19:49:27 +0200241 lua b1, b2, w1, w2 = nil
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200242 %bwipe!
243endfunc
244
245" Test vim.window():previous() and vim.window():next()
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100246func Test_lua_window_next_previous()
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200247 new Xfoo1
248 new Xfoo2
249 new Xfoo3
250 wincmd j
251
252 call assert_equal('Xfoo2', luaeval('vim.window().buffer().name'))
253 call assert_equal('Xfoo1', luaeval('vim.window():next():buffer().name'))
254 call assert_equal('Xfoo3', luaeval('vim.window():previous():buffer().name'))
255
256 %bwipe!
257endfunc
258
259" Test vim.window():isvalid()
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100260func Test_lua_window_isvalid()
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200261 new Xfoo
262 lua w = vim.window()
263 call assert_true(luaeval('w:isvalid()'))
264
265 " FIXME: how to test the case when isvalid() returns v:false?
266 " isvalid() gives errors when the window is deleted. Is it a bug?
267
268 lua w = nil
269 bwipe!
270endfunc
271
272" Test vim.buffer() with and without argument
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100273func Test_lua_buffer()
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200274 new Xfoo1
275 let bn1 = bufnr('%')
276 new Xfoo2
277 let bn2 = bufnr('%')
278
279 " Test vim.buffer() without argument.
280 call assert_equal('Xfoo2', luaeval("vim.buffer().name"))
281
282 " Test vim.buffer() with string argument.
283 call assert_equal('Xfoo1', luaeval("vim.buffer('Xfoo1').name"))
284 call assert_equal('Xfoo2', luaeval("vim.buffer('Xfoo2').name"))
285
286 " Test vim.buffer() with integer argument.
287 call assert_equal('Xfoo1', luaeval("vim.buffer(" . bn1 . ").name"))
288 call assert_equal('Xfoo2', luaeval("vim.buffer(" . bn2 . ").name"))
289
Bram Moolenaar2f362bf2018-07-01 19:49:27 +0200290 lua bn1, bn2 = nil
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200291 %bwipe!
292endfunc
293
294" Test vim.buffer().name and vim.buffer().fname
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100295func Test_lua_buffer_name()
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200296 new
Bram Moolenaarfe08df42018-07-07 23:07:41 +0200297 call assert_equal('', luaeval('vim.buffer().name'))
298 call assert_equal('', luaeval('vim.buffer().fname'))
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200299 bwipe!
300
301 new Xfoo
302 call assert_equal('Xfoo', luaeval('vim.buffer().name'))
303 call assert_equal(expand('%:p'), luaeval('vim.buffer().fname'))
304 bwipe!
305endfunc
306
307" Test vim.buffer().number
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100308func Test_lua_buffer_number()
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200309 " All numbers in Lua are floating points number (no integers).
310 call assert_equal(bufnr('%'), float2nr(luaeval('vim.buffer().number')))
311endfunc
312
313" Test inserting lines in buffer.
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100314func Test_lua_buffer_insert()
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200315 new
316 lua vim.buffer()[1] = '3'
317 lua vim.buffer():insert('1', 0)
318 lua vim.buffer():insert('2', 1)
319 lua vim.buffer():insert('4', 10)
320
321 call assert_equal(['1', '2', '3', '4'], getline(1, '$'))
Bram Moolenaare49b8e82020-07-01 13:52:55 +0200322 call assert_equal('4', luaeval('vim.buffer()[4]'))
323 call assert_equal(v:null, luaeval('vim.buffer()[5]'))
324 call assert_equal(v:null, luaeval('vim.buffer()[{}]'))
325 call assert_fails('lua vim.buffer():xyz()',
326 \ s:lua_53_or_later
327 \ ? "[string \"vim chunk\"]:1: attempt to call a nil value (method 'xyz')"
328 \ : "[string \"vim chunk\"]:1: attempt to call method 'xyz' (a nil value)")
329 call assert_fails('lua vim.buffer()[1] = {}',
330 \ '[string "vim chunk"]:1: wrong argument to change')
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200331 bwipe!
332endfunc
333
334" Test deleting line in buffer
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100335func Test_lua_buffer_delete()
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200336 new
337 call setline(1, ['1', '2', '3'])
Bram Moolenaare49b8e82020-07-01 13:52:55 +0200338 call cursor(3, 1)
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200339 lua vim.buffer()[2] = nil
340 call assert_equal(['1', '3'], getline(1, '$'))
341
342 call assert_fails('lua vim.buffer()[3] = nil',
343 \ '[string "vim chunk"]:1: invalid line number')
344 bwipe!
345endfunc
346
347" Test #vim.buffer() i.e. number of lines in buffer
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100348func Test_lua_buffer_number_lines()
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200349 new
350 call setline(1, ['a', 'b', 'c'])
Bram Moolenaareb04f082020-05-17 14:32:35 +0200351 call assert_equal(3, luaeval('#vim.buffer()'))
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200352 bwipe!
353endfunc
354
355" Test vim.buffer():next() and vim.buffer():previous()
356" Note that these functions get the next or previous buffers
357" but do not switch buffer.
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100358func Test_lua_buffer_next_previous()
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200359 new Xfoo1
360 new Xfoo2
361 new Xfoo3
362 b Xfoo2
363
364 lua bn = vim.buffer():next()
365 lua bp = vim.buffer():previous()
366
367 call assert_equal('Xfoo2', luaeval('vim.buffer().name'))
368 call assert_equal('Xfoo1', luaeval('bp.name'))
369 call assert_equal('Xfoo3', luaeval('bn.name'))
370
371 call assert_equal('Xfoo2', bufname('%'))
372
373 lua bn()
374 call assert_equal('Xfoo3', luaeval('vim.buffer().name'))
375 call assert_equal('Xfoo3', bufname('%'))
376
377 lua bp()
378 call assert_equal('Xfoo1', luaeval('vim.buffer().name'))
379 call assert_equal('Xfoo1', bufname('%'))
380
Bram Moolenaar2f362bf2018-07-01 19:49:27 +0200381 lua bn, bp = nil
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200382 %bwipe!
383endfunc
384
385" Test vim.buffer():isvalid()
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100386func Test_lua_buffer_isvalid()
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200387 new Xfoo
388 lua b = vim.buffer()
389 call assert_true(luaeval('b:isvalid()'))
390
391 " FIXME: how to test the case when isvalid() returns v:false?
392 " isvalid() gives errors when the buffer is wiped. Is it a bug?
393
394 lua b = nil
395 bwipe!
396endfunc
397
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100398func Test_lua_list()
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200399 call assert_equal([], luaeval('vim.list()'))
400
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200401 let l = []
402 lua l = vim.eval('l')
403 lua l:add(123)
404 lua l:add('abc')
405 lua l:add(true)
406 lua l:add(false)
Bram Moolenaar9067cd62019-01-01 00:41:54 +0100407 lua l:add(nil)
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200408 lua l:add(vim.eval("[1, 2, 3]"))
409 lua l:add(vim.eval("{'a':1, 'b':2, 'c':3}"))
Bram Moolenaareb04f082020-05-17 14:32:35 +0200410 call assert_equal([123, 'abc', v:true, v:false, v:null, [1, 2, 3], {'a': 1, 'b': 2, 'c': 3}], l)
411 call assert_equal(7, luaeval('#l'))
Bram Moolenaara8a60d02018-07-02 22:54:36 +0200412 call assert_match('^list: \%(0x\)\?\x\+$', luaeval('tostring(l)'))
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200413
Bram Moolenaarbd846172020-06-27 12:32:57 +0200414 lua l[1] = 124
415 lua l[6] = nil
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200416 lua l:insert('first')
417 lua l:insert('xx', 3)
Bram Moolenaare49b8e82020-07-01 13:52:55 +0200418 call assert_fails('lua l:insert("xx", -20)',
419 \ '[string "vim chunk"]:1: invalid position')
Bram Moolenaareb04f082020-05-17 14:32:35 +0200420 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 +0200421
Bram Moolenaar2f362bf2018-07-01 19:49:27 +0200422 lockvar 1 l
423 call assert_fails('lua l:add("x")', '[string "vim chunk"]:1: list is locked')
Bram Moolenaare49b8e82020-07-01 13:52:55 +0200424 call assert_fails('lua l:insert(2)', '[string "vim chunk"]:1: list is locked')
425 call assert_fails('lua l[9] = 1', '[string "vim chunk"]:1: list is locked')
426
427 unlockvar l
428 let l = [1, 2]
429 lua ll = vim.eval('l')
430 let x = luaeval("ll[3]")
431 call assert_equal(v:null, x)
432 call assert_fails('let x = luaeval("ll:xyz(3)")',
433 \ s:lua_53_or_later
434 \ ? "[string \"luaeval\"]:1: attempt to call a nil value (method 'xyz')"
435 \ : "[string \"luaeval\"]:1: attempt to call method 'xyz' (a nil value)")
436 let y = luaeval("ll[{}]")
437 call assert_equal(v:null, y)
Bram Moolenaar2f362bf2018-07-01 19:49:27 +0200438
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200439 lua l = nil
440endfunc
441
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100442func Test_lua_list_table()
Bram Moolenaar2f362bf2018-07-01 19:49:27 +0200443 " See :help lua-vim
444 " Non-numeric keys should not be used to initialize the list
445 " so say = 'hi' should be ignored.
446 lua t = {3.14, 'hello', false, true, say = 'hi'}
447 call assert_equal([3.14, 'hello', v:false, v:true], luaeval('vim.list(t)'))
448 lua t = nil
449
450 call assert_fails('lua vim.list(1)', '[string "vim chunk"]:1: table expected, got number')
451 call assert_fails('lua vim.list("x")', '[string "vim chunk"]:1: table expected, got string')
452 call assert_fails('lua vim.list(print)', '[string "vim chunk"]:1: table expected, got function')
453 call assert_fails('lua vim.list(true)', '[string "vim chunk"]:1: table expected, got boolean')
454endfunc
455
Bram Moolenaara1f9f862020-06-28 22:41:26 +0200456func Test_lua_list_table_insert_remove()
Bram Moolenaare49b8e82020-07-01 13:52:55 +0200457 if !s:lua_53_or_later
Bram Moolenaara1f9f862020-06-28 22:41:26 +0200458 throw 'Skipped: Lua version < 5.3'
459 endif
460
461 let l = [1, 2]
462 lua t = vim.eval('l')
463 lua table.insert(t, 10)
464 lua t[#t + 1] = 20
465 lua table.insert(t, 2, 30)
466 call assert_equal(l, [1, 30, 2, 10, 20])
467 lua table.remove(t, 2)
468 call assert_equal(l, [1, 2, 10, 20])
469 lua t[3] = nil
470 call assert_equal(l, [1, 2, 20])
471 lua removed_value = table.remove(t, 3)
472 call assert_equal(luaeval('removed_value'), 20)
473 lua t = nil
474 lua removed_value = nil
475 unlet l
476endfunc
477
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200478" Test l() i.e. iterator on list
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100479func Test_lua_list_iter()
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200480 lua l = vim.list():add('foo'):add('bar')
481 lua str = ''
482 lua for v in l() do str = str .. v end
483 call assert_equal('foobar', luaeval('str'))
484
Bram Moolenaar2f362bf2018-07-01 19:49:27 +0200485 lua str, l = nil
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200486endfunc
487
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100488func Test_lua_recursive_list()
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200489 lua l = vim.list():add(1):add(2)
490 lua l = l:add(l)
491
Bram Moolenaarbd846172020-06-27 12:32:57 +0200492 call assert_equal(1, luaeval('l[1]'))
493 call assert_equal(2, luaeval('l[2]'))
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200494
Bram Moolenaarbd846172020-06-27 12:32:57 +0200495 call assert_equal(1, luaeval('l[3][1]'))
496 call assert_equal(2, luaeval('l[3][2]'))
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200497
Bram Moolenaarbd846172020-06-27 12:32:57 +0200498 call assert_equal(1, luaeval('l[3][3][1]'))
499 call assert_equal(2, luaeval('l[3][3][2]'))
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200500
Bram Moolenaareb04f082020-05-17 14:32:35 +0200501 call assert_equal('[1, 2, [...]]', string(luaeval('l')))
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200502
Bram Moolenaara8a60d02018-07-02 22:54:36 +0200503 call assert_match('^list: \%(0x\)\?\x\+$', luaeval('tostring(l)'))
Bram Moolenaarbd846172020-06-27 12:32:57 +0200504 call assert_equal(luaeval('tostring(l)'), luaeval('tostring(l[3])'))
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200505
Bram Moolenaarbd846172020-06-27 12:32:57 +0200506 call assert_equal(luaeval('l'), luaeval('l[3]'))
507 call assert_equal(luaeval('l'), luaeval('l[3][3]'))
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200508
509 lua l = nil
510endfunc
511
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100512func Test_lua_dict()
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200513 call assert_equal({}, luaeval('vim.dict()'))
514
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200515 let d = {}
516 lua d = vim.eval('d')
517 lua d[0] = 123
518 lua d[1] = "abc"
519 lua d[2] = true
520 lua d[3] = false
521 lua d[4] = vim.eval("[1, 2, 3]")
522 lua d[5] = vim.eval("{'a':1, 'b':2, 'c':3}")
Bram Moolenaareb04f082020-05-17 14:32:35 +0200523 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)
524 call assert_equal(6, luaeval('#d'))
Bram Moolenaara8a60d02018-07-02 22:54:36 +0200525 call assert_match('^dict: \%(0x\)\?\x\+$', luaeval('tostring(d)'))
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200526
527 call assert_equal('abc', luaeval('d[1]'))
Bram Moolenaare49b8e82020-07-01 13:52:55 +0200528 call assert_equal(v:null, luaeval('d[22]'))
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200529
530 lua d[0] = 124
531 lua d[4] = nil
Bram Moolenaareb04f082020-05-17 14:32:35 +0200532 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 +0200533
Bram Moolenaar2f362bf2018-07-01 19:49:27 +0200534 lockvar 1 d
535 call assert_fails('lua d[6] = 1', '[string "vim chunk"]:1: dict is locked')
Bram Moolenaare49b8e82020-07-01 13:52:55 +0200536 unlockvar d
537
538 " Error case
539 lua d = {}
540 lua d[''] = 10
541 call assert_fails("let t = luaeval('vim.dict(d)')",
Bram Moolenaarb898a022020-07-12 18:33:53 +0200542 \ s:lua_53_or_later
543 \ ? '[string "luaeval"]:1: table has empty key'
544 \ : 'table has empty key')
Bram Moolenaare49b8e82020-07-01 13:52:55 +0200545 let d = {}
546 lua x = vim.eval('d')
547 call assert_fails("lua x[''] = 10", '[string "vim chunk"]:1: empty key')
548 lua x['a'] = nil
549 call assert_equal({}, d)
550
551 " cannot assign funcrefs in the global scope
552 lua x = vim.eval('g:')
553 call assert_fails("lua x['min'] = vim.funcref('max')",
554 \ '[string "vim chunk"]:1: cannot assign funcref to builtin scope')
Bram Moolenaar2f362bf2018-07-01 19:49:27 +0200555
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200556 lua d = nil
557endfunc
558
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100559func Test_lua_dict_table()
Bram Moolenaar2f362bf2018-07-01 19:49:27 +0200560 lua t = {key1 = 'x', key2 = 3.14, key3 = true, key4 = false}
561 call assert_equal({'key1': 'x', 'key2': 3.14, 'key3': v:true, 'key4': v:false},
562 \ luaeval('vim.dict(t)'))
563
564 " Same example as in :help lua-vim.
565 lua t = {math.pi, false, say = 'hi'}
566 " FIXME: commented out as it currently does not work as documented:
567 " Expected {'say': 'hi'}
568 " but got {'1': 3.141593, '2': v:false, 'say': 'hi'}
569 " Is the documentation or the code wrong?
570 "call assert_equal({'say' : 'hi'}, luaeval('vim.dict(t)'))
571 lua t = nil
572
573 call assert_fails('lua vim.dict(1)', '[string "vim chunk"]:1: table expected, got number')
574 call assert_fails('lua vim.dict("x")', '[string "vim chunk"]:1: table expected, got string')
575 call assert_fails('lua vim.dict(print)', '[string "vim chunk"]:1: table expected, got function')
576 call assert_fails('lua vim.dict(true)', '[string "vim chunk"]:1: table expected, got boolean')
577endfunc
578
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200579" Test d() i.e. iterator on dictionary
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100580func Test_lua_dict_iter()
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200581 let d = {'a': 1, 'b':2}
582 lua d = vim.eval('d')
583 lua str = ''
584 lua for k,v in d() do str = str .. k ..':' .. v .. ',' end
585 call assert_equal('a:1,b:2,', luaeval('str'))
586
Bram Moolenaar2f362bf2018-07-01 19:49:27 +0200587 lua str, d = nil
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200588endfunc
589
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100590func Test_lua_blob()
Bram Moolenaarb7828692019-03-23 13:57:02 +0100591 call assert_equal(0z, luaeval('vim.blob("")'))
592 call assert_equal(0z31326162, luaeval('vim.blob("12ab")'))
593 call assert_equal(0z00010203, luaeval('vim.blob("\x00\x01\x02\x03")'))
594 call assert_equal(0z8081FEFF, luaeval('vim.blob("\x80\x81\xfe\xff")'))
595
596 lua b = vim.blob("\x00\x00\x00\x00")
597 call assert_equal(0z00000000, luaeval('b'))
Bram Moolenaareb04f082020-05-17 14:32:35 +0200598 call assert_equal(4, luaeval('#b'))
Bram Moolenaarb7828692019-03-23 13:57:02 +0100599 lua b[0], b[1], b[2], b[3] = 1, 32, 256, 0xff
600 call assert_equal(0z012000ff, luaeval('b'))
601 lua b[4] = string.byte("z", 1)
602 call assert_equal(0z012000ff.7a, luaeval('b'))
Bram Moolenaareb04f082020-05-17 14:32:35 +0200603 call assert_equal(5, luaeval('#b'))
Bram Moolenaarb7828692019-03-23 13:57:02 +0100604 call assert_fails('lua b[#b+1] = 0x80', '[string "vim chunk"]:1: index out of range')
605 lua b:add("12ab")
606 call assert_equal(0z012000ff.7a313261.62, luaeval('b'))
Bram Moolenaareb04f082020-05-17 14:32:35 +0200607 call assert_equal(9, luaeval('#b'))
Bram Moolenaarb7828692019-03-23 13:57:02 +0100608 call assert_fails('lua b:add(nil)', '[string "vim chunk"]:1: string expected, got nil')
609 call assert_fails('lua b:add(true)', '[string "vim chunk"]:1: string expected, got boolean')
610 call assert_fails('lua b:add({})', '[string "vim chunk"]:1: string expected, got table')
611 lua b = nil
Bram Moolenaare49b8e82020-07-01 13:52:55 +0200612
613 let b = 0z0102
614 lua lb = vim.eval('b')
615 let n = luaeval('lb[1]')
616 call assert_equal(2, n)
617 let n = luaeval('lb[6]')
618 call assert_equal(v:null, n)
619 call assert_fails('let x = luaeval("lb:xyz(3)")',
620 \ s:lua_53_or_later
621 \ ? "[string \"luaeval\"]:1: attempt to call a nil value (method 'xyz')"
622 \ : "[string \"luaeval\"]:1: attempt to call method 'xyz' (a nil value)")
623 let y = luaeval("lb[{}]")
624 call assert_equal(v:null, y)
625
626 lockvar b
627 call assert_fails('lua lb[1] = 2', '[string "vim chunk"]:1: blob is locked')
628 call assert_fails('lua lb:add("12")', '[string "vim chunk"]:1: blob is locked')
629
630 " Error cases
631 lua t = {}
632 call assert_fails('lua b = vim.blob(t)',
633 \ '[string "vim chunk"]:1: string expected, got table')
Bram Moolenaarb7828692019-03-23 13:57:02 +0100634endfunc
635
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100636func Test_lua_funcref()
Bram Moolenaarca06da92018-07-01 15:12:05 +0200637 function I(x)
638 return a:x
639 endfunction
640 let R = function('I')
641 lua i1 = vim.funcref"I"
642 lua i2 = vim.eval"R"
643 lua msg = "funcref|test|" .. (#i2(i1) == #i1(i2) and "OK" or "FAIL")
644 lua msg = vim.funcref"tr"(msg, "|", " ")
645 call assert_equal("funcref test OK", luaeval('msg'))
646
Bram Moolenaare49b8e82020-07-01 13:52:55 +0200647 " Error cases
648 call assert_fails('lua f1 = vim.funcref("")',
649 \ '[string "vim chunk"]:1: invalid function name: ')
650 call assert_fails('lua f1 = vim.funcref("10")',
651 \ '[string "vim chunk"]:1: invalid function name: 10')
652 let fname = test_null_string()
653 call assert_fails('lua f1 = vim.funcref(fname)',
654 \ "[string \"vim chunk\"]:1: bad argument #1 to 'funcref' (string expected, got nil)")
655 call assert_fails('lua vim.funcref("abc")()',
Bram Moolenaarecdd14a2020-07-11 22:49:59 +0200656 \ ['E117:', '\[string "vim chunk"]:1: cannot call funcref'])
Bram Moolenaare49b8e82020-07-01 13:52:55 +0200657
Bram Moolenaarca06da92018-07-01 15:12:05 +0200658 " dict funcref
659 function Mylen() dict
660 return len(self.data)
661 endfunction
662 let l = [0, 1, 2, 3]
663 let mydict = {'data': l}
664 lua d = vim.eval"mydict"
665 lua d.len = vim.funcref"Mylen" -- assign d as 'self'
666 lua res = (d.len() == vim.funcref"len"(vim.eval"l")) and "OK" or "FAIL"
667 call assert_equal("OK", luaeval('res'))
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100668 call assert_equal(function('Mylen', {'data': l, 'len': function('Mylen')}), mydict.len)
Bram Moolenaar2f362bf2018-07-01 19:49:27 +0200669
670 lua i1, i2, msg, d, res = nil
Bram Moolenaarca06da92018-07-01 15:12:05 +0200671endfunc
672
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200673" Test vim.type()
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100674func Test_lua_type()
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200675 " The following values are identical to Lua's type function.
676 call assert_equal('string', luaeval('vim.type("foo")'))
677 call assert_equal('number', luaeval('vim.type(1)'))
678 call assert_equal('number', luaeval('vim.type(1.2)'))
679 call assert_equal('function', luaeval('vim.type(print)'))
680 call assert_equal('table', luaeval('vim.type({})'))
681 call assert_equal('boolean', luaeval('vim.type(true)'))
682 call assert_equal('boolean', luaeval('vim.type(false)'))
683 call assert_equal('nil', luaeval('vim.type(nil)'))
684
685 " The following values are specific to Vim.
686 call assert_equal('window', luaeval('vim.type(vim.window())'))
687 call assert_equal('buffer', luaeval('vim.type(vim.buffer())'))
688 call assert_equal('list', luaeval('vim.type(vim.list())'))
689 call assert_equal('dict', luaeval('vim.type(vim.dict())'))
Bram Moolenaarca06da92018-07-01 15:12:05 +0200690 call assert_equal('funcref', luaeval('vim.type(vim.funcref("Test_type"))'))
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200691endfunc
692
693" Test vim.open()
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100694func Test_lua_open()
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200695 call assert_notmatch('XOpen', execute('ls'))
696
697 " Open a buffer XOpen1, but do not jump to it.
698 lua b = vim.open('XOpen1')
699 call assert_equal('XOpen1', luaeval('b.name'))
700 call assert_equal('', bufname('%'))
701
702 call assert_match('XOpen1', execute('ls'))
703 call assert_notequal('XOpen2', bufname('%'))
704
705 " Open a buffer XOpen2 and jump to it.
706 lua b = vim.open('XOpen2')()
707 call assert_equal('XOpen2', luaeval('b.name'))
708 call assert_equal('XOpen2', bufname('%'))
709
710 lua b = nil
711 %bwipe!
712endfunc
713
Bram Moolenaar788fbb42020-05-31 14:08:12 +0200714func Test_update_package_paths()
715 set runtimepath+=./testluaplugin
716 call assert_equal("hello from lua", luaeval("require('testluaplugin').hello()"))
717endfunc
718
Bram Moolenaar801ab062020-06-25 19:27:56 +0200719func Vim_func_call_lua_callback(Concat, Cb)
720 let l:message = a:Concat("hello", "vim")
721 call a:Cb(l:message)
722endfunc
723
724func Test_pass_lua_callback_to_vim_from_lua()
725 lua pass_lua_callback_to_vim_from_lua_result = ""
726 call assert_equal("", luaeval("pass_lua_callback_to_vim_from_lua_result"))
727 lua <<EOF
728 vim.funcref('Vim_func_call_lua_callback')(
729 function(greeting, message)
730 return greeting .. " " .. message
731 end,
732 function(message)
733 pass_lua_callback_to_vim_from_lua_result = message
734 end)
735EOF
736 call assert_equal("hello vim", luaeval("pass_lua_callback_to_vim_from_lua_result"))
737endfunc
738
739func Vim_func_call_metatable_lua_callback(Greet)
740 return a:Greet("world")
741endfunc
742
743func Test_pass_lua_metatable_callback_to_vim_from_lua()
744 let result = luaeval("vim.funcref('Vim_func_call_metatable_lua_callback')(setmetatable({ space = ' '}, { __call = function(tbl, msg) return 'hello' .. tbl.space .. msg end }) )")
745 call assert_equal("hello world", result)
746endfunc
747
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200748" Test vim.line()
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100749func Test_lua_line()
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200750 new
751 call setline(1, ['first line', 'second line'])
752 1
753 call assert_equal('first line', luaeval('vim.line()'))
754 2
755 call assert_equal('second line', luaeval('vim.line()'))
756 bwipe!
757endfunc
758
759" Test vim.beep()
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100760func Test_lua_beep()
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200761 call assert_beeps('lua vim.beep()')
762endfunc
763
764" Test errors in luaeval()
765func Test_luaeval_error()
766 " Compile error
767 call assert_fails("call luaeval('-nil')",
768 \ '[string "luaeval"]:1: attempt to perform arithmetic on a nil value')
769 call assert_fails("call luaeval(']')",
770 \ "[string \"luaeval\"]:1: unexpected symbol near ']'")
Bram Moolenaare49b8e82020-07-01 13:52:55 +0200771 lua co = coroutine.create(function () print("hi") end)
772 call assert_fails('let i = luaeval("co")', 'luaeval: cannot convert value')
773 lua co = nil
774 call assert_fails('let m = luaeval("{}")', 'luaeval: cannot convert value')
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200775endfunc
776
777" Test :luafile foo.lua
778func Test_luafile()
779 call delete('Xlua_file')
Bram Moolenaareb04f082020-05-17 14:32:35 +0200780 call writefile(["str = 'hello'", "num = 123" ], 'Xlua_file')
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200781 call setfperm('Xlua_file', 'r-xr-xr-x')
782
783 luafile Xlua_file
784 call assert_equal('hello', luaeval('str'))
Bram Moolenaareb04f082020-05-17 14:32:35 +0200785 call assert_equal(123, luaeval('num'))
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200786
Bram Moolenaar2f362bf2018-07-01 19:49:27 +0200787 lua str, num = nil
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200788 call delete('Xlua_file')
789endfunc
790
791" Test :luafile %
792func Test_luafile_percent()
793 new Xlua_file
794 append
795 str, num = 'foo', 321.0
796 print(string.format('str=%s, num=%d', str, num))
797.
798 w!
799 luafile %
800 let msg = split(execute('message'), "\n")[-1]
801 call assert_equal('str=foo, num=321', msg)
802
Bram Moolenaar2f362bf2018-07-01 19:49:27 +0200803 lua str, num = nil
804 call delete('Xlua_file')
805 bwipe!
806endfunc
807
808" Test :luafile with syntax error
809func Test_luafile_error()
810 new Xlua_file
811 call writefile(['nil = 0' ], 'Xlua_file')
812 call setfperm('Xlua_file', 'r-xr-xr-x')
813
814 call assert_fails('luafile Xlua_file', "Xlua_file:1: unexpected symbol near 'nil'")
815
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200816 call delete('Xlua_file')
Bram Moolenaard58f03b2017-01-29 22:48:45 +0100817 bwipe!
818endfunc
Bram Moolenaar53901442018-07-25 22:02:36 +0200819
Bram Moolenaare49b8e82020-07-01 13:52:55 +0200820" Test for dealing with strings containing newlines and null character
821func Test_lua_string_with_newline()
822 let x = execute('lua print("Hello\nWorld")')
823 call assert_equal("\nHello\nWorld", x)
824 new
825 lua k = vim.buffer(vim.eval('bufnr()'))
826 lua k:insert("Hello\0World", 0)
827 call assert_equal(["Hello\nWorld", ''], getline(1, '$'))
828 close!
829endfunc
830
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100831func Test_lua_set_cursor()
Bram Moolenaar53901442018-07-25 22:02:36 +0200832 " Check that setting the cursor position works.
833 new
834 call setline(1, ['first line', 'second line'])
835 normal gg
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200836 lua << trim EOF
837 w = vim.window()
838 w.line = 1
839 w.col = 5
840 EOF
Bram Moolenaar53901442018-07-25 22:02:36 +0200841 call assert_equal([1, 5], [line('.'), col('.')])
842
843 " Check that movement after setting cursor position keeps current column.
844 normal j
845 call assert_equal([2, 5], [line('.'), col('.')])
846endfunc
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200847
848" Test for various heredoc syntax
849func Test_lua_heredoc()
850 lua << END
851vim.command('let s = "A"')
852END
853 lua <<
854vim.command('let s ..= "B"')
855.
856 lua << trim END
857 vim.command('let s ..= "C"')
858 END
859 lua << trim
860 vim.command('let s ..= "D"')
861 .
Bram Moolenaar6ab09532020-05-01 14:10:13 +0200862 lua << trim eof
863 vim.command('let s ..= "E"')
864 eof
865 call assert_equal('ABCDE', s)
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200866endfunc
867
868" vim: shiftwidth=2 sts=2 expandtab