blob: cb5b95d010398e773518163f953f611bdc0afac0 [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 Moolenaard58f03b2017-01-29 22:48:45 +01005
Bram Moolenaare165f632019-03-10 09:48:59 +01006func TearDown()
7 " Run garbage collection after each test to exercise luaV_setref().
8 call test_garbagecollect_now()
9endfunc
10
Bram Moolenaar4ff48142018-06-30 21:50:25 +020011" Check that switching to another buffer does not trigger ml_get error.
12func Test_command_new_no_ml_get_error()
Bram Moolenaard58f03b2017-01-29 22:48:45 +010013 new
14 let wincount = winnr('$')
15 call setline(1, ['one', 'two', 'three'])
16 luado vim.command("new")
17 call assert_equal(wincount + 1, winnr('$'))
Bram Moolenaar4ff48142018-06-30 21:50:25 +020018 %bwipe!
19endfunc
20
21" Test vim.command()
22func Test_command()
23 new
24 call setline(1, ['one', 'two', 'three'])
25 luado vim.command("1,2d_")
26 call assert_equal(['three'], getline(1, '$'))
Bram Moolenaard58f03b2017-01-29 22:48:45 +010027 bwipe!
Bram Moolenaar4ff48142018-06-30 21:50:25 +020028endfunc
29
30" Test vim.eval()
31func Test_eval()
32 " lua.eval with a number
33 lua v = vim.eval('123')
34 call assert_equal('number', luaeval('vim.type(v)'))
35 call assert_equal(123.0, luaeval('v'))
36
37 " lua.eval with a string
38 lua v = vim.eval('"abc"')
Bram Moolenaar02b31112019-08-31 22:16:38 +020039 call assert_equal('string', 'vim.type(v)'->luaeval())
Bram Moolenaar4ff48142018-06-30 21:50:25 +020040 call assert_equal('abc', luaeval('v'))
41
42 " lua.eval with a list
43 lua v = vim.eval("['a']")
44 call assert_equal('list', luaeval('vim.type(v)'))
45 call assert_equal(['a'], luaeval('v'))
46
47 " lua.eval with a dict
48 lua v = vim.eval("{'a':'b'}")
49 call assert_equal('dict', luaeval('vim.type(v)'))
50 call assert_equal({'a':'b'}, luaeval('v'))
51
Bram Moolenaarb7828692019-03-23 13:57:02 +010052 " lua.eval with a blob
53 lua v = vim.eval("0z00112233.deadbeef")
54 call assert_equal('blob', luaeval('vim.type(v)'))
55 call assert_equal(0z00112233.deadbeef, luaeval('v'))
56
Bram Moolenaar4ff48142018-06-30 21:50:25 +020057 call assert_fails('lua v = vim.eval(nil)',
58 \ "[string \"vim chunk\"]:1: bad argument #1 to 'eval' (string expected, got nil)")
59 call assert_fails('lua v = vim.eval(true)',
60 \ "[string \"vim chunk\"]:1: bad argument #1 to 'eval' (string expected, got boolean)")
61 call assert_fails('lua v = vim.eval({})',
62 \ "[string \"vim chunk\"]:1: bad argument #1 to 'eval' (string expected, got table)")
63 call assert_fails('lua v = vim.eval(print)',
64 \ "[string \"vim chunk\"]:1: bad argument #1 to 'eval' (string expected, got function)")
65 call assert_fails('lua v = vim.eval(vim.buffer())',
66 \ "[string \"vim chunk\"]:1: bad argument #1 to 'eval' (string expected, got userdata)")
67
68 lua v = nil
69endfunc
70
71" Test vim.window()
72func Test_window()
73 e Xfoo2
74 new Xfoo1
75
76 " Window 1 (top window) contains Xfoo1
77 " Window 2 (bottom window) contains Xfoo2
78 call assert_equal('Xfoo1', luaeval('vim.window(1):buffer().name'))
79 call assert_equal('Xfoo2', luaeval('vim.window(2):buffer().name'))
80
81 " Window 3 does not exist so vim.window(3) should return nil
82 call assert_equal('nil', luaeval('tostring(vim.window(3))'))
83
84 %bwipe!
85endfunc
86
87" Test vim.window().height
88func Test_window_height()
89 new
90 lua vim.window().height = 2
91 call assert_equal(2, winheight(0))
92 lua vim.window().height = vim.window().height + 1
93 call assert_equal(3, winheight(0))
94 bwipe!
95endfunc
96
97" Test vim.window().width
98func Test_window_width()
99 vert new
100 lua vim.window().width = 2
101 call assert_equal(2, winwidth(0))
102 lua vim.window().width = vim.window().width + 1
103 call assert_equal(3, winwidth(0))
104 bwipe!
105endfunc
106
107" Test vim.window().line and vim.window.col
108func Test_window_line_col()
109 new
110 call setline(1, ['line1', 'line2', 'line3'])
111 lua vim.window().line = 2
112 lua vim.window().col = 4
113 call assert_equal([0, 2, 4, 0], getpos('.'))
114 lua vim.window().line = vim.window().line + 1
115 lua vim.window().col = vim.window().col - 1
116 call assert_equal([0, 3, 3, 0], getpos('.'))
117
118 call assert_fails('lua vim.window().line = 10',
119 \ '[string "vim chunk"]:1: line out of range')
120 bwipe!
121endfunc
122
123" Test setting the current window
124func Test_window_set_current()
125 new Xfoo1
126 lua w1 = vim.window()
127 new Xfoo2
128 lua w2 = vim.window()
129
130 call assert_equal('Xfoo2', bufname('%'))
131 lua w1()
132 call assert_equal('Xfoo1', bufname('%'))
133 lua w2()
134 call assert_equal('Xfoo2', bufname('%'))
135
Bram Moolenaar2f362bf2018-07-01 19:49:27 +0200136 lua w1, w2 = nil
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200137 %bwipe!
138endfunc
139
140" Test vim.window().buffer
141func Test_window_buffer()
142 new Xfoo1
143 lua w1 = vim.window()
144 lua b1 = w1.buffer()
145 new Xfoo2
146 lua w2 = vim.window()
147 lua b2 = w2.buffer()
148
149 lua b1()
150 call assert_equal('Xfoo1', bufname('%'))
151 lua b2()
152 call assert_equal('Xfoo2', bufname('%'))
153
Bram Moolenaar2f362bf2018-07-01 19:49:27 +0200154 lua b1, b2, w1, w2 = nil
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200155 %bwipe!
156endfunc
157
158" Test vim.window():previous() and vim.window():next()
159func Test_window_next_previous()
160 new Xfoo1
161 new Xfoo2
162 new Xfoo3
163 wincmd j
164
165 call assert_equal('Xfoo2', luaeval('vim.window().buffer().name'))
166 call assert_equal('Xfoo1', luaeval('vim.window():next():buffer().name'))
167 call assert_equal('Xfoo3', luaeval('vim.window():previous():buffer().name'))
168
169 %bwipe!
170endfunc
171
172" Test vim.window():isvalid()
173func Test_window_isvalid()
174 new Xfoo
175 lua w = vim.window()
176 call assert_true(luaeval('w:isvalid()'))
177
178 " FIXME: how to test the case when isvalid() returns v:false?
179 " isvalid() gives errors when the window is deleted. Is it a bug?
180
181 lua w = nil
182 bwipe!
183endfunc
184
185" Test vim.buffer() with and without argument
186func Test_buffer()
187 new Xfoo1
188 let bn1 = bufnr('%')
189 new Xfoo2
190 let bn2 = bufnr('%')
191
192 " Test vim.buffer() without argument.
193 call assert_equal('Xfoo2', luaeval("vim.buffer().name"))
194
195 " Test vim.buffer() with string argument.
196 call assert_equal('Xfoo1', luaeval("vim.buffer('Xfoo1').name"))
197 call assert_equal('Xfoo2', luaeval("vim.buffer('Xfoo2').name"))
198
199 " Test vim.buffer() with integer argument.
200 call assert_equal('Xfoo1', luaeval("vim.buffer(" . bn1 . ").name"))
201 call assert_equal('Xfoo2', luaeval("vim.buffer(" . bn2 . ").name"))
202
Bram Moolenaar2f362bf2018-07-01 19:49:27 +0200203 lua bn1, bn2 = nil
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200204 %bwipe!
205endfunc
206
207" Test vim.buffer().name and vim.buffer().fname
208func Test_buffer_name()
209 new
Bram Moolenaarfe08df42018-07-07 23:07:41 +0200210 call assert_equal('', luaeval('vim.buffer().name'))
211 call assert_equal('', luaeval('vim.buffer().fname'))
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200212 bwipe!
213
214 new Xfoo
215 call assert_equal('Xfoo', luaeval('vim.buffer().name'))
216 call assert_equal(expand('%:p'), luaeval('vim.buffer().fname'))
217 bwipe!
218endfunc
219
220" Test vim.buffer().number
221func Test_buffer_number()
222 " All numbers in Lua are floating points number (no integers).
223 call assert_equal(bufnr('%'), float2nr(luaeval('vim.buffer().number')))
224endfunc
225
226" Test inserting lines in buffer.
227func Test_buffer_insert()
228 new
229 lua vim.buffer()[1] = '3'
230 lua vim.buffer():insert('1', 0)
231 lua vim.buffer():insert('2', 1)
232 lua vim.buffer():insert('4', 10)
233
234 call assert_equal(['1', '2', '3', '4'], getline(1, '$'))
235 bwipe!
236endfunc
237
238" Test deleting line in buffer
239func Test_buffer_delete()
240 new
241 call setline(1, ['1', '2', '3'])
242 lua vim.buffer()[2] = nil
243 call assert_equal(['1', '3'], getline(1, '$'))
244
245 call assert_fails('lua vim.buffer()[3] = nil',
246 \ '[string "vim chunk"]:1: invalid line number')
247 bwipe!
248endfunc
249
250" Test #vim.buffer() i.e. number of lines in buffer
251func Test_buffer_number_lines()
252 new
253 call setline(1, ['a', 'b', 'c'])
254 call assert_equal(3.0, luaeval('#vim.buffer()'))
255 bwipe!
256endfunc
257
258" Test vim.buffer():next() and vim.buffer():previous()
259" Note that these functions get the next or previous buffers
260" but do not switch buffer.
261func Test_buffer_next_previous()
262 new Xfoo1
263 new Xfoo2
264 new Xfoo3
265 b Xfoo2
266
267 lua bn = vim.buffer():next()
268 lua bp = vim.buffer():previous()
269
270 call assert_equal('Xfoo2', luaeval('vim.buffer().name'))
271 call assert_equal('Xfoo1', luaeval('bp.name'))
272 call assert_equal('Xfoo3', luaeval('bn.name'))
273
274 call assert_equal('Xfoo2', bufname('%'))
275
276 lua bn()
277 call assert_equal('Xfoo3', luaeval('vim.buffer().name'))
278 call assert_equal('Xfoo3', bufname('%'))
279
280 lua bp()
281 call assert_equal('Xfoo1', luaeval('vim.buffer().name'))
282 call assert_equal('Xfoo1', bufname('%'))
283
Bram Moolenaar2f362bf2018-07-01 19:49:27 +0200284 lua bn, bp = nil
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200285 %bwipe!
286endfunc
287
288" Test vim.buffer():isvalid()
289func Test_buffer_isvalid()
290 new Xfoo
291 lua b = vim.buffer()
292 call assert_true(luaeval('b:isvalid()'))
293
294 " FIXME: how to test the case when isvalid() returns v:false?
295 " isvalid() gives errors when the buffer is wiped. Is it a bug?
296
297 lua b = nil
298 bwipe!
299endfunc
300
301func Test_list()
302 call assert_equal([], luaeval('vim.list()'))
303
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200304 let l = []
305 lua l = vim.eval('l')
306 lua l:add(123)
307 lua l:add('abc')
308 lua l:add(true)
309 lua l:add(false)
Bram Moolenaar9067cd62019-01-01 00:41:54 +0100310 lua l:add(nil)
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200311 lua l:add(vim.eval("[1, 2, 3]"))
312 lua l:add(vim.eval("{'a':1, 'b':2, 'c':3}"))
Bram Moolenaar9067cd62019-01-01 00:41:54 +0100313 call assert_equal([123.0, 'abc', v:true, v:false, v:null, [1, 2, 3], {'a': 1, 'b': 2, 'c': 3}], l)
314 call assert_equal(7.0, luaeval('#l'))
Bram Moolenaara8a60d02018-07-02 22:54:36 +0200315 call assert_match('^list: \%(0x\)\?\x\+$', luaeval('tostring(l)'))
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200316
317 lua l[0] = 124
Bram Moolenaar9067cd62019-01-01 00:41:54 +0100318 lua l[5] = nil
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200319 lua l:insert('first')
320 lua l:insert('xx', 3)
Bram Moolenaar9067cd62019-01-01 00:41:54 +0100321 call assert_equal(['first', 124.0, 'abc', 'xx', v:true, v:false, v:null, {'a': 1, 'b': 2, 'c': 3}], l)
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200322
Bram Moolenaar2f362bf2018-07-01 19:49:27 +0200323 lockvar 1 l
324 call assert_fails('lua l:add("x")', '[string "vim chunk"]:1: list is locked')
325
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200326 lua l = nil
327endfunc
328
Bram Moolenaar2f362bf2018-07-01 19:49:27 +0200329func Test_list_table()
330 " See :help lua-vim
331 " Non-numeric keys should not be used to initialize the list
332 " so say = 'hi' should be ignored.
333 lua t = {3.14, 'hello', false, true, say = 'hi'}
334 call assert_equal([3.14, 'hello', v:false, v:true], luaeval('vim.list(t)'))
335 lua t = nil
336
337 call assert_fails('lua vim.list(1)', '[string "vim chunk"]:1: table expected, got number')
338 call assert_fails('lua vim.list("x")', '[string "vim chunk"]:1: table expected, got string')
339 call assert_fails('lua vim.list(print)', '[string "vim chunk"]:1: table expected, got function')
340 call assert_fails('lua vim.list(true)', '[string "vim chunk"]:1: table expected, got boolean')
341endfunc
342
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200343" Test l() i.e. iterator on list
344func Test_list_iter()
345 lua l = vim.list():add('foo'):add('bar')
346 lua str = ''
347 lua for v in l() do str = str .. v end
348 call assert_equal('foobar', luaeval('str'))
349
Bram Moolenaar2f362bf2018-07-01 19:49:27 +0200350 lua str, l = nil
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200351endfunc
352
353func Test_recursive_list()
354 lua l = vim.list():add(1):add(2)
355 lua l = l:add(l)
356
357 call assert_equal(1.0, luaeval('l[0]'))
358 call assert_equal(2.0, luaeval('l[1]'))
359
360 call assert_equal(1.0, luaeval('l[2][0]'))
361 call assert_equal(2.0, luaeval('l[2][1]'))
362
363 call assert_equal(1.0, luaeval('l[2][2][0]'))
364 call assert_equal(2.0, luaeval('l[2][2][1]'))
365
366 call assert_equal('[1.0, 2.0, [...]]', string(luaeval('l')))
367
Bram Moolenaara8a60d02018-07-02 22:54:36 +0200368 call assert_match('^list: \%(0x\)\?\x\+$', luaeval('tostring(l)'))
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200369 call assert_equal(luaeval('tostring(l)'), luaeval('tostring(l[2])'))
370
371 call assert_equal(luaeval('l'), luaeval('l[2]'))
372 call assert_equal(luaeval('l'), luaeval('l[2][2]'))
373
374 lua l = nil
375endfunc
376
377func Test_dict()
378 call assert_equal({}, luaeval('vim.dict()'))
379
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200380 let d = {}
381 lua d = vim.eval('d')
382 lua d[0] = 123
383 lua d[1] = "abc"
384 lua d[2] = true
385 lua d[3] = false
386 lua d[4] = vim.eval("[1, 2, 3]")
387 lua d[5] = vim.eval("{'a':1, 'b':2, 'c':3}")
388 call assert_equal({'0':123.0, '1':'abc', '2':v:true, '3':v:false, '4': [1, 2, 3], '5': {'a':1, 'b':2, 'c':3}}, d)
389 call assert_equal(6.0, luaeval('#d'))
Bram Moolenaara8a60d02018-07-02 22:54:36 +0200390 call assert_match('^dict: \%(0x\)\?\x\+$', luaeval('tostring(d)'))
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200391
392 call assert_equal('abc', luaeval('d[1]'))
393
394 lua d[0] = 124
395 lua d[4] = nil
396 call assert_equal({'0':124.0, '1':'abc', '2':v:true, '3':v:false, '5': {'a':1, 'b':2, 'c':3}}, d)
397
Bram Moolenaar2f362bf2018-07-01 19:49:27 +0200398 lockvar 1 d
399 call assert_fails('lua d[6] = 1', '[string "vim chunk"]:1: dict is locked')
400
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200401 lua d = nil
402endfunc
403
Bram Moolenaar2f362bf2018-07-01 19:49:27 +0200404func Test_dict_table()
405 lua t = {key1 = 'x', key2 = 3.14, key3 = true, key4 = false}
406 call assert_equal({'key1': 'x', 'key2': 3.14, 'key3': v:true, 'key4': v:false},
407 \ luaeval('vim.dict(t)'))
408
409 " Same example as in :help lua-vim.
410 lua t = {math.pi, false, say = 'hi'}
411 " FIXME: commented out as it currently does not work as documented:
412 " Expected {'say': 'hi'}
413 " but got {'1': 3.141593, '2': v:false, 'say': 'hi'}
414 " Is the documentation or the code wrong?
415 "call assert_equal({'say' : 'hi'}, luaeval('vim.dict(t)'))
416 lua t = nil
417
418 call assert_fails('lua vim.dict(1)', '[string "vim chunk"]:1: table expected, got number')
419 call assert_fails('lua vim.dict("x")', '[string "vim chunk"]:1: table expected, got string')
420 call assert_fails('lua vim.dict(print)', '[string "vim chunk"]:1: table expected, got function')
421 call assert_fails('lua vim.dict(true)', '[string "vim chunk"]:1: table expected, got boolean')
422endfunc
423
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200424" Test d() i.e. iterator on dictionary
425func Test_dict_iter()
426 let d = {'a': 1, 'b':2}
427 lua d = vim.eval('d')
428 lua str = ''
429 lua for k,v in d() do str = str .. k ..':' .. v .. ',' end
430 call assert_equal('a:1,b:2,', luaeval('str'))
431
Bram Moolenaar2f362bf2018-07-01 19:49:27 +0200432 lua str, d = nil
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200433endfunc
434
Bram Moolenaarb7828692019-03-23 13:57:02 +0100435func Test_blob()
436 call assert_equal(0z, luaeval('vim.blob("")'))
437 call assert_equal(0z31326162, luaeval('vim.blob("12ab")'))
438 call assert_equal(0z00010203, luaeval('vim.blob("\x00\x01\x02\x03")'))
439 call assert_equal(0z8081FEFF, luaeval('vim.blob("\x80\x81\xfe\xff")'))
440
441 lua b = vim.blob("\x00\x00\x00\x00")
442 call assert_equal(0z00000000, luaeval('b'))
443 call assert_equal(4.0, luaeval('#b'))
444 lua b[0], b[1], b[2], b[3] = 1, 32, 256, 0xff
445 call assert_equal(0z012000ff, luaeval('b'))
446 lua b[4] = string.byte("z", 1)
447 call assert_equal(0z012000ff.7a, luaeval('b'))
448 call assert_equal(5.0, luaeval('#b'))
449 call assert_fails('lua b[#b+1] = 0x80', '[string "vim chunk"]:1: index out of range')
450 lua b:add("12ab")
451 call assert_equal(0z012000ff.7a313261.62, luaeval('b'))
452 call assert_equal(9.0, luaeval('#b'))
453 call assert_fails('lua b:add(nil)', '[string "vim chunk"]:1: string expected, got nil')
454 call assert_fails('lua b:add(true)', '[string "vim chunk"]:1: string expected, got boolean')
455 call assert_fails('lua b:add({})', '[string "vim chunk"]:1: string expected, got table')
456 lua b = nil
457endfunc
458
Bram Moolenaarca06da92018-07-01 15:12:05 +0200459func Test_funcref()
460 function I(x)
461 return a:x
462 endfunction
463 let R = function('I')
464 lua i1 = vim.funcref"I"
465 lua i2 = vim.eval"R"
466 lua msg = "funcref|test|" .. (#i2(i1) == #i1(i2) and "OK" or "FAIL")
467 lua msg = vim.funcref"tr"(msg, "|", " ")
468 call assert_equal("funcref test OK", luaeval('msg'))
469
470 " dict funcref
471 function Mylen() dict
472 return len(self.data)
473 endfunction
474 let l = [0, 1, 2, 3]
475 let mydict = {'data': l}
476 lua d = vim.eval"mydict"
477 lua d.len = vim.funcref"Mylen" -- assign d as 'self'
478 lua res = (d.len() == vim.funcref"len"(vim.eval"l")) and "OK" or "FAIL"
479 call assert_equal("OK", luaeval('res'))
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100480 call assert_equal(function('Mylen', {'data': l, 'len': function('Mylen')}), mydict.len)
Bram Moolenaar2f362bf2018-07-01 19:49:27 +0200481
482 lua i1, i2, msg, d, res = nil
Bram Moolenaarca06da92018-07-01 15:12:05 +0200483endfunc
484
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200485" Test vim.type()
486func Test_type()
487 " The following values are identical to Lua's type function.
488 call assert_equal('string', luaeval('vim.type("foo")'))
489 call assert_equal('number', luaeval('vim.type(1)'))
490 call assert_equal('number', luaeval('vim.type(1.2)'))
491 call assert_equal('function', luaeval('vim.type(print)'))
492 call assert_equal('table', luaeval('vim.type({})'))
493 call assert_equal('boolean', luaeval('vim.type(true)'))
494 call assert_equal('boolean', luaeval('vim.type(false)'))
495 call assert_equal('nil', luaeval('vim.type(nil)'))
496
497 " The following values are specific to Vim.
498 call assert_equal('window', luaeval('vim.type(vim.window())'))
499 call assert_equal('buffer', luaeval('vim.type(vim.buffer())'))
500 call assert_equal('list', luaeval('vim.type(vim.list())'))
501 call assert_equal('dict', luaeval('vim.type(vim.dict())'))
Bram Moolenaarca06da92018-07-01 15:12:05 +0200502 call assert_equal('funcref', luaeval('vim.type(vim.funcref("Test_type"))'))
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200503endfunc
504
505" Test vim.open()
506func Test_open()
507 call assert_notmatch('XOpen', execute('ls'))
508
509 " Open a buffer XOpen1, but do not jump to it.
510 lua b = vim.open('XOpen1')
511 call assert_equal('XOpen1', luaeval('b.name'))
512 call assert_equal('', bufname('%'))
513
514 call assert_match('XOpen1', execute('ls'))
515 call assert_notequal('XOpen2', bufname('%'))
516
517 " Open a buffer XOpen2 and jump to it.
518 lua b = vim.open('XOpen2')()
519 call assert_equal('XOpen2', luaeval('b.name'))
520 call assert_equal('XOpen2', bufname('%'))
521
522 lua b = nil
523 %bwipe!
524endfunc
525
526" Test vim.line()
527func Test_line()
528 new
529 call setline(1, ['first line', 'second line'])
530 1
531 call assert_equal('first line', luaeval('vim.line()'))
532 2
533 call assert_equal('second line', luaeval('vim.line()'))
534 bwipe!
535endfunc
536
537" Test vim.beep()
538func Test_beep()
539 call assert_beeps('lua vim.beep()')
540endfunc
541
542" Test errors in luaeval()
543func Test_luaeval_error()
544 " Compile error
545 call assert_fails("call luaeval('-nil')",
546 \ '[string "luaeval"]:1: attempt to perform arithmetic on a nil value')
547 call assert_fails("call luaeval(']')",
548 \ "[string \"luaeval\"]:1: unexpected symbol near ']'")
549endfunc
550
551" Test :luafile foo.lua
552func Test_luafile()
553 call delete('Xlua_file')
554 call writefile(["str = 'hello'", "num = 123.0" ], 'Xlua_file')
555 call setfperm('Xlua_file', 'r-xr-xr-x')
556
557 luafile Xlua_file
558 call assert_equal('hello', luaeval('str'))
559 call assert_equal(123.0, luaeval('num'))
560
Bram Moolenaar2f362bf2018-07-01 19:49:27 +0200561 lua str, num = nil
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200562 call delete('Xlua_file')
563endfunc
564
565" Test :luafile %
566func Test_luafile_percent()
567 new Xlua_file
568 append
569 str, num = 'foo', 321.0
570 print(string.format('str=%s, num=%d', str, num))
571.
572 w!
573 luafile %
574 let msg = split(execute('message'), "\n")[-1]
575 call assert_equal('str=foo, num=321', msg)
576
Bram Moolenaar2f362bf2018-07-01 19:49:27 +0200577 lua str, num = nil
578 call delete('Xlua_file')
579 bwipe!
580endfunc
581
582" Test :luafile with syntax error
583func Test_luafile_error()
584 new Xlua_file
585 call writefile(['nil = 0' ], 'Xlua_file')
586 call setfperm('Xlua_file', 'r-xr-xr-x')
587
588 call assert_fails('luafile Xlua_file', "Xlua_file:1: unexpected symbol near 'nil'")
589
Bram Moolenaar4ff48142018-06-30 21:50:25 +0200590 call delete('Xlua_file')
Bram Moolenaard58f03b2017-01-29 22:48:45 +0100591 bwipe!
592endfunc
Bram Moolenaar53901442018-07-25 22:02:36 +0200593
594func Test_set_cursor()
595 " Check that setting the cursor position works.
596 new
597 call setline(1, ['first line', 'second line'])
598 normal gg
599 lua << EOF
600w = vim.window()
601w.line = 1
602w.col = 5
603EOF
604 call assert_equal([1, 5], [line('.'), col('.')])
605
606 " Check that movement after setting cursor position keeps current column.
607 normal j
608 call assert_equal([2, 5], [line('.'), col('.')])
609endfunc