xref: /vim-8.2.3635/src/testdir/test_lua.vim (revision f65ed86f)
1" Tests for Lua.
2
3source check.vim
4
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
12CheckFeature lua
13CheckFeature float
14
15" Depending on the lua version, the error messages are different.
16let s:luaver = split(split(systemlist('lua -v')[0], ' ')[1], '\.')
17let s:major = str2nr(s:luaver[0])
18let s:minor = str2nr(s:luaver[1])
19let s:patch = str2nr(s:luaver[2])
20let s:lua_53_or_later = 0
21let s:lua_543_or_later = 0
22if (s:major == 5 && s:minor >= 3) || s:major > 5
23  let s:lua_53_or_later = 1
24  if (s:major == 5
25        \ && ((s:minor == 4 && s:patch >= 3) || s:minor > 4))
26        \ || s:major > 5
27    let s:lua_543_or_later = 1
28  endif
29endif
30
31func TearDown()
32  " Run garbage collection after each test to exercise luaV_setref().
33  call test_garbagecollect_now()
34endfunc
35
36" Check that switching to another buffer does not trigger ml_get error.
37func Test_lua_command_new_no_ml_get_error()
38  new
39  let wincount = winnr('$')
40  call setline(1, ['one', 'two', 'three'])
41  luado vim.command("new")
42  call assert_equal(wincount + 1, winnr('$'))
43  %bwipe!
44endfunc
45
46" Test vim.command()
47func Test_lua_command()
48  new
49  call setline(1, ['one', 'two', 'three'])
50  luado vim.command("1,2d_")
51  call assert_equal(['three'], getline(1, '$'))
52  bwipe!
53endfunc
54
55func Test_lua_luado()
56  new
57  call setline(1, ['one', 'two'])
58  luado return(linenr)
59  call assert_equal(['1', '2'], getline(1, '$'))
60  close!
61
62  " Error cases
63  call assert_fails('luado string.format()',
64        \ "[string \"vim chunk\"]:1: bad argument #1 to 'format' (string expected, got no value)")
65  if s:lua_543_or_later
66    let msg = "[string \"vim chunk\"]:1: global 'func' is not callable (a nil value)"
67  elseif s:lua_53_or_later
68    let msg = "[string \"vim chunk\"]:1: attempt to call a nil value (global 'func')"
69  else
70    let msg = "[string \"vim chunk\"]:1: attempt to call global 'func' (a nil value)"
71  endif
72  call assert_fails('luado func()', msg)
73  call assert_fails('luado error("failed")', "[string \"vim chunk\"]:1: failed")
74endfunc
75
76" Test vim.eval()
77func Test_lua_eval()
78  " lua.eval with a number
79  lua v = vim.eval('123')
80  call assert_equal('number', luaeval('vim.type(v)'))
81  call assert_equal(123, luaeval('v'))
82
83  " lua.eval with a string
84  lua v = vim.eval('"abc"')
85  call assert_equal('string', 'vim.type(v)'->luaeval())
86  call assert_equal('abc', luaeval('v'))
87
88  " lua.eval with a list
89  lua v = vim.eval("['a']")
90  call assert_equal('list', luaeval('vim.type(v)'))
91  call assert_equal(['a'], luaeval('v'))
92
93  " lua.eval with a dict
94  lua v = vim.eval("{'a':'b'}")
95  call assert_equal('dict', luaeval('vim.type(v)'))
96  call assert_equal({'a':'b'}, luaeval('v'))
97
98  " lua.eval with a blob
99  lua v = vim.eval("0z00112233.deadbeef")
100  call assert_equal('blob', luaeval('vim.type(v)'))
101  call assert_equal(0z00112233.deadbeef, luaeval('v'))
102
103  " lua.eval with a float
104  lua v = vim.eval('3.14')
105  call assert_equal('number', luaeval('vim.type(v)'))
106  call assert_equal(3.14, luaeval('v'))
107
108  " lua.eval with a bool
109  lua v = vim.eval('v:true')
110  call assert_equal('number', luaeval('vim.type(v)'))
111  call assert_equal(1, luaeval('v'))
112  lua v = vim.eval('v:false')
113  call assert_equal('number', luaeval('vim.type(v)'))
114  call assert_equal(0, luaeval('v'))
115
116  " lua.eval with a null
117  lua v = vim.eval('v:null')
118  call assert_equal('nil', luaeval('vim.type(v)'))
119  call assert_equal(v:null, luaeval('v'))
120
121  call assert_fails('lua v = vim.eval(nil)',
122        \ "[string \"vim chunk\"]:1: bad argument #1 to 'eval' (string expected, got nil)")
123  call assert_fails('lua v = vim.eval(true)',
124        \ "[string \"vim chunk\"]:1: bad argument #1 to 'eval' (string expected, got boolean)")
125  call assert_fails('lua v = vim.eval({})',
126        \ "[string \"vim chunk\"]:1: bad argument #1 to 'eval' (string expected, got table)")
127  call assert_fails('lua v = vim.eval(print)',
128        \ "[string \"vim chunk\"]:1: bad argument #1 to 'eval' (string expected, got function)")
129  call assert_fails('lua v = vim.eval(vim.buffer())',
130        \ "[string \"vim chunk\"]:1: bad argument #1 to 'eval' (string expected, got userdata)")
131
132  lua v = nil
133endfunc
134
135" Test luaeval() with lambda
136func Test_luaeval_with_lambda()
137  lua function hello_luaeval_lambda(a, cb) return a .. cb() end
138  call assert_equal('helloworld',
139        \ luaeval('hello_luaeval_lambda(_A[1], _A[2])',
140        \         ['hello', {->'world'}]))
141  lua hello_luaeval_lambda = nil
142endfunc
143
144" Test vim.window()
145func Test_lua_window()
146  e Xfoo2
147  new Xfoo1
148
149  " Window 1 (top window) contains Xfoo1
150  " Window 2 (bottom window) contains Xfoo2
151  call assert_equal('Xfoo1', luaeval('vim.window(1):buffer().name'))
152  call assert_equal('Xfoo2', luaeval('vim.window(2):buffer().name'))
153
154  " Window 3 does not exist so vim.window(3) should return nil
155  call assert_equal('nil', luaeval('tostring(vim.window(3))'))
156
157  if s:lua_543_or_later
158    let msg = "[string \"luaeval\"]:1: field 'xyz' is not callable (a nil value)"
159  elseif s:lua_53_or_later
160    let msg = "[string \"luaeval\"]:1: attempt to call a nil value (field 'xyz')"
161  else
162    let msg = "[string \"luaeval\"]:1: attempt to call field 'xyz' (a nil value)"
163  endif
164  call assert_fails("let n = luaeval('vim.window().xyz()')", msg)
165  call assert_fails('lua vim.window().xyz = 1',
166        \ "[string \"vim chunk\"]:1: invalid window property: `xyz'")
167
168  %bwipe!
169endfunc
170
171" Test vim.window().height
172func Test_lua_window_height()
173  new
174  lua vim.window().height = 2
175  call assert_equal(2, winheight(0))
176  lua vim.window().height = vim.window().height + 1
177  call assert_equal(3, winheight(0))
178  bwipe!
179endfunc
180
181" Test vim.window().width
182func Test_lua_window_width()
183  vert new
184  lua vim.window().width = 2
185  call assert_equal(2, winwidth(0))
186  lua vim.window().width = vim.window().width + 1
187  call assert_equal(3, winwidth(0))
188  bwipe!
189endfunc
190
191" Test vim.window().line and vim.window.col
192func Test_lua_window_line_col()
193  new
194  call setline(1, ['line1', 'line2', 'line3'])
195  lua vim.window().line = 2
196  lua vim.window().col = 4
197  call assert_equal([0, 2, 4, 0], getpos('.'))
198  lua vim.window().line = vim.window().line + 1
199  lua vim.window().col = vim.window().col - 1
200  call assert_equal([0, 3, 3, 0], getpos('.'))
201
202  call assert_fails('lua vim.window().line = 10',
203        \           '[string "vim chunk"]:1: line out of range')
204  bwipe!
205endfunc
206
207" Test vim.call
208func Test_lua_call()
209  call assert_equal(has('lua'), luaeval('vim.call("has", "lua")'))
210  call assert_equal(printf("Hello %s", "vim"), luaeval('vim.call("printf", "Hello %s", "vim")'))
211
212  " Error cases
213  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)')",
214        \ s:lua_53_or_later
215        \ ? '[string "luaeval"]:1: Function called with too many arguments'
216        \ : 'Function called with too many arguments')
217  lua co = coroutine.create(function () print("hi") end)
218  call assert_fails("call luaeval('vim.call(\"type\", co)')",
219        \ s:lua_53_or_later
220        \ ? '[string "luaeval"]:1: lua: cannot convert value'
221        \ : 'lua: cannot convert value')
222  lua co = nil
223  call assert_fails("call luaeval('vim.call(\"abc\")')",
224        \ ['E117:', s:lua_53_or_later ? '\[string "luaeval"]:1: lua: call_vim_function failed'
225        \                             : 'lua: call_vim_function failed'])
226endfunc
227
228" Test vim.fn.*
229func Test_lua_fn()
230  call assert_equal(has('lua'), luaeval('vim.fn.has("lua")'))
231  call assert_equal(printf("Hello %s", "vim"), luaeval('vim.fn.printf("Hello %s", "vim")'))
232endfunc
233
234" Test setting the current window
235func Test_lua_window_set_current()
236  new Xfoo1
237  lua w1 = vim.window()
238  new Xfoo2
239  lua w2 = vim.window()
240
241  call assert_equal('Xfoo2', bufname('%'))
242  lua w1()
243  call assert_equal('Xfoo1', bufname('%'))
244  lua w2()
245  call assert_equal('Xfoo2', bufname('%'))
246
247  lua w1, w2 = nil
248  %bwipe!
249endfunc
250
251" Test vim.window().buffer
252func Test_lua_window_buffer()
253  new Xfoo1
254  lua w1 = vim.window()
255  lua b1 = w1.buffer()
256  new Xfoo2
257  lua w2 = vim.window()
258  lua b2 = w2.buffer()
259
260  lua b1()
261  call assert_equal('Xfoo1', bufname('%'))
262  lua b2()
263  call assert_equal('Xfoo2', bufname('%'))
264
265  lua b1, b2, w1, w2 = nil
266  %bwipe!
267endfunc
268
269" Test vim.window():previous() and vim.window():next()
270func Test_lua_window_next_previous()
271  new Xfoo1
272  new Xfoo2
273  new Xfoo3
274  wincmd j
275
276  call assert_equal('Xfoo2', luaeval('vim.window().buffer().name'))
277  call assert_equal('Xfoo1', luaeval('vim.window():next():buffer().name'))
278  call assert_equal('Xfoo3', luaeval('vim.window():previous():buffer().name'))
279
280  %bwipe!
281endfunc
282
283" Test vim.window():isvalid()
284func Test_lua_window_isvalid()
285  new Xfoo
286  lua w = vim.window()
287  call assert_true(luaeval('w:isvalid()'))
288
289  " FIXME: how to test the case when isvalid() returns v:false?
290  " isvalid() gives errors when the window is deleted. Is it a bug?
291
292  lua w = nil
293  bwipe!
294endfunc
295
296" Test vim.buffer() with and without argument
297func Test_lua_buffer()
298  new Xfoo1
299  let bn1 = bufnr('%')
300  new Xfoo2
301  let bn2 = bufnr('%')
302
303  " Test vim.buffer() without argument.
304  call assert_equal('Xfoo2', luaeval("vim.buffer().name"))
305
306  " Test vim.buffer() with string argument.
307  call assert_equal('Xfoo1', luaeval("vim.buffer('Xfoo1').name"))
308  call assert_equal('Xfoo2', luaeval("vim.buffer('Xfoo2').name"))
309
310  " Test vim.buffer() with integer argument.
311  call assert_equal('Xfoo1', luaeval("vim.buffer(" . bn1 . ").name"))
312  call assert_equal('Xfoo2', luaeval("vim.buffer(" . bn2 . ").name"))
313
314  lua bn1, bn2 = nil
315  %bwipe!
316endfunc
317
318" Test vim.buffer().name and vim.buffer().fname
319func Test_lua_buffer_name()
320  new
321  call assert_equal('', luaeval('vim.buffer().name'))
322  call assert_equal('', luaeval('vim.buffer().fname'))
323  bwipe!
324
325  new Xfoo
326  call assert_equal('Xfoo', luaeval('vim.buffer().name'))
327  call assert_equal(expand('%:p'), luaeval('vim.buffer().fname'))
328  bwipe!
329endfunc
330
331" Test vim.buffer().number
332func Test_lua_buffer_number()
333  " All numbers in Lua are floating points number (no integers).
334  call assert_equal(bufnr('%'), float2nr(luaeval('vim.buffer().number')))
335endfunc
336
337" Test inserting lines in buffer.
338func Test_lua_buffer_insert()
339  new
340  lua vim.buffer()[1] = '3'
341  lua vim.buffer():insert('1', 0)
342  lua vim.buffer():insert('2', 1)
343  lua vim.buffer():insert('4', 10)
344
345  call assert_equal(['1', '2', '3', '4'], getline(1, '$'))
346  call assert_equal('4', luaeval('vim.buffer()[4]'))
347  call assert_equal(v:null, luaeval('vim.buffer()[5]'))
348  call assert_equal(v:null, luaeval('vim.buffer()[{}]'))
349  if s:lua_543_or_later
350    let msg = "[string \"vim chunk\"]:1: method 'xyz' is not callable (a nil value)"
351  elseif s:lua_53_or_later
352    let msg = "[string \"vim chunk\"]:1: attempt to call a nil value (method 'xyz')"
353  else
354    let msg = "[string \"vim chunk\"]:1: attempt to call method 'xyz' (a nil value)"
355  endif
356  call assert_fails('lua vim.buffer():xyz()', msg)
357  call assert_fails('lua vim.buffer()[1] = {}',
358        \ '[string "vim chunk"]:1: wrong argument to change')
359  bwipe!
360endfunc
361
362" Test deleting line in buffer
363func Test_lua_buffer_delete()
364  new
365  call setline(1, ['1', '2', '3'])
366  call cursor(3, 1)
367  lua vim.buffer()[2] = nil
368  call assert_equal(['1', '3'], getline(1, '$'))
369
370  call assert_fails('lua vim.buffer()[3] = nil',
371        \           '[string "vim chunk"]:1: invalid line number')
372  bwipe!
373endfunc
374
375" Test #vim.buffer() i.e. number of lines in buffer
376func Test_lua_buffer_number_lines()
377  new
378  call setline(1, ['a', 'b', 'c'])
379  call assert_equal(3, luaeval('#vim.buffer()'))
380  bwipe!
381endfunc
382
383" Test vim.buffer():next() and vim.buffer():previous()
384" Note that these functions get the next or previous buffers
385" but do not switch buffer.
386func Test_lua_buffer_next_previous()
387  new Xfoo1
388  new Xfoo2
389  new Xfoo3
390  b Xfoo2
391
392  lua bn = vim.buffer():next()
393  lua bp = vim.buffer():previous()
394
395  call assert_equal('Xfoo2', luaeval('vim.buffer().name'))
396  call assert_equal('Xfoo1', luaeval('bp.name'))
397  call assert_equal('Xfoo3', luaeval('bn.name'))
398
399  call assert_equal('Xfoo2', bufname('%'))
400
401  lua bn()
402  call assert_equal('Xfoo3', luaeval('vim.buffer().name'))
403  call assert_equal('Xfoo3', bufname('%'))
404
405  lua bp()
406  call assert_equal('Xfoo1', luaeval('vim.buffer().name'))
407  call assert_equal('Xfoo1', bufname('%'))
408
409  lua bn, bp = nil
410  %bwipe!
411endfunc
412
413" Test vim.buffer():isvalid()
414func Test_lua_buffer_isvalid()
415  new Xfoo
416  lua b = vim.buffer()
417  call assert_true(luaeval('b:isvalid()'))
418
419  " FIXME: how to test the case when isvalid() returns v:false?
420  " isvalid() gives errors when the buffer is wiped. Is it a bug?
421
422  lua b = nil
423  bwipe!
424endfunc
425
426func Test_lua_list()
427  call assert_equal([], luaeval('vim.list()'))
428
429  let l = []
430  lua l = vim.eval('l')
431  lua l:add(123)
432  lua l:add('abc')
433  lua l:add(true)
434  lua l:add(false)
435  lua l:add(nil)
436  lua l:add(vim.eval("[1, 2, 3]"))
437  lua l:add(vim.eval("{'a':1, 'b':2, 'c':3}"))
438  call assert_equal([123, 'abc', v:true, v:false, v:null, [1, 2, 3], {'a': 1, 'b': 2, 'c': 3}], l)
439  call assert_equal(7, luaeval('#l'))
440  call assert_match('^list: \%(0x\)\?\x\+$', luaeval('tostring(l)'))
441
442  lua l[1] = 124
443  lua l[6] = nil
444  lua l:insert('first')
445  lua l:insert('xx', 3)
446  call assert_fails('lua l:insert("xx", -20)',
447        \ '[string "vim chunk"]:1: invalid position')
448  call assert_equal(['first', 124, 'abc', 'xx', v:true, v:false, v:null, {'a': 1, 'b': 2, 'c': 3}], l)
449
450  lockvar 1 l
451  call assert_fails('lua l:add("x")', '[string "vim chunk"]:1: list is locked')
452  call assert_fails('lua l:insert(2)', '[string "vim chunk"]:1: list is locked')
453  call assert_fails('lua l[9] = 1', '[string "vim chunk"]:1: list is locked')
454
455  unlockvar l
456  let l = [1, 2]
457  lua ll = vim.eval('l')
458  let x = luaeval("ll[3]")
459  call assert_equal(v:null, x)
460  if s:lua_543_or_later
461    let msg = "[string \"luaeval\"]:1: method 'xyz' is not callable (a nil value)"
462  elseif s:lua_53_or_later
463    let msg = "[string \"luaeval\"]:1: attempt to call a nil value (method 'xyz')"
464  else
465    let msg = "[string \"luaeval\"]:1: attempt to call method 'xyz' (a nil value)"
466  endif
467  call assert_fails('let x = luaeval("ll:xyz(3)")', msg)
468  let y = luaeval("ll[{}]")
469  call assert_equal(v:null, y)
470
471  lua l = nil
472endfunc
473
474func Test_lua_list_table()
475  " See :help lua-vim
476  " Non-numeric keys should not be used to initialize the list
477  " so say = 'hi' should be ignored.
478  lua t = {3.14, 'hello', false, true, say = 'hi'}
479  call assert_equal([3.14, 'hello', v:false, v:true], luaeval('vim.list(t)'))
480  lua t = nil
481
482  call assert_fails('lua vim.list(1)', '[string "vim chunk"]:1: table expected, got number')
483  call assert_fails('lua vim.list("x")', '[string "vim chunk"]:1: table expected, got string')
484  call assert_fails('lua vim.list(print)', '[string "vim chunk"]:1: table expected, got function')
485  call assert_fails('lua vim.list(true)', '[string "vim chunk"]:1: table expected, got boolean')
486endfunc
487
488func Test_lua_list_table_insert_remove()
489  if !s:lua_53_or_later
490    throw 'Skipped: Lua version < 5.3'
491  endif
492
493  let l = [1, 2]
494  lua t = vim.eval('l')
495  lua table.insert(t, 10)
496  lua t[#t + 1] = 20
497  lua table.insert(t, 2, 30)
498  call assert_equal(l, [1, 30, 2, 10, 20])
499  lua table.remove(t, 2)
500  call assert_equal(l, [1, 2, 10, 20])
501  lua t[3] = nil
502  call assert_equal(l, [1, 2, 20])
503  lua removed_value = table.remove(t, 3)
504  call assert_equal(luaeval('removed_value'), 20)
505  lua t = nil
506  lua removed_value = nil
507  unlet l
508endfunc
509
510" Test l() i.e. iterator on list
511func Test_lua_list_iter()
512  lua l = vim.list():add('foo'):add('bar')
513  lua str = ''
514  lua for v in l() do str = str .. v end
515  call assert_equal('foobar', luaeval('str'))
516
517  lua str, l = nil
518endfunc
519
520func Test_lua_recursive_list()
521  lua l = vim.list():add(1):add(2)
522  lua l = l:add(l)
523
524  call assert_equal(1, luaeval('l[1]'))
525  call assert_equal(2, luaeval('l[2]'))
526
527  call assert_equal(1, luaeval('l[3][1]'))
528  call assert_equal(2, luaeval('l[3][2]'))
529
530  call assert_equal(1, luaeval('l[3][3][1]'))
531  call assert_equal(2, luaeval('l[3][3][2]'))
532
533  call assert_equal('[1, 2, [...]]', string(luaeval('l')))
534
535  call assert_match('^list: \%(0x\)\?\x\+$', luaeval('tostring(l)'))
536  call assert_equal(luaeval('tostring(l)'), luaeval('tostring(l[3])'))
537
538  call assert_equal(luaeval('l'), luaeval('l[3]'))
539  call assert_equal(luaeval('l'), luaeval('l[3][3]'))
540
541  lua l = nil
542endfunc
543
544func Test_lua_dict()
545  call assert_equal({}, luaeval('vim.dict()'))
546
547  let d = {}
548  lua d = vim.eval('d')
549  lua d[0] = 123
550  lua d[1] = "abc"
551  lua d[2] = true
552  lua d[3] = false
553  lua d[4] = vim.eval("[1, 2, 3]")
554  lua d[5] = vim.eval("{'a':1, 'b':2, 'c':3}")
555  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)
556  call assert_equal(6, luaeval('#d'))
557  call assert_match('^dict: \%(0x\)\?\x\+$', luaeval('tostring(d)'))
558
559  call assert_equal('abc', luaeval('d[1]'))
560  call assert_equal(v:null, luaeval('d[22]'))
561
562  lua d[0] = 124
563  lua d[4] = nil
564  call assert_equal({'0':124, '1':'abc', '2':v:true, '3':v:false, '5': {'a':1, 'b':2, 'c':3}}, d)
565
566  lockvar 1 d
567  call assert_fails('lua d[6] = 1', '[string "vim chunk"]:1: dict is locked')
568  unlockvar d
569
570  " Error case
571  lua d = {}
572  lua d[''] = 10
573  call assert_fails("let t = luaeval('vim.dict(d)')",
574        \ s:lua_53_or_later
575        \ ? '[string "luaeval"]:1: table has empty key'
576        \ : 'table has empty key')
577  let d = {}
578  lua x = vim.eval('d')
579  call assert_fails("lua x[''] = 10", '[string "vim chunk"]:1: empty key')
580  lua x['a'] = nil
581  call assert_equal({}, d)
582
583  " cannot assign funcrefs in the global scope
584  lua x = vim.eval('g:')
585  call assert_fails("lua x['min'] = vim.funcref('max')",
586        \ '[string "vim chunk"]:1: cannot assign funcref to builtin scope')
587
588  lua d = nil
589endfunc
590
591func Test_lua_dict_table()
592  lua t = {key1 = 'x', key2 = 3.14, key3 = true, key4 = false}
593  call assert_equal({'key1': 'x', 'key2': 3.14, 'key3': v:true, 'key4': v:false},
594        \           luaeval('vim.dict(t)'))
595
596  " Same example as in :help lua-vim.
597  lua t = {math.pi, false, say = 'hi'}
598  " FIXME: commented out as it currently does not work as documented:
599  " Expected {'say': 'hi'}
600  " but got {'1': 3.141593, '2': v:false, 'say': 'hi'}
601  " Is the documentation or the code wrong?
602  "call assert_equal({'say' : 'hi'}, luaeval('vim.dict(t)'))
603  lua t = nil
604
605  call assert_fails('lua vim.dict(1)', '[string "vim chunk"]:1: table expected, got number')
606  call assert_fails('lua vim.dict("x")', '[string "vim chunk"]:1: table expected, got string')
607  call assert_fails('lua vim.dict(print)', '[string "vim chunk"]:1: table expected, got function')
608  call assert_fails('lua vim.dict(true)', '[string "vim chunk"]:1: table expected, got boolean')
609endfunc
610
611" Test d() i.e. iterator on dictionary
612func Test_lua_dict_iter()
613  let d = {'a': 1, 'b':2}
614  lua d = vim.eval('d')
615  lua str = ''
616  lua for k,v in d() do str = str .. k ..':' .. v .. ',' end
617  call assert_equal('a:1,b:2,', luaeval('str'))
618
619  lua str, d = nil
620endfunc
621
622func Test_lua_blob()
623  call assert_equal(0z, luaeval('vim.blob("")'))
624  call assert_equal(0z31326162, luaeval('vim.blob("12ab")'))
625  call assert_equal(0z00010203, luaeval('vim.blob("\x00\x01\x02\x03")'))
626  call assert_equal(0z8081FEFF, luaeval('vim.blob("\x80\x81\xfe\xff")'))
627
628  lua b = vim.blob("\x00\x00\x00\x00")
629  call assert_equal(0z00000000, luaeval('b'))
630  call assert_equal(4, luaeval('#b'))
631  lua b[0], b[1], b[2], b[3] = 1, 32, 256, 0xff
632  call assert_equal(0z012000ff, luaeval('b'))
633  lua b[4] = string.byte("z", 1)
634  call assert_equal(0z012000ff.7a, luaeval('b'))
635  call assert_equal(5, luaeval('#b'))
636  call assert_fails('lua b[#b+1] = 0x80', '[string "vim chunk"]:1: index out of range')
637  lua b:add("12ab")
638  call assert_equal(0z012000ff.7a313261.62, luaeval('b'))
639  call assert_equal(9, luaeval('#b'))
640  call assert_fails('lua b:add(nil)', '[string "vim chunk"]:1: string expected, got nil')
641  call assert_fails('lua b:add(true)', '[string "vim chunk"]:1: string expected, got boolean')
642  call assert_fails('lua b:add({})', '[string "vim chunk"]:1: string expected, got table')
643  lua b = nil
644
645  let b = 0z0102
646  lua lb = vim.eval('b')
647  let n = luaeval('lb[1]')
648  call assert_equal(2, n)
649  let n = luaeval('lb[6]')
650  call assert_equal(v:null, n)
651  if s:lua_543_or_later
652    let msg = "[string \"luaeval\"]:1: method 'xyz' is not callable (a nil value)"
653  elseif s:lua_53_or_later
654    let msg = "[string \"luaeval\"]:1: attempt to call a nil value (method 'xyz')"
655  else
656    let msg = "[string \"luaeval\"]:1: attempt to call method 'xyz' (a nil value)"
657  endif
658  call assert_fails('let x = luaeval("lb:xyz(3)")', msg)
659  let y = luaeval("lb[{}]")
660  call assert_equal(v:null, y)
661
662  lockvar b
663  call assert_fails('lua lb[1] = 2', '[string "vim chunk"]:1: blob is locked')
664  call assert_fails('lua lb:add("12")', '[string "vim chunk"]:1: blob is locked')
665
666  " Error cases
667  lua t = {}
668  call assert_fails('lua b = vim.blob(t)',
669        \ '[string "vim chunk"]:1: string expected, got table')
670endfunc
671
672func Test_lua_funcref()
673  function I(x)
674    return a:x
675  endfunction
676  let R = function('I')
677  lua i1 = vim.funcref"I"
678  lua i2 = vim.eval"R"
679  lua msg = "funcref|test|" .. (#i2(i1) == #i1(i2) and "OK" or "FAIL")
680  lua msg = vim.funcref"tr"(msg, "|", " ")
681  call assert_equal("funcref test OK", luaeval('msg'))
682
683  " Error cases
684  call assert_fails('lua f1 = vim.funcref("")',
685        \ '[string "vim chunk"]:1: invalid function name: ')
686  call assert_fails('lua f1 = vim.funcref("10")',
687        \ '[string "vim chunk"]:1: invalid function name: 10')
688  let fname = test_null_string()
689  call assert_fails('lua f1 = vim.funcref(fname)',
690        \ "[string \"vim chunk\"]:1: bad argument #1 to 'funcref' (string expected, got nil)")
691  call assert_fails('lua vim.funcref("abc")()',
692        \ ['E117:', '\[string "vim chunk"]:1: cannot call funcref'])
693
694  " dict funcref
695  function Mylen() dict
696    return len(self.data)
697  endfunction
698  let l = [0, 1, 2, 3]
699  let mydict = {'data': l}
700  lua d = vim.eval"mydict"
701  lua d.len = vim.funcref"Mylen" -- assign d as 'self'
702  lua res = (d.len() == vim.funcref"len"(vim.eval"l")) and "OK" or "FAIL"
703  call assert_equal("OK", luaeval('res'))
704  call assert_equal(function('Mylen', {'data': l, 'len': function('Mylen')}), mydict.len)
705
706  lua i1, i2, msg, d, res = nil
707endfunc
708
709" Test vim.type()
710func Test_lua_type()
711  " The following values are identical to Lua's type function.
712  call assert_equal('string',   luaeval('vim.type("foo")'))
713  call assert_equal('number',   luaeval('vim.type(1)'))
714  call assert_equal('number',   luaeval('vim.type(1.2)'))
715  call assert_equal('function', luaeval('vim.type(print)'))
716  call assert_equal('table',    luaeval('vim.type({})'))
717  call assert_equal('boolean',  luaeval('vim.type(true)'))
718  call assert_equal('boolean',  luaeval('vim.type(false)'))
719  call assert_equal('nil',      luaeval('vim.type(nil)'))
720
721  " The following values are specific to Vim.
722  call assert_equal('window',   luaeval('vim.type(vim.window())'))
723  call assert_equal('buffer',   luaeval('vim.type(vim.buffer())'))
724  call assert_equal('list',     luaeval('vim.type(vim.list())'))
725  call assert_equal('dict',     luaeval('vim.type(vim.dict())'))
726  call assert_equal('funcref',  luaeval('vim.type(vim.funcref("Test_type"))'))
727endfunc
728
729" Test vim.open()
730func Test_lua_open()
731  call assert_notmatch('XOpen', execute('ls'))
732
733  " Open a buffer XOpen1, but do not jump to it.
734  lua b = vim.open('XOpen1')
735  call assert_equal('XOpen1', luaeval('b.name'))
736  call assert_equal('', bufname('%'))
737
738  call assert_match('XOpen1', execute('ls'))
739  call assert_notequal('XOpen2', bufname('%'))
740
741  " Open a buffer XOpen2 and jump to it.
742  lua b = vim.open('XOpen2')()
743  call assert_equal('XOpen2', luaeval('b.name'))
744  call assert_equal('XOpen2', bufname('%'))
745
746  lua b = nil
747  %bwipe!
748endfunc
749
750func Test_update_package_paths()
751  set runtimepath+=./testluaplugin
752  call assert_equal("hello from lua", luaeval("require('testluaplugin').hello()"))
753endfunc
754
755func Vim_func_call_lua_callback(Concat, Cb)
756  let l:message = a:Concat("hello", "vim")
757  call a:Cb(l:message)
758endfunc
759
760func Test_pass_lua_callback_to_vim_from_lua()
761  lua pass_lua_callback_to_vim_from_lua_result = ""
762  call assert_equal("", luaeval("pass_lua_callback_to_vim_from_lua_result"))
763  lua <<EOF
764  vim.funcref('Vim_func_call_lua_callback')(
765    function(greeting, message)
766      return greeting .. " " .. message
767    end,
768    function(message)
769      pass_lua_callback_to_vim_from_lua_result = message
770    end)
771EOF
772  call assert_equal("hello vim", luaeval("pass_lua_callback_to_vim_from_lua_result"))
773endfunc
774
775func Vim_func_call_metatable_lua_callback(Greet)
776  return a:Greet("world")
777endfunc
778
779func Test_pass_lua_metatable_callback_to_vim_from_lua()
780  let result = luaeval("vim.funcref('Vim_func_call_metatable_lua_callback')(setmetatable({ space = ' '}, { __call = function(tbl, msg) return 'hello' .. tbl.space .. msg  end }) )")
781  call assert_equal("hello world", result)
782endfunc
783
784" Test vim.line()
785func Test_lua_line()
786  new
787  call setline(1, ['first line', 'second line'])
788  1
789  call assert_equal('first line', luaeval('vim.line()'))
790  2
791  call assert_equal('second line', luaeval('vim.line()'))
792  bwipe!
793endfunc
794
795" Test vim.beep()
796func Test_lua_beep()
797  call assert_beeps('lua vim.beep()')
798endfunc
799
800" Test errors in luaeval()
801func Test_luaeval_error()
802  " Compile error
803  call assert_fails("call luaeval('-nil')",
804  \ '[string "luaeval"]:1: attempt to perform arithmetic on a nil value')
805  call assert_fails("call luaeval(']')",
806  \ "[string \"luaeval\"]:1: unexpected symbol near ']'")
807  lua co = coroutine.create(function () print("hi") end)
808  call assert_fails('let i = luaeval("co")', 'luaeval: cannot convert value')
809  lua co = nil
810  call assert_fails('let m = luaeval("{}")', 'luaeval: cannot convert value')
811endfunc
812
813" Test :luafile foo.lua
814func Test_luafile()
815  call delete('Xlua_file')
816  call writefile(["str = 'hello'", "num = 123" ], 'Xlua_file')
817  call setfperm('Xlua_file', 'r-xr-xr-x')
818
819  luafile Xlua_file
820  call assert_equal('hello', luaeval('str'))
821  call assert_equal(123, luaeval('num'))
822
823  lua str, num = nil
824  call delete('Xlua_file')
825endfunc
826
827" Test :luafile %
828func Test_luafile_percent()
829  new Xlua_file
830  append
831    str, num = 'foo', 321.0
832    print(string.format('str=%s, num=%d', str, num))
833.
834  w!
835  luafile %
836  let msg = split(execute('message'), "\n")[-1]
837  call assert_equal('str=foo, num=321', msg)
838
839  lua str, num = nil
840  call delete('Xlua_file')
841  bwipe!
842endfunc
843
844" Test :luafile with syntax error
845func Test_luafile_error()
846  new Xlua_file
847  call writefile(['nil = 0' ], 'Xlua_file')
848  call setfperm('Xlua_file', 'r-xr-xr-x')
849
850  call assert_fails('luafile Xlua_file', "Xlua_file:1: unexpected symbol near 'nil'")
851
852  call delete('Xlua_file')
853  bwipe!
854endfunc
855
856" Test for dealing with strings containing newlines and null character
857func Test_lua_string_with_newline()
858  let x = execute('lua print("Hello\nWorld")')
859  call assert_equal("\nHello\nWorld", x)
860  new
861  lua k = vim.buffer(vim.eval('bufnr()'))
862  lua k:insert("Hello\0World", 0)
863  call assert_equal(["Hello\nWorld", ''], getline(1, '$'))
864  close!
865endfunc
866
867func Test_lua_set_cursor()
868  " Check that setting the cursor position works.
869  new
870  call setline(1, ['first line', 'second line'])
871  normal gg
872  lua << trim EOF
873    w = vim.window()
874    w.line = 1
875    w.col = 5
876  EOF
877  call assert_equal([1, 5], [line('.'), col('.')])
878
879  " Check that movement after setting cursor position keeps current column.
880  normal j
881  call assert_equal([2, 5], [line('.'), col('.')])
882endfunc
883
884" Test for various heredoc syntax
885func Test_lua_heredoc()
886  lua << END
887vim.command('let s = "A"')
888END
889  lua <<
890vim.command('let s ..= "B"')
891.
892  lua << trim END
893    vim.command('let s ..= "C"')
894  END
895  lua << trim
896    vim.command('let s ..= "D"')
897  .
898  lua << trim eof
899    vim.command('let s ..= "E"')
900  eof
901  call assert_equal('ABCDE', s)
902endfunc
903
904" vim: shiftwidth=2 sts=2 expandtab
905