robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
af23d17388
master
1" Test for gn command 2 3func Test_gn_command () 4noautocmd new 5" replace a single char by itself quoted: 6call setline ( '.' , 'abc x def x ghi x jkl' ) 7let @/= 'x' 8exe "norm! cgn'x'\<esc>.." 9call assert_equal ( "abc 'x' def 'x' ghi 'x' jkl" , getline ( '.' )) 10sil ! %d _ 11 12" simple search match 13call setline ( '.' , 'foobar' ) 14let @/= 'foobar' 15exe "norm! gncsearchmatch" 16call assert_equal ( 'searchmatch' , getline ( '.' )) 17sil ! %d _ 18 19" replace a multi-line match 20call setline ( '.' , [ '' , 'one' , 'two' ]) 21let @/= 'one\_s*two\_s' 22exe "norm! gnceins\<CR>zwei" 23call assert_equal ([ '' , 'eins' , 'zwei' ], getline ( 1 , '$' )) 24sil ! %d _ 25 26" test count argument 27call setline ( '.' , [ '' , 'abcdx | abcdx | abcdx' ]) 28let @/= '[a]bcdx' 29exe "norm! 2gnd" 30call assert_equal ([ '' , 'abcdx | | abcdx' ], getline ( 1 , '$' )) 31sil ! %d _ 32 33" join lines 34call setline ( '.' , [ 'join ' , 'lines' ]) 35let @/= '$' 36exe "norm! 0gnd" 37call assert_equal ([ 'join lines' ], getline ( 1 , '$' )) 38sil ! %d _ 39 40" zero-width match 41call setline ( '.' , [ '' , 'zero width pattern' ]) 42let @/= '\>\zs' 43exe "norm! 0gnd" 44call assert_equal ([ '' , 'zerowidth pattern' ], getline ( 1 , '$' )) 45sil ! %d _ 46 47" delete first and last chars 48call setline ( '.' , [ 'delete first and last chars' ]) 49let @/= '^' 50exe "norm! 0gnd$" 51let @/= '\zs' 52exe "norm! gnd" 53call assert_equal ([ 'elete first and last char' ], getline ( 1 , '$' )) 54sil ! %d _ 55 56" using visual mode 57call setline ( '.' , [ '' , 'uniquepattern uniquepattern' ]) 58exe "norm! /[u]niquepattern/s\<cr>vlgnd" 59call assert_equal ([ '' , ' uniquepattern' ], getline ( 1 , '$' )) 60sil ! %d _ 61 62" backwards search 63call setline ( '.' , [ 'my very excellent mother just served us nachos' ]) 64let @/= 'mother' 65exe "norm! $cgNmongoose" 66call assert_equal ([ 'my very excellent mongoose just served us nachos' ], getline ( 1 , '$' )) 67sil ! %d _ 68 69" search for single char 70call setline ( '.' , [ '' , 'for (i=0; i<=10; i++)' ]) 71let @/= 'i' 72exe "norm! cgnj" 73call assert_equal ([ '' , 'for (j=0; i<=10; i++)' ], getline ( 1 , '$' )) 74sil ! %d _ 75 76" search hex char 77call setline ( '.' , [ '' , 'Y' ]) 78set noignorecase 79let @/= '\%x59' 80exe "norm! gnd" 81call assert_equal ([ '' , '' ], getline ( 1 , '$' )) 82sil ! %d _ 83 84" test repeating gdn 85call setline ( '.' , [ '' , '1' , 'Johnny' , '2' , 'Johnny' , '3' ]) 86let @/= 'Johnny' 87exe "norm! dgn." 88call assert_equal ([ '' , '1' , '' , '2' , '' , '3' ], getline ( 1 , '$' )) 89sil ! %d _ 90 91" test repeating gUgn 92call setline ( '.' , [ '' , '1' , 'Depp' , '2' , 'Depp' , '3' ]) 93let @/= 'Depp' 94exe "norm! gUgn." 95call assert_equal ([ '' , '1' , 'DEPP' , '2' , 'DEPP' , '3' ], getline ( 1 , '$' )) 96sil ! %d _ 97 98" test using look-ahead assertions 99call setline ( '.' , [ 'a:10' , '' , 'a:1' , '' , 'a:20' ]) 100let @/= 'a:0\@!\zs\d\+' 101exe "norm! 2nygno\<esc>p" 102call assert_equal ([ 'a:10' , '' , 'a:1' , '1' , '' , 'a:20' ], getline ( 1 , '$' )) 103sil ! %d _ 104 105" test using nowrapscan 106set nowrapscan 107call setline ( 1 , 'foo bar baz' ) 108exe "norm! /bar/e\<cr>" 109exe "norm! gnd" 110call assert_equal ([ 'foo baz' ], getline ( 1 , '$' )) 111sil ! %d _ 112 113" search upwards with nowrapscan set 114call setline ( '.' , [ 'foo' , 'bar' , 'foo' , 'baz' ]) 115set nowrapscan 116let @/= 'foo' 117$ 118norm ! dgN 119call assert_equal ([ 'foo' , 'bar' , '' , 'baz' ], getline ( 1 , '$' )) 120sil ! %d _ 121 122" search using the \zs atom 123call setline ( 1 , [ ' nnoremap' , '' , 'nnoremap' ]) 124set wrapscan & vim 125let @/= '\_s\zsnnoremap' 126$ 127norm ! cgnmatch 128call assert_equal ([ ' nnoremap' , '' , 'match' ], getline ( 1 , '$' )) 129sil ! %d _ 130 131" make sure it works correctly for one-char wide search items 132call setline ( '.' , [ 'abcdefghi' ]) 133let @/= 'a' 134exe "norm! 0fhvhhgNgU" 135call assert_equal ([ 'ABCDEFGHi' ], getline ( 1 , '$' )) 136call setline ( '.' , [ 'abcdefghi' ]) 137let @/= 'b' 138" this gn wraps around the end of the file 139exe "norm! 0fhvhhgngU" 140call assert_equal ([ 'aBCDEFGHi' ], getline ( 1 , '$' )) 141sil ! %d _ 142call setline ( '.' , [ 'abcdefghi' ]) 143let @/= 'f' 144exe "norm! 0vllgngU" 145call assert_equal ([ 'ABCDEFghi' ], getline ( 1 , '$' )) 146sil ! %d _ 147call setline ( '.' , [ '12345678' ]) 148let @/= '5' 149norm ! gg0f7vhhhhgnd 150call assert_equal ([ '12348' ], getline ( 1 , '$' )) 151sil ! %d _ 152call setline ( '.' , [ '12345678' ]) 153let @/= '5' 154norm ! gg0f2vf7gNd 155call assert_equal ([ '1678' ], getline ( 1 , '$' )) 156sil ! %d _ 157set wrapscan & vim 158 159" Without 'wrapscan', in visual mode, running gn without a match should fail 160" but the visual mode should be kept. 161set nowrapscan 162call setline ( '.' , 'one two' ) 163let @/= 'one' 164call assert_beeps ( 'normal 0wvlgn' ) 165exe "normal y" 166call assert_equal ( 'tw' , @") 167 168" with exclusive selection, run gn and gN 169set selection = exclusive 170normal 0gny 171call assert_equal ( 'one' , @") 172normal 0wgNy 173call assert_equal ( 'one' , @") 174set selection & 175endfunc 176 177func Test_gN_repeat () 178new 179call setline ( 1 , 'this list is a list with a list of a list.' ) 180/ list 181normal $gNgNgNx 182call assert_equal ( 'list with a list of a list' , @") 183bwipe ! 184endfunc 185 186func Test_gN_then_gn () 187new 188 189call setline ( 1 , 'this list is a list with a list of a last.' ) 190/ l.st 191normal $gNgNgnx 192call assert_equal ( 'last' , @") 193 194call setline ( 1 , 'this list is a list with a lust of a last.' ) 195/ l.st 196normal $gNgNgNgnx 197call assert_equal ( 'lust of a last' , @") 198 199bwipe ! 200endfunc 201 202func Test_gn_multi_line () 203new 204call setline ( 1 , [ 205 \'func Tm1()' , 206 \' echo "one"' , 207 \'endfunc' , 208 \'func Tm2()' , 209 \' echo "two"' , 210 \'endfunc' , 211 \'func Tm3()' , 212 \' echo "three"' , 213 \'endfunc' , 214 \]) 215/ \v^func Tm\d\(\)\n. * \zs". * "\ze$ 216normal jgnrx 217call assert_equal ( ' echo xxxxx' , getline ( 5 )) 218bwipe ! 219endfunc 220 221" vim: shiftwidth=2 sts=2 expandtab