2015-04-26 05:31:39 -07:00
|
|
|
-- ShaDa variables saving/reading support
|
2024-04-09 04:26:16 -07:00
|
|
|
local t = require('test.functional.testutil')()
|
2024-04-08 02:03:20 -07:00
|
|
|
local api, fn, nvim_command, eq, eval = t.api, t.fn, t.command, t.eq, t.eval
|
|
|
|
local expect_exit = t.expect_exit
|
2015-04-26 05:31:39 -07:00
|
|
|
|
2024-04-08 02:03:20 -07:00
|
|
|
local t_shada = require('test.functional.shada.testutil')
|
|
|
|
local reset, clear = t_shada.reset, t_shada.clear
|
2015-04-26 05:31:39 -07:00
|
|
|
|
|
|
|
describe('ShaDa support code', function()
|
|
|
|
before_each(reset)
|
|
|
|
after_each(clear)
|
|
|
|
|
|
|
|
it('is able to dump and read back string variable', function()
|
2024-01-12 10:59:57 -07:00
|
|
|
api.nvim_set_var('STRVAR', 'foo')
|
2015-07-05 16:26:44 -07:00
|
|
|
nvim_command('set shada+=!')
|
|
|
|
nvim_command('wshada')
|
2015-04-26 05:31:39 -07:00
|
|
|
reset()
|
2015-07-05 16:26:44 -07:00
|
|
|
nvim_command('set shada+=!')
|
|
|
|
nvim_command('rshada')
|
2024-01-12 10:59:57 -07:00
|
|
|
eq('foo', api.nvim_get_var('STRVAR'))
|
2015-04-26 05:31:39 -07:00
|
|
|
end)
|
|
|
|
|
2016-02-06 11:03:33 -07:00
|
|
|
local autotest = function(tname, varname, varval, val_is_expr)
|
2015-04-26 05:31:39 -07:00
|
|
|
it('is able to dump and read back ' .. tname .. ' variable automatically', function()
|
2019-08-09 01:23:57 -07:00
|
|
|
reset('set shada+=!')
|
2016-02-06 11:03:33 -07:00
|
|
|
if val_is_expr then
|
|
|
|
nvim_command('let g:' .. varname .. ' = ' .. varval)
|
2024-01-12 10:59:57 -07:00
|
|
|
varval = api.nvim_get_var(varname)
|
2016-02-06 11:03:33 -07:00
|
|
|
else
|
2024-01-12 10:59:57 -07:00
|
|
|
api.nvim_set_var(varname, varval)
|
2016-02-06 11:03:33 -07:00
|
|
|
end
|
2021-07-30 07:23:37 -07:00
|
|
|
local vartype = eval('type(g:' .. varname .. ')')
|
2016-11-16 16:33:45 -07:00
|
|
|
-- Exit during `reset` is not a regular exit: it does not write shada
|
2015-04-26 05:31:39 -07:00
|
|
|
-- automatically
|
2022-06-08 14:22:50 -07:00
|
|
|
expect_exit(nvim_command, 'qall')
|
2019-08-09 01:23:57 -07:00
|
|
|
reset('set shada+=!')
|
2021-07-30 07:23:37 -07:00
|
|
|
eq(vartype, eval('type(g:' .. varname .. ')'))
|
2024-01-12 10:59:57 -07:00
|
|
|
eq(varval, api.nvim_get_var(varname))
|
2015-04-26 05:31:39 -07:00
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
|
|
|
autotest('string', 'STRVAR', 'foo')
|
|
|
|
autotest('number', 'NUMVAR', 42)
|
|
|
|
autotest('float', 'FLTVAR', 42.5)
|
|
|
|
autotest('dictionary', 'DCTVAR', { a = 10 })
|
|
|
|
autotest('list', 'LSTVAR', { { a = 10 }, { b = 10.5 }, { c = 'str' } })
|
2016-02-06 11:03:33 -07:00
|
|
|
autotest('true', 'TRUEVAR', true)
|
|
|
|
autotest('false', 'FALSEVAR', false)
|
|
|
|
autotest('null', 'NULLVAR', 'v:null', true)
|
|
|
|
autotest('ext', 'EXTVAR', '{"_TYPE": v:msgpack_types.ext, "_VAL": [2, ["", ""]]}', true)
|
2021-07-30 07:23:37 -07:00
|
|
|
autotest('blob', 'BLOBVAR', '0z12ab34cd', true)
|
|
|
|
autotest('blob (with NULs)', 'BLOBVARNULS', '0z004e554c7300', true)
|
2015-04-26 05:31:39 -07:00
|
|
|
|
2015-07-05 16:26:44 -07:00
|
|
|
it('does not read back variables without `!` in &shada', function()
|
2024-01-12 10:59:57 -07:00
|
|
|
api.nvim_set_var('STRVAR', 'foo')
|
2015-07-05 16:26:44 -07:00
|
|
|
nvim_command('set shada+=!')
|
|
|
|
nvim_command('wshada')
|
2019-08-09 01:23:57 -07:00
|
|
|
reset('set shada-=!')
|
2015-07-05 16:26:44 -07:00
|
|
|
nvim_command('rshada')
|
2024-01-12 10:59:57 -07:00
|
|
|
eq(0, fn.exists('g:STRVAR'))
|
2015-04-26 05:31:39 -07:00
|
|
|
end)
|
|
|
|
|
2015-07-05 16:26:44 -07:00
|
|
|
it('does not dump variables without `!` in &shada', function()
|
|
|
|
nvim_command('set shada-=!')
|
2024-01-12 10:59:57 -07:00
|
|
|
api.nvim_set_var('STRVAR', 'foo')
|
2015-07-05 16:26:44 -07:00
|
|
|
nvim_command('wshada')
|
2015-04-26 05:31:39 -07:00
|
|
|
reset()
|
2015-07-05 16:26:44 -07:00
|
|
|
nvim_command('set shada+=!')
|
|
|
|
nvim_command('rshada')
|
2024-01-12 10:59:57 -07:00
|
|
|
eq(0, fn.exists('g:STRVAR'))
|
2015-04-26 05:31:39 -07:00
|
|
|
end)
|
|
|
|
|
|
|
|
it('does not dump session variables', function()
|
2015-07-05 16:26:44 -07:00
|
|
|
nvim_command('set shada+=!')
|
2024-01-12 10:59:57 -07:00
|
|
|
api.nvim_set_var('StrVar', 'foo')
|
2015-07-05 16:26:44 -07:00
|
|
|
nvim_command('wshada')
|
2015-04-26 05:31:39 -07:00
|
|
|
reset()
|
2015-07-05 16:26:44 -07:00
|
|
|
nvim_command('set shada+=!')
|
|
|
|
nvim_command('rshada')
|
2024-01-12 10:59:57 -07:00
|
|
|
eq(0, fn.exists('g:StrVar'))
|
2015-04-26 05:31:39 -07:00
|
|
|
end)
|
|
|
|
|
|
|
|
it('does not dump regular variables', function()
|
2015-07-05 16:26:44 -07:00
|
|
|
nvim_command('set shada+=!')
|
2024-01-12 10:59:57 -07:00
|
|
|
api.nvim_set_var('str_var', 'foo')
|
2015-07-05 16:26:44 -07:00
|
|
|
nvim_command('wshada')
|
2015-04-26 05:31:39 -07:00
|
|
|
reset()
|
2015-07-05 16:26:44 -07:00
|
|
|
nvim_command('set shada+=!')
|
|
|
|
nvim_command('rshada')
|
2024-01-12 10:59:57 -07:00
|
|
|
eq(0, fn.exists('g:str_var'))
|
2015-04-26 05:31:39 -07:00
|
|
|
end)
|
2015-07-05 07:24:02 -07:00
|
|
|
|
2015-09-14 04:10:51 -07:00
|
|
|
it('dumps and loads variables correctly with utf-8 strings', function()
|
2015-07-05 07:24:02 -07:00
|
|
|
reset()
|
2024-01-12 10:59:57 -07:00
|
|
|
api.nvim_set_var('STRVAR', '«')
|
|
|
|
api.nvim_set_var('LSTVAR', { '«' })
|
|
|
|
api.nvim_set_var('DCTVAR', { ['«'] = '«' })
|
|
|
|
api.nvim_set_var('NESTEDVAR', { ['«'] = { { '«' }, { ['«'] = '«' }, { a = 'Test' } } })
|
2022-06-08 14:22:50 -07:00
|
|
|
expect_exit(nvim_command, 'qall')
|
2015-07-05 07:24:02 -07:00
|
|
|
reset()
|
2024-01-12 10:59:57 -07:00
|
|
|
eq('«', api.nvim_get_var('STRVAR'))
|
|
|
|
eq({ '«' }, api.nvim_get_var('LSTVAR'))
|
|
|
|
eq({ ['«'] = '«' }, api.nvim_get_var('DCTVAR'))
|
|
|
|
eq({ ['«'] = { { '«' }, { ['«'] = '«' }, { a = 'Test' } } }, api.nvim_get_var('NESTEDVAR'))
|
2015-07-05 07:24:02 -07:00
|
|
|
end)
|
|
|
|
|
2015-09-14 04:10:51 -07:00
|
|
|
it('dumps and loads variables correctly with 8-bit strings', function()
|
|
|
|
reset()
|
2015-07-05 07:24:02 -07:00
|
|
|
-- \171 is U+00AB LEFT-POINTING DOUBLE ANGLE QUOTATION MARK in latin1
|
2015-09-14 04:10:51 -07:00
|
|
|
-- This is invalid unicode, but we should still dump and restore it.
|
2024-01-12 10:59:57 -07:00
|
|
|
api.nvim_set_var('STRVAR', '\171')
|
|
|
|
api.nvim_set_var('LSTVAR', { '\171' })
|
|
|
|
api.nvim_set_var('DCTVAR', { ['«\171'] = '«\171' })
|
|
|
|
api.nvim_set_var(
|
2015-09-14 04:10:51 -07:00
|
|
|
'NESTEDVAR',
|
|
|
|
{ ['\171'] = { { '\171«' }, { ['\171'] = '\171' }, { a = 'Test' } } }
|
|
|
|
)
|
2022-06-08 14:22:50 -07:00
|
|
|
expect_exit(nvim_command, 'qall')
|
2015-07-05 07:24:02 -07:00
|
|
|
reset()
|
2024-01-12 10:59:57 -07:00
|
|
|
eq('\171', api.nvim_get_var('STRVAR'))
|
|
|
|
eq({ '\171' }, api.nvim_get_var('LSTVAR'))
|
|
|
|
eq({ ['«\171'] = '«\171' }, api.nvim_get_var('DCTVAR'))
|
2015-09-14 04:10:51 -07:00
|
|
|
eq(
|
|
|
|
{ ['\171'] = { { '\171«' }, { ['\171'] = '\171' }, { a = 'Test' } } },
|
2024-01-12 10:59:57 -07:00
|
|
|
api.nvim_get_var('NESTEDVAR')
|
2024-01-02 18:09:18 -07:00
|
|
|
)
|
2015-07-05 07:24:02 -07:00
|
|
|
end)
|
2015-11-23 04:24:25 -07:00
|
|
|
|
2020-06-06 09:31:23 -07:00
|
|
|
it('ignore when a funcref is stored in a variable', function()
|
2015-11-23 04:24:25 -07:00
|
|
|
nvim_command('let F = function("tr")')
|
2024-01-12 10:59:57 -07:00
|
|
|
api.nvim_set_var('U', '10')
|
2015-11-23 04:24:25 -07:00
|
|
|
nvim_command('set shada+=!')
|
2020-06-06 09:31:23 -07:00
|
|
|
nvim_command('wshada')
|
|
|
|
reset()
|
|
|
|
nvim_command('set shada+=!')
|
|
|
|
nvim_command('rshada')
|
2024-01-12 10:59:57 -07:00
|
|
|
eq('10', api.nvim_get_var('U'))
|
2020-06-06 09:31:23 -07:00
|
|
|
end)
|
|
|
|
|
|
|
|
it('ignore when a partial is stored in a variable', function()
|
|
|
|
nvim_command('let P = { -> 1 }')
|
2024-01-12 10:59:57 -07:00
|
|
|
api.nvim_set_var('U', '10')
|
2020-06-06 09:31:23 -07:00
|
|
|
nvim_command('set shada+=!')
|
|
|
|
nvim_command('wshada')
|
|
|
|
reset()
|
|
|
|
nvim_command('set shada+=!')
|
|
|
|
nvim_command('rshada')
|
2024-01-12 10:59:57 -07:00
|
|
|
eq('10', api.nvim_get_var('U'))
|
2015-11-23 04:24:25 -07:00
|
|
|
end)
|
|
|
|
|
2020-06-06 10:00:49 -07:00
|
|
|
it('ignore when a self-referencing list is stored in a variable', function()
|
2024-01-12 10:59:57 -07:00
|
|
|
api.nvim_set_var('L', {})
|
2015-11-23 04:24:25 -07:00
|
|
|
nvim_command('call add(L, L)')
|
2024-01-12 10:59:57 -07:00
|
|
|
api.nvim_set_var('U', '10')
|
2015-11-23 04:24:25 -07:00
|
|
|
nvim_command('set shada+=!')
|
2020-06-06 10:00:49 -07:00
|
|
|
nvim_command('wshada')
|
|
|
|
reset()
|
|
|
|
nvim_command('rshada')
|
2024-01-12 10:59:57 -07:00
|
|
|
eq('10', api.nvim_get_var('U'))
|
2015-11-23 04:24:25 -07:00
|
|
|
end)
|
2015-04-26 05:31:39 -07:00
|
|
|
end)
|