xref: /vim-8.2.3635/src/testdir/runtest.vim (revision de0ad40c)
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.
2443345546SBram Moolenaarif 0
2543345546SBram Moolenaar  quit!
2643345546SBram Moolenaarendif
2743345546SBram Moolenaar
2843345546SBram Moolenaar" Check that the screen size is at least 24 x 80 characters.
2943345546SBram Moolenaarif &lines < 24 || &columns < 80
3043345546SBram Moolenaar  let error = 'Screen size too small! Tests require at least 24 lines with 80 characters'
3143345546SBram Moolenaar  echoerr error
3243345546SBram Moolenaar  split test.log
3343345546SBram Moolenaar  $put =error
3443345546SBram Moolenaar  w
3543345546SBram Moolenaar  cquit
3643345546SBram Moolenaarendif
3743345546SBram Moolenaar
3843345546SBram Moolenaar" Source the test script.  First grab the file name, in case the script
3943345546SBram Moolenaar" navigates away.
4043345546SBram Moolenaarlet testname = expand('%')
4143345546SBram Moolenaarsource %
4243345546SBram Moolenaar
4343345546SBram Moolenaar" Locate Test_ functions and execute them.
4443345546SBram Moolenaarredir @q
4543345546SBram Moolenaarfunction /^Test_
4643345546SBram Moolenaarredir END
4743345546SBram Moolenaarlet tests = split(substitute(@q, 'function \(\k*()\)', '\1', 'g'))
4843345546SBram Moolenaar
4943345546SBram Moolenaarlet done = 0
5043345546SBram Moolenaarlet fail = 0
5143345546SBram Moolenaarlet errors = []
5243345546SBram Moolenaarfor test in tests
5343345546SBram Moolenaar  if exists("*SetUp")
5443345546SBram Moolenaar    call SetUp()
5543345546SBram Moolenaar  endif
5643345546SBram Moolenaar
5743345546SBram Moolenaar  let done += 1
5843345546SBram Moolenaar  try
5943345546SBram Moolenaar    exe 'call ' . test
6043345546SBram Moolenaar  catch
6143345546SBram Moolenaar    let fail += 1
6243345546SBram Moolenaar    call add(v:errors, 'Caught exception in ' . test . ': ' . v:exception . ' @ ' . v:throwpoint)
6343345546SBram Moolenaar  endtry
6443345546SBram Moolenaar
6543345546SBram Moolenaar  if len(v:errors) > 0
6643345546SBram Moolenaar    let fail += 1
6743345546SBram Moolenaar    call add(errors, 'Found errors in ' . test . ':')
6843345546SBram Moolenaar    call extend(errors, v:errors)
6943345546SBram Moolenaar    let v:errors = []
7043345546SBram Moolenaar  endif
7143345546SBram Moolenaar
7243345546SBram Moolenaar  if exists("*TearDown")
7343345546SBram Moolenaar    call TearDown()
7443345546SBram Moolenaar  endif
7543345546SBram Moolenaarendfor
7643345546SBram Moolenaar
7743345546SBram Moolenaarif fail == 0
7843345546SBram Moolenaar  " Success, create the .res file so that make knows it's done.
79*de0ad40cSBram Moolenaar  exe 'split ' . fnamemodify(testname, ':r') . '.res'
8043345546SBram Moolenaar  write
8143345546SBram Moolenaarendif
8243345546SBram Moolenaar
8343345546SBram Moolenaarif len(errors) > 0
8443345546SBram Moolenaar  " Append errors to test.log
8543345546SBram Moolenaar  split test.log
8643345546SBram Moolenaar  call append(line('$'), '')
8743345546SBram Moolenaar  call append(line('$'), 'From ' . testname . ':')
8843345546SBram Moolenaar  call append(line('$'), errors)
8943345546SBram Moolenaar  write
9043345546SBram Moolenaarendif
9143345546SBram Moolenaar
9243345546SBram Moolenaarecho 'Executed ' . done . (done > 1 ? ' tests': ' test')
9343345546SBram Moolenaarif fail > 0
9443345546SBram Moolenaar  echo fail . ' FAILED'
9543345546SBram Moolenaarendif
9643345546SBram Moolenaar
9743345546SBram Moolenaarqall!
98