blob: 1c4ceadb4c2f8f0f4035ca6f106020cf42264224 [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 Moolenaar43345542015-11-29 17:35:35 +010046" Source the test script. First grab the file name, in case the script
47" navigates away.
48let testname = expand('%')
Bram Moolenaar4686b322015-12-28 14:44:10 +010049let done = 0
50let fail = 0
51let errors = []
Bram Moolenaar096c8bb2015-12-29 14:26:57 +010052let messages = []
Bram Moolenaara2cce862016-01-02 19:50:04 +010053if expand('%') =~ 'test_viml.vim'
54 " this test has intentional errors, don't use try/catch.
Bram Moolenaar4686b322015-12-28 14:44:10 +010055 source %
Bram Moolenaara2cce862016-01-02 19:50:04 +010056else
57 try
58 source %
59 catch
60 let fail += 1
61 call add(errors, 'Caught exception: ' . v:exception . ' @ ' . v:throwpoint)
62 endtry
63endif
Bram Moolenaar43345542015-11-29 17:35:35 +010064
65" Locate Test_ functions and execute them.
66redir @q
67function /^Test_
68redir END
69let tests = split(substitute(@q, 'function \(\k*()\)', '\1', 'g'))
70
Bram Moolenaar43345542015-11-29 17:35:35 +010071for test in tests
72 if exists("*SetUp")
73 call SetUp()
74 endif
75
Bram Moolenaar096c8bb2015-12-29 14:26:57 +010076 call add(messages, 'Executing ' . test)
Bram Moolenaar43345542015-11-29 17:35:35 +010077 let done += 1
78 try
79 exe 'call ' . test
80 catch
81 let fail += 1
82 call add(v:errors, 'Caught exception in ' . test . ': ' . v:exception . ' @ ' . v:throwpoint)
83 endtry
84
85 if len(v:errors) > 0
86 let fail += 1
87 call add(errors, 'Found errors in ' . test . ':')
88 call extend(errors, v:errors)
89 let v:errors = []
90 endif
91
92 if exists("*TearDown")
93 call TearDown()
94 endif
95endfor
96
97if fail == 0
98 " Success, create the .res file so that make knows it's done.
Bram Moolenaarde0ad402015-12-03 17:21:28 +010099 exe 'split ' . fnamemodify(testname, ':r') . '.res'
Bram Moolenaar43345542015-11-29 17:35:35 +0100100 write
101endif
102
103if len(errors) > 0
104 " Append errors to test.log
105 split test.log
106 call append(line('$'), '')
107 call append(line('$'), 'From ' . testname . ':')
108 call append(line('$'), errors)
109 write
110endif
111
Bram Moolenaar096c8bb2015-12-29 14:26:57 +0100112let message = 'Executed ' . done . (done > 1 ? ' tests': ' test')
113echo message
114call add(messages, message)
Bram Moolenaar43345542015-11-29 17:35:35 +0100115if fail > 0
Bram Moolenaar096c8bb2015-12-29 14:26:57 +0100116 let message = fail . ' FAILED'
117 echo message
118 call add(messages, message)
Bram Moolenaar43345542015-11-29 17:35:35 +0100119endif
120
Bram Moolenaar096c8bb2015-12-29 14:26:57 +0100121" Append messages to "messages"
122split messages
123call append(line('$'), '')
124call append(line('$'), 'From ' . testname . ':')
125call append(line('$'), messages)
126write
127
Bram Moolenaar43345542015-11-29 17:35:35 +0100128qall!