blob: cca7c751dfd91f63617f57a788bb620481ca5b8e [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 = []
42try
43 source %
44catch
45 let fail += 1
46 call add(errors, 'Caught exception: ' . v:exception . ' @ ' . v:throwpoint)
47endtry
Bram Moolenaar43345542015-11-29 17:35:35 +010048
49" Locate Test_ functions and execute them.
50redir @q
51function /^Test_
52redir END
53let tests = split(substitute(@q, 'function \(\k*()\)', '\1', 'g'))
54
Bram Moolenaar43345542015-11-29 17:35:35 +010055for test in tests
56 if exists("*SetUp")
57 call SetUp()
58 endif
59
60 let done += 1
61 try
62 exe 'call ' . test
63 catch
64 let fail += 1
65 call add(v:errors, 'Caught exception in ' . test . ': ' . v:exception . ' @ ' . v:throwpoint)
66 endtry
67
68 if len(v:errors) > 0
69 let fail += 1
70 call add(errors, 'Found errors in ' . test . ':')
71 call extend(errors, v:errors)
72 let v:errors = []
73 endif
74
75 if exists("*TearDown")
76 call TearDown()
77 endif
78endfor
79
80if fail == 0
81 " Success, create the .res file so that make knows it's done.
Bram Moolenaarde0ad402015-12-03 17:21:28 +010082 exe 'split ' . fnamemodify(testname, ':r') . '.res'
Bram Moolenaar43345542015-11-29 17:35:35 +010083 write
84endif
85
86if len(errors) > 0
87 " Append errors to test.log
88 split test.log
89 call append(line('$'), '')
90 call append(line('$'), 'From ' . testname . ':')
91 call append(line('$'), errors)
92 write
93endif
94
95echo 'Executed ' . done . (done > 1 ? ' tests': ' test')
96if fail > 0
97 echo fail . ' FAILED'
98endif
99
100qall!