local t = require('test.testutil')
local n = require('test.functional.testnvim')()
local Screen = require('test.functional.ui.screen')
local describe, it, before_each, pending = t.describe, t.it, t.before_each, t.pending
local NIL = vim.NIL
local feed = n.feed
local command = n.command
local clear = n.clear
local api = n.api
local eq = t.eq
local eval = n.eval
local exec = n.exec
local exec_lua = n.exec_lua
local fn = n.fn
local matches = t.matches
local mkdir_p = n.mkdir_p
local pcall_err = t.pcall_err
local rmdir = n.rmdir
local write_file = t.write_file
local function eq_exec_lua(expected, f)
eq(expected, exec_lua(f))
end
describe('lua stdlib', function()
before_each(clear)
describe('variables', function()
it('vim.g', function()
exec_lua(function()
vim.api.nvim_set_var('testing', 'hi')
vim.api.nvim_set_var('other', 123)
vim.api.nvim_set_var('floaty', 5120.1)
vim.api.nvim_set_var('nullvar', vim.NIL)
vim.api.nvim_set_var('to_delete', { hello = 'world' })
end)
eq('hi', fn.luaeval 'vim.g.testing')
eq(123, fn.luaeval 'vim.g.other')
eq(5120.1, fn.luaeval 'vim.g.floaty')
eq(NIL, fn.luaeval 'vim.g.nonexistent')
eq(NIL, fn.luaeval 'vim.g.nullvar')
-- lost over RPC, so test locally:
eq_exec_lua({ false, true }, function()
return { vim.g.nonexistent == vim.NIL, vim.g.nullvar == vim.NIL }
end)
eq({ hello = 'world' }, fn.luaeval 'vim.g.to_delete')
exec_lua [[
vim.g.to_delete = nil
]]
eq(NIL, fn.luaeval 'vim.g.to_delete')
matches([[attempt to index .* nil value]], pcall_err(exec_lua, 'return vim.g[0].testing'))
exec_lua(function()
local counter = 0
local function add_counter()
counter = counter + 1
end
local function get_counter()
return counter
end
vim.g.AddCounter = add_counter
vim.g.GetCounter = get_counter
vim.g.fn = { add = add_counter, get = get_counter }
vim.g.AddParens = function(s)
return '(' .. s .. ')'
end
end)
eq(0, eval('g:GetCounter()'))
eval('g:AddCounter()')
eq(1, eval('g:GetCounter()'))
eval('g:AddCounter()')
eq(2, eval('g:GetCounter()'))
exec_lua([[vim.g.AddCounter()]])
eq(3, exec_lua([[return vim.g.GetCounter()]]))
exec_lua([[vim.api.nvim_get_var('AddCounter')()]])
eq(
|