blob: c5fa2e83ccdd964a3677d57cecb2dd9e776f83ba [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
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
208
209test_settime({expr}) *test_settime()*
210 Set the time Vim uses internally. Currently only used for
211 timestamps in the history, as they are used in viminfo, and
212 for undo.
213 Using a value of 1 makes Vim not sleep after a warning or
214 error message.
215 {expr} must evaluate to a number. When the value is zero the
216 normal behavior is restored.
217
Bram Moolenaarce90e362019-09-08 18:58:44 +0200218 Can also be used as a |method|: >
219 GetTime()->test_settime()
220
Bram Moolenaared997ad2019-07-21 16:42:00 +0200221==============================================================================
Bram Moolenaar54775062019-07-31 21:07:14 +02002223. Assert functions *assert-functions-details*
Bram Moolenaared997ad2019-07-21 16:42:00 +0200223
224
225assert_beeps({cmd}) *assert_beeps()*
226 Run {cmd} and add an error message to |v:errors| if it does
227 NOT produce a beep or visual bell.
228 Also see |assert_fails()| and |assert-return|.
229
Bram Moolenaar24278d22019-08-16 21:49:22 +0200230 Can also be used as a |method|: >
231 GetCmd()->assert_beeps()
232<
Bram Moolenaared997ad2019-07-21 16:42:00 +0200233 *assert_equal()*
234assert_equal({expected}, {actual} [, {msg}])
235 When {expected} and {actual} are not equal an error message is
236 added to |v:errors| and 1 is returned. Otherwise zero is
237 returned |assert-return|.
238 There is no automatic conversion, the String "4" is different
239 from the Number 4. And the number 4 is different from the
240 Float 4.0. The value of 'ignorecase' is not used here, case
241 always matters.
242 When {msg} is omitted an error in the form "Expected
243 {expected} but got {actual}" is produced.
244 Example: >
245 assert_equal('foo', 'bar')
246< Will result in a string to be added to |v:errors|:
247 test.vim line 12: Expected 'foo' but got 'bar' ~
248
Bram Moolenaar25e42232019-08-04 15:04:10 +0200249 Can also be used as a |method|: >
250 mylist->assert_equal([1, 2, 3])
251
252
253< *assert_equalfile()*
Bram Moolenaared997ad2019-07-21 16:42:00 +0200254assert_equalfile({fname-one}, {fname-two})
255 When the files {fname-one} and {fname-two} do not contain
256 exactly the same text an error message is added to |v:errors|.
257 Also see |assert-return|.
258 When {fname-one} or {fname-two} does not exist the error will
259 mention that.
260 Mainly useful with |terminal-diff|.
261
Bram Moolenaare49fbff2019-08-21 22:50:07 +0200262 Can also be used as a |method|: >
263 GetLog()->assert_equalfile('expected.log')
264
265
Bram Moolenaared997ad2019-07-21 16:42:00 +0200266assert_exception({error} [, {msg}]) *assert_exception()*
267 When v:exception does not contain the string {error} an error
268 message is added to |v:errors|. Also see |assert-return|.
269 This can be used to assert that a command throws an exception.
270 Using the error number, followed by a colon, avoids problems
271 with translations: >
272 try
273 commandthatfails
274 call assert_false(1, 'command should have failed')
275 catch
276 call assert_exception('E492:')
277 endtry
278
279assert_fails({cmd} [, {error} [, {msg}]]) *assert_fails()*
280 Run {cmd} and add an error message to |v:errors| if it does
281 NOT produce an error. Also see |assert-return|.
282 When {error} is given it must match in |v:errmsg|.
283 Note that beeping is not considered an error, and some failing
284 commands only beep. Use |assert_beeps()| for those.
285
Bram Moolenaar24278d22019-08-16 21:49:22 +0200286 Can also be used as a |method|: >
287 GetCmd()->assert_fails('E99:')
288
Bram Moolenaared997ad2019-07-21 16:42:00 +0200289assert_false({actual} [, {msg}]) *assert_false()*
290 When {actual} is not false an error message is added to
291 |v:errors|, like with |assert_equal()|.
292 Also see |assert-return|.
293 A value is false when it is zero. When {actual} is not a
294 number the assert fails.
295 When {msg} is omitted an error in the form
296 "Expected False but got {actual}" is produced.
297
Bram Moolenaar24278d22019-08-16 21:49:22 +0200298 Can also be used as a |method|: >
299 GetResult()->assert_false()
300
Bram Moolenaared997ad2019-07-21 16:42:00 +0200301assert_inrange({lower}, {upper}, {actual} [, {msg}]) *assert_inrange()*
302 This asserts number and |Float| values. When {actual} is lower
303 than {lower} or higher than {upper} an error message is added
304 to |v:errors|. Also see |assert-return|.
305 When {msg} is omitted an error in the form
306 "Expected range {lower} - {upper}, but got {actual}" is
307 produced.
308
309 *assert_match()*
310assert_match({pattern}, {actual} [, {msg}])
311 When {pattern} does not match {actual} an error message is
312 added to |v:errors|. Also see |assert-return|.
313
314 {pattern} is used as with |=~|: The matching is always done
315 like 'magic' was set and 'cpoptions' is empty, no matter what
316 the actual value of 'magic' or 'cpoptions' is.
317
318 {actual} is used as a string, automatic conversion applies.
319 Use "^" and "$" to match with the start and end of the text.
320 Use both to match the whole text.
321
322 When {msg} is omitted an error in the form
323 "Pattern {pattern} does not match {actual}" is produced.
324 Example: >
325 assert_match('^f.*o$', 'foobar')
326< Will result in a string to be added to |v:errors|:
327 test.vim line 12: Pattern '^f.*o$' does not match 'foobar' ~
328
Bram Moolenaar24278d22019-08-16 21:49:22 +0200329 Can also be used as a |method|: >
330 getFile()->assert_match('foo.*')
331<
Bram Moolenaared997ad2019-07-21 16:42:00 +0200332 *assert_notequal()*
333assert_notequal({expected}, {actual} [, {msg}])
334 The opposite of `assert_equal()`: add an error message to
335 |v:errors| when {expected} and {actual} are equal.
336 Also see |assert-return|.
337
Bram Moolenaar25e42232019-08-04 15:04:10 +0200338 Can also be used as a |method|: >
339 mylist->assert_notequal([1, 2, 3])
340
341< *assert_notmatch()*
Bram Moolenaared997ad2019-07-21 16:42:00 +0200342assert_notmatch({pattern}, {actual} [, {msg}])
343 The opposite of `assert_match()`: add an error message to
344 |v:errors| when {pattern} matches {actual}.
345 Also see |assert-return|.
346
Bram Moolenaar24278d22019-08-16 21:49:22 +0200347 Can also be used as a |method|: >
348 getFile()->assert_notmatch('bar.*')
349
Bram Moolenaare49fbff2019-08-21 22:50:07 +0200350
Bram Moolenaared997ad2019-07-21 16:42:00 +0200351assert_report({msg}) *assert_report()*
352 Report a test failure directly, using {msg}.
353 Always returns one.
354
Bram Moolenaare49fbff2019-08-21 22:50:07 +0200355 Can also be used as a |method|: >
356 GetMessage()->assert_report()
357
358
Bram Moolenaared997ad2019-07-21 16:42:00 +0200359assert_true({actual} [, {msg}]) *assert_true()*
360 When {actual} is not true an error message is added to
361 |v:errors|, like with |assert_equal()|.
362 Also see |assert-return|.
363 A value is TRUE when it is a non-zero number. When {actual}
364 is not a number the assert fails.
365 When {msg} is omitted an error in the form "Expected True but
366 got {actual}" is produced.
367
Bram Moolenaar24278d22019-08-16 21:49:22 +0200368 Can also be used as a |method|: >
369 GetResult()->assert_true()
370<
Bram Moolenaared997ad2019-07-21 16:42:00 +0200371
372 vim:tw=78:ts=8:noet:ft=help:norl: