Mercurial > vim
comparison src/testdir/runtest.vim @ 7277:6600871bb38c v7.4.944
commit https://github.com/vim/vim/commit/43345546ae63710441f066648b8485fb545b3801
Author: Bram Moolenaar <Bram@vim.org>
Date: Sun Nov 29 17:35:35 2015 +0100
patch 7.4.944
Problem: Writing tests for Vim script is hard.
Solution: Add assertEqual(), assertFalse() and assertTrue() functions. Add
the v:errors variable. Add the runtest script. Add a first new
style test script.
author | Christian Brabandt <cb@256bit.org> |
---|---|
date | Sun, 29 Nov 2015 17:45:04 +0100 |
parents | |
children | 6922fcadafe6 |
comparison
equal
deleted
inserted
replaced
7276:b03d6d4fe42b | 7277:6600871bb38c |
---|---|
1 " 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. | |
24 if 0 | |
25 quit! | |
26 endif | |
27 | |
28 " Check that the screen size is at least 24 x 80 characters. | |
29 if &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 | |
36 endif | |
37 | |
38 " Source the test script. First grab the file name, in case the script | |
39 " navigates away. | |
40 let testname = expand('%') | |
41 source % | |
42 | |
43 " Locate Test_ functions and execute them. | |
44 redir @q | |
45 function /^Test_ | |
46 redir END | |
47 let tests = split(substitute(@q, 'function \(\k*()\)', '\1', 'g')) | |
48 | |
49 let done = 0 | |
50 let fail = 0 | |
51 let errors = [] | |
52 for 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 | |
75 endfor | |
76 | |
77 if fail == 0 | |
78 " Success, create the .res file so that make knows it's done. | |
79 split %:r.res | |
80 write | |
81 endif | |
82 | |
83 if 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 | |
90 endif | |
91 | |
92 echo 'Executed ' . done . (done > 1 ? ' tests': ' test') | |
93 if fail > 0 | |
94 echo fail . ' FAILED' | |
95 endif | |
96 | |
97 qall! |