blob: db505e7efce75522bc1618e02858b114877d450a [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<
82 *test_gui_mouse_event()*
83test_gui_mouse_event({button}, {row}, {col}, {multiclick}, {modifiers})
84 Inject a mouse button click event. This function works only
85 when GUI is running.
86 The supported values for {button} are:
87 0 right mouse button
88 1 middle mouse button
89 2 left mouse button
90 3 mouse button release
91 4 scroll wheel down
92 5 scroll wheel up
93 6 scroll wheel left
94 7 scroll wheel right
95 {row} and {col} specify the location of the mouse click.
96 To inject a multiclick event, set {multiclick} to 1.
97 The supported values for {modifiers} are:
98 4 shift is pressed
99 8 alt is pressed
100 16 ctrl is pressed
101 After injecting the mouse event you probably should call
102 |feedkeys()| to have them processed, e.g.: >
103 call feedkeys("y", 'Lx!')
104
Bram Moolenaared997ad2019-07-21 16:42:00 +0200105
106test_ignore_error({expr}) *test_ignore_error()*
107 Ignore any error containing {expr}. A normal message is given
108 instead.
109 This is only meant to be used in tests, where catching the
110 error with try/catch cannot be used (because it skips over
111 following code).
112 {expr} is used literally, not as a pattern.
113 When the {expr} is the string "RESET" then the list of ignored
114 errors is made empty.
115
Bram Moolenaarce90e362019-09-08 18:58:44 +0200116 Can also be used as a |method|: >
117 GetErrorText()->test_ignore_error()
Bram Moolenaared997ad2019-07-21 16:42:00 +0200118
119test_null_blob() *test_null_blob()*
120 Return a |Blob| that is null. Only useful for testing.
121
122
123test_null_channel() *test_null_channel()*
124 Return a |Channel| that is null. Only useful for testing.
125 {only available when compiled with the +channel feature}
126
127
128test_null_dict() *test_null_dict()*
129 Return a |Dict| that is null. Only useful for testing.
130
131
Bram Moolenaare69f6d02020-04-01 22:11:01 +0200132test_null_function() *test_null_function()*
Bram Moolenaard1caa942020-04-10 22:10:56 +0200133 Return a |Funcref| that is null. Only useful for testing.
Bram Moolenaare69f6d02020-04-01 22:11:01 +0200134
135
Bram Moolenaared997ad2019-07-21 16:42:00 +0200136test_null_job() *test_null_job()*
137 Return a |Job| that is null. Only useful for testing.
138 {only available when compiled with the +job feature}
139
140
141test_null_list() *test_null_list()*
142 Return a |List| that is null. Only useful for testing.
143
144
145test_null_partial() *test_null_partial()*
146 Return a |Partial| that is null. Only useful for testing.
147
148
149test_null_string() *test_null_string()*
150 Return a |String| that is null. Only useful for testing.
151
152
Bram Moolenaar8ed04582020-02-22 19:07:28 +0100153test_unknown() *test_unknown()*
154 Return a value with unknown type. Only useful for testing.
155
156test_void() *test_void()*
157 Return a value with void type. Only useful for testing.
158
159
Bram Moolenaared997ad2019-07-21 16:42:00 +0200160test_option_not_set({name}) *test_option_not_set()*
161 Reset the flag that indicates option {name} was set. Thus it
162 looks like it still has the default value. Use like this: >
163 set ambiwidth=double
164 call test_option_not_set('ambiwidth')
165< Now the 'ambiwidth' option behaves like it was never changed,
166 even though the value is "double".
167 Only to be used for testing!
168
Bram Moolenaarce90e362019-09-08 18:58:44 +0200169 Can also be used as a |method|: >
170 GetOptionName()->test_option_not_set()
171
Bram Moolenaared997ad2019-07-21 16:42:00 +0200172
173test_override({name}, {val}) *test_override()*
174 Overrides certain parts of Vim's internal processing to be able
175 to run tests. Only to be used for testing Vim!
176 The override is enabled when {val} is non-zero and removed
177 when {val} is zero.
178 Current supported values for name are:
179
180 name effect when {val} is non-zero ~
181 redraw disable the redrawing() function
182 redraw_flag ignore the RedrawingDisabled flag
183 char_avail disable the char_avail() function
184 starting reset the "starting" variable, see below
185 nfa_fail makes the NFA regexp engine fail to force a
186 fallback to the old engine
187 no_query_mouse do not query the mouse position for "dec"
188 terminals
189 no_wait_return set the "no_wait_return" flag. Not restored
190 with "ALL".
Bram Moolenaarb340bae2020-06-15 19:51:56 +0200191 ui_delay time in msec to use in ui_delay(); overrules a
192 wait time of up to 3 seconds for messages
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +0200193 term_props reset all terminal properties when the version
194 string is detected
Bram Moolenaarc8cdf0f2021-03-13 13:28:13 +0100195 uptime overrules sysinfo.uptime
Bram Moolenaared997ad2019-07-21 16:42:00 +0200196 ALL clear all overrides ({val} is not used)
197
198 "starting" is to be used when a test should behave like
199 startup was done. Since the tests are run by sourcing a
200 script the "starting" variable is non-zero. This is usually a
201 good thing (tests run faster), but sometimes changes behavior
202 in a way that the test doesn't work properly.
203 When using: >
204 call test_override('starting', 1)
205< The value of "starting" is saved. It is restored by: >
206 call test_override('starting', 0)
207
Bram Moolenaarce90e362019-09-08 18:58:44 +0200208< Can also be used as a |method|: >
209 GetOverrideVal()-> test_override('starting')
Bram Moolenaared997ad2019-07-21 16:42:00 +0200210
211test_refcount({expr}) *test_refcount()*
212 Return the reference count of {expr}. When {expr} is of a
213 type that does not have a reference count, returns -1. Only
214 to be used for testing.
215
Bram Moolenaarce90e362019-09-08 18:58:44 +0200216 Can also be used as a |method|: >
217 GetVarname()->test_refcount()
218
Bram Moolenaared997ad2019-07-21 16:42:00 +0200219
220test_scrollbar({which}, {value}, {dragging}) *test_scrollbar()*
221 Pretend using scrollbar {which} to move it to position
222 {value}. {which} can be:
223 left Left scrollbar of the current window
224 right Right scrollbar of the current window
225 hor Horizontal scrollbar
226
227 For the vertical scrollbars {value} can be 1 to the
228 line-count of the buffer. For the horizontal scrollbar the
229 {value} can be between 1 and the maximum line length, assuming
230 'wrap' is not set.
231
232 When {dragging} is non-zero it's like dragging the scrollbar,
233 otherwise it's like clicking in the scrollbar.
234 Only works when the {which} scrollbar actually exists,
235 obviously only when using the GUI.
236
Bram Moolenaarce90e362019-09-08 18:58:44 +0200237 Can also be used as a |method|: >
238 GetValue()->test_scrollbar('right', 0)
Bram Moolenaared997ad2019-07-21 16:42:00 +0200239
240test_setmouse({row}, {col}) *test_setmouse()*
241 Set the mouse position to be used for the next mouse action.
242 {row} and {col} are one based.
243 For example: >
244 call test_setmouse(4, 20)
245 call feedkeys("\<LeftMouse>", "xt")
246
Bram Moolenaared997ad2019-07-21 16:42:00 +0200247test_settime({expr}) *test_settime()*
248 Set the time Vim uses internally. Currently only used for
249 timestamps in the history, as they are used in viminfo, and
250 for undo.
251 Using a value of 1 makes Vim not sleep after a warning or
252 error message.
253 {expr} must evaluate to a number. When the value is zero the
254 normal behavior is restored.
255
Bram Moolenaarce90e362019-09-08 18:58:44 +0200256 Can also be used as a |method|: >
257 GetTime()->test_settime()
258
Bram Moolenaar4f645c52020-02-08 16:40:39 +0100259test_srand_seed([seed]) *test_srand_seed()*
260 When [seed] is given this sets the seed value used by
261 `srand()`. When omitted the test seed is removed.
262
Bram Moolenaared997ad2019-07-21 16:42:00 +0200263==============================================================================
Bram Moolenaar54775062019-07-31 21:07:14 +02002643. Assert functions *assert-functions-details*
Bram Moolenaared997ad2019-07-21 16:42:00 +0200265
266
267assert_beeps({cmd}) *assert_beeps()*
268 Run {cmd} and add an error message to |v:errors| if it does
269 NOT produce a beep or visual bell.
Bram Moolenaar5b8cabf2021-04-02 18:55:57 +0200270 Also see |assert_fails()|, |assert_nobeep()| and
271 |assert-return|.
Bram Moolenaared997ad2019-07-21 16:42:00 +0200272
Bram Moolenaar24278d22019-08-16 21:49:22 +0200273 Can also be used as a |method|: >
274 GetCmd()->assert_beeps()
275<
Bram Moolenaared997ad2019-07-21 16:42:00 +0200276 *assert_equal()*
277assert_equal({expected}, {actual} [, {msg}])
278 When {expected} and {actual} are not equal an error message is
279 added to |v:errors| and 1 is returned. Otherwise zero is
280 returned |assert-return|.
281 There is no automatic conversion, the String "4" is different
282 from the Number 4. And the number 4 is different from the
283 Float 4.0. The value of 'ignorecase' is not used here, case
284 always matters.
285 When {msg} is omitted an error in the form "Expected
286 {expected} but got {actual}" is produced.
287 Example: >
288 assert_equal('foo', 'bar')
289< Will result in a string to be added to |v:errors|:
290 test.vim line 12: Expected 'foo' but got 'bar' ~
291
Bram Moolenaar7ff78462020-07-10 22:00:53 +0200292 Can also be used as a |method|, the base is passed as the
293 second argument: >
Bram Moolenaar25e42232019-08-04 15:04:10 +0200294 mylist->assert_equal([1, 2, 3])
295
Bram Moolenaar25e42232019-08-04 15:04:10 +0200296< *assert_equalfile()*
Bram Moolenaarfb517ba2020-06-03 19:55:35 +0200297assert_equalfile({fname-one}, {fname-two} [, {msg}])
Bram Moolenaared997ad2019-07-21 16:42:00 +0200298 When the files {fname-one} and {fname-two} do not contain
299 exactly the same text an error message is added to |v:errors|.
300 Also see |assert-return|.
301 When {fname-one} or {fname-two} does not exist the error will
302 mention that.
303 Mainly useful with |terminal-diff|.
304
Bram Moolenaare49fbff2019-08-21 22:50:07 +0200305 Can also be used as a |method|: >
306 GetLog()->assert_equalfile('expected.log')
307
Bram Moolenaared997ad2019-07-21 16:42:00 +0200308assert_exception({error} [, {msg}]) *assert_exception()*
309 When v:exception does not contain the string {error} an error
310 message is added to |v:errors|. Also see |assert-return|.
311 This can be used to assert that a command throws an exception.
312 Using the error number, followed by a colon, avoids problems
313 with translations: >
314 try
315 commandthatfails
316 call assert_false(1, 'command should have failed')
317 catch
318 call assert_exception('E492:')
319 endtry
Bram Moolenaar1d634542020-08-18 13:41:50 +0200320<
321 *assert_fails()*
Bram Moolenaar9bd5d872020-09-06 21:47:48 +0200322assert_fails({cmd} [, {error} [, {msg} [, {lnum} [, {context}]]]])
Bram Moolenaared997ad2019-07-21 16:42:00 +0200323 Run {cmd} and add an error message to |v:errors| if it does
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200324 NOT produce an error or when {error} is not found in the
325 error message. Also see |assert-return|.
326
327 When {error} is a string it must be found literally in the
328 first reported error. Most often this will be the error code,
329 including the colon, e.g. "E123:". >
330 assert_fails('bad cmd', 'E987:')
331<
332 When {error} is a |List| with one or two strings, these are
333 used as patterns. The first pattern is matched against the
334 first reported error: >
335 assert_fails('cmd', ['E987:.*expected bool'])
336< The second pattern, if present, is matched against the last
Bram Moolenaar4072ba52020-12-23 13:56:35 +0100337 reported error.
338 If there is only one error then both patterns must match. This
339 can be used to check that there is only one error.
340 To only match the last error use an empty string for the first
341 error: >
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200342 assert_fails('cmd', ['', 'E987:'])
343<
Bram Moolenaar1d634542020-08-18 13:41:50 +0200344 If {msg} is empty then it is not used. Do this to get the
345 default message when passing the {lnum} argument.
346
347 When {lnum} is present and not negative, and the {error}
348 argument is present and matches, then this is compared with
349 the line number at which the error was reported. That can be
350 the line number in a function or in a script.
351
Bram Moolenaar9bd5d872020-09-06 21:47:48 +0200352 When {context} is present it is used as a pattern and matched
353 against the context (script name or function name) where
354 {lnum} is located in.
355
Bram Moolenaared997ad2019-07-21 16:42:00 +0200356 Note that beeping is not considered an error, and some failing
357 commands only beep. Use |assert_beeps()| for those.
358
Bram Moolenaar24278d22019-08-16 21:49:22 +0200359 Can also be used as a |method|: >
360 GetCmd()->assert_fails('E99:')
361
Bram Moolenaar1d634542020-08-18 13:41:50 +0200362assert_false({actual} [, {msg}]) *assert_false()*
Bram Moolenaared997ad2019-07-21 16:42:00 +0200363 When {actual} is not false an error message is added to
364 |v:errors|, like with |assert_equal()|.
365 Also see |assert-return|.
366 A value is false when it is zero. When {actual} is not a
367 number the assert fails.
368 When {msg} is omitted an error in the form
369 "Expected False but got {actual}" is produced.
370
Bram Moolenaar24278d22019-08-16 21:49:22 +0200371 Can also be used as a |method|: >
372 GetResult()->assert_false()
373
Bram Moolenaared997ad2019-07-21 16:42:00 +0200374assert_inrange({lower}, {upper}, {actual} [, {msg}]) *assert_inrange()*
375 This asserts number and |Float| values. When {actual} is lower
376 than {lower} or higher than {upper} an error message is added
377 to |v:errors|. Also see |assert-return|.
378 When {msg} is omitted an error in the form
379 "Expected range {lower} - {upper}, but got {actual}" is
380 produced.
381
382 *assert_match()*
383assert_match({pattern}, {actual} [, {msg}])
384 When {pattern} does not match {actual} an error message is
385 added to |v:errors|. Also see |assert-return|.
386
387 {pattern} is used as with |=~|: The matching is always done
388 like 'magic' was set and 'cpoptions' is empty, no matter what
389 the actual value of 'magic' or 'cpoptions' is.
390
391 {actual} is used as a string, automatic conversion applies.
392 Use "^" and "$" to match with the start and end of the text.
393 Use both to match the whole text.
394
395 When {msg} is omitted an error in the form
396 "Pattern {pattern} does not match {actual}" is produced.
397 Example: >
398 assert_match('^f.*o$', 'foobar')
399< Will result in a string to be added to |v:errors|:
400 test.vim line 12: Pattern '^f.*o$' does not match 'foobar' ~
401
Bram Moolenaar24278d22019-08-16 21:49:22 +0200402 Can also be used as a |method|: >
403 getFile()->assert_match('foo.*')
404<
Bram Moolenaar5b8cabf2021-04-02 18:55:57 +0200405assert_nobeep({cmd}) *assert_nobeep()*
406 Run {cmd} and add an error message to |v:errors| if it
407 produces a beep or visual bell.
408 Also see |assert_beeps()|.
409
410 Can also be used as a |method|: >
411 GetCmd()->assert_nobeep()
412<
Bram Moolenaared997ad2019-07-21 16:42:00 +0200413 *assert_notequal()*
414assert_notequal({expected}, {actual} [, {msg}])
415 The opposite of `assert_equal()`: add an error message to
416 |v:errors| when {expected} and {actual} are equal.
417 Also see |assert-return|.
418
Bram Moolenaar25e42232019-08-04 15:04:10 +0200419 Can also be used as a |method|: >
420 mylist->assert_notequal([1, 2, 3])
421
422< *assert_notmatch()*
Bram Moolenaared997ad2019-07-21 16:42:00 +0200423assert_notmatch({pattern}, {actual} [, {msg}])
424 The opposite of `assert_match()`: add an error message to
425 |v:errors| when {pattern} matches {actual}.
426 Also see |assert-return|.
427
Bram Moolenaar24278d22019-08-16 21:49:22 +0200428 Can also be used as a |method|: >
429 getFile()->assert_notmatch('bar.*')
430
Bram Moolenaare49fbff2019-08-21 22:50:07 +0200431
Bram Moolenaared997ad2019-07-21 16:42:00 +0200432assert_report({msg}) *assert_report()*
433 Report a test failure directly, using {msg}.
434 Always returns one.
435
Bram Moolenaare49fbff2019-08-21 22:50:07 +0200436 Can also be used as a |method|: >
437 GetMessage()->assert_report()
438
439
Bram Moolenaared997ad2019-07-21 16:42:00 +0200440assert_true({actual} [, {msg}]) *assert_true()*
441 When {actual} is not true an error message is added to
442 |v:errors|, like with |assert_equal()|.
443 Also see |assert-return|.
444 A value is TRUE when it is a non-zero number. When {actual}
445 is not a number the assert fails.
446 When {msg} is omitted an error in the form "Expected True but
447 got {actual}" is produced.
448
Bram Moolenaar24278d22019-08-16 21:49:22 +0200449 Can also be used as a |method|: >
450 GetResult()->assert_true()
451<
Bram Moolenaared997ad2019-07-21 16:42:00 +0200452
453 vim:tw=78:ts=8:noet:ft=help:norl: