xref: /vim-8.2.3635/src/testdir/test_lua.vim (revision a53618dd)
1" Tests for Lua.
2
3source check.vim
4CheckFeature lua
5CheckFeature float
6
7func TearDown()
8  " Run garbage collection after each test to exercise luaV_setref().
9  call test_garbagecollect_now()
10endfunc
11
12" Check that switching to another buffer does not trigger ml_get error.
13func Test_lua_command_new_no_ml_get_error()
14  new
15  let wincount = winnr('$')
16  call setline(1, ['one', 'two', 'three'])
17  luado vim.command("new")
18  call assert_equal(wincount + 1, winnr('$'))
19  %bwipe!
20endfunc
21
22" Test vim.command()
23func Test_lua_command()
24  new
25  call setline(1, ['one', 'two', 'three'])
26  luado vim.command("1,2d_")
27  call assert_equal(['three'], getline(1, '$'))
28  bwipe!
29endfunc
30
31" Test vim.eval()
32func Test_lua_eval()
33  " lua.eval with a number
34  lua v = vim.eval('123')
35  call assert_equal('number', luaeval('vim.type(v)'))
36  call assert_equal(123, luaeval('v'))
37
38  " lua.eval with a string
39  lua v = vim.eval('"abc"')
40  call assert_equal('string', 'vim.type(v)'->luaeval())
41  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
53  " 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
58  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()
73func Test_lua_window()
74  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
89func Test_lua_window_height()
90  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
99func Test_lua_window_width()
100  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
109func Test_lua_window_line_col()
110  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
124" 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
136" Test setting the current window
137func Test_lua_window_set_current()
138  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
149  lua w1, w2 = nil
150  %bwipe!
151endfunc
152
153" Test vim.window().buffer
154func Test_lua_window_buffer()
155  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
167  lua b1, b2, w1, w2 = nil
168  %bwipe!
169endfunc
170
171" Test vim.window():previous() and vim.window():next()
172func Test_lua_window_next_previous()
173  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()
186func Test_lua_window_isvalid()
187  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
199func Test_lua_buffer()
200  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
216  lua bn1, bn2 = nil
217  %bwipe!
218endfunc
219
220" Test vim.buffer().name and vim.buffer().fname
221func Test_lua_buffer_name()
222  new
223  call assert_equal('', luaeval('vim.buffer().name'))
224  call assert_equal('', luaeval('vim.buffer().fname'))
225  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
234func Test_lua_buffer_number()
235  " 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.
240func Test_lua_buffer_insert()
241  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
252func Test_lua_buffer_delete()
253  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
264func Test_lua_buffer_number_lines()
265  new
266  call setline(1, ['a', 'b', 'c'])
267  call assert_equal(3, luaeval('#vim.buffer()'))
268  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.
274func Test_lua_buffer_next_previous()
275  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
297  lua bn, bp = nil
298  %bwipe!
299endfunc
300
301" Test vim.buffer():isvalid()
302func Test_lua_buffer_isvalid()
303  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
314func Test_lua_list()
315  call assert_equal([], luaeval('vim.list()'))
316
317  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)
323  lua l:add(nil)
324  lua l:add(vim.eval("[1, 2, 3]"))
325  lua l:add(vim.eval("{'a':1, 'b':2, 'c':3}"))
326  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'))
328  call assert_match('^list: \%(0x\)\?\x\+$', luaeval('tostring(l)'))
329
330  lua l[1] = 124
331  lua l[6] = nil
332  lua l:insert('first')
333  lua l:insert('xx', 3)
334  call assert_equal(['first', 124, 'abc', 'xx', v:true, v:false, v:null, {'a': 1, 'b': 2, 'c': 3}], l)
335
336  lockvar 1 l
337  call assert_fails('lua l:add("x")', '[string "vim chunk"]:1: list is locked')
338
339  lua l = nil
340endfunc
341
342func Test_lua_list_table()
343  " 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
356func 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
382" Test l() i.e. iterator on list
383func Test_lua_list_iter()
384  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
389  lua str, l = nil
390endfunc
391
392func Test_lua_recursive_list()
393  lua l = vim.list():add(1):add(2)
394  lua l = l:add(l)
395
396  call assert_equal(1, luaeval('l[1]'))
397  call assert_equal(2, luaeval('l[2]'))
398
399  call assert_equal(1, luaeval('l[3][1]'))
400  call assert_equal(2, luaeval('l[3][2]'))
401
402  call assert_equal(1, luaeval('l[3][3][1]'))
403  call assert_equal(2, luaeval('l[3][3][2]'))
404
405  call assert_equal('[1, 2, [...]]', string(luaeval('l')))
406
407  call assert_match('^list: \%(0x\)\?\x\+$', luaeval('tostring(l)'))
408  call assert_equal(luaeval('tostring(l)'), luaeval('tostring(l[3])'))
409
410  call assert_equal(luaeval('l'), luaeval('l[3]'))
411  call assert_equal(luaeval('l'), luaeval('l[3][3]'))
412
413  lua l = nil
414endfunc
415
416func Test_lua_dict()
417  call assert_equal({}, luaeval('vim.dict()'))
418
419  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}")
427  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'))
429  call assert_match('^dict: \%(0x\)\?\x\+$', luaeval('tostring(d)'))
430
431  call assert_equal('abc', luaeval('d[1]'))
432
433  lua d[0] = 124
434  lua d[4] = nil
435  call assert_equal({'0':124, '1':'abc', '2':v:true, '3':v:false, '5': {'a':1, 'b':2, 'c':3}}, d)
436
437  lockvar 1 d
438  call assert_fails('lua d[6] = 1', '[string "vim chunk"]:1: dict is locked')
439
440  lua d = nil
441endfunc
442
443func Test_lua_dict_table()
444  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
463" Test d() i.e. iterator on dictionary
464func Test_lua_dict_iter()
465  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
471  lua str, d = nil
472endfunc
473
474func Test_lua_blob()
475  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'))
482  call assert_equal(4, luaeval('#b'))
483  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'))
487  call assert_equal(5, luaeval('#b'))
488  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'))
491  call assert_equal(9, luaeval('#b'))
492  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
498func Test_lua_funcref()
499  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'))
519  call assert_equal(function('Mylen', {'data': l, 'len': function('Mylen')}), mydict.len)
520
521  lua i1, i2, msg, d, res = nil
522endfunc
523
524" Test vim.type()
525func Test_lua_type()
526  " 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())'))
541  call assert_equal('funcref',  luaeval('vim.type(vim.funcref("Test_type"))'))
542endfunc
543
544" Test vim.open()
545func Test_lua_open()
546  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
565func Test_update_package_paths()
566  set runtimepath+=./testluaplugin
567  call assert_equal("hello from lua", luaeval("require('testluaplugin').hello()"))
568endfunc
569
570func 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
599" Test vim.line()
600func Test_lua_line()
601  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()
611func Test_lua_beep()
612  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')
627  call writefile(["str = 'hello'", "num = 123" ], 'Xlua_file')
628  call setfperm('Xlua_file', 'r-xr-xr-x')
629
630  luafile Xlua_file
631  call assert_equal('hello', luaeval('str'))
632  call assert_equal(123, luaeval('num'))
633
634  lua str, num = nil
635  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
650  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
663  call delete('Xlua_file')
664  bwipe!
665endfunc
666
667func Test_lua_set_cursor()
668  " Check that setting the cursor position works.
669  new
670  call setline(1, ['first line', 'second line'])
671  normal gg
672  lua << trim EOF
673    w = vim.window()
674    w.line = 1
675    w.col = 5
676  EOF
677  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
683
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  .
698  lua << trim eof
699    vim.command('let s ..= "E"')
700  eof
701  call assert_equal('ABCDE', s)
702endfunc
703
704" vim: shiftwidth=2 sts=2 expandtab
705