blob: fd265ef431edb5f7e1300afeaa9b67a3b3f7b48b [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
=?UTF-8?q?Jakub=20Kul=C3=ADk?=57ff2b72022-01-28 17:20:03 +000018let s:lua_543 = 0
Bram Moolenaarf65ed862021-04-03 14:13:33 +020019if (s:major == 5 && s:minor >= 3) || s:major > 5
Bram Moolenaare49b8e82020-07-01 13:52:55 +020020 let s:lua_53_or_later = 1
=?UTF-8?q?Jakub=20Kul=C3=ADk?=57ff2b72022-01-28 17:20:03 +000021 if s:major == 5 && s:minor == 4 && s:patch == 3
22 let s:lua_543 = 1
Bram Moolenaarf65ed862021-04-03 14:13:33 +020023 endif
Bram Moolenaare49b8e82020-07-01 13:52:55 +020024endif
25
Bram Moolenaare165f632019-03-10 09:48:59 +010026func TearDown()
27 " Run garbage collection after each test to exercise luaV_setref().
28 call test_garbagecollect_now()
29endfunc
30
Bram Moolenaar4ff48142018-06-30 21:50:25 +020031" Check that switching to another buffer does not trigger ml_get error.
Bram Moolenaar5feabe02020-01-30 18:24:53 +010032func Test_lua_command_new_no_ml_get_error()
Bram Moolenaard58f03b2017-01-29 22:48:45 +010033 new
34 let wincount = winnr('$')
35 call setline(1, ['one', 'two', 'three'])
36 luado vim.command("new")
37 call assert_equal(wincount + 1, winnr('$'))
Bram Moolenaar4ff48142018-06-30 21:50:25 +020038 %bwipe!
39endfunc
40
41" Test vim.command()
Bram Moolenaar5feabe02020-01-30 18:24:53 +010042func Test_lua_command()
Bram Moolenaar4ff48142018-06-30 21:50:25 +020043 new
44 call setline(1, ['one', 'two', 'three'])
45 luado vim.command("1,2d_")
46 call assert_equal(['three'], getline(1, '$'))
Bram Moolenaard58f03b2017-01-29 22:48:45 +010047 bwipe!
Bram Moolenaar4ff48142018-06-30 21:50:25 +020048endfunc
49
Bram Moolenaare49b8e82020-07-01 13:52:55 +020050func Test_lua_luado()
51 new
52 call setline(1, ['one', 'two'])
53 luado return(linenr)
54 call assert_equal(['1', '2'], getline(1, '$'))
55 close!
56
57 " Error cases
58 call assert_fails('luado string.format()',
59 \ "[string \"vim chunk\"]:1: bad argument #1 to 'format' (string expected, got no value)")
=?UTF-8?q?Jakub=20Kul=C3=ADk?=57ff2b72022-01-28 17:20:03 +000060 if s:lua_543
Bram Moolenaarf65ed862021-04-03 14:13:33 +020061 let msg = "[string \"vim chunk\"]:1: global 'func' is not callable (a nil value)"
62 elseif s:lua_53_or_later
63 let msg = "[string \"vim chunk\"]:1: attempt to call a nil value (global 'func')"
64 else
65 let msg = "[string \"vim chunk\"]:1: attempt to call global 'func' (a nil value)"
66 endif
67 call assert_fails('luado func()', msg)
Bram Moolenaare49b8e82020-07-01 13:52:55 +020068 call assert_fails('luado error("failed")', "[string \"vim chunk\"]:1: failed")
69endfunc
70
Bram Moolenaar4ff48142018-06-30 21:50:25 +020071" Test vim.eval()
Bram Moolenaar5feabe02020-01-30 18:24:53 +010072func Test_lua_eval()
Bram Moolenaar4ff48142018-06-30 21:50:25 +020073 " lua.eval with a number
74 lua v = vim.eval('123')
75 call assert_equal('number', luaeval('vim.type(v)'))
Bram Moolenaareb04f082020-05-17 14:32:35 +020076 call assert_equal(123, luaeval('v'))
Bram Moolenaar4ff48142018-06-30 21:50:25 +020077
78 " lua.eval with a string
79 lua v = vim.eval('"abc"')
Bram Moolenaar02b31112019-08-31 22:16:38 +020080 call assert_equal('string', 'vim.type(v)'->luaeval())
Bram Moolenaar4ff48142018-06-30 21:50:25 +020081 call assert_equal('abc', luaeval('v'))
82
83 " lua.eval with a list
84 lua v = vim.eval("['a']")
85 call assert_equal('list', luaeval('vim.type(v)'))
86 call assert_equal(['a'], luaeval('v'))
87
88 " lua.eval with a dict
89 lua v = vim.eval("{'a':'b'}")
90 call assert_equal('dict', luaeval('vim.type(v)'))
91 call assert_equal({'a':'b'}, luaeval('v'))
92
Bram Moolenaarb7828692019-03-23 13:57:02 +010093 " lua.eval with a blob
94 lua v = vim.eval("0z00112233.deadbeef")
95 call assert_equal('blob', luaeval('vim.type(v)'))
96 call assert_equal(0z00112233.deadbeef, luaeval('v'))
97
Bram Moolenaare49b8e82020-07-01 13:52:55 +020098 " lua.eval with a float
99 lua v = vim.eval('3.14')
100 call assert_equal('number', luaeval('vim.type(v)'))
101 call assert_equal(3.14, luaeval('v'))
102
103 " lua.eval with a bool
104 lua v = vim.eval('v:true')
105 call assert_equal('number', luaeval('vim.type(v)'))
106 call assert_equal(1, luaeval('v'))
107 lua v = vim.eval('v:false')
108 call assert_equal('number', luaeval('vim.type(v)'))
109 call assert_equal(0, luaeval('v'))
110
111 " lua.eval with a null
112 lua v = vim.eval('v:null')
113 call assert_equal('nil', luaeval('vim.type(v)'))
114 call assert_equal(v:null, luaeval('v'))
115
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200116 call assert_fails('lua v = vim.eval(nil)',
117 \ "[string \"vim chunk\"]:1: bad argument #1 to 'eval' (string expected, got nil)")
118 call assert_fails('lua v = vim.eval(true)',
119 \ "[string \"vim chunk\"]:1: bad argument #1 to 'eval' (string expected, got boolean)")
120 call assert_fails('lua v = vim.eval({})',
121 \ "[string \"vim chunk\"]:1: bad argument #1 to 'eval' (string expected, got table)")
122 call assert_fails('lua v = vim.eval(print)',
123 \ "[string \"vim chunk\"]:1: bad argument #1 to 'eval' (string expected, got function)")
124 call assert_fails('lua v = vim.eval(vim.buffer())',
125 \ "[string \"vim chunk\"]:1: bad argument #1 to 'eval' (string expected, got userdata)")
126
127 lua v = nil
128endfunc
129
Bram Moolenaar86c3a212021-03-08 19:50:24 +0100130" Test luaeval() with lambda
131func Test_luaeval_with_lambda()
132 lua function hello_luaeval_lambda(a, cb) return a .. cb() end
133 call assert_equal('helloworld',
134 \ luaeval('hello_luaeval_lambda(_A[1], _A[2])',
135 \ ['hello', {->'world'}]))
136 lua hello_luaeval_lambda = nil
137endfunc
138
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200139" Test vim.window()
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100140func Test_lua_window()
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200141 e Xfoo2
142 new Xfoo1
143
144 " Window 1 (top window) contains Xfoo1
145 " Window 2 (bottom window) contains Xfoo2
146 call assert_equal('Xfoo1', luaeval('vim.window(1):buffer().name'))
147 call assert_equal('Xfoo2', luaeval('vim.window(2):buffer().name'))
148
149 " Window 3 does not exist so vim.window(3) should return nil
150 call assert_equal('nil', luaeval('tostring(vim.window(3))'))
151
=?UTF-8?q?Jakub=20Kul=C3=ADk?=57ff2b72022-01-28 17:20:03 +0000152 if s:lua_543
Bram Moolenaarf65ed862021-04-03 14:13:33 +0200153 let msg = "[string \"luaeval\"]:1: field 'xyz' is not callable (a nil value)"
154 elseif s:lua_53_or_later
155 let msg = "[string \"luaeval\"]:1: attempt to call a nil value (field 'xyz')"
156 else
157 let msg = "[string \"luaeval\"]:1: attempt to call field 'xyz' (a nil value)"
158 endif
159 call assert_fails("let n = luaeval('vim.window().xyz()')", msg)
Bram Moolenaare49b8e82020-07-01 13:52:55 +0200160 call assert_fails('lua vim.window().xyz = 1',
161 \ "[string \"vim chunk\"]:1: invalid window property: `xyz'")
162
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200163 %bwipe!
164endfunc
165
166" Test vim.window().height
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100167func Test_lua_window_height()
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200168 new
169 lua vim.window().height = 2
170 call assert_equal(2, winheight(0))
171 lua vim.window().height = vim.window().height + 1
172 call assert_equal(3, winheight(0))
173 bwipe!
174endfunc
175
176" Test vim.window().width
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100177func Test_lua_window_width()
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200178 vert new
179 lua vim.window().width = 2
180 call assert_equal(2, winwidth(0))
181 lua vim.window().width = vim.window().width + 1
182 call assert_equal(3, winwidth(0))
183 bwipe!
184endfunc
185
186" Test vim.window().line and vim.window.col
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100187func Test_lua_window_line_col()
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200188 new
189 call setline(1, ['line1', 'line2', 'line3'])
190 lua vim.window().line = 2
191 lua vim.window().col = 4
192 call assert_equal([0, 2, 4, 0], getpos('.'))
193 lua vim.window().line = vim.window().line + 1
194 lua vim.window().col = vim.window().col - 1
195 call assert_equal([0, 3, 3, 0], getpos('.'))
196
197 call assert_fails('lua vim.window().line = 10',
198 \ '[string "vim chunk"]:1: line out of range')
199 bwipe!
200endfunc
201
Bram Moolenaareb04f082020-05-17 14:32:35 +0200202" Test vim.call
203func Test_lua_call()
204 call assert_equal(has('lua'), luaeval('vim.call("has", "lua")'))
205 call assert_equal(printf("Hello %s", "vim"), luaeval('vim.call("printf", "Hello %s", "vim")'))
Bram Moolenaare49b8e82020-07-01 13:52:55 +0200206
207 " Error cases
208 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 +0200209 \ s:lua_53_or_later
210 \ ? '[string "luaeval"]:1: Function called with too many arguments'
211 \ : 'Function called with too many arguments')
Bram Moolenaare49b8e82020-07-01 13:52:55 +0200212 lua co = coroutine.create(function () print("hi") end)
213 call assert_fails("call luaeval('vim.call(\"type\", co)')",
Bram Moolenaarb898a022020-07-12 18:33:53 +0200214 \ s:lua_53_or_later
215 \ ? '[string "luaeval"]:1: lua: cannot convert value'
216 \ : 'lua: cannot convert value')
Bram Moolenaare49b8e82020-07-01 13:52:55 +0200217 lua co = nil
Bram Moolenaarb898a022020-07-12 18:33:53 +0200218 call assert_fails("call luaeval('vim.call(\"abc\")')",
219 \ ['E117:', s:lua_53_or_later ? '\[string "luaeval"]:1: lua: call_vim_function failed'
220 \ : 'lua: call_vim_function failed'])
Bram Moolenaareb04f082020-05-17 14:32:35 +0200221endfunc
222
223" Test vim.fn.*
224func Test_lua_fn()
225 call assert_equal(has('lua'), luaeval('vim.fn.has("lua")'))
226 call assert_equal(printf("Hello %s", "vim"), luaeval('vim.fn.printf("Hello %s", "vim")'))
227endfunc
228
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200229" Test setting the current window
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100230func Test_lua_window_set_current()
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200231 new Xfoo1
232 lua w1 = vim.window()
233 new Xfoo2
234 lua w2 = vim.window()
235
236 call assert_equal('Xfoo2', bufname('%'))
237 lua w1()
238 call assert_equal('Xfoo1', bufname('%'))
239 lua w2()
240 call assert_equal('Xfoo2', bufname('%'))
241
Bram Moolenaar2f362bf2018-07-01 19:49:27 +0200242 lua w1, w2 = nil
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200243 %bwipe!
244endfunc
245
246" Test vim.window().buffer
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100247func Test_lua_window_buffer()
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200248 new Xfoo1
249 lua w1 = vim.window()
250 lua b1 = w1.buffer()
251 new Xfoo2
252 lua w2 = vim.window()
253 lua b2 = w2.buffer()
254
255 lua b1()
256 call assert_equal('Xfoo1', bufname('%'))
257 lua b2()
258 call assert_equal('Xfoo2', bufname('%'))
259
Bram Moolenaar2f362bf2018-07-01 19:49:27 +0200260 lua b1, b2, w1, w2 = nil
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200261 %bwipe!
262endfunc
263
264" Test vim.window():previous() and vim.window():next()
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100265func Test_lua_window_next_previous()
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200266 new Xfoo1
267 new Xfoo2
268 new Xfoo3
269 wincmd j
270
271 call assert_equal('Xfoo2', luaeval('vim.window().buffer().name'))
272 call assert_equal('Xfoo1', luaeval('vim.window():next():buffer().name'))
273 call assert_equal('Xfoo3', luaeval('vim.window():previous():buffer().name'))
274
275 %bwipe!
276endfunc
277
278" Test vim.window():isvalid()
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100279func Test_lua_window_isvalid()
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200280 new Xfoo
281 lua w = vim.window()
282 call assert_true(luaeval('w:isvalid()'))
283
284 " FIXME: how to test the case when isvalid() returns v:false?
285 " isvalid() gives errors when the window is deleted. Is it a bug?
286
287 lua w = nil
288 bwipe!
289endfunc
290
291" Test vim.buffer() with and without argument
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100292func Test_lua_buffer()
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200293 new Xfoo1
294 let bn1 = bufnr('%')
295 new Xfoo2
296 let bn2 = bufnr('%')
297
298 " Test vim.buffer() without argument.
299 call assert_equal('Xfoo2', luaeval("vim.buffer().name"))
300
301 " Test vim.buffer() with string argument.
302 call assert_equal('Xfoo1', luaeval("vim.buffer('Xfoo1').name"))
303 call assert_equal('Xfoo2', luaeval("vim.buffer('Xfoo2').name"))
304
305 " Test vim.buffer() with integer argument.
306 call assert_equal('Xfoo1', luaeval("vim.buffer(" . bn1 . ").name"))
307 call assert_equal('Xfoo2', luaeval("vim.buffer(" . bn2 . ").name"))
308
Bram Moolenaar2f362bf2018-07-01 19:49:27 +0200309 lua bn1, bn2 = nil
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200310 %bwipe!
311endfunc
312
313" Test vim.buffer().name and vim.buffer().fname
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100314func Test_lua_buffer_name()
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200315 new
Bram Moolenaarfe08df42018-07-07 23:07:41 +0200316 call assert_equal('', luaeval('vim.buffer().name'))
317 call assert_equal('', luaeval('vim.buffer().fname'))
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200318 bwipe!
319
320 new Xfoo
321 call assert_equal('Xfoo', luaeval('vim.buffer().name'))
322 call assert_equal(expand('%:p'), luaeval('vim.buffer().fname'))
323 bwipe!
324endfunc
325
326" Test vim.buffer().number
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100327func Test_lua_buffer_number()
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200328 " All numbers in Lua are floating points number (no integers).
329 call assert_equal(bufnr('%'), float2nr(luaeval('vim.buffer().number')))
330endfunc
331
332" Test inserting lines in buffer.
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100333func Test_lua_buffer_insert()
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200334 new
335 lua vim.buffer()[1] = '3'
336 lua vim.buffer():insert('1', 0)
337 lua vim.buffer():insert('2', 1)
338 lua vim.buffer():insert('4', 10)
339
340 call assert_equal(['1', '2', '3', '4'], getline(1, '$'))
Bram Moolenaare49b8e82020-07-01 13:52:55 +0200341 call assert_equal('4', luaeval('vim.buffer()[4]'))
342 call assert_equal(v:null, luaeval('vim.buffer()[5]'))
343 call assert_equal(v:null, luaeval('vim.buffer()[{}]'))
=?UTF-8?q?Jakub=20Kul=C3=ADk?=57ff2b72022-01-28 17:20:03 +0000344 if s:lua_543
Bram Moolenaarf65ed862021-04-03 14:13:33 +0200345 let msg = "[string \"vim chunk\"]:1: method 'xyz' is not callable (a nil value)"
346 elseif s:lua_53_or_later
347 let msg = "[string \"vim chunk\"]:1: attempt to call a nil value (method 'xyz')"
348 else
349 let msg = "[string \"vim chunk\"]:1: attempt to call method 'xyz' (a nil value)"
350 endif
351 call assert_fails('lua vim.buffer():xyz()', msg)
Bram Moolenaare49b8e82020-07-01 13:52:55 +0200352 call assert_fails('lua vim.buffer()[1] = {}',
353 \ '[string "vim chunk"]:1: wrong argument to change')
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200354 bwipe!
355endfunc
356
357" Test deleting line in buffer
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100358func Test_lua_buffer_delete()
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200359 new
360 call setline(1, ['1', '2', '3'])
Bram Moolenaare49b8e82020-07-01 13:52:55 +0200361 call cursor(3, 1)
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200362 lua vim.buffer()[2] = nil
363 call assert_equal(['1', '3'], getline(1, '$'))
364
365 call assert_fails('lua vim.buffer()[3] = nil',
366 \ '[string "vim chunk"]:1: invalid line number')
367 bwipe!
368endfunc
369
370" Test #vim.buffer() i.e. number of lines in buffer
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100371func Test_lua_buffer_number_lines()
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200372 new
373 call setline(1, ['a', 'b', 'c'])
Bram Moolenaareb04f082020-05-17 14:32:35 +0200374 call assert_equal(3, luaeval('#vim.buffer()'))
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200375 bwipe!
376endfunc
377
378" Test vim.buffer():next() and vim.buffer():previous()
379" Note that these functions get the next or previous buffers
380" but do not switch buffer.
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100381func Test_lua_buffer_next_previous()
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200382 new Xfoo1
383 new Xfoo2
384 new Xfoo3
385 b Xfoo2
386
387 lua bn = vim.buffer():next()
388 lua bp = vim.buffer():previous()
389
390 call assert_equal('Xfoo2', luaeval('vim.buffer().name'))
391 call assert_equal('Xfoo1', luaeval('bp.name'))
392 call assert_equal('Xfoo3', luaeval('bn.name'))
393
394 call assert_equal('Xfoo2', bufname('%'))
395
396 lua bn()
397 call assert_equal('Xfoo3', luaeval('vim.buffer().name'))
398 call assert_equal('Xfoo3', bufname('%'))
399
400 lua bp()
401 call assert_equal('Xfoo1', luaeval('vim.buffer().name'))
402 call assert_equal('Xfoo1', bufname('%'))
403
Bram Moolenaar2f362bf2018-07-01 19:49:27 +0200404 lua bn, bp = nil
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200405 %bwipe!
406endfunc
407
408" Test vim.buffer():isvalid()
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100409func Test_lua_buffer_isvalid()
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200410 new Xfoo
411 lua b = vim.buffer()
412 call assert_true(luaeval('b:isvalid()'))
413
414 " FIXME: how to test the case when isvalid() returns v:false?
415 " isvalid() gives errors when the buffer is wiped. Is it a bug?
416
417 lua b = nil
418 bwipe!
419endfunc
420
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100421func Test_lua_list()
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200422 call assert_equal([], luaeval('vim.list()'))
423
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200424 let l = []
425 lua l = vim.eval('l')
426 lua l:add(123)
427 lua l:add('abc')
428 lua l:add(true)
429 lua l:add(false)
Bram Moolenaar9067cd62019-01-01 00:41:54 +0100430 lua l:add(nil)
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200431 lua l:add(vim.eval("[1, 2, 3]"))
432 lua l:add(vim.eval("{'a':1, 'b':2, 'c':3}"))
Bram Moolenaareb04f082020-05-17 14:32:35 +0200433 call assert_equal([123, 'abc', v:true, v:false, v:null, [1, 2, 3], {'a': 1, 'b': 2, 'c': 3}], l)
434 call assert_equal(7, luaeval('#l'))
Bram Moolenaara8a60d02018-07-02 22:54:36 +0200435 call assert_match('^list: \%(0x\)\?\x\+$', luaeval('tostring(l)'))
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200436
Bram Moolenaarbd846172020-06-27 12:32:57 +0200437 lua l[1] = 124
438 lua l[6] = nil
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200439 lua l:insert('first')
440 lua l:insert('xx', 3)
Bram Moolenaare49b8e82020-07-01 13:52:55 +0200441 call assert_fails('lua l:insert("xx", -20)',
442 \ '[string "vim chunk"]:1: invalid position')
Bram Moolenaareb04f082020-05-17 14:32:35 +0200443 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 +0200444
Bram Moolenaar2f362bf2018-07-01 19:49:27 +0200445 lockvar 1 l
446 call assert_fails('lua l:add("x")', '[string "vim chunk"]:1: list is locked')
Bram Moolenaare49b8e82020-07-01 13:52:55 +0200447 call assert_fails('lua l:insert(2)', '[string "vim chunk"]:1: list is locked')
448 call assert_fails('lua l[9] = 1', '[string "vim chunk"]:1: list is locked')
449
450 unlockvar l
451 let l = [1, 2]
452 lua ll = vim.eval('l')
453 let x = luaeval("ll[3]")
454 call assert_equal(v:null, x)
=?UTF-8?q?Jakub=20Kul=C3=ADk?=57ff2b72022-01-28 17:20:03 +0000455 if s:lua_543
Bram Moolenaarf65ed862021-04-03 14:13:33 +0200456 let msg = "[string \"luaeval\"]:1: method 'xyz' is not callable (a nil value)"
457 elseif s:lua_53_or_later
458 let msg = "[string \"luaeval\"]:1: attempt to call a nil value (method 'xyz')"
459 else
460 let msg = "[string \"luaeval\"]:1: attempt to call method 'xyz' (a nil value)"
461 endif
462 call assert_fails('let x = luaeval("ll:xyz(3)")', msg)
Bram Moolenaare49b8e82020-07-01 13:52:55 +0200463 let y = luaeval("ll[{}]")
464 call assert_equal(v:null, y)
Bram Moolenaar2f362bf2018-07-01 19:49:27 +0200465
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200466 lua l = nil
467endfunc
468
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100469func Test_lua_list_table()
Bram Moolenaar2f362bf2018-07-01 19:49:27 +0200470 " See :help lua-vim
471 " Non-numeric keys should not be used to initialize the list
472 " so say = 'hi' should be ignored.
473 lua t = {3.14, 'hello', false, true, say = 'hi'}
474 call assert_equal([3.14, 'hello', v:false, v:true], luaeval('vim.list(t)'))
475 lua t = nil
476
477 call assert_fails('lua vim.list(1)', '[string "vim chunk"]:1: table expected, got number')
478 call assert_fails('lua vim.list("x")', '[string "vim chunk"]:1: table expected, got string')
479 call assert_fails('lua vim.list(print)', '[string "vim chunk"]:1: table expected, got function')
480 call assert_fails('lua vim.list(true)', '[string "vim chunk"]:1: table expected, got boolean')
481endfunc
482
Bram Moolenaara1f9f862020-06-28 22:41:26 +0200483func Test_lua_list_table_insert_remove()
Bram Moolenaare49b8e82020-07-01 13:52:55 +0200484 if !s:lua_53_or_later
Bram Moolenaara1f9f862020-06-28 22:41:26 +0200485 throw 'Skipped: Lua version < 5.3'
486 endif
487
Bram Moolenaarf65ed862021-04-03 14:13:33 +0200488 let l = [1, 2]
Bram Moolenaara1f9f862020-06-28 22:41:26 +0200489 lua t = vim.eval('l')
490 lua table.insert(t, 10)
491 lua t[#t + 1] = 20
492 lua table.insert(t, 2, 30)
493 call assert_equal(l, [1, 30, 2, 10, 20])
494 lua table.remove(t, 2)
495 call assert_equal(l, [1, 2, 10, 20])
496 lua t[3] = nil
497 call assert_equal(l, [1, 2, 20])
498 lua removed_value = table.remove(t, 3)
499 call assert_equal(luaeval('removed_value'), 20)
500 lua t = nil
501 lua removed_value = nil
502 unlet l
503endfunc
504
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200505" Test l() i.e. iterator on list
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100506func Test_lua_list_iter()
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200507 lua l = vim.list():add('foo'):add('bar')
508 lua str = ''
509 lua for v in l() do str = str .. v end
510 call assert_equal('foobar', luaeval('str'))
511
Bram Moolenaar2f362bf2018-07-01 19:49:27 +0200512 lua str, l = nil
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200513endfunc
514
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100515func Test_lua_recursive_list()
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200516 lua l = vim.list():add(1):add(2)
517 lua l = l:add(l)
518
Bram Moolenaarbd846172020-06-27 12:32:57 +0200519 call assert_equal(1, luaeval('l[1]'))
520 call assert_equal(2, luaeval('l[2]'))
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200521
Bram Moolenaarbd846172020-06-27 12:32:57 +0200522 call assert_equal(1, luaeval('l[3][1]'))
523 call assert_equal(2, luaeval('l[3][2]'))
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200524
Bram Moolenaarbd846172020-06-27 12:32:57 +0200525 call assert_equal(1, luaeval('l[3][3][1]'))
526 call assert_equal(2, luaeval('l[3][3][2]'))
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200527
Bram Moolenaareb04f082020-05-17 14:32:35 +0200528 call assert_equal('[1, 2, [...]]', string(luaeval('l')))
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200529
Bram Moolenaara8a60d02018-07-02 22:54:36 +0200530 call assert_match('^list: \%(0x\)\?\x\+$', luaeval('tostring(l)'))
Bram Moolenaarbd846172020-06-27 12:32:57 +0200531 call assert_equal(luaeval('tostring(l)'), luaeval('tostring(l[3])'))
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200532
Bram Moolenaarbd846172020-06-27 12:32:57 +0200533 call assert_equal(luaeval('l'), luaeval('l[3]'))
534 call assert_equal(luaeval('l'), luaeval('l[3][3]'))
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200535
536 lua l = nil
537endfunc
538
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100539func Test_lua_dict()
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200540 call assert_equal({}, luaeval('vim.dict()'))
541
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200542 let d = {}
543 lua d = vim.eval('d')
544 lua d[0] = 123
545 lua d[1] = "abc"
546 lua d[2] = true
547 lua d[3] = false
548 lua d[4] = vim.eval("[1, 2, 3]")
549 lua d[5] = vim.eval("{'a':1, 'b':2, 'c':3}")
Bram Moolenaareb04f082020-05-17 14:32:35 +0200550 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)
551 call assert_equal(6, luaeval('#d'))
Bram Moolenaara8a60d02018-07-02 22:54:36 +0200552 call assert_match('^dict: \%(0x\)\?\x\+$', luaeval('tostring(d)'))
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200553
554 call assert_equal('abc', luaeval('d[1]'))
Bram Moolenaare49b8e82020-07-01 13:52:55 +0200555 call assert_equal(v:null, luaeval('d[22]'))
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200556
557 lua d[0] = 124
558 lua d[4] = nil
Bram Moolenaareb04f082020-05-17 14:32:35 +0200559 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 +0200560
Bram Moolenaar2f362bf2018-07-01 19:49:27 +0200561 lockvar 1 d
562 call assert_fails('lua d[6] = 1', '[string "vim chunk"]:1: dict is locked')
Bram Moolenaare49b8e82020-07-01 13:52:55 +0200563 unlockvar d
564
565 " Error case
566 lua d = {}
567 lua d[''] = 10
568 call assert_fails("let t = luaeval('vim.dict(d)')",
Bram Moolenaarb898a022020-07-12 18:33:53 +0200569 \ s:lua_53_or_later
570 \ ? '[string "luaeval"]:1: table has empty key'
571 \ : 'table has empty key')
Bram Moolenaare49b8e82020-07-01 13:52:55 +0200572 let d = {}
573 lua x = vim.eval('d')
574 call assert_fails("lua x[''] = 10", '[string "vim chunk"]:1: empty key')
575 lua x['a'] = nil
576 call assert_equal({}, d)
577
578 " cannot assign funcrefs in the global scope
579 lua x = vim.eval('g:')
580 call assert_fails("lua x['min'] = vim.funcref('max')",
581 \ '[string "vim chunk"]:1: cannot assign funcref to builtin scope')
Bram Moolenaar2f362bf2018-07-01 19:49:27 +0200582
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200583 lua d = nil
584endfunc
585
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100586func Test_lua_dict_table()
Bram Moolenaar2f362bf2018-07-01 19:49:27 +0200587 lua t = {key1 = 'x', key2 = 3.14, key3 = true, key4 = false}
588 call assert_equal({'key1': 'x', 'key2': 3.14, 'key3': v:true, 'key4': v:false},
589 \ luaeval('vim.dict(t)'))
590
591 " Same example as in :help lua-vim.
592 lua t = {math.pi, false, say = 'hi'}
593 " FIXME: commented out as it currently does not work as documented:
594 " Expected {'say': 'hi'}
595 " but got {'1': 3.141593, '2': v:false, 'say': 'hi'}
596 " Is the documentation or the code wrong?
597 "call assert_equal({'say' : 'hi'}, luaeval('vim.dict(t)'))
598 lua t = nil
599
600 call assert_fails('lua vim.dict(1)', '[string "vim chunk"]:1: table expected, got number')
601 call assert_fails('lua vim.dict("x")', '[string "vim chunk"]:1: table expected, got string')
602 call assert_fails('lua vim.dict(print)', '[string "vim chunk"]:1: table expected, got function')
603 call assert_fails('lua vim.dict(true)', '[string "vim chunk"]:1: table expected, got boolean')
604endfunc
605
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200606" Test d() i.e. iterator on dictionary
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100607func Test_lua_dict_iter()
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200608 let d = {'a': 1, 'b':2}
609 lua d = vim.eval('d')
610 lua str = ''
611 lua for k,v in d() do str = str .. k ..':' .. v .. ',' end
612 call assert_equal('a:1,b:2,', luaeval('str'))
613
Bram Moolenaar2f362bf2018-07-01 19:49:27 +0200614 lua str, d = nil
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200615endfunc
616
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100617func Test_lua_blob()
Bram Moolenaarb7828692019-03-23 13:57:02 +0100618 call assert_equal(0z, luaeval('vim.blob("")'))
619 call assert_equal(0z31326162, luaeval('vim.blob("12ab")'))
620 call assert_equal(0z00010203, luaeval('vim.blob("\x00\x01\x02\x03")'))
621 call assert_equal(0z8081FEFF, luaeval('vim.blob("\x80\x81\xfe\xff")'))
622
623 lua b = vim.blob("\x00\x00\x00\x00")
624 call assert_equal(0z00000000, luaeval('b'))
Bram Moolenaareb04f082020-05-17 14:32:35 +0200625 call assert_equal(4, luaeval('#b'))
Bram Moolenaarb7828692019-03-23 13:57:02 +0100626 lua b[0], b[1], b[2], b[3] = 1, 32, 256, 0xff
627 call assert_equal(0z012000ff, luaeval('b'))
628 lua b[4] = string.byte("z", 1)
629 call assert_equal(0z012000ff.7a, luaeval('b'))
Bram Moolenaareb04f082020-05-17 14:32:35 +0200630 call assert_equal(5, luaeval('#b'))
Bram Moolenaarb7828692019-03-23 13:57:02 +0100631 call assert_fails('lua b[#b+1] = 0x80', '[string "vim chunk"]:1: index out of range')
632 lua b:add("12ab")
633 call assert_equal(0z012000ff.7a313261.62, luaeval('b'))
Bram Moolenaareb04f082020-05-17 14:32:35 +0200634 call assert_equal(9, luaeval('#b'))
Bram Moolenaarb7828692019-03-23 13:57:02 +0100635 call assert_fails('lua b:add(nil)', '[string "vim chunk"]:1: string expected, got nil')
636 call assert_fails('lua b:add(true)', '[string "vim chunk"]:1: string expected, got boolean')
637 call assert_fails('lua b:add({})', '[string "vim chunk"]:1: string expected, got table')
638 lua b = nil
Bram Moolenaare49b8e82020-07-01 13:52:55 +0200639
640 let b = 0z0102
641 lua lb = vim.eval('b')
642 let n = luaeval('lb[1]')
643 call assert_equal(2, n)
644 let n = luaeval('lb[6]')
645 call assert_equal(v:null, n)
=?UTF-8?q?Jakub=20Kul=C3=ADk?=57ff2b72022-01-28 17:20:03 +0000646 if s:lua_543
Bram Moolenaarf65ed862021-04-03 14:13:33 +0200647 let msg = "[string \"luaeval\"]:1: method 'xyz' is not callable (a nil value)"
648 elseif s:lua_53_or_later
649 let msg = "[string \"luaeval\"]:1: attempt to call a nil value (method 'xyz')"
650 else
651 let msg = "[string \"luaeval\"]:1: attempt to call method 'xyz' (a nil value)"
652 endif
653 call assert_fails('let x = luaeval("lb:xyz(3)")', msg)
Bram Moolenaare49b8e82020-07-01 13:52:55 +0200654 let y = luaeval("lb[{}]")
655 call assert_equal(v:null, y)
656
657 lockvar b
658 call assert_fails('lua lb[1] = 2', '[string "vim chunk"]:1: blob is locked')
659 call assert_fails('lua lb:add("12")', '[string "vim chunk"]:1: blob is locked')
660
661 " Error cases
662 lua t = {}
663 call assert_fails('lua b = vim.blob(t)',
664 \ '[string "vim chunk"]:1: string expected, got table')
Bram Moolenaarb7828692019-03-23 13:57:02 +0100665endfunc
666
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100667func Test_lua_funcref()
Bram Moolenaarca06da92018-07-01 15:12:05 +0200668 function I(x)
669 return a:x
670 endfunction
671 let R = function('I')
672 lua i1 = vim.funcref"I"
673 lua i2 = vim.eval"R"
674 lua msg = "funcref|test|" .. (#i2(i1) == #i1(i2) and "OK" or "FAIL")
675 lua msg = vim.funcref"tr"(msg, "|", " ")
676 call assert_equal("funcref test OK", luaeval('msg'))
677
Bram Moolenaare49b8e82020-07-01 13:52:55 +0200678 " Error cases
679 call assert_fails('lua f1 = vim.funcref("")',
680 \ '[string "vim chunk"]:1: invalid function name: ')
681 call assert_fails('lua f1 = vim.funcref("10")',
682 \ '[string "vim chunk"]:1: invalid function name: 10')
683 let fname = test_null_string()
684 call assert_fails('lua f1 = vim.funcref(fname)',
685 \ "[string \"vim chunk\"]:1: bad argument #1 to 'funcref' (string expected, got nil)")
686 call assert_fails('lua vim.funcref("abc")()',
Bram Moolenaarecdd14a2020-07-11 22:49:59 +0200687 \ ['E117:', '\[string "vim chunk"]:1: cannot call funcref'])
Bram Moolenaare49b8e82020-07-01 13:52:55 +0200688
Bram Moolenaarca06da92018-07-01 15:12:05 +0200689 " dict funcref
690 function Mylen() dict
691 return len(self.data)
692 endfunction
693 let l = [0, 1, 2, 3]
694 let mydict = {'data': l}
695 lua d = vim.eval"mydict"
696 lua d.len = vim.funcref"Mylen" -- assign d as 'self'
697 lua res = (d.len() == vim.funcref"len"(vim.eval"l")) and "OK" or "FAIL"
698 call assert_equal("OK", luaeval('res'))
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100699 call assert_equal(function('Mylen', {'data': l, 'len': function('Mylen')}), mydict.len)
Bram Moolenaar2f362bf2018-07-01 19:49:27 +0200700
701 lua i1, i2, msg, d, res = nil
Bram Moolenaarca06da92018-07-01 15:12:05 +0200702endfunc
703
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200704" Test vim.type()
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100705func Test_lua_type()
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200706 " The following values are identical to Lua's type function.
707 call assert_equal('string', luaeval('vim.type("foo")'))
708 call assert_equal('number', luaeval('vim.type(1)'))
709 call assert_equal('number', luaeval('vim.type(1.2)'))
710 call assert_equal('function', luaeval('vim.type(print)'))
711 call assert_equal('table', luaeval('vim.type({})'))
712 call assert_equal('boolean', luaeval('vim.type(true)'))
713 call assert_equal('boolean', luaeval('vim.type(false)'))
714 call assert_equal('nil', luaeval('vim.type(nil)'))
715
716 " The following values are specific to Vim.
717 call assert_equal('window', luaeval('vim.type(vim.window())'))
718 call assert_equal('buffer', luaeval('vim.type(vim.buffer())'))
719 call assert_equal('list', luaeval('vim.type(vim.list())'))
720 call assert_equal('dict', luaeval('vim.type(vim.dict())'))
Bram Moolenaarca06da92018-07-01 15:12:05 +0200721 call assert_equal('funcref', luaeval('vim.type(vim.funcref("Test_type"))'))
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200722endfunc
723
724" Test vim.open()
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100725func Test_lua_open()
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200726 call assert_notmatch('XOpen', execute('ls'))
727
728 " Open a buffer XOpen1, but do not jump to it.
729 lua b = vim.open('XOpen1')
730 call assert_equal('XOpen1', luaeval('b.name'))
731 call assert_equal('', bufname('%'))
732
733 call assert_match('XOpen1', execute('ls'))
734 call assert_notequal('XOpen2', bufname('%'))
735
736 " Open a buffer XOpen2 and jump to it.
737 lua b = vim.open('XOpen2')()
738 call assert_equal('XOpen2', luaeval('b.name'))
739 call assert_equal('XOpen2', bufname('%'))
740
741 lua b = nil
742 %bwipe!
743endfunc
744
Bram Moolenaar788fbb42020-05-31 14:08:12 +0200745func Test_update_package_paths()
746 set runtimepath+=./testluaplugin
747 call assert_equal("hello from lua", luaeval("require('testluaplugin').hello()"))
748endfunc
749
Bram Moolenaar801ab062020-06-25 19:27:56 +0200750func Vim_func_call_lua_callback(Concat, Cb)
751 let l:message = a:Concat("hello", "vim")
752 call a:Cb(l:message)
753endfunc
754
755func Test_pass_lua_callback_to_vim_from_lua()
756 lua pass_lua_callback_to_vim_from_lua_result = ""
757 call assert_equal("", luaeval("pass_lua_callback_to_vim_from_lua_result"))
758 lua <<EOF
759 vim.funcref('Vim_func_call_lua_callback')(
760 function(greeting, message)
761 return greeting .. " " .. message
762 end,
763 function(message)
764 pass_lua_callback_to_vim_from_lua_result = message
765 end)
766EOF
767 call assert_equal("hello vim", luaeval("pass_lua_callback_to_vim_from_lua_result"))
768endfunc
769
770func Vim_func_call_metatable_lua_callback(Greet)
771 return a:Greet("world")
772endfunc
773
774func Test_pass_lua_metatable_callback_to_vim_from_lua()
775 let result = luaeval("vim.funcref('Vim_func_call_metatable_lua_callback')(setmetatable({ space = ' '}, { __call = function(tbl, msg) return 'hello' .. tbl.space .. msg end }) )")
776 call assert_equal("hello world", result)
777endfunc
778
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200779" Test vim.line()
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100780func Test_lua_line()
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200781 new
782 call setline(1, ['first line', 'second line'])
783 1
784 call assert_equal('first line', luaeval('vim.line()'))
785 2
786 call assert_equal('second line', luaeval('vim.line()'))
787 bwipe!
788endfunc
789
790" Test vim.beep()
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100791func Test_lua_beep()
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200792 call assert_beeps('lua vim.beep()')
793endfunc
794
795" Test errors in luaeval()
796func Test_luaeval_error()
797 " Compile error
798 call assert_fails("call luaeval('-nil')",
799 \ '[string "luaeval"]:1: attempt to perform arithmetic on a nil value')
800 call assert_fails("call luaeval(']')",
801 \ "[string \"luaeval\"]:1: unexpected symbol near ']'")
Bram Moolenaare49b8e82020-07-01 13:52:55 +0200802 lua co = coroutine.create(function () print("hi") end)
803 call assert_fails('let i = luaeval("co")', 'luaeval: cannot convert value')
804 lua co = nil
805 call assert_fails('let m = luaeval("{}")', 'luaeval: cannot convert value')
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200806endfunc
807
808" Test :luafile foo.lua
809func Test_luafile()
810 call delete('Xlua_file')
Bram Moolenaareb04f082020-05-17 14:32:35 +0200811 call writefile(["str = 'hello'", "num = 123" ], 'Xlua_file')
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200812 call setfperm('Xlua_file', 'r-xr-xr-x')
813
814 luafile Xlua_file
815 call assert_equal('hello', luaeval('str'))
Bram Moolenaareb04f082020-05-17 14:32:35 +0200816 call assert_equal(123, luaeval('num'))
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200817
Bram Moolenaar2f362bf2018-07-01 19:49:27 +0200818 lua str, num = nil
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200819 call delete('Xlua_file')
820endfunc
821
822" Test :luafile %
823func Test_luafile_percent()
824 new Xlua_file
825 append
826 str, num = 'foo', 321.0
827 print(string.format('str=%s, num=%d', str, num))
828.
829 w!
830 luafile %
831 let msg = split(execute('message'), "\n")[-1]
832 call assert_equal('str=foo, num=321', msg)
833
Bram Moolenaar2f362bf2018-07-01 19:49:27 +0200834 lua str, num = nil
835 call delete('Xlua_file')
836 bwipe!
837endfunc
838
839" Test :luafile with syntax error
840func Test_luafile_error()
841 new Xlua_file
842 call writefile(['nil = 0' ], 'Xlua_file')
843 call setfperm('Xlua_file', 'r-xr-xr-x')
844
845 call assert_fails('luafile Xlua_file', "Xlua_file:1: unexpected symbol near 'nil'")
846
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200847 call delete('Xlua_file')
Bram Moolenaard58f03b2017-01-29 22:48:45 +0100848 bwipe!
849endfunc
Bram Moolenaar53901442018-07-25 22:02:36 +0200850
Bram Moolenaar78e006b2021-07-28 15:07:01 +0200851" Test :luafile printing a long string
852func Test_luafile_print()
853 new Xlua_file
854 let lines =<< trim END
855 local data = ''
856 for i = 1, 130 do
857 data = data .. 'xxxxx asd as as dad sad sad xz cxz czxcxzczxc ad ad asd asd asd asd asd'
858 end
859 print(data)
860 END
861 call setline(1, lines)
862 w
863 luafile %
864
865 call delete('Xlua_file')
866 bwipe!
867endfunc
868
Bram Moolenaare49b8e82020-07-01 13:52:55 +0200869" Test for dealing with strings containing newlines and null character
870func Test_lua_string_with_newline()
Bram Moolenaar2a4bd002021-07-28 21:48:59 +0200871 let x = execute('lua print("Hello\nWorld", 2)')
872 call assert_equal("\nHello\nWorld 2", x)
Bram Moolenaare49b8e82020-07-01 13:52:55 +0200873 new
874 lua k = vim.buffer(vim.eval('bufnr()'))
875 lua k:insert("Hello\0World", 0)
876 call assert_equal(["Hello\nWorld", ''], getline(1, '$'))
877 close!
878endfunc
879
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100880func Test_lua_set_cursor()
Bram Moolenaar53901442018-07-25 22:02:36 +0200881 " Check that setting the cursor position works.
882 new
883 call setline(1, ['first line', 'second line'])
884 normal gg
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200885 lua << trim EOF
886 w = vim.window()
887 w.line = 1
888 w.col = 5
889 EOF
Bram Moolenaar53901442018-07-25 22:02:36 +0200890 call assert_equal([1, 5], [line('.'), col('.')])
891
892 " Check that movement after setting cursor position keeps current column.
893 normal j
894 call assert_equal([2, 5], [line('.'), col('.')])
895endfunc
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200896
897" Test for various heredoc syntax
898func Test_lua_heredoc()
899 lua << END
900vim.command('let s = "A"')
901END
902 lua <<
903vim.command('let s ..= "B"')
904.
905 lua << trim END
906 vim.command('let s ..= "C"')
907 END
908 lua << trim
909 vim.command('let s ..= "D"')
910 .
Bram Moolenaar6ab09532020-05-01 14:10:13 +0200911 lua << trim eof
912 vim.command('let s ..= "E"')
913 eof
914 call assert_equal('ABCDE', s)
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200915endfunc
916
Yegappan Lakshmanan9dc4bef2021-08-04 21:12:52 +0200917" Test for adding, accessing and removing global variables using the vim.g
918" Lua table
919func Test_lua_global_var_table()
920 " Access global variables with different types of values
921 let g:Var1 = 10
922 let g:Var2 = 'Hello'
923 let g:Var3 = ['a', 'b']
924 let g:Var4 = #{x: 'edit', y: 'run'}
925 let g:Var5 = function('min')
926 call assert_equal(10, luaeval('vim.g.Var1'))
927 call assert_equal('Hello', luaeval('vim.g.Var2'))
928 call assert_equal(['a', 'b'], luaeval('vim.g.Var3'))
929 call assert_equal(#{x: 'edit', y: 'run'}, luaeval('vim.g.Var4'))
930 call assert_equal(2, luaeval('vim.g.Var5')([5, 9, 2]))
931
932 " Access list of dictionaries and dictionary of lists
933 let g:Var1 = [#{a: 10}, #{b: 20}]
934 let g:Var2 = #{p: [5, 6], q: [1.1, 2.2]}
935 call assert_equal([#{a: 10}, #{b: 20}], luaeval('vim.g.Var1'))
936 call assert_equal(#{p: [5, 6], q: [1.1, 2.2]}, luaeval('vim.g.Var2'))
937
938 " Create new global variables with different types of values
939 unlet g:Var1 g:Var2 g:Var3 g:Var4 g:Var5
940 lua << trim END
941 vim.g.Var1 = 34
942 vim.g.Var2 = 'World'
943 vim.g.Var3 = vim.list({'#', '$'})
944 vim.g.Var4 = vim.dict({model='honda', year=2020})
945 vim.g.Var5 = vim.funcref('max')
946 END
947 call assert_equal(34, g:Var1)
948 call assert_equal('World', g:Var2)
949 call assert_equal(['#', '$'], g:Var3)
950 call assert_equal(#{model: 'honda', year: 2020}, g:Var4)
951 call assert_equal(10, g:Var5([5, 10, 9]))
952
953 " Create list of dictionaries and dictionary of lists
954 unlet g:Var1 g:Var2
955 lua << trim END
956 vim.g.Var1 = vim.list({vim.dict({a=10}), vim.dict({b=20})})
957 vim.g.Var2 = vim.dict({p=vim.list({5, 6}), q=vim.list({1.1, 2.2})})
958 END
959 call assert_equal([#{a: 10}, #{b: 20}], luaeval('vim.g.Var1'))
960 call assert_equal(#{p: [5, 6], q: [1.1, 2.2]}, luaeval('vim.g.Var2'))
961
962 " Modify a global variable with a list value or a dictionary value
963 let g:Var1 = [10, 20]
964 let g:Var2 = #{one: 'mercury', two: 'mars'}
965 lua << trim END
966 vim.g.Var1[2] = Nil
967 vim.g.Var1[3] = 15
968 vim.g.Var2['two'] = Nil
969 vim.g.Var2['three'] = 'earth'
970 END
971 call assert_equal([10, 15], g:Var1)
972 call assert_equal(#{one: 'mercury', three: 'earth'}, g:Var2)
973
974 " Remove global variables with different types of values
975 let g:Var1 = 10
976 let g:Var2 = 'Hello'
977 let g:Var3 = ['a', 'b']
978 let g:Var4 = #{x: 'edit', y: 'run'}
979 let g:Var5 = function('min')
980 lua << trim END
981 vim.g.Var1 = Nil
982 vim.g.Var2 = Nil
983 vim.g.Var3 = Nil
984 vim.g.Var4 = Nil
985 vim.g.Var5 = Nil
986 END
987 call assert_false(exists('g:Var1'))
988 call assert_false(exists('g:Var2'))
989 call assert_false(exists('g:Var3'))
990 call assert_false(exists('g:Var4'))
991 call assert_false(exists('g:Var5'))
992
993 " Try to modify and remove a locked global variable
994 let g:Var1 = 10
995 lockvar g:Var1
996 call assert_fails('lua vim.g.Var1 = 20', 'variable is locked')
997 call assert_fails('lua vim.g.Var1 = Nil', 'variable is locked')
998 unlockvar g:Var1
999 let g:Var2 = [7, 14]
1000 lockvar 0 g:Var2
1001 lua vim.g.Var2[2] = Nil
1002 lua vim.g.Var2[3] = 21
1003 call assert_fails('lua vim.g.Var2 = Nil', 'variable is locked')
1004 call assert_equal([7, 21], g:Var2)
1005 lockvar 1 g:Var2
1006 call assert_fails('lua vim.g.Var2[2] = Nil', 'list is locked')
1007 call assert_fails('lua vim.g.Var2[3] = 21', 'list is locked')
1008 unlockvar g:Var2
1009
Bram Moolenaar4a011592021-08-05 15:11:08 +02001010 let g:TestFunc = function('len')
1011 call assert_fails('lua vim.g.func = vim.g.TestFunc', ['E704:', 'Couldn''t add to dictionary'])
1012 unlet g:TestFunc
1013
Yegappan Lakshmanan9dc4bef2021-08-04 21:12:52 +02001014 " Attempt to access a non-existing global variable
1015 call assert_equal(v:null, luaeval('vim.g.NonExistingVar'))
1016 lua vim.g.NonExisting = Nil
1017
1018 unlet! g:Var1 g:Var2 g:Var3 g:Var4 g:Var5
1019endfunc
1020
1021" Test for accessing and modifying predefined vim variables using the vim.v
1022" Lua table
1023func Test_lua_predefined_var_table()
1024 call assert_equal(v:progpath, luaeval('vim.v.progpath'))
1025 let v:errmsg = 'SomeError'
1026 call assert_equal('SomeError', luaeval('vim.v.errmsg'))
1027 lua vim.v.errmsg = 'OtherError'
1028 call assert_equal('OtherError', v:errmsg)
1029 call assert_fails('lua vim.v.errmsg = Nil', 'variable is fixed')
1030 let v:oldfiles = ['one', 'two']
1031 call assert_equal(['one', 'two'], luaeval('vim.v.oldfiles'))
1032 lua vim.v.oldfiles = vim.list({})
1033 call assert_equal([], v:oldfiles)
1034 call assert_equal(v:null, luaeval('vim.v.null'))
1035 call assert_fails('lua vim.v.argv[1] = Nil', 'list is locked')
1036 call assert_fails('lua vim.v.newvar = 1', 'Dictionary is locked')
1037endfunc
1038
1039" Test for adding, accessing and modifying window-local variables using the
1040" vim.w Lua table
1041func Test_lua_window_var_table()
1042 " Access window variables with different types of values
1043 new
1044 let w:wvar1 = 10
1045 let w:wvar2 = 'edit'
1046 let w:wvar3 = 3.14
1047 let w:wvar4 = 0zdeadbeef
1048 let w:wvar5 = ['a', 'b']
1049 let w:wvar6 = #{action: 'run'}
1050 call assert_equal(10, luaeval('vim.w.wvar1'))
1051 call assert_equal('edit', luaeval('vim.w.wvar2'))
1052 call assert_equal(3.14, luaeval('vim.w.wvar3'))
1053 call assert_equal(0zdeadbeef, luaeval('vim.w.wvar4'))
1054 call assert_equal(['a', 'b'], luaeval('vim.w.wvar5'))
1055 call assert_equal(#{action: 'run'}, luaeval('vim.w.wvar6'))
1056 call assert_equal(v:null, luaeval('vim.w.NonExisting'))
1057
1058 " modify a window variable
1059 lua vim.w.wvar2 = 'paste'
1060 call assert_equal('paste', w:wvar2)
1061
1062 " change the type stored in a variable
1063 let w:wvar2 = [1, 2]
1064 lua vim.w.wvar2 = vim.dict({a=10, b=20})
1065 call assert_equal(#{a: 10, b: 20}, w:wvar2)
1066
1067 " create a new window variable
1068 lua vim.w.wvar7 = vim.dict({a=vim.list({1, 2}), b=20})
1069 call assert_equal(#{a: [1, 2], b: 20}, w:wvar7)
1070
1071 " delete a window variable
1072 lua vim.w.wvar2 = Nil
1073 call assert_false(exists('w:wvar2'))
1074
1075 new
1076 call assert_equal(v:null, luaeval('vim.w.wvar1'))
1077 call assert_equal(v:null, luaeval('vim.w.wvar2'))
1078 %bw!
1079endfunc
1080
1081" Test for adding, accessing and modifying buffer-local variables using the
1082" vim.b Lua table
1083func Test_lua_buffer_var_table()
1084 " Access buffer variables with different types of values
1085 let b:bvar1 = 10
1086 let b:bvar2 = 'edit'
1087 let b:bvar3 = 3.14
1088 let b:bvar4 = 0zdeadbeef
1089 let b:bvar5 = ['a', 'b']
1090 let b:bvar6 = #{action: 'run'}
1091 call assert_equal(10, luaeval('vim.b.bvar1'))
1092 call assert_equal('edit', luaeval('vim.b.bvar2'))
1093 call assert_equal(3.14, luaeval('vim.b.bvar3'))
1094 call assert_equal(0zdeadbeef, luaeval('vim.b.bvar4'))
1095 call assert_equal(['a', 'b'], luaeval('vim.b.bvar5'))
1096 call assert_equal(#{action: 'run'}, luaeval('vim.b.bvar6'))
1097 call assert_equal(v:null, luaeval('vim.b.NonExisting'))
1098
1099 " modify a buffer variable
1100 lua vim.b.bvar2 = 'paste'
1101 call assert_equal('paste', b:bvar2)
1102
1103 " change the type stored in a variable
1104 let b:bvar2 = [1, 2]
1105 lua vim.b.bvar2 = vim.dict({a=10, b=20})
1106 call assert_equal(#{a: 10, b: 20}, b:bvar2)
1107
1108 " create a new buffer variable
1109 lua vim.b.bvar7 = vim.dict({a=vim.list({1, 2}), b=20})
1110 call assert_equal(#{a: [1, 2], b: 20}, b:bvar7)
1111
1112 " delete a buffer variable
1113 lua vim.b.bvar2 = Nil
1114 call assert_false(exists('b:bvar2'))
1115
1116 new
1117 call assert_equal(v:null, luaeval('vim.b.bvar1'))
1118 call assert_equal(v:null, luaeval('vim.b.bvar2'))
1119 %bw!
1120endfunc
1121
1122" Test for adding, accessing and modifying tabpage-local variables using the
1123" vim.t Lua table
1124func Test_lua_tabpage_var_table()
1125 " Access tabpage variables with different types of values
1126 let t:tvar1 = 10
1127 let t:tvar2 = 'edit'
1128 let t:tvar3 = 3.14
1129 let t:tvar4 = 0zdeadbeef
1130 let t:tvar5 = ['a', 'b']
1131 let t:tvar6 = #{action: 'run'}
1132 call assert_equal(10, luaeval('vim.t.tvar1'))
1133 call assert_equal('edit', luaeval('vim.t.tvar2'))
1134 call assert_equal(3.14, luaeval('vim.t.tvar3'))
1135 call assert_equal(0zdeadbeef, luaeval('vim.t.tvar4'))
1136 call assert_equal(['a', 'b'], luaeval('vim.t.tvar5'))
1137 call assert_equal(#{action: 'run'}, luaeval('vim.t.tvar6'))
1138 call assert_equal(v:null, luaeval('vim.t.NonExisting'))
1139
1140 " modify a tabpage variable
1141 lua vim.t.tvar2 = 'paste'
1142 call assert_equal('paste', t:tvar2)
1143
1144 " change the type stored in a variable
1145 let t:tvar2 = [1, 2]
1146 lua vim.t.tvar2 = vim.dict({a=10, b=20})
1147 call assert_equal(#{a: 10, b: 20}, t:tvar2)
1148
1149 " create a new tabpage variable
1150 lua vim.t.tvar7 = vim.dict({a=vim.list({1, 2}), b=20})
1151 call assert_equal(#{a: [1, 2], b: 20}, t:tvar7)
1152
1153 " delete a tabpage variable
1154 lua vim.t.tvar2 = Nil
1155 call assert_false(exists('t:tvar2'))
1156
1157 tabnew
1158 call assert_equal(v:null, luaeval('vim.t.tvar1'))
1159 call assert_equal(v:null, luaeval('vim.t.tvar2'))
1160 %bw!
1161endfunc
1162
Yegappan Lakshmanan11328bc2021-08-06 21:34:38 +02001163" Test for vim.version()
1164func Test_lua_vim_version()
1165 lua << trim END
1166 vimver = vim.version()
1167 vimver_n = vimver.major * 100 + vimver.minor
1168 END
1169 call assert_equal(v:version, luaeval('vimver_n'))
1170endfunc
1171
1172" Test for running multiple commands using vim.command()
1173func Test_lua_multiple_commands()
1174 lua << trim END
1175 vim.command([[
1176 let Var1 = []
1177 for i in range(3)
1178 let Var1 += [#{name: 'x'}]
1179 endfor
1180 augroup Luagroup
1181 autocmd!
1182 autocmd User Luatest echo 'Hello'
1183 augroup END
1184 ]])
1185 END
1186 call assert_equal([{'name': 'x'}, {'name': 'x'}, {'name': 'x'}], Var1)
1187 call assert_true(exists('#Luagroup'))
1188 call assert_true(exists('#Luagroup#User#Luatest'))
1189 augroup Luagroup
1190 autocmd!
1191 augroup END
1192 augroup! Luagroup
1193endfunc
1194
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02001195" vim: shiftwidth=2 sts=2 expandtab