" Tests for Lua.
source check.vim
" This test also works without the lua feature.
func Test_skip_lua()
if 0
lua print("Not executed")
endif
endfunc
CheckFeature lua
CheckFeature float
" Depending on the lua version, the error messages are different.
let [s:major, s:minor, s:patch] = luaeval('vim.lua_version')->split('\.')->map({-> str2nr(v:val)})
let s:lua_53_or_later = 0
let s:lua_543_or_later = 0
if (s:major == 5 && s:minor >= 3) || s:major > 5
let s:lua_53_or_later = 1
if (s:major == 5
\ && ((s:minor == 4 && s:patch >= 3) || s:minor > 4))
\ || s:major > 5
let s:lua_543_or_later = 1
endif
endif
func TearDown()
" Run garbage collection after each test to exercise luaV_setref().
call test_garbagecollect_now()
endfunc
" Check that switching to another buffer does not trigger ml_get error.
func Test_lua_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_lua_command()
new
call setline(1, ['one', 'two', 'three'])
luado vim.command("1,2d_")
call assert_equal(['three'], getline(1, '$'))
bwipe!
endfunc
func Test_lua_luado()
new
call setline(1, ['one', 'two'])
luado return(linenr)
call assert_equal(['1', '2'], getline(1, '$'))
close!
" Error cases
call assert_fails('luado string.format()',
\ "[string \"vim chunk\"]:1: bad argument #1 to 'format' (string expected, got no value)")
if s:lua_543_or_later
let msg = "[string \"vim chunk\"]:1: global 'func' is not callable (a nil value)"
elseif s:lua_53_or_later
let msg = "[string \"vim chunk\"]:1: attempt to call a nil value (global 'func')"
else
let msg = "[string \"vim chunk\"]:1: attempt to call global 'func' (a nil value)"
endif
call assert_fails('luado func()', msg)
call assert_fails('luado error("failed")', "[string \"vim chunk\"]:1: failed")
endfunc
" Test vim.eval()
func Test_lua_eval()
" lua.eval with a number
lua v = vim.eval('123')
call assert_equal('number', luaeval('vim.type(v)'))
call assert_equal(123, luaeval('v'))
" lua.eval with a string
lua v = vim.eval('"abc"')
call assert_equal('string', 'vim.type(v)'->luaeval())
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'))
" lua.eval with a blob
lua v = vim.eval("0z00112233.deadbeef")
call assert_equal('blob', luaeval('vim.type(v)'))
call assert_equal(0z00112233.deadbeef, luaeval('v'))
" lua.eval with a float
lua v = vim.eval('3.14')
call assert_equal('number', luaeval('vim.type(v)'))
call assert_equal(3.14, luaeval('v'))
" lua.eval with a bool
lua v = vim.eval('v:true')
call assert_equal('number', luaeval('vim.type(v)'))
call assert_equal(1, luaeval('v'))
lua v = vim.eval('v:false')
call assert_equal('number', luaeval('vim.type(v)'))
call assert_equal(0, luaeval('v'))
" lua.eval with a null
lua v = vim.eval('v:null')
call assert_equal('nil', luaeval('vim.type(v)'))
call assert_equal(v:null, 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 luaeval() with lambda
func Test_luaeval_with_lambda()
lua function hello_luaeval_lambda(a, cb) return a .. cb() end
call assert_equal('h