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