diff options
author | TJ DeVries <devries.timothyj@gmail.com> | 2020-06-30 02:05:06 -0400 |
---|---|---|
committer | TJ DeVries <devries.timothyj@gmail.com> | 2020-07-10 20:23:12 -0400 |
commit | 6360cf7ce87407bd8a519b9a17f45b2148291904 (patch) | |
tree | 6f770f564307cb8a31f6ead427114267fcba3953 /test/functional/lua/luaeval_spec.lua | |
parent | 971a191c4d772493535d55524b994fe385fae546 (diff) | |
download | rneovim-6360cf7ce87407bd8a519b9a17f45b2148291904.tar.gz rneovim-6360cf7ce87407bd8a519b9a17f45b2148291904.tar.bz2 rneovim-6360cf7ce87407bd8a519b9a17f45b2148291904.zip |
lua: Add ability to pass tables with __call
vim-patch:8.2.1054: not so easy to pass a lua function to Vim
vim-patch:8.2.1084: Lua: registering function has useless code
I think I have also opened up the possibility for people to use these
callbacks elsewhere, since I've added a new struct that we should be
able to use.
Also, this should allow us to determine what the state of a list is in
Lua or a dictionary in Lua, since we now can track the luaref as we go.
Diffstat (limited to 'test/functional/lua/luaeval_spec.lua')
-rw-r--r-- | test/functional/lua/luaeval_spec.lua | 78 |
1 files changed, 72 insertions, 6 deletions
diff --git a/test/functional/lua/luaeval_spec.lua b/test/functional/lua/luaeval_spec.lua index 4faacecd86..964ea4561e 100644 --- a/test/functional/lua/luaeval_spec.lua +++ b/test/functional/lua/luaeval_spec.lua @@ -295,13 +295,12 @@ describe('luaeval()', function() ]]) end) - -- TODO(tjdevries): Need to figure - pending('should work with metatables using __call', function() - eq(true, exec_lua [[ + it('should work with metatables using __call', function() + eq(1, exec_lua [[ local this_is_local_variable = false - local callable_table = setmetatable({}, { - __call = function(...) - this_is_local_variable = true + local callable_table = setmetatable({x = 1}, { + __call = function(t, ...) + this_is_local_variable = t.x end }) @@ -315,6 +314,73 @@ describe('luaeval()', function() ]]) end) + it('should handle being called from a timer once.', function() + eq(3, exec_lua [[ + local this_is_local_variable = false + local callable_table = setmetatable({5, 4, 3, 2, 1}, { + __call = function(t, ...) this_is_local_variable = t[3] end + }) + + vim.fn.timer_start(5, callable_table) + vim.wait(1000, function() + return this_is_local_variable + end) + + return this_is_local_variable + ]]) + end) + + it('should call functions once with __call metamethod', function() + eq(true, exec_lua [[ + local this_is_local_variable = false + local callable_table = setmetatable({a = true, b = false}, { + __call = function(t, ...) this_is_local_variable = t.a end + }) + + assert(getmetatable(callable_table).__call) + vim.fn.call(callable_table, {}) + + return this_is_local_variable + ]]) + end) + + it('should work with lists using __call', function() + eq(3, exec_lua [[ + local this_is_local_variable = false + local mt = { + __call = function(t, ...) + this_is_local_variable = t[3] + end + } + local callable_table = setmetatable({5, 4, 3, 2, 1}, mt) + + -- Call it once... + vim.fn.timer_start(5, callable_table) + vim.wait(1000, function() + return this_is_local_variable + end) + + assert(this_is_local_variable) + this_is_local_variable = false + + vim.fn.timer_start(5, callable_table) + vim.wait(1000, function() + return this_is_local_variable + end) + + return this_is_local_variable + ]]) + end) + + it('should not work with tables not using __call', function() + eq({false, 'Vim:E921: Invalid callback argument'}, exec_lua [[ + local this_is_local_variable = false + local callable_table = setmetatable({x = 1}, {}) + + return {pcall(function() vim.fn.timer_start(5, callable_table) end)} + ]]) + end) + it('correctly converts containers with type_idx', function() eq(5, eval('type(luaeval("{[vim.type_idx]=vim.types.float, [vim.val_idx]=0}"))')) eq(4, eval([[type(luaeval('{[vim.type_idx]=vim.types.dictionary}'))]])) |