blob: da515f0ae75bbba3252b8ab8e0a460fb3be7aa06 [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
93function /^Test_
94redir 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 Moolenaar00af60b2016-02-13 14:06:14 +010098 for s:test in sort(s:tests)
Bram Moolenaar43345542015-11-29 17:35:35 +010099 if exists("*SetUp")
100 call SetUp()
101 endif
102
Bram Moolenaar00af60b2016-02-13 14:06:14 +0100103 call add(s:messages, 'Executing ' . s:test)
104 let s:done += 1
Bram Moolenaar43345542015-11-29 17:35:35 +0100105 try
Bram Moolenaar00af60b2016-02-13 14:06:14 +0100106 exe 'call ' . s:test
Bram Moolenaar43345542015-11-29 17:35:35 +0100107 catch
Bram Moolenaar00af60b2016-02-13 14:06:14 +0100108 call add(v:errors, 'Caught exception in ' . s:test . ': ' . v:exception . ' @ ' . v:throwpoint)
Bram Moolenaar43345542015-11-29 17:35:35 +0100109 endtry
110
111 if len(v:errors) > 0
Bram Moolenaar00af60b2016-02-13 14:06:14 +0100112 let s:fail += 1
113 call add(s:errors, 'Found errors in ' . s:test . ':')
114 call extend(s:errors, v:errors)
Bram Moolenaar43345542015-11-29 17:35:35 +0100115 let v:errors = []
116 endif
117
118 if exists("*TearDown")
119 call TearDown()
120 endif
121endfor
122
Bram Moolenaar00af60b2016-02-13 14:06:14 +0100123if s:fail == 0
Bram Moolenaar43345542015-11-29 17:35:35 +0100124 " Success, create the .res file so that make knows it's done.
Bram Moolenaar00af60b2016-02-13 14:06:14 +0100125 exe 'split ' . fnamemodify(g:testname, ':r') . '.res'
Bram Moolenaar43345542015-11-29 17:35:35 +0100126 write
127endif
128
Bram Moolenaar00af60b2016-02-13 14:06:14 +0100129if len(s:errors) > 0
Bram Moolenaar43345542015-11-29 17:35:35 +0100130 " Append errors to test.log
131 split test.log
132 call append(line('$'), '')
Bram Moolenaar00af60b2016-02-13 14:06:14 +0100133 call append(line('$'), 'From ' . g:testname . ':')
134 call append(line('$'), s:errors)
Bram Moolenaar43345542015-11-29 17:35:35 +0100135 write
136endif
137
Bram Moolenaar00af60b2016-02-13 14:06:14 +0100138let message = 'Executed ' . s:done . (s:done > 1 ? ' tests' : ' test')
Bram Moolenaar096c8bb2015-12-29 14:26:57 +0100139echo message
Bram Moolenaar00af60b2016-02-13 14:06:14 +0100140call add(s:messages, message)
141if s:fail > 0
142 let message = s:fail . ' FAILED:'
Bram Moolenaar096c8bb2015-12-29 14:26:57 +0100143 echo message
Bram Moolenaar00af60b2016-02-13 14:06:14 +0100144 call add(s:messages, message)
145 call extend(s:messages, s:errors)
Bram Moolenaar43345542015-11-29 17:35:35 +0100146endif
147
Bram Moolenaar096c8bb2015-12-29 14:26:57 +0100148" Append messages to "messages"
149split messages
150call append(line('$'), '')
Bram Moolenaar00af60b2016-02-13 14:06:14 +0100151call append(line('$'), 'From ' . g:testname . ':')
152call append(line('$'), s:messages)
Bram Moolenaar096c8bb2015-12-29 14:26:57 +0100153write
154
Bram Moolenaar43345542015-11-29 17:35:35 +0100155qall!