blob: d1593825a4d9f6b4b366e7a145e7bd9753627d43 [file] [log] [blame]
Bram Moolenaar43345542015-11-29 17:35:35 +01001" 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"
Bram Moolenaarbefb3662016-02-20 14:41:40 +01005" To execute only specific test functions, add a second argument. It will be
6" matched against the names of the Test_ funtion. E.g.:
7" ../vim -u NONE -S runtest.vim test_channel.vim open_delay
8" The output can be found in the "messages" file.
9"
Bram Moolenaar43345542015-11-29 17:35:35 +010010" The test script may contain anything, only functions that start with
11" "Test_" are special. These will be invoked and should contain assert
12" functions. See test_assert.vim for an example.
13"
14" It is possible to source other files that contain "Test_" functions. This
15" can speed up testing, since Vim does not need to restart. But be careful
16" that the tests do not interfere with each other.
17"
18" If an error cannot be detected properly with an assert function add the
19" error to the v:errors list:
20" call add(v:errors, 'test foo failed: Cannot find xyz')
21"
22" If preparation for each Test_ function is needed, define a SetUp function.
23" It will be called before each Test_ function.
24"
25" If cleanup after each Test_ function is needed, define a TearDown function.
26" It will be called after each Test_ function.
Bram Moolenaar00af60b2016-02-13 14:06:14 +010027"
28" When debugging a test it can be useful to add messages to v:errors:
29" call add(v:errors, "this happened")
30
Bram Moolenaar43345542015-11-29 17:35:35 +010031
32" Without the +eval feature we can't run these tests, bail out.
Bram Moolenaar4686b322015-12-28 14:44:10 +010033so small.vim
Bram Moolenaar43345542015-11-29 17:35:35 +010034
35" Check that the screen size is at least 24 x 80 characters.
36if &lines < 24 || &columns < 80
37 let error = 'Screen size too small! Tests require at least 24 lines with 80 characters'
38 echoerr error
39 split test.log
40 $put =error
41 w
42 cquit
43endif
44
Bram Moolenaar89b10422016-07-12 22:51:22 +020045" Common with all tests on all systems.
46source setup.vim
47
Bram Moolenaarc0662462015-12-30 15:49:05 +010048" For consistency run all tests with 'nocompatible' set.
49" This also enables use of line continuation.
50set nocp viminfo+=nviminfo
51
Bram Moolenaareb992cb2017-03-09 18:20:16 +010052" Use utf-8 or latin1 by default, instead of whatever the system default
Bram Moolenaarac105ed2016-07-21 20:33:32 +020053" happens to be. Individual tests can overrule this at the top of the file.
54if has('multi_byte')
55 set encoding=utf-8
56else
57 set encoding=latin1
58endif
59
Bram Moolenaar7a073542017-02-01 23:17:36 +010060" Avoid stopping at the "hit enter" prompt
61set nomore
62
Bram Moolenaarc0662462015-12-30 15:49:05 +010063" Output all messages in English.
64lang mess C
65
Bram Moolenaarf60b7962016-01-16 22:47:23 +010066" Always use forward slashes.
67set shellslash
68
Bram Moolenaar28fb79d2016-01-09 22:28:33 +010069let s:srcdir = expand('%:p:h:h')
70
Bram Moolenaar8e8df252016-05-25 21:23:21 +020071" Prepare for calling test_garbagecollect_now().
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +020072let v:testing = 1
73
Bram Moolenaar28fb79d2016-01-09 22:28:33 +010074" Support function: get the alloc ID by name.
75function GetAllocId(name)
76 exe 'split ' . s:srcdir . '/alloc.h'
Bram Moolenaar065ee9a2016-01-15 20:53:38 +010077 let top = search('typedef enum')
78 if top == 0
79 call add(v:errors, 'typedef not found in alloc.h')
80 endif
Bram Moolenaar28fb79d2016-01-09 22:28:33 +010081 let lnum = search('aid_' . a:name . ',')
82 if lnum == 0
83 call add(v:errors, 'Alloc ID ' . a:name . ' not defined')
84 endif
85 close
Bram Moolenaar065ee9a2016-01-15 20:53:38 +010086 return lnum - top - 1
Bram Moolenaar28fb79d2016-01-09 22:28:33 +010087endfunc
88
Bram Moolenaar42205552017-03-18 19:42:22 +010089func RunTheTest(test)
Bram Moolenaarb5760a12016-03-03 13:10:44 +010090 echo 'Executing ' . a:test
Bram Moolenaare5f2a072017-02-01 22:31:49 +010091
92 " Avoid stopping at the "hit enter" prompt
93 set nomore
94
95 " Avoid a three second wait when a message is about to be overwritten by the
96 " mode message.
97 set noshowmode
98
Bram Moolenaareb992cb2017-03-09 18:20:16 +010099 " Clear any overrides.
100 call test_override('ALL', 0)
101
Bram Moolenaarb5760a12016-03-03 13:10:44 +0100102 if exists("*SetUp")
Bram Moolenaarcc28e2d2016-11-17 17:56:13 +0100103 try
104 call SetUp()
105 catch
106 call add(v:errors, 'Caught exception in SetUp() before ' . a:test . ': ' . v:exception . ' @ ' . v:throwpoint)
107 endtry
Bram Moolenaarb5760a12016-03-03 13:10:44 +0100108 endif
109
110 call add(s:messages, 'Executing ' . a:test)
111 let s:done += 1
Bram Moolenaarf204e052017-10-26 17:14:01 +0200112
113 if a:test =~ 'Test_nocatch_'
114 " Function handles errors itself. This avoids skipping commands after the
115 " error.
Bram Moolenaarb5760a12016-03-03 13:10:44 +0100116 exe 'call ' . a:test
Bram Moolenaarf204e052017-10-26 17:14:01 +0200117 else
118 try
119 exe 'call ' . a:test
120 catch /^\cskipped/
121 call add(s:messages, ' Skipped')
122 call add(s:skipped, 'SKIPPED ' . a:test . ': ' . substitute(v:exception, '^\S*\s\+', '', ''))
123 catch
124 call add(v:errors, 'Caught exception in ' . a:test . ': ' . v:exception . ' @ ' . v:throwpoint)
125 endtry
126 endif
Bram Moolenaarb5760a12016-03-03 13:10:44 +0100127
128 if exists("*TearDown")
Bram Moolenaarcc28e2d2016-11-17 17:56:13 +0100129 try
130 call TearDown()
131 catch
132 call add(v:errors, 'Caught exception in TearDown() after ' . a:test . ': ' . v:exception . ' @ ' . v:throwpoint)
133 endtry
Bram Moolenaarb5760a12016-03-03 13:10:44 +0100134 endif
Bram Moolenaar7cba71d2016-08-02 23:04:49 +0200135
Bram Moolenaarce11de82017-10-26 22:00:00 +0200136 " Close any extra tab pages and windows and make the current one not modified.
137 while tabpagenr('$') > 1
138 bwipe!
139 endwhile
140
Bram Moolenaar358308d2016-08-24 21:21:26 +0200141 while 1
142 let wincount = winnr('$')
143 if wincount == 1
144 break
145 endif
Bram Moolenaar7cba71d2016-08-02 23:04:49 +0200146 bwipe!
Bram Moolenaar358308d2016-08-24 21:21:26 +0200147 if wincount == winnr('$')
148 " Did not manage to close a window.
149 only!
150 break
151 endif
Bram Moolenaar7cba71d2016-08-02 23:04:49 +0200152 endwhile
Bram Moolenaarce11de82017-10-26 22:00:00 +0200153
154 " Wipe out all buffers except the current one, then wipe the current one.
155 for nr in range(1, bufnr('$'))
156 if nr != bufnr('%') && bufexists(nr)
157 exe nr . 'bwipe!'
158 endif
159 endfor
Bram Moolenaar7cba71d2016-08-02 23:04:49 +0200160 set nomodified
Bram Moolenaarce11de82017-10-26 22:00:00 +0200161 bwipe
Bram Moolenaarb5760a12016-03-03 13:10:44 +0100162endfunc
Bram Moolenaar28fb79d2016-01-09 22:28:33 +0100163
Bram Moolenaar42205552017-03-18 19:42:22 +0100164func AfterTheTest()
165 if len(v:errors) > 0
166 let s:fail += 1
167 call add(s:errors, 'Found errors in ' . s:test . ':')
168 call extend(s:errors, v:errors)
169 let v:errors = []
170 endif
171endfunc
172
173" This function can be called by a test if it wants to abort testing.
174func FinishTesting()
175 call AfterTheTest()
176
177 " Don't write viminfo on exit.
178 set viminfo=
179
Bram Moolenaard1ee0042017-07-29 20:39:53 +0200180 " Clean up files created by setup.vim
181 call delete('XfakeHOME', 'rf')
182
Bram Moolenaar42205552017-03-18 19:42:22 +0100183 if s:fail == 0
184 " Success, create the .res file so that make knows it's done.
185 exe 'split ' . fnamemodify(g:testname, ':r') . '.res'
186 write
187 endif
188
189 if len(s:errors) > 0
190 " Append errors to test.log
191 split test.log
192 call append(line('$'), '')
193 call append(line('$'), 'From ' . g:testname . ':')
194 call append(line('$'), s:errors)
195 write
196 endif
197
198 let message = 'Executed ' . s:done . (s:done > 1 ? ' tests' : ' test')
199 echo message
200 call add(s:messages, message)
201 if s:fail > 0
202 let message = s:fail . ' FAILED:'
203 echo message
204 call add(s:messages, message)
205 call extend(s:messages, s:errors)
206 endif
207
208 " Add SKIPPED messages
209 call extend(s:messages, s:skipped)
210
211 " Append messages to the file "messages"
212 split messages
213 call append(line('$'), '')
214 call append(line('$'), 'From ' . g:testname . ':')
215 call append(line('$'), s:messages)
216 write
217
218 qall!
219endfunc
220
Bram Moolenaar43345542015-11-29 17:35:35 +0100221" Source the test script. First grab the file name, in case the script
Bram Moolenaar00af60b2016-02-13 14:06:14 +0100222" navigates away. g:testname can be used by the tests.
223let g:testname = expand('%')
224let s:done = 0
225let s:fail = 0
226let s:errors = []
227let s:messages = []
Bram Moolenaardac19472016-09-03 22:35:40 +0200228let s:skipped = []
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100229if expand('%') =~ 'test_vimscript.vim'
Bram Moolenaar00af60b2016-02-13 14:06:14 +0100230 " this test has intentional s:errors, don't use try/catch.
Bram Moolenaar4686b322015-12-28 14:44:10 +0100231 source %
Bram Moolenaara2cce862016-01-02 19:50:04 +0100232else
233 try
234 source %
235 catch
Bram Moolenaar00af60b2016-02-13 14:06:14 +0100236 let s:fail += 1
237 call add(s:errors, 'Caught exception: ' . v:exception . ' @ ' . v:throwpoint)
Bram Moolenaara2cce862016-01-02 19:50:04 +0100238 endtry
239endif
Bram Moolenaar43345542015-11-29 17:35:35 +0100240
Bram Moolenaarb5760a12016-03-03 13:10:44 +0100241" Names of flaky tests.
Bram Moolenaare1c8c7a2016-09-11 16:48:50 +0200242let s:flaky = [
Bram Moolenaar42205552017-03-18 19:42:22 +0100243 \ 'Test_client_server()',
Bram Moolenaar6fe2eb42017-01-29 21:49:51 +0100244 \ 'Test_close_and_exit_cb()',
Bram Moolenaarb2455592017-02-01 18:00:13 +0100245 \ 'Test_collapse_buffers()',
246 \ 'Test_communicate()',
Bram Moolenaar0529b3e2017-03-16 22:30:37 +0100247 \ 'Test_exit_callback_interval()',
Bram Moolenaarb2455592017-02-01 18:00:13 +0100248 \ 'Test_nb_basic()',
Bram Moolenaard512e172017-02-27 21:35:53 +0100249 \ 'Test_oneshot()',
Bram Moolenaarc79d6aa2016-09-25 22:27:37 +0200250 \ 'Test_pipe_through_sort_all()',
Bram Moolenaar4e032e12017-02-01 20:48:13 +0100251 \ 'Test_pipe_through_sort_some()',
Bram Moolenaar0fbff642017-03-05 14:30:52 +0100252 \ 'Test_quoteplus()',
Bram Moolenaar7dd48502017-03-19 20:04:22 +0100253 \ 'Test_quotestar()',
Bram Moolenaarb2455592017-02-01 18:00:13 +0100254 \ 'Test_reltime()',
Bram Moolenaarf204e052017-10-26 17:14:01 +0200255 \ 'Test_terminal_composing_unicode()',
Bram Moolenaar75a60f72017-09-07 22:24:41 +0200256 \ 'Test_terminal_noblock()',
Bram Moolenaard09be322017-07-30 21:37:58 +0200257 \ 'Test_with_partial_callback()',
Bram Moolenaare1c8c7a2016-09-11 16:48:50 +0200258 \ ]
Bram Moolenaarb5760a12016-03-03 13:10:44 +0100259
Bram Moolenaar43345542015-11-29 17:35:35 +0100260" Locate Test_ functions and execute them.
261redir @q
Bram Moolenaar93bf5582016-02-18 22:25:47 +0100262silent function /^Test_
Bram Moolenaar43345542015-11-29 17:35:35 +0100263redir END
Bram Moolenaar00af60b2016-02-13 14:06:14 +0100264let s:tests = split(substitute(@q, 'function \(\k*()\)', '\1', 'g'))
Bram Moolenaar43345542015-11-29 17:35:35 +0100265
Bram Moolenaarbefb3662016-02-20 14:41:40 +0100266" If there is an extra argument filter the function names against it.
267if argc() > 1
268 let s:tests = filter(s:tests, 'v:val =~ argv(1)')
269endif
270
Bram Moolenaarcfc0a352016-01-09 20:23:00 +0100271" Execute the tests in alphabetical order.
Bram Moolenaar93bf5582016-02-18 22:25:47 +0100272for s:test in sort(s:tests)
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +0200273 " Silence, please!
274 set belloff=all
275
Bram Moolenaarb5760a12016-03-03 13:10:44 +0100276 call RunTheTest(s:test)
Bram Moolenaar43345542015-11-29 17:35:35 +0100277
Bram Moolenaarb5760a12016-03-03 13:10:44 +0100278 if len(v:errors) > 0 && index(s:flaky, s:test) >= 0
Bram Moolenaar6caf6062017-03-18 20:45:05 +0100279 call add(s:messages, 'Found errors in ' . s:test . ':')
280 call extend(s:messages, v:errors)
Bram Moolenaarb5760a12016-03-03 13:10:44 +0100281 call add(s:messages, 'Flaky test failed, running it again')
Bram Moolenaar15e737f2017-03-18 21:22:47 +0100282 let first_run = v:errors
283
Bram Moolenaarb5760a12016-03-03 13:10:44 +0100284 let v:errors = []
285 call RunTheTest(s:test)
Bram Moolenaar15e737f2017-03-18 21:22:47 +0100286 if len(v:errors) > 0
287 let second_run = v:errors
288 let v:errors = ['First run:']
289 call extend(v:errors, first_run)
290 call add(v:errors, 'Second run:')
291 call extend(v:errors, second_run)
292 endif
Bram Moolenaarb5760a12016-03-03 13:10:44 +0100293 endif
Bram Moolenaar43345542015-11-29 17:35:35 +0100294
Bram Moolenaar42205552017-03-18 19:42:22 +0100295 call AfterTheTest()
Bram Moolenaar43345542015-11-29 17:35:35 +0100296endfor
297
Bram Moolenaar42205552017-03-18 19:42:22 +0100298call FinishTesting()
Bram Moolenaarcc28e2d2016-11-17 17:56:13 +0100299
300" vim: shiftwidth=2 sts=2 expandtab