blob: 8314a45d0c6fabdaed4b66211ab6daa827994496 [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
36" Source the test script. First grab the file name, in case the script
37" navigates away.
38let testname = expand('%')
Bram Moolenaar4686b322015-12-28 14:44:10 +010039let done = 0
40let fail = 0
41let errors = []
Bram Moolenaar096c8bb2015-12-29 14:26:57 +010042let messages = []
Bram Moolenaar4686b322015-12-28 14:44:10 +010043try
44 source %
45catch
46 let fail += 1
47 call add(errors, 'Caught exception: ' . v:exception . ' @ ' . v:throwpoint)
48endtry
Bram Moolenaar43345542015-11-29 17:35:35 +010049
50" Locate Test_ functions and execute them.
51redir @q
52function /^Test_
53redir END
54let tests = split(substitute(@q, 'function \(\k*()\)', '\1', 'g'))
55
Bram Moolenaar43345542015-11-29 17:35:35 +010056for test in tests
57 if exists("*SetUp")
58 call SetUp()
59 endif
60
Bram Moolenaar096c8bb2015-12-29 14:26:57 +010061 call add(messages, 'Executing ' . test)
Bram Moolenaar43345542015-11-29 17:35:35 +010062 let done += 1
63 try
64 exe 'call ' . test
65 catch
66 let fail += 1
67 call add(v:errors, 'Caught exception in ' . test . ': ' . v:exception . ' @ ' . v:throwpoint)
68 endtry
69
70 if len(v:errors) > 0
71 let fail += 1
72 call add(errors, 'Found errors in ' . test . ':')
73 call extend(errors, v:errors)
74 let v:errors = []
75 endif
76
77 if exists("*TearDown")
78 call TearDown()
79 endif
80endfor
81
82if fail == 0
83 " Success, create the .res file so that make knows it's done.
Bram Moolenaarde0ad402015-12-03 17:21:28 +010084 exe 'split ' . fnamemodify(testname, ':r') . '.res'
Bram Moolenaar43345542015-11-29 17:35:35 +010085 write
86endif
87
88if len(errors) > 0
89 " Append errors to test.log
90 split test.log
91 call append(line('$'), '')
92 call append(line('$'), 'From ' . testname . ':')
93 call append(line('$'), errors)
94 write
95endif
96
Bram Moolenaar096c8bb2015-12-29 14:26:57 +010097let message = 'Executed ' . done . (done > 1 ? ' tests': ' test')
98echo message
99call add(messages, message)
Bram Moolenaar43345542015-11-29 17:35:35 +0100100if fail > 0
Bram Moolenaar096c8bb2015-12-29 14:26:57 +0100101 let message = fail . ' FAILED'
102 echo message
103 call add(messages, message)
Bram Moolenaar43345542015-11-29 17:35:35 +0100104endif
105
Bram Moolenaar096c8bb2015-12-29 14:26:57 +0100106" Append messages to "messages"
107split messages
108call append(line('$'), '')
109call append(line('$'), 'From ' . testname . ':')
110call append(line('$'), messages)
111write
112
Bram Moolenaar43345542015-11-29 17:35:35 +0100113qall!