robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

Justin M. KeyesMerge #40865 from justinmk/fixcif907614088

master
5.4 KiB163 linesraw
1-- ShaDa variables saving/reading support
2local t = require('test.testutil')
3local n = require('test.functional.testnvim')()
4local t_shada = require('test.functional.shada.testutil')
5
6local describe, it, before_each, after_each = t.describe, t.it, t.before_each, t.after_each
7local api, fn, nvim_command, eq, eval = n.api, n.fn, n.command, t.eq, n.eval
8local expect_exit = n.expect_exit
9local reset, clear = t_shada.reset, t_shada.clear
10
11describe('ShaDa support code', function()
12  before_each(reset)
13  after_each(clear)
14
15  it('is able to dump and read back string variable', function()
16    api.nvim_set_var('STRVAR', 'foo')
17    nvim_command('set shada+=!')
18    nvim_command('wshada')
19    reset()
20    nvim_command('set shada+=!')
21    nvim_command('rshada')
22    eq('foo', api.nvim_get_var('STRVAR'))
23  end)
24
25  local autotest = function(tname, varname, varval, val_is_expr)
26    it('is able to dump and read back ' .. tname .. ' variable automatically', function()
27      reset('set shada+=!')
28      if val_is_expr then
29        nvim_command('let g:' .. varname .. ' = ' .. varval)
30        varval = api.nvim_get_var(varname)
31      else
32        api.nvim_set_var(varname, varval)
33      end
34      local vartype = eval('type(g:' .. varname .. ')')
35      -- Exit during `reset` is not a regular exit: it does not write shada
36      -- automatically
37      expect_exit(nvim_command, 'qall')
38      reset('set shada+=!')
39      eq(vartype, eval('type(g:' .. varname .. ')'))
40      eq(varval, api.nvim_get_var(varname))
41    end)
42  end
43
44  autotest('string', 'STRVAR', 'foo')
45  autotest('number', 'NUMVAR', 42)
46  autotest('float', 'FLTVAR', 42.5)
47  autotest('dictionary', 'DCTVAR', { a = 10 })
48  autotest('list', 'LSTVAR', { { a = 10 }, { b = 10.5 }, { c = 'str' } })
49  autotest('true', 'TRUEVAR', true)
50  autotest('false', 'FALSEVAR', false)
51  autotest('null', 'NULLVAR', 'v:null', true)
52  autotest('ext', 'EXTVAR', '{"_TYPE": v:msgpack_types.ext, "_VAL": [2, ["", ""]]}', true)
53  autotest('blob', 'BLOBVAR', '0z12ab34cd', true)
54  autotest('blob (with NULs)', 'BLOBVARNULS', '0z004e554c7300', true)
55
56  it('does not read back variables without `!` in &shada', function()
57    api.nvim_set_var('STRVAR', 'foo')
58    nvim_command('set shada+=!')
59    nvim_command('wshada')
60    reset('set shada-=!')
61    nvim_command('rshada')
62    eq(0, fn.exists('g:STRVAR'))
63  end)
64
65  it('does not dump variables without `!` in &shada', function()
66    nvim_command('set shada-=!')
67    api.nvim_set_var('STRVAR', 'foo')
68    nvim_command('wshada')
69    reset()
70    nvim_command('set shada+=!')
71    nvim_command('rshada')
72    eq(0, fn.exists('g:STRVAR'))
73  end)
74
75  it('does not dump session variables', function()
76    nvim_command('set shada+=!')
77    api.nvim_set_var('StrVar', 'foo')
78    nvim_command('wshada')
79    reset()
80    nvim_command('set shada+=!')
81    nvim_command('rshada')
82    eq(0, fn.exists('g:StrVar'))
83  end)
84
85  it('does not dump regular variables', function()
86    nvim_command('set shada+=!')
87    api.nvim_set_var('str_var', 'foo')
88    nvim_command('wshada')
89    reset()
90    nvim_command('set shada+=!')
91    nvim_command('rshada')
92    eq(0, fn.exists('g:str_var'))
93  end)
94
95  it('dumps and loads variables correctly with utf-8 strings', function()
96    reset()
97    api.nvim_set_var('STRVAR', '«')
98    api.nvim_set_var('LSTVAR', { '«' })
99    api.nvim_set_var('DCTVAR', { ['«'] = '«' })
100    api.nvim_set_var('NESTEDVAR', { ['«'] = { { '«' }, { ['«'] = '«' }, { a = 'Test' } } })
101    expect_exit(nvim_command, 'qall')
102    reset()
103    eq('«', api.nvim_get_var('STRVAR'))
104    eq({ '«' }, api.nvim_get_var('LSTVAR'))
105    eq({ ['«'] = '«' }, api.nvim_get_var('DCTVAR'))
106    eq({ ['«'] = { { '«' }, { ['«'] = '«' }, { a = 'Test' } } }, api.nvim_get_var('NESTEDVAR'))
107  end)
108
109  it('dumps and loads variables correctly with 8-bit strings', function()
110    reset()
111    -- \171 is U+00AB LEFT-POINTING DOUBLE ANGLE QUOTATION MARK in latin1
112    -- This is invalid unicode, but we should still dump and restore it.
113    api.nvim_set_var('STRVAR', '\171')
114    api.nvim_set_var('LSTVAR', { '\171' })
115    api.nvim_set_var('DCTVAR', { ['«\171'] = '«\171' })
116    api.nvim_set_var(
117      'NESTEDVAR',
118      { ['\171'] = { { '\171«' }, { ['\171'] = '\171' }, { a = 'Test' } } }
119    )
120    expect_exit(nvim_command, 'qall')
121    reset()
122    eq('\171', api.nvim_get_var('STRVAR'))
123    eq({ '\171' }, api.nvim_get_var('LSTVAR'))
124    eq({ ['«\171'] = '«\171' }, api.nvim_get_var('DCTVAR'))
125    eq(
126      { ['\171'] = { { '\171«' }, { ['\171'] = '\171' }, { a = 'Test' } } },
127      api.nvim_get_var('NESTEDVAR')
128    )
129  end)
130
131  it('ignore when a funcref is stored in a variable', function()
132    nvim_command('let F = function("tr")')
133    api.nvim_set_var('U', '10')
134    nvim_command('set shada+=!')
135    nvim_command('wshada')
136    reset()
137    nvim_command('set shada+=!')
138    nvim_command('rshada')
139    eq('10', api.nvim_get_var('U'))
140  end)
141
142  it('ignore when a partial is stored in a variable', function()
143    nvim_command('let P = { -> 1 }')
144    api.nvim_set_var('U', '10')
145    nvim_command('set shada+=!')
146    nvim_command('wshada')
147    reset()
148    nvim_command('set shada+=!')
149    nvim_command('rshada')
150    eq('10', api.nvim_get_var('U'))
151  end)
152
153  it('ignore when a self-referencing list is stored in a variable', function()
154    api.nvim_set_var('L', {})
155    nvim_command('call add(L, L)')
156    api.nvim_set_var('U', '10')
157    nvim_command('set shada+=!')
158    nvim_command('wshada')
159    reset()
160    nvim_command('rshada')
161    eq('10', api.nvim_get_var('U'))
162  end)
163end)