robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
af23d17388
master
1" Tests for the :cdo, :cfdo, :ldo and :lfdo commands 2 3source check.vim 4CheckFeature quickfix 5 6" Create the files used by the tests 7func SetUp () 8call writefile ([ "Line1" , "Line2" , "Line3" ], 'Xtestfile1' ) 9call writefile ([ "Line1" , "Line2" , "Line3" ], 'Xtestfile2' ) 10call writefile ([ "Line1" , "Line2" , "Line3" ], 'Xtestfile3' ) 11endfunc 12 13" Remove the files used by the tests 14func TearDown () 15call delete ( 'Xtestfile1' ) 16call delete ( 'Xtestfile2' ) 17call delete ( 'Xtestfile3' ) 18endfunc 19 20" Returns the current line in '<filename> <linenum>L <column>C' format 21func GetRuler () 22return expand ( '%' ) . ' ' . line ( '.' ) . 'L' . ' ' . col ( '.' ) . 'C' 23endfunc 24 25" Tests for the :cdo and :ldo commands 26func XdoTests ( cchar ) 27enew 28 29" Shortcuts for calling the cdo and ldo commands 30let Xdo = a: cchar . 'do' 31let Xgetexpr = a: cchar . 'getexpr' 32let Xprev = a: cchar . 'prev' 33let XdoCmd = Xdo . ' call add(l, GetRuler())' 34 35" Try with an empty list 36let l = [] 37exe XdoCmd 38call assert_equal ([], l ) 39 40" Populate the list and then try 41exe Xgetexpr . " ['non-error 1', 'Xtestfile1:1:3:Line1', 'non-error 2', 'Xtestfile2:2:2:Line2', 'non-error 3', 'Xtestfile3:3:1:Line3']" 42 43let l = [] 44exe XdoCmd 45call assert_equal ([ 'Xtestfile1 1L 3C' , 'Xtestfile2 2L 2C' , 'Xtestfile3 3L 1C' ], l ) 46 47" Run command only on selected error lines 48let l = [] 49enew 50exe "2,3" . XdoCmd 51call assert_equal ([ 'Xtestfile2 2L 2C' , 'Xtestfile3 3L 1C' ], l ) 52 53" Boundary condition tests 54let l = [] 55enew 56exe "1,1" . XdoCmd 57call assert_equal ([ 'Xtestfile1 1L 3C' ], l ) 58 59let l = [] 60enew 61exe "3" . XdoCmd 62call assert_equal ([ 'Xtestfile3 3L 1C' ], l ) 63 64" Range test commands 65let l = [] 66enew 67exe "%" . XdoCmd 68call assert_equal ([ 'Xtestfile1 1L 3C' , 'Xtestfile2 2L 2C' , 'Xtestfile3 3L 1C' ], l ) 69 70let l = [] 71enew 72exe "1,$" . XdoCmd 73call assert_equal ([ 'Xtestfile1 1L 3C' , 'Xtestfile2 2L 2C' , 'Xtestfile3 3L 1C' ], l ) 74 75let l = [] 76enew 77exe Xprev 78exe "." . XdoCmd 79call assert_equal ([ 'Xtestfile2 2L 2C' ], l ) 80 81let l = [] 82enew 83exe "+" . XdoCmd 84call assert_equal ([ 'Xtestfile3 3L 1C' ], l ) 85 86" Invalid error lines test 87let l = [] 88enew 89exe "silent! 27" . XdoCmd 90exe "silent! 4,5" . XdoCmd 91call assert_equal ([], l ) 92 93" Run commands from an unsaved buffer 94let v: errmsg = '' 95let l = [] 96enew 97setlocal modified 98exe "silent! 2,2" . XdoCmd 99if v: errmsg !~# 'No write since last change' 100call add ( v: errors , 'Unsaved file change test failed' ) 101endif 102 103" If the executed command fails, then the operation should be aborted 104enew ! 105let subst_count = 0 106exe "silent!" . Xdo . " s/Line/xLine/ | let subst_count += 1" 107if subst_count != 1 || getline ( '.' ) != 'xLine1' 108call add ( v: errors , 'Abort command on error test failed' ) 109endif 110 111let l = [] 112exe "2,2" . Xdo . "! call add(l, GetRuler())" 113call assert_equal ([ 'Xtestfile2 2L 2C' ], l ) 114 115" List with no valid error entries 116let l = [] 117edit ! + 2 Xtestfile1 118exe Xgetexpr . " ['non-error 1', 'non-error 2', 'non-error 3']" 119exe XdoCmd 120call assert_equal ([], l ) 121exe "silent! 2" . XdoCmd 122call assert_equal ([], l ) 123let v: errmsg = '' 124exe "%" . XdoCmd 125exe "1,$" . XdoCmd 126exe "." . XdoCmd 127call assert_equal ( '' , v: errmsg ) 128 129" List with only one valid entry 130let l = [] 131exe Xgetexpr . " ['Xtestfile3:3:1:Line3']" 132exe XdoCmd 133call assert_equal ([ 'Xtestfile3 3L 1C' ], l ) 134 135endfunc 136 137" Tests for the :cfdo and :lfdo commands 138func XfdoTests ( cchar ) 139enew 140 141" Shortcuts for calling the cfdo and lfdo commands 142let Xfdo = a: cchar . 'fdo' 143let Xgetexpr = a: cchar . 'getexpr' 144let XfdoCmd = Xfdo . ' call add(l, GetRuler())' 145let Xpfile = a: cchar . 'pfile' 146 147" Clear the quickfix/location list 148exe Xgetexpr . " []" 149 150" Try with an empty list 151let l = [] 152exe XfdoCmd 153call assert_equal ([], l ) 154 155" Populate the list and then try 156exe Xgetexpr . " ['non-error 1', 'Xtestfile1:1:3:Line1', 'Xtestfile1:2:1:Line2', 'non-error 2', 'Xtestfile2:2:2:Line2', 'non-error 3', 'Xtestfile3:2:3:Line2', 'Xtestfile3:3:1:Line3']" 157 158let l = [] 159exe XfdoCmd 160call assert_equal ([ 'Xtestfile1 1L 3C' , 'Xtestfile2 2L 2C' , 'Xtestfile3 2L 3C' ], l ) 161 162" Run command only on selected error lines 163let l = [] 164exe "2,3" . XfdoCmd 165call assert_equal ([ 'Xtestfile2 2L 2C' , 'Xtestfile3 2L 3C' ], l ) 166 167" Boundary condition tests 168let l = [] 169exe "3" . XfdoCmd 170call assert_equal ([ 'Xtestfile3 2L 3C' ], l ) 171 172" Range test commands 173let l = [] 174exe "%" . XfdoCmd 175call assert_equal ([ 'Xtestfile1 1L 3C' , 'Xtestfile2 2L 2C' , 'Xtestfile3 2L 3C' ], l ) 176 177let l = [] 178exe "1,$" . XfdoCmd 179call assert_equal ([ 'Xtestfile1 1L 3C' , 'Xtestfile2 2L 2C' , 'Xtestfile3 2L 3C' ], l ) 180 181let l = [] 182exe Xpfile 183exe "." . XfdoCmd 184call assert_equal ([ 'Xtestfile2 2L 2C' ], l ) 185 186" List with only one valid entry 187let l = [] 188exe Xgetexpr . " ['Xtestfile2:2:5:Line2']" 189exe XfdoCmd 190call assert_equal ([ 'Xtestfile2 2L 5C' ], l ) 191 192endfunc 193 194" Tests for cdo and cfdo 195func Test_cdo () 196call XdoTests ( 'c' ) 197call XfdoTests ( 'c' ) 198endfunc 199 200" Tests for ldo and lfdo 201func Test_ldo () 202call XdoTests ( 'l' ) 203call XfdoTests ( 'l' ) 204endfunc 205 206" Test for making 'shm' doesn't interfere with the output. 207func Test_cdo_print () 208enew |only ! 209cgetexpr ["Xtestfile1:1:Line1","Xtestfile2:1:Line1" ,"Xtestfile3:1:Line1" ] 210cdo print 211call assert_equal ( 'Line1' , Screenline ( & lines )) 212call assert_equal ( 'Line1' , Screenline ( & lines - 3 )) 213call assert_equal ( 'Line1' , Screenline ( & lines - 6 )) 214endfunc 215 216" vim: shiftwidth=2 sts=2 expandtab