aboutsummaryrefslogtreecommitdiff
path: root/test/functional/lua/api_spec.lua
diff options
context:
space:
mode:
authorZyX <kp-pav@yandex.ru>2017-01-21 01:10:44 +0300
committerZyX <kp-pav@yandex.ru>2017-03-27 00:12:22 +0300
commitbca9c2f3c4af9c132439bbfeaed028ac3171db48 (patch)
tree0e32e3aa620a8b8a201b8c8b0c6bfebbde5489b0 /test/functional/lua/api_spec.lua
parentf8d55266e461a0a85e17e5adfd33e8429e45d9a5 (diff)
downloadrneovim-bca9c2f3c4af9c132439bbfeaed028ac3171db48.tar.gz
rneovim-bca9c2f3c4af9c132439bbfeaed028ac3171db48.tar.bz2
rneovim-bca9c2f3c4af9c132439bbfeaed028ac3171db48.zip
functests: Move existing tests from lua_spec to lua/*, fix them
Diffstat (limited to 'test/functional/lua/api_spec.lua')
-rw-r--r--test/functional/lua/api_spec.lua143
1 files changed, 143 insertions, 0 deletions
diff --git a/test/functional/lua/api_spec.lua b/test/functional/lua/api_spec.lua
index 6652c60adb..31c855c2c1 100644
--- a/test/functional/lua/api_spec.lua
+++ b/test/functional/lua/api_spec.lua
@@ -1,8 +1,10 @@
-- Test suite for testing interactions with API bindings
local helpers = require('test.functional.helpers')(after_each)
+local exc_exec = helpers.exc_exec
local funcs = helpers.funcs
local clear = helpers.clear
+local eval = helpers.eval
local NIL = helpers.NIL
local eq = helpers.eq
@@ -33,4 +35,145 @@ describe('luaeval(vim.api.…)', function()
funcs.luaeval('{pcall(vim.api.nvim_buf_set_lines, 1, 1, 2, false, {"b\\na"})}'))
end)
end)
+
+ it('correctly converts from API objects', function()
+ eq(1, funcs.luaeval('vim.api.nvim_eval("1")'))
+ eq('1', funcs.luaeval([[vim.api.nvim_eval('"1"')]]))
+ eq({}, funcs.luaeval('vim.api.nvim_eval("[]")'))
+ eq({}, funcs.luaeval('vim.api.nvim_eval("{}")'))
+ eq(1, funcs.luaeval('vim.api.nvim_eval("1.0")'))
+ eq(true, funcs.luaeval('vim.api.nvim_eval("v:true")'))
+ eq(false, funcs.luaeval('vim.api.nvim_eval("v:false")'))
+ eq(NIL, funcs.luaeval('vim.api.nvim_eval("v:null")'))
+
+ eq(0, eval([[type(luaeval('vim.api.nvim_eval("1")'))]]))
+ eq(1, eval([[type(luaeval('vim.api.nvim_eval("''1''")'))]]))
+ eq(3, eval([[type(luaeval('vim.api.nvim_eval("[]")'))]]))
+ eq(4, eval([[type(luaeval('vim.api.nvim_eval("{}")'))]]))
+ eq(5, eval([[type(luaeval('vim.api.nvim_eval("1.0")'))]]))
+ eq(6, eval([[type(luaeval('vim.api.nvim_eval("v:true")'))]]))
+ eq(6, eval([[type(luaeval('vim.api.nvim_eval("v:false")'))]]))
+ eq(7, eval([[type(luaeval('vim.api.nvim_eval("v:null")'))]]))
+
+ eq({foo=42}, funcs.luaeval([[vim.api.nvim_eval('{"foo": 42}')]]))
+ eq({42}, funcs.luaeval([[vim.api.nvim_eval('[42]')]]))
+
+ eq({foo={bar=42}, baz=50}, funcs.luaeval([[vim.api.nvim_eval('{"foo": {"bar": 42}, "baz": 50}')]]))
+ eq({{42}, {}}, funcs.luaeval([=[vim.api.nvim_eval('[[42], []]')]=]))
+ end)
+
+ it('correctly converts to API objects', function()
+ eq(1, funcs.luaeval('vim.api._vim_id(1)'))
+ eq('1', funcs.luaeval('vim.api._vim_id("1")'))
+ eq({1}, funcs.luaeval('vim.api._vim_id({1})'))
+ eq({foo=1}, funcs.luaeval('vim.api._vim_id({foo=1})'))
+ eq(1.5, funcs.luaeval('vim.api._vim_id(1.5)'))
+ eq(true, funcs.luaeval('vim.api._vim_id(true)'))
+ eq(false, funcs.luaeval('vim.api._vim_id(false)'))
+ eq(NIL, funcs.luaeval('vim.api._vim_id(nil)'))
+
+ eq(0, eval([[type(luaeval('vim.api._vim_id(1)'))]]))
+ eq(1, eval([[type(luaeval('vim.api._vim_id("1")'))]]))
+ eq(3, eval([[type(luaeval('vim.api._vim_id({1})'))]]))
+ eq(4, eval([[type(luaeval('vim.api._vim_id({foo=1})'))]]))
+ eq(5, eval([[type(luaeval('vim.api._vim_id(1.5)'))]]))
+ eq(6, eval([[type(luaeval('vim.api._vim_id(true)'))]]))
+ eq(6, eval([[type(luaeval('vim.api._vim_id(false)'))]]))
+ eq(7, eval([[type(luaeval('vim.api._vim_id(nil)'))]]))
+
+ eq({foo=1, bar={42, {{baz=true}, 5}}}, funcs.luaeval('vim.api._vim_id({foo=1, bar={42, {{baz=true}, 5}}})'))
+ end)
+
+ it('correctly converts container objects with type_idx to API objects', function()
+ eq(5, eval('type(luaeval("vim.api._vim_id({[vim.type_idx]=vim.types.float, [vim.val_idx]=0})"))'))
+ eq(4, eval([[type(luaeval('vim.api._vim_id({[vim.type_idx]=vim.types.dictionary})'))]]))
+ eq(3, eval([[type(luaeval('vim.api._vim_id({[vim.type_idx]=vim.types.array})'))]]))
+
+ eq({}, funcs.luaeval('vim.api._vim_id({[vim.type_idx]=vim.types.array})'))
+
+ -- Presence of type_idx makes Vim ignore some keys
+ eq({42}, funcs.luaeval('vim.api._vim_id({[vim.type_idx]=vim.types.array, [vim.val_idx]=10, [5]=1, foo=2, [1]=42})'))
+ eq({foo=2}, funcs.luaeval('vim.api._vim_id({[vim.type_idx]=vim.types.dictionary, [vim.val_idx]=10, [5]=1, foo=2, [1]=42})'))
+ eq(10, funcs.luaeval('vim.api._vim_id({[vim.type_idx]=vim.types.float, [vim.val_idx]=10, [5]=1, foo=2, [1]=42})'))
+ eq({}, funcs.luaeval('vim.api._vim_id({[vim.type_idx]=vim.types.array, [vim.val_idx]=10, [5]=1, foo=2})'))
+ end)
+
+ it('correctly converts arrays with type_idx to API objects', function()
+ eq(3, eval([[type(luaeval('vim.api._vim_id_array({[vim.type_idx]=vim.types.array})'))]]))
+
+ eq({}, funcs.luaeval('vim.api._vim_id_array({[vim.type_idx]=vim.types.array})'))
+
+ eq({42}, funcs.luaeval('vim.api._vim_id_array({[vim.type_idx]=vim.types.array, [vim.val_idx]=10, [5]=1, foo=2, [1]=42})'))
+ eq({{foo=2}}, funcs.luaeval('vim.api._vim_id_array({{[vim.type_idx]=vim.types.dictionary, [vim.val_idx]=10, [5]=1, foo=2, [1]=42}})'))
+ eq({10}, funcs.luaeval('vim.api._vim_id_array({{[vim.type_idx]=vim.types.float, [vim.val_idx]=10, [5]=1, foo=2, [1]=42}})'))
+ eq({}, funcs.luaeval('vim.api._vim_id_array({[vim.type_idx]=vim.types.array, [vim.val_idx]=10, [5]=1, foo=2})'))
+
+ eq({}, funcs.luaeval('vim.api._vim_id_array({})'))
+ eq(3, eval([[type(luaeval('vim.api._vim_id_array({})'))]]))
+ end)
+
+ it('correctly converts dictionaries with type_idx to API objects', function()
+ eq(4, eval([[type(luaeval('vim.api._vim_id_dictionary({[vim.type_idx]=vim.types.dictionary})'))]]))
+
+ eq({}, funcs.luaeval('vim.api._vim_id_dictionary({[vim.type_idx]=vim.types.dictionary})'))
+
+ eq({v={42}}, funcs.luaeval('vim.api._vim_id_dictionary({v={[vim.type_idx]=vim.types.array, [vim.val_idx]=10, [5]=1, foo=2, [1]=42}})'))
+ eq({foo=2}, funcs.luaeval('vim.api._vim_id_dictionary({[vim.type_idx]=vim.types.dictionary, [vim.val_idx]=10, [5]=1, foo=2, [1]=42})'))
+ eq({v=10}, funcs.luaeval('vim.api._vim_id_dictionary({v={[vim.type_idx]=vim.types.float, [vim.val_idx]=10, [5]=1, foo=2, [1]=42}})'))
+ eq({v={}}, funcs.luaeval('vim.api._vim_id_dictionary({v={[vim.type_idx]=vim.types.array, [vim.val_idx]=10, [5]=1, foo=2}})'))
+
+ -- If API requests dictionary, then empty table will be the one. This is not
+ -- the case normally because empty table is an empty arrray.
+ eq({}, funcs.luaeval('vim.api._vim_id_dictionary({})'))
+ eq(4, eval([[type(luaeval('vim.api._vim_id_dictionary({})'))]]))
+ end)
+
+ it('errors out correctly when working with API', function()
+ -- Conversion errors
+ eq('Vim(call):E5108: Error while calling lua chunk for luaeval(): [string "<VimL compiled string>"]:1: Cannot convert given lua type',
+ exc_exec([[call luaeval("vim.api._vim_id(vim.api._vim_id)")]]))
+ eq('Vim(call):E5108: Error while calling lua chunk for luaeval(): [string "<VimL compiled string>"]:1: Cannot convert given lua table',
+ exc_exec([[call luaeval("vim.api._vim_id({1, foo=42})")]]))
+ eq('Vim(call):E5108: Error while calling lua chunk for luaeval(): [string "<VimL compiled string>"]:1: Cannot convert given lua type',
+ exc_exec([[call luaeval("vim.api._vim_id({42, vim.api._vim_id})")]]))
+ -- Errors in number of arguments
+ eq('Vim(call):E5108: Error while calling lua chunk for luaeval(): [string "<VimL compiled string>"]:1: Expected 1 argument',
+ exc_exec([[call luaeval("vim.api._vim_id()")]]))
+ eq('Vim(call):E5108: Error while calling lua chunk for luaeval(): [string "<VimL compiled string>"]:1: Expected 1 argument',
+ exc_exec([[call luaeval("vim.api._vim_id(1, 2)")]]))
+ eq('Vim(call):E5108: Error while calling lua chunk for luaeval(): [string "<VimL compiled string>"]:1: Expected 2 arguments',
+ exc_exec([[call luaeval("vim.api.nvim_set_var(1, 2, 3)")]]))
+ -- Error in argument types
+ eq('Vim(call):E5108: Error while calling lua chunk for luaeval(): [string "<VimL compiled string>"]:1: Expected lua string',
+ exc_exec([[call luaeval("vim.api.nvim_set_var(1, 2)")]]))
+
+ eq('Vim(call):E5108: Error while calling lua chunk for luaeval(): [string "<VimL compiled string>"]:1: Expected lua number',
+ exc_exec([[call luaeval("vim.api.nvim_buf_get_lines(0, 'test', 1, false)")]]))
+ eq('Vim(call):E5108: Error while calling lua chunk for luaeval(): [string "<VimL compiled string>"]:1: Number is not integral',
+ exc_exec([[call luaeval("vim.api.nvim_buf_get_lines(0, 1.5, 1, false)")]]))
+
+ eq('Vim(call):E5108: Error while calling lua chunk for luaeval(): [string "<VimL compiled string>"]:1: Expected lua table',
+ exc_exec([[call luaeval("vim.api._vim_id_float('test')")]]))
+ eq('Vim(call):E5108: Error while calling lua chunk for luaeval(): [string "<VimL compiled string>"]:1: Unexpected type',
+ exc_exec([[call luaeval("vim.api._vim_id_float({[vim.type_idx]=vim.types.dictionary})")]]))
+
+ eq('Vim(call):E5108: Error while calling lua chunk for luaeval(): [string "<VimL compiled string>"]:1: Expected lua table',
+ exc_exec([[call luaeval("vim.api._vim_id_array(1)")]]))
+ eq('Vim(call):E5108: Error while calling lua chunk for luaeval(): [string "<VimL compiled string>"]:1: Unexpected type',
+ exc_exec([[call luaeval("vim.api._vim_id_array({[vim.type_idx]=vim.types.dictionary})")]]))
+
+ eq('Vim(call):E5108: Error while calling lua chunk for luaeval(): [string "<VimL compiled string>"]:1: Expected lua table',
+ exc_exec([[call luaeval("vim.api._vim_id_dictionary(1)")]]))
+ eq('Vim(call):E5108: Error while calling lua chunk for luaeval(): [string "<VimL compiled string>"]:1: Unexpected type',
+ exc_exec([[call luaeval("vim.api._vim_id_dictionary({[vim.type_idx]=vim.types.array})")]]))
+ -- TODO: check for errors with Tabpage argument
+ -- TODO: check for errors with Window argument
+ -- TODO: check for errors with Buffer argument
+ end)
+
+ it('accepts any value as API Boolean', function()
+ eq('', funcs.luaeval('vim.api.nvim_replace_termcodes("", vim, false, nil)'))
+ eq('', funcs.luaeval('vim.api.nvim_replace_termcodes("", 0, 1.5, "test")'))
+ eq('', funcs.luaeval('vim.api.nvim_replace_termcodes("", true, {}, {[vim.type_idx]=vim.types.array})'))
+ end)
end)