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