robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
3a70fc8cb8
master
1" Test Vim profiler 2 3source check.vim 4CheckFeature profile 5 6source shared.vim 7source screendump.vim 8source vim9.vim 9 10func Test_profile_func () 11call RunProfileFunc ( 'func' , 'let' , 'let' ) 12" call RunProfileFunc('def', 'var', '') 13endfunc 14 15func RunProfileFunc ( command , declare , assign ) 16let lines =<< trim [CODE] 17profile start Xprofile_func.log 18profile func Foo* 19XXX Foo1() 20endXXX 21XXX Foo2() 22DDD counter = 100 23while counter > 0 24AAA counter = counter - 1 25endwhile 26sleep 1m 27endXXX 28XXX Foo3() 29endXXX 30XXX Bar() 31endXXX 32call Foo1() 33call Foo1() 34profile pause 35call Foo1() 36profile continue 37call Foo2() 38call Foo3() 39call Bar() 40if !v:profiling 41delfunc Foo2 42endif 43delfunc Foo3 44[CODE] 45 46call map ( lines , { k , v ->substitute ( v , 'XXX' , a: command , '' ) }) 47call map ( lines , { k , v ->substitute ( v , 'DDD' , a: declare , '' ) }) 48call map ( lines , { k , v ->substitute ( v , 'AAA' , a: assign , '' ) }) 49 50call writefile ( lines , 'Xprofile_func.vim' ) 51call system ( GetVimCommand () 52 \. ' -es --clean' 53 \. ' --cmd "so Xprofile_func.vim"' 54 \. ' --cmd "qall!"' ) 55call assert_equal ( 0 , v: shell_error ) 56 57sleep 50m 58let lines = readfile ( 'Xprofile_func.log' ) 59 60" - Foo1() is called 3 times but should be reported as called twice 61" since one call is in between "profile pause" .. "profile continue". 62" - Foo2() should come before Foo1() since Foo1() does much more work. 63" - Foo3() is not reported because function is deleted. 64" - Unlike Foo3(), Foo2() should not be deleted since there is a check 65" for v:profiling. 66" - Bar() is not reported since it does not match "profile func Foo*". 67call assert_equal ( 31 , len ( lines )) 68 69call assert_equal ( 'FUNCTION Foo1()' , lines [ 0 ]) 70call assert_match ( 'Defined:.*Xprofile_func.vim:3' , lines [ 1 ]) 71call assert_equal ( 'Called 2 times' , lines [ 2 ]) 72call assert_match ( '^Total time:\s\+\d\+\.\d\+$' , lines [ 3 ]) 73call assert_match ( '^ Self time:\s\+\d\+\.\d\+$' , lines [ 4 ]) 74call assert_equal ( '' , lines [ 5 ]) 75call assert_equal ( 'count total (s) self (s)' , lines [ 6 ]) 76call assert_equal ( '' , lines [ 7 ]) 77call assert_equal ( 'FUNCTION Foo2()' , lines [ 8 ]) 78call assert_equal ( 'Called 1 time' , lines [ 10 ]) 79call assert_match ( '^Total time:\s\+\d\+\.\d\+$' , lines [ 11 ]) 80call assert_match ( '^ Self time:\s\+\d\+\.\d\+$' , lines [ 12 ]) 81call assert_equal ( '' , lines [ 13 ]) 82call assert_equal ( 'count total (s) self (s)' , lines [ 14 ]) 83call assert_match ( '^\s*1\s\+.*\s\(let\|var\) counter = 100$' , lines [ 15 ]) 84call assert_match ( '^\s*101\s\+.*\swhile counter > 0$' , lines [ 16 ]) 85call assert_match ( '^\s*100\s\+.*\s \(let\)\= counter = counter - 1$' , lines [ 17 ]) 86call assert_match ( '^\s*10[01]\s\+.*\sendwhile$' , lines [ 18 ]) 87call assert_match ( '^\s*1\s\+.\+sleep 1m$' , lines [ 19 ]) 88call assert_equal ( '' , lines [ 20 ]) 89call assert_equal ( 'FUNCTIONS SORTED ON TOTAL TIME' , lines [ 21 ]) 90call assert_equal ( 'count total (s) self (s) function' , lines [ 22 ]) 91call assert_match ( '^\s*1\s\+\d\+\.\d\+\s\+Foo2()$' , lines [ 23 ]) 92call assert_match ( '^\s*2\s\+\d\+\.\d\+\s\+Foo1()$' , lines [ 24 ]) 93call assert_equal ( '' , lines [ 25 ]) 94call assert_equal ( 'FUNCTIONS SORTED ON SELF TIME' , lines [ 26 ]) 95call assert_equal ( 'count total (s) self (s) function' , lines [ 27 ]) 96call assert_match ( '^\s*1\s\+\d\+\.\d\+\s\+Foo2()$' , lines [ 28 ]) 97call assert_match ( '^\s*2\s\+\d\+\.\d\+\s\+Foo1()$' , lines [ 29 ]) 98call assert_equal ( '' , lines [ 30 ]) 99 100call delete ( 'Xprofile_func.vim' ) 101call delete ( 'Xprofile_func.log' ) 102endfunc 103 104func Test_profile_func_with_ifelse () 105call Run_profile_func_with_ifelse ( 'func' , 'let' ) 106" call Run_profile_func_with_ifelse('def', 'var') 107endfunc 108 109func Run_profile_func_with_ifelse ( command , declare ) 110let lines =<< trim [CODE] 111XXX Foo1() 112if 1 113DDD x = 0 114elseif 1 115DDD x = 1 116else 117DDD x = 2 118endif 119endXXX 120XXX Foo2() 121if 0 122DDD x = 0 123elseif 1 124DDD x = 1 125else 126DDD x = 2 127endif 128endXXX 129XXX Foo3() 130if 0 131DDD x = 0 132elseif 0 133DDD x = 1 134else 135DDD x = 2 136endif 137endXXX 138call Foo1() 139call Foo2() 140call Foo3() 141[CODE] 142 143call map ( lines , { k , v ->substitute ( v , 'XXX' , a: command , '' ) }) 144call map ( lines , { k , v ->substitute ( v , 'DDD' , a: declare , '' ) }) 145 146call writefile ( lines , 'Xprofile_func.vim' ) 147call system ( GetVimCommand () 148 \. ' -es -i NONE --noplugin' 149 \. ' -c "profile start Xprofile_func.log"' 150 \. ' -c "profile func Foo*"' 151 \. ' -c "so Xprofile_func.vim"' 152 \. ' -c "qall!"' ) 153call assert_equal ( 0 , v: shell_error ) 154 155let lines = readfile ( 'Xprofile_func.log' ) 156 157" - Foo1() should pass 'if' block. 158" - Foo2() should pass 'elseif' block. 159" - Foo3() should pass 'else' block. 160call assert_equal ( 57 , len ( lines )) 161 162call assert_equal ( 'FUNCTION Foo1()' , lines [ 0 ]) 163call assert_match ( 'Defined:.*Xprofile_func.vim' , lines [ 1 ]) 164call assert_equal ( 'Called 1 time' , lines [ 2 ]) 165call assert_match ( '^Total time:\s\+\d\+\.\d\+$' , lines [ 3 ]) 166call assert_match ( '^ Self time:\s\+\d\+\.\d\+$' , lines [ 4 ]) 167call assert_equal ( '' , lines [ 5 ]) 168call assert_equal ( 'count total (s) self (s)' , lines [ 6 ]) 169call assert_match ( '^\s*1\s\+.*\sif 1$' , lines [ 7 ]) 170call assert_match ( '^\s*1\s\+.*\s \(let\|var\) x = 0$' , lines [ 8 ]) 171call assert_match ( '^\s\+elseif 1$' , lines [ 9 ]) 172call assert_match ( '^\s\+\(let\|var\) x = 1$' , lines [ 10 ]) 173call assert_match ( '^\s\+else$' , lines [ 11 ]) 174call assert_match ( '^\s\+\(let\|var\) x = 2$' , lines [ 12 ]) 175call assert_match ( '^\s*1\s\+.*\sendif$' , lines [ 13 ]) 176call assert_equal ( '' , lines [ 14 ]) 177call assert_equal ( 'FUNCTION Foo2()' , lines [ 15 ]) 178call assert_equal ( 'Called 1 time' , lines [ 17 ]) 179call assert_match ( '^Total time:\s\+\d\+\.\d\+$' , lines [ 18 ]) 180call assert_match ( '^ Self time:\s\+\d\+\.\d\+$' , lines [ 19 ]) 181call assert_equal ( '' , lines [ 20 ]) 182call assert_equal ( 'count total (s) self (s)' , lines [ 21 ]) 183call assert_match ( '^\s*1\s\+.*\sif 0$' , lines [ 22 ]) 184call assert_match ( '^\s\+\(let\|var\) x = 0$' , lines [ 23 ]) 185call assert_match ( '^\s*1\s\+.*\selseif 1$' , lines [ 24 ]) 186call assert_match ( '^\s*1\s\+.*\s \(let\|var\) x = 1$' , lines [ 25 ]) 187call assert_match ( '^\s\+else$' , lines [ 26 ]) 188call assert_match ( '^\s\+\(let\|var\) x = 2$' , lines [ 27 ]) 189call assert_match ( '^\s*1\s\+.*\sendif$' , lines [ 28 ]) 190call assert_equal ( '' , lines [ 29 ]) 191call assert_equal ( 'FUNCTION Foo3()' , lines [ 30 ]) 192call assert_equal ( 'Called 1 time' , lines [ 32 ]) 193call assert_match ( '^Total time:\s\+\d\+\.\d\+$' , lines [ 33 ]) 194call assert_match ( '^ Self time:\s\+\d\+\.\d\+$' , lines [ 34 ]) 195call assert_equal ( '' , lines [ 35 ]) 196call assert_equal ( 'count total (s) self (s)' , lines [ 36 ]) 197call assert_match ( '^\s*1\s\+.*\sif 0$' , lines [ 37 ]) 198call assert_match ( '^\s\+\(let\|var\) x = 0$' , lines [ 38 ]) 199call assert_match ( '^\s*1\s\+.*\selseif 0$' , lines [ 39 ]) 200call assert_match ( '^\s\+\(let\|var\) x = 1$' , lines [ 40 ]) 201call assert_match ( '^\s*1\s\+.*\selse$' , lines [ 41 ]) 202call assert_match ( '^\s*1\s\+.*\s \(let\|var\) x = 2$' , lines [ 42 ]) 203call assert_match ( '^\s*1\s\+.*\sendif$' , lines [ 43 ]) 204call assert_equal ( '' , lines [ 44 ]) 205call assert_equal ( 'FUNCTIONS SORTED ON TOTAL TIME' , lines [ 45 ]) 206call assert_equal ( 'count total (s) self (s) function' , lines [ 46 ]) 207call assert_match ( '^\s*1\s\+\d\+\.\d\+\s\+Foo.()$' , lines [ 47 ]) 208call assert_match ( '^\s*1\s\+\d\+\.\d\+\s\+Foo.()$' , lines [ 48 ]) 209call assert_match ( '^\s*1\s\+\d\+\.\d\+\s\+Foo.()$' , lines [ 49 ]) 210call assert_equal ( '' , lines [ 50 ]) 211call assert_equal ( 'FUNCTIONS SORTED ON SELF TIME' , lines [ 51 ]) 212call assert_equal ( 'count total (s) self (s) function' , lines [ 52 ]) 213call assert_match ( '^\s*1\s\+\d\+\.\d\+\s\+Foo.()$' , lines [ 53 ]) 214call assert_match ( '^\s*1\s\+\d\+\.\d\+\s\+Foo.()$' , lines [ 54 ]) 215call assert_match ( '^\s*1\s\+\d\+\.\d\+\s\+Foo.()$' , lines [ 55 ]) 216call assert_equal ( '' , lines [ 56 ]) 217 218call delete ( 'Xprofile_func.vim' ) 219call delete ( 'Xprofile_func.log' ) 220endfunc 221 222func Test_profile_func_with_trycatch () 223call Run_profile_func_with_trycatch ( 'func' , 'let' ) 224" call Run_profile_func_with_trycatch('def', 'var') 225endfunc 226 227func Run_profile_func_with_trycatch ( command , declare ) 228let lines =<< trim [CODE] 229XXX Foo1() 230try 231DDD x = 0 232catch 233DDD x = 1 234finally 235DDD x = 2 236endtry 237endXXX 238XXX Foo2() 239try 240throw 0 241catch 242DDD x = 1 243finally 244DDD x = 2 245endtry 246endXXX 247XXX Foo3() 248try 249throw 0 250catch 251throw 1 252finally 253DDD x = 2 254endtry 255endXXX 256call Foo1() 257call Foo2() 258let rethrown = 0 259try 260call Foo3() 261catch 262let rethrown = 1 263endtry 264if rethrown != 1 265" call Foo1 again so that the test fails 266call Foo1() 267endif 268[CODE] 269 270call map ( lines , { k , v ->substitute ( v , 'XXX' , a: command , '' ) }) 271call map ( lines , { k , v ->substitute ( v , 'DDD' , a: declare , '' ) }) 272 273call writefile ( lines , 'Xprofile_func.vim' ) 274call system ( GetVimCommand () 275 \. ' -es -i NONE --noplugin' 276 \. ' -c "profile start Xprofile_func.log"' 277 \. ' -c "profile func Foo*"' 278 \. ' -c "so Xprofile_func.vim"' 279 \. ' -c "qall!"' ) 280call assert_equal ( 0 , v: shell_error ) 281 282let lines = readfile ( 'Xprofile_func.log' ) 283 284" - Foo1() should pass 'try' 'finally' blocks. 285" - Foo2() should pass 'catch' 'finally' blocks. 286" - Foo3() should not pass 'endtry'. 287call assert_equal ( 57 , len ( lines )) 288 289call assert_equal ( 'FUNCTION Foo1()' , lines [ 0 ]) 290call assert_match ( 'Defined:.*Xprofile_func.vim' , lines [ 1 ]) 291call assert_equal ( 'Called 1 time' , lines [ 2 ]) 292call assert_match ( '^Total time:\s\+\d\+\.\d\+$' , lines [ 3 ]) 293call assert_match ( '^ Self time:\s\+\d\+\.\d\+$' , lines [ 4 ]) 294call assert_equal ( '' , lines [ 5 ]) 295call assert_equal ( 'count total (s) self (s)' , lines [ 6 ]) 296call assert_match ( '^\s*1\s\+.*\stry$' , lines [ 7 ]) 297call assert_match ( '^\s*1\s\+.*\s \(let\|var\) x = 0$' , lines [ 8 ]) 298call assert_match ( '^\s\+catch$' , lines [ 9 ]) 299call assert_match ( '^\s\+\(let\|var\) x = 1$' , lines [ 10 ]) 300call assert_match ( '^\s*1\s\+.*\sfinally$' , lines [ 11 ]) 301call assert_match ( '^\s*1\s\+.*\s \(let\|var\) x = 2$' , lines [ 12 ]) 302call assert_match ( '^\s*1\s\+.*\sendtry$' , lines [ 13 ]) 303call assert_equal ( '' , lines [ 14 ]) 304call assert_equal ( 'FUNCTION Foo2()' , lines [ 15 ]) 305call assert_equal ( 'Called 1 time' , lines [ 17 ]) 306call assert_match ( '^Total time:\s\+\d\+\.\d\+$' , lines [ 18 ]) 307call assert_match ( '^ Self time:\s\+\d\+\.\d\+$' , lines [ 19 ]) 308call assert_equal ( '' , lines [ 20 ]) 309call assert_equal ( 'count total (s) self (s)' , lines [ 21 ]) 310call assert_match ( '^\s*1\s\+.*\stry$' , lines [ 22 ]) 311call assert_match ( '^\s*1\s\+.*\s throw 0$' , lines [ 23 ]) 312call assert_match ( '^\s*1\s\+.*\scatch$' , lines [ 24 ]) 313call assert_match ( '^\s*1\s\+.*\s \(let\|var\) x = 1$' , lines [ 25 ]) 314call assert_match ( '^\s*1\s\+.*\sfinally$' , lines [ 26 ]) 315call assert_match ( '^\s*1\s\+.*\s \(let\|var\) x = 2$' , lines [ 27 ]) 316call assert_match ( '^\s*1\s\+.*\sendtry$' , lines [ 28 ]) 317call assert_equal ( '' , lines [ 29 ]) 318call assert_equal ( 'FUNCTION Foo3()' , lines [ 30 ]) 319call assert_equal ( 'Called 1 time' , lines [ 32 ]) 320call assert_match ( '^Total time:\s\+\d\+\.\d\+$' , lines [ 33 ]) 321call assert_match ( '^ Self time:\s\+\d\+\.\d\+$' , lines [ 34 ]) 322call assert_equal ( '' , lines [ 35 ]) 323call assert_equal ( 'count total (s) self (s)' , lines [ 36 ]) 324call assert_match ( '^\s*1\s\+.*\stry$' , lines [ 37 ]) 325call assert_match ( '^\s*1\s\+.*\s throw 0$' , lines [ 38 ]) 326call assert_match ( '^\s*1\s\+.*\scatch$' , lines [ 39 ]) 327call assert_match ( '^\s*1\s\+.*\s throw 1$' , lines [ 40 ]) 328call assert_match ( '^\s*1\s\+.*\sfinally$' , lines [ 41 ]) 329call assert_match ( '^\s*1\s\+.*\s \(let\|var\) x = 2$' , lines [ 42 ]) 330call assert_match ( '^\s\+endtry$' , lines [ 43 ]) 331call assert_equal ( '' , lines [ 44 ]) 332call assert_equal ( 'FUNCTIONS SORTED ON TOTAL TIME' , lines [ 45 ]) 333call assert_equal ( 'count total (s) self (s) function' , lines [ 46 ]) 334call assert_match ( '^\s*1\s\+\d\+\.\d\+\s\+Foo.()$' , lines [ 47 ]) 335call assert_match ( '^\s*1\s\+\d\+\.\d\+\s\+Foo.()$' , lines [ 48 ]) 336call assert_match ( '^\s*1\s\+\d\+\.\d\+\s\+Foo.()$' , lines [ 49 ]) 337call assert_equal ( '' , lines [ 50 ]) 338call assert_equal ( 'FUNCTIONS SORTED ON SELF TIME' , lines [ 51 ]) 339call assert_equal ( 'count total (s) self (s) function' , lines [ 52 ]) 340call assert_match ( '^\s*1\s\+\d\+\.\d\+\s\+Foo.()$' , lines [ 53 ]) 341call assert_match ( '^\s*1\s\+\d\+\.\d\+\s\+Foo.()$' , lines [ 54 ]) 342call assert_match ( '^\s*1\s\+\d\+\.\d\+\s\+Foo.()$' , lines [ 55 ]) 343call assert_equal ( '' , lines [ 56 ]) 344 345call delete ( 'Xprofile_func.vim' ) 346call delete ( 'Xprofile_func.log' ) 347endfunc 348 349func Test_profile_file () 350let lines =<< trim [CODE] 351func! Foo() 352endfunc 353for i in range(10) 354" a comment 355call Foo() 356endfor 357call Foo() 358[CODE] 359 360call writefile ( lines , 'Xprofile_file.vim' ) 361call system ( GetVimCommandClean () 362 \. ' -es' 363 \. ' -c "profile start Xprofile_file.log"' 364 \. ' -c "profile file Xprofile_file.vim"' 365 \. ' -c "so Xprofile_file.vim"' 366 \. ' -c "so Xprofile_file.vim"' 367 \. ' -c "qall!"' ) 368call assert_equal ( 0 , v: shell_error ) 369 370let lines = readfile ( 'Xprofile_file.log' ) 371 372call assert_equal ( 14 , len ( lines )) 373 374call assert_match ( '^SCRIPT .*Xprofile_file.vim$' , lines [ 0 ]) 375call assert_equal ( 'Sourced 2 times' , lines [ 1 ]) 376call assert_match ( '^Total time:\s\+\d\+\.\d\+$' , lines [ 2 ]) 377call assert_match ( '^ Self time:\s\+\d\+\.\d\+$' , lines [ 3 ]) 378call assert_equal ( '' , lines [ 4 ]) 379call assert_equal ( 'count total (s) self (s)' , lines [ 5 ]) 380call assert_match ( ' 2 0.\d\+ func! Foo()' , lines [ 6 ]) 381call assert_equal ( ' endfunc' , lines [ 7 ]) 382" Loop iterates 10 times. Since script runs twice, body executes 20 times. 383" First line of loop executes one more time than body to detect end of loop. 384call assert_match ( '^\s*22\s\+\d\+\.\d\+\s\+for i in range(10)$' , lines [ 8 ]) 385call assert_equal ( ' " a comment' , lines [ 9 ]) 386" if self and total are equal we only get one number 387call assert_match ( '^\s*20\s\+\(\d\+\.\d\+\s\+\)\=\d\+\.\d\+\s\+call Foo()$' , lines [ 10 ]) 388call assert_match ( '^\s*22\s\+\d\+\.\d\+\s\+endfor$' , lines [ 11 ]) 389" if self and total are equal we only get one number 390call assert_match ( '^\s*2\s\+\(\d\+\.\d\+\s\+\)\=\d\+\.\d\+\s\+call Foo()$' , lines [ 12 ]) 391call assert_equal ( '' , lines [ 13 ]) 392 393call delete ( 'Xprofile_file.vim' ) 394call delete ( 'Xprofile_file.log' ) 395endfunc 396 397func Test_profile_file_with_cont () 398let lines = [ 399 \'echo "hello' , 400 \' \ world"' , 401 \'echo "foo ' , 402 \' \bar"' , 403 \] 404 405call writefile ( lines , 'Xprofile_file.vim' ) 406call system ( GetVimCommandClean () 407 \. ' -es' 408 \. ' -c "profile start Xprofile_file.log"' 409 \. ' -c "profile file Xprofile_file.vim"' 410 \. ' -c "so Xprofile_file.vim"' 411 \. ' -c "qall!"' ) 412call assert_equal ( 0 , v: shell_error ) 413 414let lines = readfile ( 'Xprofile_file.log' ) 415call assert_equal ( 11 , len ( lines )) 416 417call assert_match ( '^SCRIPT .*Xprofile_file.vim$' , lines [ 0 ]) 418call assert_equal ( 'Sourced 1 time' , lines [ 1 ]) 419call assert_match ( '^Total time:\s\+\d\+\.\d\+$' , lines [ 2 ]) 420call assert_match ( '^ Self time:\s\+\d\+\.\d\+$' , lines [ 3 ]) 421call assert_equal ( '' , lines [ 4 ]) 422call assert_equal ( 'count total (s) self (s)' , lines [ 5 ]) 423call assert_match ( ' 1 0.\d\+ echo "hello' , lines [ 6 ]) 424call assert_equal ( ' \ world"' , lines [ 7 ]) 425call assert_match ( ' 1 0.\d\+ echo "foo ' , lines [ 8 ]) 426call assert_equal ( ' \bar"' , lines [ 9 ]) 427call assert_equal ( '' , lines [ 10 ]) 428 429call delete ( 'Xprofile_file.vim' ) 430call delete ( 'Xprofile_file.log' ) 431endfunc 432 433func Test_profile_completion () 434call feedkeys ( ":profile \<C-A>\<C-B>\"\<CR>" , 'tx' ) 435call assert_equal ( '"profile continue dump file func pause start stop' , @:) 436 437call feedkeys ( ":profile start test_prof\<C-A>\<C-B>\"\<CR>" , 'tx' ) 438call assert_match ( '^"profile start.* test_profile\.vim' , @:) 439 440call feedkeys ( ":profile file test_prof\<Tab>\<C-B>\"\<CR>" , 'tx' ) 441call assert_match ( '"profile file test_profile\.vim' , @:) 442call feedkeys ( ":profile file test_prof\<Tab>\<C-B>\"\<CR>" , 'tx' ) 443call assert_match ( '"profile file test_profile\.vim' , @:) 444call feedkeys ( ":profile file test_prof \<Tab>\<C-B>\"\<CR>" , 'tx' ) 445call assert_match ( '"profile file test_prof ' , @:) 446call feedkeys ( ":profile file X1B2C3\<Tab>\<C-B>\"\<CR>" , 'tx' ) 447call assert_match ( '"profile file X1B2C3' , @:) 448 449func Xprof_test () 450endfunc 451call feedkeys ( ":profile func Xprof\<Tab>\<C-B>\"\<CR>" , 'tx' ) 452call assert_equal ( '"profile func Xprof_test' , @:) 453call feedkeys ( ":profile func Xprof\<Tab>\<C-B>\"\<CR>" , 'tx' ) 454call assert_equal ( '"profile func Xprof_test' , @:) 455call feedkeys ( ":profile func Xprof \<Tab>\<C-B>\"\<CR>" , 'tx' ) 456call assert_equal ( '"profile func Xprof ' , @:) 457call feedkeys ( ":profile func X1B2C3\<Tab>\<C-B>\"\<CR>" , 'tx' ) 458call assert_equal ( '"profile func X1B2C3' , @:) 459 460call feedkeys ( ":profdel \<C-A>\<C-B>\"\<CR>" , 'tx' ) 461call assert_equal ( '"profdel file func' , @:) 462call feedkeys ( ":profdel fu\<Tab>\<C-B>\"\<CR>" , 'tx' ) 463call assert_equal ( '"profdel func' , @:) 464call feedkeys ( ":profdel he\<Tab>\<C-B>\"\<CR>" , 'tx' ) 465call assert_equal ( '"profdel he' , @:) 466call feedkeys ( ":profdel here \<Tab>\<C-B>\"\<CR>" , 'tx' ) 467call assert_equal ( '"profdel here ' , @:) 468call feedkeys ( ":profdel file test_prof\<Tab>\<C-B>\"\<CR>" , 'tx' ) 469call assert_equal ( '"profdel file test_profile.vim' , @:) 470call feedkeys ( ":profdel file X1B2C3\<Tab>\<C-B>\"\<CR>" , 'tx' ) 471call assert_equal ( '"profdel file X1B2C3' , @:) 472call feedkeys ( ":profdel func Xprof\<Tab>\<C-B>\"\<CR>" , 'tx' ) 473call assert_equal ( '"profdel func Xprof_test' , @:) 474call feedkeys ( ":profdel func Xprof_test \<Tab>\<C-B>\"\<CR>" , 'tx' ) 475call assert_equal ( '"profdel func Xprof_test ' , @:) 476call feedkeys ( ":profdel func X1B2C3\<Tab>\<C-B>\"\<CR>" , 'tx' ) 477call assert_equal ( '"profdel func X1B2C3' , @:) 478 479delfunc Xprof_test 480endfunc 481 482func Test_profile_errors () 483call assert_fails ( "profile func Foo" , 'E750:' ) 484call assert_fails ( "profile pause" , 'E750:' ) 485call assert_fails ( "profile continue" , 'E750:' ) 486endfunc 487 488func Test_profile_truncate_mbyte () 489if & enc !=# 'utf-8' 490return 491endif 492 493let lines = [ 494 \'scriptencoding utf-8' , 495 \'func! Foo()' , 496 \' return [' , 497 \' \ "' . join ( map ( range ( 0x4E00 , 0x4E00 + 340 ), 'nr2char(v:val)' ), '' ) . '",' , 498 \' \ "' . join ( map ( range ( 0x4F00 , 0x4F00 + 340 ), 'nr2char(v:val)' ), '' ) . '",' , 499 \' \ ]' , 500 \'endfunc' , 501 \'call Foo()' , 502 \] 503 504call writefile ( lines , 'Xprofile_file.vim' ) 505call system ( GetVimCommandClean () 506 \. ' -es --cmd "set enc=utf-8"' 507 \. ' -c "profile start Xprofile_file.log"' 508 \. ' -c "profile file Xprofile_file.vim"' 509 \. ' -c "so Xprofile_file.vim"' 510 \. ' -c "qall!"' ) 511call assert_equal ( 0 , v: shell_error ) 512 513split Xprofile_file.log 514if & fenc != '' 515call assert_equal ( 'utf-8' , & fenc ) 516endif 517/ func ! Foo() 518let lnum = line ( '.' ) 519call assert_match ( '^\s*return \[$' , getline ( lnum + 1 )) 520call assert_match ( "\u4F52$" , getline ( lnum + 2 )) 521call assert_match ( "\u5052$" , getline ( lnum + 3 )) 522call assert_match ( '^\s*\\ \]$' , getline ( lnum + 4 )) 523bwipe ! 524 525call delete ( 'Xprofile_file.vim' ) 526call delete ( 'Xprofile_file.log' ) 527endfunc 528 529func Test_profdel_func () 530let lines =<< trim [CODE] 531profile start Xprofile_file.log 532func! Foo1() 533endfunc 534func! Foo2() 535endfunc 536func! Foo3() 537endfunc 538 539profile func Foo1 540profile func Foo2 541call Foo1() 542call Foo2() 543 544profile func Foo3 545profdel func Foo2 546profdel func Foo3 547call Foo1() 548call Foo2() 549call Foo3() 550[CODE] 551call writefile ( lines , 'Xprofile_file.vim' ) 552call system ( GetVimCommandClean () . ' -es --cmd "so Xprofile_file.vim" --cmd q' ) 553call assert_equal ( 0 , v: shell_error ) 554 555let lines = readfile ( 'Xprofile_file.log' ) 556call assert_equal ( 26 , len ( lines )) 557 558" Check that: 559" - Foo1() is called twice (profdel not invoked) 560" - Foo2() is called once (profdel invoked after it was called) 561" - Foo3() is not called (profdel invoked before it was called) 562call assert_equal ( 'FUNCTION Foo1()' , lines [ 0 ]) 563call assert_match ( 'Defined:.*Xprofile_file.vim' , lines [ 1 ]) 564call assert_equal ( 'Called 2 times' , lines [ 2 ]) 565call assert_equal ( 'FUNCTION Foo2()' , lines [ 8 ]) 566call assert_equal ( 'Called 1 time' , lines [ 10 ]) 567call assert_equal ( 'FUNCTIONS SORTED ON TOTAL TIME' , lines [ 16 ]) 568call assert_equal ( 'FUNCTIONS SORTED ON SELF TIME' , lines [ 21 ]) 569 570call delete ( 'Xprofile_file.vim' ) 571call delete ( 'Xprofile_file.log' ) 572endfunc 573 574func Test_profdel_star () 575" Foo() is invoked once before and once after 'profdel *'. 576" So profiling should report it only once. 577let lines =<< trim [CODE] 578profile start Xprofile_file.log 579func! Foo() 580endfunc 581profile func Foo 582call Foo() 583profdel * 584call Foo() 585[CODE] 586call writefile ( lines , 'Xprofile_file.vim' ) 587call system ( GetVimCommandClean () . ' -es -c "so Xprofile_file.vim" -c q' ) 588call assert_equal ( 0 , v: shell_error ) 589 590let lines = readfile ( 'Xprofile_file.log' ) 591call assert_equal ( 16 , len ( lines )) 592 593call assert_equal ( 'FUNCTION Foo()' , lines [ 0 ]) 594call assert_match ( 'Defined:.*Xprofile_file.vim' , lines [ 1 ]) 595call assert_equal ( 'Called 1 time' , lines [ 2 ]) 596call assert_equal ( 'FUNCTIONS SORTED ON TOTAL TIME' , lines [ 8 ]) 597call assert_equal ( 'FUNCTIONS SORTED ON SELF TIME' , lines [ 12 ]) 598 599call delete ( 'Xprofile_file.vim' ) 600call delete ( 'Xprofile_file.log' ) 601endfunc 602 603" When typing the function it won't have a script ID, test that this works. 604func Test_profile_typed_func () 605if ! CanRunVimInTerminal () 606throw 'Skipped: cannot run Vim in a terminal window' 607endif 608 609let lines =<< trim END 610profile start XprofileTypedFunc 611END 612call writefile ( lines , 'XtestProfile' ) 613let buf = RunVimInTerminal ( '-S XtestProfile' , #{}) 614 615call term_sendkeys ( buf , ":func DoSomething()\<CR>" 616 \.. "echo 'hello'\<CR>" 617 \.. "endfunc\<CR>" ) 618call term_sendkeys ( buf , ":profile func DoSomething\<CR>" ) 619call term_sendkeys ( buf , ":call DoSomething()\<CR>" ) 620call TermWait ( buf , 100 ) 621call StopVimInTerminal ( buf ) 622let lines = readfile ( 'XprofileTypedFunc' ) 623call assert_equal ( "FUNCTION DoSomething()" , lines [ 0 ]) 624call assert_equal ( "Called 1 time" , lines [ 1 ]) 625 626" clean up 627call delete ( 'XprofileTypedFunc' ) 628call delete ( 'XtestProfile' ) 629endfunc 630 631func Test_vim9_profiling () 632throw 'Skipped: Vim9 script is N/A' 633" only tests that compiling and calling functions doesn't crash 634let lines =<< trim END 635vim9script 636def Func() 637Crash() 638enddef 639def Crash() 640enddef 641prof start Xprofile_crash.log 642prof func Func 643Func() 644END 645call writefile ( lines , 'Xprofile_crash.vim' ) 646call system ( GetVimCommandClean () . ' -es -c "so Xprofile_crash.vim" -c q' ) 647call assert_equal ( 0 , v: shell_error ) 648call assert_true ( readfile ( 'Xprofile_crash.log' ) ->len () > 10 ) 649call delete ( 'Xprofile_crash.vim' ) 650call delete ( 'Xprofile_crash.log' ) 651endfunc 652 653 654" vim: shiftwidth=2 sts=2 expandtab