blob: 2811568fc64c9799aeee508504e5b016e00e4dda [file] [log] [blame]
Bram Moolenaarfd31be22022-01-16 14:46:06 +00001*testing.txt* For Vim version 8.2. Last change: 2022 Jan 13
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:
24 test20.in oldest, only for tiny and small builds
25 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 Moolenaar0e6adf82021-12-16 14:41:10 +000068 any function. This will not work when called from a :def
69 function, because variables on the stack will be freed.
Bram Moolenaared997ad2019-07-21 16:42:00 +020070
71
72test_garbagecollect_soon() *test_garbagecollect_soon()*
73 Set the flag to call the garbagecollector as if in the main
74 loop. Only to be used in tests.
75
76
77test_getvalue({name}) *test_getvalue()*
78 Get the value of an internal variable. These values for
79 {name} are supported:
80 need_fileinfo
81
Bram Moolenaarce90e362019-09-08 18:58:44 +020082 Can also be used as a |method|: >
83 GetName()->test_getvalue()
Yegappan Lakshmananf1e74492021-06-21 18:44:26 +020084<
Yegappan Lakshmanan18d46582021-06-23 20:46:52 +020085 *test_gui_drop_files()*
86test_gui_drop_files({list}, {row}, {col}, {mods})
87 Drop one or more files in {list} in the window at {row}, {col}.
Bram Moolenaar90df4b92021-07-07 20:26:08 +020088 This function only works when the GUI is running and the
Bram Moolenaar22863042021-10-16 15:23:36 +010089 |drop_file| feature is present.
90
Yegappan Lakshmanan18d46582021-06-23 20:46:52 +020091 The supported values for {mods} are:
92 0x4 Shift
93 0x8 Alt
94 0x10 Ctrl
Bram Moolenaar22863042021-10-16 15:23:36 +010095 The files are added to the |argument-list| and the first file
96 in {list} is edited in the window. See |drag-n-drop| for more
Yegappan Lakshmanan18d46582021-06-23 20:46:52 +020097 information.
98
Yegappan Lakshmananf1e74492021-06-21 18:44:26 +020099 *test_gui_mouse_event()*
100test_gui_mouse_event({button}, {row}, {col}, {multiclick}, {modifiers})
Yegappan Lakshmanan18d46582021-06-23 20:46:52 +0200101 Inject a mouse button click event. This function only works
102 when the GUI is running.
Yegappan Lakshmananf1e74492021-06-21 18:44:26 +0200103 The supported values for {button} are:
104 0 right mouse button
105 1 middle mouse button
106 2 left mouse button
107 3 mouse button release
108 4 scroll wheel down
109 5 scroll wheel up
110 6 scroll wheel left
111 7 scroll wheel right
Yegappan Lakshmanan18d46582021-06-23 20:46:52 +0200112 {row} and {col} specify the location of the mouse click. The
113 first row of the Vim window is 1 and the last row is 'lines'.
114 The maximum value of {col} is 'columns'.
Yegappan Lakshmananf1e74492021-06-21 18:44:26 +0200115 To inject a multiclick event, set {multiclick} to 1.
116 The supported values for {modifiers} are:
117 4 shift is pressed
118 8 alt is pressed
119 16 ctrl is pressed
120 After injecting the mouse event you probably should call
121 |feedkeys()| to have them processed, e.g.: >
122 call feedkeys("y", 'Lx!')
123
Bram Moolenaared997ad2019-07-21 16:42:00 +0200124
125test_ignore_error({expr}) *test_ignore_error()*
126 Ignore any error containing {expr}. A normal message is given
127 instead.
128 This is only meant to be used in tests, where catching the
129 error with try/catch cannot be used (because it skips over
130 following code).
131 {expr} is used literally, not as a pattern.
132 When the {expr} is the string "RESET" then the list of ignored
133 errors is made empty.
134
Bram Moolenaarce90e362019-09-08 18:58:44 +0200135 Can also be used as a |method|: >
136 GetErrorText()->test_ignore_error()
Bram Moolenaared997ad2019-07-21 16:42:00 +0200137
Bram Moolenaar89a9c152021-08-29 21:55:35 +0200138
Bram Moolenaared997ad2019-07-21 16:42:00 +0200139test_null_blob() *test_null_blob()*
140 Return a |Blob| that is null. Only useful for testing.
141
142
143test_null_channel() *test_null_channel()*
144 Return a |Channel| that is null. Only useful for testing.
145 {only available when compiled with the +channel feature}
146
147
148test_null_dict() *test_null_dict()*
149 Return a |Dict| that is null. Only useful for testing.
150
151
Bram Moolenaare69f6d02020-04-01 22:11:01 +0200152test_null_function() *test_null_function()*
Bram Moolenaard1caa942020-04-10 22:10:56 +0200153 Return a |Funcref| that is null. Only useful for testing.
Bram Moolenaare69f6d02020-04-01 22:11:01 +0200154
155
Bram Moolenaared997ad2019-07-21 16:42:00 +0200156test_null_job() *test_null_job()*
157 Return a |Job| that is null. Only useful for testing.
158 {only available when compiled with the +job feature}
159
160
161test_null_list() *test_null_list()*
162 Return a |List| that is null. Only useful for testing.
163
164
165test_null_partial() *test_null_partial()*
166 Return a |Partial| that is null. Only useful for testing.
167
168
169test_null_string() *test_null_string()*
170 Return a |String| that is null. Only useful for testing.
171
172
173test_option_not_set({name}) *test_option_not_set()*
174 Reset the flag that indicates option {name} was set. Thus it
175 looks like it still has the default value. Use like this: >
176 set ambiwidth=double
177 call test_option_not_set('ambiwidth')
178< Now the 'ambiwidth' option behaves like it was never changed,
179 even though the value is "double".
180 Only to be used for testing!
181
Bram Moolenaarce90e362019-09-08 18:58:44 +0200182 Can also be used as a |method|: >
183 GetOptionName()->test_option_not_set()
184
Bram Moolenaared997ad2019-07-21 16:42:00 +0200185
186test_override({name}, {val}) *test_override()*
187 Overrides certain parts of Vim's internal processing to be able
188 to run tests. Only to be used for testing Vim!
189 The override is enabled when {val} is non-zero and removed
190 when {val} is zero.
Bram Moolenaar3e4fa3d2022-01-13 22:05:09 +0000191 Current supported values for {name} are:
Bram Moolenaared997ad2019-07-21 16:42:00 +0200192
Bram Moolenaar3e4fa3d2022-01-13 22:05:09 +0000193 {name} effect when {val} is non-zero ~
194 autoload `import autoload` will load the script right
195 away, not postponed until an item is used
Bram Moolenaared997ad2019-07-21 16:42:00 +0200196 char_avail disable the char_avail() function
Bram Moolenaared997ad2019-07-21 16:42:00 +0200197 nfa_fail makes the NFA regexp engine fail to force a
198 fallback to the old engine
199 no_query_mouse do not query the mouse position for "dec"
200 terminals
201 no_wait_return set the "no_wait_return" flag. Not restored
202 with "ALL".
Bram Moolenaar3e4fa3d2022-01-13 22:05:09 +0000203 redraw disable the redrawing() function
204 redraw_flag ignore the RedrawingDisabled flag
205 starting reset the "starting" variable, see below
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +0200206 term_props reset all terminal properties when the version
207 string is detected
Bram Moolenaar3e4fa3d2022-01-13 22:05:09 +0000208 ui_delay time in msec to use in ui_delay(); overrules a
209 wait time of up to 3 seconds for messages
Bram Moolenaarc8cdf0f2021-03-13 13:28:13 +0100210 uptime overrules sysinfo.uptime
ichizokae1bd872022-01-20 14:57:29 +0000211 vterm_title setting the window title by a job running in a
212 terminal window
Bram Moolenaared997ad2019-07-21 16:42:00 +0200213 ALL clear all overrides ({val} is not used)
214
215 "starting" is to be used when a test should behave like
216 startup was done. Since the tests are run by sourcing a
217 script the "starting" variable is non-zero. This is usually a
218 good thing (tests run faster), but sometimes changes behavior
219 in a way that the test doesn't work properly.
220 When using: >
221 call test_override('starting', 1)
222< The value of "starting" is saved. It is restored by: >
223 call test_override('starting', 0)
224
Bram Moolenaarce90e362019-09-08 18:58:44 +0200225< Can also be used as a |method|: >
226 GetOverrideVal()-> test_override('starting')
Bram Moolenaared997ad2019-07-21 16:42:00 +0200227
Bram Moolenaar89a9c152021-08-29 21:55:35 +0200228
Bram Moolenaared997ad2019-07-21 16:42:00 +0200229test_refcount({expr}) *test_refcount()*
230 Return the reference count of {expr}. When {expr} is of a
231 type that does not have a reference count, returns -1. Only
232 to be used for testing.
233
Bram Moolenaarce90e362019-09-08 18:58:44 +0200234 Can also be used as a |method|: >
235 GetVarname()->test_refcount()
236
Bram Moolenaared997ad2019-07-21 16:42:00 +0200237
238test_scrollbar({which}, {value}, {dragging}) *test_scrollbar()*
239 Pretend using scrollbar {which} to move it to position
240 {value}. {which} can be:
241 left Left scrollbar of the current window
242 right Right scrollbar of the current window
243 hor Horizontal scrollbar
244
245 For the vertical scrollbars {value} can be 1 to the
246 line-count of the buffer. For the horizontal scrollbar the
247 {value} can be between 1 and the maximum line length, assuming
248 'wrap' is not set.
249
250 When {dragging} is non-zero it's like dragging the scrollbar,
251 otherwise it's like clicking in the scrollbar.
252 Only works when the {which} scrollbar actually exists,
253 obviously only when using the GUI.
254
Bram Moolenaarce90e362019-09-08 18:58:44 +0200255 Can also be used as a |method|: >
256 GetValue()->test_scrollbar('right', 0)
Bram Moolenaared997ad2019-07-21 16:42:00 +0200257
Bram Moolenaar89a9c152021-08-29 21:55:35 +0200258
Bram Moolenaared997ad2019-07-21 16:42:00 +0200259test_setmouse({row}, {col}) *test_setmouse()*
260 Set the mouse position to be used for the next mouse action.
261 {row} and {col} are one based.
262 For example: >
263 call test_setmouse(4, 20)
264 call feedkeys("\<LeftMouse>", "xt")
265
Bram Moolenaar89a9c152021-08-29 21:55:35 +0200266
Bram Moolenaared997ad2019-07-21 16:42:00 +0200267test_settime({expr}) *test_settime()*
268 Set the time Vim uses internally. Currently only used for
269 timestamps in the history, as they are used in viminfo, and
270 for undo.
271 Using a value of 1 makes Vim not sleep after a warning or
272 error message.
273 {expr} must evaluate to a number. When the value is zero the
274 normal behavior is restored.
275
Bram Moolenaarce90e362019-09-08 18:58:44 +0200276 Can also be used as a |method|: >
277 GetTime()->test_settime()
278
Bram Moolenaar89a9c152021-08-29 21:55:35 +0200279
Bram Moolenaar4f645c52020-02-08 16:40:39 +0100280test_srand_seed([seed]) *test_srand_seed()*
281 When [seed] is given this sets the seed value used by
282 `srand()`. When omitted the test seed is removed.
283
Bram Moolenaar89a9c152021-08-29 21:55:35 +0200284
285test_unknown() *test_unknown()*
286 Return a value with unknown type. Only useful for testing.
287
288
289test_void() *test_void()*
290 Return a value with void type. Only useful for testing.
291
Bram Moolenaared997ad2019-07-21 16:42:00 +0200292==============================================================================
Bram Moolenaar54775062019-07-31 21:07:14 +02002933. Assert functions *assert-functions-details*
Bram Moolenaared997ad2019-07-21 16:42:00 +0200294
295
296assert_beeps({cmd}) *assert_beeps()*
297 Run {cmd} and add an error message to |v:errors| if it does
298 NOT produce a beep or visual bell.
Bram Moolenaar5b8cabf2021-04-02 18:55:57 +0200299 Also see |assert_fails()|, |assert_nobeep()| and
300 |assert-return|.
Bram Moolenaared997ad2019-07-21 16:42:00 +0200301
Bram Moolenaar24278d22019-08-16 21:49:22 +0200302 Can also be used as a |method|: >
303 GetCmd()->assert_beeps()
304<
Bram Moolenaared997ad2019-07-21 16:42:00 +0200305 *assert_equal()*
306assert_equal({expected}, {actual} [, {msg}])
307 When {expected} and {actual} are not equal an error message is
308 added to |v:errors| and 1 is returned. Otherwise zero is
309 returned |assert-return|.
310 There is no automatic conversion, the String "4" is different
311 from the Number 4. And the number 4 is different from the
312 Float 4.0. The value of 'ignorecase' is not used here, case
313 always matters.
314 When {msg} is omitted an error in the form "Expected
315 {expected} but got {actual}" is produced.
316 Example: >
317 assert_equal('foo', 'bar')
318< Will result in a string to be added to |v:errors|:
319 test.vim line 12: Expected 'foo' but got 'bar' ~
320
Bram Moolenaar7ff78462020-07-10 22:00:53 +0200321 Can also be used as a |method|, the base is passed as the
322 second argument: >
Bram Moolenaar25e42232019-08-04 15:04:10 +0200323 mylist->assert_equal([1, 2, 3])
324
Bram Moolenaar25e42232019-08-04 15:04:10 +0200325< *assert_equalfile()*
Bram Moolenaarfb517ba2020-06-03 19:55:35 +0200326assert_equalfile({fname-one}, {fname-two} [, {msg}])
Bram Moolenaared997ad2019-07-21 16:42:00 +0200327 When the files {fname-one} and {fname-two} do not contain
328 exactly the same text an error message is added to |v:errors|.
329 Also see |assert-return|.
330 When {fname-one} or {fname-two} does not exist the error will
331 mention that.
332 Mainly useful with |terminal-diff|.
333
Bram Moolenaare49fbff2019-08-21 22:50:07 +0200334 Can also be used as a |method|: >
335 GetLog()->assert_equalfile('expected.log')
336
Bram Moolenaared997ad2019-07-21 16:42:00 +0200337assert_exception({error} [, {msg}]) *assert_exception()*
338 When v:exception does not contain the string {error} an error
339 message is added to |v:errors|. Also see |assert-return|.
340 This can be used to assert that a command throws an exception.
341 Using the error number, followed by a colon, avoids problems
342 with translations: >
343 try
344 commandthatfails
345 call assert_false(1, 'command should have failed')
346 catch
347 call assert_exception('E492:')
348 endtry
Bram Moolenaar1d634542020-08-18 13:41:50 +0200349<
350 *assert_fails()*
Bram Moolenaar9bd5d872020-09-06 21:47:48 +0200351assert_fails({cmd} [, {error} [, {msg} [, {lnum} [, {context}]]]])
Bram Moolenaared997ad2019-07-21 16:42:00 +0200352 Run {cmd} and add an error message to |v:errors| if it does
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200353 NOT produce an error or when {error} is not found in the
354 error message. Also see |assert-return|.
355
356 When {error} is a string it must be found literally in the
357 first reported error. Most often this will be the error code,
358 including the colon, e.g. "E123:". >
359 assert_fails('bad cmd', 'E987:')
360<
361 When {error} is a |List| with one or two strings, these are
362 used as patterns. The first pattern is matched against the
363 first reported error: >
364 assert_fails('cmd', ['E987:.*expected bool'])
365< The second pattern, if present, is matched against the last
Bram Moolenaar4072ba52020-12-23 13:56:35 +0100366 reported error.
367 If there is only one error then both patterns must match. This
368 can be used to check that there is only one error.
369 To only match the last error use an empty string for the first
370 error: >
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200371 assert_fails('cmd', ['', 'E987:'])
372<
Bram Moolenaar1d634542020-08-18 13:41:50 +0200373 If {msg} is empty then it is not used. Do this to get the
374 default message when passing the {lnum} argument.
375
376 When {lnum} is present and not negative, and the {error}
377 argument is present and matches, then this is compared with
378 the line number at which the error was reported. That can be
379 the line number in a function or in a script.
380
Bram Moolenaar9bd5d872020-09-06 21:47:48 +0200381 When {context} is present it is used as a pattern and matched
382 against the context (script name or function name) where
383 {lnum} is located in.
384
Bram Moolenaared997ad2019-07-21 16:42:00 +0200385 Note that beeping is not considered an error, and some failing
386 commands only beep. Use |assert_beeps()| for those.
387
Bram Moolenaar24278d22019-08-16 21:49:22 +0200388 Can also be used as a |method|: >
389 GetCmd()->assert_fails('E99:')
390
Bram Moolenaar1d634542020-08-18 13:41:50 +0200391assert_false({actual} [, {msg}]) *assert_false()*
Bram Moolenaared997ad2019-07-21 16:42:00 +0200392 When {actual} is not false an error message is added to
393 |v:errors|, like with |assert_equal()|.
394 Also see |assert-return|.
395 A value is false when it is zero. When {actual} is not a
396 number the assert fails.
397 When {msg} is omitted an error in the form
398 "Expected False but got {actual}" is produced.
399
Bram Moolenaar24278d22019-08-16 21:49:22 +0200400 Can also be used as a |method|: >
401 GetResult()->assert_false()
402
Bram Moolenaared997ad2019-07-21 16:42:00 +0200403assert_inrange({lower}, {upper}, {actual} [, {msg}]) *assert_inrange()*
404 This asserts number and |Float| values. When {actual} is lower
405 than {lower} or higher than {upper} an error message is added
406 to |v:errors|. Also see |assert-return|.
407 When {msg} is omitted an error in the form
408 "Expected range {lower} - {upper}, but got {actual}" is
409 produced.
410
411 *assert_match()*
412assert_match({pattern}, {actual} [, {msg}])
413 When {pattern} does not match {actual} an error message is
414 added to |v:errors|. Also see |assert-return|.
415
416 {pattern} is used as with |=~|: The matching is always done
417 like 'magic' was set and 'cpoptions' is empty, no matter what
418 the actual value of 'magic' or 'cpoptions' is.
419
420 {actual} is used as a string, automatic conversion applies.
421 Use "^" and "$" to match with the start and end of the text.
422 Use both to match the whole text.
423
424 When {msg} is omitted an error in the form
425 "Pattern {pattern} does not match {actual}" is produced.
426 Example: >
427 assert_match('^f.*o$', 'foobar')
428< Will result in a string to be added to |v:errors|:
429 test.vim line 12: Pattern '^f.*o$' does not match 'foobar' ~
430
Bram Moolenaar24278d22019-08-16 21:49:22 +0200431 Can also be used as a |method|: >
432 getFile()->assert_match('foo.*')
433<
Bram Moolenaar5b8cabf2021-04-02 18:55:57 +0200434assert_nobeep({cmd}) *assert_nobeep()*
435 Run {cmd} and add an error message to |v:errors| if it
436 produces a beep or visual bell.
437 Also see |assert_beeps()|.
438
439 Can also be used as a |method|: >
440 GetCmd()->assert_nobeep()
441<
Bram Moolenaared997ad2019-07-21 16:42:00 +0200442 *assert_notequal()*
443assert_notequal({expected}, {actual} [, {msg}])
444 The opposite of `assert_equal()`: add an error message to
445 |v:errors| when {expected} and {actual} are equal.
446 Also see |assert-return|.
447
Bram Moolenaar25e42232019-08-04 15:04:10 +0200448 Can also be used as a |method|: >
449 mylist->assert_notequal([1, 2, 3])
450
451< *assert_notmatch()*
Bram Moolenaared997ad2019-07-21 16:42:00 +0200452assert_notmatch({pattern}, {actual} [, {msg}])
453 The opposite of `assert_match()`: add an error message to
454 |v:errors| when {pattern} matches {actual}.
455 Also see |assert-return|.
456
Bram Moolenaar24278d22019-08-16 21:49:22 +0200457 Can also be used as a |method|: >
458 getFile()->assert_notmatch('bar.*')
459
Bram Moolenaare49fbff2019-08-21 22:50:07 +0200460
Bram Moolenaared997ad2019-07-21 16:42:00 +0200461assert_report({msg}) *assert_report()*
Bram Moolenaar6aa57292021-08-14 21:25:52 +0200462 Report a test failure directly, using String {msg}.
Bram Moolenaared997ad2019-07-21 16:42:00 +0200463 Always returns one.
464
Bram Moolenaare49fbff2019-08-21 22:50:07 +0200465 Can also be used as a |method|: >
466 GetMessage()->assert_report()
467
468
Bram Moolenaared997ad2019-07-21 16:42:00 +0200469assert_true({actual} [, {msg}]) *assert_true()*
470 When {actual} is not true an error message is added to
471 |v:errors|, like with |assert_equal()|.
472 Also see |assert-return|.
473 A value is TRUE when it is a non-zero number. When {actual}
474 is not a number the assert fails.
475 When {msg} is omitted an error in the form "Expected True but
476 got {actual}" is produced.
477
Bram Moolenaar24278d22019-08-16 21:49:22 +0200478 Can also be used as a |method|: >
479 GetResult()->assert_true()
480<
Bram Moolenaared997ad2019-07-21 16:42:00 +0200481
482 vim:tw=78:ts=8:noet:ft=help:norl: