blob: 2d5e21c9276360981f7ab17c3f7bfd0a883f5844 [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"
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.
Bram Moolenaar00af60b2016-02-13 14:06:14 +010022"
23" When debugging a test it can be useful to add messages to v:errors:
24" call add(v:errors, "this happened")
25
Bram Moolenaar43345542015-11-29 17:35:35 +010026
27" Without the +eval feature we can't run these tests, bail out.
Bram Moolenaar4686b322015-12-28 14:44:10 +010028so small.vim
Bram Moolenaar43345542015-11-29 17:35:35 +010029
30" Check that the screen size is at least 24 x 80 characters.
31if &lines < 24 || &columns < 80
32 let error = 'Screen size too small! Tests require at least 24 lines with 80 characters'
33 echoerr error
34 split test.log
35 $put =error
36 w
37 cquit
38endif
39
Bram Moolenaarc0662462015-12-30 15:49:05 +010040" For consistency run all tests with 'nocompatible' set.
41" This also enables use of line continuation.
42set nocp viminfo+=nviminfo
43
44" Avoid stopping at the "hit enter" prompt
45set nomore
46
47" Output all messages in English.
48lang mess C
49
Bram Moolenaarf60b7962016-01-16 22:47:23 +010050" Always use forward slashes.
51set shellslash
52
Bram Moolenaar28fb79d2016-01-09 22:28:33 +010053let s:srcdir = expand('%:p:h:h')
54
55" Support function: get the alloc ID by name.
56function GetAllocId(name)
57 exe 'split ' . s:srcdir . '/alloc.h'
Bram Moolenaar065ee9a2016-01-15 20:53:38 +010058 let top = search('typedef enum')
59 if top == 0
60 call add(v:errors, 'typedef not found in alloc.h')
61 endif
Bram Moolenaar28fb79d2016-01-09 22:28:33 +010062 let lnum = search('aid_' . a:name . ',')
63 if lnum == 0
64 call add(v:errors, 'Alloc ID ' . a:name . ' not defined')
65 endif
66 close
Bram Moolenaar065ee9a2016-01-15 20:53:38 +010067 return lnum - top - 1
Bram Moolenaar28fb79d2016-01-09 22:28:33 +010068endfunc
69
70
Bram Moolenaar43345542015-11-29 17:35:35 +010071" Source the test script. First grab the file name, in case the script
Bram Moolenaar00af60b2016-02-13 14:06:14 +010072" navigates away. g:testname can be used by the tests.
73let g:testname = expand('%')
74let s:done = 0
75let s:fail = 0
76let s:errors = []
77let s:messages = []
Bram Moolenaara2cce862016-01-02 19:50:04 +010078if expand('%') =~ 'test_viml.vim'
Bram Moolenaar00af60b2016-02-13 14:06:14 +010079 " this test has intentional s:errors, don't use try/catch.
Bram Moolenaar4686b322015-12-28 14:44:10 +010080 source %
Bram Moolenaara2cce862016-01-02 19:50:04 +010081else
82 try
83 source %
84 catch
Bram Moolenaar00af60b2016-02-13 14:06:14 +010085 let s:fail += 1
86 call add(s:errors, 'Caught exception: ' . v:exception . ' @ ' . v:throwpoint)
Bram Moolenaara2cce862016-01-02 19:50:04 +010087 endtry
88endif
Bram Moolenaar43345542015-11-29 17:35:35 +010089
90" Locate Test_ functions and execute them.
Bram Moolenaara99b9042016-01-17 17:10:59 +010091set nomore
Bram Moolenaar43345542015-11-29 17:35:35 +010092redir @q
Bram Moolenaar93bf5582016-02-18 22:25:47 +010093silent function /^Test_
Bram Moolenaar43345542015-11-29 17:35:35 +010094redir END
Bram Moolenaar00af60b2016-02-13 14:06:14 +010095let s:tests = split(substitute(@q, 'function \(\k*()\)', '\1', 'g'))
Bram Moolenaar43345542015-11-29 17:35:35 +010096
Bram Moolenaarcfc0a352016-01-09 20:23:00 +010097" Execute the tests in alphabetical order.
Bram Moolenaar93bf5582016-02-18 22:25:47 +010098for s:test in sort(s:tests)
99 echo 'Executing ' . s:test
Bram Moolenaar43345542015-11-29 17:35:35 +0100100 if exists("*SetUp")
101 call SetUp()
102 endif
103
Bram Moolenaar00af60b2016-02-13 14:06:14 +0100104 call add(s:messages, 'Executing ' . s:test)
105 let s:done += 1
Bram Moolenaar43345542015-11-29 17:35:35 +0100106 try
Bram Moolenaar00af60b2016-02-13 14:06:14 +0100107 exe 'call ' . s:test
Bram Moolenaar43345542015-11-29 17:35:35 +0100108 catch
Bram Moolenaar00af60b2016-02-13 14:06:14 +0100109 call add(v:errors, 'Caught exception in ' . s:test . ': ' . v:exception . ' @ ' . v:throwpoint)
Bram Moolenaar43345542015-11-29 17:35:35 +0100110 endtry
111
112 if len(v:errors) > 0
Bram Moolenaar00af60b2016-02-13 14:06:14 +0100113 let s:fail += 1
114 call add(s:errors, 'Found errors in ' . s:test . ':')
115 call extend(s:errors, v:errors)
Bram Moolenaar43345542015-11-29 17:35:35 +0100116 let v:errors = []
117 endif
118
119 if exists("*TearDown")
120 call TearDown()
121 endif
122endfor
123
Bram Moolenaar00af60b2016-02-13 14:06:14 +0100124if s:fail == 0
Bram Moolenaar43345542015-11-29 17:35:35 +0100125 " Success, create the .res file so that make knows it's done.
Bram Moolenaar00af60b2016-02-13 14:06:14 +0100126 exe 'split ' . fnamemodify(g:testname, ':r') . '.res'
Bram Moolenaar43345542015-11-29 17:35:35 +0100127 write
128endif
129
Bram Moolenaar00af60b2016-02-13 14:06:14 +0100130if len(s:errors) > 0
Bram Moolenaar43345542015-11-29 17:35:35 +0100131 " Append errors to test.log
132 split test.log
133 call append(line('$'), '')
Bram Moolenaar00af60b2016-02-13 14:06:14 +0100134 call append(line('$'), 'From ' . g:testname . ':')
135 call append(line('$'), s:errors)
Bram Moolenaar43345542015-11-29 17:35:35 +0100136 write
137endif
138
Bram Moolenaar00af60b2016-02-13 14:06:14 +0100139let message = 'Executed ' . s:done . (s:done > 1 ? ' tests' : ' test')
Bram Moolenaar096c8bb2015-12-29 14:26:57 +0100140echo message
Bram Moolenaar00af60b2016-02-13 14:06:14 +0100141call add(s:messages, message)
142if s:fail > 0
143 let message = s:fail . ' FAILED:'
Bram Moolenaar096c8bb2015-12-29 14:26:57 +0100144 echo message
Bram Moolenaar00af60b2016-02-13 14:06:14 +0100145 call add(s:messages, message)
146 call extend(s:messages, s:errors)
Bram Moolenaar43345542015-11-29 17:35:35 +0100147endif
148
Bram Moolenaar096c8bb2015-12-29 14:26:57 +0100149" Append messages to "messages"
150split messages
151call append(line('$'), '')
Bram Moolenaar00af60b2016-02-13 14:06:14 +0100152call append(line('$'), 'From ' . g:testname . ':')
153call append(line('$'), s:messages)
Bram Moolenaar096c8bb2015-12-29 14:26:57 +0100154write
155
Bram Moolenaar43345542015-11-29 17:35:35 +0100156qall!