blob: f48984c9f2bfd81ba4849cce62bdc3f33ac55bbe [file] [log] [blame]
Bram Moolenaarebdf3c92020-02-15 21:41:42 +01001*testing.txt* For Vim version 8.2. Last change: 2020 Feb 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
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
126test_option_not_set({name}) *test_option_not_set()*
127 Reset the flag that indicates option {name} was set. Thus it
128 looks like it still has the default value. Use like this: >
129 set ambiwidth=double
130 call test_option_not_set('ambiwidth')
131< Now the 'ambiwidth' option behaves like it was never changed,
132 even though the value is "double".
133 Only to be used for testing!
134
Bram Moolenaarce90e362019-09-08 18:58:44 +0200135 Can also be used as a |method|: >
136 GetOptionName()->test_option_not_set()
137
Bram Moolenaared997ad2019-07-21 16:42:00 +0200138
139test_override({name}, {val}) *test_override()*
140 Overrides certain parts of Vim's internal processing to be able
141 to run tests. Only to be used for testing Vim!
142 The override is enabled when {val} is non-zero and removed
143 when {val} is zero.
144 Current supported values for name are:
145
146 name effect when {val} is non-zero ~
147 redraw disable the redrawing() function
148 redraw_flag ignore the RedrawingDisabled flag
149 char_avail disable the char_avail() function
150 starting reset the "starting" variable, see below
151 nfa_fail makes the NFA regexp engine fail to force a
152 fallback to the old engine
153 no_query_mouse do not query the mouse position for "dec"
154 terminals
155 no_wait_return set the "no_wait_return" flag. Not restored
156 with "ALL".
157 ALL clear all overrides ({val} is not used)
158
159 "starting" is to be used when a test should behave like
160 startup was done. Since the tests are run by sourcing a
161 script the "starting" variable is non-zero. This is usually a
162 good thing (tests run faster), but sometimes changes behavior
163 in a way that the test doesn't work properly.
164 When using: >
165 call test_override('starting', 1)
166< The value of "starting" is saved. It is restored by: >
167 call test_override('starting', 0)
168
Bram Moolenaarce90e362019-09-08 18:58:44 +0200169< Can also be used as a |method|: >
170 GetOverrideVal()-> test_override('starting')
Bram Moolenaared997ad2019-07-21 16:42:00 +0200171
172test_refcount({expr}) *test_refcount()*
173 Return the reference count of {expr}. When {expr} is of a
174 type that does not have a reference count, returns -1. Only
175 to be used for testing.
176
Bram Moolenaarce90e362019-09-08 18:58:44 +0200177 Can also be used as a |method|: >
178 GetVarname()->test_refcount()
179
Bram Moolenaared997ad2019-07-21 16:42:00 +0200180
181test_scrollbar({which}, {value}, {dragging}) *test_scrollbar()*
182 Pretend using scrollbar {which} to move it to position
183 {value}. {which} can be:
184 left Left scrollbar of the current window
185 right Right scrollbar of the current window
186 hor Horizontal scrollbar
187
188 For the vertical scrollbars {value} can be 1 to the
189 line-count of the buffer. For the horizontal scrollbar the
190 {value} can be between 1 and the maximum line length, assuming
191 'wrap' is not set.
192
193 When {dragging} is non-zero it's like dragging the scrollbar,
194 otherwise it's like clicking in the scrollbar.
195 Only works when the {which} scrollbar actually exists,
196 obviously only when using the GUI.
197
Bram Moolenaarce90e362019-09-08 18:58:44 +0200198 Can also be used as a |method|: >
199 GetValue()->test_scrollbar('right', 0)
Bram Moolenaared997ad2019-07-21 16:42:00 +0200200
201test_setmouse({row}, {col}) *test_setmouse()*
202 Set the mouse position to be used for the next mouse action.
203 {row} and {col} are one based.
204 For example: >
205 call test_setmouse(4, 20)
206 call feedkeys("\<LeftMouse>", "xt")
207
Bram Moolenaared997ad2019-07-21 16:42:00 +0200208test_settime({expr}) *test_settime()*
209 Set the time Vim uses internally. Currently only used for
210 timestamps in the history, as they are used in viminfo, and
211 for undo.
212 Using a value of 1 makes Vim not sleep after a warning or
213 error message.
214 {expr} must evaluate to a number. When the value is zero the
215 normal behavior is restored.
216
Bram Moolenaarce90e362019-09-08 18:58:44 +0200217 Can also be used as a |method|: >
218 GetTime()->test_settime()
219
Bram Moolenaar4f645c52020-02-08 16:40:39 +0100220test_srand_seed([seed]) *test_srand_seed()*
221 When [seed] is given this sets the seed value used by
222 `srand()`. When omitted the test seed is removed.
223
Bram Moolenaared997ad2019-07-21 16:42:00 +0200224==============================================================================
Bram Moolenaar54775062019-07-31 21:07:14 +02002253. Assert functions *assert-functions-details*
Bram Moolenaared997ad2019-07-21 16:42:00 +0200226
227
228assert_beeps({cmd}) *assert_beeps()*
229 Run {cmd} and add an error message to |v:errors| if it does
230 NOT produce a beep or visual bell.
231 Also see |assert_fails()| and |assert-return|.
232
Bram Moolenaar24278d22019-08-16 21:49:22 +0200233 Can also be used as a |method|: >
234 GetCmd()->assert_beeps()
235<
Bram Moolenaared997ad2019-07-21 16:42:00 +0200236 *assert_equal()*
237assert_equal({expected}, {actual} [, {msg}])
238 When {expected} and {actual} are not equal an error message is
239 added to |v:errors| and 1 is returned. Otherwise zero is
240 returned |assert-return|.
241 There is no automatic conversion, the String "4" is different
242 from the Number 4. And the number 4 is different from the
243 Float 4.0. The value of 'ignorecase' is not used here, case
244 always matters.
245 When {msg} is omitted an error in the form "Expected
246 {expected} but got {actual}" is produced.
247 Example: >
248 assert_equal('foo', 'bar')
249< Will result in a string to be added to |v:errors|:
250 test.vim line 12: Expected 'foo' but got 'bar' ~
251
Bram Moolenaar25e42232019-08-04 15:04:10 +0200252 Can also be used as a |method|: >
253 mylist->assert_equal([1, 2, 3])
254
255
256< *assert_equalfile()*
Bram Moolenaared997ad2019-07-21 16:42:00 +0200257assert_equalfile({fname-one}, {fname-two})
258 When the files {fname-one} and {fname-two} do not contain
259 exactly the same text an error message is added to |v:errors|.
260 Also see |assert-return|.
261 When {fname-one} or {fname-two} does not exist the error will
262 mention that.
263 Mainly useful with |terminal-diff|.
264
Bram Moolenaare49fbff2019-08-21 22:50:07 +0200265 Can also be used as a |method|: >
266 GetLog()->assert_equalfile('expected.log')
267
268
Bram Moolenaared997ad2019-07-21 16:42:00 +0200269assert_exception({error} [, {msg}]) *assert_exception()*
270 When v:exception does not contain the string {error} an error
271 message is added to |v:errors|. Also see |assert-return|.
272 This can be used to assert that a command throws an exception.
273 Using the error number, followed by a colon, avoids problems
274 with translations: >
275 try
276 commandthatfails
277 call assert_false(1, 'command should have failed')
278 catch
279 call assert_exception('E492:')
280 endtry
281
282assert_fails({cmd} [, {error} [, {msg}]]) *assert_fails()*
283 Run {cmd} and add an error message to |v:errors| if it does
284 NOT produce an error. Also see |assert-return|.
285 When {error} is given it must match in |v:errmsg|.
286 Note that beeping is not considered an error, and some failing
287 commands only beep. Use |assert_beeps()| for those.
288
Bram Moolenaar24278d22019-08-16 21:49:22 +0200289 Can also be used as a |method|: >
290 GetCmd()->assert_fails('E99:')
291
Bram Moolenaared997ad2019-07-21 16:42:00 +0200292assert_false({actual} [, {msg}]) *assert_false()*
293 When {actual} is not false an error message is added to
294 |v:errors|, like with |assert_equal()|.
295 Also see |assert-return|.
296 A value is false when it is zero. When {actual} is not a
297 number the assert fails.
298 When {msg} is omitted an error in the form
299 "Expected False but got {actual}" is produced.
300
Bram Moolenaar24278d22019-08-16 21:49:22 +0200301 Can also be used as a |method|: >
302 GetResult()->assert_false()
303
Bram Moolenaared997ad2019-07-21 16:42:00 +0200304assert_inrange({lower}, {upper}, {actual} [, {msg}]) *assert_inrange()*
305 This asserts number and |Float| values. When {actual} is lower
306 than {lower} or higher than {upper} an error message is added
307 to |v:errors|. Also see |assert-return|.
308 When {msg} is omitted an error in the form
309 "Expected range {lower} - {upper}, but got {actual}" is
310 produced.
311
312 *assert_match()*
313assert_match({pattern}, {actual} [, {msg}])
314 When {pattern} does not match {actual} an error message is
315 added to |v:errors|. Also see |assert-return|.
316
317 {pattern} is used as with |=~|: The matching is always done
318 like 'magic' was set and 'cpoptions' is empty, no matter what
319 the actual value of 'magic' or 'cpoptions' is.
320
321 {actual} is used as a string, automatic conversion applies.
322 Use "^" and "$" to match with the start and end of the text.
323 Use both to match the whole text.
324
325 When {msg} is omitted an error in the form
326 "Pattern {pattern} does not match {actual}" is produced.
327 Example: >
328 assert_match('^f.*o$', 'foobar')
329< Will result in a string to be added to |v:errors|:
330 test.vim line 12: Pattern '^f.*o$' does not match 'foobar' ~
331
Bram Moolenaar24278d22019-08-16 21:49:22 +0200332 Can also be used as a |method|: >
333 getFile()->assert_match('foo.*')
334<
Bram Moolenaared997ad2019-07-21 16:42:00 +0200335 *assert_notequal()*
336assert_notequal({expected}, {actual} [, {msg}])
337 The opposite of `assert_equal()`: add an error message to
338 |v:errors| when {expected} and {actual} are equal.
339 Also see |assert-return|.
340
Bram Moolenaar25e42232019-08-04 15:04:10 +0200341 Can also be used as a |method|: >
342 mylist->assert_notequal([1, 2, 3])
343
344< *assert_notmatch()*
Bram Moolenaared997ad2019-07-21 16:42:00 +0200345assert_notmatch({pattern}, {actual} [, {msg}])
346 The opposite of `assert_match()`: add an error message to
347 |v:errors| when {pattern} matches {actual}.
348 Also see |assert-return|.
349
Bram Moolenaar24278d22019-08-16 21:49:22 +0200350 Can also be used as a |method|: >
351 getFile()->assert_notmatch('bar.*')
352
Bram Moolenaare49fbff2019-08-21 22:50:07 +0200353
Bram Moolenaared997ad2019-07-21 16:42:00 +0200354assert_report({msg}) *assert_report()*
355 Report a test failure directly, using {msg}.
356 Always returns one.
357
Bram Moolenaare49fbff2019-08-21 22:50:07 +0200358 Can also be used as a |method|: >
359 GetMessage()->assert_report()
360
361
Bram Moolenaared997ad2019-07-21 16:42:00 +0200362assert_true({actual} [, {msg}]) *assert_true()*
363 When {actual} is not true an error message is added to
364 |v:errors|, like with |assert_equal()|.
365 Also see |assert-return|.
366 A value is TRUE when it is a non-zero number. When {actual}
367 is not a number the assert fails.
368 When {msg} is omitted an error in the form "Expected True but
369 got {actual}" is produced.
370
Bram Moolenaar24278d22019-08-16 21:49:22 +0200371 Can also be used as a |method|: >
372 GetResult()->assert_true()
373<
Bram Moolenaared997ad2019-07-21 16:42:00 +0200374
375 vim:tw=78:ts=8:noet:ft=help:norl: