xref: /vim-8.2.3635/src/testdir/runtest.vim (revision e5f2a075)
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
52ac105ed3SBram Moolenaar" Use utf-8 or latin1 be 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
60c0662466SBram Moolenaar" Output all messages in English.
61c0662466SBram Moolenaarlang mess C
62c0662466SBram Moolenaar
63f60b796fSBram Moolenaar" Always use forward slashes.
64f60b796fSBram Moolenaarset shellslash
65f60b796fSBram Moolenaar
6628fb79dbSBram Moolenaarlet s:srcdir = expand('%:p:h:h')
6728fb79dbSBram Moolenaar
688e8df251SBram Moolenaar" Prepare for calling test_garbagecollect_now().
69ebf7dfa6SBram Moolenaarlet v:testing = 1
70ebf7dfa6SBram Moolenaar
7128fb79dbSBram Moolenaar" Support function: get the alloc ID by name.
7228fb79dbSBram Moolenaarfunction GetAllocId(name)
7328fb79dbSBram Moolenaar  exe 'split ' . s:srcdir . '/alloc.h'
74065ee9aeSBram Moolenaar  let top = search('typedef enum')
75065ee9aeSBram Moolenaar  if top == 0
76065ee9aeSBram Moolenaar    call add(v:errors, 'typedef not found in alloc.h')
77065ee9aeSBram Moolenaar  endif
7828fb79dbSBram Moolenaar  let lnum = search('aid_' . a:name . ',')
7928fb79dbSBram Moolenaar  if lnum == 0
8028fb79dbSBram Moolenaar    call add(v:errors, 'Alloc ID ' . a:name . ' not defined')
8128fb79dbSBram Moolenaar  endif
8228fb79dbSBram Moolenaar  close
83065ee9aeSBram Moolenaar  return lnum - top - 1
8428fb79dbSBram Moolenaarendfunc
8528fb79dbSBram Moolenaar
86b5760a1cSBram Moolenaarfunction RunTheTest(test)
87b5760a1cSBram Moolenaar  echo 'Executing ' . a:test
88*e5f2a075SBram Moolenaar
89*e5f2a075SBram Moolenaar  " Avoid stopping at the "hit enter" prompt
90*e5f2a075SBram Moolenaar  set nomore
91*e5f2a075SBram Moolenaar
92*e5f2a075SBram Moolenaar  " Avoid a three second wait when a message is about to be overwritten by the
93*e5f2a075SBram Moolenaar  " mode message.
94*e5f2a075SBram Moolenaar  set noshowmode
95*e5f2a075SBram Moolenaar
96b5760a1cSBram Moolenaar  if exists("*SetUp")
97cc28e2d0SBram Moolenaar    try
98b5760a1cSBram Moolenaar      call SetUp()
99cc28e2d0SBram Moolenaar    catch
100cc28e2d0SBram Moolenaar      call add(v:errors, 'Caught exception in SetUp() before ' . a:test . ': ' . v:exception . ' @ ' . v:throwpoint)
101cc28e2d0SBram Moolenaar    endtry
102b5760a1cSBram Moolenaar  endif
103b5760a1cSBram Moolenaar
104b5760a1cSBram Moolenaar  call add(s:messages, 'Executing ' . a:test)
105b5760a1cSBram Moolenaar  let s:done += 1
106b5760a1cSBram Moolenaar  try
107b5760a1cSBram Moolenaar    exe 'call ' . a:test
108dac1947bSBram Moolenaar  catch /^\cskipped/
109dac1947bSBram Moolenaar    call add(s:messages, '    Skipped')
110dac1947bSBram Moolenaar    call add(s:skipped, 'SKIPPED ' . a:test . ': ' . substitute(v:exception, '^\S*\s\+', '',  ''))
111b5760a1cSBram Moolenaar  catch
112b5760a1cSBram Moolenaar    call add(v:errors, 'Caught exception in ' . a:test . ': ' . v:exception . ' @ ' . v:throwpoint)
113b5760a1cSBram Moolenaar  endtry
114b5760a1cSBram Moolenaar
115b5760a1cSBram Moolenaar  if exists("*TearDown")
116cc28e2d0SBram Moolenaar    try
117b5760a1cSBram Moolenaar      call TearDown()
118cc28e2d0SBram Moolenaar    catch
119cc28e2d0SBram Moolenaar      call add(v:errors, 'Caught exception in TearDown() after ' . a:test . ': ' . v:exception . ' @ ' . v:throwpoint)
120cc28e2d0SBram Moolenaar    endtry
121b5760a1cSBram Moolenaar  endif
1227cba71d7SBram Moolenaar
1237cba71d7SBram Moolenaar  " Close any extra windows and make the current one not modified.
124358308ddSBram Moolenaar  while 1
125358308ddSBram Moolenaar    let wincount = winnr('$')
126358308ddSBram Moolenaar    if wincount == 1
127358308ddSBram Moolenaar      break
128358308ddSBram Moolenaar    endif
1297cba71d7SBram Moolenaar    bwipe!
130358308ddSBram Moolenaar    if wincount == winnr('$')
131358308ddSBram Moolenaar      " Did not manage to close a window.
132358308ddSBram Moolenaar      only!
133358308ddSBram Moolenaar      break
134358308ddSBram Moolenaar    endif
1357cba71d7SBram Moolenaar  endwhile
1367cba71d7SBram Moolenaar  set nomodified
137b5760a1cSBram Moolenaarendfunc
13828fb79dbSBram Moolenaar
13943345546SBram Moolenaar" Source the test script.  First grab the file name, in case the script
14000af60bbSBram Moolenaar" navigates away.  g:testname can be used by the tests.
14100af60bbSBram Moolenaarlet g:testname = expand('%')
14200af60bbSBram Moolenaarlet s:done = 0
14300af60bbSBram Moolenaarlet s:fail = 0
14400af60bbSBram Moolenaarlet s:errors = []
14500af60bbSBram Moolenaarlet s:messages = []
146dac1947bSBram Moolenaarlet s:skipped = []
147a2cce863SBram Moolenaarif expand('%') =~ 'test_viml.vim'
14800af60bbSBram Moolenaar  " this test has intentional s:errors, don't use try/catch.
149a2cce863SBram Moolenaar  source %
150a2cce863SBram Moolenaarelse
1514686b323SBram Moolenaar  try
15243345546SBram Moolenaar    source %
1534686b323SBram Moolenaar  catch
15400af60bbSBram Moolenaar    let s:fail += 1
15500af60bbSBram Moolenaar    call add(s:errors, 'Caught exception: ' . v:exception . ' @ ' . v:throwpoint)
1564686b323SBram Moolenaar  endtry
157a2cce863SBram Moolenaarendif
15843345546SBram Moolenaar
159b5760a1cSBram Moolenaar" Names of flaky tests.
160e1c8c7a6SBram Moolenaarlet s:flaky = [
1616fe2eb43SBram Moolenaar      \ 'Test_close_and_exit_cb()',
162b245559fSBram Moolenaar      \ 'Test_collapse_buffers()',
163b245559fSBram Moolenaar      \ 'Test_communicate()',
164b245559fSBram Moolenaar      \ 'Test_nb_basic()',
165c79d6aa0SBram Moolenaar      \ 'Test_pipe_through_sort_all()',
1664e032e1bSBram Moolenaar      \ 'Test_pipe_through_sort_some()',
167b245559fSBram Moolenaar      \ 'Test_reltime()',
168e1c8c7a6SBram Moolenaar      \ ]
169b5760a1cSBram Moolenaar
17043345546SBram Moolenaar" Locate Test_ functions and execute them.
171a99b9043SBram Moolenaarset nomore
17243345546SBram Moolenaarredir @q
17393bf558cSBram Moolenaarsilent function /^Test_
17443345546SBram Moolenaarredir END
17500af60bbSBram Moolenaarlet s:tests = split(substitute(@q, 'function \(\k*()\)', '\1', 'g'))
17643345546SBram Moolenaar
177befb366aSBram Moolenaar" If there is an extra argument filter the function names against it.
178befb366aSBram Moolenaarif argc() > 1
179befb366aSBram Moolenaar  let s:tests = filter(s:tests, 'v:val =~ argv(1)')
180befb366aSBram Moolenaarendif
181befb366aSBram Moolenaar
182cfc0a350SBram Moolenaar" Execute the tests in alphabetical order.
18300af60bbSBram Moolenaarfor s:test in sort(s:tests)
184b5760a1cSBram Moolenaar  call RunTheTest(s:test)
18543345546SBram Moolenaar
186b5760a1cSBram Moolenaar  if len(v:errors) > 0 && index(s:flaky, s:test) >= 0
187b5760a1cSBram Moolenaar    call add(s:messages, 'Flaky test failed, running it again')
188b5760a1cSBram Moolenaar    let v:errors = []
189b5760a1cSBram Moolenaar    call RunTheTest(s:test)
190b5760a1cSBram Moolenaar  endif
19143345546SBram Moolenaar
19243345546SBram Moolenaar  if len(v:errors) > 0
19300af60bbSBram Moolenaar    let s:fail += 1
19400af60bbSBram Moolenaar    call add(s:errors, 'Found errors in ' . s:test . ':')
19500af60bbSBram Moolenaar    call extend(s:errors, v:errors)
19643345546SBram Moolenaar    let v:errors = []
19743345546SBram Moolenaar  endif
19843345546SBram Moolenaarendfor
19943345546SBram Moolenaar
200fc4ad616SBram Moolenaar" Don't write viminfo on exit.
201fc4ad616SBram Moolenaarset viminfo=
202fc4ad616SBram Moolenaar
20300af60bbSBram Moolenaarif s:fail == 0
20443345546SBram Moolenaar  " Success, create the .res file so that make knows it's done.
20500af60bbSBram Moolenaar  exe 'split ' . fnamemodify(g:testname, ':r') . '.res'
20643345546SBram Moolenaar  write
20743345546SBram Moolenaarendif
20843345546SBram Moolenaar
20900af60bbSBram Moolenaarif len(s:errors) > 0
21043345546SBram Moolenaar  " Append errors to test.log
21143345546SBram Moolenaar  split test.log
21243345546SBram Moolenaar  call append(line('$'), '')
21300af60bbSBram Moolenaar  call append(line('$'), 'From ' . g:testname . ':')
21400af60bbSBram Moolenaar  call append(line('$'), s:errors)
21543345546SBram Moolenaar  write
21643345546SBram Moolenaarendif
21743345546SBram Moolenaar
21800af60bbSBram Moolenaarlet message = 'Executed ' . s:done . (s:done > 1 ? ' tests' : ' test')
219096c8bb4SBram Moolenaarecho message
22000af60bbSBram Moolenaarcall add(s:messages, message)
22100af60bbSBram Moolenaarif s:fail > 0
22200af60bbSBram Moolenaar  let message = s:fail . ' FAILED:'
223096c8bb4SBram Moolenaar  echo message
22400af60bbSBram Moolenaar  call add(s:messages, message)
22500af60bbSBram Moolenaar  call extend(s:messages, s:errors)
22643345546SBram Moolenaarendif
22743345546SBram Moolenaar
228dac1947bSBram Moolenaar" Add SKIPPED messages
229dac1947bSBram Moolenaarcall extend(s:messages, s:skipped)
230dac1947bSBram Moolenaar
231dac1947bSBram Moolenaar" Append messages to the file "messages"
232096c8bb4SBram Moolenaarsplit messages
233096c8bb4SBram Moolenaarcall append(line('$'), '')
23400af60bbSBram Moolenaarcall append(line('$'), 'From ' . g:testname . ':')
23500af60bbSBram Moolenaarcall append(line('$'), s:messages)
236096c8bb4SBram Moolenaarwrite
237096c8bb4SBram Moolenaar
23843345546SBram Moolenaarqall!
239cc28e2d0SBram Moolenaar
240cc28e2d0SBram Moolenaar" vim: shiftwidth=2 sts=2 expandtab
241