xref: /vim-8.2.3635/src/testdir/runtest.vim (revision a99b9043)
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"
543345546SBram Moolenaar" The test script may contain anything, only functions that start with
643345546SBram Moolenaar" "Test_" are special.  These will be invoked and should contain assert
743345546SBram Moolenaar" functions.  See test_assert.vim for an example.
843345546SBram Moolenaar"
943345546SBram Moolenaar" It is possible to source other files that contain "Test_" functions.  This
1043345546SBram Moolenaar" can speed up testing, since Vim does not need to restart.  But be careful
1143345546SBram Moolenaar" that the tests do not interfere with each other.
1243345546SBram Moolenaar"
1343345546SBram Moolenaar" If an error cannot be detected properly with an assert function add the
1443345546SBram Moolenaar" error to the v:errors list:
1543345546SBram Moolenaar"   call add(v:errors, 'test foo failed: Cannot find xyz')
1643345546SBram Moolenaar"
1743345546SBram Moolenaar" If preparation for each Test_ function is needed, define a SetUp function.
1843345546SBram Moolenaar" It will be called before each Test_ function.
1943345546SBram Moolenaar"
2043345546SBram Moolenaar" If cleanup after each Test_ function is needed, define a TearDown function.
2143345546SBram Moolenaar" It will be called after each Test_ function.
2243345546SBram Moolenaar
2343345546SBram Moolenaar" Without the +eval feature we can't run these tests, bail out.
244686b323SBram Moolenaarso small.vim
2543345546SBram Moolenaar
2643345546SBram Moolenaar" Check that the screen size is at least 24 x 80 characters.
2743345546SBram Moolenaarif &lines < 24 || &columns < 80
2843345546SBram Moolenaar  let error = 'Screen size too small! Tests require at least 24 lines with 80 characters'
2943345546SBram Moolenaar  echoerr error
3043345546SBram Moolenaar  split test.log
3143345546SBram Moolenaar  $put =error
3243345546SBram Moolenaar  w
3343345546SBram Moolenaar  cquit
3443345546SBram Moolenaarendif
3543345546SBram Moolenaar
36c0662466SBram Moolenaar" For consistency run all tests with 'nocompatible' set.
37c0662466SBram Moolenaar" This also enables use of line continuation.
38c0662466SBram Moolenaarset nocp viminfo+=nviminfo
39c0662466SBram Moolenaar
40c0662466SBram Moolenaar" Avoid stopping at the "hit enter" prompt
41c0662466SBram Moolenaarset nomore
42c0662466SBram Moolenaar
43c0662466SBram Moolenaar" Output all messages in English.
44c0662466SBram Moolenaarlang mess C
45c0662466SBram Moolenaar
46f60b796fSBram Moolenaar" Always use forward slashes.
47f60b796fSBram Moolenaarset shellslash
48f60b796fSBram Moolenaar
4928fb79dbSBram Moolenaarlet s:srcdir = expand('%:p:h:h')
5028fb79dbSBram Moolenaar
5128fb79dbSBram Moolenaar" Support function: get the alloc ID by name.
5228fb79dbSBram Moolenaarfunction GetAllocId(name)
5328fb79dbSBram Moolenaar  exe 'split ' . s:srcdir . '/alloc.h'
54065ee9aeSBram Moolenaar  let top = search('typedef enum')
55065ee9aeSBram Moolenaar  if top == 0
56065ee9aeSBram Moolenaar    call add(v:errors, 'typedef not found in alloc.h')
57065ee9aeSBram Moolenaar  endif
5828fb79dbSBram Moolenaar  let lnum = search('aid_' . a:name . ',')
5928fb79dbSBram Moolenaar  if lnum == 0
6028fb79dbSBram Moolenaar    call add(v:errors, 'Alloc ID ' . a:name . ' not defined')
6128fb79dbSBram Moolenaar  endif
6228fb79dbSBram Moolenaar  close
63065ee9aeSBram Moolenaar  return lnum - top - 1
6428fb79dbSBram Moolenaarendfunc
6528fb79dbSBram Moolenaar
6628fb79dbSBram Moolenaar
6743345546SBram Moolenaar" Source the test script.  First grab the file name, in case the script
6843345546SBram Moolenaar" navigates away.
6943345546SBram Moolenaarlet testname = expand('%')
704686b323SBram Moolenaarlet done = 0
714686b323SBram Moolenaarlet fail = 0
724686b323SBram Moolenaarlet errors = []
73096c8bb4SBram Moolenaarlet messages = []
74a2cce863SBram Moolenaarif expand('%') =~ 'test_viml.vim'
75a2cce863SBram Moolenaar  " this test has intentional errors, don't use try/catch.
76a2cce863SBram Moolenaar  source %
77a2cce863SBram Moolenaarelse
784686b323SBram Moolenaar  try
7943345546SBram Moolenaar    source %
804686b323SBram Moolenaar  catch
814686b323SBram Moolenaar    let fail += 1
824686b323SBram Moolenaar    call add(errors, 'Caught exception: ' . v:exception . ' @ ' . v:throwpoint)
834686b323SBram Moolenaar  endtry
84a2cce863SBram Moolenaarendif
8543345546SBram Moolenaar
8643345546SBram Moolenaar" Locate Test_ functions and execute them.
87*a99b9043SBram Moolenaarset nomore
8843345546SBram Moolenaarredir @q
8943345546SBram Moolenaarfunction /^Test_
9043345546SBram Moolenaarredir END
9143345546SBram Moolenaarlet tests = split(substitute(@q, 'function \(\k*()\)', '\1', 'g'))
9243345546SBram Moolenaar
93cfc0a350SBram Moolenaar" Execute the tests in alphabetical order.
94cfc0a350SBram Moolenaar for test in sort(tests)
9543345546SBram Moolenaar  if exists("*SetUp")
9643345546SBram Moolenaar    call SetUp()
9743345546SBram Moolenaar  endif
9843345546SBram Moolenaar
99096c8bb4SBram Moolenaar  call add(messages, 'Executing ' . test)
10043345546SBram Moolenaar  let done += 1
10143345546SBram Moolenaar  try
10243345546SBram Moolenaar    exe 'call ' . test
10343345546SBram Moolenaar  catch
10443345546SBram Moolenaar    let fail += 1
10543345546SBram Moolenaar    call add(v:errors, 'Caught exception in ' . test . ': ' . v:exception . ' @ ' . v:throwpoint)
10643345546SBram Moolenaar  endtry
10743345546SBram Moolenaar
10843345546SBram Moolenaar  if len(v:errors) > 0
10943345546SBram Moolenaar    let fail += 1
11043345546SBram Moolenaar    call add(errors, 'Found errors in ' . test . ':')
11143345546SBram Moolenaar    call extend(errors, v:errors)
11243345546SBram Moolenaar    let v:errors = []
11343345546SBram Moolenaar  endif
11443345546SBram Moolenaar
11543345546SBram Moolenaar  if exists("*TearDown")
11643345546SBram Moolenaar    call TearDown()
11743345546SBram Moolenaar  endif
11843345546SBram Moolenaarendfor
11943345546SBram Moolenaar
12043345546SBram Moolenaarif fail == 0
12143345546SBram Moolenaar  " Success, create the .res file so that make knows it's done.
122de0ad40cSBram Moolenaar  exe 'split ' . fnamemodify(testname, ':r') . '.res'
12343345546SBram Moolenaar  write
12443345546SBram Moolenaarendif
12543345546SBram Moolenaar
12643345546SBram Moolenaarif len(errors) > 0
12743345546SBram Moolenaar  " Append errors to test.log
12843345546SBram Moolenaar  split test.log
12943345546SBram Moolenaar  call append(line('$'), '')
13043345546SBram Moolenaar  call append(line('$'), 'From ' . testname . ':')
13143345546SBram Moolenaar  call append(line('$'), errors)
13243345546SBram Moolenaar  write
13343345546SBram Moolenaarendif
13443345546SBram Moolenaar
135096c8bb4SBram Moolenaarlet message = 'Executed ' . done . (done > 1 ? ' tests': ' test')
136096c8bb4SBram Moolenaarecho message
137096c8bb4SBram Moolenaarcall add(messages, message)
13843345546SBram Moolenaarif fail > 0
139096c8bb4SBram Moolenaar  let message = fail . ' FAILED'
140096c8bb4SBram Moolenaar  echo message
141096c8bb4SBram Moolenaar  call add(messages, message)
14243345546SBram Moolenaarendif
14343345546SBram Moolenaar
144096c8bb4SBram Moolenaar" Append messages to "messages"
145096c8bb4SBram Moolenaarsplit messages
146096c8bb4SBram Moolenaarcall append(line('$'), '')
147096c8bb4SBram Moolenaarcall append(line('$'), 'From ' . testname . ':')
148096c8bb4SBram Moolenaarcall append(line('$'), messages)
149096c8bb4SBram Moolenaarwrite
150096c8bb4SBram Moolenaar
15143345546SBram Moolenaarqall!
152