comparison src/testdir/test_lua.vim @ 21108:43e82e8133b9 v8.2.1105

patch 8.2.1105: insufficient test coverage for Lua Commit: https://github.com/vim/vim/commit/e49b8e8d7589e85e75aedefab7ce97da47adbf74 Author: Bram Moolenaar <Bram@vim.org> Date: Wed Jul 1 13:52:55 2020 +0200 patch 8.2.1105: insufficient test coverage for Lua Problem: Insufficient test coverage for Lua. Solution: Add tests. (Yegappan Lakshmanan, closes https://github.com/vim/vim/issues/6368) Fix uncovered memory leak. Avoid unnecessary copy/free.
author Bram Moolenaar <Bram@vim.org>
date Wed, 01 Jul 2020 14:00:05 +0200
parents 89aba7895bb2
children 0a8322506058
comparison
equal deleted inserted replaced
21107:149a89396e3d 21108:43e82e8133b9
1 " Tests for Lua. 1 " Tests for Lua.
2 2
3 source check.vim 3 source check.vim
4 CheckFeature lua 4 CheckFeature lua
5 CheckFeature float 5 CheckFeature float
6
7 let s:luaver = split(split(luaeval('_VERSION'), ' ')[1], '\.')
8 let s:major = str2nr(s:luaver[0])
9 let s:minor = str2nr(s:luaver[1])
10 if s:major < 5 || (s:major == 5 && s:minor < 3)
11 let s:lua_53_or_later = 0
12 else
13 let s:lua_53_or_later = 1
14 endif
6 15
7 func TearDown() 16 func TearDown()
8 " Run garbage collection after each test to exercise luaV_setref(). 17 " Run garbage collection after each test to exercise luaV_setref().
9 call test_garbagecollect_now() 18 call test_garbagecollect_now()
10 endfunc 19 endfunc
24 new 33 new
25 call setline(1, ['one', 'two', 'three']) 34 call setline(1, ['one', 'two', 'three'])
26 luado vim.command("1,2d_") 35 luado vim.command("1,2d_")
27 call assert_equal(['three'], getline(1, '$')) 36 call assert_equal(['three'], getline(1, '$'))
28 bwipe! 37 bwipe!
38 endfunc
39
40 func Test_lua_luado()
41 new
42 call setline(1, ['one', 'two'])
43 luado return(linenr)
44 call assert_equal(['1', '2'], getline(1, '$'))
45 close!
46
47 " Error cases
48 call assert_fails('luado string.format()',
49 \ "[string \"vim chunk\"]:1: bad argument #1 to 'format' (string expected, got no value)")
50 call assert_fails('luado func()',
51 \ s:lua_53_or_later
52 \ ? "[string \"vim chunk\"]:1: attempt to call a nil value (global 'func')"
53 \ : "[string \"vim chunk\"]:1: attempt to call global 'func' (a nil value)")
54 call assert_fails('luado error("failed")', "[string \"vim chunk\"]:1: failed")
29 endfunc 55 endfunc
30 56
31 " Test vim.eval() 57 " Test vim.eval()
32 func Test_lua_eval() 58 func Test_lua_eval()
33 " lua.eval with a number 59 " lua.eval with a number
52 78
53 " lua.eval with a blob 79 " lua.eval with a blob
54 lua v = vim.eval("0z00112233.deadbeef") 80 lua v = vim.eval("0z00112233.deadbeef")
55 call assert_equal('blob', luaeval('vim.type(v)')) 81 call assert_equal('blob', luaeval('vim.type(v)'))
56 call assert_equal(0z00112233.deadbeef, luaeval('v')) 82 call assert_equal(0z00112233.deadbeef, luaeval('v'))
83
84 " lua.eval with a float
85 lua v = vim.eval('3.14')
86 call assert_equal('number', luaeval('vim.type(v)'))
87 call assert_equal(3.14, luaeval('v'))
88
89 " lua.eval with a bool
90 lua v = vim.eval('v:true')
91 call assert_equal('number', luaeval('vim.type(v)'))
92 call assert_equal(1, luaeval('v'))
93 lua v = vim.eval('v:false')
94 call assert_equal('number', luaeval('vim.type(v)'))
95 call assert_equal(0, luaeval('v'))
96
97 " lua.eval with a null
98 lua v = vim.eval('v:null')
99 call assert_equal('nil', luaeval('vim.type(v)'))
100 call assert_equal(v:null, luaeval('v'))
57 101
58 call assert_fails('lua v = vim.eval(nil)', 102 call assert_fails('lua v = vim.eval(nil)',
59 \ "[string \"vim chunk\"]:1: bad argument #1 to 'eval' (string expected, got nil)") 103 \ "[string \"vim chunk\"]:1: bad argument #1 to 'eval' (string expected, got nil)")
60 call assert_fails('lua v = vim.eval(true)', 104 call assert_fails('lua v = vim.eval(true)',
61 \ "[string \"vim chunk\"]:1: bad argument #1 to 'eval' (string expected, got boolean)") 105 \ "[string \"vim chunk\"]:1: bad argument #1 to 'eval' (string expected, got boolean)")
80 call assert_equal('Xfoo2', luaeval('vim.window(2):buffer().name')) 124 call assert_equal('Xfoo2', luaeval('vim.window(2):buffer().name'))
81 125
82 " Window 3 does not exist so vim.window(3) should return nil 126 " Window 3 does not exist so vim.window(3) should return nil
83 call assert_equal('nil', luaeval('tostring(vim.window(3))')) 127 call assert_equal('nil', luaeval('tostring(vim.window(3))'))
84 128
129 call assert_fails("let n = luaeval('vim.window().xyz()')",
130 \ s:lua_53_or_later
131 \ ? "[string \"luaeval\"]:1: attempt to call a nil value (field 'xyz')"
132 \ : "[string \"luaeval\"]:1: attempt to call field 'xyz' (a nil value)")
133 call assert_fails('lua vim.window().xyz = 1',
134 \ "[string \"vim chunk\"]:1: invalid window property: `xyz'")
135
85 %bwipe! 136 %bwipe!
86 endfunc 137 endfunc
87 138
88 " Test vim.window().height 139 " Test vim.window().height
89 func Test_lua_window_height() 140 func Test_lua_window_height()
123 174
124 " Test vim.call 175 " Test vim.call
125 func Test_lua_call() 176 func Test_lua_call()
126 call assert_equal(has('lua'), luaeval('vim.call("has", "lua")')) 177 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")')) 178 call assert_equal(printf("Hello %s", "vim"), luaeval('vim.call("printf", "Hello %s", "vim")'))
179
180 " Error cases
181 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)')",
182 \ '[string "luaeval"]:1: Function called with too many arguments')
183 lua co = coroutine.create(function () print("hi") end)
184 call assert_fails("call luaeval('vim.call(\"type\", co)')",
185 \ '[string "luaeval"]:1: lua: cannot convert value')
186 lua co = nil
187 call assert_fails("call luaeval('vim.call(\"abc\")')", '[string "luaeval"]:1: lua: call_vim_function failed')
128 endfunc 188 endfunc
129 189
130 " Test vim.fn.* 190 " Test vim.fn.*
131 func Test_lua_fn() 191 func Test_lua_fn()
132 call assert_equal(has('lua'), luaeval('vim.fn.has("lua")')) 192 call assert_equal(has('lua'), luaeval('vim.fn.has("lua")'))
243 lua vim.buffer():insert('1', 0) 303 lua vim.buffer():insert('1', 0)
244 lua vim.buffer():insert('2', 1) 304 lua vim.buffer():insert('2', 1)
245 lua vim.buffer():insert('4', 10) 305 lua vim.buffer():insert('4', 10)
246 306
247 call assert_equal(['1', '2', '3', '4'], getline(1, '$')) 307 call assert_equal(['1', '2', '3', '4'], getline(1, '$'))
308 call assert_equal('4', luaeval('vim.buffer()[4]'))
309 call assert_equal(v:null, luaeval('vim.buffer()[5]'))
310 call assert_equal(v:null, luaeval('vim.buffer()[{}]'))
311 call assert_fails('lua vim.buffer():xyz()',
312 \ s:lua_53_or_later
313 \ ? "[string \"vim chunk\"]:1: attempt to call a nil value (method 'xyz')"
314 \ : "[string \"vim chunk\"]:1: attempt to call method 'xyz' (a nil value)")
315 call assert_fails('lua vim.buffer()[1] = {}',
316 \ '[string "vim chunk"]:1: wrong argument to change')
248 bwipe! 317 bwipe!
249 endfunc 318 endfunc
250 319
251 " Test deleting line in buffer 320 " Test deleting line in buffer
252 func Test_lua_buffer_delete() 321 func Test_lua_buffer_delete()
253 new 322 new
254 call setline(1, ['1', '2', '3']) 323 call setline(1, ['1', '2', '3'])
324 call cursor(3, 1)
255 lua vim.buffer()[2] = nil 325 lua vim.buffer()[2] = nil
256 call assert_equal(['1', '3'], getline(1, '$')) 326 call assert_equal(['1', '3'], getline(1, '$'))
257 327
258 call assert_fails('lua vim.buffer()[3] = nil', 328 call assert_fails('lua vim.buffer()[3] = nil',
259 \ '[string "vim chunk"]:1: invalid line number') 329 \ '[string "vim chunk"]:1: invalid line number')
329 399
330 lua l[1] = 124 400 lua l[1] = 124
331 lua l[6] = nil 401 lua l[6] = nil
332 lua l:insert('first') 402 lua l:insert('first')
333 lua l:insert('xx', 3) 403 lua l:insert('xx', 3)
404 call assert_fails('lua l:insert("xx", -20)',
405 \ '[string "vim chunk"]:1: invalid position')
334 call assert_equal(['first', 124, 'abc', 'xx', v:true, v:false, v:null, {'a': 1, 'b': 2, 'c': 3}], l) 406 call assert_equal(['first', 124, 'abc', 'xx', v:true, v:false, v:null, {'a': 1, 'b': 2, 'c': 3}], l)
335 407
336 lockvar 1 l 408 lockvar 1 l
337 call assert_fails('lua l:add("x")', '[string "vim chunk"]:1: list is locked') 409 call assert_fails('lua l:add("x")', '[string "vim chunk"]:1: list is locked')
410 call assert_fails('lua l:insert(2)', '[string "vim chunk"]:1: list is locked')
411 call assert_fails('lua l[9] = 1', '[string "vim chunk"]:1: list is locked')
412
413 unlockvar l
414 let l = [1, 2]
415 lua ll = vim.eval('l')
416 let x = luaeval("ll[3]")
417 call assert_equal(v:null, x)
418 call assert_fails('let x = luaeval("ll:xyz(3)")',
419 \ s:lua_53_or_later
420 \ ? "[string \"luaeval\"]:1: attempt to call a nil value (method 'xyz')"
421 \ : "[string \"luaeval\"]:1: attempt to call method 'xyz' (a nil value)")
422 let y = luaeval("ll[{}]")
423 call assert_equal(v:null, y)
338 424
339 lua l = nil 425 lua l = nil
340 endfunc 426 endfunc
341 427
342 func Test_lua_list_table() 428 func Test_lua_list_table()
352 call assert_fails('lua vim.list(print)', '[string "vim chunk"]:1: table expected, got function') 438 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') 439 call assert_fails('lua vim.list(true)', '[string "vim chunk"]:1: table expected, got boolean')
354 endfunc 440 endfunc
355 441
356 func Test_lua_list_table_insert_remove() 442 func Test_lua_list_table_insert_remove()
357 let luaver = split(split(luaeval('_VERSION'), ' ')[1], '\.') 443 if !s:lua_53_or_later
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' 444 throw 'Skipped: Lua version < 5.3'
363 endif 445 endif
364 446
365 let l = [1, 2] 447 let l = [1, 2]
366 lua t = vim.eval('l') 448 lua t = vim.eval('l')
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) 509 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')) 510 call assert_equal(6, luaeval('#d'))
429 call assert_match('^dict: \%(0x\)\?\x\+$', luaeval('tostring(d)')) 511 call assert_match('^dict: \%(0x\)\?\x\+$', luaeval('tostring(d)'))
430 512
431 call assert_equal('abc', luaeval('d[1]')) 513 call assert_equal('abc', luaeval('d[1]'))
514 call assert_equal(v:null, luaeval('d[22]'))
432 515
433 lua d[0] = 124 516 lua d[0] = 124
434 lua d[4] = nil 517 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) 518 call assert_equal({'0':124, '1':'abc', '2':v:true, '3':v:false, '5': {'a':1, 'b':2, 'c':3}}, d)
436 519
437 lockvar 1 d 520 lockvar 1 d
438 call assert_fails('lua d[6] = 1', '[string "vim chunk"]:1: dict is locked') 521 call assert_fails('lua d[6] = 1', '[string "vim chunk"]:1: dict is locked')
522 unlockvar d
523
524 " Error case
525 lua d = {}
526 lua d[''] = 10
527 call assert_fails("let t = luaeval('vim.dict(d)')",
528 \ '[string "luaeval"]:1: table has empty key')
529 let d = {}
530 lua x = vim.eval('d')
531 call assert_fails("lua x[''] = 10", '[string "vim chunk"]:1: empty key')
532 lua x['a'] = nil
533 call assert_equal({}, d)
534
535 " cannot assign funcrefs in the global scope
536 lua x = vim.eval('g:')
537 call assert_fails("lua x['min'] = vim.funcref('max')",
538 \ '[string "vim chunk"]:1: cannot assign funcref to builtin scope')
439 539
440 lua d = nil 540 lua d = nil
441 endfunc 541 endfunc
442 542
443 func Test_lua_dict_table() 543 func Test_lua_dict_table()
491 call assert_equal(9, luaeval('#b')) 591 call assert_equal(9, luaeval('#b'))
492 call assert_fails('lua b:add(nil)', '[string "vim chunk"]:1: string expected, got nil') 592 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') 593 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') 594 call assert_fails('lua b:add({})', '[string "vim chunk"]:1: string expected, got table')
495 lua b = nil 595 lua b = nil
596
597 let b = 0z0102
598 lua lb = vim.eval('b')
599 let n = luaeval('lb[1]')
600 call assert_equal(2, n)
601 let n = luaeval('lb[6]')
602 call assert_equal(v:null, n)
603 call assert_fails('let x = luaeval("lb:xyz(3)")',
604 \ s:lua_53_or_later
605 \ ? "[string \"luaeval\"]:1: attempt to call a nil value (method 'xyz')"
606 \ : "[string \"luaeval\"]:1: attempt to call method 'xyz' (a nil value)")
607 let y = luaeval("lb[{}]")
608 call assert_equal(v:null, y)
609
610 lockvar b
611 call assert_fails('lua lb[1] = 2', '[string "vim chunk"]:1: blob is locked')
612 call assert_fails('lua lb:add("12")', '[string "vim chunk"]:1: blob is locked')
613
614 " Error cases
615 lua t = {}
616 call assert_fails('lua b = vim.blob(t)',
617 \ '[string "vim chunk"]:1: string expected, got table')
496 endfunc 618 endfunc
497 619
498 func Test_lua_funcref() 620 func Test_lua_funcref()
499 function I(x) 621 function I(x)
500 return a:x 622 return a:x
503 lua i1 = vim.funcref"I" 625 lua i1 = vim.funcref"I"
504 lua i2 = vim.eval"R" 626 lua i2 = vim.eval"R"
505 lua msg = "funcref|test|" .. (#i2(i1) == #i1(i2) and "OK" or "FAIL") 627 lua msg = "funcref|test|" .. (#i2(i1) == #i1(i2) and "OK" or "FAIL")
506 lua msg = vim.funcref"tr"(msg, "|", " ") 628 lua msg = vim.funcref"tr"(msg, "|", " ")
507 call assert_equal("funcref test OK", luaeval('msg')) 629 call assert_equal("funcref test OK", luaeval('msg'))
630
631 " Error cases
632 call assert_fails('lua f1 = vim.funcref("")',
633 \ '[string "vim chunk"]:1: invalid function name: ')
634 call assert_fails('lua f1 = vim.funcref("10")',
635 \ '[string "vim chunk"]:1: invalid function name: 10')
636 let fname = test_null_string()
637 call assert_fails('lua f1 = vim.funcref(fname)',
638 \ "[string \"vim chunk\"]:1: bad argument #1 to 'funcref' (string expected, got nil)")
639 call assert_fails('lua vim.funcref("abc")()',
640 \ '[string "vim chunk"]:1: cannot call funcref')
508 641
509 " dict funcref 642 " dict funcref
510 function Mylen() dict 643 function Mylen() dict
511 return len(self.data) 644 return len(self.data)
512 endfunction 645 endfunction
617 " Compile error 750 " Compile error
618 call assert_fails("call luaeval('-nil')", 751 call assert_fails("call luaeval('-nil')",
619 \ '[string "luaeval"]:1: attempt to perform arithmetic on a nil value') 752 \ '[string "luaeval"]:1: attempt to perform arithmetic on a nil value')
620 call assert_fails("call luaeval(']')", 753 call assert_fails("call luaeval(']')",
621 \ "[string \"luaeval\"]:1: unexpected symbol near ']'") 754 \ "[string \"luaeval\"]:1: unexpected symbol near ']'")
755 lua co = coroutine.create(function () print("hi") end)
756 call assert_fails('let i = luaeval("co")', 'luaeval: cannot convert value')
757 lua co = nil
758 call assert_fails('let m = luaeval("{}")', 'luaeval: cannot convert value')
622 endfunc 759 endfunc
623 760
624 " Test :luafile foo.lua 761 " Test :luafile foo.lua
625 func Test_luafile() 762 func Test_luafile()
626 call delete('Xlua_file') 763 call delete('Xlua_file')
660 797
661 call assert_fails('luafile Xlua_file', "Xlua_file:1: unexpected symbol near 'nil'") 798 call assert_fails('luafile Xlua_file', "Xlua_file:1: unexpected symbol near 'nil'")
662 799
663 call delete('Xlua_file') 800 call delete('Xlua_file')
664 bwipe! 801 bwipe!
802 endfunc
803
804 " Test for dealing with strings containing newlines and null character
805 func Test_lua_string_with_newline()
806 let x = execute('lua print("Hello\nWorld")')
807 call assert_equal("\nHello\nWorld", x)
808 new
809 lua k = vim.buffer(vim.eval('bufnr()'))
810 lua k:insert("Hello\0World", 0)
811 call assert_equal(["Hello\nWorld", ''], getline(1, '$'))
812 close!
665 endfunc 813 endfunc
666 814
667 func Test_lua_set_cursor() 815 func Test_lua_set_cursor()
668 " Check that setting the cursor position works. 816 " Check that setting the cursor position works.
669 new 817 new