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