blob: 8a6af5734d2d8d4313b04ad68a8e99140930c1a1 [file] [log] [blame]
Bram Moolenaar94738d82020-10-21 14:25:07 +02001" Test using builtin functions in the Vim9 script language.
2
3source check.vim
4source vim9.vim
5
6" Test for passing too many or too few arguments to builtin functions
7func Test_internalfunc_arg_error()
8 let l =<< trim END
9 def! FArgErr(): float
10 return ceil(1.1, 2)
11 enddef
12 defcompile
13 END
14 call writefile(l, 'Xinvalidarg')
15 call assert_fails('so Xinvalidarg', 'E118:', '', 1, 'FArgErr')
16 let l =<< trim END
17 def! FArgErr(): float
18 return ceil()
19 enddef
20 defcompile
21 END
22 call writefile(l, 'Xinvalidarg')
23 call assert_fails('so Xinvalidarg', 'E119:', '', 1, 'FArgErr')
24 call delete('Xinvalidarg')
25endfunc
26
27" Test for builtin functions returning different types
28func Test_InternalFuncRetType()
29 let lines =<< trim END
30 def RetFloat(): float
31 return ceil(1.456)
32 enddef
33
34 def RetListAny(): list<any>
Bram Moolenaare0de1712020-12-02 17:36:54 +010035 return items({k: 'v'})
Bram Moolenaar94738d82020-10-21 14:25:07 +020036 enddef
37
38 def RetListString(): list<string>
39 return split('a:b:c', ':')
40 enddef
41
42 def RetListDictAny(): list<dict<any>>
43 return getbufinfo()
44 enddef
45
46 def RetDictNumber(): dict<number>
47 return wordcount()
48 enddef
49
50 def RetDictString(): dict<string>
51 return environ()
52 enddef
53 END
54 call writefile(lines, 'Xscript')
55 source Xscript
56
57 call RetFloat()->assert_equal(2.0)
58 call RetListAny()->assert_equal([['k', 'v']])
59 call RetListString()->assert_equal(['a', 'b', 'c'])
60 call RetListDictAny()->assert_notequal([])
61 call RetDictNumber()->assert_notequal({})
62 call RetDictString()->assert_notequal({})
63 call delete('Xscript')
64endfunc
65
66def Test_abs()
67 assert_equal(0, abs(0))
68 assert_equal(2, abs(-2))
69 assert_equal(3, abs(3))
70 CheckDefFailure(['abs("text")'], 'E1013: Argument 1: type mismatch, expected number but got string', 1)
71 if has('float')
72 assert_equal(0, abs(0))
73 assert_equal(2.0, abs(-2.0))
74 assert_equal(3.0, abs(3.0))
75 endif
76enddef
77
78def Test_add_list()
79 var l: list<number> # defaults to empty list
80 add(l, 9)
81 assert_equal([9], l)
82
83 var lines =<< trim END
84 var l: list<number>
85 add(l, "x")
86 END
87 CheckDefFailure(lines, 'E1012:', 2)
88
89 lines =<< trim END
Bram Moolenaarb7c21af2021-04-18 14:12:31 +020090 add(test_null_list(), 123)
91 END
92 CheckDefExecAndScriptFailure(lines, 'E1130:', 1)
93
94 lines =<< trim END
Bram Moolenaar94738d82020-10-21 14:25:07 +020095 var l: list<number> = test_null_list()
96 add(l, 123)
97 END
98 CheckDefExecFailure(lines, 'E1130:', 2)
Bram Moolenaarb7c21af2021-04-18 14:12:31 +020099
100 # Getting variable with NULL list allocates a new list at script level
101 lines =<< trim END
102 vim9script
103 var l: list<number> = test_null_list()
104 add(l, 123)
105 END
106 CheckScriptSuccess(lines)
Bram Moolenaarf32f0992021-07-08 20:53:40 +0200107
108 lines =<< trim END
109 vim9script
110 var l: list<string> = ['a']
111 l->add(123)
112 END
113 CheckScriptFailure(lines, 'E1012: Type mismatch; expected string but got number', 3)
Bram Moolenaar94738d82020-10-21 14:25:07 +0200114enddef
115
116def Test_add_blob()
117 var b1: blob = 0z12
118 add(b1, 0x34)
119 assert_equal(0z1234, b1)
120
121 var b2: blob # defaults to empty blob
122 add(b2, 0x67)
123 assert_equal(0z67, b2)
124
125 var lines =<< trim END
126 var b: blob
127 add(b, "x")
128 END
129 CheckDefFailure(lines, 'E1012:', 2)
130
131 lines =<< trim END
Bram Moolenaarb7c21af2021-04-18 14:12:31 +0200132 add(test_null_blob(), 123)
133 END
134 CheckDefExecAndScriptFailure(lines, 'E1131:', 1)
135
136 lines =<< trim END
Bram Moolenaar94738d82020-10-21 14:25:07 +0200137 var b: blob = test_null_blob()
138 add(b, 123)
139 END
140 CheckDefExecFailure(lines, 'E1131:', 2)
Bram Moolenaarb7c21af2021-04-18 14:12:31 +0200141
142 # Getting variable with NULL blob allocates a new blob at script level
143 lines =<< trim END
144 vim9script
145 var b: blob = test_null_blob()
146 add(b, 123)
147 END
148 CheckScriptSuccess(lines)
Bram Moolenaar94738d82020-10-21 14:25:07 +0200149enddef
150
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +0200151def Test_and()
152 CheckDefFailure(['echo and("x", 0x2)'], 'E1013: Argument 1: type mismatch, expected number but got string')
153 CheckDefFailure(['echo and(0x1, "x")'], 'E1013: Argument 2: type mismatch, expected number but got string')
154enddef
155
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100156def Test_append()
157 new
158 setline(1, range(3))
159 var res1: number = append(1, 'one')
160 assert_equal(0, res1)
161 var res2: bool = append(3, 'two')
162 assert_equal(false, res2)
163 assert_equal(['0', 'one', '1', 'two', '2'], getline(1, 6))
Bram Moolenaarb2ac7d02021-03-28 15:46:16 +0200164
165 append(0, 'zero')
166 assert_equal('zero', getline(1))
167 bwipe!
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100168enddef
169
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +0200170def Test_argc()
171 CheckDefFailure(['echo argc("x")'], 'E1013: Argument 1: type mismatch, expected number but got string')
172enddef
173
174def Test_arglistid()
175 CheckDefFailure(['echo arglistid("x")'], 'E1013: Argument 1: type mismatch, expected number but got string')
176 CheckDefFailure(['echo arglistid(1, "y")'], 'E1013: Argument 2: type mismatch, expected number but got string')
177 CheckDefFailure(['echo arglistid("x", "y")'], 'E1013: Argument 1: type mismatch, expected number but got string')
178enddef
179
180def Test_argv()
181 CheckDefFailure(['echo argv("x")'], 'E1013: Argument 1: type mismatch, expected number but got string')
182 CheckDefFailure(['echo argv(1, "x")'], 'E1013: Argument 2: type mismatch, expected number but got string')
183 CheckDefFailure(['echo argv("x", "y")'], 'E1013: Argument 1: type mismatch, expected number but got string')
184enddef
185
Bram Moolenaar32105ae2021-03-27 18:59:25 +0100186def Test_balloon_show()
187 CheckGui
188 CheckFeature balloon_eval
189
Yegappan Lakshmanana26f56f2021-07-03 11:58:12 +0200190 assert_fails('balloon_show(10)', 'E1174:')
Bram Moolenaar32105ae2021-03-27 18:59:25 +0100191 assert_fails('balloon_show(true)', 'E1174:')
192enddef
193
194def Test_balloon_split()
Bram Moolenaar7b45d462021-03-27 19:09:02 +0100195 CheckFeature balloon_eval_term
Bram Moolenaar32105ae2021-03-27 18:59:25 +0100196
Yegappan Lakshmanana26f56f2021-07-03 11:58:12 +0200197 assert_fails('balloon_split([])', 'E1174:')
Bram Moolenaar32105ae2021-03-27 18:59:25 +0100198 assert_fails('balloon_split(true)', 'E1174:')
199enddef
200
Bram Moolenaarf28f2ac2021-03-22 22:21:26 +0100201def Test_browse()
202 CheckFeature browse
203
204 var lines =<< trim END
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +0100205 browse(1, 2, 3, 4)
Bram Moolenaarf28f2ac2021-03-22 22:21:26 +0100206 END
207 CheckDefExecAndScriptFailure(lines, 'E1174: String required for argument 2')
208 lines =<< trim END
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +0100209 browse(1, 'title', 3, 4)
Bram Moolenaarf28f2ac2021-03-22 22:21:26 +0100210 END
211 CheckDefExecAndScriptFailure(lines, 'E1174: String required for argument 3')
212 lines =<< trim END
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +0100213 browse(1, 'title', 'dir', 4)
Bram Moolenaarf28f2ac2021-03-22 22:21:26 +0100214 END
215 CheckDefExecAndScriptFailure(lines, 'E1174: String required for argument 4')
216enddef
217
Yegappan Lakshmanana26f56f2021-07-03 11:58:12 +0200218def Test_bufadd()
219 assert_fails('bufadd([])', 'E730:')
220enddef
221
Bram Moolenaar32105ae2021-03-27 18:59:25 +0100222def Test_bufexists()
Yegappan Lakshmanana26f56f2021-07-03 11:58:12 +0200223 assert_fails('bufexists(true)', 'E1174:')
Bram Moolenaar32105ae2021-03-27 18:59:25 +0100224enddef
225
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100226def Test_buflisted()
227 var res: bool = buflisted('asdf')
228 assert_equal(false, res)
Yegappan Lakshmanana26f56f2021-07-03 11:58:12 +0200229 assert_fails('buflisted(true)', 'E1174:')
230 assert_fails('buflisted([])', 'E1174:')
231enddef
232
233def Test_bufload()
234 assert_fails('bufload([])', 'E730:')
235enddef
236
237def Test_bufloaded()
238 assert_fails('bufloaded(true)', 'E1174:')
239 assert_fails('bufloaded([])', 'E1174:')
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100240enddef
241
Bram Moolenaar94738d82020-10-21 14:25:07 +0200242def Test_bufname()
243 split SomeFile
244 bufname('%')->assert_equal('SomeFile')
245 edit OtherFile
246 bufname('#')->assert_equal('SomeFile')
247 close
Yegappan Lakshmanana26f56f2021-07-03 11:58:12 +0200248 assert_fails('bufname(true)', 'E1138:')
249 assert_fails('bufname([])', 'E745:')
Bram Moolenaar94738d82020-10-21 14:25:07 +0200250enddef
251
252def Test_bufnr()
253 var buf = bufnr()
254 bufnr('%')->assert_equal(buf)
255
256 buf = bufnr('Xdummy', true)
257 buf->assert_notequal(-1)
258 exe 'bwipe! ' .. buf
259enddef
260
261def Test_bufwinid()
262 var origwin = win_getid()
263 below split SomeFile
264 var SomeFileID = win_getid()
265 below split OtherFile
266 below split SomeFile
267 bufwinid('SomeFile')->assert_equal(SomeFileID)
268
269 win_gotoid(origwin)
270 only
271 bwipe SomeFile
272 bwipe OtherFile
Bram Moolenaar32105ae2021-03-27 18:59:25 +0100273
Yegappan Lakshmanana26f56f2021-07-03 11:58:12 +0200274 assert_fails('bufwinid(true)', 'E1138:')
275 assert_fails('bufwinid([])', 'E745:')
276enddef
277
278def Test_bufwinnr()
279 assert_fails('bufwinnr(true)', 'E1138:')
280 assert_fails('bufwinnr([])', 'E745:')
281enddef
282
283def Test_byte2line()
284 CheckDefFailure(['byte2line("1")'], 'E1013: Argument 1: type mismatch, expected number but got string')
285 CheckDefFailure(['byte2line([])'], 'E1013: Argument 1: type mismatch, expected number but got list<unknown>')
286 assert_equal(-1, byte2line(0))
Bram Moolenaar94738d82020-10-21 14:25:07 +0200287enddef
288
289def Test_call_call()
290 var l = [3, 2, 1]
291 call('reverse', [l])
292 l->assert_equal([1, 2, 3])
293enddef
294
Bram Moolenaarc5809432021-03-27 21:23:30 +0100295def Test_ch_logfile()
Bram Moolenaar886e5e72021-04-05 13:36:34 +0200296 if !has('channel')
297 CheckFeature channel
298 endif
Yegappan Lakshmanana26f56f2021-07-03 11:58:12 +0200299 assert_fails('ch_logfile(true)', 'E1174:')
300 assert_fails('ch_logfile("foo", true)', 'E1174:')
Bram Moolenaarc5809432021-03-27 21:23:30 +0100301enddef
302
Bram Moolenaar94738d82020-10-21 14:25:07 +0200303def Test_char2nr()
304 char2nr('あ', true)->assert_equal(12354)
Bram Moolenaarc5809432021-03-27 21:23:30 +0100305
Yegappan Lakshmanana26f56f2021-07-03 11:58:12 +0200306 assert_fails('char2nr(true)', 'E1174:')
Bram Moolenaarc5809432021-03-27 21:23:30 +0100307enddef
308
309def Test_charclass()
Yegappan Lakshmanana26f56f2021-07-03 11:58:12 +0200310 assert_fails('charclass(true)', 'E1174:')
Bram Moolenaarc5809432021-03-27 21:23:30 +0100311enddef
312
313def Test_chdir()
Yegappan Lakshmanana26f56f2021-07-03 11:58:12 +0200314 assert_fails('chdir(true)', 'E1174:')
315enddef
316
317def Test_cindent()
318 CheckDefFailure(['cindent([])'], 'E1013: Argument 1: type mismatch, expected string but got list<unknown>')
319 CheckDefFailure(['cindent(null)'], 'E1013: Argument 1: type mismatch, expected string but got special')
320 assert_equal(-1, cindent(0))
321 assert_equal(0, cindent('.'))
Bram Moolenaar94738d82020-10-21 14:25:07 +0200322enddef
323
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +0200324def Test_clearmatches()
325 CheckDefFailure(['echo clearmatches("x")'], 'E1013: Argument 1: type mismatch, expected number but got string')
326enddef
327
Bram Moolenaar94738d82020-10-21 14:25:07 +0200328def Test_col()
329 new
330 setline(1, 'asdf')
331 col([1, '$'])->assert_equal(5)
Bram Moolenaarc5809432021-03-27 21:23:30 +0100332
Yegappan Lakshmanana26f56f2021-07-03 11:58:12 +0200333 assert_fails('col(true)', 'E1174:')
Bram Moolenaarc5809432021-03-27 21:23:30 +0100334enddef
335
336def Test_confirm()
337 if !has('dialog_con') && !has('dialog_gui')
338 CheckFeature dialog_con
339 endif
340
Yegappan Lakshmanana26f56f2021-07-03 11:58:12 +0200341 assert_fails('confirm(true)', 'E1174:')
342 assert_fails('confirm("yes", true)', 'E1174:')
343 assert_fails('confirm("yes", "maybe", 2, true)', 'E1174:')
344enddef
345
346def Test_complete_info()
347 CheckDefFailure(['complete_info("")'], 'E1013: Argument 1: type mismatch, expected list<string> but got string')
348 CheckDefFailure(['complete_info({})'], 'E1013: Argument 1: type mismatch, expected list<string> but got dict<unknown>')
349 assert_equal({'pum_visible': 0, 'mode': '', 'selected': -1, 'items': []}, complete_info())
350 assert_equal({'mode': '', 'items': []}, complete_info(['mode', 'items']))
Bram Moolenaar94738d82020-10-21 14:25:07 +0200351enddef
352
353def Test_copy_return_type()
354 var l = copy([1, 2, 3])
355 var res = 0
356 for n in l
357 res += n
358 endfor
359 res->assert_equal(6)
360
361 var dl = deepcopy([1, 2, 3])
362 res = 0
363 for n in dl
364 res += n
365 endfor
366 res->assert_equal(6)
367
368 dl = deepcopy([1, 2, 3], true)
369enddef
370
371def Test_count()
372 count('ABC ABC ABC', 'b', true)->assert_equal(3)
373 count('ABC ABC ABC', 'b', false)->assert_equal(0)
374enddef
375
Bram Moolenaar9a963372020-12-21 21:58:46 +0100376def Test_cursor()
377 new
378 setline(1, range(4))
379 cursor(2, 1)
380 assert_equal(2, getcurpos()[1])
381 cursor('$', 1)
382 assert_equal(4, getcurpos()[1])
383
384 var lines =<< trim END
385 cursor('2', 1)
386 END
387 CheckDefExecAndScriptFailure(lines, 'E475:')
388enddef
389
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +0200390def Test_debugbreak()
391 CheckMSWindows
392 CheckDefFailure(['echo debugbreak("x")'], 'E1013: Argument 1: type mismatch, expected number but got string')
393enddef
394
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100395def Test_delete()
396 var res: bool = delete('doesnotexist')
397 assert_equal(true, res)
398enddef
399
Yegappan Lakshmanana26f56f2021-07-03 11:58:12 +0200400def Test_diff_filler()
401 CheckDefFailure(['diff_filler([])'], 'E1013: Argument 1: type mismatch, expected string but got list<unknown>')
402 CheckDefFailure(['diff_filler(true)'], 'E1013: Argument 1: type mismatch, expected string but got bool')
403 assert_equal(0, diff_filler(1))
404 assert_equal(0, diff_filler('.'))
405enddef
406
407def Test_escape()
408 CheckDefFailure(['escape("a", 10)'], 'E1013: Argument 2: type mismatch, expected string but got number')
409 CheckDefFailure(['escape(10, " ")'], 'E1013: Argument 1: type mismatch, expected string but got number')
410 CheckDefFailure(['escape(true, false)'], 'E1013: Argument 1: type mismatch, expected string but got bool')
411 assert_equal('a\:b', escape("a:b", ":"))
412enddef
413
414def Test_eval()
415 CheckDefFailure(['eval(10)'], 'E1013: Argument 1: type mismatch, expected string but got number')
416 CheckDefFailure(['eval(null)'], 'E1013: Argument 1: type mismatch, expected string but got special')
417 assert_equal(2, eval('1 + 1'))
418enddef
419
Bram Moolenaar7bb4e742020-12-09 12:41:50 +0100420def Test_executable()
Bram Moolenaar2a9d5d32020-12-12 18:58:40 +0100421 assert_false(executable(""))
422 assert_false(executable(test_null_string()))
423
Yegappan Lakshmanana26f56f2021-07-03 11:58:12 +0200424 CheckDefExecFailure(['echo executable(123)'], 'E1013:')
425 CheckDefExecFailure(['echo executable(true)'], 'E1013:')
Bram Moolenaar7bb4e742020-12-09 12:41:50 +0100426enddef
427
Bram Moolenaarca81f0e2021-06-20 14:41:01 +0200428def Test_execute()
429 var res = execute("echo 'hello'")
430 assert_equal("\nhello", res)
431 res = execute(["echo 'here'", "echo 'there'"])
432 assert_equal("\nhere\nthere", res)
433
434 CheckDefFailure(['echo execute(123)'], 'E1013: Argument 1: type mismatch, expected string but got number')
435 CheckDefFailure(['echo execute([123])'], 'E1013: Argument 1: type mismatch, expected list<string> but got list<number>')
436 CheckDefExecFailure(['echo execute(["xx", 123])'], 'E492')
437 CheckDefFailure(['echo execute("xx", 123)'], 'E1013: Argument 2: type mismatch, expected string but got number')
438enddef
439
Bram Moolenaar7bb4e742020-12-09 12:41:50 +0100440def Test_exepath()
Yegappan Lakshmanana26f56f2021-07-03 11:58:12 +0200441 CheckDefExecFailure(['echo exepath(true)'], 'E1013:')
442 CheckDefExecFailure(['echo exepath(v:null)'], 'E1013:')
Bram Moolenaarfa984412021-03-25 22:15:28 +0100443 CheckDefExecFailure(['echo exepath("")'], 'E1175:')
Bram Moolenaar7bb4e742020-12-09 12:41:50 +0100444enddef
445
Yegappan Lakshmanana26f56f2021-07-03 11:58:12 +0200446def Test_exists()
447 CheckDefFailure(['exists(10)'], 'E1013: Argument 1: type mismatch, expected string but got number')
448 call assert_equal(1, exists('&tabstop'))
449enddef
450
Bram Moolenaar94738d82020-10-21 14:25:07 +0200451def Test_expand()
452 split SomeFile
453 expand('%', true, true)->assert_equal(['SomeFile'])
454 close
455enddef
456
Bram Moolenaar02795102021-05-03 21:40:26 +0200457def Test_expandcmd()
458 $FOO = "blue"
459 assert_equal("blue sky", expandcmd("`=$FOO .. ' sky'`"))
460
461 assert_equal("yes", expandcmd("`={a: 'yes'}['a']`"))
462enddef
463
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100464def Test_extend_arg_types()
Bram Moolenaarc03f5c62021-01-31 17:48:30 +0100465 g:number_one = 1
466 g:string_keep = 'keep'
467 var lines =<< trim END
468 assert_equal([1, 2, 3], extend([1, 2], [3]))
469 assert_equal([3, 1, 2], extend([1, 2], [3], 0))
470 assert_equal([1, 3, 2], extend([1, 2], [3], 1))
471 assert_equal([1, 3, 2], extend([1, 2], [3], g:number_one))
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100472
Bram Moolenaarc03f5c62021-01-31 17:48:30 +0100473 assert_equal({a: 1, b: 2, c: 3}, extend({a: 1, b: 2}, {c: 3}))
474 assert_equal({a: 1, b: 4}, extend({a: 1, b: 2}, {b: 4}))
475 assert_equal({a: 1, b: 2}, extend({a: 1, b: 2}, {b: 4}, 'keep'))
476 assert_equal({a: 1, b: 2}, extend({a: 1, b: 2}, {b: 4}, g:string_keep))
Bram Moolenaar193f6202020-11-16 20:08:35 +0100477
Bram Moolenaarc03f5c62021-01-31 17:48:30 +0100478 var res: list<dict<any>>
479 extend(res, mapnew([1, 2], (_, v) => ({})))
480 assert_equal([{}, {}], res)
481 END
482 CheckDefAndScriptSuccess(lines)
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100483
Yegappan Lakshmanan34fcb692021-05-25 20:14:00 +0200484 CheckDefFailure(['extend("a", 1)'], 'E1013: Argument 1: type mismatch, expected list<any> but got string')
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100485 CheckDefFailure(['extend([1, 2], 3)'], 'E1013: Argument 2: type mismatch, expected list<number> but got number')
486 CheckDefFailure(['extend([1, 2], ["x"])'], 'E1013: Argument 2: type mismatch, expected list<number> but got list<string>')
487 CheckDefFailure(['extend([1, 2], [3], "x")'], 'E1013: Argument 3: type mismatch, expected number but got string')
488
Bram Moolenaare0de1712020-12-02 17:36:54 +0100489 CheckDefFailure(['extend({a: 1}, 42)'], 'E1013: Argument 2: type mismatch, expected dict<number> but got number')
490 CheckDefFailure(['extend({a: 1}, {b: "x"})'], 'E1013: Argument 2: type mismatch, expected dict<number> but got dict<string>')
491 CheckDefFailure(['extend({a: 1}, {b: 2}, 1)'], 'E1013: Argument 3: type mismatch, expected string but got number')
Bram Moolenaar351ead02021-01-16 16:07:01 +0100492
493 CheckDefFailure(['extend([1], ["b"])'], 'E1013: Argument 2: type mismatch, expected list<number> but got list<string>')
Bram Moolenaare32e5162021-01-21 20:21:29 +0100494 CheckDefExecFailure(['extend([1], ["b", 1])'], 'E1013: Argument 2: type mismatch, expected list<number> but got list<any>')
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100495enddef
496
Bram Moolenaarb0e6b512021-01-12 20:23:40 +0100497def Test_extendnew()
498 assert_equal([1, 2, 'a'], extendnew([1, 2], ['a']))
499 assert_equal({one: 1, two: 'a'}, extendnew({one: 1}, {two: 'a'}))
500
501 CheckDefFailure(['extendnew({a: 1}, 42)'], 'E1013: Argument 2: type mismatch, expected dict<number> but got number')
502 CheckDefFailure(['extendnew({a: 1}, [42])'], 'E1013: Argument 2: type mismatch, expected dict<number> but got list<number>')
503 CheckDefFailure(['extendnew([1, 2], "x")'], 'E1013: Argument 2: type mismatch, expected list<number> but got string')
504 CheckDefFailure(['extendnew([1, 2], {x: 1})'], 'E1013: Argument 2: type mismatch, expected list<number> but got dict<number>')
505enddef
506
Bram Moolenaar94738d82020-10-21 14:25:07 +0200507def Test_extend_return_type()
508 var l = extend([1, 2], [3])
509 var res = 0
510 for n in l
511 res += n
512 endfor
513 res->assert_equal(6)
514enddef
515
Bram Moolenaaraa210a32021-01-02 15:41:03 +0100516func g:ExtendDict(d)
517 call extend(a:d, #{xx: 'x'})
518endfunc
519
520def Test_extend_dict_item_type()
521 var lines =<< trim END
522 var d: dict<number> = {a: 1}
523 extend(d, {b: 2})
524 END
525 CheckDefAndScriptSuccess(lines)
526
527 lines =<< trim END
528 var d: dict<number> = {a: 1}
529 extend(d, {b: 'x'})
530 END
Bram Moolenaarc03f5c62021-01-31 17:48:30 +0100531 CheckDefAndScriptFailure(lines, 'E1013: Argument 2: type mismatch, expected dict<number> but got dict<string>', 2)
Bram Moolenaaraa210a32021-01-02 15:41:03 +0100532
533 lines =<< trim END
534 var d: dict<number> = {a: 1}
535 g:ExtendDict(d)
536 END
537 CheckDefExecFailure(lines, 'E1012: Type mismatch; expected number but got string', 0)
538 CheckScriptFailure(['vim9script'] + lines, 'E1012:', 1)
539enddef
540
541func g:ExtendList(l)
542 call extend(a:l, ['x'])
543endfunc
544
545def Test_extend_list_item_type()
546 var lines =<< trim END
547 var l: list<number> = [1]
548 extend(l, [2])
549 END
550 CheckDefAndScriptSuccess(lines)
551
552 lines =<< trim END
553 var l: list<number> = [1]
554 extend(l, ['x'])
555 END
Bram Moolenaarc03f5c62021-01-31 17:48:30 +0100556 CheckDefAndScriptFailure(lines, 'E1013: Argument 2: type mismatch, expected list<number> but got list<string>', 2)
Bram Moolenaaraa210a32021-01-02 15:41:03 +0100557
558 lines =<< trim END
559 var l: list<number> = [1]
560 g:ExtendList(l)
561 END
562 CheckDefExecFailure(lines, 'E1012: Type mismatch; expected number but got string', 0)
563 CheckScriptFailure(['vim9script'] + lines, 'E1012:', 1)
564enddef
Bram Moolenaar94738d82020-10-21 14:25:07 +0200565
Bram Moolenaar93e1cae2021-03-13 21:24:56 +0100566def Test_extend_with_error_function()
567 var lines =<< trim END
568 vim9script
569 def F()
570 {
571 var m = 10
572 }
573 echo m
574 enddef
575
576 def Test()
577 var d: dict<any> = {}
578 d->extend({A: 10, Func: function('F', [])})
579 enddef
580
581 Test()
582 END
583 CheckScriptFailure(lines, 'E1001: Variable not found: m')
584enddef
585
Yegappan Lakshmanana26f56f2021-07-03 11:58:12 +0200586def Test_feedkeys()
587 CheckDefFailure(['feedkeys(10)'], 'E1013: Argument 1: type mismatch, expected string but got number')
588 CheckDefFailure(['feedkeys("x", 10)'], 'E1013: Argument 2: type mismatch, expected string but got number')
589 CheckDefFailure(['feedkeys([], {})'], 'E1013: Argument 1: type mismatch, expected string but got list<unknown>')
590 g:TestVar = 1
591 feedkeys(":g:TestVar = 789\n", 'xt')
592 assert_equal(789, g:TestVar)
593 unlet g:TestVar
594enddef
595
Bram Moolenaar64ed4d42021-01-12 21:22:31 +0100596def Test_job_info_return_type()
597 if has('job')
598 job_start(&shell)
599 var jobs = job_info()
Bram Moolenaara47e05f2021-01-12 21:49:00 +0100600 assert_equal('list<job>', typename(jobs))
601 assert_equal('dict<any>', typename(job_info(jobs[0])))
Bram Moolenaar64ed4d42021-01-12 21:22:31 +0100602 job_stop(jobs[0])
603 endif
604enddef
605
Bram Moolenaar7bb4e742020-12-09 12:41:50 +0100606def Test_filereadable()
Bram Moolenaar2a9d5d32020-12-12 18:58:40 +0100607 assert_false(filereadable(""))
608 assert_false(filereadable(test_null_string()))
609
Yegappan Lakshmanana26f56f2021-07-03 11:58:12 +0200610 CheckDefExecFailure(['echo filereadable(123)'], 'E1013:')
611 CheckDefExecFailure(['echo filereadable(true)'], 'E1013:')
Bram Moolenaar7bb4e742020-12-09 12:41:50 +0100612enddef
613
614def Test_filewritable()
Bram Moolenaar2a9d5d32020-12-12 18:58:40 +0100615 assert_false(filewritable(""))
616 assert_false(filewritable(test_null_string()))
617
Yegappan Lakshmanana26f56f2021-07-03 11:58:12 +0200618 CheckDefExecFailure(['echo filewritable(123)'], 'E1013:')
619 CheckDefExecFailure(['echo filewritable(true)'], 'E1013:')
Bram Moolenaar7bb4e742020-12-09 12:41:50 +0100620enddef
621
622def Test_finddir()
Bram Moolenaarf28f2ac2021-03-22 22:21:26 +0100623 CheckDefExecFailure(['echo finddir(true)'], 'E1174:')
624 CheckDefExecFailure(['echo finddir(v:null)'], 'E1174:')
Bram Moolenaarfa984412021-03-25 22:15:28 +0100625 CheckDefExecFailure(['echo finddir("")'], 'E1175:')
Bram Moolenaar7bb4e742020-12-09 12:41:50 +0100626enddef
627
628def Test_findfile()
Bram Moolenaarf28f2ac2021-03-22 22:21:26 +0100629 CheckDefExecFailure(['echo findfile(true)'], 'E1174:')
630 CheckDefExecFailure(['echo findfile(v:null)'], 'E1174:')
Bram Moolenaarfa984412021-03-25 22:15:28 +0100631 CheckDefExecFailure(['echo findfile("")'], 'E1175:')
Bram Moolenaar7bb4e742020-12-09 12:41:50 +0100632enddef
633
Bram Moolenaar3b690062021-02-01 20:14:51 +0100634def Test_flattennew()
635 var lines =<< trim END
636 var l = [1, [2, [3, 4]], 5]
637 call assert_equal([1, 2, 3, 4, 5], flattennew(l))
638 call assert_equal([1, [2, [3, 4]], 5], l)
639
640 call assert_equal([1, 2, [3, 4], 5], flattennew(l, 1))
641 call assert_equal([1, [2, [3, 4]], 5], l)
642 END
643 CheckDefAndScriptSuccess(lines)
644
645 lines =<< trim END
646 echo flatten([1, 2, 3])
647 END
648 CheckDefAndScriptFailure(lines, 'E1158:')
649enddef
650
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +0200651" Test for float functions argument type
652def Test_float_funcs_args()
653 CheckFeature float
654
655 # acos()
656 CheckDefFailure(['echo acos("a")'], 'E1013: Argument 1: type mismatch, expected number but got string')
657 # asin()
658 CheckDefFailure(['echo asin("a")'], 'E1013: Argument 1: type mismatch, expected number but got string')
659 # atan()
660 CheckDefFailure(['echo atan("a")'], 'E1013: Argument 1: type mismatch, expected number but got string')
661 # atan2()
662 CheckDefFailure(['echo atan2("a", 1.1)'], 'E1013: Argument 1: type mismatch, expected number but got string')
663 CheckDefFailure(['echo atan2(1.2, "a")'], 'E1013: Argument 2: type mismatch, expected number but got string')
664 CheckDefFailure(['echo atan2(1.2)'], 'E119:')
665 # ceil()
666 CheckDefFailure(['echo ceil("a")'], 'E1013: Argument 1: type mismatch, expected number but got string')
667 # cos()
668 CheckDefFailure(['echo cos("a")'], 'E1013: Argument 1: type mismatch, expected number but got string')
669 # cosh()
670 CheckDefFailure(['echo cosh("a")'], 'E1013: Argument 1: type mismatch, expected number but got string')
671 # exp()
672 CheckDefFailure(['echo exp("a")'], 'E1013: Argument 1: type mismatch, expected number but got string')
673 # float2nr()
674 CheckDefFailure(['echo float2nr("a")'], 'E1013: Argument 1: type mismatch, expected number but got string')
675 # floor()
676 CheckDefFailure(['echo floor("a")'], 'E1013: Argument 1: type mismatch, expected number but got string')
677 # fmod()
678 CheckDefFailure(['echo fmod(1.1, "a")'], 'E1013: Argument 2: type mismatch, expected number but got string')
679 CheckDefFailure(['echo fmod("a", 1.1)'], 'E1013: Argument 1: type mismatch, expected number but got string')
680 CheckDefFailure(['echo fmod(1.1)'], 'E119:')
681 # isinf()
682 CheckDefFailure(['echo isinf("a")'], 'E1013: Argument 1: type mismatch, expected number but got string')
683 # isnan()
684 CheckDefFailure(['echo isnan("a")'], 'E1013: Argument 1: type mismatch, expected number but got string')
685 # log()
686 CheckDefFailure(['echo log("a")'], 'E1013: Argument 1: type mismatch, expected number but got string')
687 # log10()
688 CheckDefFailure(['echo log10("a")'], 'E1013: Argument 1: type mismatch, expected number but got string')
689 # pow()
690 CheckDefFailure(['echo pow("a", 1.1)'], 'E1013: Argument 1: type mismatch, expected number but got string')
691 CheckDefFailure(['echo pow(1.1, "a")'], 'E1013: Argument 2: type mismatch, expected number but got string')
692 CheckDefFailure(['echo pow(1.1)'], 'E119:')
693 # round()
694 CheckDefFailure(['echo round("a")'], 'E1013: Argument 1: type mismatch, expected number but got string')
695 # sin()
696 CheckDefFailure(['echo sin("a")'], 'E1013: Argument 1: type mismatch, expected number but got string')
697 # sinh()
698 CheckDefFailure(['echo sinh("a")'], 'E1013: Argument 1: type mismatch, expected number but got string')
699 # sqrt()
700 CheckDefFailure(['echo sqrt("a")'], 'E1013: Argument 1: type mismatch, expected number but got string')
701 # tan()
702 CheckDefFailure(['echo tan("a")'], 'E1013: Argument 1: type mismatch, expected number but got string')
703 # tanh()
704 CheckDefFailure(['echo tanh("a")'], 'E1013: Argument 1: type mismatch, expected number but got string')
705 # trunc()
706 CheckDefFailure(['echo trunc("a")'], 'E1013: Argument 1: type mismatch, expected number but got string')
707enddef
708
Yegappan Lakshmanana26f56f2021-07-03 11:58:12 +0200709def Test_fnameescape()
710 CheckDefFailure(['fnameescape(10)'], 'E1013: Argument 1: type mismatch, expected string but got number')
711 assert_equal('\+a\%b\|', fnameescape('+a%b|'))
712enddef
713
Bram Moolenaar7bb4e742020-12-09 12:41:50 +0100714def Test_fnamemodify()
Bram Moolenaar2a9d5d32020-12-12 18:58:40 +0100715 CheckDefSuccess(['echo fnamemodify(test_null_string(), ":p")'])
716 CheckDefSuccess(['echo fnamemodify("", ":p")'])
717 CheckDefSuccess(['echo fnamemodify("file", test_null_string())'])
718 CheckDefSuccess(['echo fnamemodify("file", "")'])
719
Yegappan Lakshmanana26f56f2021-07-03 11:58:12 +0200720 CheckDefExecFailure(['echo fnamemodify(true, ":p")'], 'E1013: Argument 1: type mismatch, expected string but got bool')
721 CheckDefExecFailure(['echo fnamemodify(v:null, ":p")'], 'E1013: Argument 1: type mismatch, expected string but got special')
722 CheckDefExecFailure(['echo fnamemodify("file", true)'], 'E1013: Argument 2: type mismatch, expected string but got bool')
Bram Moolenaar7bb4e742020-12-09 12:41:50 +0100723enddef
724
Bram Moolenaar2e5910b2021-02-03 17:41:24 +0100725def Wrong_dict_key_type(items: list<number>): list<number>
726 return filter(items, (_, val) => get({[val]: 1}, 'x'))
727enddef
728
Bram Moolenaar94738d82020-10-21 14:25:07 +0200729def Test_filter_wrong_dict_key_type()
Bram Moolenaar2e5910b2021-02-03 17:41:24 +0100730 assert_fails('Wrong_dict_key_type([1, v:null, 3])', 'E1013:')
Bram Moolenaar94738d82020-10-21 14:25:07 +0200731enddef
732
733def Test_filter_return_type()
Bram Moolenaarbb8a7ce2021-04-10 20:10:26 +0200734 var l = filter([1, 2, 3], (_, _) => 1)
Bram Moolenaar94738d82020-10-21 14:25:07 +0200735 var res = 0
736 for n in l
737 res += n
738 endfor
739 res->assert_equal(6)
740enddef
741
Bram Moolenaarfc0e8f52020-12-25 20:24:51 +0100742def Test_filter_missing_argument()
743 var dict = {aa: [1], ab: [2], ac: [3], de: [4]}
Bram Moolenaarbb8a7ce2021-04-10 20:10:26 +0200744 var res = dict->filter((k, _) => k =~ 'a' && k !~ 'b')
Bram Moolenaarfc0e8f52020-12-25 20:24:51 +0100745 res->assert_equal({aa: [1], ac: [3]})
746enddef
Bram Moolenaar94738d82020-10-21 14:25:07 +0200747
Yegappan Lakshmanana26f56f2021-07-03 11:58:12 +0200748def Test_foldclosed()
749 CheckDefFailure(['foldclosed(function("min"))'], 'E1013: Argument 1: type mismatch, expected string but got func(...): any')
750 assert_equal(-1, foldclosed(1))
751 assert_equal(-1, foldclosed('$'))
752enddef
753
754def Test_foldclosedend()
755 CheckDefFailure(['foldclosedend(true)'], 'E1013: Argument 1: type mismatch, expected string but got bool')
756 assert_equal(-1, foldclosedend(1))
757 assert_equal(-1, foldclosedend('w0'))
758enddef
759
760def Test_foldlevel()
761 CheckDefFailure(['foldlevel(0z10)'], 'E1013: Argument 1: type mismatch, expected string but got blob')
762 assert_equal(0, foldlevel(1))
763 assert_equal(0, foldlevel('.'))
764enddef
765
766def Test_foldtextresult()
767 CheckDefFailure(['foldtextresult(1.1)'], 'E1013: Argument 1: type mismatch, expected string but got float')
768 assert_equal('', foldtextresult(1))
769 assert_equal('', foldtextresult('.'))
770enddef
771
Bram Moolenaar7d840e92021-05-26 21:10:11 +0200772def Test_fullcommand()
773 assert_equal('next', fullcommand('n'))
774 assert_equal('noremap', fullcommand('no'))
775 assert_equal('noremap', fullcommand('nor'))
776 assert_equal('normal', fullcommand('norm'))
777
778 assert_equal('', fullcommand('k'))
779 assert_equal('keepmarks', fullcommand('ke'))
780 assert_equal('keepmarks', fullcommand('kee'))
781 assert_equal('keepmarks', fullcommand('keep'))
782 assert_equal('keepjumps', fullcommand('keepj'))
783
784 assert_equal('dlist', fullcommand('dl'))
785 assert_equal('', fullcommand('dp'))
786 assert_equal('delete', fullcommand('del'))
787 assert_equal('', fullcommand('dell'))
788 assert_equal('', fullcommand('delp'))
789
790 assert_equal('srewind', fullcommand('sre'))
791 assert_equal('scriptnames', fullcommand('scr'))
792 assert_equal('', fullcommand('scg'))
793enddef
794
Bram Moolenaar94738d82020-10-21 14:25:07 +0200795def Test_garbagecollect()
796 garbagecollect(true)
797enddef
798
799def Test_getbufinfo()
800 var bufinfo = getbufinfo(bufnr())
801 getbufinfo('%')->assert_equal(bufinfo)
802
803 edit Xtestfile1
804 hide edit Xtestfile2
805 hide enew
Bram Moolenaare0de1712020-12-02 17:36:54 +0100806 getbufinfo({bufloaded: true, buflisted: true, bufmodified: false})
Bram Moolenaar94738d82020-10-21 14:25:07 +0200807 ->len()->assert_equal(3)
808 bwipe Xtestfile1 Xtestfile2
809enddef
810
811def Test_getbufline()
812 e SomeFile
813 var buf = bufnr()
814 e #
815 var lines = ['aaa', 'bbb', 'ccc']
816 setbufline(buf, 1, lines)
817 getbufline('#', 1, '$')->assert_equal(lines)
Bram Moolenaare6e70a12020-10-22 18:23:38 +0200818 getbufline(-1, '$', '$')->assert_equal([])
819 getbufline(-1, 1, '$')->assert_equal([])
Bram Moolenaar94738d82020-10-21 14:25:07 +0200820
821 bwipe!
822enddef
823
824def Test_getchangelist()
825 new
826 setline(1, 'some text')
827 var changelist = bufnr()->getchangelist()
828 getchangelist('%')->assert_equal(changelist)
829 bwipe!
830enddef
831
832def Test_getchar()
833 while getchar(0)
834 endwhile
835 getchar(true)->assert_equal(0)
836enddef
837
Bram Moolenaar7ad67d12021-03-10 16:08:26 +0100838def Test_getenv()
839 if getenv('does-not_exist') == ''
840 assert_report('getenv() should return null')
841 endif
842 if getenv('does-not_exist') == null
843 else
844 assert_report('getenv() should return null')
845 endif
846 $SOMEENVVAR = 'some'
847 assert_equal('some', getenv('SOMEENVVAR'))
848 unlet $SOMEENVVAR
849enddef
850
Bram Moolenaar94738d82020-10-21 14:25:07 +0200851def Test_getcompletion()
852 set wildignore=*.vim,*~
853 var l = getcompletion('run', 'file', true)
854 l->assert_equal([])
855 set wildignore&
856enddef
857
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +0200858def Test_getcurpos()
859 CheckDefFailure(['echo getcursorcharpos("x")'], 'E1013: Argument 1: type mismatch, expected number but got string')
860enddef
861
862def Test_getcursorcharpos()
863 CheckDefFailure(['echo getcursorcharpos("x")'], 'E1013: Argument 1: type mismatch, expected number but got string')
864enddef
865
866def Test_getcwd()
867 CheckDefFailure(['echo getcwd("x")'], 'E1013: Argument 1: type mismatch, expected number but got string')
868 CheckDefFailure(['echo getcwd("x", 1)'], 'E1013: Argument 1: type mismatch, expected number but got string')
869 CheckDefFailure(['echo getcwd(1, "x")'], 'E1013: Argument 2: type mismatch, expected number but got string')
870enddef
871
Bram Moolenaar94738d82020-10-21 14:25:07 +0200872def Test_getloclist_return_type()
873 var l = getloclist(1)
874 l->assert_equal([])
875
Bram Moolenaare0de1712020-12-02 17:36:54 +0100876 var d = getloclist(1, {items: 0})
877 d->assert_equal({items: []})
Bram Moolenaar94738d82020-10-21 14:25:07 +0200878enddef
879
Yegappan Lakshmanana26f56f2021-07-03 11:58:12 +0200880def Test_getfontname()
881 CheckDefFailure(['getfontname(10)'], 'E1013: Argument 1: type mismatch, expected string but got number')
882enddef
883
Bram Moolenaar7bb4e742020-12-09 12:41:50 +0100884def Test_getfperm()
Bram Moolenaar2a9d5d32020-12-12 18:58:40 +0100885 assert_equal('', getfperm(""))
886 assert_equal('', getfperm(test_null_string()))
887
Yegappan Lakshmanana26f56f2021-07-03 11:58:12 +0200888 CheckDefExecFailure(['echo getfperm(true)'], 'E1013:')
889 CheckDefExecFailure(['echo getfperm(v:null)'], 'E1013:')
Bram Moolenaar7bb4e742020-12-09 12:41:50 +0100890enddef
891
892def Test_getfsize()
Bram Moolenaar2a9d5d32020-12-12 18:58:40 +0100893 assert_equal(-1, getfsize(""))
894 assert_equal(-1, getfsize(test_null_string()))
895
Yegappan Lakshmanana26f56f2021-07-03 11:58:12 +0200896 CheckDefExecFailure(['echo getfsize(true)'], 'E1013:')
897 CheckDefExecFailure(['echo getfsize(v:null)'], 'E1013:')
Bram Moolenaar7bb4e742020-12-09 12:41:50 +0100898enddef
899
900def Test_getftime()
Bram Moolenaar2a9d5d32020-12-12 18:58:40 +0100901 assert_equal(-1, getftime(""))
902 assert_equal(-1, getftime(test_null_string()))
903
Yegappan Lakshmanana26f56f2021-07-03 11:58:12 +0200904 CheckDefExecFailure(['echo getftime(true)'], 'E1013:')
905 CheckDefExecFailure(['echo getftime(v:null)'], 'E1013:')
Bram Moolenaar7bb4e742020-12-09 12:41:50 +0100906enddef
907
908def Test_getftype()
Bram Moolenaar2a9d5d32020-12-12 18:58:40 +0100909 assert_equal('', getftype(""))
910 assert_equal('', getftype(test_null_string()))
911
Yegappan Lakshmanana26f56f2021-07-03 11:58:12 +0200912 CheckDefExecFailure(['echo getftype(true)'], 'E1013:')
913 CheckDefExecFailure(['echo getftype(v:null)'], 'E1013:')
Bram Moolenaar7bb4e742020-12-09 12:41:50 +0100914enddef
915
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +0200916def Test_getjumplist()
917 CheckDefFailure(['echo getjumplist("x")'], 'E1013: Argument 1: type mismatch, expected number but got string')
918 CheckDefFailure(['echo getjumplist("x", 1)'], 'E1013: Argument 1: type mismatch, expected number but got string')
919 CheckDefFailure(['echo getjumplist(1, "x")'], 'E1013: Argument 2: type mismatch, expected number but got string')
920enddef
921
Yegappan Lakshmanana26f56f2021-07-03 11:58:12 +0200922def Test_getmarklist()
923 CheckDefFailure(['getmarklist([])'], 'E1013: Argument 1: type mismatch, expected string but got list<unknown>')
924 assert_equal([], getmarklist(10000))
925 assert_fails('getmarklist("a%b@#")', 'E94:')
926enddef
927
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +0200928def Test_getmatches()
929 CheckDefFailure(['echo getmatches("x")'], 'E1013: Argument 1: type mismatch, expected number but got string')
930enddef
931
Yegappan Lakshmanana26f56f2021-07-03 11:58:12 +0200932def Test_getpos()
933 CheckDefFailure(['getpos(10)'], 'E1013: Argument 1: type mismatch, expected string but got number')
934 assert_equal([0, 1, 1, 0], getpos('.'))
935 assert_equal([0, 0, 0, 0], getpos('a'))
936enddef
937
938def Test_getqflist()
939 CheckDefFailure(['getqflist([])'], 'E1013: Argument 1: type mismatch, expected dict<any> but got list<unknown>')
940 call assert_equal({}, getqflist({}))
941enddef
942
Bram Moolenaar94738d82020-10-21 14:25:07 +0200943def Test_getqflist_return_type()
944 var l = getqflist()
945 l->assert_equal([])
946
Bram Moolenaare0de1712020-12-02 17:36:54 +0100947 var d = getqflist({items: 0})
948 d->assert_equal({items: []})
Bram Moolenaar94738d82020-10-21 14:25:07 +0200949enddef
950
951def Test_getreg()
952 var lines = ['aaa', 'bbb', 'ccc']
953 setreg('a', lines)
954 getreg('a', true, true)->assert_equal(lines)
Bram Moolenaar418a29f2021-02-10 22:23:41 +0100955 assert_fails('getreg("ab")', 'E1162:')
Bram Moolenaar94738d82020-10-21 14:25:07 +0200956enddef
957
958def Test_getreg_return_type()
959 var s1: string = getreg('"')
960 var s2: string = getreg('"', 1)
961 var s3: list<string> = getreg('"', 1, 1)
962enddef
963
Bram Moolenaar418a29f2021-02-10 22:23:41 +0100964def Test_getreginfo()
965 var text = 'abc'
966 setreg('a', text)
967 getreginfo('a')->assert_equal({regcontents: [text], regtype: 'v', isunnamed: false})
968 assert_fails('getreginfo("ab")', 'E1162:')
969enddef
970
971def Test_getregtype()
972 var lines = ['aaa', 'bbb', 'ccc']
973 setreg('a', lines)
974 getregtype('a')->assert_equal('V')
975 assert_fails('getregtype("ab")', 'E1162:')
976enddef
977
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +0200978def Test_gettabinfo()
979 CheckDefFailure(['echo gettabinfo("x")'], 'E1013: Argument 1: type mismatch, expected number but got string')
980enddef
981
982def Test_gettagstack()
983 CheckDefFailure(['echo gettagstack("x")'], 'E1013: Argument 1: type mismatch, expected number but got string')
984enddef
985
Yegappan Lakshmanana26f56f2021-07-03 11:58:12 +0200986def Test_gettext()
987 CheckDefFailure(['gettext(10)'], 'E1013: Argument 1: type mismatch, expected string but got number')
988 assert_equal('abc', gettext("abc"))
989enddef
990
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +0200991def Test_getwininfo()
992 CheckDefFailure(['echo getwininfo("x")'], 'E1013: Argument 1: type mismatch, expected number but got string')
993enddef
994
995def Test_getwinpos()
996 CheckDefFailure(['echo getwinpos("x")'], 'E1013: Argument 1: type mismatch, expected number but got string')
997enddef
998
Bram Moolenaar94738d82020-10-21 14:25:07 +0200999def Test_glob()
1000 glob('runtest.vim', true, true, true)->assert_equal(['runtest.vim'])
1001enddef
1002
Yegappan Lakshmanana26f56f2021-07-03 11:58:12 +02001003def Test_glob2regpat()
1004 CheckDefFailure(['glob2regpat(null)'], 'E1013: Argument 1: type mismatch, expected string but got special')
1005 assert_equal('^$', glob2regpat(''))
1006enddef
1007
Bram Moolenaar94738d82020-10-21 14:25:07 +02001008def Test_globpath()
1009 globpath('.', 'runtest.vim', true, true, true)->assert_equal(['./runtest.vim'])
1010enddef
1011
1012def Test_has()
1013 has('eval', true)->assert_equal(1)
1014enddef
1015
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +02001016def Test_haslocaldir()
1017 CheckDefFailure(['echo haslocaldir("x")'], 'E1013: Argument 1: type mismatch, expected number but got string')
1018 CheckDefFailure(['echo haslocaldir("x", 1)'], 'E1013: Argument 1: type mismatch, expected number but got string')
1019 CheckDefFailure(['echo haslocaldir(1, "x")'], 'E1013: Argument 2: type mismatch, expected number but got string')
1020enddef
1021
Bram Moolenaar94738d82020-10-21 14:25:07 +02001022def Test_hasmapto()
1023 hasmapto('foobar', 'i', true)->assert_equal(0)
1024 iabbrev foo foobar
1025 hasmapto('foobar', 'i', true)->assert_equal(1)
1026 iunabbrev foo
1027enddef
1028
Yegappan Lakshmanana26f56f2021-07-03 11:58:12 +02001029def Test_histadd()
1030 CheckDefFailure(['histadd(1, "x")'], 'E1013: Argument 1: type mismatch, expected string but got number')
1031 CheckDefFailure(['histadd(":", 10)'], 'E1013: Argument 2: type mismatch, expected string but got number')
1032 histadd("search", 'skyblue')
1033 assert_equal('skyblue', histget('/', -1))
1034enddef
1035
1036def Test_histnr()
1037 CheckDefFailure(['histnr(10)'], 'E1013: Argument 1: type mismatch, expected string but got number')
1038 assert_equal(-1, histnr('abc'))
1039enddef
1040
1041def Test_hlID()
1042 CheckDefFailure(['hlID(10)'], 'E1013: Argument 1: type mismatch, expected string but got number')
1043 assert_equal(0, hlID('NonExistingHighlight'))
1044enddef
1045
1046def Test_hlexists()
1047 CheckDefFailure(['hlexists([])'], 'E1013: Argument 1: type mismatch, expected string but got list<unknown>')
1048 assert_equal(0, hlexists('NonExistingHighlight'))
1049enddef
1050
1051def Test_iconv()
1052 CheckDefFailure(['iconv(1, "from", "to")'], 'E1013: Argument 1: type mismatch, expected string but got number')
1053 CheckDefFailure(['iconv("abc", 10, "to")'], 'E1013: Argument 2: type mismatch, expected string but got number')
1054 CheckDefFailure(['iconv("abc", "from", 20)'], 'E1013: Argument 3: type mismatch, expected string but got number')
1055 assert_equal('abc', iconv('abc', 'fromenc', 'toenc'))
1056enddef
1057
Bram Moolenaar94738d82020-10-21 14:25:07 +02001058def Test_index()
1059 index(['a', 'b', 'a', 'B'], 'b', 2, true)->assert_equal(3)
1060enddef
1061
Yegappan Lakshmanana26f56f2021-07-03 11:58:12 +02001062def Test_inputlist()
1063 CheckDefFailure(['inputlist(10)'], 'E1013: Argument 1: type mismatch, expected list<string> but got number')
1064 CheckDefFailure(['inputlist("abc")'], 'E1013: Argument 1: type mismatch, expected list<string> but got string')
1065 CheckDefFailure(['inputlist([1, 2, 3])'], 'E1013: Argument 1: type mismatch, expected list<string> but got list<number>')
1066 feedkeys("2\<CR>", 't')
1067 var r: number = inputlist(['a', 'b', 'c'])
1068 assert_equal(2, r)
1069enddef
1070
1071def Test_inputsecret()
1072 CheckDefFailure(['inputsecret(10)'], 'E1013: Argument 1: type mismatch, expected string but got number')
1073 CheckDefFailure(['inputsecret("Pass:", 20)'], 'E1013: Argument 2: type mismatch, expected string but got number')
1074 feedkeys("\<CR>", 't')
1075 var ans: string = inputsecret('Pass:', '123')
1076 assert_equal('123', ans)
1077enddef
1078
Bram Moolenaar193f6202020-11-16 20:08:35 +01001079let s:number_one = 1
1080let s:number_two = 2
1081let s:string_keep = 'keep'
1082
Bram Moolenaarca174532020-10-21 16:42:22 +02001083def Test_insert()
Bram Moolenaar94738d82020-10-21 14:25:07 +02001084 var l = insert([2, 1], 3)
1085 var res = 0
1086 for n in l
1087 res += n
1088 endfor
1089 res->assert_equal(6)
Bram Moolenaarca174532020-10-21 16:42:22 +02001090
Yegappan Lakshmanan34fcb692021-05-25 20:14:00 +02001091 var m: any = []
1092 insert(m, 4)
1093 call assert_equal([4], m)
1094 extend(m, [6], 0)
1095 call assert_equal([6, 4], m)
1096
Bram Moolenaar39211cb2021-04-18 15:48:04 +02001097 var lines =<< trim END
1098 insert(test_null_list(), 123)
1099 END
1100 CheckDefExecAndScriptFailure(lines, 'E1130:', 1)
1101
1102 lines =<< trim END
1103 insert(test_null_blob(), 123)
1104 END
1105 CheckDefExecAndScriptFailure(lines, 'E1131:', 1)
1106
Bram Moolenaarca174532020-10-21 16:42:22 +02001107 assert_equal([1, 2, 3], insert([2, 3], 1))
Bram Moolenaar193f6202020-11-16 20:08:35 +01001108 assert_equal([1, 2, 3], insert([2, 3], s:number_one))
Bram Moolenaarca174532020-10-21 16:42:22 +02001109 assert_equal([1, 2, 3], insert([1, 2], 3, 2))
Bram Moolenaar193f6202020-11-16 20:08:35 +01001110 assert_equal([1, 2, 3], insert([1, 2], 3, s:number_two))
Bram Moolenaarca174532020-10-21 16:42:22 +02001111 assert_equal(['a', 'b', 'c'], insert(['b', 'c'], 'a'))
1112 assert_equal(0z1234, insert(0z34, 0x12))
Bram Moolenaar193f6202020-11-16 20:08:35 +01001113
Yegappan Lakshmanan34fcb692021-05-25 20:14:00 +02001114 CheckDefFailure(['insert("a", 1)'], 'E1013: Argument 1: type mismatch, expected list<any> but got string', 1)
Bram Moolenaarca174532020-10-21 16:42:22 +02001115 CheckDefFailure(['insert([2, 3], "a")'], 'E1013: Argument 2: type mismatch, expected number but got string', 1)
1116 CheckDefFailure(['insert([2, 3], 1, "x")'], 'E1013: Argument 3: type mismatch, expected number but got string', 1)
Bram Moolenaar94738d82020-10-21 14:25:07 +02001117enddef
1118
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +02001119def Test_invert()
1120 CheckDefFailure(['echo invert("x")'], 'E1013: Argument 1: type mismatch, expected number but got string')
1121enddef
1122
Yegappan Lakshmanana26f56f2021-07-03 11:58:12 +02001123def Test_isdirectory()
1124 CheckDefFailure(['isdirectory(1.1)'], 'E1013: Argument 1: type mismatch, expected string but got float')
1125 assert_false(isdirectory('NonExistingDir'))
1126enddef
1127
1128def Test_items()
1129 CheckDefFailure(['[]->items()'], 'E1013: Argument 1: type mismatch, expected dict<any> but got list<unknown>')
1130 assert_equal([['a', 10], ['b', 20]], {'a': 10, 'b': 20}->items())
1131 assert_equal([], {}->items())
1132enddef
1133
1134def Test_js_decode()
1135 CheckDefFailure(['js_decode(10)'], 'E1013: Argument 1: type mismatch, expected string but got number')
1136 assert_equal([1, 2], js_decode('[1,2]'))
1137enddef
1138
1139def Test_json_decode()
1140 CheckDefFailure(['json_decode(true)'], 'E1013: Argument 1: type mismatch, expected string but got bool')
1141 assert_equal(1.0, json_decode('1.0'))
1142enddef
1143
1144def Test_keys()
1145 CheckDefFailure(['keys([])'], 'E1013: Argument 1: type mismatch, expected dict<any> but got list<unknown>')
1146 assert_equal(['a'], {a: 'v'}->keys())
1147 assert_equal([], {}->keys())
1148enddef
1149
Bram Moolenaar94738d82020-10-21 14:25:07 +02001150def Test_keys_return_type()
Bram Moolenaare0de1712020-12-02 17:36:54 +01001151 const var: list<string> = {a: 1, b: 2}->keys()
Bram Moolenaar94738d82020-10-21 14:25:07 +02001152 var->assert_equal(['a', 'b'])
1153enddef
1154
Bram Moolenaarc5809432021-03-27 21:23:30 +01001155def Test_line()
Yegappan Lakshmanana26f56f2021-07-03 11:58:12 +02001156 assert_fails('line(true)', 'E1174:')
1157enddef
1158
1159def Test_line2byte()
1160 CheckDefFailure(['line2byte(true)'], 'E1013: Argument 1: type mismatch, expected string but got bool')
1161 assert_equal(-1, line2byte(1))
1162 assert_equal(-1, line2byte(10000))
1163enddef
1164
1165def Test_lispindent()
1166 CheckDefFailure(['lispindent({})'], 'E1013: Argument 1: type mismatch, expected string but got dict<unknown>')
1167 assert_equal(0, lispindent(1))
Bram Moolenaarc5809432021-03-27 21:23:30 +01001168enddef
1169
Bram Moolenaar94738d82020-10-21 14:25:07 +02001170def Test_list2str_str2list_utf8()
1171 var s = "\u3042\u3044"
1172 var l = [0x3042, 0x3044]
1173 str2list(s, true)->assert_equal(l)
1174 list2str(l, true)->assert_equal(s)
1175enddef
1176
1177def SID(): number
1178 return expand('<SID>')
1179 ->matchstr('<SNR>\zs\d\+\ze_$')
1180 ->str2nr()
1181enddef
1182
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +02001183def Test_listener_remove()
1184 CheckDefFailure(['echo listener_remove("x")'], 'E1013: Argument 1: type mismatch, expected number but got string')
1185enddef
1186
Bram Moolenaar70250fb2021-01-16 19:01:53 +01001187def Test_map_function_arg()
1188 var lines =<< trim END
1189 def MapOne(i: number, v: string): string
1190 return i .. ':' .. v
1191 enddef
1192 var l = ['a', 'b', 'c']
1193 map(l, MapOne)
1194 assert_equal(['0:a', '1:b', '2:c'], l)
1195 END
1196 CheckDefAndScriptSuccess(lines)
Bram Moolenaar8da6d6d2021-06-05 18:15:09 +02001197
1198 lines =<< trim END
1199 range(3)->map((a, b, c) => a + b + c)
1200 END
1201 CheckDefExecAndScriptFailure(lines, 'E1190: One argument too few')
1202 lines =<< trim END
1203 range(3)->map((a, b, c, d) => a + b + c + d)
1204 END
1205 CheckDefExecAndScriptFailure(lines, 'E1190: 2 arguments too few')
Bram Moolenaar70250fb2021-01-16 19:01:53 +01001206enddef
1207
1208def Test_map_item_type()
1209 var lines =<< trim END
1210 var l = ['a', 'b', 'c']
1211 map(l, (k, v) => k .. '/' .. v )
1212 assert_equal(['0/a', '1/b', '2/c'], l)
1213 END
1214 CheckDefAndScriptSuccess(lines)
1215
1216 lines =<< trim END
1217 var l: list<number> = [0]
1218 echo map(l, (_, v) => [])
1219 END
1220 CheckDefExecAndScriptFailure(lines, 'E1012: Type mismatch; expected number but got list<unknown>', 2)
1221
1222 lines =<< trim END
1223 var l: list<number> = range(2)
1224 echo map(l, (_, v) => [])
1225 END
1226 CheckDefExecAndScriptFailure(lines, 'E1012: Type mismatch; expected number but got list<unknown>', 2)
1227
1228 lines =<< trim END
1229 var d: dict<number> = {key: 0}
1230 echo map(d, (_, v) => [])
1231 END
1232 CheckDefExecAndScriptFailure(lines, 'E1012: Type mismatch; expected number but got list<unknown>', 2)
1233enddef
1234
Bram Moolenaar94738d82020-10-21 14:25:07 +02001235def Test_maparg()
1236 var lnum = str2nr(expand('<sflnum>'))
1237 map foo bar
Bram Moolenaare0de1712020-12-02 17:36:54 +01001238 maparg('foo', '', false, true)->assert_equal({
Bram Moolenaar94738d82020-10-21 14:25:07 +02001239 lnum: lnum + 1,
1240 script: 0,
1241 mode: ' ',
1242 silent: 0,
1243 noremap: 0,
1244 lhs: 'foo',
1245 lhsraw: 'foo',
1246 nowait: 0,
1247 expr: 0,
1248 sid: SID(),
1249 rhs: 'bar',
1250 buffer: 0})
1251 unmap foo
1252enddef
1253
1254def Test_mapcheck()
1255 iabbrev foo foobar
1256 mapcheck('foo', 'i', true)->assert_equal('foobar')
1257 iunabbrev foo
1258enddef
1259
1260def Test_maparg_mapset()
1261 nnoremap <F3> :echo "hit F3"<CR>
1262 var mapsave = maparg('<F3>', 'n', false, true)
1263 mapset('n', false, mapsave)
1264
1265 nunmap <F3>
1266enddef
1267
Bram Moolenaar027c4ab2021-02-21 16:20:18 +01001268def Test_map_failure()
1269 CheckFeature job
1270
1271 var lines =<< trim END
1272 vim9script
1273 writefile([], 'Xtmpfile')
1274 silent e Xtmpfile
1275 var d = {[bufnr('%')]: {a: 0}}
1276 au BufReadPost * Func()
1277 def Func()
1278 if d->has_key('')
1279 endif
1280 eval d[expand('<abuf>')]->mapnew((_, v: dict<job>) => 0)
1281 enddef
1282 e
1283 END
1284 CheckScriptFailure(lines, 'E1013:')
1285 au! BufReadPost
1286 delete('Xtmpfile')
1287enddef
1288
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +02001289def Test_matcharg()
1290 CheckDefFailure(['echo matcharg("x")'], 'E1013: Argument 1: type mismatch, expected number but got string')
1291enddef
1292
1293def Test_matchdelete()
1294 CheckDefFailure(['echo matchdelete("x")'], 'E1013: Argument 1: type mismatch, expected number but got string')
1295 CheckDefFailure(['echo matchdelete("x", 1)'], 'E1013: Argument 1: type mismatch, expected number but got string')
1296 CheckDefFailure(['echo matchdelete(1, "x")'], 'E1013: Argument 2: type mismatch, expected number but got string')
1297enddef
1298
Bram Moolenaar9ae37052021-01-22 22:31:10 +01001299def Test_max()
1300 g:flag = true
1301 var l1: list<number> = g:flag
1302 ? [1, max([2, 3])]
1303 : [4, 5]
1304 assert_equal([1, 3], l1)
1305
1306 g:flag = false
1307 var l2: list<number> = g:flag
1308 ? [1, max([2, 3])]
1309 : [4, 5]
1310 assert_equal([4, 5], l2)
1311enddef
1312
Yegappan Lakshmanana26f56f2021-07-03 11:58:12 +02001313def Test_menu_info()
1314 CheckDefFailure(['menu_info(10)'], 'E1013: Argument 1: type mismatch, expected string but got number')
1315 CheckDefFailure(['menu_info(10, "n")'], 'E1013: Argument 1: type mismatch, expected string but got number')
1316 CheckDefFailure(['menu_info("File", 10)'], 'E1013: Argument 2: type mismatch, expected string but got number')
1317 assert_equal({}, menu_info('aMenu'))
1318enddef
1319
Bram Moolenaar9ae37052021-01-22 22:31:10 +01001320def Test_min()
1321 g:flag = true
1322 var l1: list<number> = g:flag
1323 ? [1, min([2, 3])]
1324 : [4, 5]
1325 assert_equal([1, 2], l1)
1326
1327 g:flag = false
1328 var l2: list<number> = g:flag
1329 ? [1, min([2, 3])]
1330 : [4, 5]
1331 assert_equal([4, 5], l2)
1332enddef
1333
Yegappan Lakshmanana26f56f2021-07-03 11:58:12 +02001334def Test_nextnonblank()
1335 CheckDefFailure(['nextnonblank(null)'], 'E1013: Argument 1: type mismatch, expected string but got special')
1336 assert_equal(0, nextnonblank(1))
1337enddef
1338
Bram Moolenaar94738d82020-10-21 14:25:07 +02001339def Test_nr2char()
1340 nr2char(97, true)->assert_equal('a')
1341enddef
1342
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +02001343def Test_or()
1344 CheckDefFailure(['echo or("x", 0x2)'], 'E1013: Argument 1: type mismatch, expected number but got string')
1345 CheckDefFailure(['echo or(0x1, "x")'], 'E1013: Argument 2: type mismatch, expected number but got string')
1346enddef
1347
Yegappan Lakshmanana26f56f2021-07-03 11:58:12 +02001348def Test_prevnonblank()
1349 CheckDefFailure(['prevnonblank(null)'], 'E1013: Argument 1: type mismatch, expected string but got special')
1350 assert_equal(0, prevnonblank(1))
1351enddef
1352
1353def Test_prompt_getprompt()
Dominique Pelle74509232021-07-03 19:27:37 +02001354 if has('channel')
1355 CheckDefFailure(['prompt_getprompt([])'], 'E1013: Argument 1: type mismatch, expected string but got list<unknown>')
1356 assert_equal('', prompt_getprompt('NonExistingBuf'))
1357 endif
Yegappan Lakshmanana26f56f2021-07-03 11:58:12 +02001358enddef
1359
1360def Test_rand()
1361 CheckDefFailure(['rand(10)'], 'E1013: Argument 1: type mismatch, expected list<number> but got number')
1362 CheckDefFailure(['rand(["a"])'], 'E1013: Argument 1: type mismatch, expected list<number> but got list<string>')
1363 assert_true(rand() >= 0)
1364 assert_true(rand(srand()) >= 0)
1365enddef
1366
Bram Moolenaar94738d82020-10-21 14:25:07 +02001367def Test_readdir()
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01001368 eval expand('sautest')->readdir((e) => e[0] !=# '.')
1369 eval expand('sautest')->readdirex((e) => e.name[0] !=# '.')
Bram Moolenaar94738d82020-10-21 14:25:07 +02001370enddef
1371
Bram Moolenaarc423ad72021-01-13 20:38:03 +01001372def Test_readblob()
1373 var blob = 0z12341234
1374 writefile(blob, 'Xreadblob')
1375 var read: blob = readblob('Xreadblob')
1376 assert_equal(blob, read)
1377
1378 var lines =<< trim END
1379 var read: list<string> = readblob('Xreadblob')
1380 END
1381 CheckDefAndScriptFailure(lines, 'E1012: Type mismatch; expected list<string> but got blob', 1)
1382 delete('Xreadblob')
1383enddef
1384
1385def Test_readfile()
1386 var text = ['aaa', 'bbb', 'ccc']
1387 writefile(text, 'Xreadfile')
1388 var read: list<string> = readfile('Xreadfile')
1389 assert_equal(text, read)
1390
1391 var lines =<< trim END
1392 var read: dict<string> = readfile('Xreadfile')
1393 END
1394 CheckDefAndScriptFailure(lines, 'E1012: Type mismatch; expected dict<string> but got list<string>', 1)
1395 delete('Xreadfile')
1396enddef
1397
Yegappan Lakshmanana26f56f2021-07-03 11:58:12 +02001398def Test_reltime()
1399 CheckDefFailure(['reltime("x")'], 'E1013: Argument 1: type mismatch, expected list<number> but got string')
1400 CheckDefFailure(['reltime(["x", "y"])'], 'E1013: Argument 1: type mismatch, expected list<number> but got list<string>')
1401 CheckDefFailure(['reltime([1, 2], 10)'], 'E1013: Argument 2: type mismatch, expected list<number> but got number')
1402 CheckDefFailure(['reltime([1, 2], ["a", "b"])'], 'E1013: Argument 2: type mismatch, expected list<number> but got list<string>')
1403 var start: list<any> = reltime()
1404 assert_true(type(reltime(start)) == v:t_list)
1405 var end: list<any> = reltime()
1406 assert_true(type(reltime(start, end)) == v:t_list)
1407enddef
1408
1409def Test_reltimefloat()
1410 CheckDefFailure(['reltimefloat("x")'], 'E1013: Argument 1: type mismatch, expected list<number> but got string')
1411 CheckDefFailure(['reltimefloat([1.1])'], 'E1013: Argument 1: type mismatch, expected list<number> but got list<float>')
1412 assert_true(type(reltimefloat(reltime())) == v:t_float)
1413enddef
1414
1415def Test_reltimestr()
1416 CheckDefFailure(['reltimestr(true)'], 'E1013: Argument 1: type mismatch, expected list<number> but got bool')
1417 CheckDefFailure(['reltimestr([true])'], 'E1013: Argument 1: type mismatch, expected list<number> but got list<bool>')
1418 assert_true(type(reltimestr(reltime())) == v:t_string)
1419enddef
1420
1421def Test_remote_foreground()
1422 CheckFeature clientserver
1423 # remote_foreground() doesn't fail on MS-Windows
1424 CheckNotMSWindows
Bram Moolenaard6fa7bd2021-07-05 14:10:04 +02001425 CheckEnv DISPLAY
1426
Yegappan Lakshmanana26f56f2021-07-03 11:58:12 +02001427 CheckDefFailure(['remote_foreground(10)'], 'E1013: Argument 1: type mismatch, expected string but got number')
1428 assert_fails('remote_foreground("NonExistingServer")', 'E241:')
1429enddef
1430
1431def Test_remote_startserver()
1432 CheckDefFailure(['remote_startserver({})'], 'E1013: Argument 1: type mismatch, expected string but got dict<unknown>')
1433enddef
1434
Bram Moolenaar94738d82020-10-21 14:25:07 +02001435def Test_remove_return_type()
Bram Moolenaare0de1712020-12-02 17:36:54 +01001436 var l = remove({one: [1, 2], two: [3, 4]}, 'one')
Bram Moolenaar94738d82020-10-21 14:25:07 +02001437 var res = 0
1438 for n in l
1439 res += n
1440 endfor
1441 res->assert_equal(3)
1442enddef
1443
Yegappan Lakshmanana26f56f2021-07-03 11:58:12 +02001444def Test_rename()
1445 CheckDefFailure(['rename(1, "b")'], 'E1013: Argument 1: type mismatch, expected string but got number')
1446 CheckDefFailure(['rename("a", 2)'], 'E1013: Argument 2: type mismatch, expected string but got number')
1447enddef
1448
1449def Test_resolve()
1450 CheckDefFailure(['resolve([])'], 'E1013: Argument 1: type mismatch, expected string but got list<unknown>')
1451 assert_equal('SomeFile', resolve('SomeFile'))
1452enddef
1453
Bram Moolenaar94738d82020-10-21 14:25:07 +02001454def Test_reverse_return_type()
1455 var l = reverse([1, 2, 3])
1456 var res = 0
1457 for n in l
1458 res += n
1459 endfor
1460 res->assert_equal(6)
1461enddef
1462
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +02001463def Test_screenattr()
1464 CheckDefFailure(['echo screenattr("x", 1)'], 'E1013: Argument 1: type mismatch, expected number but got string')
1465 CheckDefFailure(['echo screenattr(1, "x")'], 'E1013: Argument 2: type mismatch, expected number but got string')
1466enddef
1467
1468def Test_screenchar()
1469 CheckDefFailure(['echo screenchar("x", 1)'], 'E1013: Argument 1: type mismatch, expected number but got string')
1470 CheckDefFailure(['echo screenchar(1, "x")'], 'E1013: Argument 2: type mismatch, expected number but got string')
1471enddef
1472
1473def Test_screenchars()
1474 CheckDefFailure(['echo screenchars("x", 1)'], 'E1013: Argument 1: type mismatch, expected number but got string')
1475 CheckDefFailure(['echo screenchars(1, "x")'], 'E1013: Argument 2: type mismatch, expected number but got string')
1476enddef
1477
Yegappan Lakshmanana26f56f2021-07-03 11:58:12 +02001478def Test_screenpos()
1479 CheckDefFailure(['screenpos("a", 1, 1)'], 'E1013: Argument 1: type mismatch, expected number but got string')
1480 CheckDefFailure(['screenpos(1, "b", 1)'], 'E1013: Argument 2: type mismatch, expected number but got string')
1481 CheckDefFailure(['screenpos(1, 1, "c")'], 'E1013: Argument 3: type mismatch, expected number but got string')
1482 assert_equal({col: 1, row: 1, endcol: 1, curscol: 1}, screenpos(1, 1, 1))
1483enddef
1484
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +02001485def Test_screenstring()
1486 CheckDefFailure(['echo screenstring("x", 1)'], 'E1013: Argument 1: type mismatch, expected number but got string')
1487 CheckDefFailure(['echo screenstring(1, "x")'], 'E1013: Argument 2: type mismatch, expected number but got string')
1488enddef
1489
Bram Moolenaar94738d82020-10-21 14:25:07 +02001490def Test_search()
1491 new
1492 setline(1, ['foo', 'bar'])
1493 var val = 0
1494 # skip expr returns boolean
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01001495 search('bar', 'W', 0, 0, () => val == 1)->assert_equal(2)
Bram Moolenaar94738d82020-10-21 14:25:07 +02001496 :1
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01001497 search('bar', 'W', 0, 0, () => val == 0)->assert_equal(0)
Bram Moolenaar94738d82020-10-21 14:25:07 +02001498 # skip expr returns number, only 0 and 1 are accepted
1499 :1
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01001500 search('bar', 'W', 0, 0, () => 0)->assert_equal(2)
Bram Moolenaar94738d82020-10-21 14:25:07 +02001501 :1
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01001502 search('bar', 'W', 0, 0, () => 1)->assert_equal(0)
1503 assert_fails("search('bar', '', 0, 0, () => -1)", 'E1023:')
1504 assert_fails("search('bar', '', 0, 0, () => -1)", 'E1023:')
Bram Moolenaarf06ab6b2021-05-07 19:44:21 +02001505
1506 setline(1, "find this word")
1507 normal gg
1508 var col = 7
1509 assert_equal(1, search('this', '', 0, 0, 'col(".") > col'))
1510 normal 0
1511 assert_equal([1, 6], searchpos('this', '', 0, 0, 'col(".") > col'))
1512
1513 col = 5
1514 normal 0
1515 assert_equal(0, search('this', '', 0, 0, 'col(".") > col'))
1516 normal 0
1517 assert_equal([0, 0], searchpos('this', '', 0, 0, 'col(".") > col'))
1518 bwipe!
Bram Moolenaar94738d82020-10-21 14:25:07 +02001519enddef
1520
1521def Test_searchcount()
1522 new
1523 setline(1, "foo bar")
1524 :/foo
Bram Moolenaare0de1712020-12-02 17:36:54 +01001525 searchcount({recompute: true})
1526 ->assert_equal({
Bram Moolenaar94738d82020-10-21 14:25:07 +02001527 exact_match: 1,
1528 current: 1,
1529 total: 1,
1530 maxcount: 99,
1531 incomplete: 0})
1532 bwipe!
1533enddef
1534
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001535def Test_searchpair()
1536 new
1537 setline(1, "here { and } there")
Bram Moolenaarf06ab6b2021-05-07 19:44:21 +02001538
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001539 normal f{
1540 var col = 15
1541 assert_equal(1, searchpair('{', '', '}', '', 'col(".") > col'))
1542 assert_equal(12, col('.'))
Bram Moolenaarf06ab6b2021-05-07 19:44:21 +02001543 normal 0f{
1544 assert_equal([1, 12], searchpairpos('{', '', '}', '', 'col(".") > col'))
1545
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001546 col = 8
1547 normal 0f{
1548 assert_equal(0, searchpair('{', '', '}', '', 'col(".") > col'))
1549 assert_equal(6, col('.'))
Bram Moolenaarf06ab6b2021-05-07 19:44:21 +02001550 normal 0f{
1551 assert_equal([0, 0], searchpairpos('{', '', '}', '', 'col(".") > col'))
1552
Bram Moolenaarff652882021-05-16 15:24:49 +02001553 var lines =<< trim END
1554 vim9script
1555 setline(1, '()')
1556 normal gg
1557 def Fail()
1558 try
1559 searchpairpos('(', '', ')', 'nW', '[0]->map("")')
1560 catch
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001561 g:caught = 'yes'
Bram Moolenaarff652882021-05-16 15:24:49 +02001562 endtry
1563 enddef
1564 Fail()
1565 END
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001566 CheckScriptSuccess(lines)
1567 assert_equal('yes', g:caught)
Bram Moolenaarff652882021-05-16 15:24:49 +02001568
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001569 unlet g:caught
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001570 bwipe!
1571enddef
1572
Bram Moolenaar34453202021-01-31 13:08:38 +01001573def Test_set_get_bufline()
1574 # similar to Test_setbufline_getbufline()
1575 var lines =<< trim END
1576 new
1577 var b = bufnr('%')
1578 hide
1579 assert_equal(0, setbufline(b, 1, ['foo', 'bar']))
1580 assert_equal(['foo'], getbufline(b, 1))
1581 assert_equal(['bar'], getbufline(b, '$'))
1582 assert_equal(['foo', 'bar'], getbufline(b, 1, 2))
1583 exe "bd!" b
1584 assert_equal([], getbufline(b, 1, 2))
1585
1586 split Xtest
1587 setline(1, ['a', 'b', 'c'])
1588 b = bufnr('%')
1589 wincmd w
1590
1591 assert_equal(1, setbufline(b, 5, 'x'))
1592 assert_equal(1, setbufline(b, 5, ['x']))
1593 assert_equal(1, setbufline(b, 5, []))
1594 assert_equal(1, setbufline(b, 5, test_null_list()))
1595
1596 assert_equal(1, 'x'->setbufline(bufnr('$') + 1, 1))
1597 assert_equal(1, ['x']->setbufline(bufnr('$') + 1, 1))
1598 assert_equal(1, []->setbufline(bufnr('$') + 1, 1))
1599 assert_equal(1, test_null_list()->setbufline(bufnr('$') + 1, 1))
1600
1601 assert_equal(['a', 'b', 'c'], getbufline(b, 1, '$'))
1602
1603 assert_equal(0, setbufline(b, 4, ['d', 'e']))
1604 assert_equal(['c'], b->getbufline(3))
1605 assert_equal(['d'], getbufline(b, 4))
1606 assert_equal(['e'], getbufline(b, 5))
1607 assert_equal([], getbufline(b, 6))
1608 assert_equal([], getbufline(b, 2, 1))
1609
Bram Moolenaar00385112021-02-07 14:31:06 +01001610 if has('job')
Bram Moolenaar1328bde2021-06-05 20:51:38 +02001611 setbufline(b, 2, [function('eval'), {key: 123}, string(test_null_job())])
Bram Moolenaar00385112021-02-07 14:31:06 +01001612 assert_equal(["function('eval')",
1613 "{'key': 123}",
1614 "no process"],
1615 getbufline(b, 2, 4))
1616 endif
Bram Moolenaar34453202021-01-31 13:08:38 +01001617
1618 exe 'bwipe! ' .. b
1619 END
1620 CheckDefAndScriptSuccess(lines)
1621enddef
1622
Bram Moolenaar94738d82020-10-21 14:25:07 +02001623def Test_searchdecl()
1624 searchdecl('blah', true, true)->assert_equal(1)
1625enddef
1626
1627def Test_setbufvar()
1628 setbufvar(bufnr('%'), '&syntax', 'vim')
1629 &syntax->assert_equal('vim')
1630 setbufvar(bufnr('%'), '&ts', 16)
1631 &ts->assert_equal(16)
Bram Moolenaar31a201a2021-01-03 14:47:25 +01001632 setbufvar(bufnr('%'), '&ai', true)
1633 &ai->assert_equal(true)
1634 setbufvar(bufnr('%'), '&ft', 'filetype')
1635 &ft->assert_equal('filetype')
1636
Bram Moolenaar94738d82020-10-21 14:25:07 +02001637 settabwinvar(1, 1, '&syntax', 'vam')
1638 &syntax->assert_equal('vam')
1639 settabwinvar(1, 1, '&ts', 15)
1640 &ts->assert_equal(15)
1641 setlocal ts=8
Bram Moolenaarb0d81822021-01-03 15:55:10 +01001642 settabwinvar(1, 1, '&list', false)
1643 &list->assert_equal(false)
Bram Moolenaar31a201a2021-01-03 14:47:25 +01001644 settabwinvar(1, 1, '&list', true)
1645 &list->assert_equal(true)
1646 setlocal list&
Bram Moolenaar94738d82020-10-21 14:25:07 +02001647
1648 setbufvar('%', 'myvar', 123)
1649 getbufvar('%', 'myvar')->assert_equal(123)
1650enddef
1651
Yegappan Lakshmanana26f56f2021-07-03 11:58:12 +02001652def Test_setcharsearch()
1653 CheckDefFailure(['setcharsearch("x")'], 'E1013: Argument 1: type mismatch, expected dict<any> but got string')
1654 CheckDefFailure(['setcharsearch([])'], 'E1013: Argument 1: type mismatch, expected dict<any> but got list<unknown>')
1655 var d: dict<any> = {char: 'x', forward: 1, until: 1}
1656 setcharsearch(d)
1657 assert_equal(d, getcharsearch())
1658enddef
1659
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +02001660def Test_setcmdpos()
1661 CheckDefFailure(['echo setcmdpos("x")'], 'E1013: Argument 1: type mismatch, expected number but got string')
1662enddef
1663
Yegappan Lakshmanana26f56f2021-07-03 11:58:12 +02001664def Test_setfperm()
1665 CheckDefFailure(['setfperm(1, "b")'], 'E1013: Argument 1: type mismatch, expected string but got number')
1666 CheckDefFailure(['setfperm("a", 0z10)'], 'E1013: Argument 2: type mismatch, expected string but got blob')
1667enddef
1668
Bram Moolenaar94738d82020-10-21 14:25:07 +02001669def Test_setloclist()
Bram Moolenaare0de1712020-12-02 17:36:54 +01001670 var items = [{filename: '/tmp/file', lnum: 1, valid: true}]
1671 var what = {items: items}
Bram Moolenaar94738d82020-10-21 14:25:07 +02001672 setqflist([], ' ', what)
1673 setloclist(0, [], ' ', what)
1674enddef
1675
1676def Test_setreg()
1677 setreg('a', ['aaa', 'bbb', 'ccc'])
1678 var reginfo = getreginfo('a')
1679 setreg('a', reginfo)
1680 getreginfo('a')->assert_equal(reginfo)
Bram Moolenaar418a29f2021-02-10 22:23:41 +01001681 assert_fails('setreg("ab", 0)', 'E1162:')
Bram Moolenaar94738d82020-10-21 14:25:07 +02001682enddef
1683
Yegappan Lakshmanana26f56f2021-07-03 11:58:12 +02001684def Test_sha256()
1685 CheckDefFailure(['sha256(100)'], 'E1013: Argument 1: type mismatch, expected string but got number')
1686 CheckDefFailure(['sha256(0zABCD)'], 'E1013: Argument 1: type mismatch, expected string but got blob')
1687 assert_equal('ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad', sha256('abc'))
1688enddef
1689
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +02001690def Test_shiftwidth()
1691 CheckDefFailure(['echo shiftwidth("x")'], 'E1013: Argument 1: type mismatch, expected number but got string')
1692enddef
1693
Yegappan Lakshmanana26f56f2021-07-03 11:58:12 +02001694def Test_simplify()
1695 CheckDefFailure(['simplify(100)'], 'E1013: Argument 1: type mismatch, expected string but got number')
1696 call assert_equal('NonExistingFile', simplify('NonExistingFile'))
1697enddef
1698
Bram Moolenaar6601b622021-01-13 21:47:15 +01001699def Test_slice()
1700 assert_equal('12345', slice('012345', 1))
1701 assert_equal('123', slice('012345', 1, 4))
1702 assert_equal('1234', slice('012345', 1, -1))
1703 assert_equal('1', slice('012345', 1, -4))
1704 assert_equal('', slice('012345', 1, -5))
1705 assert_equal('', slice('012345', 1, -6))
1706
1707 assert_equal([1, 2, 3, 4, 5], slice(range(6), 1))
1708 assert_equal([1, 2, 3], slice(range(6), 1, 4))
1709 assert_equal([1, 2, 3, 4], slice(range(6), 1, -1))
1710 assert_equal([1], slice(range(6), 1, -4))
1711 assert_equal([], slice(range(6), 1, -5))
1712 assert_equal([], slice(range(6), 1, -6))
1713
1714 assert_equal(0z1122334455, slice(0z001122334455, 1))
1715 assert_equal(0z112233, slice(0z001122334455, 1, 4))
1716 assert_equal(0z11223344, slice(0z001122334455, 1, -1))
1717 assert_equal(0z11, slice(0z001122334455, 1, -4))
1718 assert_equal(0z, slice(0z001122334455, 1, -5))
1719 assert_equal(0z, slice(0z001122334455, 1, -6))
1720enddef
1721
Bram Moolenaar94738d82020-10-21 14:25:07 +02001722def Test_spellsuggest()
1723 if !has('spell')
1724 MissingFeature 'spell'
1725 else
1726 spellsuggest('marrch', 1, true)->assert_equal(['March'])
1727 endif
1728enddef
1729
Yegappan Lakshmanana26f56f2021-07-03 11:58:12 +02001730def Test_sound_stop()
1731 CheckFeature sound
1732 CheckDefFailure(['sound_stop("x")'], 'E1013: Argument 1: type mismatch, expected number but got string')
1733enddef
1734
1735def Test_soundfold()
1736 CheckDefFailure(['soundfold(20)'], 'E1013: Argument 1: type mismatch, expected string but got number')
1737 assert_equal('abc', soundfold('abc'))
1738enddef
1739
Bram Moolenaar94738d82020-10-21 14:25:07 +02001740def Test_sort_return_type()
1741 var res: list<number>
1742 res = [1, 2, 3]->sort()
1743enddef
1744
1745def Test_sort_argument()
Bram Moolenaar08cf0c02020-12-05 21:47:06 +01001746 var lines =<< trim END
1747 var res = ['b', 'a', 'c']->sort('i')
1748 res->assert_equal(['a', 'b', 'c'])
1749
1750 def Compare(a: number, b: number): number
1751 return a - b
1752 enddef
1753 var l = [3, 6, 7, 1, 8, 2, 4, 5]
1754 sort(l, Compare)
1755 assert_equal([1, 2, 3, 4, 5, 6, 7, 8], l)
1756 END
1757 CheckDefAndScriptSuccess(lines)
Bram Moolenaar94738d82020-10-21 14:25:07 +02001758enddef
1759
Yegappan Lakshmanana26f56f2021-07-03 11:58:12 +02001760def Test_spellbadword()
1761 CheckDefFailure(['spellbadword(100)'], 'E1013: Argument 1: type mismatch, expected string but got number')
1762 spellbadword('good')->assert_equal(['', ''])
1763enddef
1764
Bram Moolenaar94738d82020-10-21 14:25:07 +02001765def Test_split()
1766 split(' aa bb ', '\W\+', true)->assert_equal(['', 'aa', 'bb', ''])
1767enddef
1768
Yegappan Lakshmanana26f56f2021-07-03 11:58:12 +02001769def Test_srand()
1770 CheckDefFailure(['srand("a")'], 'E1013: Argument 1: type mismatch, expected number but got string')
1771 type(srand(100))->assert_equal(v:t_list)
1772enddef
1773
1774def Test_state()
1775 CheckDefFailure(['state({})'], 'E1013: Argument 1: type mismatch, expected string but got dict<unknown>')
1776 assert_equal('', state('a'))
1777enddef
1778
Bram Moolenaar80ad3e22021-01-31 20:48:58 +01001779def Run_str2float()
1780 if !has('float')
1781 MissingFeature 'float'
1782 endif
1783 str2float("1.00")->assert_equal(1.00)
1784 str2float("2e-2")->assert_equal(0.02)
1785
1786 CheckDefFailure(['echo str2float(123)'], 'E1013:')
1787 CheckScriptFailure(['vim9script', 'echo str2float(123)'], 'E1024:')
1788 endif
1789enddef
1790
Bram Moolenaar94738d82020-10-21 14:25:07 +02001791def Test_str2nr()
1792 str2nr("1'000'000", 10, true)->assert_equal(1000000)
Bram Moolenaarf2b26bc2021-01-30 23:05:11 +01001793
1794 CheckDefFailure(['echo str2nr(123)'], 'E1013:')
1795 CheckScriptFailure(['vim9script', 'echo str2nr(123)'], 'E1024:')
1796 CheckDefFailure(['echo str2nr("123", "x")'], 'E1013:')
1797 CheckScriptFailure(['vim9script', 'echo str2nr("123", "x")'], 'E1030:')
1798 CheckDefFailure(['echo str2nr("123", 10, "x")'], 'E1013:')
1799 CheckScriptFailure(['vim9script', 'echo str2nr("123", 10, "x")'], 'E1135:')
Bram Moolenaar94738d82020-10-21 14:25:07 +02001800enddef
1801
1802def Test_strchars()
1803 strchars("A\u20dd", true)->assert_equal(1)
1804enddef
1805
Yegappan Lakshmanana26f56f2021-07-03 11:58:12 +02001806def Test_strlen()
1807 CheckDefFailure(['strlen([])'], 'E1013: Argument 1: type mismatch, expected string but got list<unknown>')
1808 "abc"->strlen()->assert_equal(3)
1809 strlen(99)->assert_equal(2)
1810enddef
1811
1812def Test_strptime()
1813 CheckFunction strptime
1814 CheckDefFailure(['strptime(10, "2021")'], 'E1013: Argument 1: type mismatch, expected string but got number')
1815 CheckDefFailure(['strptime("%Y", 2021)'], 'E1013: Argument 2: type mismatch, expected string but got number')
1816 # BUG: Directly calling strptime() in this function gives an "E117: Unknown
1817 # function" error on MS-Windows even with the above CheckFunction call for
1818 # strptime().
1819 #assert_true(strptime('%Y', '2021') != 0)
1820enddef
1821
1822def Test_strtrans()
1823 CheckDefFailure(['strtrans(20)'], 'E1013: Argument 1: type mismatch, expected string but got number')
1824 assert_equal('abc', strtrans('abc'))
1825enddef
1826
1827def Test_strwidth()
1828 CheckDefFailure(['strwidth(10)'], 'E1013: Argument 1: type mismatch, expected string but got number')
1829 CheckScriptFailure(['vim9script', 'echo strwidth(10)'], 'E1024:')
1830 assert_equal(4, strwidth('abcd'))
1831enddef
1832
Bram Moolenaar94738d82020-10-21 14:25:07 +02001833def Test_submatch()
1834 var pat = 'A\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)'
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001835 var Rep = () => range(10)->mapnew((_, v) => submatch(v, true))->string()
Bram Moolenaar94738d82020-10-21 14:25:07 +02001836 var actual = substitute('A123456789', pat, Rep, '')
1837 var expected = "[['A123456789'], ['1'], ['2'], ['3'], ['4'], ['5'], ['6'], ['7'], ['8'], ['9']]"
1838 actual->assert_equal(expected)
1839enddef
1840
Bram Moolenaar1328bde2021-06-05 20:51:38 +02001841def Test_substitute()
1842 var res = substitute('A1234', '\d', 'X', '')
1843 assert_equal('AX234', res)
1844
1845 if has('job')
1846 assert_fails('"text"->substitute(".*", () => job_start(":"), "")', 'E908: using an invalid value as a String: job')
1847 assert_fails('"text"->substitute(".*", () => job_start(":")->job_getchannel(), "")', 'E908: using an invalid value as a String: channel')
1848 endif
1849enddef
1850
Yegappan Lakshmanana26f56f2021-07-03 11:58:12 +02001851def Test_swapinfo()
1852 CheckDefFailure(['swapinfo({})'], 'E1013: Argument 1: type mismatch, expected string but got dict<unknown>')
1853 call assert_equal({error: 'Cannot open file'}, swapinfo('x'))
1854enddef
1855
1856def Test_swapname()
1857 CheckDefFailure(['swapname([])'], 'E1013: Argument 1: type mismatch, expected string but got list<unknown>')
1858 assert_fails('swapname("NonExistingBuf")', 'E94:')
1859enddef
1860
Bram Moolenaar94738d82020-10-21 14:25:07 +02001861def Test_synID()
1862 new
1863 setline(1, "text")
1864 synID(1, 1, true)->assert_equal(0)
1865 bwipe!
1866enddef
1867
Yegappan Lakshmanana26f56f2021-07-03 11:58:12 +02001868def Test_synIDtrans()
1869 CheckDefFailure(['synIDtrans("a")'], 'E1013: Argument 1: type mismatch, expected number but got string')
1870enddef
1871
1872def Test_tabpagebuflist()
1873 CheckDefFailure(['tabpagebuflist("t")'], 'E1013: Argument 1: type mismatch, expected number but got string')
1874 assert_equal([bufnr('')], tabpagebuflist())
1875 assert_equal([bufnr('')], tabpagebuflist(1))
1876enddef
1877
1878def Test_tabpagenr()
1879 CheckDefFailure(['tabpagenr(1)'], 'E1013: Argument 1: type mismatch, expected string but got number')
1880 assert_equal(1, tabpagenr('$'))
1881 assert_equal(1, tabpagenr())
1882enddef
1883
Bram Moolenaar94738d82020-10-21 14:25:07 +02001884def Test_term_gettty()
1885 if !has('terminal')
1886 MissingFeature 'terminal'
1887 else
1888 var buf = Run_shell_in_terminal({})
1889 term_gettty(buf, true)->assert_notequal('')
1890 StopShellInTerminal(buf)
1891 endif
1892enddef
1893
1894def Test_term_start()
1895 if !has('terminal')
1896 MissingFeature 'terminal'
1897 else
1898 botright new
1899 var winnr = winnr()
Bram Moolenaare0de1712020-12-02 17:36:54 +01001900 term_start(&shell, {curwin: true})
Bram Moolenaar94738d82020-10-21 14:25:07 +02001901 winnr()->assert_equal(winnr)
1902 bwipe!
1903 endif
1904enddef
1905
Yegappan Lakshmanana26f56f2021-07-03 11:58:12 +02001906def Test_timer_info()
1907 CheckDefFailure(['timer_info("id")'], 'E1013: Argument 1: type mismatch, expected number but got string')
1908 assert_equal([], timer_info(100))
1909 assert_equal([], timer_info())
1910enddef
1911
Bram Moolenaar94738d82020-10-21 14:25:07 +02001912def Test_timer_paused()
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01001913 var id = timer_start(50, () => 0)
Bram Moolenaar94738d82020-10-21 14:25:07 +02001914 timer_pause(id, true)
1915 var info = timer_info(id)
1916 info[0]['paused']->assert_equal(1)
1917 timer_stop(id)
1918enddef
1919
Yegappan Lakshmanana26f56f2021-07-03 11:58:12 +02001920def Test_timer_stop()
1921 CheckDefFailure(['timer_stop("x")'], 'E1013: Argument 1: type mismatch, expected number but got string')
1922 assert_equal(0, timer_stop(100))
1923enddef
1924
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +02001925def Test_tolower()
1926 CheckDefFailure(['echo tolower(1)'], 'E1013: Argument 1: type mismatch, expected string but got number')
1927enddef
1928
1929def Test_toupper()
1930 CheckDefFailure(['echo toupper(1)'], 'E1013: Argument 1: type mismatch, expected string but got number')
1931enddef
1932
1933def Test_tr()
1934 CheckDefFailure(['echo tr(1, "a", "b")'], 'E1013: Argument 1: type mismatch, expected string but got number')
1935 CheckDefFailure(['echo tr("a", 1, "b")'], 'E1013: Argument 2: type mismatch, expected string but got number')
1936 CheckDefFailure(['echo tr("a", "a", 1)'], 'E1013: Argument 3: type mismatch, expected string but got number')
1937enddef
1938
Yegappan Lakshmanana26f56f2021-07-03 11:58:12 +02001939def Test_undofile()
1940 CheckDefFailure(['undofile(10)'], 'E1013: Argument 1: type mismatch, expected string but got number')
1941 assert_equal('.abc.un~', fnamemodify(undofile('abc'), ':t'))
1942enddef
1943
1944def Test_values()
1945 CheckDefFailure(['values([])'], 'E1013: Argument 1: type mismatch, expected dict<any> but got list<unknown>')
1946 assert_equal([], {}->values())
1947 assert_equal(['sun'], {star: 'sun'}->values())
1948enddef
1949
Bram Moolenaar37487e12021-01-12 22:08:53 +01001950def Test_win_execute()
1951 assert_equal("\n" .. winnr(), win_execute(win_getid(), 'echo winnr()'))
1952 assert_equal('', win_execute(342343, 'echo winnr()'))
1953enddef
1954
Yegappan Lakshmanana26f56f2021-07-03 11:58:12 +02001955def Test_win_findbuf()
1956 CheckDefFailure(['win_findbuf("a")'], 'E1013: Argument 1: type mismatch, expected number but got string')
1957 assert_equal([], win_findbuf(1000))
1958 assert_equal([win_getid()], win_findbuf(bufnr('')))
1959enddef
1960
1961def Test_win_getid()
1962 CheckDefFailure(['win_getid(".")'], 'E1013: Argument 1: type mismatch, expected number but got string')
1963 CheckDefFailure(['win_getid(1, ".")'], 'E1013: Argument 2: type mismatch, expected number but got string')
1964 assert_equal(win_getid(), win_getid(1, 1))
1965enddef
1966
Bram Moolenaar94738d82020-10-21 14:25:07 +02001967def Test_win_splitmove()
1968 split
Bram Moolenaare0de1712020-12-02 17:36:54 +01001969 win_splitmove(1, 2, {vertical: true, rightbelow: true})
Bram Moolenaar94738d82020-10-21 14:25:07 +02001970 close
1971enddef
1972
Yegappan Lakshmanana26f56f2021-07-03 11:58:12 +02001973def Test_winnr()
1974 CheckDefFailure(['winnr([])'], 'E1013: Argument 1: type mismatch, expected string but got list<unknown>')
1975 assert_equal(1, winnr())
1976 assert_equal(1, winnr('$'))
1977enddef
1978
Bram Moolenaar285b15f2020-12-29 20:25:19 +01001979def Test_winrestcmd()
1980 split
1981 var cmd = winrestcmd()
1982 wincmd _
1983 exe cmd
1984 assert_equal(cmd, winrestcmd())
1985 close
1986enddef
1987
Yegappan Lakshmanana26f56f2021-07-03 11:58:12 +02001988def Test_winrestview()
1989 CheckDefFailure(['winrestview([])'], 'E1013: Argument 1: type mismatch, expected dict<any> but got list<unknown>')
1990 :%d _
1991 setline(1, 'Hello World')
1992 winrestview({lnum: 1, col: 6})
1993 assert_equal([1, 7], [line('.'), col('.')])
1994enddef
1995
Bram Moolenaar43b69b32021-01-07 20:23:33 +01001996def Test_winsaveview()
1997 var view: dict<number> = winsaveview()
1998
1999 var lines =<< trim END
2000 var view: list<number> = winsaveview()
2001 END
2002 CheckDefAndScriptFailure(lines, 'E1012: Type mismatch; expected list<number> but got dict<number>', 1)
2003enddef
2004
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +02002005def Test_win_gettype()
2006 CheckDefFailure(['echo win_gettype("x")'], 'E1013: Argument 1: type mismatch, expected number but got string')
2007enddef
Bram Moolenaar43b69b32021-01-07 20:23:33 +01002008
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +02002009def Test_win_gotoid()
2010 CheckDefFailure(['echo win_gotoid("x")'], 'E1013: Argument 1: type mismatch, expected number but got string')
2011enddef
Bram Moolenaar285b15f2020-12-29 20:25:19 +01002012
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +02002013def Test_win_id2tabwin()
2014 CheckDefFailure(['echo win_id2tabwin("x")'], 'E1013: Argument 1: type mismatch, expected number but got string')
2015enddef
2016
2017def Test_win_id2win()
2018 CheckDefFailure(['echo win_id2win("x")'], 'E1013: Argument 1: type mismatch, expected number but got string')
2019enddef
2020
2021def Test_win_screenpos()
2022 CheckDefFailure(['echo win_screenpos("x")'], 'E1013: Argument 1: type mismatch, expected number but got string')
2023enddef
2024
2025def Test_winbufnr()
2026 CheckDefFailure(['echo winbufnr("x")'], 'E1013: Argument 1: type mismatch, expected number but got string')
2027enddef
2028
2029def Test_winheight()
2030 CheckDefFailure(['echo winheight("x")'], 'E1013: Argument 1: type mismatch, expected number but got string')
2031enddef
2032
2033def Test_winlayout()
2034 CheckDefFailure(['echo winlayout("x")'], 'E1013: Argument 1: type mismatch, expected number but got string')
2035enddef
2036
2037def Test_winwidth()
2038 CheckDefFailure(['echo winwidth("x")'], 'E1013: Argument 1: type mismatch, expected number but got string')
2039enddef
2040
2041def Test_xor()
2042 CheckDefFailure(['echo xor("x", 0x2)'], 'E1013: Argument 1: type mismatch, expected number but got string')
2043 CheckDefFailure(['echo xor(0x1, "x")'], 'E1013: Argument 2: type mismatch, expected number but got string')
2044enddef
Bram Moolenaar94738d82020-10-21 14:25:07 +02002045
2046" vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker