xref: /vim-8.2.3635/src/testdir/runtest.vim (revision e9c07270)
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
45c0662466SBram Moolenaar" For consistency run all tests with 'nocompatible' set.
46c0662466SBram Moolenaar" This also enables use of line continuation.
47c0662466SBram Moolenaarset nocp viminfo+=nviminfo
48c0662466SBram Moolenaar
49c0662466SBram Moolenaar" Avoid stopping at the "hit enter" prompt
50c0662466SBram Moolenaarset nomore
51c0662466SBram Moolenaar
52c0662466SBram Moolenaar" Output all messages in English.
53c0662466SBram Moolenaarlang mess C
54c0662466SBram Moolenaar
55f60b796fSBram Moolenaar" Always use forward slashes.
56f60b796fSBram Moolenaarset shellslash
57f60b796fSBram Moolenaar
58*e9c07270SBram Moolenaar" Make sure $HOME does not get read or written.
59*e9c07270SBram Moolenaarlet $HOME = '/does/not/exist'
60*e9c07270SBram Moolenaar
6128fb79dbSBram Moolenaarlet s:srcdir = expand('%:p:h:h')
6228fb79dbSBram Moolenaar
6328fb79dbSBram Moolenaar" Support function: get the alloc ID by name.
6428fb79dbSBram Moolenaarfunction GetAllocId(name)
6528fb79dbSBram Moolenaar  exe 'split ' . s:srcdir . '/alloc.h'
66065ee9aeSBram Moolenaar  let top = search('typedef enum')
67065ee9aeSBram Moolenaar  if top == 0
68065ee9aeSBram Moolenaar    call add(v:errors, 'typedef not found in alloc.h')
69065ee9aeSBram Moolenaar  endif
7028fb79dbSBram Moolenaar  let lnum = search('aid_' . a:name . ',')
7128fb79dbSBram Moolenaar  if lnum == 0
7228fb79dbSBram Moolenaar    call add(v:errors, 'Alloc ID ' . a:name . ' not defined')
7328fb79dbSBram Moolenaar  endif
7428fb79dbSBram Moolenaar  close
75065ee9aeSBram Moolenaar  return lnum - top - 1
7628fb79dbSBram Moolenaarendfunc
7728fb79dbSBram Moolenaar
78b5760a1cSBram Moolenaarfunction RunTheTest(test)
79b5760a1cSBram Moolenaar  echo 'Executing ' . a:test
80b5760a1cSBram Moolenaar  if exists("*SetUp")
81b5760a1cSBram Moolenaar    call SetUp()
82b5760a1cSBram Moolenaar  endif
83b5760a1cSBram Moolenaar
84b5760a1cSBram Moolenaar  call add(s:messages, 'Executing ' . a:test)
85b5760a1cSBram Moolenaar  let s:done += 1
86b5760a1cSBram Moolenaar  try
87b5760a1cSBram Moolenaar    exe 'call ' . a:test
88b5760a1cSBram Moolenaar  catch
89b5760a1cSBram Moolenaar    call add(v:errors, 'Caught exception in ' . a:test . ': ' . v:exception . ' @ ' . v:throwpoint)
90b5760a1cSBram Moolenaar  endtry
91b5760a1cSBram Moolenaar
92b5760a1cSBram Moolenaar  if exists("*TearDown")
93b5760a1cSBram Moolenaar    call TearDown()
94b5760a1cSBram Moolenaar  endif
95b5760a1cSBram Moolenaarendfunc
9628fb79dbSBram Moolenaar
9743345546SBram Moolenaar" Source the test script.  First grab the file name, in case the script
9800af60bbSBram Moolenaar" navigates away.  g:testname can be used by the tests.
9900af60bbSBram Moolenaarlet g:testname = expand('%')
10000af60bbSBram Moolenaarlet s:done = 0
10100af60bbSBram Moolenaarlet s:fail = 0
10200af60bbSBram Moolenaarlet s:errors = []
10300af60bbSBram Moolenaarlet s:messages = []
104a2cce863SBram Moolenaarif expand('%') =~ 'test_viml.vim'
10500af60bbSBram Moolenaar  " this test has intentional s:errors, don't use try/catch.
106a2cce863SBram Moolenaar  source %
107a2cce863SBram Moolenaarelse
1084686b323SBram Moolenaar  try
10943345546SBram Moolenaar    source %
1104686b323SBram Moolenaar  catch
11100af60bbSBram Moolenaar    let s:fail += 1
11200af60bbSBram Moolenaar    call add(s:errors, 'Caught exception: ' . v:exception . ' @ ' . v:throwpoint)
1134686b323SBram Moolenaar  endtry
114a2cce863SBram Moolenaarendif
11543345546SBram Moolenaar
116b5760a1cSBram Moolenaar" Names of flaky tests.
117b5760a1cSBram Moolenaarlet s:flaky = ['Test_reltime()']
118b5760a1cSBram Moolenaar
11943345546SBram Moolenaar" Locate Test_ functions and execute them.
120a99b9043SBram Moolenaarset nomore
12143345546SBram Moolenaarredir @q
12293bf558cSBram Moolenaarsilent function /^Test_
12343345546SBram Moolenaarredir END
12400af60bbSBram Moolenaarlet s:tests = split(substitute(@q, 'function \(\k*()\)', '\1', 'g'))
12543345546SBram Moolenaar
126befb366aSBram Moolenaar" If there is an extra argument filter the function names against it.
127befb366aSBram Moolenaarif argc() > 1
128befb366aSBram Moolenaar  let s:tests = filter(s:tests, 'v:val =~ argv(1)')
129befb366aSBram Moolenaarendif
130befb366aSBram Moolenaar
131cfc0a350SBram Moolenaar" Execute the tests in alphabetical order.
13200af60bbSBram Moolenaarfor s:test in sort(s:tests)
133b5760a1cSBram Moolenaar  call RunTheTest(s:test)
13443345546SBram Moolenaar
135b5760a1cSBram Moolenaar  if len(v:errors) > 0 && index(s:flaky, s:test) >= 0
136b5760a1cSBram Moolenaar    call add(s:messages, 'Flaky test failed, running it again')
137b5760a1cSBram Moolenaar    let v:errors = []
138b5760a1cSBram Moolenaar    call RunTheTest(s:test)
139b5760a1cSBram Moolenaar  endif
14043345546SBram Moolenaar
14143345546SBram Moolenaar  if len(v:errors) > 0
14200af60bbSBram Moolenaar    let s:fail += 1
14300af60bbSBram Moolenaar    call add(s:errors, 'Found errors in ' . s:test . ':')
14400af60bbSBram Moolenaar    call extend(s:errors, v:errors)
14543345546SBram Moolenaar    let v:errors = []
14643345546SBram Moolenaar  endif
14743345546SBram Moolenaarendfor
14843345546SBram Moolenaar
14900af60bbSBram Moolenaarif s:fail == 0
15043345546SBram Moolenaar  " Success, create the .res file so that make knows it's done.
15100af60bbSBram Moolenaar  exe 'split ' . fnamemodify(g:testname, ':r') . '.res'
15243345546SBram Moolenaar  write
15343345546SBram Moolenaarendif
15443345546SBram Moolenaar
15500af60bbSBram Moolenaarif len(s:errors) > 0
15643345546SBram Moolenaar  " Append errors to test.log
15743345546SBram Moolenaar  split test.log
15843345546SBram Moolenaar  call append(line('$'), '')
15900af60bbSBram Moolenaar  call append(line('$'), 'From ' . g:testname . ':')
16000af60bbSBram Moolenaar  call append(line('$'), s:errors)
16143345546SBram Moolenaar  write
16243345546SBram Moolenaarendif
16343345546SBram Moolenaar
16400af60bbSBram Moolenaarlet message = 'Executed ' . s:done . (s:done > 1 ? ' tests' : ' test')
165096c8bb4SBram Moolenaarecho message
16600af60bbSBram Moolenaarcall add(s:messages, message)
16700af60bbSBram Moolenaarif s:fail > 0
16800af60bbSBram Moolenaar  let message = s:fail . ' FAILED:'
169096c8bb4SBram Moolenaar  echo message
17000af60bbSBram Moolenaar  call add(s:messages, message)
17100af60bbSBram Moolenaar  call extend(s:messages, s:errors)
17243345546SBram Moolenaarendif
17343345546SBram Moolenaar
174096c8bb4SBram Moolenaar" Append messages to "messages"
175096c8bb4SBram Moolenaarsplit messages
176096c8bb4SBram Moolenaarcall append(line('$'), '')
17700af60bbSBram Moolenaarcall append(line('$'), 'From ' . g:testname . ':')
17800af60bbSBram Moolenaarcall append(line('$'), s:messages)
179096c8bb4SBram Moolenaarwrite
180096c8bb4SBram Moolenaar
18143345546SBram Moolenaarqall!
182