diff options
author | Bram Moolenaar <Bram@vim.org> | 2015-11-29 17:35:35 +0100 |
---|---|---|
committer | Bram Moolenaar <Bram@vim.org> | 2015-11-29 17:35:35 +0100 |
commit | 43345546ae63710441f066648b8485fb545b3801 (patch) | |
tree | 6856441fbdb36ee40e5225b97b1e0e698fc72d62 /src/testdir/runtest.vim | |
parent | 48a969b48898fb08dce636c6b918408c6fbd3ea0 (diff) | |
download | vim-git-43345546ae63710441f066648b8485fb545b3801.tar.gz |
patch 7.4.944v7.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.
Diffstat (limited to 'src/testdir/runtest.vim')
-rw-r--r-- | src/testdir/runtest.vim | 97 |
1 files changed, 97 insertions, 0 deletions
diff --git a/src/testdir/runtest.vim b/src/testdir/runtest.vim new file mode 100644 index 000000000..8589bf693 --- /dev/null +++ b/src/testdir/runtest.vim @@ -0,0 +1,97 @@ +" This script is sourced while editing the .vim file with the tests. +" When the script is successful the .res file will be created. +" Errors are appended to the test.log file. +" +" The test script may contain anything, only functions that start with +" "Test_" are special. These will be invoked and should contain assert +" functions. See test_assert.vim for an example. +" +" It is possible to source other files that contain "Test_" functions. This +" can speed up testing, since Vim does not need to restart. But be careful +" that the tests do not interfere with each other. +" +" If an error cannot be detected properly with an assert function add the +" error to the v:errors list: +" call add(v:errors, 'test foo failed: Cannot find xyz') +" +" If preparation for each Test_ function is needed, define a SetUp function. +" It will be called before each Test_ function. +" +" If cleanup after each Test_ function is needed, define a TearDown function. +" It will be called after each Test_ function. + +" Without the +eval feature we can't run these tests, bail out. +if 0 + quit! +endif + +" Check that the screen size is at least 24 x 80 characters. +if &lines < 24 || &columns < 80 + let error = 'Screen size too small! Tests require at least 24 lines with 80 characters' + echoerr error + split test.log + $put =error + w + cquit +endif + +" Source the test script. First grab the file name, in case the script +" navigates away. +let testname = expand('%') +source % + +" Locate Test_ functions and execute them. +redir @q +function /^Test_ +redir END +let tests = split(substitute(@q, 'function \(\k*()\)', '\1', 'g')) + +let done = 0 +let fail = 0 +let errors = [] +for test in tests + if exists("*SetUp") + call SetUp() + endif + + let done += 1 + try + exe 'call ' . test + catch + let fail += 1 + call add(v:errors, 'Caught exception in ' . test . ': ' . v:exception . ' @ ' . v:throwpoint) + endtry + + if len(v:errors) > 0 + let fail += 1 + call add(errors, 'Found errors in ' . test . ':') + call extend(errors, v:errors) + let v:errors = [] + endif + + if exists("*TearDown") + call TearDown() + endif +endfor + +if fail == 0 + " Success, create the .res file so that make knows it's done. + split %:r.res + write +endif + +if len(errors) > 0 + " Append errors to test.log + split test.log + call append(line('$'), '') + call append(line('$'), 'From ' . testname . ':') + call append(line('$'), errors) + write +endif + +echo 'Executed ' . done . (done > 1 ? ' tests': ' test') +if fail > 0 + echo fail . ' FAILED' +endif + +qall! |