robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
f907614088
master
1local t =require ( 'test.testutil' ) 2local n =require ( 'test.functional.testnvim' )() 3 4local describe , it , before_each =t . describe , t . it , t . before_each 5local clear =n . clear 6local eq =t . eq 7local exec =n . exec 8local api =n . api 9local dedent =t . dedent 10local Screen =require ( 'test.functional.ui.screen' ) 11 12---@param reverse_tree {[integer]:integer} 13local function generate_undo_tree_from_rev ( reverse_tree ) 14for k , v in ipairs ( reverse_tree ) do 15exec ( 'undo ' .. v ) 16api . nvim_buf_set_lines ( 0 , 0 , - 1 , true , { tostring ( k ) } ) 17end 18end 19---@param buf integer 20---@return string 21local function buf_get_lines_and_extmark ( buf ) 22local lines =api . nvim_buf_get_lines ( buf , 0 , - 1 , true ) 23local ns =api . nvim_create_namespace ( 'nvim.undotree' ) 24local extmarks =api . nvim_buf_get_extmarks ( buf , ns , 0 , - 1 , { details =true } ) 25for i =# extmarks , 1 , - 1 do 26local extmark =extmarks [ i ] 27---@type nil,integer,nil,vim.api.keyset.extmark_details 28local _ , row , _ , opts =unpack ( extmark ) 29local virt_lines =assert ( opts . virt_lines ) 30for _ , v in ipairs ( virt_lines ) do 31local virt_line =v [ 1 ][ 1 ] 32table . insert ( lines , row + 2 , virt_line ) 33end 34end 35return table . concat ( lines , '\n' ) 36end 37 38local function strip_time ( text ) 39return text : gsub ( '%s-%(.-%)' , '' ) 40end 41 42describe ( ':Undotree' , function () 43before_each ( function () 44clear ( { 45args ={ 46'--clean' , 47-- autoread plugin causes flaky tests 48'--cmd' , 49'let g:loaded_autoread=1' , 50} , 51} ) 52exec 'packadd nvim.undotree' 53end ) 54 55it ( 'works' , function () 56api . nvim_set_current_line ( 'foo' ) 57exec 'Undotree' 58local buf =api . nvim_get_current_buf () 59local win =api . nvim_get_current_win () 60eq ( 61dedent [[ 62* 0 63* 1]] , 64strip_time ( buf_get_lines_and_extmark ( buf )) 65) 66eq ( 2 , api . nvim_win_get_cursor ( win )[ 1 ]) 67exec 'wincmd w' 68 69-- Doing changes moves cursor in undotree 70exec 'undo' 71eq ( 1 , api . nvim_win_get_cursor ( win )[ 1 ]) 72api . nvim_set_current_line ( 'bar' ) 73eq ( 3 , api . nvim_win_get_cursor ( win )[ 1 ]) 74 75eq ( 76dedent [[ 77* 0 78|\ 79| * 1 80* 2]] , 81strip_time ( buf_get_lines_and_extmark ( buf )) 82) 83 84-- Moving the cursor in undotree changes the buffer 85eq ( 'bar' , api . nvim_get_current_line ()) 86exec 'wincmd w' 87exec '2' 88exec 'wincmd w' 89eq ( 'foo' , api . nvim_get_current_line ()) 90end ) 91 92it ( 'sync scroll pos when undo' , function () 93local screen =Screen . new ( 30 , 10 ) 94for _ =1 , n . api . nvim_get_option_value ( 'lines' , {} ) do 95api . nvim_set_current_line ( 'foo' ) 96end 97exec 'Undotree' 98exec 'wincmd w' 99exec 'silent undo' 100screen : expect ( [[ 101* 3 │^foo | 102* 4 │{1:~ }| 103* 5 │{1:~ }| 104* 6 │{1:~ }| 105* 7 │{1:~ }| 106* 8 │{1:~ }| 107{21:* 9 }│{1:~ }| 108* 10 │{1:~ }| 109{2:<or [-] }{3:[No Name] [+] }| 110| 111]] ) 112end ) 113 114describe ( 'branch+remove is correctly graphed' , function () 115it ( 'when branching left' , function () 116generate_undo_tree_from_rev ( { 0 , 1 , 2 , 3 , 1 , 3 , 4 , 3 , 2 , 0 } ) 117exec 'Undotree' 118eq ( 119dedent ( [[ 120* 0 121|\ 122| * 1 123| |\ 124| | * 2 125| | |\ 126| | | * 3 127| | | |\ 128| | | | * 4 129| * | | | 5 130| / /| |]] --[[This is the line being tested, e.g. remove&branch left]] .. '\n' .. [[ 131| | | * | 6 132| | | / 133| | | * 7 134| | * 8 135| * 9 136* 10]] ), 137strip_time ( buf_get_lines_and_extmark ( 0 )) 138) 139end ) 140 141it ( 'when branching right' , function () 142generate_undo_tree_from_rev ( { 0 , 1 , 2 , 3 , 3 , 1 , 4 , 2 , 1 , 0 } ) 143exec 'Undotree' 144eq ( 145dedent ( [[ 146* 0 147|\ 148| * 1 149| |\ 150| | * 2 151| | |\ 152| | | * 3 153| | | |\ 154| | | | * 4 155| | | * | 5 156| |\ \ |]] --[[This is the line being tested, e.g. remove&branch right]] .. '\n' .. [[ 157| | * | | 6 158| | / / 159| | | * 7 160| | * 8 161| * 9 162* 10]] ), 163strip_time ( buf_get_lines_and_extmark ( 0 )) 164) 165end ) 166end ) 167end )