Bram Moolenaar | 4334554 | 2015-11-29 17:35:35 +0100 | [diff] [blame] | 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 | " |
Bram Moolenaar | befb366 | 2016-02-20 14:41:40 +0100 | [diff] [blame] | 5 | " To execute only specific test functions, add a second argument. It will be |
| 6 | " matched against the names of the Test_ funtion. E.g.: |
| 7 | " ../vim -u NONE -S runtest.vim test_channel.vim open_delay |
| 8 | " The output can be found in the "messages" file. |
| 9 | " |
Bram Moolenaar | 4334554 | 2015-11-29 17:35:35 +0100 | [diff] [blame] | 10 | " The test script may contain anything, only functions that start with |
| 11 | " "Test_" are special. These will be invoked and should contain assert |
| 12 | " functions. See test_assert.vim for an example. |
| 13 | " |
| 14 | " It is possible to source other files that contain "Test_" functions. This |
| 15 | " can speed up testing, since Vim does not need to restart. But be careful |
| 16 | " that the tests do not interfere with each other. |
| 17 | " |
| 18 | " If an error cannot be detected properly with an assert function add the |
| 19 | " error to the v:errors list: |
| 20 | " call add(v:errors, 'test foo failed: Cannot find xyz') |
| 21 | " |
| 22 | " If preparation for each Test_ function is needed, define a SetUp function. |
| 23 | " It will be called before each Test_ function. |
| 24 | " |
| 25 | " If cleanup after each Test_ function is needed, define a TearDown function. |
| 26 | " It will be called after each Test_ function. |
Bram Moolenaar | 00af60b | 2016-02-13 14:06:14 +0100 | [diff] [blame] | 27 | " |
| 28 | " When debugging a test it can be useful to add messages to v:errors: |
| 29 | " call add(v:errors, "this happened") |
| 30 | |
Bram Moolenaar | 4334554 | 2015-11-29 17:35:35 +0100 | [diff] [blame] | 31 | |
| 32 | " Without the +eval feature we can't run these tests, bail out. |
Bram Moolenaar | 4686b32 | 2015-12-28 14:44:10 +0100 | [diff] [blame] | 33 | so small.vim |
Bram Moolenaar | 4334554 | 2015-11-29 17:35:35 +0100 | [diff] [blame] | 34 | |
| 35 | " Check that the screen size is at least 24 x 80 characters. |
| 36 | if &lines < 24 || &columns < 80 |
| 37 | let error = 'Screen size too small! Tests require at least 24 lines with 80 characters' |
| 38 | echoerr error |
| 39 | split test.log |
| 40 | $put =error |
| 41 | w |
| 42 | cquit |
| 43 | endif |
| 44 | |
Bram Moolenaar | c066246 | 2015-12-30 15:49:05 +0100 | [diff] [blame] | 45 | " For consistency run all tests with 'nocompatible' set. |
| 46 | " This also enables use of line continuation. |
| 47 | set nocp viminfo+=nviminfo |
| 48 | |
| 49 | " Avoid stopping at the "hit enter" prompt |
| 50 | set nomore |
| 51 | |
| 52 | " Output all messages in English. |
| 53 | lang mess C |
| 54 | |
Bram Moolenaar | f60b796 | 2016-01-16 22:47:23 +0100 | [diff] [blame] | 55 | " Always use forward slashes. |
| 56 | set shellslash |
| 57 | |
Bram Moolenaar | e9c0727 | 2016-03-30 20:50:46 +0200 | [diff] [blame] | 58 | " Make sure $HOME does not get read or written. |
| 59 | let $HOME = '/does/not/exist' |
| 60 | |
Bram Moolenaar | 28fb79d | 2016-01-09 22:28:33 +0100 | [diff] [blame] | 61 | let s:srcdir = expand('%:p:h:h') |
| 62 | |
| 63 | " Support function: get the alloc ID by name. |
| 64 | function GetAllocId(name) |
| 65 | exe 'split ' . s:srcdir . '/alloc.h' |
Bram Moolenaar | 065ee9a | 2016-01-15 20:53:38 +0100 | [diff] [blame] | 66 | let top = search('typedef enum') |
| 67 | if top == 0 |
| 68 | call add(v:errors, 'typedef not found in alloc.h') |
| 69 | endif |
Bram Moolenaar | 28fb79d | 2016-01-09 22:28:33 +0100 | [diff] [blame] | 70 | let lnum = search('aid_' . a:name . ',') |
| 71 | if lnum == 0 |
| 72 | call add(v:errors, 'Alloc ID ' . a:name . ' not defined') |
| 73 | endif |
| 74 | close |
Bram Moolenaar | 065ee9a | 2016-01-15 20:53:38 +0100 | [diff] [blame] | 75 | return lnum - top - 1 |
Bram Moolenaar | 28fb79d | 2016-01-09 22:28:33 +0100 | [diff] [blame] | 76 | endfunc |
| 77 | |
Bram Moolenaar | b5760a1 | 2016-03-03 13:10:44 +0100 | [diff] [blame] | 78 | function RunTheTest(test) |
| 79 | echo 'Executing ' . a:test |
| 80 | if exists("*SetUp") |
| 81 | call SetUp() |
| 82 | endif |
| 83 | |
| 84 | call add(s:messages, 'Executing ' . a:test) |
| 85 | let s:done += 1 |
| 86 | try |
| 87 | exe 'call ' . a:test |
| 88 | catch |
| 89 | call add(v:errors, 'Caught exception in ' . a:test . ': ' . v:exception . ' @ ' . v:throwpoint) |
| 90 | endtry |
| 91 | |
| 92 | if exists("*TearDown") |
| 93 | call TearDown() |
| 94 | endif |
| 95 | endfunc |
Bram Moolenaar | 28fb79d | 2016-01-09 22:28:33 +0100 | [diff] [blame] | 96 | |
Bram Moolenaar | 4334554 | 2015-11-29 17:35:35 +0100 | [diff] [blame] | 97 | " Source the test script. First grab the file name, in case the script |
Bram Moolenaar | 00af60b | 2016-02-13 14:06:14 +0100 | [diff] [blame] | 98 | " navigates away. g:testname can be used by the tests. |
| 99 | let g:testname = expand('%') |
| 100 | let s:done = 0 |
| 101 | let s:fail = 0 |
| 102 | let s:errors = [] |
| 103 | let s:messages = [] |
Bram Moolenaar | a2cce86 | 2016-01-02 19:50:04 +0100 | [diff] [blame] | 104 | if expand('%') =~ 'test_viml.vim' |
Bram Moolenaar | 00af60b | 2016-02-13 14:06:14 +0100 | [diff] [blame] | 105 | " this test has intentional s:errors, don't use try/catch. |
Bram Moolenaar | 4686b32 | 2015-12-28 14:44:10 +0100 | [diff] [blame] | 106 | source % |
Bram Moolenaar | a2cce86 | 2016-01-02 19:50:04 +0100 | [diff] [blame] | 107 | else |
| 108 | try |
| 109 | source % |
| 110 | catch |
Bram Moolenaar | 00af60b | 2016-02-13 14:06:14 +0100 | [diff] [blame] | 111 | let s:fail += 1 |
| 112 | call add(s:errors, 'Caught exception: ' . v:exception . ' @ ' . v:throwpoint) |
Bram Moolenaar | a2cce86 | 2016-01-02 19:50:04 +0100 | [diff] [blame] | 113 | endtry |
| 114 | endif |
Bram Moolenaar | 4334554 | 2015-11-29 17:35:35 +0100 | [diff] [blame] | 115 | |
Bram Moolenaar | b5760a1 | 2016-03-03 13:10:44 +0100 | [diff] [blame] | 116 | " Names of flaky tests. |
| 117 | let s:flaky = ['Test_reltime()'] |
| 118 | |
Bram Moolenaar | 4334554 | 2015-11-29 17:35:35 +0100 | [diff] [blame] | 119 | " Locate Test_ functions and execute them. |
Bram Moolenaar | a99b904 | 2016-01-17 17:10:59 +0100 | [diff] [blame] | 120 | set nomore |
Bram Moolenaar | 4334554 | 2015-11-29 17:35:35 +0100 | [diff] [blame] | 121 | redir @q |
Bram Moolenaar | 93bf558 | 2016-02-18 22:25:47 +0100 | [diff] [blame] | 122 | silent function /^Test_ |
Bram Moolenaar | 4334554 | 2015-11-29 17:35:35 +0100 | [diff] [blame] | 123 | redir END |
Bram Moolenaar | 00af60b | 2016-02-13 14:06:14 +0100 | [diff] [blame] | 124 | let s:tests = split(substitute(@q, 'function \(\k*()\)', '\1', 'g')) |
Bram Moolenaar | 4334554 | 2015-11-29 17:35:35 +0100 | [diff] [blame] | 125 | |
Bram Moolenaar | befb366 | 2016-02-20 14:41:40 +0100 | [diff] [blame] | 126 | " If there is an extra argument filter the function names against it. |
| 127 | if argc() > 1 |
| 128 | let s:tests = filter(s:tests, 'v:val =~ argv(1)') |
| 129 | endif |
| 130 | |
Bram Moolenaar | cfc0a35 | 2016-01-09 20:23:00 +0100 | [diff] [blame] | 131 | " Execute the tests in alphabetical order. |
Bram Moolenaar | 93bf558 | 2016-02-18 22:25:47 +0100 | [diff] [blame] | 132 | for s:test in sort(s:tests) |
Bram Moolenaar | b5760a1 | 2016-03-03 13:10:44 +0100 | [diff] [blame] | 133 | call RunTheTest(s:test) |
Bram Moolenaar | 4334554 | 2015-11-29 17:35:35 +0100 | [diff] [blame] | 134 | |
Bram Moolenaar | b5760a1 | 2016-03-03 13:10:44 +0100 | [diff] [blame] | 135 | if len(v:errors) > 0 && index(s:flaky, s:test) >= 0 |
| 136 | call add(s:messages, 'Flaky test failed, running it again') |
| 137 | let v:errors = [] |
| 138 | call RunTheTest(s:test) |
| 139 | endif |
Bram Moolenaar | 4334554 | 2015-11-29 17:35:35 +0100 | [diff] [blame] | 140 | |
| 141 | if len(v:errors) > 0 |
Bram Moolenaar | 00af60b | 2016-02-13 14:06:14 +0100 | [diff] [blame] | 142 | let s:fail += 1 |
| 143 | call add(s:errors, 'Found errors in ' . s:test . ':') |
| 144 | call extend(s:errors, v:errors) |
Bram Moolenaar | 4334554 | 2015-11-29 17:35:35 +0100 | [diff] [blame] | 145 | let v:errors = [] |
| 146 | endif |
Bram Moolenaar | 4334554 | 2015-11-29 17:35:35 +0100 | [diff] [blame] | 147 | endfor |
| 148 | |
Bram Moolenaar | 00af60b | 2016-02-13 14:06:14 +0100 | [diff] [blame] | 149 | if s:fail == 0 |
Bram Moolenaar | 4334554 | 2015-11-29 17:35:35 +0100 | [diff] [blame] | 150 | " Success, create the .res file so that make knows it's done. |
Bram Moolenaar | 00af60b | 2016-02-13 14:06:14 +0100 | [diff] [blame] | 151 | exe 'split ' . fnamemodify(g:testname, ':r') . '.res' |
Bram Moolenaar | 4334554 | 2015-11-29 17:35:35 +0100 | [diff] [blame] | 152 | write |
| 153 | endif |
| 154 | |
Bram Moolenaar | 00af60b | 2016-02-13 14:06:14 +0100 | [diff] [blame] | 155 | if len(s:errors) > 0 |
Bram Moolenaar | 4334554 | 2015-11-29 17:35:35 +0100 | [diff] [blame] | 156 | " Append errors to test.log |
| 157 | split test.log |
| 158 | call append(line('$'), '') |
Bram Moolenaar | 00af60b | 2016-02-13 14:06:14 +0100 | [diff] [blame] | 159 | call append(line('$'), 'From ' . g:testname . ':') |
| 160 | call append(line('$'), s:errors) |
Bram Moolenaar | 4334554 | 2015-11-29 17:35:35 +0100 | [diff] [blame] | 161 | write |
| 162 | endif |
| 163 | |
Bram Moolenaar | 00af60b | 2016-02-13 14:06:14 +0100 | [diff] [blame] | 164 | let message = 'Executed ' . s:done . (s:done > 1 ? ' tests' : ' test') |
Bram Moolenaar | 096c8bb | 2015-12-29 14:26:57 +0100 | [diff] [blame] | 165 | echo message |
Bram Moolenaar | 00af60b | 2016-02-13 14:06:14 +0100 | [diff] [blame] | 166 | call add(s:messages, message) |
| 167 | if s:fail > 0 |
| 168 | let message = s:fail . ' FAILED:' |
Bram Moolenaar | 096c8bb | 2015-12-29 14:26:57 +0100 | [diff] [blame] | 169 | echo message |
Bram Moolenaar | 00af60b | 2016-02-13 14:06:14 +0100 | [diff] [blame] | 170 | call add(s:messages, message) |
| 171 | call extend(s:messages, s:errors) |
Bram Moolenaar | 4334554 | 2015-11-29 17:35:35 +0100 | [diff] [blame] | 172 | endif |
| 173 | |
Bram Moolenaar | 096c8bb | 2015-12-29 14:26:57 +0100 | [diff] [blame] | 174 | " Append messages to "messages" |
| 175 | split messages |
| 176 | call append(line('$'), '') |
Bram Moolenaar | 00af60b | 2016-02-13 14:06:14 +0100 | [diff] [blame] | 177 | call append(line('$'), 'From ' . g:testname . ':') |
| 178 | call append(line('$'), s:messages) |
Bram Moolenaar | 096c8bb | 2015-12-29 14:26:57 +0100 | [diff] [blame] | 179 | write |
| 180 | |
Bram Moolenaar | 4334554 | 2015-11-29 17:35:35 +0100 | [diff] [blame] | 181 | qall! |