blob: 1147673d599236cf998e5bbae2a45bbebed145cc [file] [log] [blame]
Bram Moolenaar2547aa92020-07-26 17:00:44 +02001*testing.txt* For Vim version 8.2. Last change: 2020 Jul 11
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 Moolenaared997ad2019-07-21 16:42:00 +020023 *new-style-testing*
Bram Moolenaarf7c4d832020-08-11 20:42:19 +020024New tests should be added as new style tests. The test scripts are named
25test_<feature>.vim (replace <feature> with the feature under test). These use
26functions such as |assert_equal()| to keep the test commands and the expected
27result in one place.
Bram Moolenaared997ad2019-07-21 16:42:00 +020028
29Find more information in the file src/testdir/README.txt.
30
31==============================================================================
Bram Moolenaar54775062019-07-31 21:07:14 +0200322. Test functions *test-functions-details*
Bram Moolenaared997ad2019-07-21 16:42:00 +020033
34test_alloc_fail({id}, {countdown}, {repeat}) *test_alloc_fail()*
35 This is for testing: If the memory allocation with {id} is
36 called, then decrement {countdown}, and when it reaches zero
37 let memory allocation fail {repeat} times. When {repeat} is
38 smaller than one it fails one time.
39
Bram Moolenaarce90e362019-09-08 18:58:44 +020040 Can also be used as a |method|: >
41 GetAllocId()->test_alloc_fail()
Bram Moolenaared997ad2019-07-21 16:42:00 +020042
43test_autochdir() *test_autochdir()*
44 Set a flag to enable the effect of 'autochdir' before Vim
45 startup has finished.
46
47
48test_feedinput({string}) *test_feedinput()*
49 Characters in {string} are queued for processing as if they
50 were typed by the user. This uses a low level input buffer.
51 This function works only when with |+unix| or GUI is running.
52
Bram Moolenaarce90e362019-09-08 18:58:44 +020053 Can also be used as a |method|: >
54 GetText()->test_feedinput()
Bram Moolenaared997ad2019-07-21 16:42:00 +020055
56test_garbagecollect_now() *test_garbagecollect_now()*
57 Like garbagecollect(), but executed right away. This must
58 only be called directly to avoid any structure to exist
59 internally, and |v:testing| must have been set before calling
60 any function.
61
62
63test_garbagecollect_soon() *test_garbagecollect_soon()*
64 Set the flag to call the garbagecollector as if in the main
65 loop. Only to be used in tests.
66
67
68test_getvalue({name}) *test_getvalue()*
69 Get the value of an internal variable. These values for
70 {name} are supported:
71 need_fileinfo
72
Bram Moolenaarce90e362019-09-08 18:58:44 +020073 Can also be used as a |method|: >
74 GetName()->test_getvalue()
Bram Moolenaared997ad2019-07-21 16:42:00 +020075
76test_ignore_error({expr}) *test_ignore_error()*
77 Ignore any error containing {expr}. A normal message is given
78 instead.
79 This is only meant to be used in tests, where catching the
80 error with try/catch cannot be used (because it skips over
81 following code).
82 {expr} is used literally, not as a pattern.
83 When the {expr} is the string "RESET" then the list of ignored
84 errors is made empty.
85
Bram Moolenaarce90e362019-09-08 18:58:44 +020086 Can also be used as a |method|: >
87 GetErrorText()->test_ignore_error()
Bram Moolenaared997ad2019-07-21 16:42:00 +020088
89test_null_blob() *test_null_blob()*
90 Return a |Blob| that is null. Only useful for testing.
91
92
93test_null_channel() *test_null_channel()*
94 Return a |Channel| that is null. Only useful for testing.
95 {only available when compiled with the +channel feature}
96
97
98test_null_dict() *test_null_dict()*
99 Return a |Dict| that is null. Only useful for testing.
100
101
Bram Moolenaare69f6d02020-04-01 22:11:01 +0200102test_null_function() *test_null_function()*
Bram Moolenaard1caa942020-04-10 22:10:56 +0200103 Return a |Funcref| that is null. Only useful for testing.
Bram Moolenaare69f6d02020-04-01 22:11:01 +0200104
105
Bram Moolenaared997ad2019-07-21 16:42:00 +0200106test_null_job() *test_null_job()*
107 Return a |Job| that is null. Only useful for testing.
108 {only available when compiled with the +job feature}
109
110
111test_null_list() *test_null_list()*
112 Return a |List| that is null. Only useful for testing.
113
114
115test_null_partial() *test_null_partial()*
116 Return a |Partial| that is null. Only useful for testing.
117
118
119test_null_string() *test_null_string()*
120 Return a |String| that is null. Only useful for testing.
121
122
Bram Moolenaar8ed04582020-02-22 19:07:28 +0100123test_unknown() *test_unknown()*
124 Return a value with unknown type. Only useful for testing.
125
126test_void() *test_void()*
127 Return a value with void type. Only useful for testing.
128
129
Bram Moolenaared997ad2019-07-21 16:42:00 +0200130test_option_not_set({name}) *test_option_not_set()*
131 Reset the flag that indicates option {name} was set. Thus it
132 looks like it still has the default value. Use like this: >
133 set ambiwidth=double
134 call test_option_not_set('ambiwidth')
135< Now the 'ambiwidth' option behaves like it was never changed,
136 even though the value is "double".
137 Only to be used for testing!
138
Bram Moolenaarce90e362019-09-08 18:58:44 +0200139 Can also be used as a |method|: >
140 GetOptionName()->test_option_not_set()
141
Bram Moolenaared997ad2019-07-21 16:42:00 +0200142
143test_override({name}, {val}) *test_override()*
144 Overrides certain parts of Vim's internal processing to be able
145 to run tests. Only to be used for testing Vim!
146 The override is enabled when {val} is non-zero and removed
147 when {val} is zero.
148 Current supported values for name are:
149
150 name effect when {val} is non-zero ~
151 redraw disable the redrawing() function
152 redraw_flag ignore the RedrawingDisabled flag
153 char_avail disable the char_avail() function
154 starting reset the "starting" variable, see below
155 nfa_fail makes the NFA regexp engine fail to force a
156 fallback to the old engine
157 no_query_mouse do not query the mouse position for "dec"
158 terminals
159 no_wait_return set the "no_wait_return" flag. Not restored
160 with "ALL".
Bram Moolenaarb340bae2020-06-15 19:51:56 +0200161 ui_delay time in msec to use in ui_delay(); overrules a
162 wait time of up to 3 seconds for messages
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +0200163 term_props reset all terminal properties when the version
164 string is detected
Bram Moolenaared997ad2019-07-21 16:42:00 +0200165 ALL clear all overrides ({val} is not used)
166
167 "starting" is to be used when a test should behave like
168 startup was done. Since the tests are run by sourcing a
169 script the "starting" variable is non-zero. This is usually a
170 good thing (tests run faster), but sometimes changes behavior
171 in a way that the test doesn't work properly.
172 When using: >
173 call test_override('starting', 1)
174< The value of "starting" is saved. It is restored by: >
175 call test_override('starting', 0)
176
Bram Moolenaarce90e362019-09-08 18:58:44 +0200177< Can also be used as a |method|: >
178 GetOverrideVal()-> test_override('starting')
Bram Moolenaared997ad2019-07-21 16:42:00 +0200179
180test_refcount({expr}) *test_refcount()*
181 Return the reference count of {expr}. When {expr} is of a
182 type that does not have a reference count, returns -1. Only
183 to be used for testing.
184
Bram Moolenaarce90e362019-09-08 18:58:44 +0200185 Can also be used as a |method|: >
186 GetVarname()->test_refcount()
187
Bram Moolenaared997ad2019-07-21 16:42:00 +0200188
189test_scrollbar({which}, {value}, {dragging}) *test_scrollbar()*
190 Pretend using scrollbar {which} to move it to position
191 {value}. {which} can be:
192 left Left scrollbar of the current window
193 right Right scrollbar of the current window
194 hor Horizontal scrollbar
195
196 For the vertical scrollbars {value} can be 1 to the
197 line-count of the buffer. For the horizontal scrollbar the
198 {value} can be between 1 and the maximum line length, assuming
199 'wrap' is not set.
200
201 When {dragging} is non-zero it's like dragging the scrollbar,
202 otherwise it's like clicking in the scrollbar.
203 Only works when the {which} scrollbar actually exists,
204 obviously only when using the GUI.
205
Bram Moolenaarce90e362019-09-08 18:58:44 +0200206 Can also be used as a |method|: >
207 GetValue()->test_scrollbar('right', 0)
Bram Moolenaared997ad2019-07-21 16:42:00 +0200208
209test_setmouse({row}, {col}) *test_setmouse()*
210 Set the mouse position to be used for the next mouse action.
211 {row} and {col} are one based.
212 For example: >
213 call test_setmouse(4, 20)
214 call feedkeys("\<LeftMouse>", "xt")
215
Bram Moolenaared997ad2019-07-21 16:42:00 +0200216test_settime({expr}) *test_settime()*
217 Set the time Vim uses internally. Currently only used for
218 timestamps in the history, as they are used in viminfo, and
219 for undo.
220 Using a value of 1 makes Vim not sleep after a warning or
221 error message.
222 {expr} must evaluate to a number. When the value is zero the
223 normal behavior is restored.
224
Bram Moolenaarce90e362019-09-08 18:58:44 +0200225 Can also be used as a |method|: >
226 GetTime()->test_settime()
227
Bram Moolenaar4f645c52020-02-08 16:40:39 +0100228test_srand_seed([seed]) *test_srand_seed()*
229 When [seed] is given this sets the seed value used by
230 `srand()`. When omitted the test seed is removed.
231
Bram Moolenaared997ad2019-07-21 16:42:00 +0200232==============================================================================
Bram Moolenaar54775062019-07-31 21:07:14 +02002333. Assert functions *assert-functions-details*
Bram Moolenaared997ad2019-07-21 16:42:00 +0200234
235
236assert_beeps({cmd}) *assert_beeps()*
237 Run {cmd} and add an error message to |v:errors| if it does
238 NOT produce a beep or visual bell.
239 Also see |assert_fails()| and |assert-return|.
240
Bram Moolenaar24278d22019-08-16 21:49:22 +0200241 Can also be used as a |method|: >
242 GetCmd()->assert_beeps()
243<
Bram Moolenaared997ad2019-07-21 16:42:00 +0200244 *assert_equal()*
245assert_equal({expected}, {actual} [, {msg}])
246 When {expected} and {actual} are not equal an error message is
247 added to |v:errors| and 1 is returned. Otherwise zero is
248 returned |assert-return|.
249 There is no automatic conversion, the String "4" is different
250 from the Number 4. And the number 4 is different from the
251 Float 4.0. The value of 'ignorecase' is not used here, case
252 always matters.
253 When {msg} is omitted an error in the form "Expected
254 {expected} but got {actual}" is produced.
255 Example: >
256 assert_equal('foo', 'bar')
257< Will result in a string to be added to |v:errors|:
258 test.vim line 12: Expected 'foo' but got 'bar' ~
259
Bram Moolenaar7ff78462020-07-10 22:00:53 +0200260 Can also be used as a |method|, the base is passed as the
261 second argument: >
Bram Moolenaar25e42232019-08-04 15:04:10 +0200262 mylist->assert_equal([1, 2, 3])
263
Bram Moolenaar25e42232019-08-04 15:04:10 +0200264< *assert_equalfile()*
Bram Moolenaarfb517ba2020-06-03 19:55:35 +0200265assert_equalfile({fname-one}, {fname-two} [, {msg}])
Bram Moolenaared997ad2019-07-21 16:42:00 +0200266 When the files {fname-one} and {fname-two} do not contain
267 exactly the same text an error message is added to |v:errors|.
268 Also see |assert-return|.
269 When {fname-one} or {fname-two} does not exist the error will
270 mention that.
271 Mainly useful with |terminal-diff|.
272
Bram Moolenaare49fbff2019-08-21 22:50:07 +0200273 Can also be used as a |method|: >
274 GetLog()->assert_equalfile('expected.log')
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
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200291 NOT produce an error or when {error} is not found in the
292 error message. Also see |assert-return|.
293
294 When {error} is a string it must be found literally in the
295 first reported error. Most often this will be the error code,
296 including the colon, e.g. "E123:". >
297 assert_fails('bad cmd', 'E987:')
298<
299 When {error} is a |List| with one or two strings, these are
300 used as patterns. The first pattern is matched against the
301 first reported error: >
302 assert_fails('cmd', ['E987:.*expected bool'])
303< The second pattern, if present, is matched against the last
304 reported error. To only match the last error use an empty
305 string for the first error: >
306 assert_fails('cmd', ['', 'E987:'])
307<
Bram Moolenaared997ad2019-07-21 16:42:00 +0200308 Note that beeping is not considered an error, and some failing
309 commands only beep. Use |assert_beeps()| for those.
310
Bram Moolenaar24278d22019-08-16 21:49:22 +0200311 Can also be used as a |method|: >
312 GetCmd()->assert_fails('E99:')
313
Bram Moolenaared997ad2019-07-21 16:42:00 +0200314assert_false({actual} [, {msg}]) *assert_false()*
315 When {actual} is not false an error message is added to
316 |v:errors|, like with |assert_equal()|.
317 Also see |assert-return|.
318 A value is false when it is zero. When {actual} is not a
319 number the assert fails.
320 When {msg} is omitted an error in the form
321 "Expected False but got {actual}" is produced.
322
Bram Moolenaar24278d22019-08-16 21:49:22 +0200323 Can also be used as a |method|: >
324 GetResult()->assert_false()
325
Bram Moolenaared997ad2019-07-21 16:42:00 +0200326assert_inrange({lower}, {upper}, {actual} [, {msg}]) *assert_inrange()*
327 This asserts number and |Float| values. When {actual} is lower
328 than {lower} or higher than {upper} an error message is added
329 to |v:errors|. Also see |assert-return|.
330 When {msg} is omitted an error in the form
331 "Expected range {lower} - {upper}, but got {actual}" is
332 produced.
333
334 *assert_match()*
335assert_match({pattern}, {actual} [, {msg}])
336 When {pattern} does not match {actual} an error message is
337 added to |v:errors|. Also see |assert-return|.
338
339 {pattern} is used as with |=~|: The matching is always done
340 like 'magic' was set and 'cpoptions' is empty, no matter what
341 the actual value of 'magic' or 'cpoptions' is.
342
343 {actual} is used as a string, automatic conversion applies.
344 Use "^" and "$" to match with the start and end of the text.
345 Use both to match the whole text.
346
347 When {msg} is omitted an error in the form
348 "Pattern {pattern} does not match {actual}" is produced.
349 Example: >
350 assert_match('^f.*o$', 'foobar')
351< Will result in a string to be added to |v:errors|:
352 test.vim line 12: Pattern '^f.*o$' does not match 'foobar' ~
353
Bram Moolenaar24278d22019-08-16 21:49:22 +0200354 Can also be used as a |method|: >
355 getFile()->assert_match('foo.*')
356<
Bram Moolenaared997ad2019-07-21 16:42:00 +0200357 *assert_notequal()*
358assert_notequal({expected}, {actual} [, {msg}])
359 The opposite of `assert_equal()`: add an error message to
360 |v:errors| when {expected} and {actual} are equal.
361 Also see |assert-return|.
362
Bram Moolenaar25e42232019-08-04 15:04:10 +0200363 Can also be used as a |method|: >
364 mylist->assert_notequal([1, 2, 3])
365
366< *assert_notmatch()*
Bram Moolenaared997ad2019-07-21 16:42:00 +0200367assert_notmatch({pattern}, {actual} [, {msg}])
368 The opposite of `assert_match()`: add an error message to
369 |v:errors| when {pattern} matches {actual}.
370 Also see |assert-return|.
371
Bram Moolenaar24278d22019-08-16 21:49:22 +0200372 Can also be used as a |method|: >
373 getFile()->assert_notmatch('bar.*')
374
Bram Moolenaare49fbff2019-08-21 22:50:07 +0200375
Bram Moolenaared997ad2019-07-21 16:42:00 +0200376assert_report({msg}) *assert_report()*
377 Report a test failure directly, using {msg}.
378 Always returns one.
379
Bram Moolenaare49fbff2019-08-21 22:50:07 +0200380 Can also be used as a |method|: >
381 GetMessage()->assert_report()
382
383
Bram Moolenaared997ad2019-07-21 16:42:00 +0200384assert_true({actual} [, {msg}]) *assert_true()*
385 When {actual} is not true an error message is added to
386 |v:errors|, like with |assert_equal()|.
387 Also see |assert-return|.
388 A value is TRUE when it is a non-zero number. When {actual}
389 is not a number the assert fails.
390 When {msg} is omitted an error in the form "Expected True but
391 got {actual}" is produced.
392
Bram Moolenaar24278d22019-08-16 21:49:22 +0200393 Can also be used as a |method|: >
394 GetResult()->assert_true()
395<
Bram Moolenaared997ad2019-07-21 16:42:00 +0200396
397 vim:tw=78:ts=8:noet:ft=help:norl: