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 | " |
| 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 Moolenaar | 4686b32 | 2015-12-28 14:44:10 +0100 | [diff] [blame] | 24 | so small.vim |
Bram Moolenaar | 4334554 | 2015-11-29 17:35:35 +0100 | [diff] [blame] | 25 | |
| 26 | " Check that the screen size is at least 24 x 80 characters. |
| 27 | if &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 |
| 34 | endif |
| 35 | |
Bram Moolenaar | c066246 | 2015-12-30 15:49:05 +0100 | [diff] [blame] | 36 | " For consistency run all tests with 'nocompatible' set. |
| 37 | " This also enables use of line continuation. |
| 38 | set nocp viminfo+=nviminfo |
| 39 | |
| 40 | " Avoid stopping at the "hit enter" prompt |
| 41 | set nomore |
| 42 | |
| 43 | " Output all messages in English. |
| 44 | lang mess C |
| 45 | |
Bram Moolenaar | 28fb79d | 2016-01-09 22:28:33 +0100 | [diff] [blame] | 46 | let s:srcdir = expand('%:p:h:h') |
| 47 | |
| 48 | " Support function: get the alloc ID by name. |
| 49 | function GetAllocId(name) |
| 50 | exe 'split ' . s:srcdir . '/alloc.h' |
| 51 | /typedef enum/ |
| 52 | let top = getline('.') |
| 53 | let lnum = search('aid_' . a:name . ',') |
| 54 | if lnum == 0 |
| 55 | call add(v:errors, 'Alloc ID ' . a:name . ' not defined') |
| 56 | endif |
| 57 | close |
| 58 | return lnum - top |
| 59 | endfunc |
| 60 | |
| 61 | |
Bram Moolenaar | 4334554 | 2015-11-29 17:35:35 +0100 | [diff] [blame] | 62 | " Source the test script. First grab the file name, in case the script |
| 63 | " navigates away. |
| 64 | let testname = expand('%') |
Bram Moolenaar | 4686b32 | 2015-12-28 14:44:10 +0100 | [diff] [blame] | 65 | let done = 0 |
| 66 | let fail = 0 |
| 67 | let errors = [] |
Bram Moolenaar | 096c8bb | 2015-12-29 14:26:57 +0100 | [diff] [blame] | 68 | let messages = [] |
Bram Moolenaar | a2cce86 | 2016-01-02 19:50:04 +0100 | [diff] [blame] | 69 | if expand('%') =~ 'test_viml.vim' |
| 70 | " this test has intentional errors, don't use try/catch. |
Bram Moolenaar | 4686b32 | 2015-12-28 14:44:10 +0100 | [diff] [blame] | 71 | source % |
Bram Moolenaar | a2cce86 | 2016-01-02 19:50:04 +0100 | [diff] [blame] | 72 | else |
| 73 | try |
| 74 | source % |
| 75 | catch |
| 76 | let fail += 1 |
| 77 | call add(errors, 'Caught exception: ' . v:exception . ' @ ' . v:throwpoint) |
| 78 | endtry |
| 79 | endif |
Bram Moolenaar | 4334554 | 2015-11-29 17:35:35 +0100 | [diff] [blame] | 80 | |
| 81 | " Locate Test_ functions and execute them. |
| 82 | redir @q |
| 83 | function /^Test_ |
| 84 | redir END |
| 85 | let tests = split(substitute(@q, 'function \(\k*()\)', '\1', 'g')) |
| 86 | |
Bram Moolenaar | cfc0a35 | 2016-01-09 20:23:00 +0100 | [diff] [blame] | 87 | " Execute the tests in alphabetical order. |
| 88 | for test in sort(tests) |
Bram Moolenaar | 4334554 | 2015-11-29 17:35:35 +0100 | [diff] [blame] | 89 | if exists("*SetUp") |
| 90 | call SetUp() |
| 91 | endif |
| 92 | |
Bram Moolenaar | 096c8bb | 2015-12-29 14:26:57 +0100 | [diff] [blame] | 93 | call add(messages, 'Executing ' . test) |
Bram Moolenaar | 4334554 | 2015-11-29 17:35:35 +0100 | [diff] [blame] | 94 | let done += 1 |
| 95 | try |
| 96 | exe 'call ' . test |
| 97 | catch |
| 98 | let fail += 1 |
| 99 | call add(v:errors, 'Caught exception in ' . test . ': ' . v:exception . ' @ ' . v:throwpoint) |
| 100 | endtry |
| 101 | |
| 102 | if len(v:errors) > 0 |
| 103 | let fail += 1 |
| 104 | call add(errors, 'Found errors in ' . test . ':') |
| 105 | call extend(errors, v:errors) |
| 106 | let v:errors = [] |
| 107 | endif |
| 108 | |
| 109 | if exists("*TearDown") |
| 110 | call TearDown() |
| 111 | endif |
| 112 | endfor |
| 113 | |
| 114 | if fail == 0 |
| 115 | " Success, create the .res file so that make knows it's done. |
Bram Moolenaar | de0ad40 | 2015-12-03 17:21:28 +0100 | [diff] [blame] | 116 | exe 'split ' . fnamemodify(testname, ':r') . '.res' |
Bram Moolenaar | 4334554 | 2015-11-29 17:35:35 +0100 | [diff] [blame] | 117 | write |
| 118 | endif |
| 119 | |
| 120 | if len(errors) > 0 |
| 121 | " Append errors to test.log |
| 122 | split test.log |
| 123 | call append(line('$'), '') |
| 124 | call append(line('$'), 'From ' . testname . ':') |
| 125 | call append(line('$'), errors) |
| 126 | write |
| 127 | endif |
| 128 | |
Bram Moolenaar | 096c8bb | 2015-12-29 14:26:57 +0100 | [diff] [blame] | 129 | let message = 'Executed ' . done . (done > 1 ? ' tests': ' test') |
| 130 | echo message |
| 131 | call add(messages, message) |
Bram Moolenaar | 4334554 | 2015-11-29 17:35:35 +0100 | [diff] [blame] | 132 | if fail > 0 |
Bram Moolenaar | 096c8bb | 2015-12-29 14:26:57 +0100 | [diff] [blame] | 133 | let message = fail . ' FAILED' |
| 134 | echo message |
| 135 | call add(messages, message) |
Bram Moolenaar | 4334554 | 2015-11-29 17:35:35 +0100 | [diff] [blame] | 136 | endif |
| 137 | |
Bram Moolenaar | 096c8bb | 2015-12-29 14:26:57 +0100 | [diff] [blame] | 138 | " Append messages to "messages" |
| 139 | split messages |
| 140 | call append(line('$'), '') |
| 141 | call append(line('$'), 'From ' . testname . ':') |
| 142 | call append(line('$'), messages) |
| 143 | write |
| 144 | |
Bram Moolenaar | 4334554 | 2015-11-29 17:35:35 +0100 | [diff] [blame] | 145 | qall! |