" 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
" 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 = 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
let s:lua_543 = 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_luado_change_buffer()
new
let wincount = winnr('$')
call setline(1, ['one', 'two', 'three'])
luado vim.command("new")
call assert_equal(wincount + 1, winnr('$'))
%bwipe!
endfunc
" Check that :luado deleting lines does not trigger ml_get error.
func Test_lua_luado_delete_lines()
new
call setline(1, ['one', 'two', 'three'])
luado vim.command("%d_")
call assert_equal([''], getline(1, '$'))
call setline(1, ['one', 'two', 'three'])
luado vim.command("1,2d_")
call assert_equal(['three'], getline(1, '$'))
call setline(1, ['one', 'two', 'three'])
luado vim.command("2,3d_"); return "REPLACED"
call assert_equal(['REPLACED'], getline(1, '$'))
call setline(1, ['one', 'two', 'three'])
2,3luado vim.command("1,2d_"); return "REPLACED"
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
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