blob: e1ad5004bed5ab78c7f12bd987e07e8e248f4d8b [file] [log] [blame]
Bram Moolenaard58f03b2017-01-29 22:48:45 +01001" Tests for Lua.
Bram Moolenaard58f03b2017-01-29 22:48:45 +01002
Bram Moolenaarb46fecd2019-06-15 17:58:09 +02003source check.vim
4CheckFeature lua
Bram Moolenaar5feabe02020-01-30 18:24:53 +01005CheckFeature float
Bram Moolenaard58f03b2017-01-29 22:48:45 +01006
Bram Moolenaare165f632019-03-10 09:48:59 +01007func TearDown()
8 " Run garbage collection after each test to exercise luaV_setref().
9 call test_garbagecollect_now()
10endfunc
11
Bram Moolenaar4ff48142018-06-30 21:50:25 +020012" Check that switching to another buffer does not trigger ml_get error.
Bram Moolenaar5feabe02020-01-30 18:24:53 +010013func Test_lua_command_new_no_ml_get_error()
Bram Moolenaard58f03b2017-01-29 22:48:45 +010014 new
15 let wincount = winnr('$')
16 call setline(1, ['one', 'two', 'three'])
17 luado vim.command("new")
18 call assert_equal(wincount + 1, winnr('$'))
Bram Moolenaar4ff48142018-06-30 21:50:25 +020019 %bwipe!
20endfunc
21
22" Test vim.command()
Bram Moolenaar5feabe02020-01-30 18:24:53 +010023func Test_lua_command()
Bram Moolenaar4ff48142018-06-30 21:50:25 +020024 new
25 call setline(1, ['one', 'two', 'three'])
26 luado vim.command("1,2d_")
27 call assert_equal(['three'], getline(1, '$'))
Bram Moolenaard58f03b2017-01-29 22:48:45 +010028 bwipe!
Bram Moolenaar4ff48142018-06-30 21:50:25 +020029endfunc
30
31" Test vim.eval()
Bram Moolenaar5feabe02020-01-30 18:24:53 +010032func Test_lua_eval()
Bram Moolenaar4ff48142018-06-30 21:50:25 +020033 " lua.eval with a number
34 lua v = vim.eval('123')
35 call assert_equal('number', luaeval('vim.type(v)'))
Bram Moolenaareb04f082020-05-17 14:32:35 +020036 call assert_equal(123, luaeval('v'))
Bram Moolenaar4ff48142018-06-30 21:50:25 +020037
38 " lua.eval with a string
39 lua v = vim.eval('"abc"')
Bram Moolenaar02b31112019-08-31 22:16:38 +020040 call assert_equal('string', 'vim.type(v)'->luaeval())
Bram Moolenaar4ff48142018-06-30 21:50:25 +020041 call assert_equal('abc', luaeval('v'))
42
43 " lua.eval with a list
44 lua v = vim.eval("['a']")
45 call assert_equal('list', luaeval('vim.type(v)'))
46 call assert_equal(['a'], luaeval('v'))
47
48 " lua.eval with a dict
49 lua v = vim.eval("{'a':'b'}")
50 call assert_equal('dict', luaeval('vim.type(v)'))
51 call assert_equal({'a':'b'}, luaeval('v'))
52
Bram Moolenaarb7828692019-03-23 13:57:02 +010053 " lua.eval with a blob
54 lua v = vim.eval("0z00112233.deadbeef")
55 call assert_equal('blob', luaeval('vim.type(v)'))
56 call assert_equal(0z00112233.deadbeef, luaeval('v'))
57
Bram Moolenaar4ff48142018-06-30 21:50:25 +020058 call assert_fails('lua v = vim.eval(nil)',
59 \ "[string \"vim chunk\"]:1: bad argument #1 to 'eval' (string expected, got nil)")
60 call assert_fails('lua v = vim.eval(true)',
61 \ "[string \"vim chunk\"]:1: bad argument #1 to 'eval' (string expected, got boolean)")
62 call assert_fails('lua v = vim.eval({})',
63 \ "[string \"vim chunk\"]:1: bad argument #1 to 'eval' (string expected, got table)")
64 call assert_fails('lua v = vim.eval(print)',
65 \ "[string \"vim chunk\"]:1: bad argument #1 to 'eval' (string expected, got function)")
66 call assert_fails('lua v = vim.eval(vim.buffer())',
67 \ "[string \"vim chunk\"]:1: bad argument #1 to 'eval' (string expected, got userdata)")
68
69 lua v = nil
70endfunc
71
72" Test vim.window()
Bram Moolenaar5feabe02020-01-30 18:24:53 +010073func Test_lua_window()
Bram Moolenaar4ff48142018-06-30 21:50:25 +020074 e Xfoo2
75 new Xfoo1
76
77 " Window 1 (top window) contains Xfoo1
78 " Window 2 (bottom window) contains Xfoo2
79 call assert_equal('Xfoo1', luaeval('vim.window(1):buffer().name'))
80 call assert_equal('Xfoo2', luaeval('vim.window(2):buffer().name'))
81
82 " Window 3 does not exist so vim.window(3) should return nil
83 call assert_equal('nil', luaeval('tostring(vim.window(3))'))
84
85 %bwipe!
86endfunc
87
88" Test vim.window().height
Bram Moolenaar5feabe02020-01-30 18:24:53 +010089func Test_lua_window_height()
Bram Moolenaar4ff48142018-06-30 21:50:25 +020090 new
91 lua vim.window().height = 2
92 call assert_equal(2, winheight(0))
93 lua vim.window().height = vim.window().height + 1
94 call assert_equal(3, winheight(0))
95 bwipe!
96endfunc
97
98" Test vim.window().width
Bram Moolenaar5feabe02020-01-30 18:24:53 +010099func Test_lua_window_width()
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200100 vert new
101 lua vim.window().width = 2
102 call assert_equal(2, winwidth(0))
103 lua vim.window().width = vim.window().width + 1
104 call assert_equal(3, winwidth(0))
105 bwipe!
106endfunc
107
108" Test vim.window().line and vim.window.col
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100109func Test_lua_window_line_col()
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200110 new
111 call setline(1, ['line1', 'line2', 'line3'])
112 lua vim.window().line = 2
113 lua vim.window().col = 4
114 call assert_equal([0, 2, 4, 0], getpos('.'))
115 lua vim.window().line = vim.window().line + 1
116 lua vim.window().col = vim.window().col - 1
117 call assert_equal([0, 3, 3, 0], getpos('.'))
118
119 call assert_fails('lua vim.window().line = 10',
120 \ '[string "vim chunk"]:1: line out of range')
121 bwipe!
122endfunc
123
Bram Moolenaareb04f082020-05-17 14:32:35 +0200124" Test vim.call
125func Test_lua_call()
126 call assert_equal(has('lua'), luaeval('vim.call("has", "lua")'))
127 call assert_equal(printf("Hello %s", "vim"), luaeval('vim.call("printf", "Hello %s", "vim")'))
128endfunc
129
130" Test vim.fn.*
131func Test_lua_fn()
132 call assert_equal(has('lua'), luaeval('vim.fn.has("lua")'))
133 call assert_equal(printf("Hello %s", "vim"), luaeval('vim.fn.printf("Hello %s", "vim")'))
134endfunc
135
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200136" Test setting the current window
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100137func Test_lua_window_set_current()
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200138 new Xfoo1
139 lua w1 = vim.window()
140 new Xfoo2
141 lua w2 = vim.window()
142
143 call assert_equal('Xfoo2', bufname('%'))
144 lua w1()
145 call assert_equal('Xfoo1', bufname('%'))
146 lua w2()
147 call assert_equal('Xfoo2', bufname('%'))
148
Bram Moolenaar2f362bf2018-07-01 19:49:27 +0200149 lua w1, w2 = nil
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200150 %bwipe!
151endfunc
152
153" Test vim.window().buffer
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100154func Test_lua_window_buffer()
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200155 new Xfoo1
156 lua w1 = vim.window()
157 lua b1 = w1.buffer()
158 new Xfoo2
159 lua w2 = vim.window()
160 lua b2 = w2.buffer()
161
162 lua b1()
163 call assert_equal('Xfoo1', bufname('%'))
164 lua b2()
165 call assert_equal('Xfoo2', bufname('%'))
166
Bram Moolenaar2f362bf2018-07-01 19:49:27 +0200167 lua b1, b2, w1, w2 = nil
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200168 %bwipe!
169endfunc
170
171" Test vim.window():previous() and vim.window():next()
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100172func Test_lua_window_next_previous()
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200173 new Xfoo1
174 new Xfoo2
175 new Xfoo3
176 wincmd j
177
178 call assert_equal('Xfoo2', luaeval('vim.window().buffer().name'))
179 call assert_equal('Xfoo1', luaeval('vim.window():next():buffer().name'))
180 call assert_equal('Xfoo3', luaeval('vim.window():previous():buffer().name'))
181
182 %bwipe!
183endfunc
184
185" Test vim.window():isvalid()
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100186func Test_lua_window_isvalid()
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200187 new Xfoo
188 lua w = vim.window()
189 call assert_true(luaeval('w:isvalid()'))
190
191 " FIXME: how to test the case when isvalid() returns v:false?
192 " isvalid() gives errors when the window is deleted. Is it a bug?
193
194 lua w = nil
195 bwipe!
196endfunc
197
198" Test vim.buffer() with and without argument
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100199func Test_lua_buffer()
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200200 new Xfoo1
201 let bn1 = bufnr('%')
202 new Xfoo2
203 let bn2 = bufnr('%')
204
205 " Test vim.buffer() without argument.
206 call assert_equal('Xfoo2', luaeval("vim.buffer().name"))
207
208 " Test vim.buffer() with string argument.
209 call assert_equal('Xfoo1', luaeval("vim.buffer('Xfoo1').name"))
210 call assert_equal('Xfoo2', luaeval("vim.buffer('Xfoo2').name"))
211
212 " Test vim.buffer() with integer argument.
213 call assert_equal('Xfoo1', luaeval("vim.buffer(" . bn1 . ").name"))
214 call assert_equal('Xfoo2', luaeval("vim.buffer(" . bn2 . ").name"))
215
Bram Moolenaar2f362bf2018-07-01 19:49:27 +0200216 lua bn1, bn2 = nil
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200217 %bwipe!
218endfunc
219
220" Test vim.buffer().name and vim.buffer().fname
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100221func Test_lua_buffer_name()
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200222 new
Bram Moolenaarfe08df42018-07-07 23:07:41 +0200223 call assert_equal('', luaeval('vim.buffer().name'))
224 call assert_equal('', luaeval('vim.buffer().fname'))
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200225 bwipe!
226
227 new Xfoo
228 call assert_equal('Xfoo', luaeval('vim.buffer().name'))
229 call assert_equal(expand('%:p'), luaeval('vim.buffer().fname'))
230 bwipe!
231endfunc
232
233" Test vim.buffer().number
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100234func Test_lua_buffer_number()
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200235 " All numbers in Lua are floating points number (no integers).
236 call assert_equal(bufnr('%'), float2nr(luaeval('vim.buffer().number')))
237endfunc
238
239" Test inserting lines in buffer.
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100240func Test_lua_buffer_insert()
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200241 new
242 lua vim.buffer()[1] = '3'
243 lua vim.buffer():insert('1', 0)
244 lua vim.buffer():insert('2', 1)
245 lua vim.buffer():insert('4', 10)
246
247 call assert_equal(['1', '2', '3', '4'], getline(1, '$'))
248 bwipe!
249endfunc
250
251" Test deleting line in buffer
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100252func Test_lua_buffer_delete()
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200253 new
254 call setline(1, ['1', '2', '3'])
255 lua vim.buffer()[2] = nil
256 call assert_equal(['1', '3'], getline(1, '$'))
257
258 call assert_fails('lua vim.buffer()[3] = nil',
259 \ '[string "vim chunk"]:1: invalid line number')
260 bwipe!
261endfunc
262
263" Test #vim.buffer() i.e. number of lines in buffer
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100264func Test_lua_buffer_number_lines()
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200265 new
266 call setline(1, ['a', 'b', 'c'])
Bram Moolenaareb04f082020-05-17 14:32:35 +0200267 call assert_equal(3, luaeval('#vim.buffer()'))
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200268 bwipe!
269endfunc
270
271" Test vim.buffer():next() and vim.buffer():previous()
272" Note that these functions get the next or previous buffers
273" but do not switch buffer.
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100274func Test_lua_buffer_next_previous()
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200275 new Xfoo1
276 new Xfoo2
277 new Xfoo3
278 b Xfoo2
279
280 lua bn = vim.buffer():next()
281 lua bp = vim.buffer():previous()
282
283 call assert_equal('Xfoo2', luaeval('vim.buffer().name'))
284 call assert_equal('Xfoo1', luaeval('bp.name'))
285 call assert_equal('Xfoo3', luaeval('bn.name'))
286
287 call assert_equal('Xfoo2', bufname('%'))
288
289 lua bn()
290 call assert_equal('Xfoo3', luaeval('vim.buffer().name'))
291 call assert_equal('Xfoo3', bufname('%'))
292
293 lua bp()
294 call assert_equal('Xfoo1', luaeval('vim.buffer().name'))
295 call assert_equal('Xfoo1', bufname('%'))
296
Bram Moolenaar2f362bf2018-07-01 19:49:27 +0200297 lua bn, bp = nil
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200298 %bwipe!
299endfunc
300
301" Test vim.buffer():isvalid()
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100302func Test_lua_buffer_isvalid()
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200303 new Xfoo
304 lua b = vim.buffer()
305 call assert_true(luaeval('b:isvalid()'))
306
307 " FIXME: how to test the case when isvalid() returns v:false?
308 " isvalid() gives errors when the buffer is wiped. Is it a bug?
309
310 lua b = nil
311 bwipe!
312endfunc
313
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100314func Test_lua_list()
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200315 call assert_equal([], luaeval('vim.list()'))
316
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200317 let l = []
318 lua l = vim.eval('l')
319 lua l:add(123)
320 lua l:add('abc')
321 lua l:add(true)
322 lua l:add(false)
Bram Moolenaar9067cd62019-01-01 00:41:54 +0100323 lua l:add(nil)
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200324 lua l:add(vim.eval("[1, 2, 3]"))
325 lua l:add(vim.eval("{'a':1, 'b':2, 'c':3}"))
Bram Moolenaareb04f082020-05-17 14:32:35 +0200326 call assert_equal([123, 'abc', v:true, v:false, v:null, [1, 2, 3], {'a': 1, 'b': 2, 'c': 3}], l)
327 call assert_equal(7, luaeval('#l'))
Bram Moolenaara8a60d02018-07-02 22:54:36 +0200328 call assert_match('^list: \%(0x\)\?\x\+$', luaeval('tostring(l)'))
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200329
Bram Moolenaarbd846172020-06-27 12:32:57 +0200330 lua l[1] = 124
331 lua l[6] = nil
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200332 lua l:insert('first')
333 lua l:insert('xx', 3)
Bram Moolenaareb04f082020-05-17 14:32:35 +0200334 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 +0200335
Bram Moolenaar2f362bf2018-07-01 19:49:27 +0200336 lockvar 1 l
337 call assert_fails('lua l:add("x")', '[string "vim chunk"]:1: list is locked')
338
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200339 lua l = nil
340endfunc
341
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100342func Test_lua_list_table()
Bram Moolenaar2f362bf2018-07-01 19:49:27 +0200343 " See :help lua-vim
344 " Non-numeric keys should not be used to initialize the list
345 " so say = 'hi' should be ignored.
346 lua t = {3.14, 'hello', false, true, say = 'hi'}
347 call assert_equal([3.14, 'hello', v:false, v:true], luaeval('vim.list(t)'))
348 lua t = nil
349
350 call assert_fails('lua vim.list(1)', '[string "vim chunk"]:1: table expected, got number')
351 call assert_fails('lua vim.list("x")', '[string "vim chunk"]:1: table expected, got string')
352 call assert_fails('lua vim.list(print)', '[string "vim chunk"]:1: table expected, got function')
353 call assert_fails('lua vim.list(true)', '[string "vim chunk"]:1: table expected, got boolean')
354endfunc
355
Bram Moolenaara1f9f862020-06-28 22:41:26 +0200356func Test_lua_list_table_insert_remove()
357 let luaver = split(split(luaeval('_VERSION'), ' ')[1], '\.')
358 let major = str2nr(luaver[0])
359 let minor = str2nr(luaver[1])
360
361 if major < 5 || (major == 5 && minor < 3)
362 throw 'Skipped: Lua version < 5.3'
363 endif
364
365 let l = [1, 2]
366 lua t = vim.eval('l')
367 lua table.insert(t, 10)
368 lua t[#t + 1] = 20
369 lua table.insert(t, 2, 30)
370 call assert_equal(l, [1, 30, 2, 10, 20])
371 lua table.remove(t, 2)
372 call assert_equal(l, [1, 2, 10, 20])
373 lua t[3] = nil
374 call assert_equal(l, [1, 2, 20])
375 lua removed_value = table.remove(t, 3)
376 call assert_equal(luaeval('removed_value'), 20)
377 lua t = nil
378 lua removed_value = nil
379 unlet l
380endfunc
381
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200382" Test l() i.e. iterator on list
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100383func Test_lua_list_iter()
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200384 lua l = vim.list():add('foo'):add('bar')
385 lua str = ''
386 lua for v in l() do str = str .. v end
387 call assert_equal('foobar', luaeval('str'))
388
Bram Moolenaar2f362bf2018-07-01 19:49:27 +0200389 lua str, l = nil
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200390endfunc
391
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100392func Test_lua_recursive_list()
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200393 lua l = vim.list():add(1):add(2)
394 lua l = l:add(l)
395
Bram Moolenaarbd846172020-06-27 12:32:57 +0200396 call assert_equal(1, luaeval('l[1]'))
397 call assert_equal(2, luaeval('l[2]'))
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200398
Bram Moolenaarbd846172020-06-27 12:32:57 +0200399 call assert_equal(1, luaeval('l[3][1]'))
400 call assert_equal(2, luaeval('l[3][2]'))
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200401
Bram Moolenaarbd846172020-06-27 12:32:57 +0200402 call assert_equal(1, luaeval('l[3][3][1]'))
403 call assert_equal(2, luaeval('l[3][3][2]'))
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200404
Bram Moolenaareb04f082020-05-17 14:32:35 +0200405 call assert_equal('[1, 2, [...]]', string(luaeval('l')))
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200406
Bram Moolenaara8a60d02018-07-02 22:54:36 +0200407 call assert_match('^list: \%(0x\)\?\x\+$', luaeval('tostring(l)'))
Bram Moolenaarbd846172020-06-27 12:32:57 +0200408 call assert_equal(luaeval('tostring(l)'), luaeval('tostring(l[3])'))
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200409
Bram Moolenaarbd846172020-06-27 12:32:57 +0200410 call assert_equal(luaeval('l'), luaeval('l[3]'))
411 call assert_equal(luaeval('l'), luaeval('l[3][3]'))
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200412
413 lua l = nil
414endfunc
415
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100416func Test_lua_dict()
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200417 call assert_equal({}, luaeval('vim.dict()'))
418
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200419 let d = {}
420 lua d = vim.eval('d')
421 lua d[0] = 123
422 lua d[1] = "abc"
423 lua d[2] = true
424 lua d[3] = false
425 lua d[4] = vim.eval("[1, 2, 3]")
426 lua d[5] = vim.eval("{'a':1, 'b':2, 'c':3}")
Bram Moolenaareb04f082020-05-17 14:32:35 +0200427 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)
428 call assert_equal(6, luaeval('#d'))
Bram Moolenaara8a60d02018-07-02 22:54:36 +0200429 call assert_match('^dict: \%(0x\)\?\x\+$', luaeval('tostring(d)'))
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200430
431 call assert_equal('abc', luaeval('d[1]'))
432
433 lua d[0] = 124
434 lua d[4] = nil
Bram Moolenaareb04f082020-05-17 14:32:35 +0200435 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 +0200436
Bram Moolenaar2f362bf2018-07-01 19:49:27 +0200437 lockvar 1 d
438 call assert_fails('lua d[6] = 1', '[string "vim chunk"]:1: dict is locked')
439
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200440 lua d = nil
441endfunc
442
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100443func Test_lua_dict_table()
Bram Moolenaar2f362bf2018-07-01 19:49:27 +0200444 lua t = {key1 = 'x', key2 = 3.14, key3 = true, key4 = false}
445 call assert_equal({'key1': 'x', 'key2': 3.14, 'key3': v:true, 'key4': v:false},
446 \ luaeval('vim.dict(t)'))
447
448 " Same example as in :help lua-vim.
449 lua t = {math.pi, false, say = 'hi'}
450 " FIXME: commented out as it currently does not work as documented:
451 " Expected {'say': 'hi'}
452 " but got {'1': 3.141593, '2': v:false, 'say': 'hi'}
453 " Is the documentation or the code wrong?
454 "call assert_equal({'say' : 'hi'}, luaeval('vim.dict(t)'))
455 lua t = nil
456
457 call assert_fails('lua vim.dict(1)', '[string "vim chunk"]:1: table expected, got number')
458 call assert_fails('lua vim.dict("x")', '[string "vim chunk"]:1: table expected, got string')
459 call assert_fails('lua vim.dict(print)', '[string "vim chunk"]:1: table expected, got function')
460 call assert_fails('lua vim.dict(true)', '[string "vim chunk"]:1: table expected, got boolean')
461endfunc
462
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200463" Test d() i.e. iterator on dictionary
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100464func Test_lua_dict_iter()
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200465 let d = {'a': 1, 'b':2}
466 lua d = vim.eval('d')
467 lua str = ''
468 lua for k,v in d() do str = str .. k ..':' .. v .. ',' end
469 call assert_equal('a:1,b:2,', luaeval('str'))
470
Bram Moolenaar2f362bf2018-07-01 19:49:27 +0200471 lua str, d = nil
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200472endfunc
473
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100474func Test_lua_blob()
Bram Moolenaarb7828692019-03-23 13:57:02 +0100475 call assert_equal(0z, luaeval('vim.blob("")'))
476 call assert_equal(0z31326162, luaeval('vim.blob("12ab")'))
477 call assert_equal(0z00010203, luaeval('vim.blob("\x00\x01\x02\x03")'))
478 call assert_equal(0z8081FEFF, luaeval('vim.blob("\x80\x81\xfe\xff")'))
479
480 lua b = vim.blob("\x00\x00\x00\x00")
481 call assert_equal(0z00000000, luaeval('b'))
Bram Moolenaareb04f082020-05-17 14:32:35 +0200482 call assert_equal(4, luaeval('#b'))
Bram Moolenaarb7828692019-03-23 13:57:02 +0100483 lua b[0], b[1], b[2], b[3] = 1, 32, 256, 0xff
484 call assert_equal(0z012000ff, luaeval('b'))
485 lua b[4] = string.byte("z", 1)
486 call assert_equal(0z012000ff.7a, luaeval('b'))
Bram Moolenaareb04f082020-05-17 14:32:35 +0200487 call assert_equal(5, luaeval('#b'))
Bram Moolenaarb7828692019-03-23 13:57:02 +0100488 call assert_fails('lua b[#b+1] = 0x80', '[string "vim chunk"]:1: index out of range')
489 lua b:add("12ab")
490 call assert_equal(0z012000ff.7a313261.62, luaeval('b'))
Bram Moolenaareb04f082020-05-17 14:32:35 +0200491 call assert_equal(9, luaeval('#b'))
Bram Moolenaarb7828692019-03-23 13:57:02 +0100492 call assert_fails('lua b:add(nil)', '[string "vim chunk"]:1: string expected, got nil')
493 call assert_fails('lua b:add(true)', '[string "vim chunk"]:1: string expected, got boolean')
494 call assert_fails('lua b:add({})', '[string "vim chunk"]:1: string expected, got table')
495 lua b = nil
496endfunc
497
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100498func Test_lua_funcref()
Bram Moolenaarca06da92018-07-01 15:12:05 +0200499 function I(x)
500 return a:x
501 endfunction
502 let R = function('I')
503 lua i1 = vim.funcref"I"
504 lua i2 = vim.eval"R"
505 lua msg = "funcref|test|" .. (#i2(i1) == #i1(i2) and "OK" or "FAIL")
506 lua msg = vim.funcref"tr"(msg, "|", " ")
507 call assert_equal("funcref test OK", luaeval('msg'))
508
509 " dict funcref
510 function Mylen() dict
511 return len(self.data)
512 endfunction
513 let l = [0, 1, 2, 3]
514 let mydict = {'data': l}
515 lua d = vim.eval"mydict"
516 lua d.len = vim.funcref"Mylen" -- assign d as 'self'
517 lua res = (d.len() == vim.funcref"len"(vim.eval"l")) and "OK" or "FAIL"
518 call assert_equal("OK", luaeval('res'))
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100519 call assert_equal(function('Mylen', {'data': l, 'len': function('Mylen')}), mydict.len)
Bram Moolenaar2f362bf2018-07-01 19:49:27 +0200520
521 lua i1, i2, msg, d, res = nil
Bram Moolenaarca06da92018-07-01 15:12:05 +0200522endfunc
523
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200524" Test vim.type()
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100525func Test_lua_type()
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200526 " The following values are identical to Lua's type function.
527 call assert_equal('string', luaeval('vim.type("foo")'))
528 call assert_equal('number', luaeval('vim.type(1)'))
529 call assert_equal('number', luaeval('vim.type(1.2)'))
530 call assert_equal('function', luaeval('vim.type(print)'))
531 call assert_equal('table', luaeval('vim.type({})'))
532 call assert_equal('boolean', luaeval('vim.type(true)'))
533 call assert_equal('boolean', luaeval('vim.type(false)'))
534 call assert_equal('nil', luaeval('vim.type(nil)'))
535
536 " The following values are specific to Vim.
537 call assert_equal('window', luaeval('vim.type(vim.window())'))
538 call assert_equal('buffer', luaeval('vim.type(vim.buffer())'))
539 call assert_equal('list', luaeval('vim.type(vim.list())'))
540 call assert_equal('dict', luaeval('vim.type(vim.dict())'))
Bram Moolenaarca06da92018-07-01 15:12:05 +0200541 call assert_equal('funcref', luaeval('vim.type(vim.funcref("Test_type"))'))
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200542endfunc
543
544" Test vim.open()
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100545func Test_lua_open()
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200546 call assert_notmatch('XOpen', execute('ls'))
547
548 " Open a buffer XOpen1, but do not jump to it.
549 lua b = vim.open('XOpen1')
550 call assert_equal('XOpen1', luaeval('b.name'))
551 call assert_equal('', bufname('%'))
552
553 call assert_match('XOpen1', execute('ls'))
554 call assert_notequal('XOpen2', bufname('%'))
555
556 " Open a buffer XOpen2 and jump to it.
557 lua b = vim.open('XOpen2')()
558 call assert_equal('XOpen2', luaeval('b.name'))
559 call assert_equal('XOpen2', bufname('%'))
560
561 lua b = nil
562 %bwipe!
563endfunc
564
Bram Moolenaar788fbb42020-05-31 14:08:12 +0200565func Test_update_package_paths()
566 set runtimepath+=./testluaplugin
567 call assert_equal("hello from lua", luaeval("require('testluaplugin').hello()"))
568endfunc
569
Bram Moolenaar801ab062020-06-25 19:27:56 +0200570func Vim_func_call_lua_callback(Concat, Cb)
571 let l:message = a:Concat("hello", "vim")
572 call a:Cb(l:message)
573endfunc
574
575func Test_pass_lua_callback_to_vim_from_lua()
576 lua pass_lua_callback_to_vim_from_lua_result = ""
577 call assert_equal("", luaeval("pass_lua_callback_to_vim_from_lua_result"))
578 lua <<EOF
579 vim.funcref('Vim_func_call_lua_callback')(
580 function(greeting, message)
581 return greeting .. " " .. message
582 end,
583 function(message)
584 pass_lua_callback_to_vim_from_lua_result = message
585 end)
586EOF
587 call assert_equal("hello vim", luaeval("pass_lua_callback_to_vim_from_lua_result"))
588endfunc
589
590func Vim_func_call_metatable_lua_callback(Greet)
591 return a:Greet("world")
592endfunc
593
594func Test_pass_lua_metatable_callback_to_vim_from_lua()
595 let result = luaeval("vim.funcref('Vim_func_call_metatable_lua_callback')(setmetatable({ space = ' '}, { __call = function(tbl, msg) return 'hello' .. tbl.space .. msg end }) )")
596 call assert_equal("hello world", result)
597endfunc
598
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200599" Test vim.line()
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100600func Test_lua_line()
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200601 new
602 call setline(1, ['first line', 'second line'])
603 1
604 call assert_equal('first line', luaeval('vim.line()'))
605 2
606 call assert_equal('second line', luaeval('vim.line()'))
607 bwipe!
608endfunc
609
610" Test vim.beep()
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100611func Test_lua_beep()
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200612 call assert_beeps('lua vim.beep()')
613endfunc
614
615" Test errors in luaeval()
616func Test_luaeval_error()
617 " Compile error
618 call assert_fails("call luaeval('-nil')",
619 \ '[string "luaeval"]:1: attempt to perform arithmetic on a nil value')
620 call assert_fails("call luaeval(']')",
621 \ "[string \"luaeval\"]:1: unexpected symbol near ']'")
622endfunc
623
624" Test :luafile foo.lua
625func Test_luafile()
626 call delete('Xlua_file')
Bram Moolenaareb04f082020-05-17 14:32:35 +0200627 call writefile(["str = 'hello'", "num = 123" ], 'Xlua_file')
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200628 call setfperm('Xlua_file', 'r-xr-xr-x')
629
630 luafile Xlua_file
631 call assert_equal('hello', luaeval('str'))
Bram Moolenaareb04f082020-05-17 14:32:35 +0200632 call assert_equal(123, luaeval('num'))
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200633
Bram Moolenaar2f362bf2018-07-01 19:49:27 +0200634 lua str, num = nil
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200635 call delete('Xlua_file')
636endfunc
637
638" Test :luafile %
639func Test_luafile_percent()
640 new Xlua_file
641 append
642 str, num = 'foo', 321.0
643 print(string.format('str=%s, num=%d', str, num))
644.
645 w!
646 luafile %
647 let msg = split(execute('message'), "\n")[-1]
648 call assert_equal('str=foo, num=321', msg)
649
Bram Moolenaar2f362bf2018-07-01 19:49:27 +0200650 lua str, num = nil
651 call delete('Xlua_file')
652 bwipe!
653endfunc
654
655" Test :luafile with syntax error
656func Test_luafile_error()
657 new Xlua_file
658 call writefile(['nil = 0' ], 'Xlua_file')
659 call setfperm('Xlua_file', 'r-xr-xr-x')
660
661 call assert_fails('luafile Xlua_file', "Xlua_file:1: unexpected symbol near 'nil'")
662
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200663 call delete('Xlua_file')
Bram Moolenaard58f03b2017-01-29 22:48:45 +0100664 bwipe!
665endfunc
Bram Moolenaar53901442018-07-25 22:02:36 +0200666
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100667func Test_lua_set_cursor()
Bram Moolenaar53901442018-07-25 22:02:36 +0200668 " Check that setting the cursor position works.
669 new
670 call setline(1, ['first line', 'second line'])
671 normal gg
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200672 lua << trim EOF
673 w = vim.window()
674 w.line = 1
675 w.col = 5
676 EOF
Bram Moolenaar53901442018-07-25 22:02:36 +0200677 call assert_equal([1, 5], [line('.'), col('.')])
678
679 " Check that movement after setting cursor position keeps current column.
680 normal j
681 call assert_equal([2, 5], [line('.'), col('.')])
682endfunc
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200683
684" Test for various heredoc syntax
685func Test_lua_heredoc()
686 lua << END
687vim.command('let s = "A"')
688END
689 lua <<
690vim.command('let s ..= "B"')
691.
692 lua << trim END
693 vim.command('let s ..= "C"')
694 END
695 lua << trim
696 vim.command('let s ..= "D"')
697 .
Bram Moolenaar6ab09532020-05-01 14:10:13 +0200698 lua << trim eof
699 vim.command('let s ..= "E"')
700 eof
701 call assert_equal('ABCDE', s)
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200702endfunc
703
704" vim: shiftwidth=2 sts=2 expandtab