comparison src/testdir/test_lua.vim @ 14230:2e95ea9afa1e v8.1.0132

patch 8.1.0132: lua tests are old style commit https://github.com/vim/vim/commit/4ff4814b383bc85fbf5d8f62c8022f4379d7a490 Author: Bram Moolenaar <Bram@vim.org> Date: Sat Jun 30 21:50:25 2018 +0200 patch 8.1.0132: lua tests are old style Problem: Lua tests are old style. Solution: Convert to new style tests. Improve coverage. (Dominique Pelle, closes #3091)
author Christian Brabandt <cb@256bit.org>
date Sat, 30 Jun 2018 22:00:07 +0200
parents 6c3d42d18366
children e3bc8cdc94dd
comparison
equal deleted inserted replaced
14229:23b2d6a264cf 14230:2e95ea9afa1e
1 " Tests for Lua. 1 " Tests for Lua.
2 " TODO: move tests from test85.in here.
3 2
4 if !has('lua') 3 if !has('lua')
5 finish 4 finish
6 endif 5 endif
7 6
8 func Test_luado() 7 " Check that switching to another buffer does not trigger ml_get error.
9 new 8 func Test_command_new_no_ml_get_error()
10 call setline(1, ['one', 'two', 'three'])
11 luado vim.command("%d_")
12 bwipe!
13
14 " Check switching to another buffer does not trigger ml_get error.
15 new 9 new
16 let wincount = winnr('$') 10 let wincount = winnr('$')
17 call setline(1, ['one', 'two', 'three']) 11 call setline(1, ['one', 'two', 'three'])
18 luado vim.command("new") 12 luado vim.command("new")
19 call assert_equal(wincount + 1, winnr('$')) 13 call assert_equal(wincount + 1, winnr('$'))
20 bwipe! 14 %bwipe!
21 bwipe! 15 endfunc
22 endfunc 16
17 " Test vim.command()
18 func Test_command()
19 new
20 call setline(1, ['one', 'two', 'three'])
21 luado vim.command("1,2d_")
22 call assert_equal(['three'], getline(1, '$'))
23 bwipe!
24 endfunc
25
26 " Test vim.eval()
27 func Test_eval()
28 " lua.eval with a number
29 lua v = vim.eval('123')
30 call assert_equal('number', luaeval('vim.type(v)'))
31 call assert_equal(123.0, luaeval('v'))
32
33 " lua.eval with a string
34 lua v = vim.eval('"abc"')
35 call assert_equal('string', luaeval('vim.type(v)'))
36 call assert_equal('abc', luaeval('v'))
37
38 " lua.eval with a list
39 lua v = vim.eval("['a']")
40 call assert_equal('list', luaeval('vim.type(v)'))
41 call assert_equal(['a'], luaeval('v'))
42
43 " lua.eval with a dict
44 lua v = vim.eval("{'a':'b'}")
45 call assert_equal('dict', luaeval('vim.type(v)'))
46 call assert_equal({'a':'b'}, luaeval('v'))
47
48 call assert_fails('lua v = vim.eval(nil)',
49 \ "[string \"vim chunk\"]:1: bad argument #1 to 'eval' (string expected, got nil)")
50 call assert_fails('lua v = vim.eval(true)',
51 \ "[string \"vim chunk\"]:1: bad argument #1 to 'eval' (string expected, got boolean)")
52 call assert_fails('lua v = vim.eval({})',
53 \ "[string \"vim chunk\"]:1: bad argument #1 to 'eval' (string expected, got table)")
54 call assert_fails('lua v = vim.eval(print)',
55 \ "[string \"vim chunk\"]:1: bad argument #1 to 'eval' (string expected, got function)")
56 call assert_fails('lua v = vim.eval(vim.buffer())',
57 \ "[string \"vim chunk\"]:1: bad argument #1 to 'eval' (string expected, got userdata)")
58
59 lua v = nil
60 endfunc
61
62 " Test vim.window()
63 func Test_window()
64 e Xfoo2
65 new Xfoo1
66
67 " Window 1 (top window) contains Xfoo1
68 " Window 2 (bottom window) contains Xfoo2
69 call assert_equal('Xfoo1', luaeval('vim.window(1):buffer().name'))
70 call assert_equal('Xfoo2', luaeval('vim.window(2):buffer().name'))
71
72 " Window 3 does not exist so vim.window(3) should return nil
73 call assert_equal('nil', luaeval('tostring(vim.window(3))'))
74
75 %bwipe!
76 endfunc
77
78 " Test vim.window().height
79 func Test_window_height()
80 new
81 lua vim.window().height = 2
82 call assert_equal(2, winheight(0))
83 lua vim.window().height = vim.window().height + 1
84 call assert_equal(3, winheight(0))
85 bwipe!
86 endfunc
87
88 " Test vim.window().width
89 func Test_window_width()
90 vert new
91 lua vim.window().width = 2
92 call assert_equal(2, winwidth(0))
93 lua vim.window().width = vim.window().width + 1
94 call assert_equal(3, winwidth(0))
95 bwipe!
96 endfunc
97
98 " Test vim.window().line and vim.window.col
99 func Test_window_line_col()
100 new
101 call setline(1, ['line1', 'line2', 'line3'])
102 lua vim.window().line = 2
103 lua vim.window().col = 4
104 call assert_equal([0, 2, 4, 0], getpos('.'))
105 lua vim.window().line = vim.window().line + 1
106 lua vim.window().col = vim.window().col - 1
107 call assert_equal([0, 3, 3, 0], getpos('.'))
108
109 call assert_fails('lua vim.window().line = 10',
110 \ '[string "vim chunk"]:1: line out of range')
111 bwipe!
112 endfunc
113
114 " Test setting the current window
115 func Test_window_set_current()
116 new Xfoo1
117 lua w1 = vim.window()
118 new Xfoo2
119 lua w2 = vim.window()
120
121 call assert_equal('Xfoo2', bufname('%'))
122 lua w1()
123 call assert_equal('Xfoo1', bufname('%'))
124 lua w2()
125 call assert_equal('Xfoo2', bufname('%'))
126
127 lua w1, w2 = nil, nil
128 %bwipe!
129 endfunc
130
131 " Test vim.window().buffer
132 func Test_window_buffer()
133 new Xfoo1
134 lua w1 = vim.window()
135 lua b1 = w1.buffer()
136 new Xfoo2
137 lua w2 = vim.window()
138 lua b2 = w2.buffer()
139
140 lua b1()
141 call assert_equal('Xfoo1', bufname('%'))
142 lua b2()
143 call assert_equal('Xfoo2', bufname('%'))
144
145 lua b1, b2 = nil, nil
146 %bwipe!
147 endfunc
148
149 " Test vim.window():previous() and vim.window():next()
150 func Test_window_next_previous()
151 new Xfoo1
152 new Xfoo2
153 new Xfoo3
154 wincmd j
155
156 call assert_equal('Xfoo2', luaeval('vim.window().buffer().name'))
157 call assert_equal('Xfoo1', luaeval('vim.window():next():buffer().name'))
158 call assert_equal('Xfoo3', luaeval('vim.window():previous():buffer().name'))
159
160 %bwipe!
161 endfunc
162
163 " Test vim.window():isvalid()
164 func Test_window_isvalid()
165 new Xfoo
166 lua w = vim.window()
167 call assert_true(luaeval('w:isvalid()'))
168
169 " FIXME: how to test the case when isvalid() returns v:false?
170 " isvalid() gives errors when the window is deleted. Is it a bug?
171
172 lua w = nil
173 bwipe!
174 endfunc
175
176 " Test vim.buffer() with and without argument
177 func Test_buffer()
178 new Xfoo1
179 let bn1 = bufnr('%')
180 new Xfoo2
181 let bn2 = bufnr('%')
182
183 " Test vim.buffer() without argument.
184 call assert_equal('Xfoo2', luaeval("vim.buffer().name"))
185
186 " Test vim.buffer() with string argument.
187 call assert_equal('Xfoo1', luaeval("vim.buffer('Xfoo1').name"))
188 call assert_equal('Xfoo2', luaeval("vim.buffer('Xfoo2').name"))
189
190 " Test vim.buffer() with integer argument.
191 call assert_equal('Xfoo1', luaeval("vim.buffer(" . bn1 . ").name"))
192 call assert_equal('Xfoo2', luaeval("vim.buffer(" . bn2 . ").name"))
193
194 lua bn1, bn2 = nil, nil
195 %bwipe!
196 endfunc
197
198 " Test vim.buffer().name and vim.buffer().fname
199 func Test_buffer_name()
200 new
201 " FIXME: for an unnamed buffer, I would expect
202 " vim.buffer().name to give an empty string, but
203 " it returns 0. Is it a bug?
204 " so this assert_equal is commented out.
205 " call assert_equal('', luaeval('vim.buffer().name'))
206 bwipe!
207
208 new Xfoo
209 call assert_equal('Xfoo', luaeval('vim.buffer().name'))
210 call assert_equal(expand('%:p'), luaeval('vim.buffer().fname'))
211 bwipe!
212 endfunc
213
214 " Test vim.buffer().number
215 func Test_buffer_number()
216 " All numbers in Lua are floating points number (no integers).
217 call assert_equal(bufnr('%'), float2nr(luaeval('vim.buffer().number')))
218 endfunc
219
220 " Test inserting lines in buffer.
221 func Test_buffer_insert()
222 new
223 lua vim.buffer()[1] = '3'
224 lua vim.buffer():insert('1', 0)
225 lua vim.buffer():insert('2', 1)
226 lua vim.buffer():insert('4', 10)
227
228 call assert_equal(['1', '2', '3', '4'], getline(1, '$'))
229 bwipe!
230 endfunc
231
232 " Test deleting line in buffer
233 func Test_buffer_delete()
234 new
235 call setline(1, ['1', '2', '3'])
236 lua vim.buffer()[2] = nil
237 call assert_equal(['1', '3'], getline(1, '$'))
238
239 call assert_fails('lua vim.buffer()[3] = nil',
240 \ '[string "vim chunk"]:1: invalid line number')
241 bwipe!
242 endfunc
243
244 " Test #vim.buffer() i.e. number of lines in buffer
245 func Test_buffer_number_lines()
246 new
247 call setline(1, ['a', 'b', 'c'])
248 call assert_equal(3.0, luaeval('#vim.buffer()'))
249 bwipe!
250 endfunc
251
252 " Test vim.buffer():next() and vim.buffer():previous()
253 " Note that these functions get the next or previous buffers
254 " but do not switch buffer.
255 func Test_buffer_next_previous()
256 new Xfoo1
257 new Xfoo2
258 new Xfoo3
259 b Xfoo2
260
261 lua bn = vim.buffer():next()
262 lua bp = vim.buffer():previous()
263
264 call assert_equal('Xfoo2', luaeval('vim.buffer().name'))
265 call assert_equal('Xfoo1', luaeval('bp.name'))
266 call assert_equal('Xfoo3', luaeval('bn.name'))
267
268 call assert_equal('Xfoo2', bufname('%'))
269
270 lua bn()
271 call assert_equal('Xfoo3', luaeval('vim.buffer().name'))
272 call assert_equal('Xfoo3', bufname('%'))
273
274 lua bp()
275 call assert_equal('Xfoo1', luaeval('vim.buffer().name'))
276 call assert_equal('Xfoo1', bufname('%'))
277
278 lua bn, bp = nil, nil
279 %bwipe!
280 endfunc
281
282 " Test vim.buffer():isvalid()
283 func Test_buffer_isvalid()
284 new Xfoo
285 lua b = vim.buffer()
286 call assert_true(luaeval('b:isvalid()'))
287
288 " FIXME: how to test the case when isvalid() returns v:false?
289 " isvalid() gives errors when the buffer is wiped. Is it a bug?
290
291 lua b = nil
292 bwipe!
293 endfunc
294
295 func Test_list()
296 call assert_equal([], luaeval('vim.list()'))
297
298 " Same example as in :help lua-vim.
299 " FIXME: test is disabled because it does not work.
300 " See https://github.com/vim/vim/issues/3086
301 " lua t = {math.pi, false, say = 'hi'}
302 " call assert_equal([3.141593, 0], luaeval('vim.list(t)'))
303
304 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)
310 lua l:add(vim.eval("[1, 2, 3]"))
311 lua l:add(vim.eval("{'a':1, 'b':2, 'c':3}"))
312 call assert_equal([123.0, 'abc', v:true, v:false, [1, 2, 3], {'a': 1, 'b': 2, 'c': 3}], l)
313 call assert_equal(6.0, luaeval('#l'))
314 call assert_match('^list: 0x\x\+$', luaeval('tostring(l)'))
315
316 lua l[0] = 124
317 lua l[4] = nil
318 lua l:insert('first')
319 lua l:insert('xx', 3)
320 call assert_equal(['first', 124.0, 'abc', 'xx', v:true, v:false, {'a': 1, 'b': 2, 'c': 3}], l)
321
322 lua l = nil
323 endfunc
324
325 " Test l() i.e. iterator on list
326 func Test_list_iter()
327 lua l = vim.list():add('foo'):add('bar')
328 lua str = ''
329 lua for v in l() do str = str .. v end
330 call assert_equal('foobar', luaeval('str'))
331
332 lua str, v, l = nil, nil, nil
333 endfunc
334
335 func Test_recursive_list()
336 lua l = vim.list():add(1):add(2)
337 lua l = l:add(l)
338
339 call assert_equal(1.0, luaeval('l[0]'))
340 call assert_equal(2.0, luaeval('l[1]'))
341
342 call assert_equal(1.0, luaeval('l[2][0]'))
343 call assert_equal(2.0, luaeval('l[2][1]'))
344
345 call assert_equal(1.0, luaeval('l[2][2][0]'))
346 call assert_equal(2.0, luaeval('l[2][2][1]'))
347
348 call assert_equal('[1.0, 2.0, [...]]', string(luaeval('l')))
349
350 call assert_match('^list: 0x\x\+$', luaeval('tostring(l)'))
351 call assert_equal(luaeval('tostring(l)'), luaeval('tostring(l[2])'))
352
353 call assert_equal(luaeval('l'), luaeval('l[2]'))
354 call assert_equal(luaeval('l'), luaeval('l[2][2]'))
355
356 lua l = nil
357 endfunc
358
359 func Test_dict()
360 call assert_equal({}, luaeval('vim.dict()'))
361
362 " Same example as in :help lua-vim.
363 " FIXME: test is disabled because it does not work.
364 " See https://github.com/vim/vim/issues/3086
365 " lua t = {math.pi, false, say = 'hi'}
366 " call assert_equal({'say' : 'hi'}, luaeval('vim.dict(t)'))
367
368 let d = {}
369 lua d = vim.eval('d')
370 lua d[0] = 123
371 lua d[1] = "abc"
372 lua d[2] = true
373 lua d[3] = false
374 lua d[4] = vim.eval("[1, 2, 3]")
375 lua d[5] = vim.eval("{'a':1, 'b':2, 'c':3}")
376 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)
377 call assert_equal(6.0, luaeval('#d'))
378 call assert_match('^dict: 0x\x\+$', luaeval('tostring(d)'))
379
380 call assert_equal('abc', luaeval('d[1]'))
381
382 lua d[0] = 124
383 lua d[4] = nil
384 call assert_equal({'0':124.0, '1':'abc', '2':v:true, '3':v:false, '5': {'a':1, 'b':2, 'c':3}}, d)
385
386 lua d = nil
387 endfunc
388
389 " Test d() i.e. iterator on dictionary
390 func Test_dict_iter()
391 let d = {'a': 1, 'b':2}
392 lua d = vim.eval('d')
393 lua str = ''
394 lua for k,v in d() do str = str .. k ..':' .. v .. ',' end
395 call assert_equal('a:1,b:2,', luaeval('str'))
396
397 lua str, k, v, d = nil, nil, nil, nil
398 endfunc
399
400 " Test vim.type()
401 func Test_type()
402 " The following values are identical to Lua's type function.
403 call assert_equal('string', luaeval('vim.type("foo")'))
404 call assert_equal('number', luaeval('vim.type(1)'))
405 call assert_equal('number', luaeval('vim.type(1.2)'))
406 call assert_equal('function', luaeval('vim.type(print)'))
407 call assert_equal('table', luaeval('vim.type({})'))
408 call assert_equal('boolean', luaeval('vim.type(true)'))
409 call assert_equal('boolean', luaeval('vim.type(false)'))
410 call assert_equal('nil', luaeval('vim.type(nil)'))
411
412 " The following values are specific to Vim.
413 call assert_equal('window', luaeval('vim.type(vim.window())'))
414 call assert_equal('buffer', luaeval('vim.type(vim.buffer())'))
415 call assert_equal('list', luaeval('vim.type(vim.list())'))
416 call assert_equal('dict', luaeval('vim.type(vim.dict())'))
417 endfunc
418
419 " Test vim.open()
420 func Test_open()
421 call assert_notmatch('XOpen', execute('ls'))
422
423 " Open a buffer XOpen1, but do not jump to it.
424 lua b = vim.open('XOpen1')
425 call assert_equal('XOpen1', luaeval('b.name'))
426 call assert_equal('', bufname('%'))
427
428 call assert_match('XOpen1', execute('ls'))
429 call assert_notequal('XOpen2', bufname('%'))
430
431 " Open a buffer XOpen2 and jump to it.
432 lua b = vim.open('XOpen2')()
433 call assert_equal('XOpen2', luaeval('b.name'))
434 call assert_equal('XOpen2', bufname('%'))
435
436 lua b = nil
437 %bwipe!
438 endfunc
439
440 " Test vim.line()
441 func Test_line()
442 new
443 call setline(1, ['first line', 'second line'])
444 1
445 call assert_equal('first line', luaeval('vim.line()'))
446 2
447 call assert_equal('second line', luaeval('vim.line()'))
448 bwipe!
449 endfunc
450
451 " Test vim.beep()
452 func Test_beep()
453 call assert_beeps('lua vim.beep()')
454 endfunc
455
456 " Test errors in luaeval()
457 func Test_luaeval_error()
458 " Compile error
459 call assert_fails("call luaeval('-nil')",
460 \ '[string "luaeval"]:1: attempt to perform arithmetic on a nil value')
461 call assert_fails("call luaeval(']')",
462 \ "[string \"luaeval\"]:1: unexpected symbol near ']'")
463 endfunc
464
465 " Test :luafile foo.lua
466 func Test_luafile()
467 call delete('Xlua_file')
468 call writefile(["str = 'hello'", "num = 123.0" ], 'Xlua_file')
469 call setfperm('Xlua_file', 'r-xr-xr-x')
470
471 luafile Xlua_file
472 call assert_equal('hello', luaeval('str'))
473 call assert_equal(123.0, luaeval('num'))
474
475 lua str, num = nil, nil
476 call delete('Xlua_file')
477 endfunc
478
479 " Test :luafile %
480 func Test_luafile_percent()
481 new Xlua_file
482 append
483 str, num = 'foo', 321.0
484 print(string.format('str=%s, num=%d', str, num))
485 .
486 w!
487 luafile %
488 let msg = split(execute('message'), "\n")[-1]
489 call assert_equal('str=foo, num=321', msg)
490
491 lua str, num = nil, nil
492 call delete('Xlua_file')
493 bwipe!
494 endfunc