blob: 0dc142eb97fe1ee8c1519f28bbd33b23ff3ae2fe [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.
24if 0
25 quit!
26endif
27
28" Check that the screen size is at least 24 x 80 characters.
29if &lines < 24 || &columns < 80
30 let error = 'Screen size too small! Tests require at least 24 lines with 80 characters'
31 echoerr error
32 split test.log
33 $put =error
34 w
35 cquit
36endif
37
38" Source the test script. First grab the file name, in case the script
39" navigates away.
40let testname = expand('%')
41source %
42
43" Locate Test_ functions and execute them.
44redir @q
45function /^Test_
46redir END
47let tests = split(substitute(@q, 'function \(\k*()\)', '\1', 'g'))
48
49let done = 0
50let fail = 0
51let errors = []
52for test in tests
53 if exists("*SetUp")
54 call SetUp()
55 endif
56
57 let done += 1
58 try
59 exe 'call ' . test
60 catch
61 let fail += 1
62 call add(v:errors, 'Caught exception in ' . test . ': ' . v:exception . ' @ ' . v:throwpoint)
63 endtry
64
65 if len(v:errors) > 0
66 let fail += 1
67 call add(errors, 'Found errors in ' . test . ':')
68 call extend(errors, v:errors)
69 let v:errors = []
70 endif
71
72 if exists("*TearDown")
73 call TearDown()
74 endif
75endfor
76
77if fail == 0
78 " Success, create the .res file so that make knows it's done.
Bram Moolenaarde0ad402015-12-03 17:21:28 +010079 exe 'split ' . fnamemodify(testname, ':r') . '.res'
Bram Moolenaar43345542015-11-29 17:35:35 +010080 write
81endif
82
83if len(errors) > 0
84 " Append errors to test.log
85 split test.log
86 call append(line('$'), '')
87 call append(line('$'), 'From ' . testname . ':')
88 call append(line('$'), errors)
89 write
90endif
91
92echo 'Executed ' . done . (done > 1 ? ' tests': ' test')
93if fail > 0
94 echo fail . ' FAILED'
95endif
96
97qall!