Bram Moolenaar | fd31be2 | 2022-01-16 14:46:06 +0000 | [diff] [blame] | 1 | *testing.txt* For Vim version 8.2. Last change: 2022 Jan 13 |
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 | |
Bram Moolenaar | b96a32e | 2020-08-13 18:59:55 +0200 | [diff] [blame] | 23 | There are two types of tests added over time: |
| 24 | test20.in oldest, only for tiny and small builds |
| 25 | test_something.vim new style tests |
| 26 | |
Bram Moolenaar | ed997ad | 2019-07-21 16:42:00 +0200 | [diff] [blame] | 27 | *new-style-testing* |
Bram Moolenaar | f7c4d83 | 2020-08-11 20:42:19 +0200 | [diff] [blame] | 28 | New tests should be added as new style tests. The test scripts are named |
| 29 | test_<feature>.vim (replace <feature> with the feature under test). These use |
| 30 | functions such as |assert_equal()| to keep the test commands and the expected |
| 31 | result in one place. |
Bram Moolenaar | b96a32e | 2020-08-13 18:59:55 +0200 | [diff] [blame] | 32 | *old-style-testing* |
| 33 | These tests are used only for testing Vim without the |+eval| feature. |
Bram Moolenaar | ed997ad | 2019-07-21 16:42:00 +0200 | [diff] [blame] | 34 | |
| 35 | Find more information in the file src/testdir/README.txt. |
| 36 | |
| 37 | ============================================================================== |
Bram Moolenaar | 5477506 | 2019-07-31 21:07:14 +0200 | [diff] [blame] | 38 | 2. Test functions *test-functions-details* |
Bram Moolenaar | ed997ad | 2019-07-21 16:42:00 +0200 | [diff] [blame] | 39 | |
| 40 | test_alloc_fail({id}, {countdown}, {repeat}) *test_alloc_fail()* |
| 41 | This is for testing: If the memory allocation with {id} is |
| 42 | called, then decrement {countdown}, and when it reaches zero |
| 43 | let memory allocation fail {repeat} times. When {repeat} is |
| 44 | smaller than one it fails one time. |
| 45 | |
Bram Moolenaar | ce90e36 | 2019-09-08 18:58:44 +0200 | [diff] [blame] | 46 | Can also be used as a |method|: > |
| 47 | GetAllocId()->test_alloc_fail() |
Bram Moolenaar | ed997ad | 2019-07-21 16:42:00 +0200 | [diff] [blame] | 48 | |
Bram Moolenaar | 89a9c15 | 2021-08-29 21:55:35 +0200 | [diff] [blame] | 49 | |
Bram Moolenaar | ed997ad | 2019-07-21 16:42:00 +0200 | [diff] [blame] | 50 | test_autochdir() *test_autochdir()* |
| 51 | Set a flag to enable the effect of 'autochdir' before Vim |
| 52 | startup has finished. |
| 53 | |
| 54 | |
| 55 | test_feedinput({string}) *test_feedinput()* |
| 56 | Characters in {string} are queued for processing as if they |
| 57 | were typed by the user. This uses a low level input buffer. |
| 58 | This function works only when with |+unix| or GUI is running. |
| 59 | |
Bram Moolenaar | ce90e36 | 2019-09-08 18:58:44 +0200 | [diff] [blame] | 60 | Can also be used as a |method|: > |
| 61 | GetText()->test_feedinput() |
Bram Moolenaar | ed997ad | 2019-07-21 16:42:00 +0200 | [diff] [blame] | 62 | |
Bram Moolenaar | 89a9c15 | 2021-08-29 21:55:35 +0200 | [diff] [blame] | 63 | |
Bram Moolenaar | ed997ad | 2019-07-21 16:42:00 +0200 | [diff] [blame] | 64 | test_garbagecollect_now() *test_garbagecollect_now()* |
| 65 | Like garbagecollect(), but executed right away. This must |
| 66 | only be called directly to avoid any structure to exist |
| 67 | internally, and |v:testing| must have been set before calling |
Bram Moolenaar | 0e6adf8 | 2021-12-16 14:41:10 +0000 | [diff] [blame] | 68 | any function. This will not work when called from a :def |
| 69 | function, because variables on the stack will be freed. |
Bram Moolenaar | ed997ad | 2019-07-21 16:42:00 +0200 | [diff] [blame] | 70 | |
| 71 | |
| 72 | test_garbagecollect_soon() *test_garbagecollect_soon()* |
| 73 | Set the flag to call the garbagecollector as if in the main |
| 74 | loop. Only to be used in tests. |
| 75 | |
| 76 | |
| 77 | test_getvalue({name}) *test_getvalue()* |
| 78 | Get the value of an internal variable. These values for |
| 79 | {name} are supported: |
| 80 | need_fileinfo |
| 81 | |
Bram Moolenaar | ce90e36 | 2019-09-08 18:58:44 +0200 | [diff] [blame] | 82 | Can also be used as a |method|: > |
| 83 | GetName()->test_getvalue() |
Yegappan Lakshmanan | f1e7449 | 2021-06-21 18:44:26 +0200 | [diff] [blame] | 84 | < |
Yegappan Lakshmanan | 18d4658 | 2021-06-23 20:46:52 +0200 | [diff] [blame] | 85 | *test_gui_drop_files()* |
| 86 | test_gui_drop_files({list}, {row}, {col}, {mods}) |
| 87 | Drop one or more files in {list} in the window at {row}, {col}. |
Bram Moolenaar | 90df4b9 | 2021-07-07 20:26:08 +0200 | [diff] [blame] | 88 | This function only works when the GUI is running and the |
Bram Moolenaar | 2286304 | 2021-10-16 15:23:36 +0100 | [diff] [blame] | 89 | |drop_file| feature is present. |
| 90 | |
Yegappan Lakshmanan | 18d4658 | 2021-06-23 20:46:52 +0200 | [diff] [blame] | 91 | The supported values for {mods} are: |
| 92 | 0x4 Shift |
| 93 | 0x8 Alt |
| 94 | 0x10 Ctrl |
Bram Moolenaar | 2286304 | 2021-10-16 15:23:36 +0100 | [diff] [blame] | 95 | The files are added to the |argument-list| and the first file |
| 96 | in {list} is edited in the window. See |drag-n-drop| for more |
Yegappan Lakshmanan | 18d4658 | 2021-06-23 20:46:52 +0200 | [diff] [blame] | 97 | information. |
| 98 | |
Yegappan Lakshmanan | f1e7449 | 2021-06-21 18:44:26 +0200 | [diff] [blame] | 99 | *test_gui_mouse_event()* |
| 100 | test_gui_mouse_event({button}, {row}, {col}, {multiclick}, {modifiers}) |
Yegappan Lakshmanan | 18d4658 | 2021-06-23 20:46:52 +0200 | [diff] [blame] | 101 | Inject a mouse button click event. This function only works |
| 102 | when the GUI is running. |
Yegappan Lakshmanan | f1e7449 | 2021-06-21 18:44:26 +0200 | [diff] [blame] | 103 | The supported values for {button} are: |
| 104 | 0 right mouse button |
| 105 | 1 middle mouse button |
| 106 | 2 left mouse button |
| 107 | 3 mouse button release |
| 108 | 4 scroll wheel down |
| 109 | 5 scroll wheel up |
| 110 | 6 scroll wheel left |
| 111 | 7 scroll wheel right |
Yegappan Lakshmanan | 18d4658 | 2021-06-23 20:46:52 +0200 | [diff] [blame] | 112 | {row} and {col} specify the location of the mouse click. The |
| 113 | first row of the Vim window is 1 and the last row is 'lines'. |
| 114 | The maximum value of {col} is 'columns'. |
Yegappan Lakshmanan | f1e7449 | 2021-06-21 18:44:26 +0200 | [diff] [blame] | 115 | To inject a multiclick event, set {multiclick} to 1. |
| 116 | The supported values for {modifiers} are: |
| 117 | 4 shift is pressed |
| 118 | 8 alt is pressed |
| 119 | 16 ctrl is pressed |
| 120 | After injecting the mouse event you probably should call |
| 121 | |feedkeys()| to have them processed, e.g.: > |
| 122 | call feedkeys("y", 'Lx!') |
| 123 | |
Bram Moolenaar | ed997ad | 2019-07-21 16:42:00 +0200 | [diff] [blame] | 124 | |
| 125 | test_ignore_error({expr}) *test_ignore_error()* |
| 126 | Ignore any error containing {expr}. A normal message is given |
| 127 | instead. |
| 128 | This is only meant to be used in tests, where catching the |
| 129 | error with try/catch cannot be used (because it skips over |
| 130 | following code). |
| 131 | {expr} is used literally, not as a pattern. |
| 132 | When the {expr} is the string "RESET" then the list of ignored |
| 133 | errors is made empty. |
| 134 | |
Bram Moolenaar | ce90e36 | 2019-09-08 18:58:44 +0200 | [diff] [blame] | 135 | Can also be used as a |method|: > |
| 136 | GetErrorText()->test_ignore_error() |
Bram Moolenaar | ed997ad | 2019-07-21 16:42:00 +0200 | [diff] [blame] | 137 | |
Bram Moolenaar | 89a9c15 | 2021-08-29 21:55:35 +0200 | [diff] [blame] | 138 | |
Bram Moolenaar | ed997ad | 2019-07-21 16:42:00 +0200 | [diff] [blame] | 139 | test_null_blob() *test_null_blob()* |
| 140 | Return a |Blob| that is null. Only useful for testing. |
| 141 | |
| 142 | |
| 143 | test_null_channel() *test_null_channel()* |
| 144 | Return a |Channel| that is null. Only useful for testing. |
| 145 | {only available when compiled with the +channel feature} |
| 146 | |
| 147 | |
| 148 | test_null_dict() *test_null_dict()* |
| 149 | Return a |Dict| that is null. Only useful for testing. |
| 150 | |
| 151 | |
Bram Moolenaar | e69f6d0 | 2020-04-01 22:11:01 +0200 | [diff] [blame] | 152 | test_null_function() *test_null_function()* |
Bram Moolenaar | d1caa94 | 2020-04-10 22:10:56 +0200 | [diff] [blame] | 153 | Return a |Funcref| that is null. Only useful for testing. |
Bram Moolenaar | e69f6d0 | 2020-04-01 22:11:01 +0200 | [diff] [blame] | 154 | |
| 155 | |
Bram Moolenaar | ed997ad | 2019-07-21 16:42:00 +0200 | [diff] [blame] | 156 | test_null_job() *test_null_job()* |
| 157 | Return a |Job| that is null. Only useful for testing. |
| 158 | {only available when compiled with the +job feature} |
| 159 | |
| 160 | |
| 161 | test_null_list() *test_null_list()* |
| 162 | Return a |List| that is null. Only useful for testing. |
| 163 | |
| 164 | |
| 165 | test_null_partial() *test_null_partial()* |
| 166 | Return a |Partial| that is null. Only useful for testing. |
| 167 | |
| 168 | |
| 169 | test_null_string() *test_null_string()* |
| 170 | Return a |String| that is null. Only useful for testing. |
| 171 | |
| 172 | |
| 173 | test_option_not_set({name}) *test_option_not_set()* |
| 174 | Reset the flag that indicates option {name} was set. Thus it |
| 175 | looks like it still has the default value. Use like this: > |
| 176 | set ambiwidth=double |
| 177 | call test_option_not_set('ambiwidth') |
| 178 | < Now the 'ambiwidth' option behaves like it was never changed, |
| 179 | even though the value is "double". |
| 180 | Only to be used for testing! |
| 181 | |
Bram Moolenaar | ce90e36 | 2019-09-08 18:58:44 +0200 | [diff] [blame] | 182 | Can also be used as a |method|: > |
| 183 | GetOptionName()->test_option_not_set() |
| 184 | |
Bram Moolenaar | ed997ad | 2019-07-21 16:42:00 +0200 | [diff] [blame] | 185 | |
| 186 | test_override({name}, {val}) *test_override()* |
| 187 | Overrides certain parts of Vim's internal processing to be able |
| 188 | to run tests. Only to be used for testing Vim! |
| 189 | The override is enabled when {val} is non-zero and removed |
| 190 | when {val} is zero. |
Bram Moolenaar | 3e4fa3d | 2022-01-13 22:05:09 +0000 | [diff] [blame] | 191 | Current supported values for {name} are: |
Bram Moolenaar | ed997ad | 2019-07-21 16:42:00 +0200 | [diff] [blame] | 192 | |
Bram Moolenaar | 3e4fa3d | 2022-01-13 22:05:09 +0000 | [diff] [blame] | 193 | {name} effect when {val} is non-zero ~ |
| 194 | autoload `import autoload` will load the script right |
| 195 | away, not postponed until an item is used |
Bram Moolenaar | ed997ad | 2019-07-21 16:42:00 +0200 | [diff] [blame] | 196 | char_avail disable the char_avail() function |
Bram Moolenaar | ed997ad | 2019-07-21 16:42:00 +0200 | [diff] [blame] | 197 | nfa_fail makes the NFA regexp engine fail to force a |
| 198 | fallback to the old engine |
| 199 | no_query_mouse do not query the mouse position for "dec" |
| 200 | terminals |
| 201 | no_wait_return set the "no_wait_return" flag. Not restored |
| 202 | with "ALL". |
Bram Moolenaar | 3e4fa3d | 2022-01-13 22:05:09 +0000 | [diff] [blame] | 203 | redraw disable the redrawing() function |
| 204 | redraw_flag ignore the RedrawingDisabled flag |
| 205 | starting reset the "starting" variable, see below |
Bram Moolenaar | 0c0eddd | 2020-06-13 15:47:25 +0200 | [diff] [blame] | 206 | term_props reset all terminal properties when the version |
| 207 | string is detected |
Bram Moolenaar | 3e4fa3d | 2022-01-13 22:05:09 +0000 | [diff] [blame] | 208 | ui_delay time in msec to use in ui_delay(); overrules a |
| 209 | wait time of up to 3 seconds for messages |
Bram Moolenaar | c8cdf0f | 2021-03-13 13:28:13 +0100 | [diff] [blame] | 210 | uptime overrules sysinfo.uptime |
Bram Moolenaar | ed997ad | 2019-07-21 16:42:00 +0200 | [diff] [blame] | 211 | ALL clear all overrides ({val} is not used) |
| 212 | |
| 213 | "starting" is to be used when a test should behave like |
| 214 | startup was done. Since the tests are run by sourcing a |
| 215 | script the "starting" variable is non-zero. This is usually a |
| 216 | good thing (tests run faster), but sometimes changes behavior |
| 217 | in a way that the test doesn't work properly. |
| 218 | When using: > |
| 219 | call test_override('starting', 1) |
| 220 | < The value of "starting" is saved. It is restored by: > |
| 221 | call test_override('starting', 0) |
| 222 | |
Bram Moolenaar | ce90e36 | 2019-09-08 18:58:44 +0200 | [diff] [blame] | 223 | < Can also be used as a |method|: > |
| 224 | GetOverrideVal()-> test_override('starting') |
Bram Moolenaar | ed997ad | 2019-07-21 16:42:00 +0200 | [diff] [blame] | 225 | |
Bram Moolenaar | 89a9c15 | 2021-08-29 21:55:35 +0200 | [diff] [blame] | 226 | |
Bram Moolenaar | ed997ad | 2019-07-21 16:42:00 +0200 | [diff] [blame] | 227 | test_refcount({expr}) *test_refcount()* |
| 228 | Return the reference count of {expr}. When {expr} is of a |
| 229 | type that does not have a reference count, returns -1. Only |
| 230 | to be used for testing. |
| 231 | |
Bram Moolenaar | ce90e36 | 2019-09-08 18:58:44 +0200 | [diff] [blame] | 232 | Can also be used as a |method|: > |
| 233 | GetVarname()->test_refcount() |
| 234 | |
Bram Moolenaar | ed997ad | 2019-07-21 16:42:00 +0200 | [diff] [blame] | 235 | |
| 236 | test_scrollbar({which}, {value}, {dragging}) *test_scrollbar()* |
| 237 | Pretend using scrollbar {which} to move it to position |
| 238 | {value}. {which} can be: |
| 239 | left Left scrollbar of the current window |
| 240 | right Right scrollbar of the current window |
| 241 | hor Horizontal scrollbar |
| 242 | |
| 243 | For the vertical scrollbars {value} can be 1 to the |
| 244 | line-count of the buffer. For the horizontal scrollbar the |
| 245 | {value} can be between 1 and the maximum line length, assuming |
| 246 | 'wrap' is not set. |
| 247 | |
| 248 | When {dragging} is non-zero it's like dragging the scrollbar, |
| 249 | otherwise it's like clicking in the scrollbar. |
| 250 | Only works when the {which} scrollbar actually exists, |
| 251 | obviously only when using the GUI. |
| 252 | |
Bram Moolenaar | ce90e36 | 2019-09-08 18:58:44 +0200 | [diff] [blame] | 253 | Can also be used as a |method|: > |
| 254 | GetValue()->test_scrollbar('right', 0) |
Bram Moolenaar | ed997ad | 2019-07-21 16:42:00 +0200 | [diff] [blame] | 255 | |
Bram Moolenaar | 89a9c15 | 2021-08-29 21:55:35 +0200 | [diff] [blame] | 256 | |
Bram Moolenaar | ed997ad | 2019-07-21 16:42:00 +0200 | [diff] [blame] | 257 | test_setmouse({row}, {col}) *test_setmouse()* |
| 258 | Set the mouse position to be used for the next mouse action. |
| 259 | {row} and {col} are one based. |
| 260 | For example: > |
| 261 | call test_setmouse(4, 20) |
| 262 | call feedkeys("\<LeftMouse>", "xt") |
| 263 | |
Bram Moolenaar | 89a9c15 | 2021-08-29 21:55:35 +0200 | [diff] [blame] | 264 | |
Bram Moolenaar | ed997ad | 2019-07-21 16:42:00 +0200 | [diff] [blame] | 265 | test_settime({expr}) *test_settime()* |
| 266 | Set the time Vim uses internally. Currently only used for |
| 267 | timestamps in the history, as they are used in viminfo, and |
| 268 | for undo. |
| 269 | Using a value of 1 makes Vim not sleep after a warning or |
| 270 | error message. |
| 271 | {expr} must evaluate to a number. When the value is zero the |
| 272 | normal behavior is restored. |
| 273 | |
Bram Moolenaar | ce90e36 | 2019-09-08 18:58:44 +0200 | [diff] [blame] | 274 | Can also be used as a |method|: > |
| 275 | GetTime()->test_settime() |
| 276 | |
Bram Moolenaar | 89a9c15 | 2021-08-29 21:55:35 +0200 | [diff] [blame] | 277 | |
Bram Moolenaar | 4f645c5 | 2020-02-08 16:40:39 +0100 | [diff] [blame] | 278 | test_srand_seed([seed]) *test_srand_seed()* |
| 279 | When [seed] is given this sets the seed value used by |
| 280 | `srand()`. When omitted the test seed is removed. |
| 281 | |
Bram Moolenaar | 89a9c15 | 2021-08-29 21:55:35 +0200 | [diff] [blame] | 282 | |
| 283 | test_unknown() *test_unknown()* |
| 284 | Return a value with unknown type. Only useful for testing. |
| 285 | |
| 286 | |
| 287 | test_void() *test_void()* |
| 288 | Return a value with void type. Only useful for testing. |
| 289 | |
Bram Moolenaar | ed997ad | 2019-07-21 16:42:00 +0200 | [diff] [blame] | 290 | ============================================================================== |
Bram Moolenaar | 5477506 | 2019-07-31 21:07:14 +0200 | [diff] [blame] | 291 | 3. Assert functions *assert-functions-details* |
Bram Moolenaar | ed997ad | 2019-07-21 16:42:00 +0200 | [diff] [blame] | 292 | |
| 293 | |
| 294 | assert_beeps({cmd}) *assert_beeps()* |
| 295 | Run {cmd} and add an error message to |v:errors| if it does |
| 296 | NOT produce a beep or visual bell. |
Bram Moolenaar | 5b8cabf | 2021-04-02 18:55:57 +0200 | [diff] [blame] | 297 | Also see |assert_fails()|, |assert_nobeep()| and |
| 298 | |assert-return|. |
Bram Moolenaar | ed997ad | 2019-07-21 16:42:00 +0200 | [diff] [blame] | 299 | |
Bram Moolenaar | 24278d2 | 2019-08-16 21:49:22 +0200 | [diff] [blame] | 300 | Can also be used as a |method|: > |
| 301 | GetCmd()->assert_beeps() |
| 302 | < |
Bram Moolenaar | ed997ad | 2019-07-21 16:42:00 +0200 | [diff] [blame] | 303 | *assert_equal()* |
| 304 | assert_equal({expected}, {actual} [, {msg}]) |
| 305 | When {expected} and {actual} are not equal an error message is |
| 306 | added to |v:errors| and 1 is returned. Otherwise zero is |
| 307 | returned |assert-return|. |
| 308 | There is no automatic conversion, the String "4" is different |
| 309 | from the Number 4. And the number 4 is different from the |
| 310 | Float 4.0. The value of 'ignorecase' is not used here, case |
| 311 | always matters. |
| 312 | When {msg} is omitted an error in the form "Expected |
| 313 | {expected} but got {actual}" is produced. |
| 314 | Example: > |
| 315 | assert_equal('foo', 'bar') |
| 316 | < Will result in a string to be added to |v:errors|: |
| 317 | test.vim line 12: Expected 'foo' but got 'bar' ~ |
| 318 | |
Bram Moolenaar | 7ff7846 | 2020-07-10 22:00:53 +0200 | [diff] [blame] | 319 | Can also be used as a |method|, the base is passed as the |
| 320 | second argument: > |
Bram Moolenaar | 25e4223 | 2019-08-04 15:04:10 +0200 | [diff] [blame] | 321 | mylist->assert_equal([1, 2, 3]) |
| 322 | |
Bram Moolenaar | 25e4223 | 2019-08-04 15:04:10 +0200 | [diff] [blame] | 323 | < *assert_equalfile()* |
Bram Moolenaar | fb517ba | 2020-06-03 19:55:35 +0200 | [diff] [blame] | 324 | assert_equalfile({fname-one}, {fname-two} [, {msg}]) |
Bram Moolenaar | ed997ad | 2019-07-21 16:42:00 +0200 | [diff] [blame] | 325 | When the files {fname-one} and {fname-two} do not contain |
| 326 | exactly the same text an error message is added to |v:errors|. |
| 327 | Also see |assert-return|. |
| 328 | When {fname-one} or {fname-two} does not exist the error will |
| 329 | mention that. |
| 330 | Mainly useful with |terminal-diff|. |
| 331 | |
Bram Moolenaar | e49fbff | 2019-08-21 22:50:07 +0200 | [diff] [blame] | 332 | Can also be used as a |method|: > |
| 333 | GetLog()->assert_equalfile('expected.log') |
| 334 | |
Bram Moolenaar | ed997ad | 2019-07-21 16:42:00 +0200 | [diff] [blame] | 335 | assert_exception({error} [, {msg}]) *assert_exception()* |
| 336 | When v:exception does not contain the string {error} an error |
| 337 | message is added to |v:errors|. Also see |assert-return|. |
| 338 | This can be used to assert that a command throws an exception. |
| 339 | Using the error number, followed by a colon, avoids problems |
| 340 | with translations: > |
| 341 | try |
| 342 | commandthatfails |
| 343 | call assert_false(1, 'command should have failed') |
| 344 | catch |
| 345 | call assert_exception('E492:') |
| 346 | endtry |
Bram Moolenaar | 1d63454 | 2020-08-18 13:41:50 +0200 | [diff] [blame] | 347 | < |
| 348 | *assert_fails()* |
Bram Moolenaar | 9bd5d87 | 2020-09-06 21:47:48 +0200 | [diff] [blame] | 349 | assert_fails({cmd} [, {error} [, {msg} [, {lnum} [, {context}]]]]) |
Bram Moolenaar | ed997ad | 2019-07-21 16:42:00 +0200 | [diff] [blame] | 350 | Run {cmd} and add an error message to |v:errors| if it does |
Bram Moolenaar | 9b7bf9e | 2020-07-11 22:14:59 +0200 | [diff] [blame] | 351 | NOT produce an error or when {error} is not found in the |
| 352 | error message. Also see |assert-return|. |
| 353 | |
| 354 | When {error} is a string it must be found literally in the |
| 355 | first reported error. Most often this will be the error code, |
| 356 | including the colon, e.g. "E123:". > |
| 357 | assert_fails('bad cmd', 'E987:') |
| 358 | < |
| 359 | When {error} is a |List| with one or two strings, these are |
| 360 | used as patterns. The first pattern is matched against the |
| 361 | first reported error: > |
| 362 | assert_fails('cmd', ['E987:.*expected bool']) |
| 363 | < The second pattern, if present, is matched against the last |
Bram Moolenaar | 4072ba5 | 2020-12-23 13:56:35 +0100 | [diff] [blame] | 364 | reported error. |
| 365 | If there is only one error then both patterns must match. This |
| 366 | can be used to check that there is only one error. |
| 367 | To only match the last error use an empty string for the first |
| 368 | error: > |
Bram Moolenaar | 9b7bf9e | 2020-07-11 22:14:59 +0200 | [diff] [blame] | 369 | assert_fails('cmd', ['', 'E987:']) |
| 370 | < |
Bram Moolenaar | 1d63454 | 2020-08-18 13:41:50 +0200 | [diff] [blame] | 371 | If {msg} is empty then it is not used. Do this to get the |
| 372 | default message when passing the {lnum} argument. |
| 373 | |
| 374 | When {lnum} is present and not negative, and the {error} |
| 375 | argument is present and matches, then this is compared with |
| 376 | the line number at which the error was reported. That can be |
| 377 | the line number in a function or in a script. |
| 378 | |
Bram Moolenaar | 9bd5d87 | 2020-09-06 21:47:48 +0200 | [diff] [blame] | 379 | When {context} is present it is used as a pattern and matched |
| 380 | against the context (script name or function name) where |
| 381 | {lnum} is located in. |
| 382 | |
Bram Moolenaar | ed997ad | 2019-07-21 16:42:00 +0200 | [diff] [blame] | 383 | Note that beeping is not considered an error, and some failing |
| 384 | commands only beep. Use |assert_beeps()| for those. |
| 385 | |
Bram Moolenaar | 24278d2 | 2019-08-16 21:49:22 +0200 | [diff] [blame] | 386 | Can also be used as a |method|: > |
| 387 | GetCmd()->assert_fails('E99:') |
| 388 | |
Bram Moolenaar | 1d63454 | 2020-08-18 13:41:50 +0200 | [diff] [blame] | 389 | assert_false({actual} [, {msg}]) *assert_false()* |
Bram Moolenaar | ed997ad | 2019-07-21 16:42:00 +0200 | [diff] [blame] | 390 | When {actual} is not false an error message is added to |
| 391 | |v:errors|, like with |assert_equal()|. |
| 392 | Also see |assert-return|. |
| 393 | A value is false when it is zero. When {actual} is not a |
| 394 | number the assert fails. |
| 395 | When {msg} is omitted an error in the form |
| 396 | "Expected False but got {actual}" is produced. |
| 397 | |
Bram Moolenaar | 24278d2 | 2019-08-16 21:49:22 +0200 | [diff] [blame] | 398 | Can also be used as a |method|: > |
| 399 | GetResult()->assert_false() |
| 400 | |
Bram Moolenaar | ed997ad | 2019-07-21 16:42:00 +0200 | [diff] [blame] | 401 | assert_inrange({lower}, {upper}, {actual} [, {msg}]) *assert_inrange()* |
| 402 | This asserts number and |Float| values. When {actual} is lower |
| 403 | than {lower} or higher than {upper} an error message is added |
| 404 | to |v:errors|. Also see |assert-return|. |
| 405 | When {msg} is omitted an error in the form |
| 406 | "Expected range {lower} - {upper}, but got {actual}" is |
| 407 | produced. |
| 408 | |
| 409 | *assert_match()* |
| 410 | assert_match({pattern}, {actual} [, {msg}]) |
| 411 | When {pattern} does not match {actual} an error message is |
| 412 | added to |v:errors|. Also see |assert-return|. |
| 413 | |
| 414 | {pattern} is used as with |=~|: The matching is always done |
| 415 | like 'magic' was set and 'cpoptions' is empty, no matter what |
| 416 | the actual value of 'magic' or 'cpoptions' is. |
| 417 | |
| 418 | {actual} is used as a string, automatic conversion applies. |
| 419 | Use "^" and "$" to match with the start and end of the text. |
| 420 | Use both to match the whole text. |
| 421 | |
| 422 | When {msg} is omitted an error in the form |
| 423 | "Pattern {pattern} does not match {actual}" is produced. |
| 424 | Example: > |
| 425 | assert_match('^f.*o$', 'foobar') |
| 426 | < Will result in a string to be added to |v:errors|: |
| 427 | test.vim line 12: Pattern '^f.*o$' does not match 'foobar' ~ |
| 428 | |
Bram Moolenaar | 24278d2 | 2019-08-16 21:49:22 +0200 | [diff] [blame] | 429 | Can also be used as a |method|: > |
| 430 | getFile()->assert_match('foo.*') |
| 431 | < |
Bram Moolenaar | 5b8cabf | 2021-04-02 18:55:57 +0200 | [diff] [blame] | 432 | assert_nobeep({cmd}) *assert_nobeep()* |
| 433 | Run {cmd} and add an error message to |v:errors| if it |
| 434 | produces a beep or visual bell. |
| 435 | Also see |assert_beeps()|. |
| 436 | |
| 437 | Can also be used as a |method|: > |
| 438 | GetCmd()->assert_nobeep() |
| 439 | < |
Bram Moolenaar | ed997ad | 2019-07-21 16:42:00 +0200 | [diff] [blame] | 440 | *assert_notequal()* |
| 441 | assert_notequal({expected}, {actual} [, {msg}]) |
| 442 | The opposite of `assert_equal()`: add an error message to |
| 443 | |v:errors| when {expected} and {actual} are equal. |
| 444 | Also see |assert-return|. |
| 445 | |
Bram Moolenaar | 25e4223 | 2019-08-04 15:04:10 +0200 | [diff] [blame] | 446 | Can also be used as a |method|: > |
| 447 | mylist->assert_notequal([1, 2, 3]) |
| 448 | |
| 449 | < *assert_notmatch()* |
Bram Moolenaar | ed997ad | 2019-07-21 16:42:00 +0200 | [diff] [blame] | 450 | assert_notmatch({pattern}, {actual} [, {msg}]) |
| 451 | The opposite of `assert_match()`: add an error message to |
| 452 | |v:errors| when {pattern} matches {actual}. |
| 453 | Also see |assert-return|. |
| 454 | |
Bram Moolenaar | 24278d2 | 2019-08-16 21:49:22 +0200 | [diff] [blame] | 455 | Can also be used as a |method|: > |
| 456 | getFile()->assert_notmatch('bar.*') |
| 457 | |
Bram Moolenaar | e49fbff | 2019-08-21 22:50:07 +0200 | [diff] [blame] | 458 | |
Bram Moolenaar | ed997ad | 2019-07-21 16:42:00 +0200 | [diff] [blame] | 459 | assert_report({msg}) *assert_report()* |
Bram Moolenaar | 6aa5729 | 2021-08-14 21:25:52 +0200 | [diff] [blame] | 460 | Report a test failure directly, using String {msg}. |
Bram Moolenaar | ed997ad | 2019-07-21 16:42:00 +0200 | [diff] [blame] | 461 | Always returns one. |
| 462 | |
Bram Moolenaar | e49fbff | 2019-08-21 22:50:07 +0200 | [diff] [blame] | 463 | Can also be used as a |method|: > |
| 464 | GetMessage()->assert_report() |
| 465 | |
| 466 | |
Bram Moolenaar | ed997ad | 2019-07-21 16:42:00 +0200 | [diff] [blame] | 467 | assert_true({actual} [, {msg}]) *assert_true()* |
| 468 | When {actual} is not true an error message is added to |
| 469 | |v:errors|, like with |assert_equal()|. |
| 470 | Also see |assert-return|. |
| 471 | A value is TRUE when it is a non-zero number. When {actual} |
| 472 | is not a number the assert fails. |
| 473 | When {msg} is omitted an error in the form "Expected True but |
| 474 | got {actual}" is produced. |
| 475 | |
Bram Moolenaar | 24278d2 | 2019-08-16 21:49:22 +0200 | [diff] [blame] | 476 | Can also be used as a |method|: > |
| 477 | GetResult()->assert_true() |
| 478 | < |
Bram Moolenaar | ed997ad | 2019-07-21 16:42:00 +0200 | [diff] [blame] | 479 | |
| 480 | vim:tw=78:ts=8:noet:ft=help:norl: |