blob: 2660d93e8ac2c083bb92544ce50e139cf37e1d30 [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 Moolenaarac105ed2016-07-21 20:33:32 +020052" Use utf-8 or latin1 be default, instead of whatever the system default
53" 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 Moolenaarc0662462015-12-30 15:49:05 +010060" Avoid stopping at the "hit enter" prompt
61set nomore
62
63" 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 Moolenaarb5760a12016-03-03 13:10:44 +010089function RunTheTest(test)
90 echo 'Executing ' . a:test
91 if exists("*SetUp")
92 call SetUp()
93 endif
94
95 call add(s:messages, 'Executing ' . a:test)
96 let s:done += 1
97 try
98 exe 'call ' . a:test
99 catch
100 call add(v:errors, 'Caught exception in ' . a:test . ': ' . v:exception . ' @ ' . v:throwpoint)
101 endtry
102
103 if exists("*TearDown")
104 call TearDown()
105 endif
Bram Moolenaar7cba71d2016-08-02 23:04:49 +0200106
107 " Close any extra windows and make the current one not modified.
Bram Moolenaar358308d2016-08-24 21:21:26 +0200108 while 1
109 let wincount = winnr('$')
110 if wincount == 1
111 break
112 endif
Bram Moolenaar7cba71d2016-08-02 23:04:49 +0200113 bwipe!
Bram Moolenaar358308d2016-08-24 21:21:26 +0200114 if wincount == winnr('$')
115 " Did not manage to close a window.
116 only!
117 break
118 endif
Bram Moolenaar7cba71d2016-08-02 23:04:49 +0200119 endwhile
120 set nomodified
Bram Moolenaarb5760a12016-03-03 13:10:44 +0100121endfunc
Bram Moolenaar28fb79d2016-01-09 22:28:33 +0100122
Bram Moolenaar43345542015-11-29 17:35:35 +0100123" Source the test script. First grab the file name, in case the script
Bram Moolenaar00af60b2016-02-13 14:06:14 +0100124" navigates away. g:testname can be used by the tests.
125let g:testname = expand('%')
126let s:done = 0
127let s:fail = 0
128let s:errors = []
129let s:messages = []
Bram Moolenaara2cce862016-01-02 19:50:04 +0100130if expand('%') =~ 'test_viml.vim'
Bram Moolenaar00af60b2016-02-13 14:06:14 +0100131 " this test has intentional s:errors, don't use try/catch.
Bram Moolenaar4686b322015-12-28 14:44:10 +0100132 source %
Bram Moolenaara2cce862016-01-02 19:50:04 +0100133else
134 try
135 source %
136 catch
Bram Moolenaar00af60b2016-02-13 14:06:14 +0100137 let s:fail += 1
138 call add(s:errors, 'Caught exception: ' . v:exception . ' @ ' . v:throwpoint)
Bram Moolenaara2cce862016-01-02 19:50:04 +0100139 endtry
140endif
Bram Moolenaar43345542015-11-29 17:35:35 +0100141
Bram Moolenaarb5760a12016-03-03 13:10:44 +0100142" Names of flaky tests.
Bram Moolenaarfe948922016-08-04 21:11:32 +0200143let s:flaky = ['Test_reltime()', 'Test_nb_basic()', 'Test_communicate()']
Bram Moolenaarb5760a12016-03-03 13:10:44 +0100144
Bram Moolenaar43345542015-11-29 17:35:35 +0100145" Locate Test_ functions and execute them.
Bram Moolenaara99b9042016-01-17 17:10:59 +0100146set nomore
Bram Moolenaar43345542015-11-29 17:35:35 +0100147redir @q
Bram Moolenaar93bf5582016-02-18 22:25:47 +0100148silent function /^Test_
Bram Moolenaar43345542015-11-29 17:35:35 +0100149redir END
Bram Moolenaar00af60b2016-02-13 14:06:14 +0100150let s:tests = split(substitute(@q, 'function \(\k*()\)', '\1', 'g'))
Bram Moolenaar43345542015-11-29 17:35:35 +0100151
Bram Moolenaarbefb3662016-02-20 14:41:40 +0100152" If there is an extra argument filter the function names against it.
153if argc() > 1
154 let s:tests = filter(s:tests, 'v:val =~ argv(1)')
155endif
156
Bram Moolenaarcfc0a352016-01-09 20:23:00 +0100157" Execute the tests in alphabetical order.
Bram Moolenaar93bf5582016-02-18 22:25:47 +0100158for s:test in sort(s:tests)
Bram Moolenaarb5760a12016-03-03 13:10:44 +0100159 call RunTheTest(s:test)
Bram Moolenaar43345542015-11-29 17:35:35 +0100160
Bram Moolenaarb5760a12016-03-03 13:10:44 +0100161 if len(v:errors) > 0 && index(s:flaky, s:test) >= 0
162 call add(s:messages, 'Flaky test failed, running it again')
163 let v:errors = []
164 call RunTheTest(s:test)
165 endif
Bram Moolenaar43345542015-11-29 17:35:35 +0100166
167 if len(v:errors) > 0
Bram Moolenaar00af60b2016-02-13 14:06:14 +0100168 let s:fail += 1
169 call add(s:errors, 'Found errors in ' . s:test . ':')
170 call extend(s:errors, v:errors)
Bram Moolenaar43345542015-11-29 17:35:35 +0100171 let v:errors = []
172 endif
Bram Moolenaar43345542015-11-29 17:35:35 +0100173endfor
174
Bram Moolenaarfc4ad612016-07-09 15:38:32 +0200175" Don't write viminfo on exit.
176set viminfo=
177
Bram Moolenaar00af60b2016-02-13 14:06:14 +0100178if s:fail == 0
Bram Moolenaar43345542015-11-29 17:35:35 +0100179 " Success, create the .res file so that make knows it's done.
Bram Moolenaar00af60b2016-02-13 14:06:14 +0100180 exe 'split ' . fnamemodify(g:testname, ':r') . '.res'
Bram Moolenaar43345542015-11-29 17:35:35 +0100181 write
182endif
183
Bram Moolenaar00af60b2016-02-13 14:06:14 +0100184if len(s:errors) > 0
Bram Moolenaar43345542015-11-29 17:35:35 +0100185 " Append errors to test.log
186 split test.log
187 call append(line('$'), '')
Bram Moolenaar00af60b2016-02-13 14:06:14 +0100188 call append(line('$'), 'From ' . g:testname . ':')
189 call append(line('$'), s:errors)
Bram Moolenaar43345542015-11-29 17:35:35 +0100190 write
191endif
192
Bram Moolenaar00af60b2016-02-13 14:06:14 +0100193let message = 'Executed ' . s:done . (s:done > 1 ? ' tests' : ' test')
Bram Moolenaar096c8bb2015-12-29 14:26:57 +0100194echo message
Bram Moolenaar00af60b2016-02-13 14:06:14 +0100195call add(s:messages, message)
196if s:fail > 0
197 let message = s:fail . ' FAILED:'
Bram Moolenaar096c8bb2015-12-29 14:26:57 +0100198 echo message
Bram Moolenaar00af60b2016-02-13 14:06:14 +0100199 call add(s:messages, message)
200 call extend(s:messages, s:errors)
Bram Moolenaar43345542015-11-29 17:35:35 +0100201endif
202
Bram Moolenaar096c8bb2015-12-29 14:26:57 +0100203" Append messages to "messages"
204split messages
205call append(line('$'), '')
Bram Moolenaar00af60b2016-02-13 14:06:14 +0100206call append(line('$'), 'From ' . g:testname . ':')
207call append(line('$'), s:messages)
Bram Moolenaar096c8bb2015-12-29 14:26:57 +0100208write
209
Bram Moolenaar43345542015-11-29 17:35:35 +0100210qall!