blob: 6fd2d458876893723a02cbf0002b21e1823f839b [file] [log] [blame]
Bram Moolenaar130cbfc2021-04-07 21:07:20 +02001*testing.txt* For Vim version 8.2. Last change: 2021 Apr 02
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()
Bram Moolenaared997ad2019-07-21 16:42:00 +020081
82test_ignore_error({expr}) *test_ignore_error()*
83 Ignore any error containing {expr}. A normal message is given
84 instead.
85 This is only meant to be used in tests, where catching the
86 error with try/catch cannot be used (because it skips over
87 following code).
88 {expr} is used literally, not as a pattern.
89 When the {expr} is the string "RESET" then the list of ignored
90 errors is made empty.
91
Bram Moolenaarce90e362019-09-08 18:58:44 +020092 Can also be used as a |method|: >
93 GetErrorText()->test_ignore_error()
Bram Moolenaared997ad2019-07-21 16:42:00 +020094
95test_null_blob() *test_null_blob()*
96 Return a |Blob| that is null. Only useful for testing.
97
98
99test_null_channel() *test_null_channel()*
100 Return a |Channel| that is null. Only useful for testing.
101 {only available when compiled with the +channel feature}
102
103
104test_null_dict() *test_null_dict()*
105 Return a |Dict| that is null. Only useful for testing.
106
107
Bram Moolenaare69f6d02020-04-01 22:11:01 +0200108test_null_function() *test_null_function()*
Bram Moolenaard1caa942020-04-10 22:10:56 +0200109 Return a |Funcref| that is null. Only useful for testing.
Bram Moolenaare69f6d02020-04-01 22:11:01 +0200110
111
Bram Moolenaared997ad2019-07-21 16:42:00 +0200112test_null_job() *test_null_job()*
113 Return a |Job| that is null. Only useful for testing.
114 {only available when compiled with the +job feature}
115
116
117test_null_list() *test_null_list()*
118 Return a |List| that is null. Only useful for testing.
119
120
121test_null_partial() *test_null_partial()*
122 Return a |Partial| that is null. Only useful for testing.
123
124
125test_null_string() *test_null_string()*
126 Return a |String| that is null. Only useful for testing.
127
128
Bram Moolenaar8ed04582020-02-22 19:07:28 +0100129test_unknown() *test_unknown()*
130 Return a value with unknown type. Only useful for testing.
131
132test_void() *test_void()*
133 Return a value with void type. Only useful for testing.
134
135
Bram Moolenaared997ad2019-07-21 16:42:00 +0200136test_option_not_set({name}) *test_option_not_set()*
137 Reset the flag that indicates option {name} was set. Thus it
138 looks like it still has the default value. Use like this: >
139 set ambiwidth=double
140 call test_option_not_set('ambiwidth')
141< Now the 'ambiwidth' option behaves like it was never changed,
142 even though the value is "double".
143 Only to be used for testing!
144
Bram Moolenaarce90e362019-09-08 18:58:44 +0200145 Can also be used as a |method|: >
146 GetOptionName()->test_option_not_set()
147
Bram Moolenaared997ad2019-07-21 16:42:00 +0200148
149test_override({name}, {val}) *test_override()*
150 Overrides certain parts of Vim's internal processing to be able
151 to run tests. Only to be used for testing Vim!
152 The override is enabled when {val} is non-zero and removed
153 when {val} is zero.
154 Current supported values for name are:
155
156 name effect when {val} is non-zero ~
157 redraw disable the redrawing() function
158 redraw_flag ignore the RedrawingDisabled flag
159 char_avail disable the char_avail() function
160 starting reset the "starting" variable, see below
161 nfa_fail makes the NFA regexp engine fail to force a
162 fallback to the old engine
163 no_query_mouse do not query the mouse position for "dec"
164 terminals
165 no_wait_return set the "no_wait_return" flag. Not restored
166 with "ALL".
Bram Moolenaarb340bae2020-06-15 19:51:56 +0200167 ui_delay time in msec to use in ui_delay(); overrules a
168 wait time of up to 3 seconds for messages
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +0200169 term_props reset all terminal properties when the version
170 string is detected
Bram Moolenaarc8cdf0f2021-03-13 13:28:13 +0100171 uptime overrules sysinfo.uptime
Bram Moolenaared997ad2019-07-21 16:42:00 +0200172 ALL clear all overrides ({val} is not used)
173
174 "starting" is to be used when a test should behave like
175 startup was done. Since the tests are run by sourcing a
176 script the "starting" variable is non-zero. This is usually a
177 good thing (tests run faster), but sometimes changes behavior
178 in a way that the test doesn't work properly.
179 When using: >
180 call test_override('starting', 1)
181< The value of "starting" is saved. It is restored by: >
182 call test_override('starting', 0)
183
Bram Moolenaarce90e362019-09-08 18:58:44 +0200184< Can also be used as a |method|: >
185 GetOverrideVal()-> test_override('starting')
Bram Moolenaared997ad2019-07-21 16:42:00 +0200186
187test_refcount({expr}) *test_refcount()*
188 Return the reference count of {expr}. When {expr} is of a
189 type that does not have a reference count, returns -1. Only
190 to be used for testing.
191
Bram Moolenaarce90e362019-09-08 18:58:44 +0200192 Can also be used as a |method|: >
193 GetVarname()->test_refcount()
194
Bram Moolenaared997ad2019-07-21 16:42:00 +0200195
196test_scrollbar({which}, {value}, {dragging}) *test_scrollbar()*
197 Pretend using scrollbar {which} to move it to position
198 {value}. {which} can be:
199 left Left scrollbar of the current window
200 right Right scrollbar of the current window
201 hor Horizontal scrollbar
202
203 For the vertical scrollbars {value} can be 1 to the
204 line-count of the buffer. For the horizontal scrollbar the
205 {value} can be between 1 and the maximum line length, assuming
206 'wrap' is not set.
207
208 When {dragging} is non-zero it's like dragging the scrollbar,
209 otherwise it's like clicking in the scrollbar.
210 Only works when the {which} scrollbar actually exists,
211 obviously only when using the GUI.
212
Bram Moolenaarce90e362019-09-08 18:58:44 +0200213 Can also be used as a |method|: >
214 GetValue()->test_scrollbar('right', 0)
Bram Moolenaared997ad2019-07-21 16:42:00 +0200215
216test_setmouse({row}, {col}) *test_setmouse()*
217 Set the mouse position to be used for the next mouse action.
218 {row} and {col} are one based.
219 For example: >
220 call test_setmouse(4, 20)
221 call feedkeys("\<LeftMouse>", "xt")
222
Bram Moolenaared997ad2019-07-21 16:42:00 +0200223test_settime({expr}) *test_settime()*
224 Set the time Vim uses internally. Currently only used for
225 timestamps in the history, as they are used in viminfo, and
226 for undo.
227 Using a value of 1 makes Vim not sleep after a warning or
228 error message.
229 {expr} must evaluate to a number. When the value is zero the
230 normal behavior is restored.
231
Bram Moolenaarce90e362019-09-08 18:58:44 +0200232 Can also be used as a |method|: >
233 GetTime()->test_settime()
234
Bram Moolenaar4f645c52020-02-08 16:40:39 +0100235test_srand_seed([seed]) *test_srand_seed()*
236 When [seed] is given this sets the seed value used by
237 `srand()`. When omitted the test seed is removed.
238
Bram Moolenaared997ad2019-07-21 16:42:00 +0200239==============================================================================
Bram Moolenaar54775062019-07-31 21:07:14 +02002403. Assert functions *assert-functions-details*
Bram Moolenaared997ad2019-07-21 16:42:00 +0200241
242
243assert_beeps({cmd}) *assert_beeps()*
244 Run {cmd} and add an error message to |v:errors| if it does
245 NOT produce a beep or visual bell.
Bram Moolenaar5b8cabf2021-04-02 18:55:57 +0200246 Also see |assert_fails()|, |assert_nobeep()| and
247 |assert-return|.
Bram Moolenaared997ad2019-07-21 16:42:00 +0200248
Bram Moolenaar24278d22019-08-16 21:49:22 +0200249 Can also be used as a |method|: >
250 GetCmd()->assert_beeps()
251<
Bram Moolenaared997ad2019-07-21 16:42:00 +0200252 *assert_equal()*
253assert_equal({expected}, {actual} [, {msg}])
254 When {expected} and {actual} are not equal an error message is
255 added to |v:errors| and 1 is returned. Otherwise zero is
256 returned |assert-return|.
257 There is no automatic conversion, the String "4" is different
258 from the Number 4. And the number 4 is different from the
259 Float 4.0. The value of 'ignorecase' is not used here, case
260 always matters.
261 When {msg} is omitted an error in the form "Expected
262 {expected} but got {actual}" is produced.
263 Example: >
264 assert_equal('foo', 'bar')
265< Will result in a string to be added to |v:errors|:
266 test.vim line 12: Expected 'foo' but got 'bar' ~
267
Bram Moolenaar7ff78462020-07-10 22:00:53 +0200268 Can also be used as a |method|, the base is passed as the
269 second argument: >
Bram Moolenaar25e42232019-08-04 15:04:10 +0200270 mylist->assert_equal([1, 2, 3])
271
Bram Moolenaar25e42232019-08-04 15:04:10 +0200272< *assert_equalfile()*
Bram Moolenaarfb517ba2020-06-03 19:55:35 +0200273assert_equalfile({fname-one}, {fname-two} [, {msg}])
Bram Moolenaared997ad2019-07-21 16:42:00 +0200274 When the files {fname-one} and {fname-two} do not contain
275 exactly the same text an error message is added to |v:errors|.
276 Also see |assert-return|.
277 When {fname-one} or {fname-two} does not exist the error will
278 mention that.
279 Mainly useful with |terminal-diff|.
280
Bram Moolenaare49fbff2019-08-21 22:50:07 +0200281 Can also be used as a |method|: >
282 GetLog()->assert_equalfile('expected.log')
283
Bram Moolenaared997ad2019-07-21 16:42:00 +0200284assert_exception({error} [, {msg}]) *assert_exception()*
285 When v:exception does not contain the string {error} an error
286 message is added to |v:errors|. Also see |assert-return|.
287 This can be used to assert that a command throws an exception.
288 Using the error number, followed by a colon, avoids problems
289 with translations: >
290 try
291 commandthatfails
292 call assert_false(1, 'command should have failed')
293 catch
294 call assert_exception('E492:')
295 endtry
Bram Moolenaar1d634542020-08-18 13:41:50 +0200296<
297 *assert_fails()*
Bram Moolenaar9bd5d872020-09-06 21:47:48 +0200298assert_fails({cmd} [, {error} [, {msg} [, {lnum} [, {context}]]]])
Bram Moolenaared997ad2019-07-21 16:42:00 +0200299 Run {cmd} and add an error message to |v:errors| if it does
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200300 NOT produce an error or when {error} is not found in the
301 error message. Also see |assert-return|.
302
303 When {error} is a string it must be found literally in the
304 first reported error. Most often this will be the error code,
305 including the colon, e.g. "E123:". >
306 assert_fails('bad cmd', 'E987:')
307<
308 When {error} is a |List| with one or two strings, these are
309 used as patterns. The first pattern is matched against the
310 first reported error: >
311 assert_fails('cmd', ['E987:.*expected bool'])
312< The second pattern, if present, is matched against the last
Bram Moolenaar4072ba52020-12-23 13:56:35 +0100313 reported error.
314 If there is only one error then both patterns must match. This
315 can be used to check that there is only one error.
316 To only match the last error use an empty string for the first
317 error: >
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200318 assert_fails('cmd', ['', 'E987:'])
319<
Bram Moolenaar1d634542020-08-18 13:41:50 +0200320 If {msg} is empty then it is not used. Do this to get the
321 default message when passing the {lnum} argument.
322
323 When {lnum} is present and not negative, and the {error}
324 argument is present and matches, then this is compared with
325 the line number at which the error was reported. That can be
326 the line number in a function or in a script.
327
Bram Moolenaar9bd5d872020-09-06 21:47:48 +0200328 When {context} is present it is used as a pattern and matched
329 against the context (script name or function name) where
330 {lnum} is located in.
331
Bram Moolenaared997ad2019-07-21 16:42:00 +0200332 Note that beeping is not considered an error, and some failing
333 commands only beep. Use |assert_beeps()| for those.
334
Bram Moolenaar24278d22019-08-16 21:49:22 +0200335 Can also be used as a |method|: >
336 GetCmd()->assert_fails('E99:')
337
Bram Moolenaar1d634542020-08-18 13:41:50 +0200338assert_false({actual} [, {msg}]) *assert_false()*
Bram Moolenaared997ad2019-07-21 16:42:00 +0200339 When {actual} is not false an error message is added to
340 |v:errors|, like with |assert_equal()|.
341 Also see |assert-return|.
342 A value is false when it is zero. When {actual} is not a
343 number the assert fails.
344 When {msg} is omitted an error in the form
345 "Expected False but got {actual}" is produced.
346
Bram Moolenaar24278d22019-08-16 21:49:22 +0200347 Can also be used as a |method|: >
348 GetResult()->assert_false()
349
Bram Moolenaared997ad2019-07-21 16:42:00 +0200350assert_inrange({lower}, {upper}, {actual} [, {msg}]) *assert_inrange()*
351 This asserts number and |Float| values. When {actual} is lower
352 than {lower} or higher than {upper} an error message is added
353 to |v:errors|. Also see |assert-return|.
354 When {msg} is omitted an error in the form
355 "Expected range {lower} - {upper}, but got {actual}" is
356 produced.
357
358 *assert_match()*
359assert_match({pattern}, {actual} [, {msg}])
360 When {pattern} does not match {actual} an error message is
361 added to |v:errors|. Also see |assert-return|.
362
363 {pattern} is used as with |=~|: The matching is always done
364 like 'magic' was set and 'cpoptions' is empty, no matter what
365 the actual value of 'magic' or 'cpoptions' is.
366
367 {actual} is used as a string, automatic conversion applies.
368 Use "^" and "$" to match with the start and end of the text.
369 Use both to match the whole text.
370
371 When {msg} is omitted an error in the form
372 "Pattern {pattern} does not match {actual}" is produced.
373 Example: >
374 assert_match('^f.*o$', 'foobar')
375< Will result in a string to be added to |v:errors|:
376 test.vim line 12: Pattern '^f.*o$' does not match 'foobar' ~
377
Bram Moolenaar24278d22019-08-16 21:49:22 +0200378 Can also be used as a |method|: >
379 getFile()->assert_match('foo.*')
380<
Bram Moolenaar5b8cabf2021-04-02 18:55:57 +0200381assert_nobeep({cmd}) *assert_nobeep()*
382 Run {cmd} and add an error message to |v:errors| if it
383 produces a beep or visual bell.
384 Also see |assert_beeps()|.
385
386 Can also be used as a |method|: >
387 GetCmd()->assert_nobeep()
388<
Bram Moolenaared997ad2019-07-21 16:42:00 +0200389 *assert_notequal()*
390assert_notequal({expected}, {actual} [, {msg}])
391 The opposite of `assert_equal()`: add an error message to
392 |v:errors| when {expected} and {actual} are equal.
393 Also see |assert-return|.
394
Bram Moolenaar25e42232019-08-04 15:04:10 +0200395 Can also be used as a |method|: >
396 mylist->assert_notequal([1, 2, 3])
397
398< *assert_notmatch()*
Bram Moolenaared997ad2019-07-21 16:42:00 +0200399assert_notmatch({pattern}, {actual} [, {msg}])
400 The opposite of `assert_match()`: add an error message to
401 |v:errors| when {pattern} matches {actual}.
402 Also see |assert-return|.
403
Bram Moolenaar24278d22019-08-16 21:49:22 +0200404 Can also be used as a |method|: >
405 getFile()->assert_notmatch('bar.*')
406
Bram Moolenaare49fbff2019-08-21 22:50:07 +0200407
Bram Moolenaared997ad2019-07-21 16:42:00 +0200408assert_report({msg}) *assert_report()*
409 Report a test failure directly, using {msg}.
410 Always returns one.
411
Bram Moolenaare49fbff2019-08-21 22:50:07 +0200412 Can also be used as a |method|: >
413 GetMessage()->assert_report()
414
415
Bram Moolenaared997ad2019-07-21 16:42:00 +0200416assert_true({actual} [, {msg}]) *assert_true()*
417 When {actual} is not true an error message is added to
418 |v:errors|, like with |assert_equal()|.
419 Also see |assert-return|.
420 A value is TRUE when it is a non-zero number. When {actual}
421 is not a number the assert fails.
422 When {msg} is omitted an error in the form "Expected True but
423 got {actual}" is produced.
424
Bram Moolenaar24278d22019-08-16 21:49:22 +0200425 Can also be used as a |method|: >
426 GetResult()->assert_true()
427<
Bram Moolenaared997ad2019-07-21 16:42:00 +0200428
429 vim:tw=78:ts=8:noet:ft=help:norl: