Bram Moolenaar | 06fe74a | 2019-08-31 16:20:32 +0200 | [diff] [blame] | 1 | *testing.txt* For Vim version 8.1. Last change: 2019 Aug 21 |
Bram Moolenaar | ed997ad | 2019-07-21 16:42:00 +0200 | [diff] [blame] | 2 | |
| 3 | |
| 4 | VIM REFERENCE MANUAL by Bram Moolenaar |
| 5 | |
| 6 | |
| 7 | Testing Vim and Vim script *testing-support* |
| 8 | |
| 9 | Expression evaluation is explained in |eval.txt|. This file goes into details |
| 10 | about writing tests in Vim script. This can be used for testing Vim itself |
| 11 | and for testing plugins. |
| 12 | |
| 13 | 1. Testing Vim |testing| |
Bram Moolenaar | 5477506 | 2019-07-31 21:07:14 +0200 | [diff] [blame] | 14 | 2. Test functions |test-functions-details| |
| 15 | 3. Assert functions |assert-functions-details| |
Bram Moolenaar | ed997ad | 2019-07-21 16:42:00 +0200 | [diff] [blame] | 16 | |
| 17 | ============================================================================== |
| 18 | 1. Testing Vim *testing* |
| 19 | |
| 20 | Vim can be tested after building it, usually with "make test". |
| 21 | The tests are located in the directory "src/testdir". |
| 22 | |
| 23 | There are several types of tests added over time: |
| 24 | test33.in oldest, don't add any of these |
| 25 | test_something.in old style tests |
| 26 | test_something.vim new style tests |
| 27 | |
| 28 | *new-style-testing* |
| 29 | New tests should be added as new style tests. These use functions such as |
| 30 | |assert_equal()| to keep the test commands and the expected result in one |
| 31 | place. |
| 32 | *old-style-testing* |
| 33 | In some cases an old style test needs to be used. E.g. when testing Vim |
| 34 | without the |+eval| feature. |
| 35 | |
| 36 | Find more information in the file src/testdir/README.txt. |
| 37 | |
| 38 | ============================================================================== |
Bram Moolenaar | 5477506 | 2019-07-31 21:07:14 +0200 | [diff] [blame] | 39 | 2. Test functions *test-functions-details* |
Bram Moolenaar | ed997ad | 2019-07-21 16:42:00 +0200 | [diff] [blame] | 40 | |
| 41 | test_alloc_fail({id}, {countdown}, {repeat}) *test_alloc_fail()* |
| 42 | This is for testing: If the memory allocation with {id} is |
| 43 | called, then decrement {countdown}, and when it reaches zero |
| 44 | let memory allocation fail {repeat} times. When {repeat} is |
| 45 | smaller than one it fails one time. |
| 46 | |
| 47 | |
| 48 | test_autochdir() *test_autochdir()* |
| 49 | Set a flag to enable the effect of 'autochdir' before Vim |
| 50 | startup has finished. |
| 51 | |
| 52 | |
| 53 | test_feedinput({string}) *test_feedinput()* |
| 54 | Characters in {string} are queued for processing as if they |
| 55 | were typed by the user. This uses a low level input buffer. |
| 56 | This function works only when with |+unix| or GUI is running. |
| 57 | |
| 58 | |
| 59 | test_garbagecollect_now() *test_garbagecollect_now()* |
| 60 | Like garbagecollect(), but executed right away. This must |
| 61 | only be called directly to avoid any structure to exist |
| 62 | internally, and |v:testing| must have been set before calling |
| 63 | any function. |
| 64 | |
| 65 | |
| 66 | test_garbagecollect_soon() *test_garbagecollect_soon()* |
| 67 | Set the flag to call the garbagecollector as if in the main |
| 68 | loop. Only to be used in tests. |
| 69 | |
| 70 | |
| 71 | test_getvalue({name}) *test_getvalue()* |
| 72 | Get the value of an internal variable. These values for |
| 73 | {name} are supported: |
| 74 | need_fileinfo |
| 75 | |
| 76 | |
| 77 | test_ignore_error({expr}) *test_ignore_error()* |
| 78 | Ignore any error containing {expr}. A normal message is given |
| 79 | instead. |
| 80 | This is only meant to be used in tests, where catching the |
| 81 | error with try/catch cannot be used (because it skips over |
| 82 | following code). |
| 83 | {expr} is used literally, not as a pattern. |
| 84 | When the {expr} is the string "RESET" then the list of ignored |
| 85 | errors is made empty. |
| 86 | |
| 87 | |
| 88 | test_null_blob() *test_null_blob()* |
| 89 | Return a |Blob| that is null. Only useful for testing. |
| 90 | |
| 91 | |
| 92 | test_null_channel() *test_null_channel()* |
| 93 | Return a |Channel| that is null. Only useful for testing. |
| 94 | {only available when compiled with the +channel feature} |
| 95 | |
| 96 | |
| 97 | test_null_dict() *test_null_dict()* |
| 98 | Return a |Dict| that is null. Only useful for testing. |
| 99 | |
| 100 | |
| 101 | test_null_job() *test_null_job()* |
| 102 | Return a |Job| that is null. Only useful for testing. |
| 103 | {only available when compiled with the +job feature} |
| 104 | |
| 105 | |
| 106 | test_null_list() *test_null_list()* |
| 107 | Return a |List| that is null. Only useful for testing. |
| 108 | |
| 109 | |
| 110 | test_null_partial() *test_null_partial()* |
| 111 | Return a |Partial| that is null. Only useful for testing. |
| 112 | |
| 113 | |
| 114 | test_null_string() *test_null_string()* |
| 115 | Return a |String| that is null. Only useful for testing. |
| 116 | |
| 117 | |
| 118 | test_option_not_set({name}) *test_option_not_set()* |
| 119 | Reset the flag that indicates option {name} was set. Thus it |
| 120 | looks like it still has the default value. Use like this: > |
| 121 | set ambiwidth=double |
| 122 | call test_option_not_set('ambiwidth') |
| 123 | < Now the 'ambiwidth' option behaves like it was never changed, |
| 124 | even though the value is "double". |
| 125 | Only to be used for testing! |
| 126 | |
| 127 | |
| 128 | test_override({name}, {val}) *test_override()* |
| 129 | Overrides certain parts of Vim's internal processing to be able |
| 130 | to run tests. Only to be used for testing Vim! |
| 131 | The override is enabled when {val} is non-zero and removed |
| 132 | when {val} is zero. |
| 133 | Current supported values for name are: |
| 134 | |
| 135 | name effect when {val} is non-zero ~ |
| 136 | redraw disable the redrawing() function |
| 137 | redraw_flag ignore the RedrawingDisabled flag |
| 138 | char_avail disable the char_avail() function |
| 139 | starting reset the "starting" variable, see below |
| 140 | nfa_fail makes the NFA regexp engine fail to force a |
| 141 | fallback to the old engine |
| 142 | no_query_mouse do not query the mouse position for "dec" |
| 143 | terminals |
| 144 | no_wait_return set the "no_wait_return" flag. Not restored |
| 145 | with "ALL". |
| 146 | ALL clear all overrides ({val} is not used) |
| 147 | |
| 148 | "starting" is to be used when a test should behave like |
| 149 | startup was done. Since the tests are run by sourcing a |
| 150 | script the "starting" variable is non-zero. This is usually a |
| 151 | good thing (tests run faster), but sometimes changes behavior |
| 152 | in a way that the test doesn't work properly. |
| 153 | When using: > |
| 154 | call test_override('starting', 1) |
| 155 | < The value of "starting" is saved. It is restored by: > |
| 156 | call test_override('starting', 0) |
| 157 | |
| 158 | |
| 159 | test_refcount({expr}) *test_refcount()* |
| 160 | Return the reference count of {expr}. When {expr} is of a |
| 161 | type that does not have a reference count, returns -1. Only |
| 162 | to be used for testing. |
| 163 | |
| 164 | |
| 165 | test_scrollbar({which}, {value}, {dragging}) *test_scrollbar()* |
| 166 | Pretend using scrollbar {which} to move it to position |
| 167 | {value}. {which} can be: |
| 168 | left Left scrollbar of the current window |
| 169 | right Right scrollbar of the current window |
| 170 | hor Horizontal scrollbar |
| 171 | |
| 172 | For the vertical scrollbars {value} can be 1 to the |
| 173 | line-count of the buffer. For the horizontal scrollbar the |
| 174 | {value} can be between 1 and the maximum line length, assuming |
| 175 | 'wrap' is not set. |
| 176 | |
| 177 | When {dragging} is non-zero it's like dragging the scrollbar, |
| 178 | otherwise it's like clicking in the scrollbar. |
| 179 | Only works when the {which} scrollbar actually exists, |
| 180 | obviously only when using the GUI. |
| 181 | |
| 182 | |
| 183 | test_setmouse({row}, {col}) *test_setmouse()* |
| 184 | Set the mouse position to be used for the next mouse action. |
| 185 | {row} and {col} are one based. |
| 186 | For example: > |
| 187 | call test_setmouse(4, 20) |
| 188 | call feedkeys("\<LeftMouse>", "xt") |
| 189 | |
| 190 | |
| 191 | test_settime({expr}) *test_settime()* |
| 192 | Set the time Vim uses internally. Currently only used for |
| 193 | timestamps in the history, as they are used in viminfo, and |
| 194 | for undo. |
| 195 | Using a value of 1 makes Vim not sleep after a warning or |
| 196 | error message. |
| 197 | {expr} must evaluate to a number. When the value is zero the |
| 198 | normal behavior is restored. |
| 199 | |
| 200 | ============================================================================== |
Bram Moolenaar | 5477506 | 2019-07-31 21:07:14 +0200 | [diff] [blame] | 201 | 3. Assert functions *assert-functions-details* |
Bram Moolenaar | ed997ad | 2019-07-21 16:42:00 +0200 | [diff] [blame] | 202 | |
| 203 | |
| 204 | assert_beeps({cmd}) *assert_beeps()* |
| 205 | Run {cmd} and add an error message to |v:errors| if it does |
| 206 | NOT produce a beep or visual bell. |
| 207 | Also see |assert_fails()| and |assert-return|. |
| 208 | |
Bram Moolenaar | 24278d2 | 2019-08-16 21:49:22 +0200 | [diff] [blame] | 209 | Can also be used as a |method|: > |
| 210 | GetCmd()->assert_beeps() |
| 211 | < |
Bram Moolenaar | ed997ad | 2019-07-21 16:42:00 +0200 | [diff] [blame] | 212 | *assert_equal()* |
| 213 | assert_equal({expected}, {actual} [, {msg}]) |
| 214 | When {expected} and {actual} are not equal an error message is |
| 215 | added to |v:errors| and 1 is returned. Otherwise zero is |
| 216 | returned |assert-return|. |
| 217 | There is no automatic conversion, the String "4" is different |
| 218 | from the Number 4. And the number 4 is different from the |
| 219 | Float 4.0. The value of 'ignorecase' is not used here, case |
| 220 | always matters. |
| 221 | When {msg} is omitted an error in the form "Expected |
| 222 | {expected} but got {actual}" is produced. |
| 223 | Example: > |
| 224 | assert_equal('foo', 'bar') |
| 225 | < Will result in a string to be added to |v:errors|: |
| 226 | test.vim line 12: Expected 'foo' but got 'bar' ~ |
| 227 | |
Bram Moolenaar | 25e4223 | 2019-08-04 15:04:10 +0200 | [diff] [blame] | 228 | Can also be used as a |method|: > |
| 229 | mylist->assert_equal([1, 2, 3]) |
| 230 | |
| 231 | |
| 232 | < *assert_equalfile()* |
Bram Moolenaar | ed997ad | 2019-07-21 16:42:00 +0200 | [diff] [blame] | 233 | assert_equalfile({fname-one}, {fname-two}) |
| 234 | When the files {fname-one} and {fname-two} do not contain |
| 235 | exactly the same text an error message is added to |v:errors|. |
| 236 | Also see |assert-return|. |
| 237 | When {fname-one} or {fname-two} does not exist the error will |
| 238 | mention that. |
| 239 | Mainly useful with |terminal-diff|. |
| 240 | |
Bram Moolenaar | e49fbff | 2019-08-21 22:50:07 +0200 | [diff] [blame] | 241 | Can also be used as a |method|: > |
| 242 | GetLog()->assert_equalfile('expected.log') |
| 243 | |
| 244 | |
Bram Moolenaar | ed997ad | 2019-07-21 16:42:00 +0200 | [diff] [blame] | 245 | assert_exception({error} [, {msg}]) *assert_exception()* |
| 246 | When v:exception does not contain the string {error} an error |
| 247 | message is added to |v:errors|. Also see |assert-return|. |
| 248 | This can be used to assert that a command throws an exception. |
| 249 | Using the error number, followed by a colon, avoids problems |
| 250 | with translations: > |
| 251 | try |
| 252 | commandthatfails |
| 253 | call assert_false(1, 'command should have failed') |
| 254 | catch |
| 255 | call assert_exception('E492:') |
| 256 | endtry |
| 257 | |
| 258 | assert_fails({cmd} [, {error} [, {msg}]]) *assert_fails()* |
| 259 | Run {cmd} and add an error message to |v:errors| if it does |
| 260 | NOT produce an error. Also see |assert-return|. |
| 261 | When {error} is given it must match in |v:errmsg|. |
| 262 | Note that beeping is not considered an error, and some failing |
| 263 | commands only beep. Use |assert_beeps()| for those. |
| 264 | |
Bram Moolenaar | 24278d2 | 2019-08-16 21:49:22 +0200 | [diff] [blame] | 265 | Can also be used as a |method|: > |
| 266 | GetCmd()->assert_fails('E99:') |
| 267 | |
Bram Moolenaar | ed997ad | 2019-07-21 16:42:00 +0200 | [diff] [blame] | 268 | assert_false({actual} [, {msg}]) *assert_false()* |
| 269 | When {actual} is not false an error message is added to |
| 270 | |v:errors|, like with |assert_equal()|. |
| 271 | Also see |assert-return|. |
| 272 | A value is false when it is zero. When {actual} is not a |
| 273 | number the assert fails. |
| 274 | When {msg} is omitted an error in the form |
| 275 | "Expected False but got {actual}" is produced. |
| 276 | |
Bram Moolenaar | 24278d2 | 2019-08-16 21:49:22 +0200 | [diff] [blame] | 277 | Can also be used as a |method|: > |
| 278 | GetResult()->assert_false() |
| 279 | |
Bram Moolenaar | ed997ad | 2019-07-21 16:42:00 +0200 | [diff] [blame] | 280 | assert_inrange({lower}, {upper}, {actual} [, {msg}]) *assert_inrange()* |
| 281 | This asserts number and |Float| values. When {actual} is lower |
| 282 | than {lower} or higher than {upper} an error message is added |
| 283 | to |v:errors|. Also see |assert-return|. |
| 284 | When {msg} is omitted an error in the form |
| 285 | "Expected range {lower} - {upper}, but got {actual}" is |
| 286 | produced. |
| 287 | |
| 288 | *assert_match()* |
| 289 | assert_match({pattern}, {actual} [, {msg}]) |
| 290 | When {pattern} does not match {actual} an error message is |
| 291 | added to |v:errors|. Also see |assert-return|. |
| 292 | |
| 293 | {pattern} is used as with |=~|: The matching is always done |
| 294 | like 'magic' was set and 'cpoptions' is empty, no matter what |
| 295 | the actual value of 'magic' or 'cpoptions' is. |
| 296 | |
| 297 | {actual} is used as a string, automatic conversion applies. |
| 298 | Use "^" and "$" to match with the start and end of the text. |
| 299 | Use both to match the whole text. |
| 300 | |
| 301 | When {msg} is omitted an error in the form |
| 302 | "Pattern {pattern} does not match {actual}" is produced. |
| 303 | Example: > |
| 304 | assert_match('^f.*o$', 'foobar') |
| 305 | < Will result in a string to be added to |v:errors|: |
| 306 | test.vim line 12: Pattern '^f.*o$' does not match 'foobar' ~ |
| 307 | |
Bram Moolenaar | 24278d2 | 2019-08-16 21:49:22 +0200 | [diff] [blame] | 308 | Can also be used as a |method|: > |
| 309 | getFile()->assert_match('foo.*') |
| 310 | < |
Bram Moolenaar | ed997ad | 2019-07-21 16:42:00 +0200 | [diff] [blame] | 311 | *assert_notequal()* |
| 312 | assert_notequal({expected}, {actual} [, {msg}]) |
| 313 | The opposite of `assert_equal()`: add an error message to |
| 314 | |v:errors| when {expected} and {actual} are equal. |
| 315 | Also see |assert-return|. |
| 316 | |
Bram Moolenaar | 25e4223 | 2019-08-04 15:04:10 +0200 | [diff] [blame] | 317 | Can also be used as a |method|: > |
| 318 | mylist->assert_notequal([1, 2, 3]) |
| 319 | |
| 320 | < *assert_notmatch()* |
Bram Moolenaar | ed997ad | 2019-07-21 16:42:00 +0200 | [diff] [blame] | 321 | assert_notmatch({pattern}, {actual} [, {msg}]) |
| 322 | The opposite of `assert_match()`: add an error message to |
| 323 | |v:errors| when {pattern} matches {actual}. |
| 324 | Also see |assert-return|. |
| 325 | |
Bram Moolenaar | 24278d2 | 2019-08-16 21:49:22 +0200 | [diff] [blame] | 326 | Can also be used as a |method|: > |
| 327 | getFile()->assert_notmatch('bar.*') |
| 328 | |
Bram Moolenaar | e49fbff | 2019-08-21 22:50:07 +0200 | [diff] [blame] | 329 | |
Bram Moolenaar | ed997ad | 2019-07-21 16:42:00 +0200 | [diff] [blame] | 330 | assert_report({msg}) *assert_report()* |
| 331 | Report a test failure directly, using {msg}. |
| 332 | Always returns one. |
| 333 | |
Bram Moolenaar | e49fbff | 2019-08-21 22:50:07 +0200 | [diff] [blame] | 334 | Can also be used as a |method|: > |
| 335 | GetMessage()->assert_report() |
| 336 | |
| 337 | |
Bram Moolenaar | ed997ad | 2019-07-21 16:42:00 +0200 | [diff] [blame] | 338 | assert_true({actual} [, {msg}]) *assert_true()* |
| 339 | When {actual} is not true an error message is added to |
| 340 | |v:errors|, like with |assert_equal()|. |
| 341 | Also see |assert-return|. |
| 342 | A value is TRUE when it is a non-zero number. When {actual} |
| 343 | is not a number the assert fails. |
| 344 | When {msg} is omitted an error in the form "Expected True but |
| 345 | got {actual}" is produced. |
| 346 | |
Bram Moolenaar | 24278d2 | 2019-08-16 21:49:22 +0200 | [diff] [blame] | 347 | Can also be used as a |method|: > |
| 348 | GetResult()->assert_true() |
| 349 | < |
Bram Moolenaar | ed997ad | 2019-07-21 16:42:00 +0200 | [diff] [blame] | 350 | |
| 351 | vim:tw=78:ts=8:noet:ft=help:norl: |