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