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 , eval , eq =n . clear , n . eval , t . eq 6local feed , command , expect =n . feed , n . command , n . expect 7local api , fn , neq =n . api , n . fn , t . neq 8 9describe ( 'TextYankPost' , function () 10before_each ( function () 11clear () 12 13-- emulate the clipboard so system clipboard isn't affected 14command ( 'set rtp^=test/functional/fixtures' ) 15 16command ( 'let g:count = 0' ) 17command ( 'autocmd TextYankPost * let g:event = copy(v:event)' ) 18command ( 'autocmd TextYankPost * let g:count += 1' ) 19 20api . nvim_buf_set_lines ( 0 , 0 , - 1 , true , { 21'foo\0bar' , 22'baz text' , 23} ) 24end ) 25 26it ( 'is executed after yank and handles register types' , function () 27feed ( 'yy' ) 28eq ( { 29inclusive =false , 30operator ='y' , 31regcontents ={ 'foo\nbar' } , 32regname ='' , 33regtype ='V' , 34visual =false , 35} , eval ( 'g:event' )) 36eq ( 1 , eval ( 'g:count' )) 37 38-- v:event is cleared after the autocommand is done 39eq ( {} , eval ( 'v:event' )) 40 41feed ( '+yw' ) 42eq ( { 43inclusive =false , 44operator ='y' , 45regcontents ={ 'baz ' } , 46regname ='' , 47regtype ='v' , 48visual =false , 49} , eval ( 'g:event' )) 50eq ( 2 , eval ( 'g:count' )) 51 52feed ( '<c-v>eky' ) 53eq ( { 54inclusive =true , 55operator ='y' , 56regcontents ={ 'foo' , 'baz' } , 57regname ='' , 58regtype ='\0223' , -- ^V + block width 59visual =true , 60} , eval ( 'g:event' )) 61eq ( 3 , eval ( 'g:count' )) 62end ) 63 64it ( 'makes v:event immutable' , function () 65feed ( 'yy' ) 66eq ( { 67inclusive =false , 68operator ='y' , 69regcontents ={ 'foo\nbar' } , 70regname ='' , 71regtype ='V' , 72visual =false , 73} , eval ( 'g:event' )) 74 75command ( 'set debug=msg' ) 76-- the regcontents should not be changed without copy. 77local status , err =pcall ( command , 'call extend(g:event.regcontents, ["more text"])' ) 78eq ( false , status ) 79neq ( nil , string . find ( err , ':E742:' )) 80 81-- can't mutate keys inside the autocommand 82command ( 'autocmd! TextYankPost * let v:event.regcontents = 0' ) 83status , err =pcall ( command , 'normal yy' ) 84eq ( false , status ) 85neq ( nil , string . find ( err , ':E46:' )) 86 87-- can't add keys inside the autocommand 88command ( 'autocmd! TextYankPost * let v:event.mykey = 0' ) 89status , err =pcall ( command , 'normal yy' ) 90eq ( false , status ) 91neq ( nil , string . find ( err , ':E742:' )) 92end ) 93 94it ( 'is not invoked recursively' , function () 95command ( 'autocmd TextYankPost * normal "+yy' ) 96feed ( 'yy' ) 97eq ( { 98inclusive =false , 99operator ='y' , 100regcontents ={ 'foo\nbar' } , 101regname ='' , 102regtype ='V' , 103visual =false , 104} , eval ( 'g:event' )) 105eq ( 1 , eval ( 'g:count' )) 106eq ( { 'foo\nbar' } , fn . getreg ( '+' , 1 , 1 )) 107end ) 108 109it ( 'is executed after delete and change' , function () 110feed ( 'dw' ) 111eq ( { 112inclusive =false , 113operator ='d' , 114regcontents ={ 'foo' } , 115regname ='' , 116regtype ='v' , 117visual =false , 118} , eval ( 'g:event' )) 119eq ( 1 , eval ( 'g:count' )) 120 121feed ( 'dd' ) 122eq ( { 123inclusive =false , 124operator ='d' , 125regcontents ={ '\nbar' } , 126regname ='' , 127regtype ='V' , 128visual =false , 129} , eval ( 'g:event' )) 130eq ( 2 , eval ( 'g:count' )) 131 132feed ( 'cwspam<esc>' ) 133eq ( { 134inclusive =true , 135operator ='c' , 136regcontents ={ 'baz' } , 137regname ='' , 138regtype ='v' , 139visual =false , 140} , eval ( 'g:event' )) 141eq ( 3 , eval ( 'g:count' )) 142end ) 143 144it ( 'is not executed after black-hole operation' , function () 145feed ( '"_dd' ) 146eq ( 0 , eval ( 'g:count' )) 147 148feed ( '"_cwgood<esc>' ) 149eq ( 0 , eval ( 'g:count' )) 150 151expect ( [[ 152good text]] ) 153feed ( '"_yy' ) 154eq ( 0 , eval ( 'g:count' )) 155 156command ( 'delete _' ) 157eq ( 0 , eval ( 'g:count' )) 158end ) 159 160it ( 'gives the correct register name' , function () 161feed ( '$"byiw' ) 162eq ( { 163inclusive =true , 164operator ='y' , 165regcontents ={ 'bar' } , 166regname ='b' , 167regtype ='v' , 168visual =false , 169} , eval ( 'g:event' )) 170 171feed ( '"*yy' ) 172eq ( { 173inclusive =true , 174operator ='y' , 175regcontents ={ 'foo\nbar' } , 176regname ='*' , 177regtype ='V' , 178visual =false , 179} , eval ( 'g:event' )) 180 181command ( 'set clipboard=unnamed' ) 182 183-- regname still shows the name the user requested 184feed ( 'yy' ) 185eq ( { 186inclusive =true , 187operator ='y' , 188regcontents ={ 'foo\nbar' } , 189regname ='' , 190regtype ='V' , 191visual =false , 192} , eval ( 'g:event' )) 193 194feed ( '"*yy' ) 195eq ( { 196inclusive =true , 197operator ='y' , 198regcontents ={ 'foo\nbar' } , 199regname ='*' , 200regtype ='V' , 201visual =false , 202} , eval ( 'g:event' )) 203end ) 204 205it ( 'works with Ex commands' , function () 206command ( '1delete +' ) 207eq ( { 208inclusive =false , 209operator ='d' , 210regcontents ={ 'foo\nbar' } , 211regname ='+' , 212regtype ='V' , 213visual =false , 214} , eval ( 'g:event' )) 215eq ( 1 , eval ( 'g:count' )) 216 217command ( 'yank' ) 218eq ( { 219inclusive =false , 220operator ='y' , 221regcontents ={ 'baz text' } , 222regname ='' , 223regtype ='V' , 224visual =false , 225} , eval ( 'g:event' )) 226eq ( 2 , eval ( 'g:count' )) 227 228command ( 'normal yw' ) 229eq ( { 230inclusive =false , 231operator ='y' , 232regcontents ={ 'baz ' } , 233regname ='' , 234regtype ='v' , 235visual =false , 236} , eval ( 'g:event' )) 237eq ( 3 , eval ( 'g:count' )) 238 239command ( 'normal! dd' ) 240eq ( { 241inclusive =false , 242operator ='d' , 243regcontents ={ 'baz text' } , 244regname ='' , 245regtype ='V' , 246visual =false , 247} , eval ( 'g:event' )) 248eq ( 4 , eval ( 'g:count' )) 249end ) 250 251it ( 'updates numbered registers correctly #10225' , function () 252command ( 'autocmd TextYankPost * let g:reg = getreg("1")' ) 253feed ( '"adj' ) 254eq ( 'foo\nbar\nbaz text\n' , eval ( 'g:reg' )) 255end ) 256end )