blob: d21c40de5bc67116ba27e9ab9007d0d4984332f1 [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
4CheckFeature lua
Bram Moolenaar5feabe02020-01-30 18:24:53 +01005CheckFeature float
Bram Moolenaard58f03b2017-01-29 22:48:45 +01006
Bram Moolenaare49b8e82020-07-01 13:52:55 +02007let s:luaver = split(split(luaeval('_VERSION'), ' ')[1], '\.')
8let s:major = str2nr(s:luaver[0])
9let s:minor = str2nr(s:luaver[1])
10if s:major < 5 || (s:major == 5 && s:minor < 3)
11 let s:lua_53_or_later = 0
12else
13 let s:lua_53_or_later = 1
14endif
15
Bram Moolenaare165f632019-03-10 09:48:59 +010016func TearDown()
17 " Run garbage collection after each test to exercise luaV_setref().
18 call test_garbagecollect_now()
19endfunc
20
Bram Moolenaar4ff48142018-06-30 21:50:25 +020021" Check that switching to another buffer does not trigger ml_get error.
Bram Moolenaar5feabe02020-01-30 18:24:53 +010022func Test_lua_command_new_no_ml_get_error()
Bram Moolenaard58f03b2017-01-29 22:48:45 +010023 new
24 let wincount = winnr('$')
25 call setline(1, ['one', 'two', 'three'])
26 luado vim.command("new")
27 call assert_equal(wincount + 1, winnr('$'))
Bram Moolenaar4ff48142018-06-30 21:50:25 +020028 %bwipe!
29endfunc
30
31" Test vim.command()
Bram Moolenaar5feabe02020-01-30 18:24:53 +010032func Test_lua_command()
Bram Moolenaar4ff48142018-06-30 21:50:25 +020033 new
34 call setline(1, ['one', 'two', 'three'])
35 luado vim.command("1,2d_")
36 call assert_equal(['three'], getline(1, '$'))
Bram Moolenaard58f03b2017-01-29 22:48:45 +010037 bwipe!
Bram Moolenaar4ff48142018-06-30 21:50:25 +020038endfunc
39
Bram Moolenaare49b8e82020-07-01 13:52:55 +020040func Test_lua_luado()
41 new
42 call setline(1, ['one', 'two'])
43 luado return(linenr)
44 call assert_equal(['1', '2'], getline(1, '$'))
45 close!
46
47 " Error cases
48 call assert_fails('luado string.format()',
49 \ "[string \"vim chunk\"]:1: bad argument #1 to 'format' (string expected, got no value)")
50 call assert_fails('luado func()',
51 \ s:lua_53_or_later
52 \ ? "[string \"vim chunk\"]:1: attempt to call a nil value (global 'func')"
53 \ : "[string \"vim chunk\"]:1: attempt to call global 'func' (a nil value)")
54 call assert_fails('luado error("failed")', "[string \"vim chunk\"]:1: failed")
55endfunc
56
Bram Moolenaar4ff48142018-06-30 21:50:25 +020057" Test vim.eval()
Bram Moolenaar5feabe02020-01-30 18:24:53 +010058func Test_lua_eval()
Bram Moolenaar4ff48142018-06-30 21:50:25 +020059 " lua.eval with a number
60 lua v = vim.eval('123')
61 call assert_equal('number', luaeval('vim.type(v)'))
Bram Moolenaareb04f082020-05-17 14:32:35 +020062 call assert_equal(123, luaeval('v'))
Bram Moolenaar4ff48142018-06-30 21:50:25 +020063
64 " lua.eval with a string
65 lua v = vim.eval('"abc"')
Bram Moolenaar02b31112019-08-31 22:16:38 +020066 call assert_equal('string', 'vim.type(v)'->luaeval())
Bram Moolenaar4ff48142018-06-30 21:50:25 +020067 call assert_equal('abc', luaeval('v'))
68
69 " lua.eval with a list
70 lua v = vim.eval("['a']")
71 call assert_equal('list', luaeval('vim.type(v)'))
72 call assert_equal(['a'], luaeval('v'))
73
74 " lua.eval with a dict
75 lua v = vim.eval("{'a':'b'}")
76 call assert_equal('dict', luaeval('vim.type(v)'))
77 call assert_equal({'a':'b'}, luaeval('v'))
78
Bram Moolenaarb7828692019-03-23 13:57:02 +010079 " lua.eval with a blob
80 lua v = vim.eval("0z00112233.deadbeef")
81 call assert_equal('blob', luaeval('vim.type(v)'))
82 call assert_equal(0z00112233.deadbeef, luaeval('v'))
83
Bram Moolenaare49b8e82020-07-01 13:52:55 +020084 " lua.eval with a float
85 lua v = vim.eval('3.14')
86 call assert_equal('number', luaeval('vim.type(v)'))
87 call assert_equal(3.14, luaeval('v'))
88
89 " lua.eval with a bool
90 lua v = vim.eval('v:true')
91 call assert_equal('number', luaeval('vim.type(v)'))
92 call assert_equal(1, luaeval('v'))
93 lua v = vim.eval('v:false')
94 call assert_equal('number', luaeval('vim.type(v)'))
95 call assert_equal(0, luaeval('v'))
96
97 " lua.eval with a null
98 lua v = vim.eval('v:null')
99 call assert_equal('nil', luaeval('vim.type(v)'))
100 call assert_equal(v:null, luaeval('v'))
101
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200102 call assert_fails('lua v = vim.eval(nil)',
103 \ "[string \"vim chunk\"]:1: bad argument #1 to 'eval' (string expected, got nil)")
104 call assert_fails('lua v = vim.eval(true)',
105 \ "[string \"vim chunk\"]:1: bad argument #1 to 'eval' (string expected, got boolean)")
106 call assert_fails('lua v = vim.eval({})',
107 \ "[string \"vim chunk\"]:1: bad argument #1 to 'eval' (string expected, got table)")
108 call assert_fails('lua v = vim.eval(print)',
109 \ "[string \"vim chunk\"]:1: bad argument #1 to 'eval' (string expected, got function)")
110 call assert_fails('lua v = vim.eval(vim.buffer())',
111 \ "[string \"vim chunk\"]:1: bad argument #1 to 'eval' (string expected, got userdata)")
112
113 lua v = nil
114endfunc
115
116" Test vim.window()
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100117func Test_lua_window()
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200118 e Xfoo2
119 new Xfoo1
120
121 " Window 1 (top window) contains Xfoo1
122 " Window 2 (bottom window) contains Xfoo2
123 call assert_equal('Xfoo1', luaeval('vim.window(1):buffer().name'))
124 call assert_equal('Xfoo2', luaeval('vim.window(2):buffer().name'))
125
126 " Window 3 does not exist so vim.window(3) should return nil
127 call assert_equal('nil', luaeval('tostring(vim.window(3))'))
128
Bram Moolenaare49b8e82020-07-01 13:52:55 +0200129 call assert_fails("let n = luaeval('vim.window().xyz()')",
130 \ s:lua_53_or_later
131 \ ? "[string \"luaeval\"]:1: attempt to call a nil value (field 'xyz')"
132 \ : "[string \"luaeval\"]:1: attempt to call field 'xyz' (a nil value)")
133 call assert_fails('lua vim.window().xyz = 1',
134 \ "[string \"vim chunk\"]:1: invalid window property: `xyz'")
135
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200136 %bwipe!
137endfunc
138
139" Test vim.window().height
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100140func Test_lua_window_height()
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200141 new
142 lua vim.window().height = 2
143 call assert_equal(2, winheight(0))
144 lua vim.window().height = vim.window().height + 1
145 call assert_equal(3, winheight(0))
146 bwipe!
147endfunc
148
149" Test vim.window().width
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100150func Test_lua_window_width()
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200151 vert new
152 lua vim.window().width = 2
153 call assert_equal(2, winwidth(0))
154 lua vim.window().width = vim.window().width + 1
155 call assert_equal(3, winwidth(0))
156 bwipe!
157endfunc
158
159" Test vim.window().line and vim.window.col
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100160func Test_lua_window_line_col()
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200161 new
162 call setline(1, ['line1', 'line2', 'line3'])
163 lua vim.window().line = 2
164 lua vim.window().col = 4
165 call assert_equal([0, 2, 4, 0], getpos('.'))
166 lua vim.window().line = vim.window().line + 1
167 lua vim.window().col = vim.window().col - 1
168 call assert_equal([0, 3, 3, 0], getpos('.'))
169
170 call assert_fails('lua vim.window().line = 10',
171 \ '[string "vim chunk"]:1: line out of range')
172 bwipe!
173endfunc
174
Bram Moolenaareb04f082020-05-17 14:32:35 +0200175" Test vim.call
176func Test_lua_call()
177 call assert_equal(has('lua'), luaeval('vim.call("has", "lua")'))
178 call assert_equal(printf("Hello %s", "vim"), luaeval('vim.call("printf", "Hello %s", "vim")'))
Bram Moolenaare49b8e82020-07-01 13:52:55 +0200179
180 " Error cases
181 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 +0200182 \ s:lua_53_or_later
183 \ ? '[string "luaeval"]:1: Function called with too many arguments'
184 \ : 'Function called with too many arguments')
Bram Moolenaare49b8e82020-07-01 13:52:55 +0200185 lua co = coroutine.create(function () print("hi") end)
186 call assert_fails("call luaeval('vim.call(\"type\", co)')",
Bram Moolenaarb898a022020-07-12 18:33:53 +0200187 \ s:lua_53_or_later
188 \ ? '[string "luaeval"]:1: lua: cannot convert value'
189 \ : 'lua: cannot convert value')
Bram Moolenaare49b8e82020-07-01 13:52:55 +0200190 lua co = nil
Bram Moolenaarb898a022020-07-12 18:33:53 +0200191 call assert_fails("call luaeval('vim.call(\"abc\")')",
192 \ ['E117:', s:lua_53_or_later ? '\[string "luaeval"]:1: lua: call_vim_function failed'
193 \ : 'lua: call_vim_function failed'])
Bram Moolenaareb04f082020-05-17 14:32:35 +0200194endfunc
195
196" Test vim.fn.*
197func Test_lua_fn()
198 call assert_equal(has('lua'), luaeval('vim.fn.has("lua")'))
199 call assert_equal(printf("Hello %s", "vim"), luaeval('vim.fn.printf("Hello %s", "vim")'))
200endfunc
201
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200202" Test setting the current window
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100203func Test_lua_window_set_current()
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200204 new Xfoo1
205 lua w1 = vim.window()
206 new Xfoo2
207 lua w2 = vim.window()
208
209 call assert_equal('Xfoo2', bufname('%'))
210 lua w1()
211 call assert_equal('Xfoo1', bufname('%'))
212 lua w2()
213 call assert_equal('Xfoo2', bufname('%'))
214
Bram Moolenaar2f362bf2018-07-01 19:49:27 +0200215 lua w1, w2 = nil
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200216 %bwipe!
217endfunc
218
219" Test vim.window().buffer
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100220func Test_lua_window_buffer()
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200221 new Xfoo1
222 lua w1 = vim.window()
223 lua b1 = w1.buffer()
224 new Xfoo2
225 lua w2 = vim.window()
226 lua b2 = w2.buffer()
227
228 lua b1()
229 call assert_equal('Xfoo1', bufname('%'))
230 lua b2()
231 call assert_equal('Xfoo2', bufname('%'))
232
Bram Moolenaar2f362bf2018-07-01 19:49:27 +0200233 lua b1, b2, w1, w2 = nil
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200234 %bwipe!
235endfunc
236
237" Test vim.window():previous() and vim.window():next()
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100238func Test_lua_window_next_previous()
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200239 new Xfoo1
240 new Xfoo2
241 new Xfoo3
242 wincmd j
243
244 call assert_equal('Xfoo2', luaeval('vim.window().buffer().name'))
245 call assert_equal('Xfoo1', luaeval('vim.window():next():buffer().name'))
246 call assert_equal('Xfoo3', luaeval('vim.window():previous():buffer().name'))
247
248 %bwipe!
249endfunc
250
251" Test vim.window():isvalid()
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100252func Test_lua_window_isvalid()
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200253 new Xfoo
254 lua w = vim.window()
255 call assert_true(luaeval('w:isvalid()'))
256
257 " FIXME: how to test the case when isvalid() returns v:false?
258 " isvalid() gives errors when the window is deleted. Is it a bug?
259
260 lua w = nil
261 bwipe!
262endfunc
263
264" Test vim.buffer() with and without argument
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100265func Test_lua_buffer()
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200266 new Xfoo1
267 let bn1 = bufnr('%')
268 new Xfoo2
269 let bn2 = bufnr('%')
270
271 " Test vim.buffer() without argument.
272 call assert_equal('Xfoo2', luaeval("vim.buffer().name"))
273
274 " Test vim.buffer() with string argument.
275 call assert_equal('Xfoo1', luaeval("vim.buffer('Xfoo1').name"))
276 call assert_equal('Xfoo2', luaeval("vim.buffer('Xfoo2').name"))
277
278 " Test vim.buffer() with integer argument.
279 call assert_equal('Xfoo1', luaeval("vim.buffer(" . bn1 . ").name"))
280 call assert_equal('Xfoo2', luaeval("vim.buffer(" . bn2 . ").name"))
281
Bram Moolenaar2f362bf2018-07-01 19:49:27 +0200282 lua bn1, bn2 = nil
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200283 %bwipe!
284endfunc
285
286" Test vim.buffer().name and vim.buffer().fname
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100287func Test_lua_buffer_name()
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200288 new
Bram Moolenaarfe08df42018-07-07 23:07:41 +0200289 call assert_equal('', luaeval('vim.buffer().name'))
290 call assert_equal('', luaeval('vim.buffer().fname'))
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200291 bwipe!
292
293 new Xfoo
294 call assert_equal('Xfoo', luaeval('vim.buffer().name'))
295 call assert_equal(expand('%:p'), luaeval('vim.buffer().fname'))
296 bwipe!
297endfunc
298
299" Test vim.buffer().number
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100300func Test_lua_buffer_number()
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200301 " All numbers in Lua are floating points number (no integers).
302 call assert_equal(bufnr('%'), float2nr(luaeval('vim.buffer().number')))
303endfunc
304
305" Test inserting lines in buffer.
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100306func Test_lua_buffer_insert()
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200307 new
308 lua vim.buffer()[1] = '3'
309 lua vim.buffer():insert('1', 0)
310 lua vim.buffer():insert('2', 1)
311 lua vim.buffer():insert('4', 10)
312
313 call assert_equal(['1', '2', '3', '4'], getline(1, '$'))
Bram Moolenaare49b8e82020-07-01 13:52:55 +0200314 call assert_equal('4', luaeval('vim.buffer()[4]'))
315 call assert_equal(v:null, luaeval('vim.buffer()[5]'))
316 call assert_equal(v:null, luaeval('vim.buffer()[{}]'))
317 call assert_fails('lua vim.buffer():xyz()',
318 \ s:lua_53_or_later
319 \ ? "[string \"vim chunk\"]:1: attempt to call a nil value (method 'xyz')"
320 \ : "[string \"vim chunk\"]:1: attempt to call method 'xyz' (a nil value)")
321 call assert_fails('lua vim.buffer()[1] = {}',
322 \ '[string "vim chunk"]:1: wrong argument to change')
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200323 bwipe!
324endfunc
325
326" Test deleting line in buffer
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100327func Test_lua_buffer_delete()
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200328 new
329 call setline(1, ['1', '2', '3'])
Bram Moolenaare49b8e82020-07-01 13:52:55 +0200330 call cursor(3, 1)
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200331 lua vim.buffer()[2] = nil
332 call assert_equal(['1', '3'], getline(1, '$'))
333
334 call assert_fails('lua vim.buffer()[3] = nil',
335 \ '[string "vim chunk"]:1: invalid line number')
336 bwipe!
337endfunc
338
339" Test #vim.buffer() i.e. number of lines in buffer
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100340func Test_lua_buffer_number_lines()
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200341 new
342 call setline(1, ['a', 'b', 'c'])
Bram Moolenaareb04f082020-05-17 14:32:35 +0200343 call assert_equal(3, luaeval('#vim.buffer()'))
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200344 bwipe!
345endfunc
346
347" Test vim.buffer():next() and vim.buffer():previous()
348" Note that these functions get the next or previous buffers
349" but do not switch buffer.
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100350func Test_lua_buffer_next_previous()
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200351 new Xfoo1
352 new Xfoo2
353 new Xfoo3
354 b Xfoo2
355
356 lua bn = vim.buffer():next()
357 lua bp = vim.buffer():previous()
358
359 call assert_equal('Xfoo2', luaeval('vim.buffer().name'))
360 call assert_equal('Xfoo1', luaeval('bp.name'))
361 call assert_equal('Xfoo3', luaeval('bn.name'))
362
363 call assert_equal('Xfoo2', bufname('%'))
364
365 lua bn()
366 call assert_equal('Xfoo3', luaeval('vim.buffer().name'))
367 call assert_equal('Xfoo3', bufname('%'))
368
369 lua bp()
370 call assert_equal('Xfoo1', luaeval('vim.buffer().name'))
371 call assert_equal('Xfoo1', bufname('%'))
372
Bram Moolenaar2f362bf2018-07-01 19:49:27 +0200373 lua bn, bp = nil
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200374 %bwipe!
375endfunc
376
377" Test vim.buffer():isvalid()
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100378func Test_lua_buffer_isvalid()
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200379 new Xfoo
380 lua b = vim.buffer()
381 call assert_true(luaeval('b:isvalid()'))
382
383 " FIXME: how to test the case when isvalid() returns v:false?
384 " isvalid() gives errors when the buffer is wiped. Is it a bug?
385
386 lua b = nil
387 bwipe!
388endfunc
389
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100390func Test_lua_list()
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200391 call assert_equal([], luaeval('vim.list()'))
392
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200393 let l = []
394 lua l = vim.eval('l')
395 lua l:add(123)
396 lua l:add('abc')
397 lua l:add(true)
398 lua l:add(false)
Bram Moolenaar9067cd62019-01-01 00:41:54 +0100399 lua l:add(nil)
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200400 lua l:add(vim.eval("[1, 2, 3]"))
401 lua l:add(vim.eval("{'a':1, 'b':2, 'c':3}"))
Bram Moolenaareb04f082020-05-17 14:32:35 +0200402 call assert_equal([123, 'abc', v:true, v:false, v:null, [1, 2, 3], {'a': 1, 'b': 2, 'c': 3}], l)
403 call assert_equal(7, luaeval('#l'))
Bram Moolenaara8a60d02018-07-02 22:54:36 +0200404 call assert_match('^list: \%(0x\)\?\x\+$', luaeval('tostring(l)'))
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200405
Bram Moolenaarbd846172020-06-27 12:32:57 +0200406 lua l[1] = 124
407 lua l[6] = nil
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200408 lua l:insert('first')
409 lua l:insert('xx', 3)
Bram Moolenaare49b8e82020-07-01 13:52:55 +0200410 call assert_fails('lua l:insert("xx", -20)',
411 \ '[string "vim chunk"]:1: invalid position')
Bram Moolenaareb04f082020-05-17 14:32:35 +0200412 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 +0200413
Bram Moolenaar2f362bf2018-07-01 19:49:27 +0200414 lockvar 1 l
415 call assert_fails('lua l:add("x")', '[string "vim chunk"]:1: list is locked')
Bram Moolenaare49b8e82020-07-01 13:52:55 +0200416 call assert_fails('lua l:insert(2)', '[string "vim chunk"]:1: list is locked')
417 call assert_fails('lua l[9] = 1', '[string "vim chunk"]:1: list is locked')
418
419 unlockvar l
420 let l = [1, 2]
421 lua ll = vim.eval('l')
422 let x = luaeval("ll[3]")
423 call assert_equal(v:null, x)
424 call assert_fails('let x = luaeval("ll:xyz(3)")',
425 \ s:lua_53_or_later
426 \ ? "[string \"luaeval\"]:1: attempt to call a nil value (method 'xyz')"
427 \ : "[string \"luaeval\"]:1: attempt to call method 'xyz' (a nil value)")
428 let y = luaeval("ll[{}]")
429 call assert_equal(v:null, y)
Bram Moolenaar2f362bf2018-07-01 19:49:27 +0200430
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200431 lua l = nil
432endfunc
433
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100434func Test_lua_list_table()
Bram Moolenaar2f362bf2018-07-01 19:49:27 +0200435 " See :help lua-vim
436 " Non-numeric keys should not be used to initialize the list
437 " so say = 'hi' should be ignored.
438 lua t = {3.14, 'hello', false, true, say = 'hi'}
439 call assert_equal([3.14, 'hello', v:false, v:true], luaeval('vim.list(t)'))
440 lua t = nil
441
442 call assert_fails('lua vim.list(1)', '[string "vim chunk"]:1: table expected, got number')
443 call assert_fails('lua vim.list("x")', '[string "vim chunk"]:1: table expected, got string')
444 call assert_fails('lua vim.list(print)', '[string "vim chunk"]:1: table expected, got function')
445 call assert_fails('lua vim.list(true)', '[string "vim chunk"]:1: table expected, got boolean')
446endfunc
447
Bram Moolenaara1f9f862020-06-28 22:41:26 +0200448func Test_lua_list_table_insert_remove()
Bram Moolenaare49b8e82020-07-01 13:52:55 +0200449 if !s:lua_53_or_later
Bram Moolenaara1f9f862020-06-28 22:41:26 +0200450 throw 'Skipped: Lua version < 5.3'
451 endif
452
453 let l = [1, 2]
454 lua t = vim.eval('l')
455 lua table.insert(t, 10)
456 lua t[#t + 1] = 20
457 lua table.insert(t, 2, 30)
458 call assert_equal(l, [1, 30, 2, 10, 20])
459 lua table.remove(t, 2)
460 call assert_equal(l, [1, 2, 10, 20])
461 lua t[3] = nil
462 call assert_equal(l, [1, 2, 20])
463 lua removed_value = table.remove(t, 3)
464 call assert_equal(luaeval('removed_value'), 20)
465 lua t = nil
466 lua removed_value = nil
467 unlet l
468endfunc
469
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200470" Test l() i.e. iterator on list
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100471func Test_lua_list_iter()
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200472 lua l = vim.list():add('foo'):add('bar')
473 lua str = ''
474 lua for v in l() do str = str .. v end
475 call assert_equal('foobar', luaeval('str'))
476
Bram Moolenaar2f362bf2018-07-01 19:49:27 +0200477 lua str, l = nil
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200478endfunc
479
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100480func Test_lua_recursive_list()
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200481 lua l = vim.list():add(1):add(2)
482 lua l = l:add(l)
483
Bram Moolenaarbd846172020-06-27 12:32:57 +0200484 call assert_equal(1, luaeval('l[1]'))
485 call assert_equal(2, luaeval('l[2]'))
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200486
Bram Moolenaarbd846172020-06-27 12:32:57 +0200487 call assert_equal(1, luaeval('l[3][1]'))
488 call assert_equal(2, luaeval('l[3][2]'))
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200489
Bram Moolenaarbd846172020-06-27 12:32:57 +0200490 call assert_equal(1, luaeval('l[3][3][1]'))
491 call assert_equal(2, luaeval('l[3][3][2]'))
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200492
Bram Moolenaareb04f082020-05-17 14:32:35 +0200493 call assert_equal('[1, 2, [...]]', string(luaeval('l')))
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200494
Bram Moolenaara8a60d02018-07-02 22:54:36 +0200495 call assert_match('^list: \%(0x\)\?\x\+$', luaeval('tostring(l)'))
Bram Moolenaarbd846172020-06-27 12:32:57 +0200496 call assert_equal(luaeval('tostring(l)'), luaeval('tostring(l[3])'))
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200497
Bram Moolenaarbd846172020-06-27 12:32:57 +0200498 call assert_equal(luaeval('l'), luaeval('l[3]'))
499 call assert_equal(luaeval('l'), luaeval('l[3][3]'))
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200500
501 lua l = nil
502endfunc
503
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100504func Test_lua_dict()
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200505 call assert_equal({}, luaeval('vim.dict()'))
506
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200507 let d = {}
508 lua d = vim.eval('d')
509 lua d[0] = 123
510 lua d[1] = "abc"
511 lua d[2] = true
512 lua d[3] = false
513 lua d[4] = vim.eval("[1, 2, 3]")
514 lua d[5] = vim.eval("{'a':1, 'b':2, 'c':3}")
Bram Moolenaareb04f082020-05-17 14:32:35 +0200515 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)
516 call assert_equal(6, luaeval('#d'))
Bram Moolenaara8a60d02018-07-02 22:54:36 +0200517 call assert_match('^dict: \%(0x\)\?\x\+$', luaeval('tostring(d)'))
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200518
519 call assert_equal('abc', luaeval('d[1]'))
Bram Moolenaare49b8e82020-07-01 13:52:55 +0200520 call assert_equal(v:null, luaeval('d[22]'))
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200521
522 lua d[0] = 124
523 lua d[4] = nil
Bram Moolenaareb04f082020-05-17 14:32:35 +0200524 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 +0200525
Bram Moolenaar2f362bf2018-07-01 19:49:27 +0200526 lockvar 1 d
527 call assert_fails('lua d[6] = 1', '[string "vim chunk"]:1: dict is locked')
Bram Moolenaare49b8e82020-07-01 13:52:55 +0200528 unlockvar d
529
530 " Error case
531 lua d = {}
532 lua d[''] = 10
533 call assert_fails("let t = luaeval('vim.dict(d)')",
Bram Moolenaarb898a022020-07-12 18:33:53 +0200534 \ s:lua_53_or_later
535 \ ? '[string "luaeval"]:1: table has empty key'
536 \ : 'table has empty key')
Bram Moolenaare49b8e82020-07-01 13:52:55 +0200537 let d = {}
538 lua x = vim.eval('d')
539 call assert_fails("lua x[''] = 10", '[string "vim chunk"]:1: empty key')
540 lua x['a'] = nil
541 call assert_equal({}, d)
542
543 " cannot assign funcrefs in the global scope
544 lua x = vim.eval('g:')
545 call assert_fails("lua x['min'] = vim.funcref('max')",
546 \ '[string "vim chunk"]:1: cannot assign funcref to builtin scope')
Bram Moolenaar2f362bf2018-07-01 19:49:27 +0200547
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200548 lua d = nil
549endfunc
550
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100551func Test_lua_dict_table()
Bram Moolenaar2f362bf2018-07-01 19:49:27 +0200552 lua t = {key1 = 'x', key2 = 3.14, key3 = true, key4 = false}
553 call assert_equal({'key1': 'x', 'key2': 3.14, 'key3': v:true, 'key4': v:false},
554 \ luaeval('vim.dict(t)'))
555
556 " Same example as in :help lua-vim.
557 lua t = {math.pi, false, say = 'hi'}
558 " FIXME: commented out as it currently does not work as documented:
559 " Expected {'say': 'hi'}
560 " but got {'1': 3.141593, '2': v:false, 'say': 'hi'}
561 " Is the documentation or the code wrong?
562 "call assert_equal({'say' : 'hi'}, luaeval('vim.dict(t)'))
563 lua t = nil
564
565 call assert_fails('lua vim.dict(1)', '[string "vim chunk"]:1: table expected, got number')
566 call assert_fails('lua vim.dict("x")', '[string "vim chunk"]:1: table expected, got string')
567 call assert_fails('lua vim.dict(print)', '[string "vim chunk"]:1: table expected, got function')
568 call assert_fails('lua vim.dict(true)', '[string "vim chunk"]:1: table expected, got boolean')
569endfunc
570
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200571" Test d() i.e. iterator on dictionary
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100572func Test_lua_dict_iter()
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200573 let d = {'a': 1, 'b':2}
574 lua d = vim.eval('d')
575 lua str = ''
576 lua for k,v in d() do str = str .. k ..':' .. v .. ',' end
577 call assert_equal('a:1,b:2,', luaeval('str'))
578
Bram Moolenaar2f362bf2018-07-01 19:49:27 +0200579 lua str, d = nil
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200580endfunc
581
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100582func Test_lua_blob()
Bram Moolenaarb7828692019-03-23 13:57:02 +0100583 call assert_equal(0z, luaeval('vim.blob("")'))
584 call assert_equal(0z31326162, luaeval('vim.blob("12ab")'))
585 call assert_equal(0z00010203, luaeval('vim.blob("\x00\x01\x02\x03")'))
586 call assert_equal(0z8081FEFF, luaeval('vim.blob("\x80\x81\xfe\xff")'))
587
588 lua b = vim.blob("\x00\x00\x00\x00")
589 call assert_equal(0z00000000, luaeval('b'))
Bram Moolenaareb04f082020-05-17 14:32:35 +0200590 call assert_equal(4, luaeval('#b'))
Bram Moolenaarb7828692019-03-23 13:57:02 +0100591 lua b[0], b[1], b[2], b[3] = 1, 32, 256, 0xff
592 call assert_equal(0z012000ff, luaeval('b'))
593 lua b[4] = string.byte("z", 1)
594 call assert_equal(0z012000ff.7a, luaeval('b'))
Bram Moolenaareb04f082020-05-17 14:32:35 +0200595 call assert_equal(5, luaeval('#b'))
Bram Moolenaarb7828692019-03-23 13:57:02 +0100596 call assert_fails('lua b[#b+1] = 0x80', '[string "vim chunk"]:1: index out of range')
597 lua b:add("12ab")
598 call assert_equal(0z012000ff.7a313261.62, luaeval('b'))
Bram Moolenaareb04f082020-05-17 14:32:35 +0200599 call assert_equal(9, luaeval('#b'))
Bram Moolenaarb7828692019-03-23 13:57:02 +0100600 call assert_fails('lua b:add(nil)', '[string "vim chunk"]:1: string expected, got nil')
601 call assert_fails('lua b:add(true)', '[string "vim chunk"]:1: string expected, got boolean')
602 call assert_fails('lua b:add({})', '[string "vim chunk"]:1: string expected, got table')
603 lua b = nil
Bram Moolenaare49b8e82020-07-01 13:52:55 +0200604
605 let b = 0z0102
606 lua lb = vim.eval('b')
607 let n = luaeval('lb[1]')
608 call assert_equal(2, n)
609 let n = luaeval('lb[6]')
610 call assert_equal(v:null, n)
611 call assert_fails('let x = luaeval("lb:xyz(3)")',
612 \ s:lua_53_or_later
613 \ ? "[string \"luaeval\"]:1: attempt to call a nil value (method 'xyz')"
614 \ : "[string \"luaeval\"]:1: attempt to call method 'xyz' (a nil value)")
615 let y = luaeval("lb[{}]")
616 call assert_equal(v:null, y)
617
618 lockvar b
619 call assert_fails('lua lb[1] = 2', '[string "vim chunk"]:1: blob is locked')
620 call assert_fails('lua lb:add("12")', '[string "vim chunk"]:1: blob is locked')
621
622 " Error cases
623 lua t = {}
624 call assert_fails('lua b = vim.blob(t)',
625 \ '[string "vim chunk"]:1: string expected, got table')
Bram Moolenaarb7828692019-03-23 13:57:02 +0100626endfunc
627
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100628func Test_lua_funcref()
Bram Moolenaarca06da92018-07-01 15:12:05 +0200629 function I(x)
630 return a:x
631 endfunction
632 let R = function('I')
633 lua i1 = vim.funcref"I"
634 lua i2 = vim.eval"R"
635 lua msg = "funcref|test|" .. (#i2(i1) == #i1(i2) and "OK" or "FAIL")
636 lua msg = vim.funcref"tr"(msg, "|", " ")
637 call assert_equal("funcref test OK", luaeval('msg'))
638
Bram Moolenaare49b8e82020-07-01 13:52:55 +0200639 " Error cases
640 call assert_fails('lua f1 = vim.funcref("")',
641 \ '[string "vim chunk"]:1: invalid function name: ')
642 call assert_fails('lua f1 = vim.funcref("10")',
643 \ '[string "vim chunk"]:1: invalid function name: 10')
644 let fname = test_null_string()
645 call assert_fails('lua f1 = vim.funcref(fname)',
646 \ "[string \"vim chunk\"]:1: bad argument #1 to 'funcref' (string expected, got nil)")
647 call assert_fails('lua vim.funcref("abc")()',
Bram Moolenaarecdd14a2020-07-11 22:49:59 +0200648 \ ['E117:', '\[string "vim chunk"]:1: cannot call funcref'])
Bram Moolenaare49b8e82020-07-01 13:52:55 +0200649
Bram Moolenaarca06da92018-07-01 15:12:05 +0200650 " dict funcref
651 function Mylen() dict
652 return len(self.data)
653 endfunction
654 let l = [0, 1, 2, 3]
655 let mydict = {'data': l}
656 lua d = vim.eval"mydict"
657 lua d.len = vim.funcref"Mylen" -- assign d as 'self'
658 lua res = (d.len() == vim.funcref"len"(vim.eval"l")) and "OK" or "FAIL"
659 call assert_equal("OK", luaeval('res'))
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100660 call assert_equal(function('Mylen', {'data': l, 'len': function('Mylen')}), mydict.len)
Bram Moolenaar2f362bf2018-07-01 19:49:27 +0200661
662 lua i1, i2, msg, d, res = nil
Bram Moolenaarca06da92018-07-01 15:12:05 +0200663endfunc
664
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200665" Test vim.type()
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100666func Test_lua_type()
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200667 " The following values are identical to Lua's type function.
668 call assert_equal('string', luaeval('vim.type("foo")'))
669 call assert_equal('number', luaeval('vim.type(1)'))
670 call assert_equal('number', luaeval('vim.type(1.2)'))
671 call assert_equal('function', luaeval('vim.type(print)'))
672 call assert_equal('table', luaeval('vim.type({})'))
673 call assert_equal('boolean', luaeval('vim.type(true)'))
674 call assert_equal('boolean', luaeval('vim.type(false)'))
675 call assert_equal('nil', luaeval('vim.type(nil)'))
676
677 " The following values are specific to Vim.
678 call assert_equal('window', luaeval('vim.type(vim.window())'))
679 call assert_equal('buffer', luaeval('vim.type(vim.buffer())'))
680 call assert_equal('list', luaeval('vim.type(vim.list())'))
681 call assert_equal('dict', luaeval('vim.type(vim.dict())'))
Bram Moolenaarca06da92018-07-01 15:12:05 +0200682 call assert_equal('funcref', luaeval('vim.type(vim.funcref("Test_type"))'))
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200683endfunc
684
685" Test vim.open()
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100686func Test_lua_open()
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200687 call assert_notmatch('XOpen', execute('ls'))
688
689 " Open a buffer XOpen1, but do not jump to it.
690 lua b = vim.open('XOpen1')
691 call assert_equal('XOpen1', luaeval('b.name'))
692 call assert_equal('', bufname('%'))
693
694 call assert_match('XOpen1', execute('ls'))
695 call assert_notequal('XOpen2', bufname('%'))
696
697 " Open a buffer XOpen2 and jump to it.
698 lua b = vim.open('XOpen2')()
699 call assert_equal('XOpen2', luaeval('b.name'))
700 call assert_equal('XOpen2', bufname('%'))
701
702 lua b = nil
703 %bwipe!
704endfunc
705
Bram Moolenaar788fbb42020-05-31 14:08:12 +0200706func Test_update_package_paths()
707 set runtimepath+=./testluaplugin
708 call assert_equal("hello from lua", luaeval("require('testluaplugin').hello()"))
709endfunc
710
Bram Moolenaar801ab062020-06-25 19:27:56 +0200711func Vim_func_call_lua_callback(Concat, Cb)
712 let l:message = a:Concat("hello", "vim")
713 call a:Cb(l:message)
714endfunc
715
716func Test_pass_lua_callback_to_vim_from_lua()
717 lua pass_lua_callback_to_vim_from_lua_result = ""
718 call assert_equal("", luaeval("pass_lua_callback_to_vim_from_lua_result"))
719 lua <<EOF
720 vim.funcref('Vim_func_call_lua_callback')(
721 function(greeting, message)
722 return greeting .. " " .. message
723 end,
724 function(message)
725 pass_lua_callback_to_vim_from_lua_result = message
726 end)
727EOF
728 call assert_equal("hello vim", luaeval("pass_lua_callback_to_vim_from_lua_result"))
729endfunc
730
731func Vim_func_call_metatable_lua_callback(Greet)
732 return a:Greet("world")
733endfunc
734
735func Test_pass_lua_metatable_callback_to_vim_from_lua()
736 let result = luaeval("vim.funcref('Vim_func_call_metatable_lua_callback')(setmetatable({ space = ' '}, { __call = function(tbl, msg) return 'hello' .. tbl.space .. msg end }) )")
737 call assert_equal("hello world", result)
738endfunc
739
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200740" Test vim.line()
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100741func Test_lua_line()
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200742 new
743 call setline(1, ['first line', 'second line'])
744 1
745 call assert_equal('first line', luaeval('vim.line()'))
746 2
747 call assert_equal('second line', luaeval('vim.line()'))
748 bwipe!
749endfunc
750
751" Test vim.beep()
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100752func Test_lua_beep()
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200753 call assert_beeps('lua vim.beep()')
754endfunc
755
756" Test errors in luaeval()
757func Test_luaeval_error()
758 " Compile error
759 call assert_fails("call luaeval('-nil')",
760 \ '[string "luaeval"]:1: attempt to perform arithmetic on a nil value')
761 call assert_fails("call luaeval(']')",
762 \ "[string \"luaeval\"]:1: unexpected symbol near ']'")
Bram Moolenaare49b8e82020-07-01 13:52:55 +0200763 lua co = coroutine.create(function () print("hi") end)
764 call assert_fails('let i = luaeval("co")', 'luaeval: cannot convert value')
765 lua co = nil
766 call assert_fails('let m = luaeval("{}")', 'luaeval: cannot convert value')
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200767endfunc
768
769" Test :luafile foo.lua
770func Test_luafile()
771 call delete('Xlua_file')
Bram Moolenaareb04f082020-05-17 14:32:35 +0200772 call writefile(["str = 'hello'", "num = 123" ], 'Xlua_file')
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200773 call setfperm('Xlua_file', 'r-xr-xr-x')
774
775 luafile Xlua_file
776 call assert_equal('hello', luaeval('str'))
Bram Moolenaareb04f082020-05-17 14:32:35 +0200777 call assert_equal(123, luaeval('num'))
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200778
Bram Moolenaar2f362bf2018-07-01 19:49:27 +0200779 lua str, num = nil
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200780 call delete('Xlua_file')
781endfunc
782
783" Test :luafile %
784func Test_luafile_percent()
785 new Xlua_file
786 append
787 str, num = 'foo', 321.0
788 print(string.format('str=%s, num=%d', str, num))
789.
790 w!
791 luafile %
792 let msg = split(execute('message'), "\n")[-1]
793 call assert_equal('str=foo, num=321', msg)
794
Bram Moolenaar2f362bf2018-07-01 19:49:27 +0200795 lua str, num = nil
796 call delete('Xlua_file')
797 bwipe!
798endfunc
799
800" Test :luafile with syntax error
801func Test_luafile_error()
802 new Xlua_file
803 call writefile(['nil = 0' ], 'Xlua_file')
804 call setfperm('Xlua_file', 'r-xr-xr-x')
805
806 call assert_fails('luafile Xlua_file', "Xlua_file:1: unexpected symbol near 'nil'")
807
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200808 call delete('Xlua_file')
Bram Moolenaard58f03b2017-01-29 22:48:45 +0100809 bwipe!
810endfunc
Bram Moolenaar53901442018-07-25 22:02:36 +0200811
Bram Moolenaare49b8e82020-07-01 13:52:55 +0200812" Test for dealing with strings containing newlines and null character
813func Test_lua_string_with_newline()
814 let x = execute('lua print("Hello\nWorld")')
815 call assert_equal("\nHello\nWorld", x)
816 new
817 lua k = vim.buffer(vim.eval('bufnr()'))
818 lua k:insert("Hello\0World", 0)
819 call assert_equal(["Hello\nWorld", ''], getline(1, '$'))
820 close!
821endfunc
822
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100823func Test_lua_set_cursor()
Bram Moolenaar53901442018-07-25 22:02:36 +0200824 " Check that setting the cursor position works.
825 new
826 call setline(1, ['first line', 'second line'])
827 normal gg
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200828 lua << trim EOF
829 w = vim.window()
830 w.line = 1
831 w.col = 5
832 EOF
Bram Moolenaar53901442018-07-25 22:02:36 +0200833 call assert_equal([1, 5], [line('.'), col('.')])
834
835 " Check that movement after setting cursor position keeps current column.
836 normal j
837 call assert_equal([2, 5], [line('.'), col('.')])
838endfunc
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200839
840" Test for various heredoc syntax
841func Test_lua_heredoc()
842 lua << END
843vim.command('let s = "A"')
844END
845 lua <<
846vim.command('let s ..= "B"')
847.
848 lua << trim END
849 vim.command('let s ..= "C"')
850 END
851 lua << trim
852 vim.command('let s ..= "D"')
853 .
Bram Moolenaar6ab09532020-05-01 14:10:13 +0200854 lua << trim eof
855 vim.command('let s ..= "E"')
856 eof
857 call assert_equal('ABCDE', s)
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200858endfunc
859
860" vim: shiftwidth=2 sts=2 expandtab