robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
f907614088
master
1-- ShaDa history 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 nvim_command , fn , api , nvim_feed , eq =n . command , n . fn , n . api , n . feed , t . eq 8local assert_alive =n . assert_alive 9local expect_exit =n . expect_exit 10local pcall_err =t . pcall_err 11 12local reset , clear =t_shada . reset , t_shada . clear 13 14describe ( 'ShaDa support code' , function () 15before_each ( reset ) 16after_each ( clear ) 17 18it ( 'is able to dump and read back command-line history' , function () 19nvim_command ( "set shada='0" ) 20nvim_feed ( ':" Test\n' ) 21nvim_command ( 'wshada' ) 22reset () 23nvim_command ( "set shada='0" ) 24nvim_command ( 'rshada' ) 25eq ( '" Test' , fn . histget ( ':' , - 1 )) 26end ) 27 28it ( 'is able to dump and read back 2 items in command-line history' , function () 29nvim_command ( "set shada='0 history=2" ) 30nvim_feed ( ':" Test\n' ) 31nvim_feed ( ':" Test 2\n' ) 32expect_exit ( nvim_command , 'qall' ) 33reset () 34nvim_command ( "set shada='0 history=2" ) 35nvim_command ( 'rshada' ) 36eq ( '" Test 2' , fn . histget ( ':' , - 1 )) 37eq ( '" Test' , fn . histget ( ':' , - 2 )) 38end ) 39 40it ( 'respects &history when dumping' , function () 41nvim_command ( "set shada='0 history=1" ) 42nvim_feed ( ':" Test\n' ) 43nvim_feed ( ':" Test 2\n' ) 44nvim_command ( 'wshada' ) 45reset () 46nvim_command ( "set shada='0 history=2" ) 47nvim_command ( 'rshada' ) 48eq ( '" Test 2' , fn . histget ( ':' , - 1 )) 49eq ( '' , fn . histget ( ':' , - 2 )) 50end ) 51 52it ( 'respects &history when loading' , function () 53nvim_command ( "set shada='0 history=2" ) 54nvim_feed ( ':" Test\n' ) 55nvim_feed ( ':" Test 2\n' ) 56nvim_command ( 'wshada' ) 57reset () 58nvim_command ( "set shada='0 history=1" ) 59nvim_command ( 'rshada' ) 60eq ( '" Test 2' , fn . histget ( ':' , - 1 )) 61eq ( '' , fn . histget ( ':' , - 2 )) 62end ) 63 64it ( 'dumps only requested amount of command-line history items' , function () 65nvim_command ( "set shada='0,:1" ) 66nvim_feed ( ':" Test\n' ) 67nvim_feed ( ':" Test 2\n' ) 68nvim_command ( 'wshada' ) 69-- Regression test: :wshada should not alter or free history. 70eq ( '" Test 2' , fn . histget ( ':' , - 1 )) 71eq ( '" Test' , fn . histget ( ':' , - 2 )) 72reset () 73nvim_command ( "set shada='0" ) 74nvim_command ( 'rshada' ) 75eq ( '" Test 2' , fn . histget ( ':' , - 1 )) 76eq ( '' , fn . histget ( ':' , - 2 )) 77end ) 78 79it ( 'does not respect number in &shada when loading history' , function () 80nvim_command ( "set shada='0" ) 81nvim_feed ( ':" Test\n' ) 82nvim_feed ( ':" Test 2\n' ) 83nvim_command ( 'wshada' ) 84reset () 85nvim_command ( "set shada='0,:1" ) 86nvim_command ( 'rshada' ) 87eq ( '" Test 2' , fn . histget ( ':' , - 1 )) 88eq ( '" Test' , fn . histget ( ':' , - 2 )) 89end ) 90 91it ( 'dumps and loads all kinds of histories' , function () 92nvim_command ( 'debuggreedy' ) 93nvim_feed ( ':debug echo "Test"\n" Test 2\nc\n' ) -- Debug history. 94nvim_feed ( ':call input("")\nTest 2\n' ) -- Input history. 95nvim_feed ( '"="Test"\nyy' ) -- Expression history. 96nvim_feed ( '/Test\n' ) -- Search history 97nvim_feed ( ':" Test\n' ) -- Command-line history 98nvim_command ( '0debuggreedy' ) 99nvim_command ( 'wshada' ) 100reset () 101nvim_command ( 'rshada' ) 102eq ( '" Test' , fn . histget ( ':' , - 1 )) 103eq ( 'Test' , fn . histget ( '/' , - 1 )) 104eq ( '"Test"' , fn . histget ( '=' , - 1 )) 105eq ( 'Test 2' , fn . histget ( '@' , - 1 )) 106eq ( 'c' , fn . histget ( '>' , - 1 )) 107end ) 108 109it ( 'does not dump last search pattern if /0 in shada' , function () 110nvim_command ( 'set shada+=/0' ) 111nvim_command ( 'silent! /test/' ) 112expect_exit ( nvim_command , 'qall!' ) 113reset () 114nvim_command ( 'language C' ) 115local err =pcall_err ( n . exec_capture , 'normal! n' ) 116eq ( 'nvim_exec2(), line 1: Vim(normal):E35: No previous regular expression' , err ) 117end ) 118 119it ( 'dumps and loads last search pattern with offset' , function () 120api . nvim_set_option_value ( 'wrapscan' , false , {} ) 121fn . setline ( '.' , { 'foo' , 'bar--' } ) 122nvim_feed ( 'gg0/a/e+1\n' ) 123eq ( { 0 , 2 , 3 , 0 } , fn . getpos ( '.' )) 124nvim_command ( 'wshada' ) 125reset () 126api . nvim_set_option_value ( 'wrapscan' , false , {} ) 127fn . setline ( '.' , { 'foo' , 'bar--' } ) 128nvim_feed ( 'gg0n' ) 129eq ( { 0 , 2 , 3 , 0 } , fn . getpos ( '.' )) 130eq ( 1 , api . nvim_get_vvar ( 'searchforward' )) 131-- Autocommands shouldn't cause search pattern to change 132nvim_command ( 'autocmd User * :' ) 133nvim_command ( 'doautocmd User' ) 134nvim_feed ( 'gg0n' ) 135eq ( { 0 , 2 , 3 , 0 } , fn . getpos ( '.' )) 136eq ( 1 , api . nvim_get_vvar ( 'searchforward' )) 137end ) 138 139it ( 'dumps and loads last search pattern with offset and backward direction' , function () 140api . nvim_set_option_value ( 'wrapscan' , false , {} ) 141fn . setline ( '.' , { 'foo' , 'bar--' } ) 142nvim_feed ( 'G$?a?e+1\n' ) 143eq ( { 0 , 2 , 3 , 0 } , fn . getpos ( '.' )) 144nvim_command ( 'wshada' ) 145reset () 146api . nvim_set_option_value ( 'wrapscan' , false , {} ) 147fn . setline ( '.' , { 'foo' , 'bar--' } ) 148nvim_feed ( 'G$n' ) 149eq ( { 0 , 2 , 3 , 0 } , fn . getpos ( '.' )) 150eq ( 0 , api . nvim_get_vvar ( 'searchforward' )) 151-- Autocommands shouldn't cause search pattern to change 152nvim_command ( 'autocmd User * :' ) 153nvim_command ( 'doautocmd User' ) 154nvim_feed ( 'G$n' ) 155eq ( { 0 , 2 , 3 , 0 } , fn . getpos ( '.' )) 156eq ( 0 , api . nvim_get_vvar ( 'searchforward' )) 157end ) 158 159it ( 'saves v:hlsearch=1' , function () 160nvim_command ( 'set hlsearch shada-=h' ) 161nvim_feed ( '/test\n' ) 162eq ( 1 , api . nvim_get_vvar ( 'hlsearch' )) 163expect_exit ( nvim_command , 'qall' ) 164reset () 165eq ( 1 , api . nvim_get_vvar ( 'hlsearch' )) 166end ) 167 168it ( 'saves v:hlsearch=0 with :nohl' , function () 169nvim_command ( 'set hlsearch shada-=h' ) 170nvim_feed ( '/test\n' ) 171nvim_command ( 'nohlsearch' ) 172expect_exit ( nvim_command , 'qall' ) 173reset () 174eq ( 0 , api . nvim_get_vvar ( 'hlsearch' )) 175end ) 176 177it ( 'saves v:hlsearch=0 with default &shada' , function () 178nvim_command ( 'set hlsearch' ) 179nvim_feed ( '/test\n' ) 180eq ( 1 , api . nvim_get_vvar ( 'hlsearch' )) 181expect_exit ( nvim_command , 'qall' ) 182reset () 183eq ( 0 , api . nvim_get_vvar ( 'hlsearch' )) 184end ) 185 186it ( 'dumps and loads last substitute pattern and replacement string' , function () 187fn . setline ( '.' , { 'foo' , 'bar' } ) 188nvim_command ( '%s/f/g/g' ) 189eq ( 'goo' , fn . getline ( 1 )) 190nvim_command ( 'wshada' ) 191reset () 192fn . setline ( '.' , { 'foo' , 'bar' } ) 193nvim_command ( '&' ) 194eq ( 'goo' , fn . getline ( 1 )) 195end ) 196 197it ( 'does not dump last substitute pattern or replacement string if /0 in shada' , function () 198nvim_command ( 'set shada+=/0' ) 199fn . setline ( '.' , { 'foo' , 'bar' } ) 200nvim_command ( '%s/f/g/g' ) 201eq ( 'goo' , fn . getline ( 1 )) 202expect_exit ( nvim_command , 'qall!' ) 203reset () 204fn . setline ( '.' , { 'foo' , 'bar' } ) 205nvim_command ( 'language C' ) 206local err =pcall_err ( nvim_command , '&' ) 207eq ( 'Vim(&):E33: No previous substitute regular expression' , err ) 208nvim_feed ( '/f\n' ) 209err =pcall_err ( nvim_command , '~&' ) 210eq ( 'Vim(~):E33: No previous substitute regular expression' , err ) 211end ) 212 213it ( 'dumps and loads history with UTF-8 characters' , function () 214reset () 215nvim_feed ( ':echo "«"\n' ) 216expect_exit ( nvim_command , 'qall' ) 217reset () 218eq ( 'echo "«"' , fn . histget ( ':' , - 1 )) 219end ) 220 221it ( 'dumps and loads replacement with UTF-8 characters' , function () 222nvim_command ( 'substitute/./«/ge' ) 223expect_exit ( nvim_command , 'qall!' ) 224reset () 225fn . setline ( '.' , { '.' } ) 226nvim_command ( '&' ) 227eq ( '«' , fn . getline ( '.' )) 228end ) 229 230it ( 'dumps and loads substitute pattern with UTF-8 characters' , function () 231nvim_command ( 'substitute/«/./ge' ) 232expect_exit ( nvim_command , 'qall!' ) 233reset () 234fn . setline ( '.' , { '«\171' } ) 235nvim_command ( '&' ) 236eq ( '.\171' , fn . getline ( '.' )) 237end ) 238 239it ( 'dumps and loads search pattern with UTF-8 characters' , function () 240nvim_command ( 'silent! /«/' ) 241expect_exit ( nvim_command , 'qall!' ) 242reset () 243fn . setline ( '.' , { 'foo' , '\171«' } ) 244nvim_feed ( 'gg0n' ) 245eq ( { 0 , 2 , 2 , 0 } , fn . getpos ( '.' )) 246eq ( '\171«' , fn . getline ( '.' )) 247eq ( '' , fn . histget ( '/' , - 1 )) 248end ) 249 250it ( 'dumps and loads search pattern with 8-bit single-byte' , function () 251-- \171 is U+00AB LEFT-POINTING DOUBLE ANGLE QUOTATION MARK in latin1 252nvim_command ( 'silent! /\171/' ) 253expect_exit ( nvim_command , 'qall!' ) 254reset () 255fn . setline ( '.' , { 'foo' , '\171«' } ) 256nvim_feed ( 'gg0n' ) 257eq ( { 0 , 2 , 1 , 0 } , fn . getpos ( '.' )) 258eq ( '\171«' , fn . getline ( '.' )) 259eq ( '' , fn . histget ( '/' , - 1 )) 260end ) 261 262it ( 'does not crash when dumping last search pattern (#10945)' , function () 263nvim_command ( 'edit Xtest-functional-shada-history_spec' ) 264-- Save jump list 265nvim_command ( 'wshada' ) 266-- Wipe out buffer list (jump list entry gets removed) 267nvim_command ( '%bwipeout' ) 268-- Restore jump list 269nvim_command ( 'rshada' ) 270nvim_command ( 'silent! /pat/' ) 271nvim_command ( 'au BufNew * echo' ) 272nvim_command ( 'wshada' ) 273end ) 274 275it ( 'does not crash when number of history save to zero (#11497)' , function () 276nvim_command ( "set shada='10" ) 277nvim_feed ( ':" Test\n' ) 278nvim_command ( 'wshada' ) 279nvim_command ( "set shada='10,:0" ) 280nvim_command ( 'wshada' ) 281assert_alive () 282end ) 283 284it ( 'does not dump empty replacement string' , function () 285expect_exit ( nvim_command , 'qall!' ) 286reset () 287fn . setline ( '.' , { 'foo' , 'bar' } ) 288nvim_command ( 'language C' ) 289nvim_feed ( '/f\n' ) 290local err =pcall_err ( nvim_command , '~&' ) 291eq ( 'Vim(~):E33: No previous substitute regular expression' , err ) 292end ) 293end )