xref: /vim-8.2.3635/src/testdir/runtest.vim (revision dac1947b)
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" Avoid stopping at the "hit enter" prompt
61c0662466SBram Moolenaarset nomore
62c0662466SBram 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
89b5760a1cSBram Moolenaarfunction RunTheTest(test)
90b5760a1cSBram Moolenaar  echo 'Executing ' . a:test
91b5760a1cSBram Moolenaar  if exists("*SetUp")
92b5760a1cSBram Moolenaar    call SetUp()
93b5760a1cSBram Moolenaar  endif
94b5760a1cSBram Moolenaar
95b5760a1cSBram Moolenaar  call add(s:messages, 'Executing ' . a:test)
96b5760a1cSBram Moolenaar  let s:done += 1
97b5760a1cSBram Moolenaar  try
98b5760a1cSBram Moolenaar    exe 'call ' . a:test
99*dac1947bSBram Moolenaar  catch /^\cskipped/
100*dac1947bSBram Moolenaar    call add(s:messages, '    Skipped')
101*dac1947bSBram Moolenaar    call add(s:skipped, 'SKIPPED ' . a:test . ': ' . substitute(v:exception, '^\S*\s\+', '',  ''))
102b5760a1cSBram Moolenaar  catch
103b5760a1cSBram Moolenaar    call add(v:errors, 'Caught exception in ' . a:test . ': ' . v:exception . ' @ ' . v:throwpoint)
104b5760a1cSBram Moolenaar  endtry
105b5760a1cSBram Moolenaar
106b5760a1cSBram Moolenaar  if exists("*TearDown")
107b5760a1cSBram Moolenaar    call TearDown()
108b5760a1cSBram Moolenaar  endif
1097cba71d7SBram Moolenaar
1107cba71d7SBram Moolenaar  " Close any extra windows and make the current one not modified.
111358308ddSBram Moolenaar  while 1
112358308ddSBram Moolenaar    let wincount = winnr('$')
113358308ddSBram Moolenaar    if wincount == 1
114358308ddSBram Moolenaar      break
115358308ddSBram Moolenaar    endif
1167cba71d7SBram Moolenaar    bwipe!
117358308ddSBram Moolenaar    if wincount == winnr('$')
118358308ddSBram Moolenaar      " Did not manage to close a window.
119358308ddSBram Moolenaar      only!
120358308ddSBram Moolenaar      break
121358308ddSBram Moolenaar    endif
1227cba71d7SBram Moolenaar  endwhile
1237cba71d7SBram Moolenaar  set nomodified
124b5760a1cSBram Moolenaarendfunc
12528fb79dbSBram Moolenaar
12643345546SBram Moolenaar" Source the test script.  First grab the file name, in case the script
12700af60bbSBram Moolenaar" navigates away.  g:testname can be used by the tests.
12800af60bbSBram Moolenaarlet g:testname = expand('%')
12900af60bbSBram Moolenaarlet s:done = 0
13000af60bbSBram Moolenaarlet s:fail = 0
13100af60bbSBram Moolenaarlet s:errors = []
13200af60bbSBram Moolenaarlet s:messages = []
133*dac1947bSBram Moolenaarlet s:skipped = []
134a2cce863SBram Moolenaarif expand('%') =~ 'test_viml.vim'
13500af60bbSBram Moolenaar  " this test has intentional s:errors, don't use try/catch.
136a2cce863SBram Moolenaar  source %
137a2cce863SBram Moolenaarelse
1384686b323SBram Moolenaar  try
13943345546SBram Moolenaar    source %
1404686b323SBram Moolenaar  catch
14100af60bbSBram Moolenaar    let s:fail += 1
14200af60bbSBram Moolenaar    call add(s:errors, 'Caught exception: ' . v:exception . ' @ ' . v:throwpoint)
1434686b323SBram Moolenaar  endtry
144a2cce863SBram Moolenaarendif
14543345546SBram Moolenaar
146b5760a1cSBram Moolenaar" Names of flaky tests.
147fe948923SBram Moolenaarlet s:flaky = ['Test_reltime()', 'Test_nb_basic()', 'Test_communicate()']
148b5760a1cSBram Moolenaar
14943345546SBram Moolenaar" Locate Test_ functions and execute them.
150a99b9043SBram Moolenaarset nomore
15143345546SBram Moolenaarredir @q
15293bf558cSBram Moolenaarsilent function /^Test_
15343345546SBram Moolenaarredir END
15400af60bbSBram Moolenaarlet s:tests = split(substitute(@q, 'function \(\k*()\)', '\1', 'g'))
15543345546SBram Moolenaar
156befb366aSBram Moolenaar" If there is an extra argument filter the function names against it.
157befb366aSBram Moolenaarif argc() > 1
158befb366aSBram Moolenaar  let s:tests = filter(s:tests, 'v:val =~ argv(1)')
159befb366aSBram Moolenaarendif
160befb366aSBram Moolenaar
161cfc0a350SBram Moolenaar" Execute the tests in alphabetical order.
16200af60bbSBram Moolenaarfor s:test in sort(s:tests)
163b5760a1cSBram Moolenaar  call RunTheTest(s:test)
16443345546SBram Moolenaar
165b5760a1cSBram Moolenaar  if len(v:errors) > 0 && index(s:flaky, s:test) >= 0
166b5760a1cSBram Moolenaar    call add(s:messages, 'Flaky test failed, running it again')
167b5760a1cSBram Moolenaar    let v:errors = []
168b5760a1cSBram Moolenaar    call RunTheTest(s:test)
169b5760a1cSBram Moolenaar  endif
17043345546SBram Moolenaar
17143345546SBram Moolenaar  if len(v:errors) > 0
17200af60bbSBram Moolenaar    let s:fail += 1
17300af60bbSBram Moolenaar    call add(s:errors, 'Found errors in ' . s:test . ':')
17400af60bbSBram Moolenaar    call extend(s:errors, v:errors)
17543345546SBram Moolenaar    let v:errors = []
17643345546SBram Moolenaar  endif
17743345546SBram Moolenaarendfor
17843345546SBram Moolenaar
179fc4ad616SBram Moolenaar" Don't write viminfo on exit.
180fc4ad616SBram Moolenaarset viminfo=
181fc4ad616SBram Moolenaar
18200af60bbSBram Moolenaarif s:fail == 0
18343345546SBram Moolenaar  " Success, create the .res file so that make knows it's done.
18400af60bbSBram Moolenaar  exe 'split ' . fnamemodify(g:testname, ':r') . '.res'
18543345546SBram Moolenaar  write
18643345546SBram Moolenaarendif
18743345546SBram Moolenaar
18800af60bbSBram Moolenaarif len(s:errors) > 0
18943345546SBram Moolenaar  " Append errors to test.log
19043345546SBram Moolenaar  split test.log
19143345546SBram Moolenaar  call append(line('$'), '')
19200af60bbSBram Moolenaar  call append(line('$'), 'From ' . g:testname . ':')
19300af60bbSBram Moolenaar  call append(line('$'), s:errors)
19443345546SBram Moolenaar  write
19543345546SBram Moolenaarendif
19643345546SBram Moolenaar
19700af60bbSBram Moolenaarlet message = 'Executed ' . s:done . (s:done > 1 ? ' tests' : ' test')
198096c8bb4SBram Moolenaarecho message
19900af60bbSBram Moolenaarcall add(s:messages, message)
20000af60bbSBram Moolenaarif s:fail > 0
20100af60bbSBram Moolenaar  let message = s:fail . ' FAILED:'
202096c8bb4SBram Moolenaar  echo message
20300af60bbSBram Moolenaar  call add(s:messages, message)
20400af60bbSBram Moolenaar  call extend(s:messages, s:errors)
20543345546SBram Moolenaarendif
20643345546SBram Moolenaar
207*dac1947bSBram Moolenaar" Add SKIPPED messages
208*dac1947bSBram Moolenaarcall extend(s:messages, s:skipped)
209*dac1947bSBram Moolenaar
210*dac1947bSBram Moolenaar" Append messages to the file "messages"
211096c8bb4SBram Moolenaarsplit messages
212096c8bb4SBram Moolenaarcall append(line('$'), '')
21300af60bbSBram Moolenaarcall append(line('$'), 'From ' . g:testname . ':')
21400af60bbSBram Moolenaarcall append(line('$'), s:messages)
215096c8bb4SBram Moolenaarwrite
216096c8bb4SBram Moolenaar
21743345546SBram Moolenaarqall!
218