143345546SBram Moolenaar" This script is sourced while editing the .vim file with the tests. 243345546SBram Moolenaar" When the script is successful the .res file will be created. 343345546SBram Moolenaar" Errors are appended to the test.log file. 443345546SBram Moolenaar" 5befb366aSBram Moolenaar" To execute only specific test functions, add a second argument. It will be 6befb366aSBram Moolenaar" matched against the names of the Test_ funtion. E.g.: 7befb366aSBram Moolenaar" ../vim -u NONE -S runtest.vim test_channel.vim open_delay 8befb366aSBram Moolenaar" The output can be found in the "messages" file. 9befb366aSBram Moolenaar" 1043345546SBram Moolenaar" The test script may contain anything, only functions that start with 1143345546SBram Moolenaar" "Test_" are special. These will be invoked and should contain assert 1243345546SBram Moolenaar" functions. See test_assert.vim for an example. 1343345546SBram Moolenaar" 1443345546SBram Moolenaar" It is possible to source other files that contain "Test_" functions. This 1543345546SBram Moolenaar" can speed up testing, since Vim does not need to restart. But be careful 1643345546SBram Moolenaar" that the tests do not interfere with each other. 1743345546SBram Moolenaar" 1843345546SBram Moolenaar" If an error cannot be detected properly with an assert function add the 1943345546SBram Moolenaar" error to the v:errors list: 2043345546SBram Moolenaar" call add(v:errors, 'test foo failed: Cannot find xyz') 2143345546SBram Moolenaar" 2243345546SBram Moolenaar" If preparation for each Test_ function is needed, define a SetUp function. 2343345546SBram Moolenaar" It will be called before each Test_ function. 2443345546SBram Moolenaar" 2543345546SBram Moolenaar" If cleanup after each Test_ function is needed, define a TearDown function. 2643345546SBram Moolenaar" It will be called after each Test_ function. 2700af60bbSBram Moolenaar" 2800af60bbSBram Moolenaar" When debugging a test it can be useful to add messages to v:errors: 2900af60bbSBram Moolenaar" call add(v:errors, "this happened") 3000af60bbSBram Moolenaar 3143345546SBram Moolenaar 3243345546SBram Moolenaar" Without the +eval feature we can't run these tests, bail out. 334686b323SBram Moolenaarso small.vim 3443345546SBram Moolenaar 3543345546SBram Moolenaar" Check that the screen size is at least 24 x 80 characters. 3643345546SBram Moolenaarif &lines < 24 || &columns < 80 3743345546SBram Moolenaar let error = 'Screen size too small! Tests require at least 24 lines with 80 characters' 3843345546SBram Moolenaar echoerr error 3943345546SBram Moolenaar split test.log 4043345546SBram Moolenaar $put =error 4143345546SBram Moolenaar w 4243345546SBram Moolenaar cquit 4343345546SBram Moolenaarendif 4443345546SBram Moolenaar 4589b10421SBram Moolenaar" Common with all tests on all systems. 4689b10421SBram Moolenaarsource setup.vim 4789b10421SBram Moolenaar 48c0662466SBram Moolenaar" For consistency run all tests with 'nocompatible' set. 49c0662466SBram Moolenaar" This also enables use of line continuation. 50c0662466SBram Moolenaarset nocp viminfo+=nviminfo 51c0662466SBram Moolenaar 52eb992cb9SBram Moolenaar" Use utf-8 or latin1 by default, instead of whatever the system default 53ac105ed3SBram Moolenaar" happens to be. Individual tests can overrule this at the top of the file. 54ac105ed3SBram Moolenaarif has('multi_byte') 55ac105ed3SBram Moolenaar set encoding=utf-8 56ac105ed3SBram Moolenaarelse 57ac105ed3SBram Moolenaar set encoding=latin1 58ac105ed3SBram Moolenaarendif 59ac105ed3SBram Moolenaar 60d8f27b30SBram Moolenaar" REDIR_TEST_TO_NULL has a very permissive SwapExists autocommand which is for 61d8f27b30SBram Moolenaar" the test_name.vim file itself. Replace it here with a more restrictive one, 62d8f27b30SBram Moolenaar" so we still catch mistakes. 63d8f27b30SBram Moolenaarlet s:test_script_fname = expand('%') 64d8f27b30SBram Moolenaarau! SwapExists * call HandleSwapExists() 65d8f27b30SBram Moolenaarfunc HandleSwapExists() 66d8f27b30SBram Moolenaar " Only ignore finding a swap file for the test script (the user might be 67d8f27b30SBram Moolenaar " editing it and do ":make test_name") and the output file. 68d8f27b30SBram Moolenaar if expand('<afile>') == 'messages' || expand('<afile>') =~ s:test_script_fname 69d8f27b30SBram Moolenaar let v:swapchoice = 'e' 70d8f27b30SBram Moolenaar endif 71d8f27b30SBram Moolenaarendfunc 72d8f27b30SBram Moolenaar 737a073549SBram Moolenaar" Avoid stopping at the "hit enter" prompt 747a073549SBram Moolenaarset nomore 757a073549SBram Moolenaar 76c0662466SBram Moolenaar" Output all messages in English. 77c0662466SBram Moolenaarlang mess C 78c0662466SBram Moolenaar 79f60b796fSBram Moolenaar" Always use forward slashes. 80f60b796fSBram Moolenaarset shellslash 81f60b796fSBram Moolenaar 8228fb79dbSBram Moolenaarlet s:srcdir = expand('%:p:h:h') 8328fb79dbSBram Moolenaar 848e8df251SBram Moolenaar" Prepare for calling test_garbagecollect_now(). 85ebf7dfa6SBram Moolenaarlet v:testing = 1 86ebf7dfa6SBram Moolenaar 8728fb79dbSBram Moolenaar" Support function: get the alloc ID by name. 8828fb79dbSBram Moolenaarfunction GetAllocId(name) 8928fb79dbSBram Moolenaar exe 'split ' . s:srcdir . '/alloc.h' 90065ee9aeSBram Moolenaar let top = search('typedef enum') 91065ee9aeSBram Moolenaar if top == 0 92065ee9aeSBram Moolenaar call add(v:errors, 'typedef not found in alloc.h') 93065ee9aeSBram Moolenaar endif 9428fb79dbSBram Moolenaar let lnum = search('aid_' . a:name . ',') 9528fb79dbSBram Moolenaar if lnum == 0 9628fb79dbSBram Moolenaar call add(v:errors, 'Alloc ID ' . a:name . ' not defined') 9728fb79dbSBram Moolenaar endif 9828fb79dbSBram Moolenaar close 99065ee9aeSBram Moolenaar return lnum - top - 1 10028fb79dbSBram Moolenaarendfunc 10128fb79dbSBram Moolenaar 10242205551SBram Moolenaarfunc RunTheTest(test) 103b5760a1cSBram Moolenaar echo 'Executing ' . a:test 104e5f2a075SBram Moolenaar 105e5f2a075SBram Moolenaar " Avoid stopping at the "hit enter" prompt 106e5f2a075SBram Moolenaar set nomore 107e5f2a075SBram Moolenaar 108e5f2a075SBram Moolenaar " Avoid a three second wait when a message is about to be overwritten by the 109e5f2a075SBram Moolenaar " mode message. 110e5f2a075SBram Moolenaar set noshowmode 111e5f2a075SBram Moolenaar 112eb992cb9SBram Moolenaar " Clear any overrides. 113eb992cb9SBram Moolenaar call test_override('ALL', 0) 114eb992cb9SBram Moolenaar 115cf1ba35fSBram Moolenaar " Some tests wipe out buffers. To be consistent, always wipe out all 116cf1ba35fSBram Moolenaar " buffers. 117cf1ba35fSBram Moolenaar %bwipe! 118cf1ba35fSBram Moolenaar 119209d3874SBram Moolenaar " The test may change the current directory. Save and restore the 120209d3874SBram Moolenaar " directory after executing the test. 121209d3874SBram Moolenaar let save_cwd = getcwd() 122209d3874SBram Moolenaar 123b5760a1cSBram Moolenaar if exists("*SetUp") 124cc28e2d0SBram Moolenaar try 125b5760a1cSBram Moolenaar call SetUp() 126cc28e2d0SBram Moolenaar catch 127cc28e2d0SBram Moolenaar call add(v:errors, 'Caught exception in SetUp() before ' . a:test . ': ' . v:exception . ' @ ' . v:throwpoint) 128cc28e2d0SBram Moolenaar endtry 129b5760a1cSBram Moolenaar endif 130b5760a1cSBram Moolenaar 131b5760a1cSBram Moolenaar call add(s:messages, 'Executing ' . a:test) 132b5760a1cSBram Moolenaar let s:done += 1 133f204e05aSBram Moolenaar 134f204e05aSBram Moolenaar if a:test =~ 'Test_nocatch_' 135f204e05aSBram Moolenaar " Function handles errors itself. This avoids skipping commands after the 136f204e05aSBram Moolenaar " error. 137f204e05aSBram Moolenaar exe 'call ' . a:test 138f204e05aSBram Moolenaar else 139b5760a1cSBram Moolenaar try 1408903676dSBram Moolenaar let s:test = a:test 1418903676dSBram Moolenaar au VimLeavePre * call EarlyExit(s:test) 142b5760a1cSBram Moolenaar exe 'call ' . a:test 1438903676dSBram Moolenaar au! VimLeavePre 144dac1947bSBram Moolenaar catch /^\cskipped/ 145dac1947bSBram Moolenaar call add(s:messages, ' Skipped') 146dac1947bSBram Moolenaar call add(s:skipped, 'SKIPPED ' . a:test . ': ' . substitute(v:exception, '^\S*\s\+', '', '')) 147b5760a1cSBram Moolenaar catch 148b5760a1cSBram Moolenaar call add(v:errors, 'Caught exception in ' . a:test . ': ' . v:exception . ' @ ' . v:throwpoint) 149b5760a1cSBram Moolenaar endtry 150f204e05aSBram Moolenaar endif 151b5760a1cSBram Moolenaar 152b5760a1cSBram Moolenaar if exists("*TearDown") 153cc28e2d0SBram Moolenaar try 154b5760a1cSBram Moolenaar call TearDown() 155cc28e2d0SBram Moolenaar catch 156cc28e2d0SBram Moolenaar call add(v:errors, 'Caught exception in TearDown() after ' . a:test . ': ' . v:exception . ' @ ' . v:throwpoint) 157cc28e2d0SBram Moolenaar endtry 158b5760a1cSBram Moolenaar endif 1597cba71d7SBram Moolenaar 160cf1ba35fSBram Moolenaar " Clear any autocommands 161cf1ba35fSBram Moolenaar au! 162d8f27b30SBram Moolenaar au SwapExists * call HandleSwapExists() 163cf1ba35fSBram Moolenaar 164ce11de87SBram Moolenaar " Close any extra tab pages and windows and make the current one not modified. 165ce11de87SBram Moolenaar while tabpagenr('$') > 1 166cf1ba35fSBram Moolenaar quit! 167ce11de87SBram Moolenaar endwhile 168ce11de87SBram Moolenaar 169358308ddSBram Moolenaar while 1 170358308ddSBram Moolenaar let wincount = winnr('$') 171358308ddSBram Moolenaar if wincount == 1 172358308ddSBram Moolenaar break 173358308ddSBram Moolenaar endif 1747cba71d7SBram Moolenaar bwipe! 175358308ddSBram Moolenaar if wincount == winnr('$') 176358308ddSBram Moolenaar " Did not manage to close a window. 177358308ddSBram Moolenaar only! 178358308ddSBram Moolenaar break 179358308ddSBram Moolenaar endif 1807cba71d7SBram Moolenaar endwhile 181209d3874SBram Moolenaar 182209d3874SBram Moolenaar exe 'cd ' . save_cwd 183b5760a1cSBram Moolenaarendfunc 18428fb79dbSBram Moolenaar 18542205551SBram Moolenaarfunc AfterTheTest() 18643345546SBram Moolenaar if len(v:errors) > 0 18700af60bbSBram Moolenaar let s:fail += 1 18800af60bbSBram Moolenaar call add(s:errors, 'Found errors in ' . s:test . ':') 18900af60bbSBram Moolenaar call extend(s:errors, v:errors) 19043345546SBram Moolenaar let v:errors = [] 19143345546SBram Moolenaar endif 19242205551SBram Moolenaarendfunc 19342205551SBram Moolenaar 1948903676dSBram Moolenaarfunc EarlyExit(test) 1958903676dSBram Moolenaar " It's OK for the test we use to test the quit detection. 1968903676dSBram Moolenaar if a:test != 'Test_zz_quit_detected()' 1978903676dSBram Moolenaar call add(v:errors, 'Test caused Vim to exit: ' . a:test) 1988903676dSBram Moolenaar endif 1998903676dSBram Moolenaar 2008903676dSBram Moolenaar call FinishTesting() 2018903676dSBram Moolenaarendfunc 2028903676dSBram Moolenaar 20342205551SBram Moolenaar" This function can be called by a test if it wants to abort testing. 20442205551SBram Moolenaarfunc FinishTesting() 20542205551SBram Moolenaar call AfterTheTest() 20643345546SBram Moolenaar 207fc4ad616SBram Moolenaar " Don't write viminfo on exit. 208fc4ad616SBram Moolenaar set viminfo= 209fc4ad616SBram Moolenaar 210d1ee0043SBram Moolenaar " Clean up files created by setup.vim 211d1ee0043SBram Moolenaar call delete('XfakeHOME', 'rf') 212d1ee0043SBram Moolenaar 21300af60bbSBram Moolenaar if s:fail == 0 21443345546SBram Moolenaar " Success, create the .res file so that make knows it's done. 21500af60bbSBram Moolenaar exe 'split ' . fnamemodify(g:testname, ':r') . '.res' 21643345546SBram Moolenaar write 21743345546SBram Moolenaar endif 21843345546SBram Moolenaar 21900af60bbSBram Moolenaar if len(s:errors) > 0 22043345546SBram Moolenaar " Append errors to test.log 22143345546SBram Moolenaar split test.log 22243345546SBram Moolenaar call append(line('$'), '') 22300af60bbSBram Moolenaar call append(line('$'), 'From ' . g:testname . ':') 22400af60bbSBram Moolenaar call append(line('$'), s:errors) 22543345546SBram Moolenaar write 22643345546SBram Moolenaar endif 22743345546SBram Moolenaar 22829f9ed20SBram Moolenaar if s:done == 0 22929f9ed20SBram Moolenaar let message = 'NO tests executed' 23029f9ed20SBram Moolenaar else 23100af60bbSBram Moolenaar let message = 'Executed ' . s:done . (s:done > 1 ? ' tests' : ' test') 23229f9ed20SBram Moolenaar endif 233096c8bb4SBram Moolenaar echo message 23400af60bbSBram Moolenaar call add(s:messages, message) 23500af60bbSBram Moolenaar if s:fail > 0 23600af60bbSBram Moolenaar let message = s:fail . ' FAILED:' 237096c8bb4SBram Moolenaar echo message 23800af60bbSBram Moolenaar call add(s:messages, message) 23900af60bbSBram Moolenaar call extend(s:messages, s:errors) 24043345546SBram Moolenaar endif 24143345546SBram Moolenaar 242dac1947bSBram Moolenaar " Add SKIPPED messages 243dac1947bSBram Moolenaar call extend(s:messages, s:skipped) 244dac1947bSBram Moolenaar 245dac1947bSBram Moolenaar " Append messages to the file "messages" 246096c8bb4SBram Moolenaar split messages 247096c8bb4SBram Moolenaar call append(line('$'), '') 24800af60bbSBram Moolenaar call append(line('$'), 'From ' . g:testname . ':') 24900af60bbSBram Moolenaar call append(line('$'), s:messages) 250096c8bb4SBram Moolenaar write 251096c8bb4SBram Moolenaar 25243345546SBram Moolenaar qall! 25342205551SBram Moolenaarendfunc 25442205551SBram Moolenaar 25542205551SBram Moolenaar" Source the test script. First grab the file name, in case the script 25642205551SBram Moolenaar" navigates away. g:testname can be used by the tests. 25742205551SBram Moolenaarlet g:testname = expand('%') 25842205551SBram Moolenaarlet s:done = 0 25942205551SBram Moolenaarlet s:fail = 0 26042205551SBram Moolenaarlet s:errors = [] 26142205551SBram Moolenaarlet s:messages = [] 26242205551SBram Moolenaarlet s:skipped = [] 26342205551SBram Moolenaarif expand('%') =~ 'test_vimscript.vim' 26442205551SBram Moolenaar " this test has intentional s:errors, don't use try/catch. 26542205551SBram Moolenaar source % 26642205551SBram Moolenaarelse 26742205551SBram Moolenaar try 26842205551SBram Moolenaar source % 26942205551SBram Moolenaar catch 27042205551SBram Moolenaar let s:fail += 1 27142205551SBram Moolenaar call add(s:errors, 'Caught exception: ' . v:exception . ' @ ' . v:throwpoint) 27242205551SBram Moolenaar endtry 27342205551SBram Moolenaarendif 27442205551SBram Moolenaar 27542205551SBram Moolenaar" Names of flaky tests. 27642205551SBram Moolenaarlet s:flaky = [ 27742205551SBram Moolenaar \ 'Test_client_server()', 27842205551SBram Moolenaar \ 'Test_close_and_exit_cb()', 27942205551SBram Moolenaar \ 'Test_collapse_buffers()', 28042205551SBram Moolenaar \ 'Test_communicate()', 28165873846SBram Moolenaar \ 'Test_cwd()', 282*218959bcSBram Moolenaar \ 'Test_diff_screen()', 28342205551SBram Moolenaar \ 'Test_exit_callback_interval()', 28442205551SBram Moolenaar \ 'Test_nb_basic()', 28542205551SBram Moolenaar \ 'Test_oneshot()', 2861eca6f13SBram Moolenaar \ 'Test_out_cb()', 28724820691SBram Moolenaar \ 'Test_paused()', 28842205551SBram Moolenaar \ 'Test_pipe_through_sort_all()', 28942205551SBram Moolenaar \ 'Test_pipe_through_sort_some()', 290142ae736SBram Moolenaar \ 'Test_popup_and_window_resize()', 29142205551SBram Moolenaar \ 'Test_quoteplus()', 2927dd48506SBram Moolenaar \ 'Test_quotestar()', 29342205551SBram Moolenaar \ 'Test_reltime()', 294bfbea567SBram Moolenaar \ 'Test_repeat_three()', 295f204e05aSBram Moolenaar \ 'Test_terminal_composing_unicode()', 29675a60f74SBram Moolenaar \ 'Test_terminal_noblock()', 2977dd88c51SBram Moolenaar \ 'Test_terminal_redir_file()', 29824820691SBram Moolenaar \ 'Test_terminal_tmap()', 299d09be321SBram Moolenaar \ 'Test_with_partial_callback()', 30042205551SBram Moolenaar \ ] 30142205551SBram Moolenaar 30242205551SBram Moolenaar" Locate Test_ functions and execute them. 30342205551SBram Moolenaarredir @q 30442205551SBram Moolenaarsilent function /^Test_ 30542205551SBram Moolenaarredir END 30642205551SBram Moolenaarlet s:tests = split(substitute(@q, 'function \(\k*()\)', '\1', 'g')) 30742205551SBram Moolenaar 30842205551SBram Moolenaar" If there is an extra argument filter the function names against it. 30942205551SBram Moolenaarif argc() > 1 31042205551SBram Moolenaar let s:tests = filter(s:tests, 'v:val =~ argv(1)') 31142205551SBram Moolenaarendif 31242205551SBram Moolenaar 31342205551SBram Moolenaar" Execute the tests in alphabetical order. 31442205551SBram Moolenaarfor s:test in sort(s:tests) 3154a6fcf80SBram Moolenaar " Silence, please! 3164a6fcf80SBram Moolenaar set belloff=all 3174a6fcf80SBram Moolenaar 31842205551SBram Moolenaar call RunTheTest(s:test) 31942205551SBram Moolenaar 32042205551SBram Moolenaar if len(v:errors) > 0 && index(s:flaky, s:test) >= 0 3216caf606bSBram Moolenaar call add(s:messages, 'Found errors in ' . s:test . ':') 3226caf606bSBram Moolenaar call extend(s:messages, v:errors) 32342205551SBram Moolenaar call add(s:messages, 'Flaky test failed, running it again') 32415e737f7SBram Moolenaar let first_run = v:errors 32515e737f7SBram Moolenaar 32655058601SBram Moolenaar " Flakiness is often caused by the system being very busy. Sleep a couple 32755058601SBram Moolenaar " of seconds to have a higher chance of succeeding the second time. 32855058601SBram Moolenaar sleep 2 32955058601SBram Moolenaar 33042205551SBram Moolenaar let v:errors = [] 33142205551SBram Moolenaar call RunTheTest(s:test) 33215e737f7SBram Moolenaar if len(v:errors) > 0 33315e737f7SBram Moolenaar let second_run = v:errors 33415e737f7SBram Moolenaar let v:errors = ['First run:'] 33515e737f7SBram Moolenaar call extend(v:errors, first_run) 33615e737f7SBram Moolenaar call add(v:errors, 'Second run:') 33715e737f7SBram Moolenaar call extend(v:errors, second_run) 33815e737f7SBram Moolenaar endif 33942205551SBram Moolenaar endif 34042205551SBram Moolenaar 34142205551SBram Moolenaar call AfterTheTest() 34242205551SBram Moolenaarendfor 34342205551SBram Moolenaar 34442205551SBram Moolenaarcall FinishTesting() 345cc28e2d0SBram Moolenaar 346cc28e2d0SBram Moolenaar" vim: shiftwidth=2 sts=2 expandtab 347