blob: 356d74e390b10f96780078f8a44d3071a2d6323a [file] [log] [blame]
zeertzjqc86bff12024-02-18 18:53:08 +01001*testing.txt* For Vim version 9.1. Last change: 2024 Feb 18
Bram Moolenaared997ad2019-07-21 16:42:00 +02002
3
4 VIM REFERENCE MANUAL by Bram Moolenaar
5
6
7Testing Vim and Vim script *testing-support*
8
9Expression evaluation is explained in |eval.txt|. This file goes into details
10about writing tests in Vim script. This can be used for testing Vim itself
11and for testing plugins.
12
131. Testing Vim |testing|
Bram Moolenaar54775062019-07-31 21:07:14 +0200142. Test functions |test-functions-details|
153. Assert functions |assert-functions-details|
Bram Moolenaared997ad2019-07-21 16:42:00 +020016
17==============================================================================
181. Testing Vim *testing*
19
20Vim can be tested after building it, usually with "make test".
21The tests are located in the directory "src/testdir".
22
Bram Moolenaarb96a32e2020-08-13 18:59:55 +020023There are two types of tests added over time:
RestorerZac9c6d52023-10-05 22:25:12 +020024 test20.in oldest, only for tiny builds
Bram Moolenaarb96a32e2020-08-13 18:59:55 +020025 test_something.vim new style tests
26
Bram Moolenaared997ad2019-07-21 16:42:00 +020027 *new-style-testing*
Bram Moolenaarf7c4d832020-08-11 20:42:19 +020028New tests should be added as new style tests. The test scripts are named
29test_<feature>.vim (replace <feature> with the feature under test). These use
30functions such as |assert_equal()| to keep the test commands and the expected
31result in one place.
Bram Moolenaarb96a32e2020-08-13 18:59:55 +020032 *old-style-testing*
33These tests are used only for testing Vim without the |+eval| feature.
Bram Moolenaared997ad2019-07-21 16:42:00 +020034
35Find more information in the file src/testdir/README.txt.
36
37==============================================================================
Bram Moolenaar54775062019-07-31 21:07:14 +0200382. Test functions *test-functions-details*
Bram Moolenaared997ad2019-07-21 16:42:00 +020039
40test_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 Moolenaarce90e362019-09-08 18:58:44 +020046 Can also be used as a |method|: >
47 GetAllocId()->test_alloc_fail()
Bram Moolenaared997ad2019-07-21 16:42:00 +020048
Bram Moolenaar89a9c152021-08-29 21:55:35 +020049
Bram Moolenaared997ad2019-07-21 16:42:00 +020050test_autochdir() *test_autochdir()*
51 Set a flag to enable the effect of 'autochdir' before Vim
52 startup has finished.
53
54
55test_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 Moolenaarce90e362019-09-08 18:58:44 +020060 Can also be used as a |method|: >
61 GetText()->test_feedinput()
Bram Moolenaared997ad2019-07-21 16:42:00 +020062
Bram Moolenaar89a9c152021-08-29 21:55:35 +020063
Bram Moolenaared997ad2019-07-21 16:42:00 +020064test_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 Moolenaara2baa732022-02-04 16:09:54 +000068 any function. *E1142*
69 This will not work when called from a :def function, because
70 variables on the stack will be freed.
Bram Moolenaared997ad2019-07-21 16:42:00 +020071
72
73test_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
78test_getvalue({name}) *test_getvalue()*
79 Get the value of an internal variable. These values for
80 {name} are supported:
81 need_fileinfo
82
Bram Moolenaarce90e362019-09-08 18:58:44 +020083 Can also be used as a |method|: >
84 GetName()->test_getvalue()
Yegappan Lakshmananf1e74492021-06-21 18:44:26 +020085<
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +000086 *test_gui_event()*
87test_gui_event({event}, {args})
88 Generate a GUI {event} with arguments {args} for testing Vim
Yegappan Lakshmananec3637c2022-01-30 18:01:24 +000089 functionality. This function works only when the GUI is
90 running.
Bram Moolenaar22863042021-10-16 15:23:36 +010091
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +000092 {event} is a String and the supported values are:
93 "dropfiles" drop one or more files in a window.
Bram Moolenaar8a3b8052022-06-26 12:21:15 +010094 "findrepl" search and replace text.
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +000095 "mouse" mouse button click event.
Bram Moolenaar8a3b8052022-06-26 12:21:15 +010096 "scrollbar" move or drag the scrollbar.
Christopher Plewright20b795e2022-12-20 20:01:58 +000097 "key" send a low-level keyboard event.
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +000098 "tabline" select a tab page by mouse click.
99 "tabmenu" select a tabline menu entry.
Yegappan Lakshmanan18d46582021-06-23 20:46:52 +0200100
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +0000101 {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 Lakshmananec3637c2022-01-30 18:01:24 +0000115 for more information. This event works only when the
116 |drop_file| feature is present.
117
118 "findrepl":
Bram Moolenaara2baa732022-02-04 16:09:54 +0000119 {only available when the GUI has a find/replace dialog}
Yegappan Lakshmananec3637c2022-01-30 18:01:24 +0000120 Perform a search and replace of text. The supported items
121 in {args} are:
122 find_text: string to find.
Bram Moolenaar8a3b8052022-06-26 12:21:15 +0100123 repl_text: replacement string.
Yegappan Lakshmananec3637c2022-01-30 18:01:24 +0000124 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 Lakshmanan06011e12022-01-30 12:37:29 +0000133
134 "mouse":
Ernie Raelc4cb5442022-04-03 15:47:28 +0100135 Inject either a mouse button click, or a mouse move, event.
136 The supported items in {args} are:
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +0000137 button: mouse button. The supported values are:
Bram Moolenaar86b48162022-12-06 18:20:10 +0000138 0 left mouse button
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +0000139 1 middle mouse button
Bram Moolenaar86b48162022-12-06 18:20:10 +0000140 2 right mouse button
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +0000141 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 Raelc4cb5442022-04-03 15:47:28 +0100155 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 Moolenaar7add8d32022-05-16 15:27:46 +0100158 required; they are interpreted as pixels or
159 screen cells, depending on "cell".
Ernie Raelc4cb5442022-04-03 15:47:28 +0100160 Only results in an event when 'mousemoveevent'
161 is set or a popup uses mouse move events.
Bram Moolenaar7add8d32022-05-16 15:27:46 +0100162 cell: Optional: when present and TRUE then "move"
163 uses screen cells instead of pixel positions
Ernie Raelc4cb5442022-04-03 15:47:28 +0100164
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 Moolenaar10e8ff92023-06-10 21:40:39 +0100169 which: Selects the scrollbar. The supported values
170 are:
Ernie Raelc4cb5442022-04-03 15:47:28 +0100171 left Left scrollbar of the current window
172 right Right scrollbar of the current window
173 hor Horizontal scrollbar
Bram Moolenaar10e8ff92023-06-10 21:40:39 +0100174 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 Raelc4cb5442022-04-03 15:47:28 +0100180 dragging: 1 to drag the scrollbar and 0 to click in the
181 scrollbar.
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +0000182
Christopher Plewright20b795e2022-12-20 20:01:58 +0000183 "key":
184 Send a low-level keyboard event (e.g. key-up or down).
Yegappan Lakshmanan81a3ff92022-07-23 05:04:16 +0100185 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 Moolenaar2ecbe532022-07-29 21:36:21 +0100191 *E1291*
Yegappan Lakshmanan81a3ff92022-07-23 05:04:16 +0100192
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +0000193 "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 Moolenaar938ae282023-02-20 20:44:55 +0000202 item: tab page menu item number. 1 for the first
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +0000203 menu item, 2 for the second item and so on.
204
205 After injecting the GUI events you probably should call
Yegappan Lakshmananf1e74492021-06-21 18:44:26 +0200206 |feedkeys()| to have them processed, e.g.: >
207 call feedkeys("y", 'Lx!')
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +0000208<
Yegappan Lakshmananb0ad2d92022-01-27 13:16:59 +0000209 Returns TRUE if the event is successfully added, FALSE if
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +0000210 there is a failure.
Yegappan Lakshmananb0ad2d92022-01-27 13:16:59 +0000211
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +0000212 Can also be used as a |method|: >
213 GetEvent()->test_gui_event({args})
214<
Bram Moolenaared997ad2019-07-21 16:42:00 +0200215test_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 Moolenaarce90e362019-09-08 18:58:44 +0200225 Can also be used as a |method|: >
226 GetErrorText()->test_ignore_error()
Bram Moolenaared997ad2019-07-21 16:42:00 +0200227
Bram Moolenaarbe4e0162023-02-02 13:59:48 +0000228
Christopher Plewright20b795e2022-12-20 20:01:58 +0000229test_mswin_event({event}, {args}) *test_mswin_event()*
230 Generate a low-level MS-Windows {event} with arguments {args}
Bram Moolenaarbe4e0162023-02-02 13:59:48 +0000231 for testing Vim functionality. It works for MS-Windows GUI
Christopher Plewright20b795e2022-12-20 20:01:58 +0000232 and for the console.
Bram Moolenaarbe4e0162023-02-02 13:59:48 +0000233
Christopher Plewright20b795e2022-12-20 20:01:58 +0000234 {event} is a String and the supported values are:
235 "mouse" mouse event.
236 "key" keyboard event.
Anton Sharonov68d94722024-01-23 23:19:02 +0100237 "set_keycode_trans_strategy"
h_east026b1742024-02-19 02:51:46 +0900238 Change the key translation method.
Christopher Plewright20b795e2022-12-20 20:01:58 +0000239
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 Plewright7b0afc12022-12-30 16:54:58 +0000281 keycode: Keycode to use for a keyup or a keydown event.
Christopher Plewright20b795e2022-12-20 20:01:58 +0000282 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 Plewright7b0afc12022-12-30 16:54:58 +0000289 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 Sharonov68d94722024-01-23 23:19:02 +0100295 "set_keycode_trans_strategy":
296 |w32-experimental-keycode-trans-strategy|
h_east026b1742024-02-19 02:51:46 +0900297 Switch the keycode translation method. The supported methods
298 are:
Anton Sharonov68d94722024-01-23 23:19:02 +0100299 experimental: The method used after Patch v8.2.4807
300 using ToUnicode() Win API call.
301 classic: The method used pre Patch v8.2.4807
302 using the TranslateMessage() Win API call.
303
Christopher Plewright7b0afc12022-12-30 16:54:58 +0000304 Returns TRUE if the event is successfully added or executed,
305 FALSE if there is a failure.
Christopher Plewright20b795e2022-12-20 20:01:58 +0000306
307 Can also be used as a |method|: >
308 GetEvent()->test_mswin_event({args})
309<
Bram Moolenaar89a9c152021-08-29 21:55:35 +0200310
Bram Moolenaared997ad2019-07-21 16:42:00 +0200311test_null_blob() *test_null_blob()*
312 Return a |Blob| that is null. Only useful for testing.
313
314
315test_null_channel() *test_null_channel()*
316 Return a |Channel| that is null. Only useful for testing.
317 {only available when compiled with the +channel feature}
318
319
320test_null_dict() *test_null_dict()*
321 Return a |Dict| that is null. Only useful for testing.
322
323
Bram Moolenaare69f6d02020-04-01 22:11:01 +0200324test_null_function() *test_null_function()*
Bram Moolenaard1caa942020-04-10 22:10:56 +0200325 Return a |Funcref| that is null. Only useful for testing.
Bram Moolenaare69f6d02020-04-01 22:11:01 +0200326
327
Bram Moolenaared997ad2019-07-21 16:42:00 +0200328test_null_job() *test_null_job()*
329 Return a |Job| that is null. Only useful for testing.
330 {only available when compiled with the +job feature}
331
332
333test_null_list() *test_null_list()*
334 Return a |List| that is null. Only useful for testing.
335
336
337test_null_partial() *test_null_partial()*
338 Return a |Partial| that is null. Only useful for testing.
339
340
341test_null_string() *test_null_string()*
342 Return a |String| that is null. Only useful for testing.
343
344
345test_option_not_set({name}) *test_option_not_set()*
346 Reset the flag that indicates option {name} was set. Thus it
347 looks like it still has the default value. Use like this: >
348 set ambiwidth=double
349 call test_option_not_set('ambiwidth')
350< Now the 'ambiwidth' option behaves like it was never changed,
351 even though the value is "double".
352 Only to be used for testing!
353
Bram Moolenaarce90e362019-09-08 18:58:44 +0200354 Can also be used as a |method|: >
355 GetOptionName()->test_option_not_set()
356
Bram Moolenaared997ad2019-07-21 16:42:00 +0200357
358test_override({name}, {val}) *test_override()*
359 Overrides certain parts of Vim's internal processing to be able
360 to run tests. Only to be used for testing Vim!
361 The override is enabled when {val} is non-zero and removed
362 when {val} is zero.
Bram Moolenaar3e4fa3d2022-01-13 22:05:09 +0000363 Current supported values for {name} are:
Bram Moolenaared997ad2019-07-21 16:42:00 +0200364
Bram Moolenaar3e4fa3d2022-01-13 22:05:09 +0000365 {name} effect when {val} is non-zero ~
Bram Moolenaarfa4873c2022-06-30 22:13:59 +0100366 alloc_lines make a copy of every buffer line into allocated
367 memory, so that memory access errors can be found
368 by valgrind
Bram Moolenaar3e4fa3d2022-01-13 22:05:09 +0000369 autoload `import autoload` will load the script right
370 away, not postponed until an item is used
Bram Moolenaared997ad2019-07-21 16:42:00 +0200371 char_avail disable the char_avail() function
Bram Moolenaared997ad2019-07-21 16:42:00 +0200372 nfa_fail makes the NFA regexp engine fail to force a
373 fallback to the old engine
374 no_query_mouse do not query the mouse position for "dec"
375 terminals
376 no_wait_return set the "no_wait_return" flag. Not restored
377 with "ALL".
Bram Moolenaar3e4fa3d2022-01-13 22:05:09 +0000378 redraw disable the redrawing() function
379 redraw_flag ignore the RedrawingDisabled flag
380 starting reset the "starting" variable, see below
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +0200381 term_props reset all terminal properties when the version
382 string is detected
Bram Moolenaar3e4fa3d2022-01-13 22:05:09 +0000383 ui_delay time in msec to use in ui_delay(); overrules a
384 wait time of up to 3 seconds for messages
Bram Moolenaar9d383f32023-05-14 21:38:12 +0100385 unreachable no error for code after `:throw` and `:return`
Bram Moolenaar938ae282023-02-20 20:44:55 +0000386 uptime overrules sysinfo.uptime
ichizokae1bd872022-01-20 14:57:29 +0000387 vterm_title setting the window title by a job running in a
388 terminal window
Bram Moolenaarfa4873c2022-06-30 22:13:59 +0100389 ALL clear all overrides, except alloc_lines ({val} is
390 not used)
Bram Moolenaared997ad2019-07-21 16:42:00 +0200391
392 "starting" is to be used when a test should behave like
393 startup was done. Since the tests are run by sourcing a
394 script the "starting" variable is non-zero. This is usually a
Bram Moolenaar9d383f32023-05-14 21:38:12 +0100395 good thing (tests run faster), but sometimes this changes
396 behavior in a way that the test doesn't work properly.
Bram Moolenaared997ad2019-07-21 16:42:00 +0200397 When using: >
398 call test_override('starting', 1)
399< The value of "starting" is saved. It is restored by: >
400 call test_override('starting', 0)
401
Bram Moolenaar9d383f32023-05-14 21:38:12 +0100402< To make sure the flag is reset later using `:defer` can be
403 useful: >
404 call test_override('unreachable', 1)
405 defer call test_override('unreachable', 0)
406
Bram Moolenaarce90e362019-09-08 18:58:44 +0200407< Can also be used as a |method|: >
408 GetOverrideVal()-> test_override('starting')
Bram Moolenaared997ad2019-07-21 16:42:00 +0200409
Bram Moolenaar89a9c152021-08-29 21:55:35 +0200410
Bram Moolenaared997ad2019-07-21 16:42:00 +0200411test_refcount({expr}) *test_refcount()*
412 Return the reference count of {expr}. When {expr} is of a
413 type that does not have a reference count, returns -1. Only
414 to be used for testing.
415
Bram Moolenaarce90e362019-09-08 18:58:44 +0200416 Can also be used as a |method|: >
417 GetVarname()->test_refcount()
418
Bram Moolenaared997ad2019-07-21 16:42:00 +0200419
Bram Moolenaared997ad2019-07-21 16:42:00 +0200420test_setmouse({row}, {col}) *test_setmouse()*
421 Set the mouse position to be used for the next mouse action.
422 {row} and {col} are one based.
423 For example: >
424 call test_setmouse(4, 20)
425 call feedkeys("\<LeftMouse>", "xt")
426
Bram Moolenaar89a9c152021-08-29 21:55:35 +0200427
Bram Moolenaared997ad2019-07-21 16:42:00 +0200428test_settime({expr}) *test_settime()*
429 Set the time Vim uses internally. Currently only used for
430 timestamps in the history, as they are used in viminfo, and
431 for undo.
432 Using a value of 1 makes Vim not sleep after a warning or
433 error message.
434 {expr} must evaluate to a number. When the value is zero the
435 normal behavior is restored.
436
Bram Moolenaarce90e362019-09-08 18:58:44 +0200437 Can also be used as a |method|: >
438 GetTime()->test_settime()
439
Bram Moolenaar89a9c152021-08-29 21:55:35 +0200440
Bram Moolenaar4f645c52020-02-08 16:40:39 +0100441test_srand_seed([seed]) *test_srand_seed()*
442 When [seed] is given this sets the seed value used by
443 `srand()`. When omitted the test seed is removed.
444
Bram Moolenaar89a9c152021-08-29 21:55:35 +0200445
446test_unknown() *test_unknown()*
447 Return a value with unknown type. Only useful for testing.
448
449
450test_void() *test_void()*
451 Return a value with void type. Only useful for testing.
452
Bram Moolenaared997ad2019-07-21 16:42:00 +0200453==============================================================================
Bram Moolenaar54775062019-07-31 21:07:14 +02004543. Assert functions *assert-functions-details*
Bram Moolenaared997ad2019-07-21 16:42:00 +0200455
456
457assert_beeps({cmd}) *assert_beeps()*
458 Run {cmd} and add an error message to |v:errors| if it does
459 NOT produce a beep or visual bell.
Bram Moolenaar5b8cabf2021-04-02 18:55:57 +0200460 Also see |assert_fails()|, |assert_nobeep()| and
461 |assert-return|.
Bram Moolenaared997ad2019-07-21 16:42:00 +0200462
Bram Moolenaar24278d22019-08-16 21:49:22 +0200463 Can also be used as a |method|: >
464 GetCmd()->assert_beeps()
465<
Bram Moolenaared997ad2019-07-21 16:42:00 +0200466 *assert_equal()*
467assert_equal({expected}, {actual} [, {msg}])
468 When {expected} and {actual} are not equal an error message is
469 added to |v:errors| and 1 is returned. Otherwise zero is
Bram Moolenaarb7398fe2023-05-14 18:50:25 +0100470 returned. |assert-return|
471 The error is in the form "Expected {expected} but got
472 {actual}". When {msg} is present it is prefixed to that.
473
Bram Moolenaared997ad2019-07-21 16:42:00 +0200474 There is no automatic conversion, the String "4" is different
475 from the Number 4. And the number 4 is different from the
476 Float 4.0. The value of 'ignorecase' is not used here, case
477 always matters.
Bram Moolenaared997ad2019-07-21 16:42:00 +0200478 Example: >
479 assert_equal('foo', 'bar')
480< Will result in a string to be added to |v:errors|:
481 test.vim line 12: Expected 'foo' but got 'bar' ~
482
Bram Moolenaar7ff78462020-07-10 22:00:53 +0200483 Can also be used as a |method|, the base is passed as the
484 second argument: >
Bram Moolenaar25e42232019-08-04 15:04:10 +0200485 mylist->assert_equal([1, 2, 3])
486
Bram Moolenaar25e42232019-08-04 15:04:10 +0200487< *assert_equalfile()*
Bram Moolenaarfb517ba2020-06-03 19:55:35 +0200488assert_equalfile({fname-one}, {fname-two} [, {msg}])
Bram Moolenaared997ad2019-07-21 16:42:00 +0200489 When the files {fname-one} and {fname-two} do not contain
490 exactly the same text an error message is added to |v:errors|.
491 Also see |assert-return|.
492 When {fname-one} or {fname-two} does not exist the error will
493 mention that.
494 Mainly useful with |terminal-diff|.
495
Bram Moolenaare49fbff2019-08-21 22:50:07 +0200496 Can also be used as a |method|: >
497 GetLog()->assert_equalfile('expected.log')
498
Bram Moolenaared997ad2019-07-21 16:42:00 +0200499assert_exception({error} [, {msg}]) *assert_exception()*
500 When v:exception does not contain the string {error} an error
501 message is added to |v:errors|. Also see |assert-return|.
502 This can be used to assert that a command throws an exception.
503 Using the error number, followed by a colon, avoids problems
504 with translations: >
505 try
506 commandthatfails
507 call assert_false(1, 'command should have failed')
508 catch
509 call assert_exception('E492:')
510 endtry
Bram Moolenaar1d634542020-08-18 13:41:50 +0200511<
512 *assert_fails()*
Bram Moolenaar9bd5d872020-09-06 21:47:48 +0200513assert_fails({cmd} [, {error} [, {msg} [, {lnum} [, {context}]]]])
Bram Moolenaared997ad2019-07-21 16:42:00 +0200514 Run {cmd} and add an error message to |v:errors| if it does
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200515 NOT produce an error or when {error} is not found in the
516 error message. Also see |assert-return|.
Bram Moolenaar6f4754b2022-01-23 12:07:04 +0000517 *E856*
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200518 When {error} is a string it must be found literally in the
519 first reported error. Most often this will be the error code,
520 including the colon, e.g. "E123:". >
521 assert_fails('bad cmd', 'E987:')
522<
523 When {error} is a |List| with one or two strings, these are
524 used as patterns. The first pattern is matched against the
525 first reported error: >
526 assert_fails('cmd', ['E987:.*expected bool'])
527< The second pattern, if present, is matched against the last
Bram Moolenaar4072ba52020-12-23 13:56:35 +0100528 reported error.
529 If there is only one error then both patterns must match. This
530 can be used to check that there is only one error.
531 To only match the last error use an empty string for the first
532 error: >
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200533 assert_fails('cmd', ['', 'E987:'])
534<
Bram Moolenaar1d634542020-08-18 13:41:50 +0200535 If {msg} is empty then it is not used. Do this to get the
536 default message when passing the {lnum} argument.
Bram Moolenaarf10911e2022-01-29 22:20:48 +0000537 *E1115*
Bram Moolenaar1d634542020-08-18 13:41:50 +0200538 When {lnum} is present and not negative, and the {error}
539 argument is present and matches, then this is compared with
540 the line number at which the error was reported. That can be
541 the line number in a function or in a script.
Bram Moolenaarf10911e2022-01-29 22:20:48 +0000542 *E1116*
Bram Moolenaar9bd5d872020-09-06 21:47:48 +0200543 When {context} is present it is used as a pattern and matched
544 against the context (script name or function name) where
545 {lnum} is located in.
546
Bram Moolenaared997ad2019-07-21 16:42:00 +0200547 Note that beeping is not considered an error, and some failing
548 commands only beep. Use |assert_beeps()| for those.
549
Bram Moolenaar24278d22019-08-16 21:49:22 +0200550 Can also be used as a |method|: >
551 GetCmd()->assert_fails('E99:')
552
Bram Moolenaar1d634542020-08-18 13:41:50 +0200553assert_false({actual} [, {msg}]) *assert_false()*
Bram Moolenaared997ad2019-07-21 16:42:00 +0200554 When {actual} is not false an error message is added to
555 |v:errors|, like with |assert_equal()|.
Bram Moolenaarb7398fe2023-05-14 18:50:25 +0100556 The error is in the form "Expected False but got {actual}".
557 When {msg} is present it is prepended to that.
Bram Moolenaared997ad2019-07-21 16:42:00 +0200558 Also see |assert-return|.
Bram Moolenaarb7398fe2023-05-14 18:50:25 +0100559
Bram Moolenaared997ad2019-07-21 16:42:00 +0200560 A value is false when it is zero. When {actual} is not a
561 number the assert fails.
Bram Moolenaared997ad2019-07-21 16:42:00 +0200562
Bram Moolenaar24278d22019-08-16 21:49:22 +0200563 Can also be used as a |method|: >
564 GetResult()->assert_false()
565
Bram Moolenaared997ad2019-07-21 16:42:00 +0200566assert_inrange({lower}, {upper}, {actual} [, {msg}]) *assert_inrange()*
567 This asserts number and |Float| values. When {actual} is lower
568 than {lower} or higher than {upper} an error message is added
569 to |v:errors|. Also see |assert-return|.
Bram Moolenaarb7398fe2023-05-14 18:50:25 +0100570 The error is in the form "Expected range {lower} - {upper},
571 but got {actual}". When {msg} is present it is prefixed to
572 that.
Bram Moolenaared997ad2019-07-21 16:42:00 +0200573
574 *assert_match()*
575assert_match({pattern}, {actual} [, {msg}])
576 When {pattern} does not match {actual} an error message is
577 added to |v:errors|. Also see |assert-return|.
Bram Moolenaarb7398fe2023-05-14 18:50:25 +0100578 The error is in the form "Pattern {pattern} does not match
579 {actual}". When {msg} is present it is prefixed to that.
Bram Moolenaared997ad2019-07-21 16:42:00 +0200580
581 {pattern} is used as with |=~|: The matching is always done
582 like 'magic' was set and 'cpoptions' is empty, no matter what
583 the actual value of 'magic' or 'cpoptions' is.
584
585 {actual} is used as a string, automatic conversion applies.
586 Use "^" and "$" to match with the start and end of the text.
587 Use both to match the whole text.
588
Bram Moolenaared997ad2019-07-21 16:42:00 +0200589 Example: >
590 assert_match('^f.*o$', 'foobar')
591< Will result in a string to be added to |v:errors|:
592 test.vim line 12: Pattern '^f.*o$' does not match 'foobar' ~
593
Bram Moolenaar24278d22019-08-16 21:49:22 +0200594 Can also be used as a |method|: >
595 getFile()->assert_match('foo.*')
596<
Bram Moolenaar5b8cabf2021-04-02 18:55:57 +0200597assert_nobeep({cmd}) *assert_nobeep()*
598 Run {cmd} and add an error message to |v:errors| if it
599 produces a beep or visual bell.
600 Also see |assert_beeps()|.
601
602 Can also be used as a |method|: >
603 GetCmd()->assert_nobeep()
604<
Bram Moolenaared997ad2019-07-21 16:42:00 +0200605 *assert_notequal()*
606assert_notequal({expected}, {actual} [, {msg}])
607 The opposite of `assert_equal()`: add an error message to
608 |v:errors| when {expected} and {actual} are equal.
609 Also see |assert-return|.
610
Bram Moolenaar25e42232019-08-04 15:04:10 +0200611 Can also be used as a |method|: >
612 mylist->assert_notequal([1, 2, 3])
613
614< *assert_notmatch()*
Bram Moolenaared997ad2019-07-21 16:42:00 +0200615assert_notmatch({pattern}, {actual} [, {msg}])
616 The opposite of `assert_match()`: add an error message to
617 |v:errors| when {pattern} matches {actual}.
618 Also see |assert-return|.
619
Bram Moolenaar24278d22019-08-16 21:49:22 +0200620 Can also be used as a |method|: >
621 getFile()->assert_notmatch('bar.*')
622
Bram Moolenaare49fbff2019-08-21 22:50:07 +0200623
Bram Moolenaared997ad2019-07-21 16:42:00 +0200624assert_report({msg}) *assert_report()*
Bram Moolenaar6aa57292021-08-14 21:25:52 +0200625 Report a test failure directly, using String {msg}.
Bram Moolenaared997ad2019-07-21 16:42:00 +0200626 Always returns one.
627
Bram Moolenaare49fbff2019-08-21 22:50:07 +0200628 Can also be used as a |method|: >
629 GetMessage()->assert_report()
630
631
Bram Moolenaared997ad2019-07-21 16:42:00 +0200632assert_true({actual} [, {msg}]) *assert_true()*
633 When {actual} is not true an error message is added to
634 |v:errors|, like with |assert_equal()|.
635 Also see |assert-return|.
636 A value is TRUE when it is a non-zero number. When {actual}
637 is not a number the assert fails.
Bram Moolenaarb7398fe2023-05-14 18:50:25 +0100638 When {msg} is given it precedes the default message.
Bram Moolenaared997ad2019-07-21 16:42:00 +0200639
Bram Moolenaar24278d22019-08-16 21:49:22 +0200640 Can also be used as a |method|: >
641 GetResult()->assert_true()
642<
Bram Moolenaared997ad2019-07-21 16:42:00 +0200643
644 vim:tw=78:ts=8:noet:ft=help:norl: