xref: /vim-8.2.3635/src/testdir/runtest.vim (revision 43345546)
1*43345546SBram Moolenaar" This script is sourced while editing the .vim file with the tests.
2*43345546SBram Moolenaar" When the script is successful the .res file will be created.
3*43345546SBram Moolenaar" Errors are appended to the test.log file.
4*43345546SBram Moolenaar"
5*43345546SBram Moolenaar" The test script may contain anything, only functions that start with
6*43345546SBram Moolenaar" "Test_" are special.  These will be invoked and should contain assert
7*43345546SBram Moolenaar" functions.  See test_assert.vim for an example.
8*43345546SBram Moolenaar"
9*43345546SBram Moolenaar" It is possible to source other files that contain "Test_" functions.  This
10*43345546SBram Moolenaar" can speed up testing, since Vim does not need to restart.  But be careful
11*43345546SBram Moolenaar" that the tests do not interfere with each other.
12*43345546SBram Moolenaar"
13*43345546SBram Moolenaar" If an error cannot be detected properly with an assert function add the
14*43345546SBram Moolenaar" error to the v:errors list:
15*43345546SBram Moolenaar"   call add(v:errors, 'test foo failed: Cannot find xyz')
16*43345546SBram Moolenaar"
17*43345546SBram Moolenaar" If preparation for each Test_ function is needed, define a SetUp function.
18*43345546SBram Moolenaar" It will be called before each Test_ function.
19*43345546SBram Moolenaar"
20*43345546SBram Moolenaar" If cleanup after each Test_ function is needed, define a TearDown function.
21*43345546SBram Moolenaar" It will be called after each Test_ function.
22*43345546SBram Moolenaar
23*43345546SBram Moolenaar" Without the +eval feature we can't run these tests, bail out.
24*43345546SBram Moolenaarif 0
25*43345546SBram Moolenaar  quit!
26*43345546SBram Moolenaarendif
27*43345546SBram Moolenaar
28*43345546SBram Moolenaar" Check that the screen size is at least 24 x 80 characters.
29*43345546SBram Moolenaarif &lines < 24 || &columns < 80
30*43345546SBram Moolenaar  let error = 'Screen size too small! Tests require at least 24 lines with 80 characters'
31*43345546SBram Moolenaar  echoerr error
32*43345546SBram Moolenaar  split test.log
33*43345546SBram Moolenaar  $put =error
34*43345546SBram Moolenaar  w
35*43345546SBram Moolenaar  cquit
36*43345546SBram Moolenaarendif
37*43345546SBram Moolenaar
38*43345546SBram Moolenaar" Source the test script.  First grab the file name, in case the script
39*43345546SBram Moolenaar" navigates away.
40*43345546SBram Moolenaarlet testname = expand('%')
41*43345546SBram Moolenaarsource %
42*43345546SBram Moolenaar
43*43345546SBram Moolenaar" Locate Test_ functions and execute them.
44*43345546SBram Moolenaarredir @q
45*43345546SBram Moolenaarfunction /^Test_
46*43345546SBram Moolenaarredir END
47*43345546SBram Moolenaarlet tests = split(substitute(@q, 'function \(\k*()\)', '\1', 'g'))
48*43345546SBram Moolenaar
49*43345546SBram Moolenaarlet done = 0
50*43345546SBram Moolenaarlet fail = 0
51*43345546SBram Moolenaarlet errors = []
52*43345546SBram Moolenaarfor test in tests
53*43345546SBram Moolenaar  if exists("*SetUp")
54*43345546SBram Moolenaar    call SetUp()
55*43345546SBram Moolenaar  endif
56*43345546SBram Moolenaar
57*43345546SBram Moolenaar  let done += 1
58*43345546SBram Moolenaar  try
59*43345546SBram Moolenaar    exe 'call ' . test
60*43345546SBram Moolenaar  catch
61*43345546SBram Moolenaar    let fail += 1
62*43345546SBram Moolenaar    call add(v:errors, 'Caught exception in ' . test . ': ' . v:exception . ' @ ' . v:throwpoint)
63*43345546SBram Moolenaar  endtry
64*43345546SBram Moolenaar
65*43345546SBram Moolenaar  if len(v:errors) > 0
66*43345546SBram Moolenaar    let fail += 1
67*43345546SBram Moolenaar    call add(errors, 'Found errors in ' . test . ':')
68*43345546SBram Moolenaar    call extend(errors, v:errors)
69*43345546SBram Moolenaar    let v:errors = []
70*43345546SBram Moolenaar  endif
71*43345546SBram Moolenaar
72*43345546SBram Moolenaar  if exists("*TearDown")
73*43345546SBram Moolenaar    call TearDown()
74*43345546SBram Moolenaar  endif
75*43345546SBram Moolenaarendfor
76*43345546SBram Moolenaar
77*43345546SBram Moolenaarif fail == 0
78*43345546SBram Moolenaar  " Success, create the .res file so that make knows it's done.
79*43345546SBram Moolenaar  split %:r.res
80*43345546SBram Moolenaar  write
81*43345546SBram Moolenaarendif
82*43345546SBram Moolenaar
83*43345546SBram Moolenaarif len(errors) > 0
84*43345546SBram Moolenaar  " Append errors to test.log
85*43345546SBram Moolenaar  split test.log
86*43345546SBram Moolenaar  call append(line('$'), '')
87*43345546SBram Moolenaar  call append(line('$'), 'From ' . testname . ':')
88*43345546SBram Moolenaar  call append(line('$'), errors)
89*43345546SBram Moolenaar  write
90*43345546SBram Moolenaarendif
91*43345546SBram Moolenaar
92*43345546SBram Moolenaarecho 'Executed ' . done . (done > 1 ? ' tests': ' test')
93*43345546SBram Moolenaarif fail > 0
94*43345546SBram Moolenaar  echo fail . ' FAILED'
95*43345546SBram Moolenaarendif
96*43345546SBram Moolenaar
97*43345546SBram Moolenaarqall!
98