blob: dd493fe79ab8bf4b388e5b3c546fde1717329e64 [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 Moolenaarf65ed862021-04-03 14:13:33 +020015" Depending on the lua version, the error messages are different.
Bram Moolenaar125ed272021-04-07 20:11:12 +020016let [s:major, s:minor, s:patch] = luaeval('vim.lua_version')->split('\.')->map({-> str2nr(v:val)})
Bram Moolenaarf65ed862021-04-03 14:13:33 +020017let s:lua_53_or_later = 0
18let s:lua_543_or_later = 0
19if (s:major == 5 && s:minor >= 3) || s:major > 5
Bram Moolenaare49b8e82020-07-01 13:52:55 +020020 let s:lua_53_or_later = 1
Bram Moolenaarf65ed862021-04-03 14:13:33 +020021 if (s:major == 5
22 \ && ((s:minor == 4 && s:patch >= 3) || s:minor > 4))
23 \ || s:major > 5
24 let s:lua_543_or_later = 1
25 endif
Bram Moolenaare49b8e82020-07-01 13:52:55 +020026endif
27
Bram Moolenaare165f632019-03-10 09:48:59 +010028func TearDown()
29 " Run garbage collection after each test to exercise luaV_setref().
30 call test_garbagecollect_now()
31endfunc
32
Bram Moolenaar4ff48142018-06-30 21:50:25 +020033" Check that switching to another buffer does not trigger ml_get error.
Bram Moolenaar5feabe02020-01-30 18:24:53 +010034func Test_lua_command_new_no_ml_get_error()
Bram Moolenaard58f03b2017-01-29 22:48:45 +010035 new
36 let wincount = winnr('$')
37 call setline(1, ['one', 'two', 'three'])
38 luado vim.command("new")
39 call assert_equal(wincount + 1, winnr('$'))
Bram Moolenaar4ff48142018-06-30 21:50:25 +020040 %bwipe!
41endfunc
42
43" Test vim.command()
Bram Moolenaar5feabe02020-01-30 18:24:53 +010044func Test_lua_command()
Bram Moolenaar4ff48142018-06-30 21:50:25 +020045 new
46 call setline(1, ['one', 'two', 'three'])
47 luado vim.command("1,2d_")
48 call assert_equal(['three'], getline(1, '$'))
Bram Moolenaard58f03b2017-01-29 22:48:45 +010049 bwipe!
Bram Moolenaar4ff48142018-06-30 21:50:25 +020050endfunc
51
Bram Moolenaare49b8e82020-07-01 13:52:55 +020052func Test_lua_luado()
53 new
54 call setline(1, ['one', 'two'])
55 luado return(linenr)
56 call assert_equal(['1', '2'], getline(1, '$'))
57 close!
58
59 " Error cases
60 call assert_fails('luado string.format()',
61 \ "[string \"vim chunk\"]:1: bad argument #1 to 'format' (string expected, got no value)")
Bram Moolenaarf65ed862021-04-03 14:13:33 +020062 if s:lua_543_or_later
63 let msg = "[string \"vim chunk\"]:1: global 'func' is not callable (a nil value)"
64 elseif s:lua_53_or_later
65 let msg = "[string \"vim chunk\"]:1: attempt to call a nil value (global 'func')"
66 else
67 let msg = "[string \"vim chunk\"]:1: attempt to call global 'func' (a nil value)"
68 endif
69 call assert_fails('luado func()', msg)
Bram Moolenaare49b8e82020-07-01 13:52:55 +020070 call assert_fails('luado error("failed")', "[string \"vim chunk\"]:1: failed")
71endfunc
72
Bram Moolenaar4ff48142018-06-30 21:50:25 +020073" Test vim.eval()
Bram Moolenaar5feabe02020-01-30 18:24:53 +010074func Test_lua_eval()
Bram Moolenaar4ff48142018-06-30 21:50:25 +020075 " lua.eval with a number
76 lua v = vim.eval('123')
77 call assert_equal('number', luaeval('vim.type(v)'))
Bram Moolenaareb04f082020-05-17 14:32:35 +020078 call assert_equal(123, luaeval('v'))
Bram Moolenaar4ff48142018-06-30 21:50:25 +020079
80 " lua.eval with a string
81 lua v = vim.eval('"abc"')
Bram Moolenaar02b31112019-08-31 22:16:38 +020082 call assert_equal('string', 'vim.type(v)'->luaeval())
Bram Moolenaar4ff48142018-06-30 21:50:25 +020083 call assert_equal('abc', luaeval('v'))
84
85 " lua.eval with a list
86 lua v = vim.eval("['a']")
87 call assert_equal('list', luaeval('vim.type(v)'))
88 call assert_equal(['a'], luaeval('v'))
89
90 " lua.eval with a dict
91 lua v = vim.eval("{'a':'b'}")
92 call assert_equal('dict', luaeval('vim.type(v)'))
93 call assert_equal({'a':'b'}, luaeval('v'))
94
Bram Moolenaarb7828692019-03-23 13:57:02 +010095 " lua.eval with a blob
96 lua v = vim.eval("0z00112233.deadbeef")
97 call assert_equal('blob', luaeval('vim.type(v)'))
98 call assert_equal(0z00112233.deadbeef, luaeval('v'))
99
Bram Moolenaare49b8e82020-07-01 13:52:55 +0200100 " lua.eval with a float
101 lua v = vim.eval('3.14')
102 call assert_equal('number', luaeval('vim.type(v)'))
103 call assert_equal(3.14, luaeval('v'))
104
105 " lua.eval with a bool
106 lua v = vim.eval('v:true')
107 call assert_equal('number', luaeval('vim.type(v)'))
108 call assert_equal(1, luaeval('v'))
109 lua v = vim.eval('v:false')
110 call assert_equal('number', luaeval('vim.type(v)'))
111 call assert_equal(0, luaeval('v'))
112
113 " lua.eval with a null
114 lua v = vim.eval('v:null')
115 call assert_equal('nil', luaeval('vim.type(v)'))
116 call assert_equal(v:null, luaeval('v'))
117
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200118 call assert_fails('lua v = vim.eval(nil)',
119 \ "[string \"vim chunk\"]:1: bad argument #1 to 'eval' (string expected, got nil)")
120 call assert_fails('lua v = vim.eval(true)',
121 \ "[string \"vim chunk\"]:1: bad argument #1 to 'eval' (string expected, got boolean)")
122 call assert_fails('lua v = vim.eval({})',
123 \ "[string \"vim chunk\"]:1: bad argument #1 to 'eval' (string expected, got table)")
124 call assert_fails('lua v = vim.eval(print)',
125 \ "[string \"vim chunk\"]:1: bad argument #1 to 'eval' (string expected, got function)")
126 call assert_fails('lua v = vim.eval(vim.buffer())',
127 \ "[string \"vim chunk\"]:1: bad argument #1 to 'eval' (string expected, got userdata)")
128
129 lua v = nil
130endfunc
131
Bram Moolenaar86c3a212021-03-08 19:50:24 +0100132" Test luaeval() with lambda
133func Test_luaeval_with_lambda()
134 lua function hello_luaeval_lambda(a, cb) return a .. cb() end
135 call assert_equal('helloworld',
136 \ luaeval('hello_luaeval_lambda(_A[1], _A[2])',
137 \ ['hello', {->'world'}]))
138 lua hello_luaeval_lambda = nil
139endfunc
140
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200141" Test vim.window()
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100142func Test_lua_window()
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200143 e Xfoo2
144 new Xfoo1
145
146 " Window 1 (top window) contains Xfoo1
147 " Window 2 (bottom window) contains Xfoo2
148 call assert_equal('Xfoo1', luaeval('vim.window(1):buffer().name'))
149 call assert_equal('Xfoo2', luaeval('vim.window(2):buffer().name'))
150
151 " Window 3 does not exist so vim.window(3) should return nil
152 call assert_equal('nil', luaeval('tostring(vim.window(3))'))
153
Bram Moolenaarf65ed862021-04-03 14:13:33 +0200154 if s:lua_543_or_later
155 let msg = "[string \"luaeval\"]:1: field 'xyz' is not callable (a nil value)"
156 elseif s:lua_53_or_later
157 let msg = "[string \"luaeval\"]:1: attempt to call a nil value (field 'xyz')"
158 else
159 let msg = "[string \"luaeval\"]:1: attempt to call field 'xyz' (a nil value)"
160 endif
161 call assert_fails("let n = luaeval('vim.window().xyz()')", msg)
Bram Moolenaare49b8e82020-07-01 13:52:55 +0200162 call assert_fails('lua vim.window().xyz = 1',
163 \ "[string \"vim chunk\"]:1: invalid window property: `xyz'")
164
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200165 %bwipe!
166endfunc
167
168" Test vim.window().height
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100169func Test_lua_window_height()
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200170 new
171 lua vim.window().height = 2
172 call assert_equal(2, winheight(0))
173 lua vim.window().height = vim.window().height + 1
174 call assert_equal(3, winheight(0))
175 bwipe!
176endfunc
177
178" Test vim.window().width
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100179func Test_lua_window_width()
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200180 vert new
181 lua vim.window().width = 2
182 call assert_equal(2, winwidth(0))
183 lua vim.window().width = vim.window().width + 1
184 call assert_equal(3, winwidth(0))
185 bwipe!
186endfunc
187
188" Test vim.window().line and vim.window.col
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100189func Test_lua_window_line_col()
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200190 new
191 call setline(1, ['line1', 'line2', 'line3'])
192 lua vim.window().line = 2
193 lua vim.window().col = 4
194 call assert_equal([0, 2, 4, 0], getpos('.'))
195 lua vim.window().line = vim.window().line + 1
196 lua vim.window().col = vim.window().col - 1
197 call assert_equal([0, 3, 3, 0], getpos('.'))
198
199 call assert_fails('lua vim.window().line = 10',
200 \ '[string "vim chunk"]:1: line out of range')
201 bwipe!
202endfunc
203
Bram Moolenaareb04f082020-05-17 14:32:35 +0200204" Test vim.call
205func Test_lua_call()
206 call assert_equal(has('lua'), luaeval('vim.call("has", "lua")'))
207 call assert_equal(printf("Hello %s", "vim"), luaeval('vim.call("printf", "Hello %s", "vim")'))
Bram Moolenaare49b8e82020-07-01 13:52:55 +0200208
209 " Error cases
210 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 +0200211 \ s:lua_53_or_later
212 \ ? '[string "luaeval"]:1: Function called with too many arguments'
213 \ : 'Function called with too many arguments')
Bram Moolenaare49b8e82020-07-01 13:52:55 +0200214 lua co = coroutine.create(function () print("hi") end)
215 call assert_fails("call luaeval('vim.call(\"type\", co)')",
Bram Moolenaarb898a022020-07-12 18:33:53 +0200216 \ s:lua_53_or_later
217 \ ? '[string "luaeval"]:1: lua: cannot convert value'
218 \ : 'lua: cannot convert value')
Bram Moolenaare49b8e82020-07-01 13:52:55 +0200219 lua co = nil
Bram Moolenaarb898a022020-07-12 18:33:53 +0200220 call assert_fails("call luaeval('vim.call(\"abc\")')",
221 \ ['E117:', s:lua_53_or_later ? '\[string "luaeval"]:1: lua: call_vim_function failed'
222 \ : 'lua: call_vim_function failed'])
Bram Moolenaareb04f082020-05-17 14:32:35 +0200223endfunc
224
225" Test vim.fn.*
226func Test_lua_fn()
227 call assert_equal(has('lua'), luaeval('vim.fn.has("lua")'))
228 call assert_equal(printf("Hello %s", "vim"), luaeval('vim.fn.printf("Hello %s", "vim")'))
229endfunc
230
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200231" Test setting the current window
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100232func Test_lua_window_set_current()
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200233 new Xfoo1
234 lua w1 = vim.window()
235 new Xfoo2
236 lua w2 = vim.window()
237
238 call assert_equal('Xfoo2', bufname('%'))
239 lua w1()
240 call assert_equal('Xfoo1', bufname('%'))
241 lua w2()
242 call assert_equal('Xfoo2', bufname('%'))
243
Bram Moolenaar2f362bf2018-07-01 19:49:27 +0200244 lua w1, w2 = nil
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200245 %bwipe!
246endfunc
247
248" Test vim.window().buffer
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100249func Test_lua_window_buffer()
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200250 new Xfoo1
251 lua w1 = vim.window()
252 lua b1 = w1.buffer()
253 new Xfoo2
254 lua w2 = vim.window()
255 lua b2 = w2.buffer()
256
257 lua b1()
258 call assert_equal('Xfoo1', bufname('%'))
259 lua b2()
260 call assert_equal('Xfoo2', bufname('%'))
261
Bram Moolenaar2f362bf2018-07-01 19:49:27 +0200262 lua b1, b2, w1, w2 = nil
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200263 %bwipe!
264endfunc
265
266" Test vim.window():previous() and vim.window():next()
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100267func Test_lua_window_next_previous()
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200268 new Xfoo1
269 new Xfoo2
270 new Xfoo3
271 wincmd j
272
273 call assert_equal('Xfoo2', luaeval('vim.window().buffer().name'))
274 call assert_equal('Xfoo1', luaeval('vim.window():next():buffer().name'))
275 call assert_equal('Xfoo3', luaeval('vim.window():previous():buffer().name'))
276
277 %bwipe!
278endfunc
279
280" Test vim.window():isvalid()
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100281func Test_lua_window_isvalid()
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200282 new Xfoo
283 lua w = vim.window()
284 call assert_true(luaeval('w:isvalid()'))
285
286 " FIXME: how to test the case when isvalid() returns v:false?
287 " isvalid() gives errors when the window is deleted. Is it a bug?
288
289 lua w = nil
290 bwipe!
291endfunc
292
293" Test vim.buffer() with and without argument
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100294func Test_lua_buffer()
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200295 new Xfoo1
296 let bn1 = bufnr('%')
297 new Xfoo2
298 let bn2 = bufnr('%')
299
300 " Test vim.buffer() without argument.
301 call assert_equal('Xfoo2', luaeval("vim.buffer().name"))
302
303 " Test vim.buffer() with string argument.
304 call assert_equal('Xfoo1', luaeval("vim.buffer('Xfoo1').name"))
305 call assert_equal('Xfoo2', luaeval("vim.buffer('Xfoo2').name"))
306
307 " Test vim.buffer() with integer argument.
308 call assert_equal('Xfoo1', luaeval("vim.buffer(" . bn1 . ").name"))
309 call assert_equal('Xfoo2', luaeval("vim.buffer(" . bn2 . ").name"))
310
Bram Moolenaar2f362bf2018-07-01 19:49:27 +0200311 lua bn1, bn2 = nil
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200312 %bwipe!
313endfunc
314
315" Test vim.buffer().name and vim.buffer().fname
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100316func Test_lua_buffer_name()
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200317 new
Bram Moolenaarfe08df42018-07-07 23:07:41 +0200318 call assert_equal('', luaeval('vim.buffer().name'))
319 call assert_equal('', luaeval('vim.buffer().fname'))
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200320 bwipe!
321
322 new Xfoo
323 call assert_equal('Xfoo', luaeval('vim.buffer().name'))
324 call assert_equal(expand('%:p'), luaeval('vim.buffer().fname'))
325 bwipe!
326endfunc
327
328" Test vim.buffer().number
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100329func Test_lua_buffer_number()
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200330 " All numbers in Lua are floating points number (no integers).
331 call assert_equal(bufnr('%'), float2nr(luaeval('vim.buffer().number')))
332endfunc
333
334" Test inserting lines in buffer.
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100335func Test_lua_buffer_insert()
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200336 new
337 lua vim.buffer()[1] = '3'
338 lua vim.buffer():insert('1', 0)
339 lua vim.buffer():insert('2', 1)
340 lua vim.buffer():insert('4', 10)
341
342 call assert_equal(['1', '2', '3', '4'], getline(1, '$'))
Bram Moolenaare49b8e82020-07-01 13:52:55 +0200343 call assert_equal('4', luaeval('vim.buffer()[4]'))
344 call assert_equal(v:null, luaeval('vim.buffer()[5]'))
345 call assert_equal(v:null, luaeval('vim.buffer()[{}]'))
Bram Moolenaarf65ed862021-04-03 14:13:33 +0200346 if s:lua_543_or_later
347 let msg = "[string \"vim chunk\"]:1: method 'xyz' is not callable (a nil value)"
348 elseif s:lua_53_or_later
349 let msg = "[string \"vim chunk\"]:1: attempt to call a nil value (method 'xyz')"
350 else
351 let msg = "[string \"vim chunk\"]:1: attempt to call method 'xyz' (a nil value)"
352 endif
353 call assert_fails('lua vim.buffer():xyz()', msg)
Bram Moolenaare49b8e82020-07-01 13:52:55 +0200354 call assert_fails('lua vim.buffer()[1] = {}',
355 \ '[string "vim chunk"]:1: wrong argument to change')
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200356 bwipe!
357endfunc
358
359" Test deleting line in buffer
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100360func Test_lua_buffer_delete()
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200361 new
362 call setline(1, ['1', '2', '3'])
Bram Moolenaare49b8e82020-07-01 13:52:55 +0200363 call cursor(3, 1)
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200364 lua vim.buffer()[2] = nil
365 call assert_equal(['1', '3'], getline(1, '$'))
366
367 call assert_fails('lua vim.buffer()[3] = nil',
368 \ '[string "vim chunk"]:1: invalid line number')
369 bwipe!
370endfunc
371
372" Test #vim.buffer() i.e. number of lines in buffer
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100373func Test_lua_buffer_number_lines()
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200374 new
375 call setline(1, ['a', 'b', 'c'])
Bram Moolenaareb04f082020-05-17 14:32:35 +0200376 call assert_equal(3, luaeval('#vim.buffer()'))
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200377 bwipe!
378endfunc
379
380" Test vim.buffer():next() and vim.buffer():previous()
381" Note that these functions get the next or previous buffers
382" but do not switch buffer.
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100383func Test_lua_buffer_next_previous()
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200384 new Xfoo1
385 new Xfoo2
386 new Xfoo3
387 b Xfoo2
388
389 lua bn = vim.buffer():next()
390 lua bp = vim.buffer():previous()
391
392 call assert_equal('Xfoo2', luaeval('vim.buffer().name'))
393 call assert_equal('Xfoo1', luaeval('bp.name'))
394 call assert_equal('Xfoo3', luaeval('bn.name'))
395
396 call assert_equal('Xfoo2', bufname('%'))
397
398 lua bn()
399 call assert_equal('Xfoo3', luaeval('vim.buffer().name'))
400 call assert_equal('Xfoo3', bufname('%'))
401
402 lua bp()
403 call assert_equal('Xfoo1', luaeval('vim.buffer().name'))
404 call assert_equal('Xfoo1', bufname('%'))
405
Bram Moolenaar2f362bf2018-07-01 19:49:27 +0200406 lua bn, bp = nil
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200407 %bwipe!
408endfunc
409
410" Test vim.buffer():isvalid()
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100411func Test_lua_buffer_isvalid()
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200412 new Xfoo
413 lua b = vim.buffer()
414 call assert_true(luaeval('b:isvalid()'))
415
416 " FIXME: how to test the case when isvalid() returns v:false?
417 " isvalid() gives errors when the buffer is wiped. Is it a bug?
418
419 lua b = nil
420 bwipe!
421endfunc
422
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100423func Test_lua_list()
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200424 call assert_equal([], luaeval('vim.list()'))
425
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200426 let l = []
427 lua l = vim.eval('l')
428 lua l:add(123)
429 lua l:add('abc')
430 lua l:add(true)
431 lua l:add(false)
Bram Moolenaar9067cd62019-01-01 00:41:54 +0100432 lua l:add(nil)
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200433 lua l:add(vim.eval("[1, 2, 3]"))
434 lua l:add(vim.eval("{'a':1, 'b':2, 'c':3}"))
Bram Moolenaareb04f082020-05-17 14:32:35 +0200435 call assert_equal([123, 'abc', v:true, v:false, v:null, [1, 2, 3], {'a': 1, 'b': 2, 'c': 3}], l)
436 call assert_equal(7, luaeval('#l'))
Bram Moolenaara8a60d02018-07-02 22:54:36 +0200437 call assert_match('^list: \%(0x\)\?\x\+$', luaeval('tostring(l)'))
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200438
Bram Moolenaarbd846172020-06-27 12:32:57 +0200439 lua l[1] = 124
440 lua l[6] = nil
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200441 lua l:insert('first')
442 lua l:insert('xx', 3)
Bram Moolenaare49b8e82020-07-01 13:52:55 +0200443 call assert_fails('lua l:insert("xx", -20)',
444 \ '[string "vim chunk"]:1: invalid position')
Bram Moolenaareb04f082020-05-17 14:32:35 +0200445 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 +0200446
Bram Moolenaar2f362bf2018-07-01 19:49:27 +0200447 lockvar 1 l
448 call assert_fails('lua l:add("x")', '[string "vim chunk"]:1: list is locked')
Bram Moolenaare49b8e82020-07-01 13:52:55 +0200449 call assert_fails('lua l:insert(2)', '[string "vim chunk"]:1: list is locked')
450 call assert_fails('lua l[9] = 1', '[string "vim chunk"]:1: list is locked')
451
452 unlockvar l
453 let l = [1, 2]
454 lua ll = vim.eval('l')
455 let x = luaeval("ll[3]")
456 call assert_equal(v:null, x)
Bram Moolenaarf65ed862021-04-03 14:13:33 +0200457 if s:lua_543_or_later
458 let msg = "[string \"luaeval\"]:1: method 'xyz' is not callable (a nil value)"
459 elseif s:lua_53_or_later
460 let msg = "[string \"luaeval\"]:1: attempt to call a nil value (method 'xyz')"
461 else
462 let msg = "[string \"luaeval\"]:1: attempt to call method 'xyz' (a nil value)"
463 endif
464 call assert_fails('let x = luaeval("ll:xyz(3)")', msg)
Bram Moolenaare49b8e82020-07-01 13:52:55 +0200465 let y = luaeval("ll[{}]")
466 call assert_equal(v:null, y)
Bram Moolenaar2f362bf2018-07-01 19:49:27 +0200467
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200468 lua l = nil
469endfunc
470
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100471func Test_lua_list_table()
Bram Moolenaar2f362bf2018-07-01 19:49:27 +0200472 " See :help lua-vim
473 " Non-numeric keys should not be used to initialize the list
474 " so say = 'hi' should be ignored.
475 lua t = {3.14, 'hello', false, true, say = 'hi'}
476 call assert_equal([3.14, 'hello', v:false, v:true], luaeval('vim.list(t)'))
477 lua t = nil
478
479 call assert_fails('lua vim.list(1)', '[string "vim chunk"]:1: table expected, got number')
480 call assert_fails('lua vim.list("x")', '[string "vim chunk"]:1: table expected, got string')
481 call assert_fails('lua vim.list(print)', '[string "vim chunk"]:1: table expected, got function')
482 call assert_fails('lua vim.list(true)', '[string "vim chunk"]:1: table expected, got boolean')
483endfunc
484
Bram Moolenaara1f9f862020-06-28 22:41:26 +0200485func Test_lua_list_table_insert_remove()
Bram Moolenaare49b8e82020-07-01 13:52:55 +0200486 if !s:lua_53_or_later
Bram Moolenaara1f9f862020-06-28 22:41:26 +0200487 throw 'Skipped: Lua version < 5.3'
488 endif
489
Bram Moolenaarf65ed862021-04-03 14:13:33 +0200490 let l = [1, 2]
Bram Moolenaara1f9f862020-06-28 22:41:26 +0200491 lua t = vim.eval('l')
492 lua table.insert(t, 10)
493 lua t[#t + 1] = 20
494 lua table.insert(t, 2, 30)
495 call assert_equal(l, [1, 30, 2, 10, 20])
496 lua table.remove(t, 2)
497 call assert_equal(l, [1, 2, 10, 20])
498 lua t[3] = nil
499 call assert_equal(l, [1, 2, 20])
500 lua removed_value = table.remove(t, 3)
501 call assert_equal(luaeval('removed_value'), 20)
502 lua t = nil
503 lua removed_value = nil
504 unlet l
505endfunc
506
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200507" Test l() i.e. iterator on list
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100508func Test_lua_list_iter()
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200509 lua l = vim.list():add('foo'):add('bar')
510 lua str = ''
511 lua for v in l() do str = str .. v end
512 call assert_equal('foobar', luaeval('str'))
513
Bram Moolenaar2f362bf2018-07-01 19:49:27 +0200514 lua str, l = nil
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200515endfunc
516
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100517func Test_lua_recursive_list()
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200518 lua l = vim.list():add(1):add(2)
519 lua l = l:add(l)
520
Bram Moolenaarbd846172020-06-27 12:32:57 +0200521 call assert_equal(1, luaeval('l[1]'))
522 call assert_equal(2, luaeval('l[2]'))
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200523
Bram Moolenaarbd846172020-06-27 12:32:57 +0200524 call assert_equal(1, luaeval('l[3][1]'))
525 call assert_equal(2, luaeval('l[3][2]'))
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200526
Bram Moolenaarbd846172020-06-27 12:32:57 +0200527 call assert_equal(1, luaeval('l[3][3][1]'))
528 call assert_equal(2, luaeval('l[3][3][2]'))
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200529
Bram Moolenaareb04f082020-05-17 14:32:35 +0200530 call assert_equal('[1, 2, [...]]', string(luaeval('l')))
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200531
Bram Moolenaara8a60d02018-07-02 22:54:36 +0200532 call assert_match('^list: \%(0x\)\?\x\+$', luaeval('tostring(l)'))
Bram Moolenaarbd846172020-06-27 12:32:57 +0200533 call assert_equal(luaeval('tostring(l)'), luaeval('tostring(l[3])'))
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200534
Bram Moolenaarbd846172020-06-27 12:32:57 +0200535 call assert_equal(luaeval('l'), luaeval('l[3]'))
536 call assert_equal(luaeval('l'), luaeval('l[3][3]'))
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200537
538 lua l = nil
539endfunc
540
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100541func Test_lua_dict()
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200542 call assert_equal({}, luaeval('vim.dict()'))
543
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200544 let d = {}
545 lua d = vim.eval('d')
546 lua d[0] = 123
547 lua d[1] = "abc"
548 lua d[2] = true
549 lua d[3] = false
550 lua d[4] = vim.eval("[1, 2, 3]")
551 lua d[5] = vim.eval("{'a':1, 'b':2, 'c':3}")
Bram Moolenaareb04f082020-05-17 14:32:35 +0200552 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)
553 call assert_equal(6, luaeval('#d'))
Bram Moolenaara8a60d02018-07-02 22:54:36 +0200554 call assert_match('^dict: \%(0x\)\?\x\+$', luaeval('tostring(d)'))
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200555
556 call assert_equal('abc', luaeval('d[1]'))
Bram Moolenaare49b8e82020-07-01 13:52:55 +0200557 call assert_equal(v:null, luaeval('d[22]'))
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200558
559 lua d[0] = 124
560 lua d[4] = nil
Bram Moolenaareb04f082020-05-17 14:32:35 +0200561 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 +0200562
Bram Moolenaar2f362bf2018-07-01 19:49:27 +0200563 lockvar 1 d
564 call assert_fails('lua d[6] = 1', '[string "vim chunk"]:1: dict is locked')
Bram Moolenaare49b8e82020-07-01 13:52:55 +0200565 unlockvar d
566
567 " Error case
568 lua d = {}
569 lua d[''] = 10
570 call assert_fails("let t = luaeval('vim.dict(d)')",
Bram Moolenaarb898a022020-07-12 18:33:53 +0200571 \ s:lua_53_or_later
572 \ ? '[string "luaeval"]:1: table has empty key'
573 \ : 'table has empty key')
Bram Moolenaare49b8e82020-07-01 13:52:55 +0200574 let d = {}
575 lua x = vim.eval('d')
576 call assert_fails("lua x[''] = 10", '[string "vim chunk"]:1: empty key')
577 lua x['a'] = nil
578 call assert_equal({}, d)
579
580 " cannot assign funcrefs in the global scope
581 lua x = vim.eval('g:')
582 call assert_fails("lua x['min'] = vim.funcref('max')",
583 \ '[string "vim chunk"]:1: cannot assign funcref to builtin scope')
Bram Moolenaar2f362bf2018-07-01 19:49:27 +0200584
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200585 lua d = nil
586endfunc
587
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100588func Test_lua_dict_table()
Bram Moolenaar2f362bf2018-07-01 19:49:27 +0200589 lua t = {key1 = 'x', key2 = 3.14, key3 = true, key4 = false}
590 call assert_equal({'key1': 'x', 'key2': 3.14, 'key3': v:true, 'key4': v:false},
591 \ luaeval('vim.dict(t)'))
592
593 " Same example as in :help lua-vim.
594 lua t = {math.pi, false, say = 'hi'}
595 " FIXME: commented out as it currently does not work as documented:
596 " Expected {'say': 'hi'}
597 " but got {'1': 3.141593, '2': v:false, 'say': 'hi'}
598 " Is the documentation or the code wrong?
599 "call assert_equal({'say' : 'hi'}, luaeval('vim.dict(t)'))
600 lua t = nil
601
602 call assert_fails('lua vim.dict(1)', '[string "vim chunk"]:1: table expected, got number')
603 call assert_fails('lua vim.dict("x")', '[string "vim chunk"]:1: table expected, got string')
604 call assert_fails('lua vim.dict(print)', '[string "vim chunk"]:1: table expected, got function')
605 call assert_fails('lua vim.dict(true)', '[string "vim chunk"]:1: table expected, got boolean')
606endfunc
607
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200608" Test d() i.e. iterator on dictionary
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100609func Test_lua_dict_iter()
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200610 let d = {'a': 1, 'b':2}
611 lua d = vim.eval('d')
612 lua str = ''
613 lua for k,v in d() do str = str .. k ..':' .. v .. ',' end
614 call assert_equal('a:1,b:2,', luaeval('str'))
615
Bram Moolenaar2f362bf2018-07-01 19:49:27 +0200616 lua str, d = nil
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200617endfunc
618
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100619func Test_lua_blob()
Bram Moolenaarb7828692019-03-23 13:57:02 +0100620 call assert_equal(0z, luaeval('vim.blob("")'))
621 call assert_equal(0z31326162, luaeval('vim.blob("12ab")'))
622 call assert_equal(0z00010203, luaeval('vim.blob("\x00\x01\x02\x03")'))
623 call assert_equal(0z8081FEFF, luaeval('vim.blob("\x80\x81\xfe\xff")'))
624
625 lua b = vim.blob("\x00\x00\x00\x00")
626 call assert_equal(0z00000000, luaeval('b'))
Bram Moolenaareb04f082020-05-17 14:32:35 +0200627 call assert_equal(4, luaeval('#b'))
Bram Moolenaarb7828692019-03-23 13:57:02 +0100628 lua b[0], b[1], b[2], b[3] = 1, 32, 256, 0xff
629 call assert_equal(0z012000ff, luaeval('b'))
630 lua b[4] = string.byte("z", 1)
631 call assert_equal(0z012000ff.7a, luaeval('b'))
Bram Moolenaareb04f082020-05-17 14:32:35 +0200632 call assert_equal(5, luaeval('#b'))
Bram Moolenaarb7828692019-03-23 13:57:02 +0100633 call assert_fails('lua b[#b+1] = 0x80', '[string "vim chunk"]:1: index out of range')
634 lua b:add("12ab")
635 call assert_equal(0z012000ff.7a313261.62, luaeval('b'))
Bram Moolenaareb04f082020-05-17 14:32:35 +0200636 call assert_equal(9, luaeval('#b'))
Bram Moolenaarb7828692019-03-23 13:57:02 +0100637 call assert_fails('lua b:add(nil)', '[string "vim chunk"]:1: string expected, got nil')
638 call assert_fails('lua b:add(true)', '[string "vim chunk"]:1: string expected, got boolean')
639 call assert_fails('lua b:add({})', '[string "vim chunk"]:1: string expected, got table')
640 lua b = nil
Bram Moolenaare49b8e82020-07-01 13:52:55 +0200641
642 let b = 0z0102
643 lua lb = vim.eval('b')
644 let n = luaeval('lb[1]')
645 call assert_equal(2, n)
646 let n = luaeval('lb[6]')
647 call assert_equal(v:null, n)
Bram Moolenaarf65ed862021-04-03 14:13:33 +0200648 if s:lua_543_or_later
649 let msg = "[string \"luaeval\"]:1: method 'xyz' is not callable (a nil value)"
650 elseif s:lua_53_or_later
651 let msg = "[string \"luaeval\"]:1: attempt to call a nil value (method 'xyz')"
652 else
653 let msg = "[string \"luaeval\"]:1: attempt to call method 'xyz' (a nil value)"
654 endif
655 call assert_fails('let x = luaeval("lb:xyz(3)")', msg)
Bram Moolenaare49b8e82020-07-01 13:52:55 +0200656 let y = luaeval("lb[{}]")
657 call assert_equal(v:null, y)
658
659 lockvar b
660 call assert_fails('lua lb[1] = 2', '[string "vim chunk"]:1: blob is locked')
661 call assert_fails('lua lb:add("12")', '[string "vim chunk"]:1: blob is locked')
662
663 " Error cases
664 lua t = {}
665 call assert_fails('lua b = vim.blob(t)',
666 \ '[string "vim chunk"]:1: string expected, got table')
Bram Moolenaarb7828692019-03-23 13:57:02 +0100667endfunc
668
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100669func Test_lua_funcref()
Bram Moolenaarca06da92018-07-01 15:12:05 +0200670 function I(x)
671 return a:x
672 endfunction
673 let R = function('I')
674 lua i1 = vim.funcref"I"
675 lua i2 = vim.eval"R"
676 lua msg = "funcref|test|" .. (#i2(i1) == #i1(i2) and "OK" or "FAIL")
677 lua msg = vim.funcref"tr"(msg, "|", " ")
678 call assert_equal("funcref test OK", luaeval('msg'))
679
Bram Moolenaare49b8e82020-07-01 13:52:55 +0200680 " Error cases
681 call assert_fails('lua f1 = vim.funcref("")',
682 \ '[string "vim chunk"]:1: invalid function name: ')
683 call assert_fails('lua f1 = vim.funcref("10")',
684 \ '[string "vim chunk"]:1: invalid function name: 10')
685 let fname = test_null_string()
686 call assert_fails('lua f1 = vim.funcref(fname)',
687 \ "[string \"vim chunk\"]:1: bad argument #1 to 'funcref' (string expected, got nil)")
688 call assert_fails('lua vim.funcref("abc")()',
Bram Moolenaarecdd14a2020-07-11 22:49:59 +0200689 \ ['E117:', '\[string "vim chunk"]:1: cannot call funcref'])
Bram Moolenaare49b8e82020-07-01 13:52:55 +0200690
Bram Moolenaarca06da92018-07-01 15:12:05 +0200691 " dict funcref
692 function Mylen() dict
693 return len(self.data)
694 endfunction
695 let l = [0, 1, 2, 3]
696 let mydict = {'data': l}
697 lua d = vim.eval"mydict"
698 lua d.len = vim.funcref"Mylen" -- assign d as 'self'
699 lua res = (d.len() == vim.funcref"len"(vim.eval"l")) and "OK" or "FAIL"
700 call assert_equal("OK", luaeval('res'))
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100701 call assert_equal(function('Mylen', {'data': l, 'len': function('Mylen')}), mydict.len)
Bram Moolenaar2f362bf2018-07-01 19:49:27 +0200702
703 lua i1, i2, msg, d, res = nil
Bram Moolenaarca06da92018-07-01 15:12:05 +0200704endfunc
705
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200706" Test vim.type()
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100707func Test_lua_type()
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200708 " The following values are identical to Lua's type function.
709 call assert_equal('string', luaeval('vim.type("foo")'))
710 call assert_equal('number', luaeval('vim.type(1)'))
711 call assert_equal('number', luaeval('vim.type(1.2)'))
712 call assert_equal('function', luaeval('vim.type(print)'))
713 call assert_equal('table', luaeval('vim.type({})'))
714 call assert_equal('boolean', luaeval('vim.type(true)'))
715 call assert_equal('boolean', luaeval('vim.type(false)'))
716 call assert_equal('nil', luaeval('vim.type(nil)'))
717
718 " The following values are specific to Vim.
719 call assert_equal('window', luaeval('vim.type(vim.window())'))
720 call assert_equal('buffer', luaeval('vim.type(vim.buffer())'))
721 call assert_equal('list', luaeval('vim.type(vim.list())'))
722 call assert_equal('dict', luaeval('vim.type(vim.dict())'))
Bram Moolenaarca06da92018-07-01 15:12:05 +0200723 call assert_equal('funcref', luaeval('vim.type(vim.funcref("Test_type"))'))
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200724endfunc
725
726" Test vim.open()
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100727func Test_lua_open()
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200728 call assert_notmatch('XOpen', execute('ls'))
729
730 " Open a buffer XOpen1, but do not jump to it.
731 lua b = vim.open('XOpen1')
732 call assert_equal('XOpen1', luaeval('b.name'))
733 call assert_equal('', bufname('%'))
734
735 call assert_match('XOpen1', execute('ls'))
736 call assert_notequal('XOpen2', bufname('%'))
737
738 " Open a buffer XOpen2 and jump to it.
739 lua b = vim.open('XOpen2')()
740 call assert_equal('XOpen2', luaeval('b.name'))
741 call assert_equal('XOpen2', bufname('%'))
742
743 lua b = nil
744 %bwipe!
745endfunc
746
Bram Moolenaar788fbb42020-05-31 14:08:12 +0200747func Test_update_package_paths()
748 set runtimepath+=./testluaplugin
749 call assert_equal("hello from lua", luaeval("require('testluaplugin').hello()"))
750endfunc
751
Bram Moolenaar801ab062020-06-25 19:27:56 +0200752func Vim_func_call_lua_callback(Concat, Cb)
753 let l:message = a:Concat("hello", "vim")
754 call a:Cb(l:message)
755endfunc
756
757func Test_pass_lua_callback_to_vim_from_lua()
758 lua pass_lua_callback_to_vim_from_lua_result = ""
759 call assert_equal("", luaeval("pass_lua_callback_to_vim_from_lua_result"))
760 lua <<EOF
761 vim.funcref('Vim_func_call_lua_callback')(
762 function(greeting, message)
763 return greeting .. " " .. message
764 end,
765 function(message)
766 pass_lua_callback_to_vim_from_lua_result = message
767 end)
768EOF
769 call assert_equal("hello vim", luaeval("pass_lua_callback_to_vim_from_lua_result"))
770endfunc
771
772func Vim_func_call_metatable_lua_callback(Greet)
773 return a:Greet("world")
774endfunc
775
776func Test_pass_lua_metatable_callback_to_vim_from_lua()
777 let result = luaeval("vim.funcref('Vim_func_call_metatable_lua_callback')(setmetatable({ space = ' '}, { __call = function(tbl, msg) return 'hello' .. tbl.space .. msg end }) )")
778 call assert_equal("hello world", result)
779endfunc
780
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200781" Test vim.line()
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100782func Test_lua_line()
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200783 new
784 call setline(1, ['first line', 'second line'])
785 1
786 call assert_equal('first line', luaeval('vim.line()'))
787 2
788 call assert_equal('second line', luaeval('vim.line()'))
789 bwipe!
790endfunc
791
792" Test vim.beep()
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100793func Test_lua_beep()
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200794 call assert_beeps('lua vim.beep()')
795endfunc
796
797" Test errors in luaeval()
798func Test_luaeval_error()
799 " Compile error
800 call assert_fails("call luaeval('-nil')",
801 \ '[string "luaeval"]:1: attempt to perform arithmetic on a nil value')
802 call assert_fails("call luaeval(']')",
803 \ "[string \"luaeval\"]:1: unexpected symbol near ']'")
Bram Moolenaare49b8e82020-07-01 13:52:55 +0200804 lua co = coroutine.create(function () print("hi") end)
805 call assert_fails('let i = luaeval("co")', 'luaeval: cannot convert value')
806 lua co = nil
807 call assert_fails('let m = luaeval("{}")', 'luaeval: cannot convert value')
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200808endfunc
809
810" Test :luafile foo.lua
811func Test_luafile()
812 call delete('Xlua_file')
Bram Moolenaareb04f082020-05-17 14:32:35 +0200813 call writefile(["str = 'hello'", "num = 123" ], 'Xlua_file')
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200814 call setfperm('Xlua_file', 'r-xr-xr-x')
815
816 luafile Xlua_file
817 call assert_equal('hello', luaeval('str'))
Bram Moolenaareb04f082020-05-17 14:32:35 +0200818 call assert_equal(123, luaeval('num'))
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200819
Bram Moolenaar2f362bf2018-07-01 19:49:27 +0200820 lua str, num = nil
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200821 call delete('Xlua_file')
822endfunc
823
824" Test :luafile %
825func Test_luafile_percent()
826 new Xlua_file
827 append
828 str, num = 'foo', 321.0
829 print(string.format('str=%s, num=%d', str, num))
830.
831 w!
832 luafile %
833 let msg = split(execute('message'), "\n")[-1]
834 call assert_equal('str=foo, num=321', msg)
835
Bram Moolenaar2f362bf2018-07-01 19:49:27 +0200836 lua str, num = nil
837 call delete('Xlua_file')
838 bwipe!
839endfunc
840
841" Test :luafile with syntax error
842func Test_luafile_error()
843 new Xlua_file
844 call writefile(['nil = 0' ], 'Xlua_file')
845 call setfperm('Xlua_file', 'r-xr-xr-x')
846
847 call assert_fails('luafile Xlua_file', "Xlua_file:1: unexpected symbol near 'nil'")
848
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200849 call delete('Xlua_file')
Bram Moolenaard58f03b2017-01-29 22:48:45 +0100850 bwipe!
851endfunc
Bram Moolenaar53901442018-07-25 22:02:36 +0200852
Bram Moolenaar78e006b2021-07-28 15:07:01 +0200853" Test :luafile printing a long string
854func Test_luafile_print()
855 new Xlua_file
856 let lines =<< trim END
857 local data = ''
858 for i = 1, 130 do
859 data = data .. 'xxxxx asd as as dad sad sad xz cxz czxcxzczxc ad ad asd asd asd asd asd'
860 end
861 print(data)
862 END
863 call setline(1, lines)
864 w
865 luafile %
866
867 call delete('Xlua_file')
868 bwipe!
869endfunc
870
Bram Moolenaare49b8e82020-07-01 13:52:55 +0200871" Test for dealing with strings containing newlines and null character
872func Test_lua_string_with_newline()
Bram Moolenaar2a4bd002021-07-28 21:48:59 +0200873 let x = execute('lua print("Hello\nWorld", 2)')
874 call assert_equal("\nHello\nWorld 2", x)
Bram Moolenaare49b8e82020-07-01 13:52:55 +0200875 new
876 lua k = vim.buffer(vim.eval('bufnr()'))
877 lua k:insert("Hello\0World", 0)
878 call assert_equal(["Hello\nWorld", ''], getline(1, '$'))
879 close!
880endfunc
881
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100882func Test_lua_set_cursor()
Bram Moolenaar53901442018-07-25 22:02:36 +0200883 " Check that setting the cursor position works.
884 new
885 call setline(1, ['first line', 'second line'])
886 normal gg
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200887 lua << trim EOF
888 w = vim.window()
889 w.line = 1
890 w.col = 5
891 EOF
Bram Moolenaar53901442018-07-25 22:02:36 +0200892 call assert_equal([1, 5], [line('.'), col('.')])
893
894 " Check that movement after setting cursor position keeps current column.
895 normal j
896 call assert_equal([2, 5], [line('.'), col('.')])
897endfunc
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200898
899" Test for various heredoc syntax
900func Test_lua_heredoc()
901 lua << END
902vim.command('let s = "A"')
903END
904 lua <<
905vim.command('let s ..= "B"')
906.
907 lua << trim END
908 vim.command('let s ..= "C"')
909 END
910 lua << trim
911 vim.command('let s ..= "D"')
912 .
Bram Moolenaar6ab09532020-05-01 14:10:13 +0200913 lua << trim eof
914 vim.command('let s ..= "E"')
915 eof
916 call assert_equal('ABCDE', s)
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200917endfunc
918
Yegappan Lakshmanan9dc4bef2021-08-04 21:12:52 +0200919" Test for adding, accessing and removing global variables using the vim.g
920" Lua table
921func Test_lua_global_var_table()
922 " Access global variables with different types of values
923 let g:Var1 = 10
924 let g:Var2 = 'Hello'
925 let g:Var3 = ['a', 'b']
926 let g:Var4 = #{x: 'edit', y: 'run'}
927 let g:Var5 = function('min')
928 call assert_equal(10, luaeval('vim.g.Var1'))
929 call assert_equal('Hello', luaeval('vim.g.Var2'))
930 call assert_equal(['a', 'b'], luaeval('vim.g.Var3'))
931 call assert_equal(#{x: 'edit', y: 'run'}, luaeval('vim.g.Var4'))
932 call assert_equal(2, luaeval('vim.g.Var5')([5, 9, 2]))
933
934 " Access list of dictionaries and dictionary of lists
935 let g:Var1 = [#{a: 10}, #{b: 20}]
936 let g:Var2 = #{p: [5, 6], q: [1.1, 2.2]}
937 call assert_equal([#{a: 10}, #{b: 20}], luaeval('vim.g.Var1'))
938 call assert_equal(#{p: [5, 6], q: [1.1, 2.2]}, luaeval('vim.g.Var2'))
939
940 " Create new global variables with different types of values
941 unlet g:Var1 g:Var2 g:Var3 g:Var4 g:Var5
942 lua << trim END
943 vim.g.Var1 = 34
944 vim.g.Var2 = 'World'
945 vim.g.Var3 = vim.list({'#', '$'})
946 vim.g.Var4 = vim.dict({model='honda', year=2020})
947 vim.g.Var5 = vim.funcref('max')
948 END
949 call assert_equal(34, g:Var1)
950 call assert_equal('World', g:Var2)
951 call assert_equal(['#', '$'], g:Var3)
952 call assert_equal(#{model: 'honda', year: 2020}, g:Var4)
953 call assert_equal(10, g:Var5([5, 10, 9]))
954
955 " Create list of dictionaries and dictionary of lists
956 unlet g:Var1 g:Var2
957 lua << trim END
958 vim.g.Var1 = vim.list({vim.dict({a=10}), vim.dict({b=20})})
959 vim.g.Var2 = vim.dict({p=vim.list({5, 6}), q=vim.list({1.1, 2.2})})
960 END
961 call assert_equal([#{a: 10}, #{b: 20}], luaeval('vim.g.Var1'))
962 call assert_equal(#{p: [5, 6], q: [1.1, 2.2]}, luaeval('vim.g.Var2'))
963
964 " Modify a global variable with a list value or a dictionary value
965 let g:Var1 = [10, 20]
966 let g:Var2 = #{one: 'mercury', two: 'mars'}
967 lua << trim END
968 vim.g.Var1[2] = Nil
969 vim.g.Var1[3] = 15
970 vim.g.Var2['two'] = Nil
971 vim.g.Var2['three'] = 'earth'
972 END
973 call assert_equal([10, 15], g:Var1)
974 call assert_equal(#{one: 'mercury', three: 'earth'}, g:Var2)
975
976 " Remove global variables with different types of values
977 let g:Var1 = 10
978 let g:Var2 = 'Hello'
979 let g:Var3 = ['a', 'b']
980 let g:Var4 = #{x: 'edit', y: 'run'}
981 let g:Var5 = function('min')
982 lua << trim END
983 vim.g.Var1 = Nil
984 vim.g.Var2 = Nil
985 vim.g.Var3 = Nil
986 vim.g.Var4 = Nil
987 vim.g.Var5 = Nil
988 END
989 call assert_false(exists('g:Var1'))
990 call assert_false(exists('g:Var2'))
991 call assert_false(exists('g:Var3'))
992 call assert_false(exists('g:Var4'))
993 call assert_false(exists('g:Var5'))
994
995 " Try to modify and remove a locked global variable
996 let g:Var1 = 10
997 lockvar g:Var1
998 call assert_fails('lua vim.g.Var1 = 20', 'variable is locked')
999 call assert_fails('lua vim.g.Var1 = Nil', 'variable is locked')
1000 unlockvar g:Var1
1001 let g:Var2 = [7, 14]
1002 lockvar 0 g:Var2
1003 lua vim.g.Var2[2] = Nil
1004 lua vim.g.Var2[3] = 21
1005 call assert_fails('lua vim.g.Var2 = Nil', 'variable is locked')
1006 call assert_equal([7, 21], g:Var2)
1007 lockvar 1 g:Var2
1008 call assert_fails('lua vim.g.Var2[2] = Nil', 'list is locked')
1009 call assert_fails('lua vim.g.Var2[3] = 21', 'list is locked')
1010 unlockvar g:Var2
1011
Bram Moolenaar4a011592021-08-05 15:11:08 +02001012 let g:TestFunc = function('len')
1013 call assert_fails('lua vim.g.func = vim.g.TestFunc', ['E704:', 'Couldn''t add to dictionary'])
1014 unlet g:TestFunc
1015
Yegappan Lakshmanan9dc4bef2021-08-04 21:12:52 +02001016 " Attempt to access a non-existing global variable
1017 call assert_equal(v:null, luaeval('vim.g.NonExistingVar'))
1018 lua vim.g.NonExisting = Nil
1019
1020 unlet! g:Var1 g:Var2 g:Var3 g:Var4 g:Var5
1021endfunc
1022
1023" Test for accessing and modifying predefined vim variables using the vim.v
1024" Lua table
1025func Test_lua_predefined_var_table()
1026 call assert_equal(v:progpath, luaeval('vim.v.progpath'))
1027 let v:errmsg = 'SomeError'
1028 call assert_equal('SomeError', luaeval('vim.v.errmsg'))
1029 lua vim.v.errmsg = 'OtherError'
1030 call assert_equal('OtherError', v:errmsg)
1031 call assert_fails('lua vim.v.errmsg = Nil', 'variable is fixed')
1032 let v:oldfiles = ['one', 'two']
1033 call assert_equal(['one', 'two'], luaeval('vim.v.oldfiles'))
1034 lua vim.v.oldfiles = vim.list({})
1035 call assert_equal([], v:oldfiles)
1036 call assert_equal(v:null, luaeval('vim.v.null'))
1037 call assert_fails('lua vim.v.argv[1] = Nil', 'list is locked')
1038 call assert_fails('lua vim.v.newvar = 1', 'Dictionary is locked')
1039endfunc
1040
1041" Test for adding, accessing and modifying window-local variables using the
1042" vim.w Lua table
1043func Test_lua_window_var_table()
1044 " Access window variables with different types of values
1045 new
1046 let w:wvar1 = 10
1047 let w:wvar2 = 'edit'
1048 let w:wvar3 = 3.14
1049 let w:wvar4 = 0zdeadbeef
1050 let w:wvar5 = ['a', 'b']
1051 let w:wvar6 = #{action: 'run'}
1052 call assert_equal(10, luaeval('vim.w.wvar1'))
1053 call assert_equal('edit', luaeval('vim.w.wvar2'))
1054 call assert_equal(3.14, luaeval('vim.w.wvar3'))
1055 call assert_equal(0zdeadbeef, luaeval('vim.w.wvar4'))
1056 call assert_equal(['a', 'b'], luaeval('vim.w.wvar5'))
1057 call assert_equal(#{action: 'run'}, luaeval('vim.w.wvar6'))
1058 call assert_equal(v:null, luaeval('vim.w.NonExisting'))
1059
1060 " modify a window variable
1061 lua vim.w.wvar2 = 'paste'
1062 call assert_equal('paste', w:wvar2)
1063
1064 " change the type stored in a variable
1065 let w:wvar2 = [1, 2]
1066 lua vim.w.wvar2 = vim.dict({a=10, b=20})
1067 call assert_equal(#{a: 10, b: 20}, w:wvar2)
1068
1069 " create a new window variable
1070 lua vim.w.wvar7 = vim.dict({a=vim.list({1, 2}), b=20})
1071 call assert_equal(#{a: [1, 2], b: 20}, w:wvar7)
1072
1073 " delete a window variable
1074 lua vim.w.wvar2 = Nil
1075 call assert_false(exists('w:wvar2'))
1076
1077 new
1078 call assert_equal(v:null, luaeval('vim.w.wvar1'))
1079 call assert_equal(v:null, luaeval('vim.w.wvar2'))
1080 %bw!
1081endfunc
1082
1083" Test for adding, accessing and modifying buffer-local variables using the
1084" vim.b Lua table
1085func Test_lua_buffer_var_table()
1086 " Access buffer variables with different types of values
1087 let b:bvar1 = 10
1088 let b:bvar2 = 'edit'
1089 let b:bvar3 = 3.14
1090 let b:bvar4 = 0zdeadbeef
1091 let b:bvar5 = ['a', 'b']
1092 let b:bvar6 = #{action: 'run'}
1093 call assert_equal(10, luaeval('vim.b.bvar1'))
1094 call assert_equal('edit', luaeval('vim.b.bvar2'))
1095 call assert_equal(3.14, luaeval('vim.b.bvar3'))
1096 call assert_equal(0zdeadbeef, luaeval('vim.b.bvar4'))
1097 call assert_equal(['a', 'b'], luaeval('vim.b.bvar5'))
1098 call assert_equal(#{action: 'run'}, luaeval('vim.b.bvar6'))
1099 call assert_equal(v:null, luaeval('vim.b.NonExisting'))
1100
1101 " modify a buffer variable
1102 lua vim.b.bvar2 = 'paste'
1103 call assert_equal('paste', b:bvar2)
1104
1105 " change the type stored in a variable
1106 let b:bvar2 = [1, 2]
1107 lua vim.b.bvar2 = vim.dict({a=10, b=20})
1108 call assert_equal(#{a: 10, b: 20}, b:bvar2)
1109
1110 " create a new buffer variable
1111 lua vim.b.bvar7 = vim.dict({a=vim.list({1, 2}), b=20})
1112 call assert_equal(#{a: [1, 2], b: 20}, b:bvar7)
1113
1114 " delete a buffer variable
1115 lua vim.b.bvar2 = Nil
1116 call assert_false(exists('b:bvar2'))
1117
1118 new
1119 call assert_equal(v:null, luaeval('vim.b.bvar1'))
1120 call assert_equal(v:null, luaeval('vim.b.bvar2'))
1121 %bw!
1122endfunc
1123
1124" Test for adding, accessing and modifying tabpage-local variables using the
1125" vim.t Lua table
1126func Test_lua_tabpage_var_table()
1127 " Access tabpage variables with different types of values
1128 let t:tvar1 = 10
1129 let t:tvar2 = 'edit'
1130 let t:tvar3 = 3.14
1131 let t:tvar4 = 0zdeadbeef
1132 let t:tvar5 = ['a', 'b']
1133 let t:tvar6 = #{action: 'run'}
1134 call assert_equal(10, luaeval('vim.t.tvar1'))
1135 call assert_equal('edit', luaeval('vim.t.tvar2'))
1136 call assert_equal(3.14, luaeval('vim.t.tvar3'))
1137 call assert_equal(0zdeadbeef, luaeval('vim.t.tvar4'))
1138 call assert_equal(['a', 'b'], luaeval('vim.t.tvar5'))
1139 call assert_equal(#{action: 'run'}, luaeval('vim.t.tvar6'))
1140 call assert_equal(v:null, luaeval('vim.t.NonExisting'))
1141
1142 " modify a tabpage variable
1143 lua vim.t.tvar2 = 'paste'
1144 call assert_equal('paste', t:tvar2)
1145
1146 " change the type stored in a variable
1147 let t:tvar2 = [1, 2]
1148 lua vim.t.tvar2 = vim.dict({a=10, b=20})
1149 call assert_equal(#{a: 10, b: 20}, t:tvar2)
1150
1151 " create a new tabpage variable
1152 lua vim.t.tvar7 = vim.dict({a=vim.list({1, 2}), b=20})
1153 call assert_equal(#{a: [1, 2], b: 20}, t:tvar7)
1154
1155 " delete a tabpage variable
1156 lua vim.t.tvar2 = Nil
1157 call assert_false(exists('t:tvar2'))
1158
1159 tabnew
1160 call assert_equal(v:null, luaeval('vim.t.tvar1'))
1161 call assert_equal(v:null, luaeval('vim.t.tvar2'))
1162 %bw!
1163endfunc
1164
Yegappan Lakshmanan11328bc2021-08-06 21:34:38 +02001165" Test for vim.version()
1166func Test_lua_vim_version()
1167 lua << trim END
1168 vimver = vim.version()
1169 vimver_n = vimver.major * 100 + vimver.minor
1170 END
1171 call assert_equal(v:version, luaeval('vimver_n'))
1172endfunc
1173
1174" Test for running multiple commands using vim.command()
1175func Test_lua_multiple_commands()
1176 lua << trim END
1177 vim.command([[
1178 let Var1 = []
1179 for i in range(3)
1180 let Var1 += [#{name: 'x'}]
1181 endfor
1182 augroup Luagroup
1183 autocmd!
1184 autocmd User Luatest echo 'Hello'
1185 augroup END
1186 ]])
1187 END
1188 call assert_equal([{'name': 'x'}, {'name': 'x'}, {'name': 'x'}], Var1)
1189 call assert_true(exists('#Luagroup'))
1190 call assert_true(exists('#Luagroup#User#Luatest'))
1191 augroup Luagroup
1192 autocmd!
1193 augroup END
1194 augroup! Luagroup
1195endfunc
1196
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02001197" vim: shiftwidth=2 sts=2 expandtab