# HG changeset patch # User Christian Brabandt # Date 1530388807 -7200 # Node ID 2e95ea9afa1e1c827b47517a746f4945635b1874 # Parent 23b2d6a264cf74f0ac84a410393e81f0a1825a50 patch 8.1.0132: lua tests are old style commit https://github.com/vim/vim/commit/4ff4814b383bc85fbf5d8f62c8022f4379d7a490 Author: Bram Moolenaar 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) diff --git a/src/Makefile b/src/Makefile --- a/src/Makefile +++ b/src/Makefile @@ -2119,7 +2119,7 @@ test1 \ test52 test59 \ test64 test69 \ test70 test72 \ - test85 test86 test87 test88 \ + test86 test87 test88 \ test94 test95 test99 test108: cd testdir; rm -f $@.out; $(MAKE) -f Makefile $@.out VIMPROG=../$(VIMTESTTARGET) $(GUI_TESTARG) SCRIPTSOURCE=../$(SCRIPTSOURCE) diff --git a/src/testdir/Make_all.mak b/src/testdir/Make_all.mak --- a/src/testdir/Make_all.mak +++ b/src/testdir/Make_all.mak @@ -36,7 +36,6 @@ SCRIPTS_ALL = \ SCRIPTS_MORE1 = \ test11.out \ test52.out \ - test85.out \ test86.out \ test87.out diff --git a/src/testdir/Make_amiga.mak b/src/testdir/Make_amiga.mak --- a/src/testdir/Make_amiga.mak +++ b/src/testdir/Make_amiga.mak @@ -14,7 +14,6 @@ include Make_all.mak # test10 'errorformat' is different # test11 "cat" doesn't work properly # test52 only for Win32 -# test85 no Lua interface # test86, 87 no Python interface SCRIPTS = $(SCRIPTS_ALL) $(SCRIPTS_MORE4) diff --git a/src/testdir/Make_vms.mms b/src/testdir/Make_vms.mms --- a/src/testdir/Make_vms.mms +++ b/src/testdir/Make_vms.mms @@ -93,7 +93,6 @@ SCRIPT = test1.out test3.out \ # # test72: bug - Vim hangs at :rename (while rename works well otherwise) # test78: bug - Vim dies at :recover Xtest -# test85: no Lua interface # test89: bug - findfile() does not work on VMS (just in the current directory) # test102: Just ODS-5 supports space and special chars in the filename. # On ODS-2 tests fail. @@ -119,9 +118,9 @@ SCRIPT_SPELL = test59.out SCRIPT_MZSCH = test70.out .ENDIF -.IFDEF HAVE_ODS5 -SCRIPT_ODS5 = test102.out -.ENDIF +.IFDEF HAVE_ODS5 +SCRIPT_ODS5 = test102.out +.ENDIF .IFDEF HAVE_GZIP SCRIPT_GZIP = test11.out @@ -131,10 +130,6 @@ SCRIPT_GZIP = test11.out SCRIPT_GDIFF = test47.out .ENDIF -.IFDEF HAVE_LUA -SCRIPT_LUA = test85.out -.ENDIF - .IFDEF HAVE_PYTHON SCRIPT_PYTHON = test86.out test87.out .ENDIF diff --git a/src/testdir/test85.in b/src/testdir/test85.in deleted file mode 100644 --- a/src/testdir/test85.in +++ /dev/null @@ -1,86 +0,0 @@ -Test for Lua interface and luaeval() function - -STARTTEST -:so small.vim -:set belloff=all -:so lua.vim -:set nocompatible viminfo+=nviminfo -:lua l = vim.list():add"item0":add"dictionary with list OK":add"item2" -:lua h = vim.dict(); h.list = l -:call garbagecollect() -/^1 -:" change buffer contents -:lua curbuf = vim.buffer() -:lua curline = vim.eval"line('.')" -:lua curbuf[curline] = "1 changed line 1" -:" scalar test -:let tmp_string = luaeval('"string"') -:let tmp_1000 = luaeval('1000') -:if printf("%s%.0f", tmp_string, tmp_1000) == "string1000" -:let scalar_res = "OK" -:else -:let scalar_res = "FAILED" -:endif -:call append(search("^1"), "scalar test " . scalar_res) -:" dictionary containing a list -:let tmp = luaeval("h").list[1] -:/^2/put =tmp -:" circular list (at the same time test lists containing lists) -:lua l[2] = l -:let l2 = luaeval("h").list -:if l2[2] == l2 -:let res = "OK" -:else -:let res = "FAILED" -:endif -:call setline(search("^3"), "circular test " . res) - -:let l = [] -:lua l = vim.eval("l") -:lua l:add(123) -:lua l:add("abc") -:lua l:add(vim.eval("[1, 2, 3]")) -:lua l:add(vim.eval("{'a':1, 'b':2, 'c':3}")) -:lua l:insert(123) -:lua l:insert("abc") -:lua l:insert(vim.eval("[1, 2, 3]")) -:lua l:insert(vim.eval("{'a':1, 'b':2, 'c':3}")) -:lua l[0] = l[0] -:lua l[1] = l[1] -:lua l[2] = l[2] -:lua l[3] = l[3] -:lua l[0] = 123 -:lua l[1] = "abc" -:lua l[2] = vim.eval("[1, 2, 3]") -:lua l[3] = vim.eval("{'a':1, 'b':2, 'c':3}") -:lua l[3] = nil -:lua l[2] = nil -:lua l[1] = nil -:lua l[0] = nil -:lua l = nil -:$put =string(l) - -:let d = {} -:lua d = vim.eval("d") -:lua d[0] = 123 -:lua d[1] = "abc" -:lua d[2] = vim.eval("[1, 2, 3]") -:lua d[3] = vim.eval("{'a':1, 'b':2, 'c':3}") -:lua d[4] = d[0] -:lua d[5] = d[1] -:lua d[6] = d[2] -:lua d[7] = d[3] -:lua d[3] = nil -:lua d[2] = nil -:lua d[1] = nil -:lua d[0] = nil -:lua d = nil -:$put =string(d) - -:?^1?,$w! test.out -:qa! -ENDTEST - -1 line 1 -2 line 2 -3 line 3 diff --git a/src/testdir/test_lua.vim b/src/testdir/test_lua.vim --- a/src/testdir/test_lua.vim +++ b/src/testdir/test_lua.vim @@ -1,22 +1,494 @@ " Tests for Lua. -" TODO: move tests from test85.in here. if !has('lua') finish endif -func Test_luado() - new - call setline(1, ['one', 'two', 'three']) - luado vim.command("%d_") - bwipe! - - " Check switching to another buffer does not trigger ml_get error. +" Check that switching to another buffer does not trigger ml_get error. +func Test_command_new_no_ml_get_error() new let wincount = winnr('$') call setline(1, ['one', 'two', 'three']) luado vim.command("new") call assert_equal(wincount + 1, winnr('$')) + %bwipe! +endfunc + +" Test vim.command() +func Test_command() + new + call setline(1, ['one', 'two', 'three']) + luado vim.command("1,2d_") + call assert_equal(['three'], getline(1, '$')) bwipe! +endfunc + +" Test vim.eval() +func Test_eval() + " lua.eval with a number + lua v = vim.eval('123') + call assert_equal('number', luaeval('vim.type(v)')) + call assert_equal(123.0, luaeval('v')) + + " lua.eval with a string + lua v = vim.eval('"abc"') + call assert_equal('string', luaeval('vim.type(v)')) + call assert_equal('abc', luaeval('v')) + + " lua.eval with a list + lua v = vim.eval("['a']") + call assert_equal('list', luaeval('vim.type(v)')) + call assert_equal(['a'], luaeval('v')) + + " lua.eval with a dict + lua v = vim.eval("{'a':'b'}") + call assert_equal('dict', luaeval('vim.type(v)')) + call assert_equal({'a':'b'}, luaeval('v')) + + call assert_fails('lua v = vim.eval(nil)', + \ "[string \"vim chunk\"]:1: bad argument #1 to 'eval' (string expected, got nil)") + call assert_fails('lua v = vim.eval(true)', + \ "[string \"vim chunk\"]:1: bad argument #1 to 'eval' (string expected, got boolean)") + call assert_fails('lua v = vim.eval({})', + \ "[string \"vim chunk\"]:1: bad argument #1 to 'eval' (string expected, got table)") + call assert_fails('lua v = vim.eval(print)', + \ "[string \"vim chunk\"]:1: bad argument #1 to 'eval' (string expected, got function)") + call assert_fails('lua v = vim.eval(vim.buffer())', + \ "[string \"vim chunk\"]:1: bad argument #1 to 'eval' (string expected, got userdata)") + + lua v = nil +endfunc + +" Test vim.window() +func Test_window() + e Xfoo2 + new Xfoo1 + + " Window 1 (top window) contains Xfoo1 + " Window 2 (bottom window) contains Xfoo2 + call assert_equal('Xfoo1', luaeval('vim.window(1):buffer().name')) + call assert_equal('Xfoo2', luaeval('vim.window(2):buffer().name')) + + " Window 3 does not exist so vim.window(3) should return nil + call assert_equal('nil', luaeval('tostring(vim.window(3))')) + + %bwipe! +endfunc + +" Test vim.window().height +func Test_window_height() + new + lua vim.window().height = 2 + call assert_equal(2, winheight(0)) + lua vim.window().height = vim.window().height + 1 + call assert_equal(3, winheight(0)) + bwipe! +endfunc + +" Test vim.window().width +func Test_window_width() + vert new + lua vim.window().width = 2 + call assert_equal(2, winwidth(0)) + lua vim.window().width = vim.window().width + 1 + call assert_equal(3, winwidth(0)) + bwipe! +endfunc + +" Test vim.window().line and vim.window.col +func Test_window_line_col() + new + call setline(1, ['line1', 'line2', 'line3']) + lua vim.window().line = 2 + lua vim.window().col = 4 + call assert_equal([0, 2, 4, 0], getpos('.')) + lua vim.window().line = vim.window().line + 1 + lua vim.window().col = vim.window().col - 1 + call assert_equal([0, 3, 3, 0], getpos('.')) + + call assert_fails('lua vim.window().line = 10', + \ '[string "vim chunk"]:1: line out of range') + bwipe! +endfunc + +" Test setting the current window +func Test_window_set_current() + new Xfoo1 + lua w1 = vim.window() + new Xfoo2 + lua w2 = vim.window() + + call assert_equal('Xfoo2', bufname('%')) + lua w1() + call assert_equal('Xfoo1', bufname('%')) + lua w2() + call assert_equal('Xfoo2', bufname('%')) + + lua w1, w2 = nil, nil + %bwipe! +endfunc + +" Test vim.window().buffer +func Test_window_buffer() + new Xfoo1 + lua w1 = vim.window() + lua b1 = w1.buffer() + new Xfoo2 + lua w2 = vim.window() + lua b2 = w2.buffer() + + lua b1() + call assert_equal('Xfoo1', bufname('%')) + lua b2() + call assert_equal('Xfoo2', bufname('%')) + + lua b1, b2 = nil, nil + %bwipe! +endfunc + +" Test vim.window():previous() and vim.window():next() +func Test_window_next_previous() + new Xfoo1 + new Xfoo2 + new Xfoo3 + wincmd j + + call assert_equal('Xfoo2', luaeval('vim.window().buffer().name')) + call assert_equal('Xfoo1', luaeval('vim.window():next():buffer().name')) + call assert_equal('Xfoo3', luaeval('vim.window():previous():buffer().name')) + + %bwipe! +endfunc + +" Test vim.window():isvalid() +func Test_window_isvalid() + new Xfoo + lua w = vim.window() + call assert_true(luaeval('w:isvalid()')) + + " FIXME: how to test the case when isvalid() returns v:false? + " isvalid() gives errors when the window is deleted. Is it a bug? + + lua w = nil + bwipe! +endfunc + +" Test vim.buffer() with and without argument +func Test_buffer() + new Xfoo1 + let bn1 = bufnr('%') + new Xfoo2 + let bn2 = bufnr('%') + + " Test vim.buffer() without argument. + call assert_equal('Xfoo2', luaeval("vim.buffer().name")) + + " Test vim.buffer() with string argument. + call assert_equal('Xfoo1', luaeval("vim.buffer('Xfoo1').name")) + call assert_equal('Xfoo2', luaeval("vim.buffer('Xfoo2').name")) + + " Test vim.buffer() with integer argument. + call assert_equal('Xfoo1', luaeval("vim.buffer(" . bn1 . ").name")) + call assert_equal('Xfoo2', luaeval("vim.buffer(" . bn2 . ").name")) + + lua bn1, bn2 = nil, nil + %bwipe! +endfunc + +" Test vim.buffer().name and vim.buffer().fname +func Test_buffer_name() + new + " FIXME: for an unnamed buffer, I would expect + " vim.buffer().name to give an empty string, but + " it returns 0. Is it a bug? + " so this assert_equal is commented out. + " call assert_equal('', luaeval('vim.buffer().name')) + bwipe! + + new Xfoo + call assert_equal('Xfoo', luaeval('vim.buffer().name')) + call assert_equal(expand('%:p'), luaeval('vim.buffer().fname')) + bwipe! +endfunc + +" Test vim.buffer().number +func Test_buffer_number() + " All numbers in Lua are floating points number (no integers). + call assert_equal(bufnr('%'), float2nr(luaeval('vim.buffer().number'))) +endfunc + +" Test inserting lines in buffer. +func Test_buffer_insert() + new + lua vim.buffer()[1] = '3' + lua vim.buffer():insert('1', 0) + lua vim.buffer():insert('2', 1) + lua vim.buffer():insert('4', 10) + + call assert_equal(['1', '2', '3', '4'], getline(1, '$')) + bwipe! +endfunc + +" Test deleting line in buffer +func Test_buffer_delete() + new + call setline(1, ['1', '2', '3']) + lua vim.buffer()[2] = nil + call assert_equal(['1', '3'], getline(1, '$')) + + call assert_fails('lua vim.buffer()[3] = nil', + \ '[string "vim chunk"]:1: invalid line number') + bwipe! +endfunc + +" Test #vim.buffer() i.e. number of lines in buffer +func Test_buffer_number_lines() + new + call setline(1, ['a', 'b', 'c']) + call assert_equal(3.0, luaeval('#vim.buffer()')) bwipe! endfunc + +" Test vim.buffer():next() and vim.buffer():previous() +" Note that these functions get the next or previous buffers +" but do not switch buffer. +func Test_buffer_next_previous() + new Xfoo1 + new Xfoo2 + new Xfoo3 + b Xfoo2 + + lua bn = vim.buffer():next() + lua bp = vim.buffer():previous() + + call assert_equal('Xfoo2', luaeval('vim.buffer().name')) + call assert_equal('Xfoo1', luaeval('bp.name')) + call assert_equal('Xfoo3', luaeval('bn.name')) + + call assert_equal('Xfoo2', bufname('%')) + + lua bn() + call assert_equal('Xfoo3', luaeval('vim.buffer().name')) + call assert_equal('Xfoo3', bufname('%')) + + lua bp() + call assert_equal('Xfoo1', luaeval('vim.buffer().name')) + call assert_equal('Xfoo1', bufname('%')) + + lua bn, bp = nil, nil + %bwipe! +endfunc + +" Test vim.buffer():isvalid() +func Test_buffer_isvalid() + new Xfoo + lua b = vim.buffer() + call assert_true(luaeval('b:isvalid()')) + + " FIXME: how to test the case when isvalid() returns v:false? + " isvalid() gives errors when the buffer is wiped. Is it a bug? + + lua b = nil + bwipe! +endfunc + +func Test_list() + call assert_equal([], luaeval('vim.list()')) + + " Same example as in :help lua-vim. + " FIXME: test is disabled because it does not work. + " See https://github.com/vim/vim/issues/3086 + " lua t = {math.pi, false, say = 'hi'} + " call assert_equal([3.141593, 0], luaeval('vim.list(t)')) + + let l = [] + lua l = vim.eval('l') + lua l:add(123) + lua l:add('abc') + lua l:add(true) + lua l:add(false) + lua l:add(vim.eval("[1, 2, 3]")) + lua l:add(vim.eval("{'a':1, 'b':2, 'c':3}")) + call assert_equal([123.0, 'abc', v:true, v:false, [1, 2, 3], {'a': 1, 'b': 2, 'c': 3}], l) + call assert_equal(6.0, luaeval('#l')) + call assert_match('^list: 0x\x\+$', luaeval('tostring(l)')) + + lua l[0] = 124 + lua l[4] = nil + lua l:insert('first') + lua l:insert('xx', 3) + call assert_equal(['first', 124.0, 'abc', 'xx', v:true, v:false, {'a': 1, 'b': 2, 'c': 3}], l) + + lua l = nil +endfunc + +" Test l() i.e. iterator on list +func Test_list_iter() + lua l = vim.list():add('foo'):add('bar') + lua str = '' + lua for v in l() do str = str .. v end + call assert_equal('foobar', luaeval('str')) + + lua str, v, l = nil, nil, nil +endfunc + +func Test_recursive_list() + lua l = vim.list():add(1):add(2) + lua l = l:add(l) + + call assert_equal(1.0, luaeval('l[0]')) + call assert_equal(2.0, luaeval('l[1]')) + + call assert_equal(1.0, luaeval('l[2][0]')) + call assert_equal(2.0, luaeval('l[2][1]')) + + call assert_equal(1.0, luaeval('l[2][2][0]')) + call assert_equal(2.0, luaeval('l[2][2][1]')) + + call assert_equal('[1.0, 2.0, [...]]', string(luaeval('l'))) + + call assert_match('^list: 0x\x\+$', luaeval('tostring(l)')) + call assert_equal(luaeval('tostring(l)'), luaeval('tostring(l[2])')) + + call assert_equal(luaeval('l'), luaeval('l[2]')) + call assert_equal(luaeval('l'), luaeval('l[2][2]')) + + lua l = nil +endfunc + +func Test_dict() + call assert_equal({}, luaeval('vim.dict()')) + + " Same example as in :help lua-vim. + " FIXME: test is disabled because it does not work. + " See https://github.com/vim/vim/issues/3086 + " lua t = {math.pi, false, say = 'hi'} + " call assert_equal({'say' : 'hi'}, luaeval('vim.dict(t)')) + + let d = {} + lua d = vim.eval('d') + lua d[0] = 123 + lua d[1] = "abc" + lua d[2] = true + lua d[3] = false + lua d[4] = vim.eval("[1, 2, 3]") + lua d[5] = vim.eval("{'a':1, 'b':2, 'c':3}") + 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) + call assert_equal(6.0, luaeval('#d')) + call assert_match('^dict: 0x\x\+$', luaeval('tostring(d)')) + + call assert_equal('abc', luaeval('d[1]')) + + lua d[0] = 124 + lua d[4] = nil + call assert_equal({'0':124.0, '1':'abc', '2':v:true, '3':v:false, '5': {'a':1, 'b':2, 'c':3}}, d) + + lua d = nil +endfunc + +" Test d() i.e. iterator on dictionary +func Test_dict_iter() + let d = {'a': 1, 'b':2} + lua d = vim.eval('d') + lua str = '' + lua for k,v in d() do str = str .. k ..':' .. v .. ',' end + call assert_equal('a:1,b:2,', luaeval('str')) + + lua str, k, v, d = nil, nil, nil, nil +endfunc + +" Test vim.type() +func Test_type() + " The following values are identical to Lua's type function. + call assert_equal('string', luaeval('vim.type("foo")')) + call assert_equal('number', luaeval('vim.type(1)')) + call assert_equal('number', luaeval('vim.type(1.2)')) + call assert_equal('function', luaeval('vim.type(print)')) + call assert_equal('table', luaeval('vim.type({})')) + call assert_equal('boolean', luaeval('vim.type(true)')) + call assert_equal('boolean', luaeval('vim.type(false)')) + call assert_equal('nil', luaeval('vim.type(nil)')) + + " The following values are specific to Vim. + call assert_equal('window', luaeval('vim.type(vim.window())')) + call assert_equal('buffer', luaeval('vim.type(vim.buffer())')) + call assert_equal('list', luaeval('vim.type(vim.list())')) + call assert_equal('dict', luaeval('vim.type(vim.dict())')) +endfunc + +" Test vim.open() +func Test_open() + call assert_notmatch('XOpen', execute('ls')) + + " Open a buffer XOpen1, but do not jump to it. + lua b = vim.open('XOpen1') + call assert_equal('XOpen1', luaeval('b.name')) + call assert_equal('', bufname('%')) + + call assert_match('XOpen1', execute('ls')) + call assert_notequal('XOpen2', bufname('%')) + + " Open a buffer XOpen2 and jump to it. + lua b = vim.open('XOpen2')() + call assert_equal('XOpen2', luaeval('b.name')) + call assert_equal('XOpen2', bufname('%')) + + lua b = nil + %bwipe! +endfunc + +" Test vim.line() +func Test_line() + new + call setline(1, ['first line', 'second line']) + 1 + call assert_equal('first line', luaeval('vim.line()')) + 2 + call assert_equal('second line', luaeval('vim.line()')) + bwipe! +endfunc + +" Test vim.beep() +func Test_beep() + call assert_beeps('lua vim.beep()') +endfunc + +" Test errors in luaeval() +func Test_luaeval_error() + " Compile error + call assert_fails("call luaeval('-nil')", + \ '[string "luaeval"]:1: attempt to perform arithmetic on a nil value') + call assert_fails("call luaeval(']')", + \ "[string \"luaeval\"]:1: unexpected symbol near ']'") +endfunc + +" Test :luafile foo.lua +func Test_luafile() + call delete('Xlua_file') + call writefile(["str = 'hello'", "num = 123.0" ], 'Xlua_file') + call setfperm('Xlua_file', 'r-xr-xr-x') + + luafile Xlua_file + call assert_equal('hello', luaeval('str')) + call assert_equal(123.0, luaeval('num')) + + lua str, num = nil, nil + call delete('Xlua_file') +endfunc + +" Test :luafile % +func Test_luafile_percent() + new Xlua_file + append + str, num = 'foo', 321.0 + print(string.format('str=%s, num=%d', str, num)) +. + w! + luafile % + let msg = split(execute('message'), "\n")[-1] + call assert_equal('str=foo, num=321', msg) + + lua str, num = nil, nil + call delete('Xlua_file') + bwipe! +endfunc diff --git a/src/version.c b/src/version.c --- a/src/version.c +++ b/src/version.c @@ -790,6 +790,8 @@ static char *(features[]) = static int included_patches[] = { /* Add new patch number below this line */ /**/ + 132, +/**/ 131, /**/ 130,