robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
f907614088
master
1local t =require ( 'test.testutil' ) 2local n =require ( 'test.functional.testnvim' )() 3local Screen =require ( 'test.functional.ui.screen' ) 4 5local describe , it , before_each , after_each =t . describe , t . it , t . before_each , t . after_each 6local clear =n . clear 7local command =n . command 8local expect_exit =n . expect_exit 9local api , eq , feed_command =n . api , t . eq , n . feed_command 10local feed , poke_eventloop =n . feed , n . poke_eventloop 11local ok =t . ok 12local eval =n . eval 13 14local shada_file ='Xtest.shada' 15 16local function _clear () 17clear { 18args ={ 19'-i' , 20shada_file , -- Need shada for these tests. 21'--cmd' , 22"set noswapfile undodir=. directory=. viewdir=. backupdir=. belloff= noshowcmd noruler shada=!,'100,<50,s10,h" , 23} , 24args_rm ={ '-i' , '--cmd' } , 25} 26end 27 28describe ( ':oldfiles' , function () 29before_each ( _clear ) 30 31after_each ( function () 32expect_exit ( command , 'qall!' ) 33os . remove ( shada_file ) 34end ) 35 36local function add_padding ( s ) 37return s .. string . rep ( ' ' , 96 - string . len ( s )) 38end 39 40it ( 'shows most recently used files' , function () 41local screen =Screen . new ( 100 , 5 ) 42screen . _default_attr_ids =nil 43feed_command ( 'edit testfile1' ) 44feed_command ( 'edit testfile2' ) 45feed_command ( 'wshada' ) 46feed_command ( 'rshada!' ) 47local oldfiles =api . nvim_get_vvar ( 'oldfiles' ) 48feed_command ( 'oldfiles' ) 49screen : expect ( [[ 50| 511: ]] .. add_padding ( oldfiles [ 1 ]) .. [[ | 522: ]] .. add_padding ( oldfiles [ 2 ]) .. [[ | 53| 54Press ENTER or type command to continue^ | 55]] ) 56feed ( '<CR>' ) 57end ) 58 59it ( 'can be filtered with :filter' , function () 60feed_command ( 'edit file_one.txt' ) 61local file1 =api . nvim_buf_get_name ( 0 ) 62feed_command ( 'edit file_two.txt' ) 63local file2 =api . nvim_buf_get_name ( 0 ) 64feed_command ( 'edit another.txt' ) 65local another =api . nvim_buf_get_name ( 0 ) 66feed_command ( 'wshada' ) 67feed_command ( 'rshada!' ) 68 69local function get_oldfiles ( cmd ) 70local q =eval ( [[split(execute(']] .. cmd .. [['), "\n")]] ) 71for i , _ in ipairs ( q ) do 72q [ i ] =q [ i ]: gsub ( '^%d+:%s+' , '' ) 73end 74table . sort ( q ) 75return q 76end 77 78local oldfiles =get_oldfiles ( 'oldfiles' ) 79eq ( { another , file1 , file2 } , oldfiles ) 80 81oldfiles =get_oldfiles ( 'filter file_ oldfiles' ) 82eq ( { file1 , file2 } , oldfiles ) 83 84oldfiles =get_oldfiles ( 'filter /another/ oldfiles' ) 85eq ( { another } , oldfiles ) 86 87oldfiles =get_oldfiles ( 'filter! file_ oldfiles' ) 88eq ( { another } , oldfiles ) 89 90-- The original v:oldfiles index is preserved in the output (matches `message_filtered()` behavior). 91local v_oldfiles =api . nvim_get_vvar ( 'oldfiles' ) 92local raw =eval ( [[split(execute('filter file_ oldfiles'), "\n")]] ) 93for _ , line in ipairs ( raw ) do 94local idx , path =line : match ( '^(%d+):%s+(.+)$' ) 95ok ( idx ~= nil , 'numbered' , line ) 96eq ( path , v_oldfiles [ tonumber ( idx )]) 97end 98end ) 99end ) 100 101describe ( ':browse oldfiles' , function () 102local filename 103local filename2 104local oldfiles 105 106before_each ( function () 107_clear () 108feed_command ( 'edit testfile1' ) 109filename =api . nvim_buf_get_name ( 0 ) 110feed_command ( 'edit testfile2' ) 111filename2 =api . nvim_buf_get_name ( 0 ) 112feed_command ( 'wshada' ) 113poke_eventloop () 114_clear () 115 116-- Ensure nvim is out of "Press ENTER..." prompt. 117feed ( '<cr>' ) 118 119-- Ensure v:oldfiles isn't busted. Since things happen so fast, 120-- the ordering of v:oldfiles is unstable (it uses qsort() under-the-hood). 121-- Let's verify the contents and the length of v:oldfiles before moving on. 122oldfiles =n . api . nvim_get_vvar ( 'oldfiles' ) 123eq ( 2 , # oldfiles ) 124ok ( filename == oldfiles [ 1 ] or filename == oldfiles [ 2 ]) 125ok ( filename2 == oldfiles [ 1 ] or filename2 == oldfiles [ 2 ]) 126 127feed_command ( 'browse oldfiles' ) 128end ) 129 130after_each ( function () 131expect_exit ( command , 'qall!' ) 132os . remove ( shada_file ) 133end ) 134 135it ( 'provides a prompt and edits the chosen file' , function () 136feed ( '2<cr>' ) 137eq ( oldfiles [ 2 ], api . nvim_buf_get_name ( 0 )) 138end ) 139 140it ( 'provides a prompt and does nothing on <cr>' , function () 141feed ( '<cr>' ) 142eq ( '' , api . nvim_buf_get_name ( 0 )) 143end ) 144 145it ( 'provides a prompt and does nothing if choice is out-of-bounds' , function () 146feed ( '3<cr>' ) 147eq ( '' , api . nvim_buf_get_name ( 0 )) 148end ) 149end )