blob: 686a4df6256bf9eb90cd55aa0bd17283d9269d5e [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.
22
23" Without the +eval feature we can't run these tests, bail out.
Bram Moolenaar4686b322015-12-28 14:44:10 +010024so small.vim
Bram Moolenaar43345542015-11-29 17:35:35 +010025
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
Bram Moolenaarc0662462015-12-30 15:49:05 +010036" 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
Bram Moolenaarf60b7962016-01-16 22:47:23 +010046" Always use forward slashes.
47set shellslash
48
Bram Moolenaar28fb79d2016-01-09 22:28:33 +010049let s:srcdir = expand('%:p:h:h')
50
51" Support function: get the alloc ID by name.
52function GetAllocId(name)
53 exe 'split ' . s:srcdir . '/alloc.h'
Bram Moolenaar065ee9a2016-01-15 20:53:38 +010054 let top = search('typedef enum')
55 if top == 0
56 call add(v:errors, 'typedef not found in alloc.h')
57 endif
Bram Moolenaar28fb79d2016-01-09 22:28:33 +010058 let lnum = search('aid_' . a:name . ',')
59 if lnum == 0
60 call add(v:errors, 'Alloc ID ' . a:name . ' not defined')
61 endif
62 close
Bram Moolenaar065ee9a2016-01-15 20:53:38 +010063 return lnum - top - 1
Bram Moolenaar28fb79d2016-01-09 22:28:33 +010064endfunc
65
66
Bram Moolenaar43345542015-11-29 17:35:35 +010067" Source the test script. First grab the file name, in case the script
68" navigates away.
69let testname = expand('%')
Bram Moolenaar4686b322015-12-28 14:44:10 +010070let done = 0
71let fail = 0
72let errors = []
Bram Moolenaar096c8bb2015-12-29 14:26:57 +010073let messages = []
Bram Moolenaara2cce862016-01-02 19:50:04 +010074if expand('%') =~ 'test_viml.vim'
75 " this test has intentional errors, don't use try/catch.
Bram Moolenaar4686b322015-12-28 14:44:10 +010076 source %
Bram Moolenaara2cce862016-01-02 19:50:04 +010077else
78 try
79 source %
80 catch
81 let fail += 1
82 call add(errors, 'Caught exception: ' . v:exception . ' @ ' . v:throwpoint)
83 endtry
84endif
Bram Moolenaar43345542015-11-29 17:35:35 +010085
86" Locate Test_ functions and execute them.
Bram Moolenaara99b9042016-01-17 17:10:59 +010087set nomore
Bram Moolenaar43345542015-11-29 17:35:35 +010088redir @q
89function /^Test_
90redir END
91let tests = split(substitute(@q, 'function \(\k*()\)', '\1', 'g'))
92
Bram Moolenaarcfc0a352016-01-09 20:23:00 +010093" Execute the tests in alphabetical order.
94 for test in sort(tests)
Bram Moolenaar43345542015-11-29 17:35:35 +010095 if exists("*SetUp")
96 call SetUp()
97 endif
98
Bram Moolenaar096c8bb2015-12-29 14:26:57 +010099 call add(messages, 'Executing ' . test)
Bram Moolenaar43345542015-11-29 17:35:35 +0100100 let done += 1
101 try
102 exe 'call ' . test
103 catch
104 let fail += 1
105 call add(v:errors, 'Caught exception in ' . test . ': ' . v:exception . ' @ ' . v:throwpoint)
106 endtry
107
108 if len(v:errors) > 0
109 let fail += 1
110 call add(errors, 'Found errors in ' . test . ':')
111 call extend(errors, v:errors)
112 let v:errors = []
113 endif
114
115 if exists("*TearDown")
116 call TearDown()
117 endif
118endfor
119
120if fail == 0
121 " Success, create the .res file so that make knows it's done.
Bram Moolenaarde0ad402015-12-03 17:21:28 +0100122 exe 'split ' . fnamemodify(testname, ':r') . '.res'
Bram Moolenaar43345542015-11-29 17:35:35 +0100123 write
124endif
125
126if len(errors) > 0
127 " Append errors to test.log
128 split test.log
129 call append(line('$'), '')
130 call append(line('$'), 'From ' . testname . ':')
131 call append(line('$'), errors)
132 write
133endif
134
Bram Moolenaar096c8bb2015-12-29 14:26:57 +0100135let message = 'Executed ' . done . (done > 1 ? ' tests': ' test')
136echo message
137call add(messages, message)
Bram Moolenaar43345542015-11-29 17:35:35 +0100138if fail > 0
Bram Moolenaar096c8bb2015-12-29 14:26:57 +0100139 let message = fail . ' FAILED'
140 echo message
141 call add(messages, message)
Bram Moolenaar43345542015-11-29 17:35:35 +0100142endif
143
Bram Moolenaar096c8bb2015-12-29 14:26:57 +0100144" Append messages to "messages"
145split messages
146call append(line('$'), '')
147call append(line('$'), 'From ' . testname . ':')
148call append(line('$'), messages)
149write
150
Bram Moolenaar43345542015-11-29 17:35:35 +0100151qall!