blob: eef322714e5563b730c0d5dfc2e164da842328bd [file] [log] [blame]
Yegappan Lakshmananf1e74492021-06-21 18:44:26 +02001*testing.txt* For Vim version 8.2. Last change: 2021 Jun 21
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
49test_autochdir() *test_autochdir()*
50 Set a flag to enable the effect of 'autochdir' before Vim
51 startup has finished.
52
53
54test_feedinput({string}) *test_feedinput()*
55 Characters in {string} are queued for processing as if they
56 were typed by the user. This uses a low level input buffer.
57 This function works only when with |+unix| or GUI is running.
58
Bram Moolenaarce90e362019-09-08 18:58:44 +020059 Can also be used as a |method|: >
60 GetText()->test_feedinput()
Bram Moolenaared997ad2019-07-21 16:42:00 +020061
62test_garbagecollect_now() *test_garbagecollect_now()*
63 Like garbagecollect(), but executed right away. This must
64 only be called directly to avoid any structure to exist
65 internally, and |v:testing| must have been set before calling
66 any function.
67
68
69test_garbagecollect_soon() *test_garbagecollect_soon()*
70 Set the flag to call the garbagecollector as if in the main
71 loop. Only to be used in tests.
72
73
74test_getvalue({name}) *test_getvalue()*
75 Get the value of an internal variable. These values for
76 {name} are supported:
77 need_fileinfo
78
Bram Moolenaarce90e362019-09-08 18:58:44 +020079 Can also be used as a |method|: >
80 GetName()->test_getvalue()
Yegappan Lakshmananf1e74492021-06-21 18:44:26 +020081<
Yegappan Lakshmanan18d46582021-06-23 20:46:52 +020082 *test_gui_drop_files()*
83test_gui_drop_files({list}, {row}, {col}, {mods})
84 Drop one or more files in {list} in the window at {row}, {col}.
85 This function only works when the GUI is running.
86
87 The supported values for {mods} are:
88 0x4 Shift
89 0x8 Alt
90 0x10 Ctrl
91 The files are added to the argument list and the first file in
92 {list} is edited in the window. See |drag-n-drop| for more
93 information.
94
Yegappan Lakshmananf1e74492021-06-21 18:44:26 +020095 *test_gui_mouse_event()*
96test_gui_mouse_event({button}, {row}, {col}, {multiclick}, {modifiers})
Yegappan Lakshmanan18d46582021-06-23 20:46:52 +020097 Inject a mouse button click event. This function only works
98 when the GUI is running.
Yegappan Lakshmananf1e74492021-06-21 18:44:26 +020099 The supported values for {button} are:
100 0 right mouse button
101 1 middle mouse button
102 2 left mouse button
103 3 mouse button release
104 4 scroll wheel down
105 5 scroll wheel up
106 6 scroll wheel left
107 7 scroll wheel right
Yegappan Lakshmanan18d46582021-06-23 20:46:52 +0200108 {row} and {col} specify the location of the mouse click. The
109 first row of the Vim window is 1 and the last row is 'lines'.
110 The maximum value of {col} is 'columns'.
Yegappan Lakshmananf1e74492021-06-21 18:44:26 +0200111 To inject a multiclick event, set {multiclick} to 1.
112 The supported values for {modifiers} are:
113 4 shift is pressed
114 8 alt is pressed
115 16 ctrl is pressed
116 After injecting the mouse event you probably should call
117 |feedkeys()| to have them processed, e.g.: >
118 call feedkeys("y", 'Lx!')
119
Bram Moolenaared997ad2019-07-21 16:42:00 +0200120
121test_ignore_error({expr}) *test_ignore_error()*
122 Ignore any error containing {expr}. A normal message is given
123 instead.
124 This is only meant to be used in tests, where catching the
125 error with try/catch cannot be used (because it skips over
126 following code).
127 {expr} is used literally, not as a pattern.
128 When the {expr} is the string "RESET" then the list of ignored
129 errors is made empty.
130
Bram Moolenaarce90e362019-09-08 18:58:44 +0200131 Can also be used as a |method|: >
132 GetErrorText()->test_ignore_error()
Bram Moolenaared997ad2019-07-21 16:42:00 +0200133
134test_null_blob() *test_null_blob()*
135 Return a |Blob| that is null. Only useful for testing.
136
137
138test_null_channel() *test_null_channel()*
139 Return a |Channel| that is null. Only useful for testing.
140 {only available when compiled with the +channel feature}
141
142
143test_null_dict() *test_null_dict()*
144 Return a |Dict| that is null. Only useful for testing.
145
146
Bram Moolenaare69f6d02020-04-01 22:11:01 +0200147test_null_function() *test_null_function()*
Bram Moolenaard1caa942020-04-10 22:10:56 +0200148 Return a |Funcref| that is null. Only useful for testing.
Bram Moolenaare69f6d02020-04-01 22:11:01 +0200149
150
Bram Moolenaared997ad2019-07-21 16:42:00 +0200151test_null_job() *test_null_job()*
152 Return a |Job| that is null. Only useful for testing.
153 {only available when compiled with the +job feature}
154
155
156test_null_list() *test_null_list()*
157 Return a |List| that is null. Only useful for testing.
158
159
160test_null_partial() *test_null_partial()*
161 Return a |Partial| that is null. Only useful for testing.
162
163
164test_null_string() *test_null_string()*
165 Return a |String| that is null. Only useful for testing.
166
167
Bram Moolenaar8ed04582020-02-22 19:07:28 +0100168test_unknown() *test_unknown()*
169 Return a value with unknown type. Only useful for testing.
170
171test_void() *test_void()*
172 Return a value with void type. Only useful for testing.
173
174
Bram Moolenaared997ad2019-07-21 16:42:00 +0200175test_option_not_set({name}) *test_option_not_set()*
176 Reset the flag that indicates option {name} was set. Thus it
177 looks like it still has the default value. Use like this: >
178 set ambiwidth=double
179 call test_option_not_set('ambiwidth')
180< Now the 'ambiwidth' option behaves like it was never changed,
181 even though the value is "double".
182 Only to be used for testing!
183
Bram Moolenaarce90e362019-09-08 18:58:44 +0200184 Can also be used as a |method|: >
185 GetOptionName()->test_option_not_set()
186
Bram Moolenaared997ad2019-07-21 16:42:00 +0200187
188test_override({name}, {val}) *test_override()*
189 Overrides certain parts of Vim's internal processing to be able
190 to run tests. Only to be used for testing Vim!
191 The override is enabled when {val} is non-zero and removed
192 when {val} is zero.
193 Current supported values for name are:
194
195 name effect when {val} is non-zero ~
196 redraw disable the redrawing() function
197 redraw_flag ignore the RedrawingDisabled flag
198 char_avail disable the char_avail() function
199 starting reset the "starting" variable, see below
200 nfa_fail makes the NFA regexp engine fail to force a
201 fallback to the old engine
202 no_query_mouse do not query the mouse position for "dec"
203 terminals
204 no_wait_return set the "no_wait_return" flag. Not restored
205 with "ALL".
Bram Moolenaarb340bae2020-06-15 19:51:56 +0200206 ui_delay time in msec to use in ui_delay(); overrules a
207 wait time of up to 3 seconds for messages
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +0200208 term_props reset all terminal properties when the version
209 string is detected
Bram Moolenaarc8cdf0f2021-03-13 13:28:13 +0100210 uptime overrules sysinfo.uptime
Bram Moolenaared997ad2019-07-21 16:42:00 +0200211 ALL clear all overrides ({val} is not used)
212
213 "starting" is to be used when a test should behave like
214 startup was done. Since the tests are run by sourcing a
215 script the "starting" variable is non-zero. This is usually a
216 good thing (tests run faster), but sometimes changes behavior
217 in a way that the test doesn't work properly.
218 When using: >
219 call test_override('starting', 1)
220< The value of "starting" is saved. It is restored by: >
221 call test_override('starting', 0)
222
Bram Moolenaarce90e362019-09-08 18:58:44 +0200223< Can also be used as a |method|: >
224 GetOverrideVal()-> test_override('starting')
Bram Moolenaared997ad2019-07-21 16:42:00 +0200225
226test_refcount({expr}) *test_refcount()*
227 Return the reference count of {expr}. When {expr} is of a
228 type that does not have a reference count, returns -1. Only
229 to be used for testing.
230
Bram Moolenaarce90e362019-09-08 18:58:44 +0200231 Can also be used as a |method|: >
232 GetVarname()->test_refcount()
233
Bram Moolenaared997ad2019-07-21 16:42:00 +0200234
235test_scrollbar({which}, {value}, {dragging}) *test_scrollbar()*
236 Pretend using scrollbar {which} to move it to position
237 {value}. {which} can be:
238 left Left scrollbar of the current window
239 right Right scrollbar of the current window
240 hor Horizontal scrollbar
241
242 For the vertical scrollbars {value} can be 1 to the
243 line-count of the buffer. For the horizontal scrollbar the
244 {value} can be between 1 and the maximum line length, assuming
245 'wrap' is not set.
246
247 When {dragging} is non-zero it's like dragging the scrollbar,
248 otherwise it's like clicking in the scrollbar.
249 Only works when the {which} scrollbar actually exists,
250 obviously only when using the GUI.
251
Bram Moolenaarce90e362019-09-08 18:58:44 +0200252 Can also be used as a |method|: >
253 GetValue()->test_scrollbar('right', 0)
Bram Moolenaared997ad2019-07-21 16:42:00 +0200254
255test_setmouse({row}, {col}) *test_setmouse()*
256 Set the mouse position to be used for the next mouse action.
257 {row} and {col} are one based.
258 For example: >
259 call test_setmouse(4, 20)
260 call feedkeys("\<LeftMouse>", "xt")
261
Bram Moolenaared997ad2019-07-21 16:42:00 +0200262test_settime({expr}) *test_settime()*
263 Set the time Vim uses internally. Currently only used for
264 timestamps in the history, as they are used in viminfo, and
265 for undo.
266 Using a value of 1 makes Vim not sleep after a warning or
267 error message.
268 {expr} must evaluate to a number. When the value is zero the
269 normal behavior is restored.
270
Bram Moolenaarce90e362019-09-08 18:58:44 +0200271 Can also be used as a |method|: >
272 GetTime()->test_settime()
273
Bram Moolenaar4f645c52020-02-08 16:40:39 +0100274test_srand_seed([seed]) *test_srand_seed()*
275 When [seed] is given this sets the seed value used by
276 `srand()`. When omitted the test seed is removed.
277
Bram Moolenaared997ad2019-07-21 16:42:00 +0200278==============================================================================
Bram Moolenaar54775062019-07-31 21:07:14 +02002793. Assert functions *assert-functions-details*
Bram Moolenaared997ad2019-07-21 16:42:00 +0200280
281
282assert_beeps({cmd}) *assert_beeps()*
283 Run {cmd} and add an error message to |v:errors| if it does
284 NOT produce a beep or visual bell.
Bram Moolenaar5b8cabf2021-04-02 18:55:57 +0200285 Also see |assert_fails()|, |assert_nobeep()| and
286 |assert-return|.
Bram Moolenaared997ad2019-07-21 16:42:00 +0200287
Bram Moolenaar24278d22019-08-16 21:49:22 +0200288 Can also be used as a |method|: >
289 GetCmd()->assert_beeps()
290<
Bram Moolenaared997ad2019-07-21 16:42:00 +0200291 *assert_equal()*
292assert_equal({expected}, {actual} [, {msg}])
293 When {expected} and {actual} are not equal an error message is
294 added to |v:errors| and 1 is returned. Otherwise zero is
295 returned |assert-return|.
296 There is no automatic conversion, the String "4" is different
297 from the Number 4. And the number 4 is different from the
298 Float 4.0. The value of 'ignorecase' is not used here, case
299 always matters.
300 When {msg} is omitted an error in the form "Expected
301 {expected} but got {actual}" is produced.
302 Example: >
303 assert_equal('foo', 'bar')
304< Will result in a string to be added to |v:errors|:
305 test.vim line 12: Expected 'foo' but got 'bar' ~
306
Bram Moolenaar7ff78462020-07-10 22:00:53 +0200307 Can also be used as a |method|, the base is passed as the
308 second argument: >
Bram Moolenaar25e42232019-08-04 15:04:10 +0200309 mylist->assert_equal([1, 2, 3])
310
Bram Moolenaar25e42232019-08-04 15:04:10 +0200311< *assert_equalfile()*
Bram Moolenaarfb517ba2020-06-03 19:55:35 +0200312assert_equalfile({fname-one}, {fname-two} [, {msg}])
Bram Moolenaared997ad2019-07-21 16:42:00 +0200313 When the files {fname-one} and {fname-two} do not contain
314 exactly the same text an error message is added to |v:errors|.
315 Also see |assert-return|.
316 When {fname-one} or {fname-two} does not exist the error will
317 mention that.
318 Mainly useful with |terminal-diff|.
319
Bram Moolenaare49fbff2019-08-21 22:50:07 +0200320 Can also be used as a |method|: >
321 GetLog()->assert_equalfile('expected.log')
322
Bram Moolenaared997ad2019-07-21 16:42:00 +0200323assert_exception({error} [, {msg}]) *assert_exception()*
324 When v:exception does not contain the string {error} an error
325 message is added to |v:errors|. Also see |assert-return|.
326 This can be used to assert that a command throws an exception.
327 Using the error number, followed by a colon, avoids problems
328 with translations: >
329 try
330 commandthatfails
331 call assert_false(1, 'command should have failed')
332 catch
333 call assert_exception('E492:')
334 endtry
Bram Moolenaar1d634542020-08-18 13:41:50 +0200335<
336 *assert_fails()*
Bram Moolenaar9bd5d872020-09-06 21:47:48 +0200337assert_fails({cmd} [, {error} [, {msg} [, {lnum} [, {context}]]]])
Bram Moolenaared997ad2019-07-21 16:42:00 +0200338 Run {cmd} and add an error message to |v:errors| if it does
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200339 NOT produce an error or when {error} is not found in the
340 error message. Also see |assert-return|.
341
342 When {error} is a string it must be found literally in the
343 first reported error. Most often this will be the error code,
344 including the colon, e.g. "E123:". >
345 assert_fails('bad cmd', 'E987:')
346<
347 When {error} is a |List| with one or two strings, these are
348 used as patterns. The first pattern is matched against the
349 first reported error: >
350 assert_fails('cmd', ['E987:.*expected bool'])
351< The second pattern, if present, is matched against the last
Bram Moolenaar4072ba52020-12-23 13:56:35 +0100352 reported error.
353 If there is only one error then both patterns must match. This
354 can be used to check that there is only one error.
355 To only match the last error use an empty string for the first
356 error: >
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200357 assert_fails('cmd', ['', 'E987:'])
358<
Bram Moolenaar1d634542020-08-18 13:41:50 +0200359 If {msg} is empty then it is not used. Do this to get the
360 default message when passing the {lnum} argument.
361
362 When {lnum} is present and not negative, and the {error}
363 argument is present and matches, then this is compared with
364 the line number at which the error was reported. That can be
365 the line number in a function or in a script.
366
Bram Moolenaar9bd5d872020-09-06 21:47:48 +0200367 When {context} is present it is used as a pattern and matched
368 against the context (script name or function name) where
369 {lnum} is located in.
370
Bram Moolenaared997ad2019-07-21 16:42:00 +0200371 Note that beeping is not considered an error, and some failing
372 commands only beep. Use |assert_beeps()| for those.
373
Bram Moolenaar24278d22019-08-16 21:49:22 +0200374 Can also be used as a |method|: >
375 GetCmd()->assert_fails('E99:')
376
Bram Moolenaar1d634542020-08-18 13:41:50 +0200377assert_false({actual} [, {msg}]) *assert_false()*
Bram Moolenaared997ad2019-07-21 16:42:00 +0200378 When {actual} is not false an error message is added to
379 |v:errors|, like with |assert_equal()|.
380 Also see |assert-return|.
381 A value is false when it is zero. When {actual} is not a
382 number the assert fails.
383 When {msg} is omitted an error in the form
384 "Expected False but got {actual}" is produced.
385
Bram Moolenaar24278d22019-08-16 21:49:22 +0200386 Can also be used as a |method|: >
387 GetResult()->assert_false()
388
Bram Moolenaared997ad2019-07-21 16:42:00 +0200389assert_inrange({lower}, {upper}, {actual} [, {msg}]) *assert_inrange()*
390 This asserts number and |Float| values. When {actual} is lower
391 than {lower} or higher than {upper} an error message is added
392 to |v:errors|. Also see |assert-return|.
393 When {msg} is omitted an error in the form
394 "Expected range {lower} - {upper}, but got {actual}" is
395 produced.
396
397 *assert_match()*
398assert_match({pattern}, {actual} [, {msg}])
399 When {pattern} does not match {actual} an error message is
400 added to |v:errors|. Also see |assert-return|.
401
402 {pattern} is used as with |=~|: The matching is always done
403 like 'magic' was set and 'cpoptions' is empty, no matter what
404 the actual value of 'magic' or 'cpoptions' is.
405
406 {actual} is used as a string, automatic conversion applies.
407 Use "^" and "$" to match with the start and end of the text.
408 Use both to match the whole text.
409
410 When {msg} is omitted an error in the form
411 "Pattern {pattern} does not match {actual}" is produced.
412 Example: >
413 assert_match('^f.*o$', 'foobar')
414< Will result in a string to be added to |v:errors|:
415 test.vim line 12: Pattern '^f.*o$' does not match 'foobar' ~
416
Bram Moolenaar24278d22019-08-16 21:49:22 +0200417 Can also be used as a |method|: >
418 getFile()->assert_match('foo.*')
419<
Bram Moolenaar5b8cabf2021-04-02 18:55:57 +0200420assert_nobeep({cmd}) *assert_nobeep()*
421 Run {cmd} and add an error message to |v:errors| if it
422 produces a beep or visual bell.
423 Also see |assert_beeps()|.
424
425 Can also be used as a |method|: >
426 GetCmd()->assert_nobeep()
427<
Bram Moolenaared997ad2019-07-21 16:42:00 +0200428 *assert_notequal()*
429assert_notequal({expected}, {actual} [, {msg}])
430 The opposite of `assert_equal()`: add an error message to
431 |v:errors| when {expected} and {actual} are equal.
432 Also see |assert-return|.
433
Bram Moolenaar25e42232019-08-04 15:04:10 +0200434 Can also be used as a |method|: >
435 mylist->assert_notequal([1, 2, 3])
436
437< *assert_notmatch()*
Bram Moolenaared997ad2019-07-21 16:42:00 +0200438assert_notmatch({pattern}, {actual} [, {msg}])
439 The opposite of `assert_match()`: add an error message to
440 |v:errors| when {pattern} matches {actual}.
441 Also see |assert-return|.
442
Bram Moolenaar24278d22019-08-16 21:49:22 +0200443 Can also be used as a |method|: >
444 getFile()->assert_notmatch('bar.*')
445
Bram Moolenaare49fbff2019-08-21 22:50:07 +0200446
Bram Moolenaared997ad2019-07-21 16:42:00 +0200447assert_report({msg}) *assert_report()*
448 Report a test failure directly, using {msg}.
449 Always returns one.
450
Bram Moolenaare49fbff2019-08-21 22:50:07 +0200451 Can also be used as a |method|: >
452 GetMessage()->assert_report()
453
454
Bram Moolenaared997ad2019-07-21 16:42:00 +0200455assert_true({actual} [, {msg}]) *assert_true()*
456 When {actual} is not true an error message is added to
457 |v:errors|, like with |assert_equal()|.
458 Also see |assert-return|.
459 A value is TRUE when it is a non-zero number. When {actual}
460 is not a number the assert fails.
461 When {msg} is omitted an error in the form "Expected True but
462 got {actual}" is produced.
463
Bram Moolenaar24278d22019-08-16 21:49:22 +0200464 Can also be used as a |method|: >
465 GetResult()->assert_true()
466<
Bram Moolenaared997ad2019-07-21 16:42:00 +0200467
468 vim:tw=78:ts=8:noet:ft=help:norl: