blob: 82dc77ecbf6a49838d354c0bf8acf230b8a31606 [file] [log] [blame]
Bram Moolenaarbc93ceb2020-02-26 13:36:21 +01001*testing.txt* For Vim version 8.2. Last change: 2020 Feb 22
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
23There are several types of tests added over time:
24 test33.in oldest, don't add any of these
25 test_something.in old style tests
26 test_something.vim new style tests
27
28 *new-style-testing*
29New tests should be added as new style tests. These use functions such as
30|assert_equal()| to keep the test commands and the expected result in one
31place.
32 *old-style-testing*
33In some cases an old style test needs to be used. E.g. when testing Vim
34without the |+eval| feature.
35
36Find more information in the file src/testdir/README.txt.
37
38==============================================================================
Bram Moolenaar54775062019-07-31 21:07:14 +0200392. Test functions *test-functions-details*
Bram Moolenaared997ad2019-07-21 16:42:00 +020040
41test_alloc_fail({id}, {countdown}, {repeat}) *test_alloc_fail()*
42 This is for testing: If the memory allocation with {id} is
43 called, then decrement {countdown}, and when it reaches zero
44 let memory allocation fail {repeat} times. When {repeat} is
45 smaller than one it fails one time.
46
Bram Moolenaarce90e362019-09-08 18:58:44 +020047 Can also be used as a |method|: >
48 GetAllocId()->test_alloc_fail()
Bram Moolenaared997ad2019-07-21 16:42:00 +020049
50test_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
63test_garbagecollect_now() *test_garbagecollect_now()*
64 Like garbagecollect(), but executed right away. This must
65 only be called directly to avoid any structure to exist
66 internally, and |v:testing| must have been set before calling
67 any function.
68
69
70test_garbagecollect_soon() *test_garbagecollect_soon()*
71 Set the flag to call the garbagecollector as if in the main
72 loop. Only to be used in tests.
73
74
75test_getvalue({name}) *test_getvalue()*
76 Get the value of an internal variable. These values for
77 {name} are supported:
78 need_fileinfo
79
Bram Moolenaarce90e362019-09-08 18:58:44 +020080 Can also be used as a |method|: >
81 GetName()->test_getvalue()
Bram Moolenaared997ad2019-07-21 16:42:00 +020082
83test_ignore_error({expr}) *test_ignore_error()*
84 Ignore any error containing {expr}. A normal message is given
85 instead.
86 This is only meant to be used in tests, where catching the
87 error with try/catch cannot be used (because it skips over
88 following code).
89 {expr} is used literally, not as a pattern.
90 When the {expr} is the string "RESET" then the list of ignored
91 errors is made empty.
92
Bram Moolenaarce90e362019-09-08 18:58:44 +020093 Can also be used as a |method|: >
94 GetErrorText()->test_ignore_error()
Bram Moolenaared997ad2019-07-21 16:42:00 +020095
96test_null_blob() *test_null_blob()*
97 Return a |Blob| that is null. Only useful for testing.
98
99
100test_null_channel() *test_null_channel()*
101 Return a |Channel| that is null. Only useful for testing.
102 {only available when compiled with the +channel feature}
103
104
105test_null_dict() *test_null_dict()*
106 Return a |Dict| that is null. Only useful for testing.
107
108
109test_null_job() *test_null_job()*
110 Return a |Job| that is null. Only useful for testing.
111 {only available when compiled with the +job feature}
112
113
114test_null_list() *test_null_list()*
115 Return a |List| that is null. Only useful for testing.
116
117
118test_null_partial() *test_null_partial()*
119 Return a |Partial| that is null. Only useful for testing.
120
121
122test_null_string() *test_null_string()*
123 Return a |String| that is null. Only useful for testing.
124
125
Bram Moolenaar8ed04582020-02-22 19:07:28 +0100126test_unknown() *test_unknown()*
127 Return a value with unknown type. Only useful for testing.
128
129test_void() *test_void()*
130 Return a value with void type. Only useful for testing.
131
132
Bram Moolenaared997ad2019-07-21 16:42:00 +0200133test_option_not_set({name}) *test_option_not_set()*
134 Reset the flag that indicates option {name} was set. Thus it
135 looks like it still has the default value. Use like this: >
136 set ambiwidth=double
137 call test_option_not_set('ambiwidth')
138< Now the 'ambiwidth' option behaves like it was never changed,
139 even though the value is "double".
140 Only to be used for testing!
141
Bram Moolenaarce90e362019-09-08 18:58:44 +0200142 Can also be used as a |method|: >
143 GetOptionName()->test_option_not_set()
144
Bram Moolenaared997ad2019-07-21 16:42:00 +0200145
146test_override({name}, {val}) *test_override()*
147 Overrides certain parts of Vim's internal processing to be able
148 to run tests. Only to be used for testing Vim!
149 The override is enabled when {val} is non-zero and removed
150 when {val} is zero.
151 Current supported values for name are:
152
153 name effect when {val} is non-zero ~
154 redraw disable the redrawing() function
155 redraw_flag ignore the RedrawingDisabled flag
156 char_avail disable the char_avail() function
157 starting reset the "starting" variable, see below
158 nfa_fail makes the NFA regexp engine fail to force a
159 fallback to the old engine
160 no_query_mouse do not query the mouse position for "dec"
161 terminals
162 no_wait_return set the "no_wait_return" flag. Not restored
163 with "ALL".
164 ALL clear all overrides ({val} is not used)
165
166 "starting" is to be used when a test should behave like
167 startup was done. Since the tests are run by sourcing a
168 script the "starting" variable is non-zero. This is usually a
169 good thing (tests run faster), but sometimes changes behavior
170 in a way that the test doesn't work properly.
171 When using: >
172 call test_override('starting', 1)
173< The value of "starting" is saved. It is restored by: >
174 call test_override('starting', 0)
175
Bram Moolenaarce90e362019-09-08 18:58:44 +0200176< Can also be used as a |method|: >
177 GetOverrideVal()-> test_override('starting')
Bram Moolenaared997ad2019-07-21 16:42:00 +0200178
179test_refcount({expr}) *test_refcount()*
180 Return the reference count of {expr}. When {expr} is of a
181 type that does not have a reference count, returns -1. Only
182 to be used for testing.
183
Bram Moolenaarce90e362019-09-08 18:58:44 +0200184 Can also be used as a |method|: >
185 GetVarname()->test_refcount()
186
Bram Moolenaared997ad2019-07-21 16:42:00 +0200187
188test_scrollbar({which}, {value}, {dragging}) *test_scrollbar()*
189 Pretend using scrollbar {which} to move it to position
190 {value}. {which} can be:
191 left Left scrollbar of the current window
192 right Right scrollbar of the current window
193 hor Horizontal scrollbar
194
195 For the vertical scrollbars {value} can be 1 to the
196 line-count of the buffer. For the horizontal scrollbar the
197 {value} can be between 1 and the maximum line length, assuming
198 'wrap' is not set.
199
200 When {dragging} is non-zero it's like dragging the scrollbar,
201 otherwise it's like clicking in the scrollbar.
202 Only works when the {which} scrollbar actually exists,
203 obviously only when using the GUI.
204
Bram Moolenaarce90e362019-09-08 18:58:44 +0200205 Can also be used as a |method|: >
206 GetValue()->test_scrollbar('right', 0)
Bram Moolenaared997ad2019-07-21 16:42:00 +0200207
208test_setmouse({row}, {col}) *test_setmouse()*
209 Set the mouse position to be used for the next mouse action.
210 {row} and {col} are one based.
211 For example: >
212 call test_setmouse(4, 20)
213 call feedkeys("\<LeftMouse>", "xt")
214
Bram Moolenaared997ad2019-07-21 16:42:00 +0200215test_settime({expr}) *test_settime()*
216 Set the time Vim uses internally. Currently only used for
217 timestamps in the history, as they are used in viminfo, and
218 for undo.
219 Using a value of 1 makes Vim not sleep after a warning or
220 error message.
221 {expr} must evaluate to a number. When the value is zero the
222 normal behavior is restored.
223
Bram Moolenaarce90e362019-09-08 18:58:44 +0200224 Can also be used as a |method|: >
225 GetTime()->test_settime()
226
Bram Moolenaar4f645c52020-02-08 16:40:39 +0100227test_srand_seed([seed]) *test_srand_seed()*
228 When [seed] is given this sets the seed value used by
229 `srand()`. When omitted the test seed is removed.
230
Bram Moolenaared997ad2019-07-21 16:42:00 +0200231==============================================================================
Bram Moolenaar54775062019-07-31 21:07:14 +02002323. Assert functions *assert-functions-details*
Bram Moolenaared997ad2019-07-21 16:42:00 +0200233
234
235assert_beeps({cmd}) *assert_beeps()*
236 Run {cmd} and add an error message to |v:errors| if it does
237 NOT produce a beep or visual bell.
238 Also see |assert_fails()| and |assert-return|.
239
Bram Moolenaar24278d22019-08-16 21:49:22 +0200240 Can also be used as a |method|: >
241 GetCmd()->assert_beeps()
242<
Bram Moolenaared997ad2019-07-21 16:42:00 +0200243 *assert_equal()*
244assert_equal({expected}, {actual} [, {msg}])
245 When {expected} and {actual} are not equal an error message is
246 added to |v:errors| and 1 is returned. Otherwise zero is
247 returned |assert-return|.
248 There is no automatic conversion, the String "4" is different
249 from the Number 4. And the number 4 is different from the
250 Float 4.0. The value of 'ignorecase' is not used here, case
251 always matters.
252 When {msg} is omitted an error in the form "Expected
253 {expected} but got {actual}" is produced.
254 Example: >
255 assert_equal('foo', 'bar')
256< Will result in a string to be added to |v:errors|:
257 test.vim line 12: Expected 'foo' but got 'bar' ~
258
Bram Moolenaar25e42232019-08-04 15:04:10 +0200259 Can also be used as a |method|: >
260 mylist->assert_equal([1, 2, 3])
261
262
263< *assert_equalfile()*
Bram Moolenaared997ad2019-07-21 16:42:00 +0200264assert_equalfile({fname-one}, {fname-two})
265 When the files {fname-one} and {fname-two} do not contain
266 exactly the same text an error message is added to |v:errors|.
267 Also see |assert-return|.
268 When {fname-one} or {fname-two} does not exist the error will
269 mention that.
270 Mainly useful with |terminal-diff|.
271
Bram Moolenaare49fbff2019-08-21 22:50:07 +0200272 Can also be used as a |method|: >
273 GetLog()->assert_equalfile('expected.log')
274
275
Bram Moolenaared997ad2019-07-21 16:42:00 +0200276assert_exception({error} [, {msg}]) *assert_exception()*
277 When v:exception does not contain the string {error} an error
278 message is added to |v:errors|. Also see |assert-return|.
279 This can be used to assert that a command throws an exception.
280 Using the error number, followed by a colon, avoids problems
281 with translations: >
282 try
283 commandthatfails
284 call assert_false(1, 'command should have failed')
285 catch
286 call assert_exception('E492:')
287 endtry
288
289assert_fails({cmd} [, {error} [, {msg}]]) *assert_fails()*
290 Run {cmd} and add an error message to |v:errors| if it does
291 NOT produce an error. Also see |assert-return|.
292 When {error} is given it must match in |v:errmsg|.
293 Note that beeping is not considered an error, and some failing
294 commands only beep. Use |assert_beeps()| for those.
295
Bram Moolenaar24278d22019-08-16 21:49:22 +0200296 Can also be used as a |method|: >
297 GetCmd()->assert_fails('E99:')
298
Bram Moolenaared997ad2019-07-21 16:42:00 +0200299assert_false({actual} [, {msg}]) *assert_false()*
300 When {actual} is not false an error message is added to
301 |v:errors|, like with |assert_equal()|.
302 Also see |assert-return|.
303 A value is false when it is zero. When {actual} is not a
304 number the assert fails.
305 When {msg} is omitted an error in the form
306 "Expected False but got {actual}" is produced.
307
Bram Moolenaar24278d22019-08-16 21:49:22 +0200308 Can also be used as a |method|: >
309 GetResult()->assert_false()
310
Bram Moolenaared997ad2019-07-21 16:42:00 +0200311assert_inrange({lower}, {upper}, {actual} [, {msg}]) *assert_inrange()*
312 This asserts number and |Float| values. When {actual} is lower
313 than {lower} or higher than {upper} an error message is added
314 to |v:errors|. Also see |assert-return|.
315 When {msg} is omitted an error in the form
316 "Expected range {lower} - {upper}, but got {actual}" is
317 produced.
318
319 *assert_match()*
320assert_match({pattern}, {actual} [, {msg}])
321 When {pattern} does not match {actual} an error message is
322 added to |v:errors|. Also see |assert-return|.
323
324 {pattern} is used as with |=~|: The matching is always done
325 like 'magic' was set and 'cpoptions' is empty, no matter what
326 the actual value of 'magic' or 'cpoptions' is.
327
328 {actual} is used as a string, automatic conversion applies.
329 Use "^" and "$" to match with the start and end of the text.
330 Use both to match the whole text.
331
332 When {msg} is omitted an error in the form
333 "Pattern {pattern} does not match {actual}" is produced.
334 Example: >
335 assert_match('^f.*o$', 'foobar')
336< Will result in a string to be added to |v:errors|:
337 test.vim line 12: Pattern '^f.*o$' does not match 'foobar' ~
338
Bram Moolenaar24278d22019-08-16 21:49:22 +0200339 Can also be used as a |method|: >
340 getFile()->assert_match('foo.*')
341<
Bram Moolenaared997ad2019-07-21 16:42:00 +0200342 *assert_notequal()*
343assert_notequal({expected}, {actual} [, {msg}])
344 The opposite of `assert_equal()`: add an error message to
345 |v:errors| when {expected} and {actual} are equal.
346 Also see |assert-return|.
347
Bram Moolenaar25e42232019-08-04 15:04:10 +0200348 Can also be used as a |method|: >
349 mylist->assert_notequal([1, 2, 3])
350
351< *assert_notmatch()*
Bram Moolenaared997ad2019-07-21 16:42:00 +0200352assert_notmatch({pattern}, {actual} [, {msg}])
353 The opposite of `assert_match()`: add an error message to
354 |v:errors| when {pattern} matches {actual}.
355 Also see |assert-return|.
356
Bram Moolenaar24278d22019-08-16 21:49:22 +0200357 Can also be used as a |method|: >
358 getFile()->assert_notmatch('bar.*')
359
Bram Moolenaare49fbff2019-08-21 22:50:07 +0200360
Bram Moolenaared997ad2019-07-21 16:42:00 +0200361assert_report({msg}) *assert_report()*
362 Report a test failure directly, using {msg}.
363 Always returns one.
364
Bram Moolenaare49fbff2019-08-21 22:50:07 +0200365 Can also be used as a |method|: >
366 GetMessage()->assert_report()
367
368
Bram Moolenaared997ad2019-07-21 16:42:00 +0200369assert_true({actual} [, {msg}]) *assert_true()*
370 When {actual} is not true an error message is added to
371 |v:errors|, like with |assert_equal()|.
372 Also see |assert-return|.
373 A value is TRUE when it is a non-zero number. When {actual}
374 is not a number the assert fails.
375 When {msg} is omitted an error in the form "Expected True but
376 got {actual}" is produced.
377
Bram Moolenaar24278d22019-08-16 21:49:22 +0200378 Can also be used as a |method|: >
379 GetResult()->assert_true()
380<
Bram Moolenaared997ad2019-07-21 16:42:00 +0200381
382 vim:tw=78:ts=8:noet:ft=help:norl: