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 607a073549SBram Moolenaar" Avoid stopping at the "hit enter" prompt 617a073549SBram Moolenaarset nomore 627a073549SBram Moolenaar 63c0662466SBram Moolenaar" Output all messages in English. 64c0662466SBram Moolenaarlang mess C 65c0662466SBram Moolenaar 66f60b796fSBram Moolenaar" Always use forward slashes. 67f60b796fSBram Moolenaarset shellslash 68f60b796fSBram Moolenaar 6928fb79dbSBram Moolenaarlet s:srcdir = expand('%:p:h:h') 7028fb79dbSBram Moolenaar 718e8df251SBram Moolenaar" Prepare for calling test_garbagecollect_now(). 72ebf7dfa6SBram Moolenaarlet v:testing = 1 73ebf7dfa6SBram Moolenaar 7428fb79dbSBram Moolenaar" Support function: get the alloc ID by name. 7528fb79dbSBram Moolenaarfunction GetAllocId(name) 7628fb79dbSBram Moolenaar exe 'split ' . s:srcdir . '/alloc.h' 77065ee9aeSBram Moolenaar let top = search('typedef enum') 78065ee9aeSBram Moolenaar if top == 0 79065ee9aeSBram Moolenaar call add(v:errors, 'typedef not found in alloc.h') 80065ee9aeSBram Moolenaar endif 8128fb79dbSBram Moolenaar let lnum = search('aid_' . a:name . ',') 8228fb79dbSBram Moolenaar if lnum == 0 8328fb79dbSBram Moolenaar call add(v:errors, 'Alloc ID ' . a:name . ' not defined') 8428fb79dbSBram Moolenaar endif 8528fb79dbSBram Moolenaar close 86065ee9aeSBram Moolenaar return lnum - top - 1 8728fb79dbSBram Moolenaarendfunc 8828fb79dbSBram Moolenaar 8942205551SBram Moolenaarfunc RunTheTest(test) 90b5760a1cSBram Moolenaar echo 'Executing ' . a:test 91e5f2a075SBram Moolenaar 92e5f2a075SBram Moolenaar " Avoid stopping at the "hit enter" prompt 93e5f2a075SBram Moolenaar set nomore 94e5f2a075SBram Moolenaar 95e5f2a075SBram Moolenaar " Avoid a three second wait when a message is about to be overwritten by the 96e5f2a075SBram Moolenaar " mode message. 97e5f2a075SBram Moolenaar set noshowmode 98e5f2a075SBram Moolenaar 99eb992cb9SBram Moolenaar " Clear any overrides. 100eb992cb9SBram Moolenaar call test_override('ALL', 0) 101eb992cb9SBram Moolenaar 102b5760a1cSBram Moolenaar if exists("*SetUp") 103cc28e2d0SBram Moolenaar try 104b5760a1cSBram Moolenaar call SetUp() 105cc28e2d0SBram Moolenaar catch 106cc28e2d0SBram Moolenaar call add(v:errors, 'Caught exception in SetUp() before ' . a:test . ': ' . v:exception . ' @ ' . v:throwpoint) 107cc28e2d0SBram Moolenaar endtry 108b5760a1cSBram Moolenaar endif 109b5760a1cSBram Moolenaar 110b5760a1cSBram Moolenaar call add(s:messages, 'Executing ' . a:test) 111b5760a1cSBram Moolenaar let s:done += 1 112f204e05aSBram Moolenaar 113f204e05aSBram Moolenaar if a:test =~ 'Test_nocatch_' 114f204e05aSBram Moolenaar " Function handles errors itself. This avoids skipping commands after the 115f204e05aSBram Moolenaar " error. 116f204e05aSBram Moolenaar exe 'call ' . a:test 117f204e05aSBram Moolenaar else 118b5760a1cSBram Moolenaar try 119b5760a1cSBram Moolenaar exe 'call ' . a:test 120dac1947bSBram Moolenaar catch /^\cskipped/ 121dac1947bSBram Moolenaar call add(s:messages, ' Skipped') 122dac1947bSBram Moolenaar call add(s:skipped, 'SKIPPED ' . a:test . ': ' . substitute(v:exception, '^\S*\s\+', '', '')) 123b5760a1cSBram Moolenaar catch 124b5760a1cSBram Moolenaar call add(v:errors, 'Caught exception in ' . a:test . ': ' . v:exception . ' @ ' . v:throwpoint) 125b5760a1cSBram Moolenaar endtry 126f204e05aSBram Moolenaar endif 127b5760a1cSBram Moolenaar 128b5760a1cSBram Moolenaar if exists("*TearDown") 129cc28e2d0SBram Moolenaar try 130b5760a1cSBram Moolenaar call TearDown() 131cc28e2d0SBram Moolenaar catch 132cc28e2d0SBram Moolenaar call add(v:errors, 'Caught exception in TearDown() after ' . a:test . ': ' . v:exception . ' @ ' . v:throwpoint) 133cc28e2d0SBram Moolenaar endtry 134b5760a1cSBram Moolenaar endif 1357cba71d7SBram Moolenaar 136*ce11de87SBram Moolenaar " Close any extra tab pages and windows and make the current one not modified. 137*ce11de87SBram Moolenaar while tabpagenr('$') > 1 138*ce11de87SBram Moolenaar bwipe! 139*ce11de87SBram Moolenaar endwhile 140*ce11de87SBram Moolenaar 141358308ddSBram Moolenaar while 1 142358308ddSBram Moolenaar let wincount = winnr('$') 143358308ddSBram Moolenaar if wincount == 1 144358308ddSBram Moolenaar break 145358308ddSBram Moolenaar endif 1467cba71d7SBram Moolenaar bwipe! 147358308ddSBram Moolenaar if wincount == winnr('$') 148358308ddSBram Moolenaar " Did not manage to close a window. 149358308ddSBram Moolenaar only! 150358308ddSBram Moolenaar break 151358308ddSBram Moolenaar endif 1527cba71d7SBram Moolenaar endwhile 153*ce11de87SBram Moolenaar 154*ce11de87SBram Moolenaar " Wipe out all buffers except the current one, then wipe the current one. 155*ce11de87SBram Moolenaar for nr in range(1, bufnr('$')) 156*ce11de87SBram Moolenaar if nr != bufnr('%') && bufexists(nr) 157*ce11de87SBram Moolenaar exe nr . 'bwipe!' 158*ce11de87SBram Moolenaar endif 159*ce11de87SBram Moolenaar endfor 1607cba71d7SBram Moolenaar set nomodified 161*ce11de87SBram Moolenaar bwipe 162b5760a1cSBram Moolenaarendfunc 16328fb79dbSBram Moolenaar 16442205551SBram Moolenaarfunc AfterTheTest() 16543345546SBram Moolenaar if len(v:errors) > 0 16600af60bbSBram Moolenaar let s:fail += 1 16700af60bbSBram Moolenaar call add(s:errors, 'Found errors in ' . s:test . ':') 16800af60bbSBram Moolenaar call extend(s:errors, v:errors) 16943345546SBram Moolenaar let v:errors = [] 17043345546SBram Moolenaar endif 17142205551SBram Moolenaarendfunc 17242205551SBram Moolenaar 17342205551SBram Moolenaar" This function can be called by a test if it wants to abort testing. 17442205551SBram Moolenaarfunc FinishTesting() 17542205551SBram Moolenaar call AfterTheTest() 17643345546SBram Moolenaar 177fc4ad616SBram Moolenaar " Don't write viminfo on exit. 178fc4ad616SBram Moolenaar set viminfo= 179fc4ad616SBram Moolenaar 180d1ee0043SBram Moolenaar " Clean up files created by setup.vim 181d1ee0043SBram Moolenaar call delete('XfakeHOME', 'rf') 182d1ee0043SBram Moolenaar 18300af60bbSBram Moolenaar if s:fail == 0 18443345546SBram Moolenaar " Success, create the .res file so that make knows it's done. 18500af60bbSBram Moolenaar exe 'split ' . fnamemodify(g:testname, ':r') . '.res' 18643345546SBram Moolenaar write 18743345546SBram Moolenaar endif 18843345546SBram Moolenaar 18900af60bbSBram Moolenaar if len(s:errors) > 0 19043345546SBram Moolenaar " Append errors to test.log 19143345546SBram Moolenaar split test.log 19243345546SBram Moolenaar call append(line('$'), '') 19300af60bbSBram Moolenaar call append(line('$'), 'From ' . g:testname . ':') 19400af60bbSBram Moolenaar call append(line('$'), s:errors) 19543345546SBram Moolenaar write 19643345546SBram Moolenaar endif 19743345546SBram Moolenaar 19800af60bbSBram Moolenaar let message = 'Executed ' . s:done . (s:done > 1 ? ' tests' : ' test') 199096c8bb4SBram Moolenaar echo message 20000af60bbSBram Moolenaar call add(s:messages, message) 20100af60bbSBram Moolenaar if s:fail > 0 20200af60bbSBram Moolenaar let message = s:fail . ' FAILED:' 203096c8bb4SBram Moolenaar echo message 20400af60bbSBram Moolenaar call add(s:messages, message) 20500af60bbSBram Moolenaar call extend(s:messages, s:errors) 20643345546SBram Moolenaar endif 20743345546SBram Moolenaar 208dac1947bSBram Moolenaar " Add SKIPPED messages 209dac1947bSBram Moolenaar call extend(s:messages, s:skipped) 210dac1947bSBram Moolenaar 211dac1947bSBram Moolenaar " Append messages to the file "messages" 212096c8bb4SBram Moolenaar split messages 213096c8bb4SBram Moolenaar call append(line('$'), '') 21400af60bbSBram Moolenaar call append(line('$'), 'From ' . g:testname . ':') 21500af60bbSBram Moolenaar call append(line('$'), s:messages) 216096c8bb4SBram Moolenaar write 217096c8bb4SBram Moolenaar 21843345546SBram Moolenaar qall! 21942205551SBram Moolenaarendfunc 22042205551SBram Moolenaar 22142205551SBram Moolenaar" Source the test script. First grab the file name, in case the script 22242205551SBram Moolenaar" navigates away. g:testname can be used by the tests. 22342205551SBram Moolenaarlet g:testname = expand('%') 22442205551SBram Moolenaarlet s:done = 0 22542205551SBram Moolenaarlet s:fail = 0 22642205551SBram Moolenaarlet s:errors = [] 22742205551SBram Moolenaarlet s:messages = [] 22842205551SBram Moolenaarlet s:skipped = [] 22942205551SBram Moolenaarif expand('%') =~ 'test_vimscript.vim' 23042205551SBram Moolenaar " this test has intentional s:errors, don't use try/catch. 23142205551SBram Moolenaar source % 23242205551SBram Moolenaarelse 23342205551SBram Moolenaar try 23442205551SBram Moolenaar source % 23542205551SBram Moolenaar catch 23642205551SBram Moolenaar let s:fail += 1 23742205551SBram Moolenaar call add(s:errors, 'Caught exception: ' . v:exception . ' @ ' . v:throwpoint) 23842205551SBram Moolenaar endtry 23942205551SBram Moolenaarendif 24042205551SBram Moolenaar 24142205551SBram Moolenaar" Names of flaky tests. 24242205551SBram Moolenaarlet s:flaky = [ 24342205551SBram Moolenaar \ 'Test_client_server()', 24442205551SBram Moolenaar \ 'Test_close_and_exit_cb()', 24542205551SBram Moolenaar \ 'Test_collapse_buffers()', 24642205551SBram Moolenaar \ 'Test_communicate()', 24742205551SBram Moolenaar \ 'Test_exit_callback_interval()', 24842205551SBram Moolenaar \ 'Test_nb_basic()', 24942205551SBram Moolenaar \ 'Test_oneshot()', 25042205551SBram Moolenaar \ 'Test_pipe_through_sort_all()', 25142205551SBram Moolenaar \ 'Test_pipe_through_sort_some()', 25242205551SBram Moolenaar \ 'Test_quoteplus()', 2537dd48506SBram Moolenaar \ 'Test_quotestar()', 25442205551SBram Moolenaar \ 'Test_reltime()', 255f204e05aSBram Moolenaar \ 'Test_terminal_composing_unicode()', 25675a60f74SBram Moolenaar \ 'Test_terminal_noblock()', 257d09be321SBram Moolenaar \ 'Test_with_partial_callback()', 25842205551SBram Moolenaar \ ] 25942205551SBram Moolenaar 26042205551SBram Moolenaar" Locate Test_ functions and execute them. 26142205551SBram Moolenaarredir @q 26242205551SBram Moolenaarsilent function /^Test_ 26342205551SBram Moolenaarredir END 26442205551SBram Moolenaarlet s:tests = split(substitute(@q, 'function \(\k*()\)', '\1', 'g')) 26542205551SBram Moolenaar 26642205551SBram Moolenaar" If there is an extra argument filter the function names against it. 26742205551SBram Moolenaarif argc() > 1 26842205551SBram Moolenaar let s:tests = filter(s:tests, 'v:val =~ argv(1)') 26942205551SBram Moolenaarendif 27042205551SBram Moolenaar 27142205551SBram Moolenaar" Execute the tests in alphabetical order. 27242205551SBram Moolenaarfor s:test in sort(s:tests) 2734a6fcf80SBram Moolenaar " Silence, please! 2744a6fcf80SBram Moolenaar set belloff=all 2754a6fcf80SBram Moolenaar 27642205551SBram Moolenaar call RunTheTest(s:test) 27742205551SBram Moolenaar 27842205551SBram Moolenaar if len(v:errors) > 0 && index(s:flaky, s:test) >= 0 2796caf606bSBram Moolenaar call add(s:messages, 'Found errors in ' . s:test . ':') 2806caf606bSBram Moolenaar call extend(s:messages, v:errors) 28142205551SBram Moolenaar call add(s:messages, 'Flaky test failed, running it again') 28215e737f7SBram Moolenaar let first_run = v:errors 28315e737f7SBram Moolenaar 28442205551SBram Moolenaar let v:errors = [] 28542205551SBram Moolenaar call RunTheTest(s:test) 28615e737f7SBram Moolenaar if len(v:errors) > 0 28715e737f7SBram Moolenaar let second_run = v:errors 28815e737f7SBram Moolenaar let v:errors = ['First run:'] 28915e737f7SBram Moolenaar call extend(v:errors, first_run) 29015e737f7SBram Moolenaar call add(v:errors, 'Second run:') 29115e737f7SBram Moolenaar call extend(v:errors, second_run) 29215e737f7SBram Moolenaar endif 29342205551SBram Moolenaar endif 29442205551SBram Moolenaar 29542205551SBram Moolenaar call AfterTheTest() 29642205551SBram Moolenaarendfor 29742205551SBram Moolenaar 29842205551SBram Moolenaarcall FinishTesting() 299cc28e2d0SBram Moolenaar 300cc28e2d0SBram Moolenaar" vim: shiftwidth=2 sts=2 expandtab 301