Anton Sharonov | 68d9472 | 2024-01-23 23:19:02 +0100 | [diff] [blame] | 1 | *testing.txt* For Vim version 9.1. Last change: 2024 Jan 23 |
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: |
RestorerZ | ac9c6d5 | 2023-10-05 22:25:12 +0200 | [diff] [blame] | 24 | test20.in oldest, only for tiny builds |
Bram Moolenaar | b96a32e | 2020-08-13 18:59:55 +0200 | [diff] [blame] | 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. |
Christopher Plewright | 20b795e | 2022-12-20 20:01:58 +0000 | [diff] [blame] | 97 | "key" send a low-level keyboard 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: |
Bram Moolenaar | 86b4816 | 2022-12-06 18:20:10 +0000 | [diff] [blame] | 138 | 0 left mouse button |
Yegappan Lakshmanan | 06011e1 | 2022-01-30 12:37:29 +0000 | [diff] [blame] | 139 | 1 middle mouse button |
Bram Moolenaar | 86b4816 | 2022-12-06 18:20:10 +0000 | [diff] [blame] | 140 | 2 right mouse button |
Yegappan Lakshmanan | 06011e1 | 2022-01-30 12:37:29 +0000 | [diff] [blame] | 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: |
Bram Moolenaar | 10e8ff9 | 2023-06-10 21:40:39 +0100 | [diff] [blame] | 169 | which: Selects the scrollbar. The supported values |
| 170 | are: |
Ernie Rael | c4cb544 | 2022-04-03 15:47:28 +0100 | [diff] [blame] | 171 | left Left scrollbar of the current window |
| 172 | right Right scrollbar of the current window |
| 173 | hor Horizontal scrollbar |
Bram Moolenaar | 10e8ff9 | 2023-06-10 21:40:39 +0100 | [diff] [blame] | 174 | value: Amount to scroll. For the vertical scrollbars |
| 175 | the value can be between 0 to the line-count |
| 176 | of the buffer minus one. For the horizontal |
| 177 | scrollbar the value can be between 1 and the |
| 178 | maximum line length, assuming 'wrap' is not |
| 179 | set. |
Ernie Rael | c4cb544 | 2022-04-03 15:47:28 +0100 | [diff] [blame] | 180 | dragging: 1 to drag the scrollbar and 0 to click in the |
| 181 | scrollbar. |
Yegappan Lakshmanan | 06011e1 | 2022-01-30 12:37:29 +0000 | [diff] [blame] | 182 | |
Christopher Plewright | 20b795e | 2022-12-20 20:01:58 +0000 | [diff] [blame] | 183 | "key": |
| 184 | Send a low-level keyboard event (e.g. key-up or down). |
Yegappan Lakshmanan | 81a3ff9 | 2022-07-23 05:04:16 +0100 | [diff] [blame] | 185 | Currently only supported on MS-Windows. |
| 186 | The supported items in {args} are: |
| 187 | event: The supported string values are: |
| 188 | keyup generate a keyup event |
| 189 | keydown generate a keydown event |
| 190 | keycode: Keycode to use for a keyup or a keydown event. |
Bram Moolenaar | 2ecbe53 | 2022-07-29 21:36:21 +0100 | [diff] [blame] | 191 | *E1291* |
Yegappan Lakshmanan | 81a3ff9 | 2022-07-23 05:04:16 +0100 | [diff] [blame] | 192 | |
Yegappan Lakshmanan | 06011e1 | 2022-01-30 12:37:29 +0000 | [diff] [blame] | 193 | "tabline": |
| 194 | Inject a mouse click event on the tabline to select a |
| 195 | tabpage. The supported items in {args} are: |
| 196 | tabnr: tab page number |
| 197 | |
| 198 | "tabmenu": |
| 199 | Inject an event to select a tabline menu entry. The |
| 200 | supported items in {args} are: |
| 201 | tabnr: tab page number |
Bram Moolenaar | 938ae28 | 2023-02-20 20:44:55 +0000 | [diff] [blame] | 202 | item: tab page menu item number. 1 for the first |
Yegappan Lakshmanan | 06011e1 | 2022-01-30 12:37:29 +0000 | [diff] [blame] | 203 | menu item, 2 for the second item and so on. |
| 204 | |
| 205 | After injecting the GUI events you probably should call |
Yegappan Lakshmanan | f1e7449 | 2021-06-21 18:44:26 +0200 | [diff] [blame] | 206 | |feedkeys()| to have them processed, e.g.: > |
| 207 | call feedkeys("y", 'Lx!') |
Yegappan Lakshmanan | 06011e1 | 2022-01-30 12:37:29 +0000 | [diff] [blame] | 208 | < |
Yegappan Lakshmanan | b0ad2d9 | 2022-01-27 13:16:59 +0000 | [diff] [blame] | 209 | Returns TRUE if the event is successfully added, FALSE if |
Yegappan Lakshmanan | 06011e1 | 2022-01-30 12:37:29 +0000 | [diff] [blame] | 210 | there is a failure. |
Yegappan Lakshmanan | b0ad2d9 | 2022-01-27 13:16:59 +0000 | [diff] [blame] | 211 | |
Yegappan Lakshmanan | 06011e1 | 2022-01-30 12:37:29 +0000 | [diff] [blame] | 212 | Can also be used as a |method|: > |
| 213 | GetEvent()->test_gui_event({args}) |
| 214 | < |
Bram Moolenaar | ed997ad | 2019-07-21 16:42:00 +0200 | [diff] [blame] | 215 | test_ignore_error({expr}) *test_ignore_error()* |
| 216 | Ignore any error containing {expr}. A normal message is given |
| 217 | instead. |
| 218 | This is only meant to be used in tests, where catching the |
| 219 | error with try/catch cannot be used (because it skips over |
| 220 | following code). |
| 221 | {expr} is used literally, not as a pattern. |
| 222 | When the {expr} is the string "RESET" then the list of ignored |
| 223 | errors is made empty. |
| 224 | |
Bram Moolenaar | ce90e36 | 2019-09-08 18:58:44 +0200 | [diff] [blame] | 225 | Can also be used as a |method|: > |
| 226 | GetErrorText()->test_ignore_error() |
Bram Moolenaar | ed997ad | 2019-07-21 16:42:00 +0200 | [diff] [blame] | 227 | |
Bram Moolenaar | be4e016 | 2023-02-02 13:59:48 +0000 | [diff] [blame] | 228 | |
Christopher Plewright | 20b795e | 2022-12-20 20:01:58 +0000 | [diff] [blame] | 229 | test_mswin_event({event}, {args}) *test_mswin_event()* |
| 230 | Generate a low-level MS-Windows {event} with arguments {args} |
Bram Moolenaar | be4e016 | 2023-02-02 13:59:48 +0000 | [diff] [blame] | 231 | for testing Vim functionality. It works for MS-Windows GUI |
Christopher Plewright | 20b795e | 2022-12-20 20:01:58 +0000 | [diff] [blame] | 232 | and for the console. |
Bram Moolenaar | be4e016 | 2023-02-02 13:59:48 +0000 | [diff] [blame] | 233 | |
Christopher Plewright | 20b795e | 2022-12-20 20:01:58 +0000 | [diff] [blame] | 234 | {event} is a String and the supported values are: |
| 235 | "mouse" mouse event. |
| 236 | "key" keyboard event. |
Anton Sharonov | 68d9472 | 2024-01-23 23:19:02 +0100 | [diff] [blame] | 237 | "set_keycode_trans_strategy" |
| 238 | Change the key transation method |
Christopher Plewright | 20b795e | 2022-12-20 20:01:58 +0000 | [diff] [blame] | 239 | |
| 240 | "mouse": |
| 241 | Inject either a mouse button click, or a mouse move, event. |
| 242 | The supported items in {args} are: |
| 243 | button: mouse button. The supported values are: |
| 244 | 0 right mouse button |
| 245 | 1 middle mouse button |
| 246 | 2 left mouse button |
| 247 | 3 mouse button release |
| 248 | 4 scroll wheel down |
| 249 | 5 scroll wheel up |
| 250 | 6 scroll wheel left |
| 251 | 7 scroll wheel right |
| 252 | row: mouse click row number. The first row of the |
| 253 | Vim window is 1 and the last row is 'lines'. |
| 254 | col: mouse click column number. The maximum value |
| 255 | of {col} is 'columns'. |
| 256 | Note: row and col are always interpreted as |
| 257 | screen cells for the console application. |
| 258 | But, they may be interpreted as pixels |
| 259 | for the GUI, depending on "cell". |
| 260 | multiclick: set to 1 to inject a double-click mouse event. |
| 261 | modifiers: key modifiers. The supported values are: |
| 262 | 4 shift is pressed |
| 263 | 8 alt is pressed |
| 264 | 16 ctrl is pressed |
| 265 | move: Optional; if used and TRUE then a mouse move |
| 266 | event can be generated. |
| 267 | Only {args} row: and col: are used and |
| 268 | required. |
| 269 | Only results in an event when 'mousemoveevent' |
| 270 | is set or a popup uses mouse move events. |
| 271 | cell: Optional for the GUI: when present and TRUE |
| 272 | then "move" uses screen cells instead of pixel |
| 273 | positions. Not used by the console. |
| 274 | |
| 275 | "key": |
| 276 | Send a low-level keyboard event (e.g. keyup or keydown). |
| 277 | The supported items in {args} are: |
| 278 | event: The supported string values are: |
| 279 | keyup generate a keyup event |
| 280 | keydown generate a keydown event |
Christopher Plewright | 7b0afc1 | 2022-12-30 16:54:58 +0000 | [diff] [blame] | 281 | keycode: Keycode to use for a keyup or a keydown event. |
Christopher Plewright | 20b795e | 2022-12-20 20:01:58 +0000 | [diff] [blame] | 282 | modifiers: Optional; key modifiers. |
| 283 | The supported values are: |
| 284 | 2 shift is pressed |
| 285 | 4 ctrl is pressed |
| 286 | 8 alt is pressed |
| 287 | Note: These values are different from the |
| 288 | mouse modifiers. |
Christopher Plewright | 7b0afc1 | 2022-12-30 16:54:58 +0000 | [diff] [blame] | 289 | execute: Optional. Similar to |feedkeys()| mode x. |
| 290 | When this is included and set to true |
| 291 | (non-zero) then Vim will process any buffered |
| 292 | unprocessed key events. All other {args} |
| 293 | items are optional when this is set and true. |
| 294 | |
Anton Sharonov | 68d9472 | 2024-01-23 23:19:02 +0100 | [diff] [blame] | 295 | "set_keycode_trans_strategy": |
| 296 | |w32-experimental-keycode-trans-strategy| |
| 297 | Switch the keycode translation method. The supported methods are: |
| 298 | experimental: The method used after Patch v8.2.4807 |
| 299 | using ToUnicode() Win API call. |
| 300 | classic: The method used pre Patch v8.2.4807 |
| 301 | using the TranslateMessage() Win API call. |
| 302 | |
Christopher Plewright | 7b0afc1 | 2022-12-30 16:54:58 +0000 | [diff] [blame] | 303 | Returns TRUE if the event is successfully added or executed, |
| 304 | FALSE if there is a failure. |
Christopher Plewright | 20b795e | 2022-12-20 20:01:58 +0000 | [diff] [blame] | 305 | |
| 306 | Can also be used as a |method|: > |
| 307 | GetEvent()->test_mswin_event({args}) |
| 308 | < |
Bram Moolenaar | 89a9c15 | 2021-08-29 21:55:35 +0200 | [diff] [blame] | 309 | |
Bram Moolenaar | ed997ad | 2019-07-21 16:42:00 +0200 | [diff] [blame] | 310 | test_null_blob() *test_null_blob()* |
| 311 | Return a |Blob| that is null. Only useful for testing. |
| 312 | |
| 313 | |
| 314 | test_null_channel() *test_null_channel()* |
| 315 | Return a |Channel| that is null. Only useful for testing. |
| 316 | {only available when compiled with the +channel feature} |
| 317 | |
| 318 | |
| 319 | test_null_dict() *test_null_dict()* |
| 320 | Return a |Dict| that is null. Only useful for testing. |
| 321 | |
| 322 | |
Bram Moolenaar | e69f6d0 | 2020-04-01 22:11:01 +0200 | [diff] [blame] | 323 | test_null_function() *test_null_function()* |
Bram Moolenaar | d1caa94 | 2020-04-10 22:10:56 +0200 | [diff] [blame] | 324 | Return a |Funcref| that is null. Only useful for testing. |
Bram Moolenaar | e69f6d0 | 2020-04-01 22:11:01 +0200 | [diff] [blame] | 325 | |
| 326 | |
Bram Moolenaar | ed997ad | 2019-07-21 16:42:00 +0200 | [diff] [blame] | 327 | test_null_job() *test_null_job()* |
| 328 | Return a |Job| that is null. Only useful for testing. |
| 329 | {only available when compiled with the +job feature} |
| 330 | |
| 331 | |
| 332 | test_null_list() *test_null_list()* |
| 333 | Return a |List| that is null. Only useful for testing. |
| 334 | |
| 335 | |
| 336 | test_null_partial() *test_null_partial()* |
| 337 | Return a |Partial| that is null. Only useful for testing. |
| 338 | |
| 339 | |
| 340 | test_null_string() *test_null_string()* |
| 341 | Return a |String| that is null. Only useful for testing. |
| 342 | |
| 343 | |
| 344 | test_option_not_set({name}) *test_option_not_set()* |
| 345 | Reset the flag that indicates option {name} was set. Thus it |
| 346 | looks like it still has the default value. Use like this: > |
| 347 | set ambiwidth=double |
| 348 | call test_option_not_set('ambiwidth') |
| 349 | < Now the 'ambiwidth' option behaves like it was never changed, |
| 350 | even though the value is "double". |
| 351 | Only to be used for testing! |
| 352 | |
Bram Moolenaar | ce90e36 | 2019-09-08 18:58:44 +0200 | [diff] [blame] | 353 | Can also be used as a |method|: > |
| 354 | GetOptionName()->test_option_not_set() |
| 355 | |
Bram Moolenaar | ed997ad | 2019-07-21 16:42:00 +0200 | [diff] [blame] | 356 | |
| 357 | test_override({name}, {val}) *test_override()* |
| 358 | Overrides certain parts of Vim's internal processing to be able |
| 359 | to run tests. Only to be used for testing Vim! |
| 360 | The override is enabled when {val} is non-zero and removed |
| 361 | when {val} is zero. |
Bram Moolenaar | 3e4fa3d | 2022-01-13 22:05:09 +0000 | [diff] [blame] | 362 | Current supported values for {name} are: |
Bram Moolenaar | ed997ad | 2019-07-21 16:42:00 +0200 | [diff] [blame] | 363 | |
Bram Moolenaar | 3e4fa3d | 2022-01-13 22:05:09 +0000 | [diff] [blame] | 364 | {name} effect when {val} is non-zero ~ |
Bram Moolenaar | fa4873c | 2022-06-30 22:13:59 +0100 | [diff] [blame] | 365 | alloc_lines make a copy of every buffer line into allocated |
| 366 | memory, so that memory access errors can be found |
| 367 | by valgrind |
Bram Moolenaar | 3e4fa3d | 2022-01-13 22:05:09 +0000 | [diff] [blame] | 368 | autoload `import autoload` will load the script right |
| 369 | away, not postponed until an item is used |
Bram Moolenaar | ed997ad | 2019-07-21 16:42:00 +0200 | [diff] [blame] | 370 | char_avail disable the char_avail() function |
Bram Moolenaar | ed997ad | 2019-07-21 16:42:00 +0200 | [diff] [blame] | 371 | nfa_fail makes the NFA regexp engine fail to force a |
| 372 | fallback to the old engine |
| 373 | no_query_mouse do not query the mouse position for "dec" |
| 374 | terminals |
| 375 | no_wait_return set the "no_wait_return" flag. Not restored |
| 376 | with "ALL". |
Bram Moolenaar | 3e4fa3d | 2022-01-13 22:05:09 +0000 | [diff] [blame] | 377 | redraw disable the redrawing() function |
| 378 | redraw_flag ignore the RedrawingDisabled flag |
| 379 | starting reset the "starting" variable, see below |
Bram Moolenaar | 0c0eddd | 2020-06-13 15:47:25 +0200 | [diff] [blame] | 380 | term_props reset all terminal properties when the version |
| 381 | string is detected |
Bram Moolenaar | 3e4fa3d | 2022-01-13 22:05:09 +0000 | [diff] [blame] | 382 | ui_delay time in msec to use in ui_delay(); overrules a |
| 383 | wait time of up to 3 seconds for messages |
Bram Moolenaar | 9d383f3 | 2023-05-14 21:38:12 +0100 | [diff] [blame] | 384 | unreachable no error for code after `:throw` and `:return` |
Bram Moolenaar | 938ae28 | 2023-02-20 20:44:55 +0000 | [diff] [blame] | 385 | uptime overrules sysinfo.uptime |
ichizok | ae1bd87 | 2022-01-20 14:57:29 +0000 | [diff] [blame] | 386 | vterm_title setting the window title by a job running in a |
| 387 | terminal window |
Bram Moolenaar | fa4873c | 2022-06-30 22:13:59 +0100 | [diff] [blame] | 388 | ALL clear all overrides, except alloc_lines ({val} is |
| 389 | not used) |
Bram Moolenaar | ed997ad | 2019-07-21 16:42:00 +0200 | [diff] [blame] | 390 | |
| 391 | "starting" is to be used when a test should behave like |
| 392 | startup was done. Since the tests are run by sourcing a |
| 393 | script the "starting" variable is non-zero. This is usually a |
Bram Moolenaar | 9d383f3 | 2023-05-14 21:38:12 +0100 | [diff] [blame] | 394 | good thing (tests run faster), but sometimes this changes |
| 395 | behavior in a way that the test doesn't work properly. |
Bram Moolenaar | ed997ad | 2019-07-21 16:42:00 +0200 | [diff] [blame] | 396 | When using: > |
| 397 | call test_override('starting', 1) |
| 398 | < The value of "starting" is saved. It is restored by: > |
| 399 | call test_override('starting', 0) |
| 400 | |
Bram Moolenaar | 9d383f3 | 2023-05-14 21:38:12 +0100 | [diff] [blame] | 401 | < To make sure the flag is reset later using `:defer` can be |
| 402 | useful: > |
| 403 | call test_override('unreachable', 1) |
| 404 | defer call test_override('unreachable', 0) |
| 405 | |
Bram Moolenaar | ce90e36 | 2019-09-08 18:58:44 +0200 | [diff] [blame] | 406 | < Can also be used as a |method|: > |
| 407 | GetOverrideVal()-> test_override('starting') |
Bram Moolenaar | ed997ad | 2019-07-21 16:42:00 +0200 | [diff] [blame] | 408 | |
Bram Moolenaar | 89a9c15 | 2021-08-29 21:55:35 +0200 | [diff] [blame] | 409 | |
Bram Moolenaar | ed997ad | 2019-07-21 16:42:00 +0200 | [diff] [blame] | 410 | test_refcount({expr}) *test_refcount()* |
| 411 | Return the reference count of {expr}. When {expr} is of a |
| 412 | type that does not have a reference count, returns -1. Only |
| 413 | to be used for testing. |
| 414 | |
Bram Moolenaar | ce90e36 | 2019-09-08 18:58:44 +0200 | [diff] [blame] | 415 | Can also be used as a |method|: > |
| 416 | GetVarname()->test_refcount() |
| 417 | |
Bram Moolenaar | ed997ad | 2019-07-21 16:42:00 +0200 | [diff] [blame] | 418 | |
Bram Moolenaar | ed997ad | 2019-07-21 16:42:00 +0200 | [diff] [blame] | 419 | test_setmouse({row}, {col}) *test_setmouse()* |
| 420 | Set the mouse position to be used for the next mouse action. |
| 421 | {row} and {col} are one based. |
| 422 | For example: > |
| 423 | call test_setmouse(4, 20) |
| 424 | call feedkeys("\<LeftMouse>", "xt") |
| 425 | |
Bram Moolenaar | 89a9c15 | 2021-08-29 21:55:35 +0200 | [diff] [blame] | 426 | |
Bram Moolenaar | ed997ad | 2019-07-21 16:42:00 +0200 | [diff] [blame] | 427 | test_settime({expr}) *test_settime()* |
| 428 | Set the time Vim uses internally. Currently only used for |
| 429 | timestamps in the history, as they are used in viminfo, and |
| 430 | for undo. |
| 431 | Using a value of 1 makes Vim not sleep after a warning or |
| 432 | error message. |
| 433 | {expr} must evaluate to a number. When the value is zero the |
| 434 | normal behavior is restored. |
| 435 | |
Bram Moolenaar | ce90e36 | 2019-09-08 18:58:44 +0200 | [diff] [blame] | 436 | Can also be used as a |method|: > |
| 437 | GetTime()->test_settime() |
| 438 | |
Bram Moolenaar | 89a9c15 | 2021-08-29 21:55:35 +0200 | [diff] [blame] | 439 | |
Bram Moolenaar | 4f645c5 | 2020-02-08 16:40:39 +0100 | [diff] [blame] | 440 | test_srand_seed([seed]) *test_srand_seed()* |
| 441 | When [seed] is given this sets the seed value used by |
| 442 | `srand()`. When omitted the test seed is removed. |
| 443 | |
Bram Moolenaar | 89a9c15 | 2021-08-29 21:55:35 +0200 | [diff] [blame] | 444 | |
| 445 | test_unknown() *test_unknown()* |
| 446 | Return a value with unknown type. Only useful for testing. |
| 447 | |
| 448 | |
| 449 | test_void() *test_void()* |
| 450 | Return a value with void type. Only useful for testing. |
| 451 | |
Bram Moolenaar | ed997ad | 2019-07-21 16:42:00 +0200 | [diff] [blame] | 452 | ============================================================================== |
Bram Moolenaar | 5477506 | 2019-07-31 21:07:14 +0200 | [diff] [blame] | 453 | 3. Assert functions *assert-functions-details* |
Bram Moolenaar | ed997ad | 2019-07-21 16:42:00 +0200 | [diff] [blame] | 454 | |
| 455 | |
| 456 | assert_beeps({cmd}) *assert_beeps()* |
| 457 | Run {cmd} and add an error message to |v:errors| if it does |
| 458 | NOT produce a beep or visual bell. |
Bram Moolenaar | 5b8cabf | 2021-04-02 18:55:57 +0200 | [diff] [blame] | 459 | Also see |assert_fails()|, |assert_nobeep()| and |
| 460 | |assert-return|. |
Bram Moolenaar | ed997ad | 2019-07-21 16:42:00 +0200 | [diff] [blame] | 461 | |
Bram Moolenaar | 24278d2 | 2019-08-16 21:49:22 +0200 | [diff] [blame] | 462 | Can also be used as a |method|: > |
| 463 | GetCmd()->assert_beeps() |
| 464 | < |
Bram Moolenaar | ed997ad | 2019-07-21 16:42:00 +0200 | [diff] [blame] | 465 | *assert_equal()* |
| 466 | assert_equal({expected}, {actual} [, {msg}]) |
| 467 | When {expected} and {actual} are not equal an error message is |
| 468 | added to |v:errors| and 1 is returned. Otherwise zero is |
Bram Moolenaar | b7398fe | 2023-05-14 18:50:25 +0100 | [diff] [blame] | 469 | returned. |assert-return| |
| 470 | The error is in the form "Expected {expected} but got |
| 471 | {actual}". When {msg} is present it is prefixed to that. |
| 472 | |
Bram Moolenaar | ed997ad | 2019-07-21 16:42:00 +0200 | [diff] [blame] | 473 | There is no automatic conversion, the String "4" is different |
| 474 | from the Number 4. And the number 4 is different from the |
| 475 | Float 4.0. The value of 'ignorecase' is not used here, case |
| 476 | always matters. |
Bram Moolenaar | ed997ad | 2019-07-21 16:42:00 +0200 | [diff] [blame] | 477 | Example: > |
| 478 | assert_equal('foo', 'bar') |
| 479 | < Will result in a string to be added to |v:errors|: |
| 480 | test.vim line 12: Expected 'foo' but got 'bar' ~ |
| 481 | |
Bram Moolenaar | 7ff7846 | 2020-07-10 22:00:53 +0200 | [diff] [blame] | 482 | Can also be used as a |method|, the base is passed as the |
| 483 | second argument: > |
Bram Moolenaar | 25e4223 | 2019-08-04 15:04:10 +0200 | [diff] [blame] | 484 | mylist->assert_equal([1, 2, 3]) |
| 485 | |
Bram Moolenaar | 25e4223 | 2019-08-04 15:04:10 +0200 | [diff] [blame] | 486 | < *assert_equalfile()* |
Bram Moolenaar | fb517ba | 2020-06-03 19:55:35 +0200 | [diff] [blame] | 487 | assert_equalfile({fname-one}, {fname-two} [, {msg}]) |
Bram Moolenaar | ed997ad | 2019-07-21 16:42:00 +0200 | [diff] [blame] | 488 | When the files {fname-one} and {fname-two} do not contain |
| 489 | exactly the same text an error message is added to |v:errors|. |
| 490 | Also see |assert-return|. |
| 491 | When {fname-one} or {fname-two} does not exist the error will |
| 492 | mention that. |
| 493 | Mainly useful with |terminal-diff|. |
| 494 | |
Bram Moolenaar | e49fbff | 2019-08-21 22:50:07 +0200 | [diff] [blame] | 495 | Can also be used as a |method|: > |
| 496 | GetLog()->assert_equalfile('expected.log') |
| 497 | |
Bram Moolenaar | ed997ad | 2019-07-21 16:42:00 +0200 | [diff] [blame] | 498 | assert_exception({error} [, {msg}]) *assert_exception()* |
| 499 | When v:exception does not contain the string {error} an error |
| 500 | message is added to |v:errors|. Also see |assert-return|. |
| 501 | This can be used to assert that a command throws an exception. |
| 502 | Using the error number, followed by a colon, avoids problems |
| 503 | with translations: > |
| 504 | try |
| 505 | commandthatfails |
| 506 | call assert_false(1, 'command should have failed') |
| 507 | catch |
| 508 | call assert_exception('E492:') |
| 509 | endtry |
Bram Moolenaar | 1d63454 | 2020-08-18 13:41:50 +0200 | [diff] [blame] | 510 | < |
| 511 | *assert_fails()* |
Bram Moolenaar | 9bd5d87 | 2020-09-06 21:47:48 +0200 | [diff] [blame] | 512 | assert_fails({cmd} [, {error} [, {msg} [, {lnum} [, {context}]]]]) |
Bram Moolenaar | ed997ad | 2019-07-21 16:42:00 +0200 | [diff] [blame] | 513 | 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] | 514 | NOT produce an error or when {error} is not found in the |
| 515 | error message. Also see |assert-return|. |
Bram Moolenaar | 6f4754b | 2022-01-23 12:07:04 +0000 | [diff] [blame] | 516 | *E856* |
Bram Moolenaar | 9b7bf9e | 2020-07-11 22:14:59 +0200 | [diff] [blame] | 517 | When {error} is a string it must be found literally in the |
| 518 | first reported error. Most often this will be the error code, |
| 519 | including the colon, e.g. "E123:". > |
| 520 | assert_fails('bad cmd', 'E987:') |
| 521 | < |
| 522 | When {error} is a |List| with one or two strings, these are |
| 523 | used as patterns. The first pattern is matched against the |
| 524 | first reported error: > |
| 525 | assert_fails('cmd', ['E987:.*expected bool']) |
| 526 | < The second pattern, if present, is matched against the last |
Bram Moolenaar | 4072ba5 | 2020-12-23 13:56:35 +0100 | [diff] [blame] | 527 | reported error. |
| 528 | If there is only one error then both patterns must match. This |
| 529 | can be used to check that there is only one error. |
| 530 | To only match the last error use an empty string for the first |
| 531 | error: > |
Bram Moolenaar | 9b7bf9e | 2020-07-11 22:14:59 +0200 | [diff] [blame] | 532 | assert_fails('cmd', ['', 'E987:']) |
| 533 | < |
Bram Moolenaar | 1d63454 | 2020-08-18 13:41:50 +0200 | [diff] [blame] | 534 | If {msg} is empty then it is not used. Do this to get the |
| 535 | default message when passing the {lnum} argument. |
Bram Moolenaar | f10911e | 2022-01-29 22:20:48 +0000 | [diff] [blame] | 536 | *E1115* |
Bram Moolenaar | 1d63454 | 2020-08-18 13:41:50 +0200 | [diff] [blame] | 537 | When {lnum} is present and not negative, and the {error} |
| 538 | argument is present and matches, then this is compared with |
| 539 | the line number at which the error was reported. That can be |
| 540 | the line number in a function or in a script. |
Bram Moolenaar | f10911e | 2022-01-29 22:20:48 +0000 | [diff] [blame] | 541 | *E1116* |
Bram Moolenaar | 9bd5d87 | 2020-09-06 21:47:48 +0200 | [diff] [blame] | 542 | When {context} is present it is used as a pattern and matched |
| 543 | against the context (script name or function name) where |
| 544 | {lnum} is located in. |
| 545 | |
Bram Moolenaar | ed997ad | 2019-07-21 16:42:00 +0200 | [diff] [blame] | 546 | Note that beeping is not considered an error, and some failing |
| 547 | commands only beep. Use |assert_beeps()| for those. |
| 548 | |
Bram Moolenaar | 24278d2 | 2019-08-16 21:49:22 +0200 | [diff] [blame] | 549 | Can also be used as a |method|: > |
| 550 | GetCmd()->assert_fails('E99:') |
| 551 | |
Bram Moolenaar | 1d63454 | 2020-08-18 13:41:50 +0200 | [diff] [blame] | 552 | assert_false({actual} [, {msg}]) *assert_false()* |
Bram Moolenaar | ed997ad | 2019-07-21 16:42:00 +0200 | [diff] [blame] | 553 | When {actual} is not false an error message is added to |
| 554 | |v:errors|, like with |assert_equal()|. |
Bram Moolenaar | b7398fe | 2023-05-14 18:50:25 +0100 | [diff] [blame] | 555 | The error is in the form "Expected False but got {actual}". |
| 556 | When {msg} is present it is prepended to that. |
Bram Moolenaar | ed997ad | 2019-07-21 16:42:00 +0200 | [diff] [blame] | 557 | Also see |assert-return|. |
Bram Moolenaar | b7398fe | 2023-05-14 18:50:25 +0100 | [diff] [blame] | 558 | |
Bram Moolenaar | ed997ad | 2019-07-21 16:42:00 +0200 | [diff] [blame] | 559 | A value is false when it is zero. When {actual} is not a |
| 560 | number the assert fails. |
Bram Moolenaar | ed997ad | 2019-07-21 16:42:00 +0200 | [diff] [blame] | 561 | |
Bram Moolenaar | 24278d2 | 2019-08-16 21:49:22 +0200 | [diff] [blame] | 562 | Can also be used as a |method|: > |
| 563 | GetResult()->assert_false() |
| 564 | |
Bram Moolenaar | ed997ad | 2019-07-21 16:42:00 +0200 | [diff] [blame] | 565 | assert_inrange({lower}, {upper}, {actual} [, {msg}]) *assert_inrange()* |
| 566 | This asserts number and |Float| values. When {actual} is lower |
| 567 | than {lower} or higher than {upper} an error message is added |
| 568 | to |v:errors|. Also see |assert-return|. |
Bram Moolenaar | b7398fe | 2023-05-14 18:50:25 +0100 | [diff] [blame] | 569 | The error is in the form "Expected range {lower} - {upper}, |
| 570 | but got {actual}". When {msg} is present it is prefixed to |
| 571 | that. |
Bram Moolenaar | ed997ad | 2019-07-21 16:42:00 +0200 | [diff] [blame] | 572 | |
| 573 | *assert_match()* |
| 574 | assert_match({pattern}, {actual} [, {msg}]) |
| 575 | When {pattern} does not match {actual} an error message is |
| 576 | added to |v:errors|. Also see |assert-return|. |
Bram Moolenaar | b7398fe | 2023-05-14 18:50:25 +0100 | [diff] [blame] | 577 | The error is in the form "Pattern {pattern} does not match |
| 578 | {actual}". When {msg} is present it is prefixed to that. |
Bram Moolenaar | ed997ad | 2019-07-21 16:42:00 +0200 | [diff] [blame] | 579 | |
| 580 | {pattern} is used as with |=~|: The matching is always done |
| 581 | like 'magic' was set and 'cpoptions' is empty, no matter what |
| 582 | the actual value of 'magic' or 'cpoptions' is. |
| 583 | |
| 584 | {actual} is used as a string, automatic conversion applies. |
| 585 | Use "^" and "$" to match with the start and end of the text. |
| 586 | Use both to match the whole text. |
| 587 | |
Bram Moolenaar | ed997ad | 2019-07-21 16:42:00 +0200 | [diff] [blame] | 588 | Example: > |
| 589 | assert_match('^f.*o$', 'foobar') |
| 590 | < Will result in a string to be added to |v:errors|: |
| 591 | test.vim line 12: Pattern '^f.*o$' does not match 'foobar' ~ |
| 592 | |
Bram Moolenaar | 24278d2 | 2019-08-16 21:49:22 +0200 | [diff] [blame] | 593 | Can also be used as a |method|: > |
| 594 | getFile()->assert_match('foo.*') |
| 595 | < |
Bram Moolenaar | 5b8cabf | 2021-04-02 18:55:57 +0200 | [diff] [blame] | 596 | assert_nobeep({cmd}) *assert_nobeep()* |
| 597 | Run {cmd} and add an error message to |v:errors| if it |
| 598 | produces a beep or visual bell. |
| 599 | Also see |assert_beeps()|. |
| 600 | |
| 601 | Can also be used as a |method|: > |
| 602 | GetCmd()->assert_nobeep() |
| 603 | < |
Bram Moolenaar | ed997ad | 2019-07-21 16:42:00 +0200 | [diff] [blame] | 604 | *assert_notequal()* |
| 605 | assert_notequal({expected}, {actual} [, {msg}]) |
| 606 | The opposite of `assert_equal()`: add an error message to |
| 607 | |v:errors| when {expected} and {actual} are equal. |
| 608 | Also see |assert-return|. |
| 609 | |
Bram Moolenaar | 25e4223 | 2019-08-04 15:04:10 +0200 | [diff] [blame] | 610 | Can also be used as a |method|: > |
| 611 | mylist->assert_notequal([1, 2, 3]) |
| 612 | |
| 613 | < *assert_notmatch()* |
Bram Moolenaar | ed997ad | 2019-07-21 16:42:00 +0200 | [diff] [blame] | 614 | assert_notmatch({pattern}, {actual} [, {msg}]) |
| 615 | The opposite of `assert_match()`: add an error message to |
| 616 | |v:errors| when {pattern} matches {actual}. |
| 617 | Also see |assert-return|. |
| 618 | |
Bram Moolenaar | 24278d2 | 2019-08-16 21:49:22 +0200 | [diff] [blame] | 619 | Can also be used as a |method|: > |
| 620 | getFile()->assert_notmatch('bar.*') |
| 621 | |
Bram Moolenaar | e49fbff | 2019-08-21 22:50:07 +0200 | [diff] [blame] | 622 | |
Bram Moolenaar | ed997ad | 2019-07-21 16:42:00 +0200 | [diff] [blame] | 623 | assert_report({msg}) *assert_report()* |
Bram Moolenaar | 6aa5729 | 2021-08-14 21:25:52 +0200 | [diff] [blame] | 624 | Report a test failure directly, using String {msg}. |
Bram Moolenaar | ed997ad | 2019-07-21 16:42:00 +0200 | [diff] [blame] | 625 | Always returns one. |
| 626 | |
Bram Moolenaar | e49fbff | 2019-08-21 22:50:07 +0200 | [diff] [blame] | 627 | Can also be used as a |method|: > |
| 628 | GetMessage()->assert_report() |
| 629 | |
| 630 | |
Bram Moolenaar | ed997ad | 2019-07-21 16:42:00 +0200 | [diff] [blame] | 631 | assert_true({actual} [, {msg}]) *assert_true()* |
| 632 | When {actual} is not true an error message is added to |
| 633 | |v:errors|, like with |assert_equal()|. |
| 634 | Also see |assert-return|. |
| 635 | A value is TRUE when it is a non-zero number. When {actual} |
| 636 | is not a number the assert fails. |
Bram Moolenaar | b7398fe | 2023-05-14 18:50:25 +0100 | [diff] [blame] | 637 | When {msg} is given it precedes the default message. |
Bram Moolenaar | ed997ad | 2019-07-21 16:42:00 +0200 | [diff] [blame] | 638 | |
Bram Moolenaar | 24278d2 | 2019-08-16 21:49:22 +0200 | [diff] [blame] | 639 | Can also be used as a |method|: > |
| 640 | GetResult()->assert_true() |
| 641 | < |
Bram Moolenaar | ed997ad | 2019-07-21 16:42:00 +0200 | [diff] [blame] | 642 | |
| 643 | vim:tw=78:ts=8:noet:ft=help:norl: |