robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
f907614088
master
1-- Tests for undo tree and :earlier and :later. 2local t =require ( 'test.testutil' ) 3local n =require ( 'test.functional.testnvim' )() 4 5local describe , it , before_each , teardown =t . describe , t . it , t . before_each , t . teardown 6local feed_command =n . feed_command 7local write_file =t . write_file 8local command =n . command 9local source =n . source 10local expect =n . expect 11local clear =n . clear 12local feed =n . feed 13local eval =n . eval 14local eq =t . eq 15 16local function expect_empty_buffer () 17return expect ( '' ) 18end 19local function expect_line ( line ) 20return eq ( line , eval ( 'getline(".")' )) 21end 22 23describe ( 'undo tree:' , function () 24before_each ( clear ) 25local fname ='Xtest_functional_legacy_undotree' 26teardown ( function () 27os . remove ( fname .. '.source' ) 28end ) 29 30describe ( ':earlier and :later' , function () 31before_each ( function () 32os . remove ( fname ) 33end ) 34teardown ( function () 35os . remove ( fname ) 36end ) 37 38it ( 'time specifications, g- g+' , function () 39-- We write the test text to a file in order to prevent nvim to record 40-- the inserting of the text into the undo history. 41write_file ( fname , '\n123456789\n' ) 42 43-- `:earlier` and `:later` are (obviously) time-sensitive, so this test 44-- sometimes fails if the system is under load. It is wrapped in a local 45-- function to allow multiple attempts. 46local function test_earlier_later () 47clear () 48feed_command ( 'e ' .. fname ) 49-- Assert that no undo history is present. 50eq ( {} , eval ( 'undotree().entries' )) 51-- Delete three characters and undo. 52feed ( 'Gxxx' ) 53expect_line ( '456789' ) 54feed ( 'g-' ) 55expect_line ( '3456789' ) 56feed ( 'g-' ) 57expect_line ( '23456789' ) 58feed ( 'g-' ) 59expect_line ( '123456789' ) 60feed ( 'g-' ) 61expect_line ( '123456789' ) 62 63-- Delete three other characters and go back in time step by step. 64feed ( '$xxx' ) 65expect_line ( '123456' ) 66command ( 'sleep 1' ) 67feed ( 'g-' ) 68expect_line ( '1234567' ) 69feed ( 'g-' ) 70expect_line ( '12345678' ) 71feed ( 'g-' ) 72expect_line ( '456789' ) 73feed ( 'g-' ) 74expect_line ( '3456789' ) 75feed ( 'g-' ) 76expect_line ( '23456789' ) 77feed ( 'g-' ) 78expect_line ( '123456789' ) 79feed ( 'g-' ) 80expect_line ( '123456789' ) 81feed ( 'g-' ) 82expect_line ( '123456789' ) 83feed ( '10g+' ) 84expect_line ( '123456' ) 85 86-- Delay for two seconds and go some seconds forward and backward. 87command ( 'sleep 2' ) 88feed ( 'Aa<esc>' ) 89feed ( 'Ab<esc>' ) 90feed ( 'Ac<esc>' ) 91expect_line ( '123456abc' ) 92feed_command ( 'earlier 1s' ) 93expect_line ( '123456' ) 94feed_command ( 'earlier 3s' ) 95expect_line ( '123456789' ) 96feed_command ( 'later 1s' ) 97expect_line ( '123456' ) 98feed_command ( 'later 1h' ) 99expect_line ( '123456abc' ) 100end 101 102t . retry ( 2 , nil , test_earlier_later ) 103end ) 104 105it ( 'file-write specifications' , function () 106feed ( 'ione one one<esc>' ) 107feed_command ( 'w ' .. fname ) 108feed ( 'otwo<esc>' ) 109feed ( 'otwo<esc>' ) 110feed_command ( 'w' ) 111feed ( 'othree<esc>' ) 112feed_command ( 'earlier 1f' ) 113expect ( [[ 114one one one 115two 116two]] ) 117feed_command ( 'earlier 1f' ) 118expect ( 'one one one' ) 119feed_command ( 'earlier 1f' ) 120expect_empty_buffer () 121feed_command ( 'later 1f' ) 122expect ( 'one one one' ) 123feed_command ( 'later 1f' ) 124expect ( [[ 125one one one 126two 127two]] ) 128feed_command ( 'later 1f' ) 129expect ( [[ 130one one one 131two 132two 133three]] ) 134end ) 135end ) 136 137it ( 'scripts produce one undo-block for all changes by default' , function () 138source ( [[ 139normal Aaaaa 140normal obbbb 141normal occcc 142]] ) 143expect ( [[ 144aaaa 145bbbb 146cccc]] ) 147feed ( 'u' ) 148expect_empty_buffer () 149end ) 150 151it ( "setting 'undolevel' can break undo-blocks (inside scripts)" , function () 152-- :source is required (because interactive changes are _not_ grouped, 153-- even with :undojoin). 154source ( [[ 155normal Aaaaa 156set ul=100 157normal obbbb 158set ul=100 159normal occcc 160]] ) 161expect ( [[ 162aaaa 163bbbb 164cccc]] ) 165feed ( 'u' ) 166expect ( [[ 167aaaa 168bbbb]] ) 169feed ( 'u' ) 170expect ( 'aaaa' ) 171feed ( 'u' ) 172expect_empty_buffer () 173end ) 174 175it ( ':undojoin can join undo-blocks inside scripts' , function () 176feed ( 'Goaaaa<esc>' ) 177feed ( 'obbbb<esc>u' ) 178expect_line ( 'aaaa' ) 179source ( [[ 180normal obbbb 181set ul=100 182undojoin 183normal occcc 184]] ) 185feed ( 'u' ) 186expect_line ( 'aaaa' ) 187end ) 188 189it ( 'undo an expression-register' , function () 190local normal_commands ='o1\027a2\018=string(123)\n\027' 191write_file ( fname .. '.source' , normal_commands ) 192 193feed ( 'oa<esc>' ) 194feed ( 'ob<esc>' ) 195feed ( [[o1<esc>a2<C-R>=setline('.','1234')<cr><esc>]] ) 196expect ( [[ 197 198a 199b 20012034]] ) 201feed ( 'uu' ) 202expect ( [[ 203 204a 205b 2061]] ) 207feed ( 'oc<esc>' ) 208feed ( [[o1<esc>a2<C-R>=setline('.','1234')<cr><esc>]] ) 209expect ( [[ 210 211a 212b 2131 214c 21512034]] ) 216feed ( 'u' ) 217expect ( [[ 218 219a 220b 2211 222c 22312]] ) 224feed ( 'od<esc>' ) 225feed_command ( 'so! ' .. fname .. '.source' ) 226expect ( [[ 227 228a 229b 2301 231c 23212 233d 23412123]] ) 235feed ( 'u' ) 236expect ( [[ 237 238a 239b 2401 241c 24212 243d]] ) 244 245-- The above behaviour was tested in the legacy Vim test because the 246-- legacy tests were executed with ':so!'. The behavior differs for 247-- interactive use (even in Vim; see ":help :undojoin"): 248feed ( normal_commands ) 249expect ( [[ 250 251a 252b 2531 254c 25512 256d 25712123]] ) 258feed ( 'u' ) 259expect ( [[ 260 261a 262b 2631 264c 26512 266d 2671]] ) 268end ) 269end )