xref: /vim-8.2.3635/src/testdir/runtest.vim (revision 00af60bb)
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.
22*00af60bbSBram Moolenaar"
23*00af60bbSBram Moolenaar" When debugging a test it can be useful to add messages to v:errors:
24*00af60bbSBram Moolenaar" 	call add(v:errors, "this happened")
25*00af60bbSBram Moolenaar
2643345546SBram Moolenaar
2743345546SBram Moolenaar" Without the +eval feature we can't run these tests, bail out.
284686b323SBram Moolenaarso small.vim
2943345546SBram Moolenaar
3043345546SBram Moolenaar" Check that the screen size is at least 24 x 80 characters.
3143345546SBram Moolenaarif &lines < 24 || &columns < 80
3243345546SBram Moolenaar  let error = 'Screen size too small! Tests require at least 24 lines with 80 characters'
3343345546SBram Moolenaar  echoerr error
3443345546SBram Moolenaar  split test.log
3543345546SBram Moolenaar  $put =error
3643345546SBram Moolenaar  w
3743345546SBram Moolenaar  cquit
3843345546SBram Moolenaarendif
3943345546SBram Moolenaar
40c0662466SBram Moolenaar" For consistency run all tests with 'nocompatible' set.
41c0662466SBram Moolenaar" This also enables use of line continuation.
42c0662466SBram Moolenaarset nocp viminfo+=nviminfo
43c0662466SBram Moolenaar
44c0662466SBram Moolenaar" Avoid stopping at the "hit enter" prompt
45c0662466SBram Moolenaarset nomore
46c0662466SBram Moolenaar
47c0662466SBram Moolenaar" Output all messages in English.
48c0662466SBram Moolenaarlang mess C
49c0662466SBram Moolenaar
50f60b796fSBram Moolenaar" Always use forward slashes.
51f60b796fSBram Moolenaarset shellslash
52f60b796fSBram Moolenaar
5328fb79dbSBram Moolenaarlet s:srcdir = expand('%:p:h:h')
5428fb79dbSBram Moolenaar
5528fb79dbSBram Moolenaar" Support function: get the alloc ID by name.
5628fb79dbSBram Moolenaarfunction GetAllocId(name)
5728fb79dbSBram Moolenaar  exe 'split ' . s:srcdir . '/alloc.h'
58065ee9aeSBram Moolenaar  let top = search('typedef enum')
59065ee9aeSBram Moolenaar  if top == 0
60065ee9aeSBram Moolenaar    call add(v:errors, 'typedef not found in alloc.h')
61065ee9aeSBram Moolenaar  endif
6228fb79dbSBram Moolenaar  let lnum = search('aid_' . a:name . ',')
6328fb79dbSBram Moolenaar  if lnum == 0
6428fb79dbSBram Moolenaar    call add(v:errors, 'Alloc ID ' . a:name . ' not defined')
6528fb79dbSBram Moolenaar  endif
6628fb79dbSBram Moolenaar  close
67065ee9aeSBram Moolenaar  return lnum - top - 1
6828fb79dbSBram Moolenaarendfunc
6928fb79dbSBram Moolenaar
7028fb79dbSBram Moolenaar
7143345546SBram Moolenaar" Source the test script.  First grab the file name, in case the script
72*00af60bbSBram Moolenaar" navigates away.  g:testname can be used by the tests.
73*00af60bbSBram Moolenaarlet g:testname = expand('%')
74*00af60bbSBram Moolenaarlet s:done = 0
75*00af60bbSBram Moolenaarlet s:fail = 0
76*00af60bbSBram Moolenaarlet s:errors = []
77*00af60bbSBram Moolenaarlet s:messages = []
78a2cce863SBram Moolenaarif expand('%') =~ 'test_viml.vim'
79*00af60bbSBram Moolenaar  " this test has intentional s:errors, don't use try/catch.
80a2cce863SBram Moolenaar  source %
81a2cce863SBram Moolenaarelse
824686b323SBram Moolenaar  try
8343345546SBram Moolenaar    source %
844686b323SBram Moolenaar  catch
85*00af60bbSBram Moolenaar    let s:fail += 1
86*00af60bbSBram Moolenaar    call add(s:errors, 'Caught exception: ' . v:exception . ' @ ' . v:throwpoint)
874686b323SBram Moolenaar  endtry
88a2cce863SBram Moolenaarendif
8943345546SBram Moolenaar
9043345546SBram Moolenaar" Locate Test_ functions and execute them.
91a99b9043SBram Moolenaarset nomore
9243345546SBram Moolenaarredir @q
9343345546SBram Moolenaarfunction /^Test_
9443345546SBram Moolenaarredir END
95*00af60bbSBram Moolenaarlet s:tests = split(substitute(@q, 'function \(\k*()\)', '\1', 'g'))
9643345546SBram Moolenaar
97cfc0a350SBram Moolenaar" Execute the tests in alphabetical order.
98*00af60bbSBram Moolenaar for s:test in sort(s:tests)
9943345546SBram Moolenaar  if exists("*SetUp")
10043345546SBram Moolenaar    call SetUp()
10143345546SBram Moolenaar  endif
10243345546SBram Moolenaar
103*00af60bbSBram Moolenaar  call add(s:messages, 'Executing ' . s:test)
104*00af60bbSBram Moolenaar  let s:done += 1
10543345546SBram Moolenaar  try
106*00af60bbSBram Moolenaar    exe 'call ' . s:test
10743345546SBram Moolenaar  catch
108*00af60bbSBram Moolenaar    call add(v:errors, 'Caught exception in ' . s:test . ': ' . v:exception . ' @ ' . v:throwpoint)
10943345546SBram Moolenaar  endtry
11043345546SBram Moolenaar
11143345546SBram Moolenaar  if len(v:errors) > 0
112*00af60bbSBram Moolenaar    let s:fail += 1
113*00af60bbSBram Moolenaar    call add(s:errors, 'Found errors in ' . s:test . ':')
114*00af60bbSBram Moolenaar    call extend(s:errors, v:errors)
11543345546SBram Moolenaar    let v:errors = []
11643345546SBram Moolenaar  endif
11743345546SBram Moolenaar
11843345546SBram Moolenaar  if exists("*TearDown")
11943345546SBram Moolenaar    call TearDown()
12043345546SBram Moolenaar  endif
12143345546SBram Moolenaarendfor
12243345546SBram Moolenaar
123*00af60bbSBram Moolenaarif s:fail == 0
12443345546SBram Moolenaar  " Success, create the .res file so that make knows it's done.
125*00af60bbSBram Moolenaar  exe 'split ' . fnamemodify(g:testname, ':r') . '.res'
12643345546SBram Moolenaar  write
12743345546SBram Moolenaarendif
12843345546SBram Moolenaar
129*00af60bbSBram Moolenaarif len(s:errors) > 0
13043345546SBram Moolenaar  " Append errors to test.log
13143345546SBram Moolenaar  split test.log
13243345546SBram Moolenaar  call append(line('$'), '')
133*00af60bbSBram Moolenaar  call append(line('$'), 'From ' . g:testname . ':')
134*00af60bbSBram Moolenaar  call append(line('$'), s:errors)
13543345546SBram Moolenaar  write
13643345546SBram Moolenaarendif
13743345546SBram Moolenaar
138*00af60bbSBram Moolenaarlet message = 'Executed ' . s:done . (s:done > 1 ? ' tests' : ' test')
139096c8bb4SBram Moolenaarecho message
140*00af60bbSBram Moolenaarcall add(s:messages, message)
141*00af60bbSBram Moolenaarif s:fail > 0
142*00af60bbSBram Moolenaar  let message = s:fail . ' FAILED:'
143096c8bb4SBram Moolenaar  echo message
144*00af60bbSBram Moolenaar  call add(s:messages, message)
145*00af60bbSBram Moolenaar  call extend(s:messages, s:errors)
14643345546SBram Moolenaarendif
14743345546SBram Moolenaar
148096c8bb4SBram Moolenaar" Append messages to "messages"
149096c8bb4SBram Moolenaarsplit messages
150096c8bb4SBram Moolenaarcall append(line('$'), '')
151*00af60bbSBram Moolenaarcall append(line('$'), 'From ' . g:testname . ':')
152*00af60bbSBram Moolenaarcall append(line('$'), s:messages)
153096c8bb4SBram Moolenaarwrite
154096c8bb4SBram Moolenaar
15543345546SBram Moolenaarqall!
156