blob: 7fb24e63079f034f6599ef0b581869661c358c6a [file] [log] [blame]
Bram Moolenaar98056532019-12-12 14:18:35 +01001*testing.txt* For Vim version 8.2. Last change: 2019 Sep 08
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
Bram Moolenaar07ada5f2020-02-05 20:38:22 +010055test_clear_search_pat() *test_clear_search_pat()*
56 Clears the last used search pattern (|/|) and the substitute
57 pattern (|:s|). This is useful for testing conditions where
58 these patterns are not set previously.
59
Bram Moolenaared997ad2019-07-21 16:42:00 +020060test_feedinput({string}) *test_feedinput()*
61 Characters in {string} are queued for processing as if they
62 were typed by the user. This uses a low level input buffer.
63 This function works only when with |+unix| or GUI is running.
64
Bram Moolenaarce90e362019-09-08 18:58:44 +020065 Can also be used as a |method|: >
66 GetText()->test_feedinput()
Bram Moolenaared997ad2019-07-21 16:42:00 +020067
68test_garbagecollect_now() *test_garbagecollect_now()*
69 Like garbagecollect(), but executed right away. This must
70 only be called directly to avoid any structure to exist
71 internally, and |v:testing| must have been set before calling
72 any function.
73
74
75test_garbagecollect_soon() *test_garbagecollect_soon()*
76 Set the flag to call the garbagecollector as if in the main
77 loop. Only to be used in tests.
78
79
80test_getvalue({name}) *test_getvalue()*
81 Get the value of an internal variable. These values for
82 {name} are supported:
83 need_fileinfo
84
Bram Moolenaarce90e362019-09-08 18:58:44 +020085 Can also be used as a |method|: >
86 GetName()->test_getvalue()
Bram Moolenaared997ad2019-07-21 16:42:00 +020087
88test_ignore_error({expr}) *test_ignore_error()*
89 Ignore any error containing {expr}. A normal message is given
90 instead.
91 This is only meant to be used in tests, where catching the
92 error with try/catch cannot be used (because it skips over
93 following code).
94 {expr} is used literally, not as a pattern.
95 When the {expr} is the string "RESET" then the list of ignored
96 errors is made empty.
97
Bram Moolenaarce90e362019-09-08 18:58:44 +020098 Can also be used as a |method|: >
99 GetErrorText()->test_ignore_error()
Bram Moolenaared997ad2019-07-21 16:42:00 +0200100
101test_null_blob() *test_null_blob()*
102 Return a |Blob| that is null. Only useful for testing.
103
104
105test_null_channel() *test_null_channel()*
106 Return a |Channel| that is null. Only useful for testing.
107 {only available when compiled with the +channel feature}
108
109
110test_null_dict() *test_null_dict()*
111 Return a |Dict| that is null. Only useful for testing.
112
113
114test_null_job() *test_null_job()*
115 Return a |Job| that is null. Only useful for testing.
116 {only available when compiled with the +job feature}
117
118
119test_null_list() *test_null_list()*
120 Return a |List| that is null. Only useful for testing.
121
122
123test_null_partial() *test_null_partial()*
124 Return a |Partial| that is null. Only useful for testing.
125
126
127test_null_string() *test_null_string()*
128 Return a |String| that is null. Only useful for testing.
129
130
131test_option_not_set({name}) *test_option_not_set()*
132 Reset the flag that indicates option {name} was set. Thus it
133 looks like it still has the default value. Use like this: >
134 set ambiwidth=double
135 call test_option_not_set('ambiwidth')
136< Now the 'ambiwidth' option behaves like it was never changed,
137 even though the value is "double".
138 Only to be used for testing!
139
Bram Moolenaarce90e362019-09-08 18:58:44 +0200140 Can also be used as a |method|: >
141 GetOptionName()->test_option_not_set()
142
Bram Moolenaared997ad2019-07-21 16:42:00 +0200143
144test_override({name}, {val}) *test_override()*
145 Overrides certain parts of Vim's internal processing to be able
146 to run tests. Only to be used for testing Vim!
147 The override is enabled when {val} is non-zero and removed
148 when {val} is zero.
149 Current supported values for name are:
150
151 name effect when {val} is non-zero ~
152 redraw disable the redrawing() function
153 redraw_flag ignore the RedrawingDisabled flag
154 char_avail disable the char_avail() function
155 starting reset the "starting" variable, see below
156 nfa_fail makes the NFA regexp engine fail to force a
157 fallback to the old engine
158 no_query_mouse do not query the mouse position for "dec"
159 terminals
160 no_wait_return set the "no_wait_return" flag. Not restored
161 with "ALL".
162 ALL clear all overrides ({val} is not used)
163
164 "starting" is to be used when a test should behave like
165 startup was done. Since the tests are run by sourcing a
166 script the "starting" variable is non-zero. This is usually a
167 good thing (tests run faster), but sometimes changes behavior
168 in a way that the test doesn't work properly.
169 When using: >
170 call test_override('starting', 1)
171< The value of "starting" is saved. It is restored by: >
172 call test_override('starting', 0)
173
Bram Moolenaarce90e362019-09-08 18:58:44 +0200174< Can also be used as a |method|: >
175 GetOverrideVal()-> test_override('starting')
Bram Moolenaared997ad2019-07-21 16:42:00 +0200176
177test_refcount({expr}) *test_refcount()*
178 Return the reference count of {expr}. When {expr} is of a
179 type that does not have a reference count, returns -1. Only
180 to be used for testing.
181
Bram Moolenaarce90e362019-09-08 18:58:44 +0200182 Can also be used as a |method|: >
183 GetVarname()->test_refcount()
184
Bram Moolenaared997ad2019-07-21 16:42:00 +0200185
186test_scrollbar({which}, {value}, {dragging}) *test_scrollbar()*
187 Pretend using scrollbar {which} to move it to position
188 {value}. {which} can be:
189 left Left scrollbar of the current window
190 right Right scrollbar of the current window
191 hor Horizontal scrollbar
192
193 For the vertical scrollbars {value} can be 1 to the
194 line-count of the buffer. For the horizontal scrollbar the
195 {value} can be between 1 and the maximum line length, assuming
196 'wrap' is not set.
197
198 When {dragging} is non-zero it's like dragging the scrollbar,
199 otherwise it's like clicking in the scrollbar.
200 Only works when the {which} scrollbar actually exists,
201 obviously only when using the GUI.
202
Bram Moolenaarce90e362019-09-08 18:58:44 +0200203 Can also be used as a |method|: >
204 GetValue()->test_scrollbar('right', 0)
Bram Moolenaared997ad2019-07-21 16:42:00 +0200205
206test_setmouse({row}, {col}) *test_setmouse()*
207 Set the mouse position to be used for the next mouse action.
208 {row} and {col} are one based.
209 For example: >
210 call test_setmouse(4, 20)
211 call feedkeys("\<LeftMouse>", "xt")
212
213
214test_settime({expr}) *test_settime()*
215 Set the time Vim uses internally. Currently only used for
216 timestamps in the history, as they are used in viminfo, and
217 for undo.
218 Using a value of 1 makes Vim not sleep after a warning or
219 error message.
220 {expr} must evaluate to a number. When the value is zero the
221 normal behavior is restored.
222
Bram Moolenaarce90e362019-09-08 18:58:44 +0200223 Can also be used as a |method|: >
224 GetTime()->test_settime()
225
Bram Moolenaared997ad2019-07-21 16:42:00 +0200226==============================================================================
Bram Moolenaar54775062019-07-31 21:07:14 +02002273. Assert functions *assert-functions-details*
Bram Moolenaared997ad2019-07-21 16:42:00 +0200228
229
230assert_beeps({cmd}) *assert_beeps()*
231 Run {cmd} and add an error message to |v:errors| if it does
232 NOT produce a beep or visual bell.
233 Also see |assert_fails()| and |assert-return|.
234
Bram Moolenaar24278d22019-08-16 21:49:22 +0200235 Can also be used as a |method|: >
236 GetCmd()->assert_beeps()
237<
Bram Moolenaared997ad2019-07-21 16:42:00 +0200238 *assert_equal()*
239assert_equal({expected}, {actual} [, {msg}])
240 When {expected} and {actual} are not equal an error message is
241 added to |v:errors| and 1 is returned. Otherwise zero is
242 returned |assert-return|.
243 There is no automatic conversion, the String "4" is different
244 from the Number 4. And the number 4 is different from the
245 Float 4.0. The value of 'ignorecase' is not used here, case
246 always matters.
247 When {msg} is omitted an error in the form "Expected
248 {expected} but got {actual}" is produced.
249 Example: >
250 assert_equal('foo', 'bar')
251< Will result in a string to be added to |v:errors|:
252 test.vim line 12: Expected 'foo' but got 'bar' ~
253
Bram Moolenaar25e42232019-08-04 15:04:10 +0200254 Can also be used as a |method|: >
255 mylist->assert_equal([1, 2, 3])
256
257
258< *assert_equalfile()*
Bram Moolenaared997ad2019-07-21 16:42:00 +0200259assert_equalfile({fname-one}, {fname-two})
260 When the files {fname-one} and {fname-two} do not contain
261 exactly the same text an error message is added to |v:errors|.
262 Also see |assert-return|.
263 When {fname-one} or {fname-two} does not exist the error will
264 mention that.
265 Mainly useful with |terminal-diff|.
266
Bram Moolenaare49fbff2019-08-21 22:50:07 +0200267 Can also be used as a |method|: >
268 GetLog()->assert_equalfile('expected.log')
269
270
Bram Moolenaared997ad2019-07-21 16:42:00 +0200271assert_exception({error} [, {msg}]) *assert_exception()*
272 When v:exception does not contain the string {error} an error
273 message is added to |v:errors|. Also see |assert-return|.
274 This can be used to assert that a command throws an exception.
275 Using the error number, followed by a colon, avoids problems
276 with translations: >
277 try
278 commandthatfails
279 call assert_false(1, 'command should have failed')
280 catch
281 call assert_exception('E492:')
282 endtry
283
284assert_fails({cmd} [, {error} [, {msg}]]) *assert_fails()*
285 Run {cmd} and add an error message to |v:errors| if it does
286 NOT produce an error. Also see |assert-return|.
287 When {error} is given it must match in |v:errmsg|.
288 Note that beeping is not considered an error, and some failing
289 commands only beep. Use |assert_beeps()| for those.
290
Bram Moolenaar24278d22019-08-16 21:49:22 +0200291 Can also be used as a |method|: >
292 GetCmd()->assert_fails('E99:')
293
Bram Moolenaared997ad2019-07-21 16:42:00 +0200294assert_false({actual} [, {msg}]) *assert_false()*
295 When {actual} is not false an error message is added to
296 |v:errors|, like with |assert_equal()|.
297 Also see |assert-return|.
298 A value is false when it is zero. When {actual} is not a
299 number the assert fails.
300 When {msg} is omitted an error in the form
301 "Expected False but got {actual}" is produced.
302
Bram Moolenaar24278d22019-08-16 21:49:22 +0200303 Can also be used as a |method|: >
304 GetResult()->assert_false()
305
Bram Moolenaared997ad2019-07-21 16:42:00 +0200306assert_inrange({lower}, {upper}, {actual} [, {msg}]) *assert_inrange()*
307 This asserts number and |Float| values. When {actual} is lower
308 than {lower} or higher than {upper} an error message is added
309 to |v:errors|. Also see |assert-return|.
310 When {msg} is omitted an error in the form
311 "Expected range {lower} - {upper}, but got {actual}" is
312 produced.
313
314 *assert_match()*
315assert_match({pattern}, {actual} [, {msg}])
316 When {pattern} does not match {actual} an error message is
317 added to |v:errors|. Also see |assert-return|.
318
319 {pattern} is used as with |=~|: The matching is always done
320 like 'magic' was set and 'cpoptions' is empty, no matter what
321 the actual value of 'magic' or 'cpoptions' is.
322
323 {actual} is used as a string, automatic conversion applies.
324 Use "^" and "$" to match with the start and end of the text.
325 Use both to match the whole text.
326
327 When {msg} is omitted an error in the form
328 "Pattern {pattern} does not match {actual}" is produced.
329 Example: >
330 assert_match('^f.*o$', 'foobar')
331< Will result in a string to be added to |v:errors|:
332 test.vim line 12: Pattern '^f.*o$' does not match 'foobar' ~
333
Bram Moolenaar24278d22019-08-16 21:49:22 +0200334 Can also be used as a |method|: >
335 getFile()->assert_match('foo.*')
336<
Bram Moolenaared997ad2019-07-21 16:42:00 +0200337 *assert_notequal()*
338assert_notequal({expected}, {actual} [, {msg}])
339 The opposite of `assert_equal()`: add an error message to
340 |v:errors| when {expected} and {actual} are equal.
341 Also see |assert-return|.
342
Bram Moolenaar25e42232019-08-04 15:04:10 +0200343 Can also be used as a |method|: >
344 mylist->assert_notequal([1, 2, 3])
345
346< *assert_notmatch()*
Bram Moolenaared997ad2019-07-21 16:42:00 +0200347assert_notmatch({pattern}, {actual} [, {msg}])
348 The opposite of `assert_match()`: add an error message to
349 |v:errors| when {pattern} matches {actual}.
350 Also see |assert-return|.
351
Bram Moolenaar24278d22019-08-16 21:49:22 +0200352 Can also be used as a |method|: >
353 getFile()->assert_notmatch('bar.*')
354
Bram Moolenaare49fbff2019-08-21 22:50:07 +0200355
Bram Moolenaared997ad2019-07-21 16:42:00 +0200356assert_report({msg}) *assert_report()*
357 Report a test failure directly, using {msg}.
358 Always returns one.
359
Bram Moolenaare49fbff2019-08-21 22:50:07 +0200360 Can also be used as a |method|: >
361 GetMessage()->assert_report()
362
363
Bram Moolenaared997ad2019-07-21 16:42:00 +0200364assert_true({actual} [, {msg}]) *assert_true()*
365 When {actual} is not true an error message is added to
366 |v:errors|, like with |assert_equal()|.
367 Also see |assert-return|.
368 A value is TRUE when it is a non-zero number. When {actual}
369 is not a number the assert fails.
370 When {msg} is omitted an error in the form "Expected True but
371 got {actual}" is produced.
372
Bram Moolenaar24278d22019-08-16 21:49:22 +0200373 Can also be used as a |method|: >
374 GetResult()->assert_true()
375<
Bram Moolenaared997ad2019-07-21 16:42:00 +0200376
377 vim:tw=78:ts=8:noet:ft=help:norl: