blob: 5bc51b11ca9f03ae55ee67ff3385075c2dbf76b0 [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 Moolenaarf055d452021-07-08 20:57:24 +0200114
115 lines =<< trim END
116 vim9script
117 var l: list<string>
118 l->add(123)
119 END
120 CheckScriptFailure(lines, 'E1012: Type mismatch; expected string but got number', 3)
Bram Moolenaar94738d82020-10-21 14:25:07 +0200121enddef
122
123def Test_add_blob()
124 var b1: blob = 0z12
125 add(b1, 0x34)
126 assert_equal(0z1234, b1)
127
128 var b2: blob # defaults to empty blob
129 add(b2, 0x67)
130 assert_equal(0z67, b2)
131
132 var lines =<< trim END
133 var b: blob
134 add(b, "x")
135 END
136 CheckDefFailure(lines, 'E1012:', 2)
137
138 lines =<< trim END
Bram Moolenaarb7c21af2021-04-18 14:12:31 +0200139 add(test_null_blob(), 123)
140 END
141 CheckDefExecAndScriptFailure(lines, 'E1131:', 1)
142
143 lines =<< trim END
Bram Moolenaar94738d82020-10-21 14:25:07 +0200144 var b: blob = test_null_blob()
145 add(b, 123)
146 END
147 CheckDefExecFailure(lines, 'E1131:', 2)
Bram Moolenaarb7c21af2021-04-18 14:12:31 +0200148
149 # Getting variable with NULL blob allocates a new blob at script level
150 lines =<< trim END
151 vim9script
152 var b: blob = test_null_blob()
153 add(b, 123)
154 END
155 CheckScriptSuccess(lines)
Bram Moolenaar94738d82020-10-21 14:25:07 +0200156enddef
157
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +0200158def Test_and()
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +0200159 CheckDefAndScriptFailure2(['and("x", 0x2)'], 'E1013: Argument 1: type mismatch, expected number but got string', 'E1030: Using a String as a Number')
160 CheckDefAndScriptFailure2(['and(0x1, "x")'], 'E1013: Argument 2: type mismatch, expected number but got string', 'E1030: Using a String as a Number')
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +0200161enddef
162
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100163def Test_append()
164 new
165 setline(1, range(3))
166 var res1: number = append(1, 'one')
167 assert_equal(0, res1)
168 var res2: bool = append(3, 'two')
169 assert_equal(false, res2)
170 assert_equal(['0', 'one', '1', 'two', '2'], getline(1, 6))
Bram Moolenaarb2ac7d02021-03-28 15:46:16 +0200171
172 append(0, 'zero')
173 assert_equal('zero', getline(1))
174 bwipe!
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100175enddef
176
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +0200177def Test_argc()
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +0200178 CheckDefFailure(['argc("x")'], 'E1013: Argument 1: type mismatch, expected number but got string')
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +0200179enddef
180
181def Test_arglistid()
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +0200182 CheckDefFailure(['arglistid("x")'], 'E1013: Argument 1: type mismatch, expected number but got string')
183 CheckDefFailure(['arglistid(1, "y")'], 'E1013: Argument 2: type mismatch, expected number but got string')
184 CheckDefFailure(['arglistid("x", "y")'], 'E1013: Argument 1: type mismatch, expected number but got string')
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +0200185enddef
186
187def Test_argv()
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +0200188 CheckDefFailure(['argv("x")'], 'E1013: Argument 1: type mismatch, expected number but got string')
189 CheckDefFailure(['argv(1, "x")'], 'E1013: Argument 2: type mismatch, expected number but got string')
190 CheckDefFailure(['argv("x", "y")'], 'E1013: Argument 1: type mismatch, expected number but got string')
191enddef
192
193def Test_assert_equalfile()
194 CheckDefFailure(['assert_equalfile(1, "f2")'], 'E1013: Argument 1: type mismatch, expected string but got number')
195 CheckDefFailure(['assert_equalfile("f1", true)'], 'E1013: Argument 2: type mismatch, expected string but got bool')
196 CheckDefFailure(['assert_equalfile("f1", "f2", ["a"])'], 'E1013: Argument 3: type mismatch, expected string but got list<string>')
197enddef
198
199def Test_assert_exception()
200 CheckDefFailure(['assert_exception({})'], 'E1013: Argument 1: type mismatch, expected string but got dict<unknown>')
201 CheckDefFailure(['assert_exception("E1:", v:null)'], 'E1013: Argument 2: type mismatch, expected string but got special')
202enddef
203
204def Test_assert_match()
205 CheckDefFailure(['assert_match({}, "b")'], 'E1013: Argument 1: type mismatch, expected string but got dict<unknown>')
206 CheckDefFailure(['assert_match("a", 1)'], 'E1013: Argument 2: type mismatch, expected string but got number')
207 CheckDefFailure(['assert_match("a", "b", null)'], 'E1013: Argument 3: type mismatch, expected string but got special')
208enddef
209
210def Test_assert_notmatch()
211 CheckDefFailure(['assert_notmatch({}, "b")'], 'E1013: Argument 1: type mismatch, expected string but got dict<unknown>')
212 CheckDefFailure(['assert_notmatch("a", 1)'], 'E1013: Argument 2: type mismatch, expected string but got number')
213 CheckDefFailure(['assert_notmatch("a", "b", null)'], 'E1013: Argument 3: type mismatch, expected string but got special')
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +0200214enddef
215
Bram Moolenaar32105ae2021-03-27 18:59:25 +0100216def Test_balloon_show()
217 CheckGui
218 CheckFeature balloon_eval
219
Yegappan Lakshmanana26f56f2021-07-03 11:58:12 +0200220 assert_fails('balloon_show(10)', 'E1174:')
Bram Moolenaar32105ae2021-03-27 18:59:25 +0100221 assert_fails('balloon_show(true)', 'E1174:')
222enddef
223
224def Test_balloon_split()
Bram Moolenaar7b45d462021-03-27 19:09:02 +0100225 CheckFeature balloon_eval_term
Bram Moolenaar32105ae2021-03-27 18:59:25 +0100226
Yegappan Lakshmanana26f56f2021-07-03 11:58:12 +0200227 assert_fails('balloon_split([])', 'E1174:')
Bram Moolenaar32105ae2021-03-27 18:59:25 +0100228 assert_fails('balloon_split(true)', 'E1174:')
229enddef
230
Bram Moolenaarf28f2ac2021-03-22 22:21:26 +0100231def Test_browse()
232 CheckFeature browse
233
234 var lines =<< trim END
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +0100235 browse(1, 2, 3, 4)
Bram Moolenaarf28f2ac2021-03-22 22:21:26 +0100236 END
237 CheckDefExecAndScriptFailure(lines, 'E1174: String required for argument 2')
238 lines =<< trim END
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +0100239 browse(1, 'title', 3, 4)
Bram Moolenaarf28f2ac2021-03-22 22:21:26 +0100240 END
241 CheckDefExecAndScriptFailure(lines, 'E1174: String required for argument 3')
242 lines =<< trim END
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +0100243 browse(1, 'title', 'dir', 4)
Bram Moolenaarf28f2ac2021-03-22 22:21:26 +0100244 END
245 CheckDefExecAndScriptFailure(lines, 'E1174: String required for argument 4')
246enddef
247
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +0200248def Test_browsedir()
249 CheckDefFailure(['browsedir({}, "b")'], 'E1013: Argument 1: type mismatch, expected string but got dict<unknown>')
250 CheckDefFailure(['browsedir("a", [])'], 'E1013: Argument 2: type mismatch, expected string but got list<unknown>')
251enddef
252
Yegappan Lakshmanana26f56f2021-07-03 11:58:12 +0200253def Test_bufadd()
254 assert_fails('bufadd([])', 'E730:')
255enddef
256
Bram Moolenaar32105ae2021-03-27 18:59:25 +0100257def Test_bufexists()
Yegappan Lakshmanana26f56f2021-07-03 11:58:12 +0200258 assert_fails('bufexists(true)', 'E1174:')
Bram Moolenaar32105ae2021-03-27 18:59:25 +0100259enddef
260
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100261def Test_buflisted()
262 var res: bool = buflisted('asdf')
263 assert_equal(false, res)
Yegappan Lakshmanana26f56f2021-07-03 11:58:12 +0200264 assert_fails('buflisted(true)', 'E1174:')
265 assert_fails('buflisted([])', 'E1174:')
266enddef
267
268def Test_bufload()
269 assert_fails('bufload([])', 'E730:')
270enddef
271
272def Test_bufloaded()
273 assert_fails('bufloaded(true)', 'E1174:')
274 assert_fails('bufloaded([])', 'E1174:')
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100275enddef
276
Bram Moolenaar94738d82020-10-21 14:25:07 +0200277def Test_bufname()
278 split SomeFile
279 bufname('%')->assert_equal('SomeFile')
280 edit OtherFile
281 bufname('#')->assert_equal('SomeFile')
282 close
Yegappan Lakshmanana26f56f2021-07-03 11:58:12 +0200283 assert_fails('bufname(true)', 'E1138:')
284 assert_fails('bufname([])', 'E745:')
Bram Moolenaar94738d82020-10-21 14:25:07 +0200285enddef
286
287def Test_bufnr()
288 var buf = bufnr()
289 bufnr('%')->assert_equal(buf)
290
291 buf = bufnr('Xdummy', true)
292 buf->assert_notequal(-1)
293 exe 'bwipe! ' .. buf
294enddef
295
296def Test_bufwinid()
297 var origwin = win_getid()
298 below split SomeFile
299 var SomeFileID = win_getid()
300 below split OtherFile
301 below split SomeFile
302 bufwinid('SomeFile')->assert_equal(SomeFileID)
303
304 win_gotoid(origwin)
305 only
306 bwipe SomeFile
307 bwipe OtherFile
Bram Moolenaar32105ae2021-03-27 18:59:25 +0100308
Yegappan Lakshmanana26f56f2021-07-03 11:58:12 +0200309 assert_fails('bufwinid(true)', 'E1138:')
310 assert_fails('bufwinid([])', 'E745:')
311enddef
312
313def Test_bufwinnr()
314 assert_fails('bufwinnr(true)', 'E1138:')
315 assert_fails('bufwinnr([])', 'E745:')
316enddef
317
318def Test_byte2line()
319 CheckDefFailure(['byte2line("1")'], 'E1013: Argument 1: type mismatch, expected number but got string')
320 CheckDefFailure(['byte2line([])'], 'E1013: Argument 1: type mismatch, expected number but got list<unknown>')
321 assert_equal(-1, byte2line(0))
Bram Moolenaar94738d82020-10-21 14:25:07 +0200322enddef
323
324def Test_call_call()
325 var l = [3, 2, 1]
326 call('reverse', [l])
327 l->assert_equal([1, 2, 3])
328enddef
329
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +0200330def Test_ch_canread()
331 if !has('channel')
332 CheckFeature channel
333 endif
334 CheckDefFailure(['ch_canread(10)'], 'E1013: Argument 1: type mismatch, expected channel but got number')
335enddef
336
337def Test_ch_close()
338 if !has('channel')
339 CheckFeature channel
340 endif
341 CheckDefFailure(['ch_close("c")'], 'E1013: Argument 1: type mismatch, expected channel but got string')
342enddef
343
344def Test_ch_close_in()
345 if !has('channel')
346 CheckFeature channel
347 endif
348 CheckDefFailure(['ch_close_in(true)'], 'E1013: Argument 1: type mismatch, expected channel but got bool')
349enddef
350
351def Test_ch_info()
352 if !has('channel')
353 CheckFeature channel
354 endif
355 CheckDefFailure(['ch_info([1])'], 'E1013: Argument 1: type mismatch, expected channel but got list<number>')
356enddef
357
Bram Moolenaarc5809432021-03-27 21:23:30 +0100358def Test_ch_logfile()
Bram Moolenaar886e5e72021-04-05 13:36:34 +0200359 if !has('channel')
360 CheckFeature channel
361 endif
Yegappan Lakshmanana26f56f2021-07-03 11:58:12 +0200362 assert_fails('ch_logfile(true)', 'E1174:')
363 assert_fails('ch_logfile("foo", true)', 'E1174:')
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +0200364
365 CheckDefAndScriptFailure2(['ch_logfile(1)'], 'E1013: Argument 1: type mismatch, expected string but got number', 'E1174: String required for argument 1')
366 CheckDefAndScriptFailure2(['ch_logfile("a", true)'], 'E1013: Argument 2: type mismatch, expected string but got bool', 'E1174: String required for argument 2')
367enddef
368
369def Test_ch_open()
370 if !has('channel')
371 CheckFeature channel
372 endif
373 CheckDefAndScriptFailure2(['ch_open({"a": 10}, "a")'], 'E1013: Argument 1: type mismatch, expected string but got dict<number>', 'E1174: String required for argument 1')
374 CheckDefAndScriptFailure2(['ch_open("a", [1])'], 'E1013: Argument 2: type mismatch, expected dict<any> but got list<number>', 'E1206: Dictionary required for argument 2')
Bram Moolenaarc5809432021-03-27 21:23:30 +0100375enddef
376
Bram Moolenaar94738d82020-10-21 14:25:07 +0200377def Test_char2nr()
378 char2nr('あ', true)->assert_equal(12354)
Bram Moolenaarc5809432021-03-27 21:23:30 +0100379
Yegappan Lakshmanana26f56f2021-07-03 11:58:12 +0200380 assert_fails('char2nr(true)', 'E1174:')
Bram Moolenaarc5809432021-03-27 21:23:30 +0100381enddef
382
383def Test_charclass()
Yegappan Lakshmanana26f56f2021-07-03 11:58:12 +0200384 assert_fails('charclass(true)', 'E1174:')
Bram Moolenaarc5809432021-03-27 21:23:30 +0100385enddef
386
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +0200387def Test_charcol()
388 CheckDefFailure(['charcol(10)'], 'E1013: Argument 1: type mismatch, expected string but got number')
389 CheckDefFailure(['charcol({a: 10})'], 'E1013: Argument 1: type mismatch, expected string but got dict<number>')
390enddef
391
392def Test_charidx()
393 CheckDefFailure(['charidx("a", "b")'], 'E1013: Argument 2: type mismatch, expected number but got string')
394 CheckDefFailure(['charidx(0z10, 1)'], 'E1013: Argument 1: type mismatch, expected string but got blob')
395 CheckDefFailure(['charidx("a", 1, "")'], 'E1013: Argument 3: type mismatch, expected bool but got string')
396enddef
397
Bram Moolenaarc5809432021-03-27 21:23:30 +0100398def Test_chdir()
Yegappan Lakshmanana26f56f2021-07-03 11:58:12 +0200399 assert_fails('chdir(true)', 'E1174:')
400enddef
401
402def Test_cindent()
403 CheckDefFailure(['cindent([])'], 'E1013: Argument 1: type mismatch, expected string but got list<unknown>')
404 CheckDefFailure(['cindent(null)'], 'E1013: Argument 1: type mismatch, expected string but got special')
405 assert_equal(-1, cindent(0))
406 assert_equal(0, cindent('.'))
Bram Moolenaar94738d82020-10-21 14:25:07 +0200407enddef
408
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +0200409def Test_clearmatches()
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +0200410 CheckDefFailure(['clearmatches("x")'], 'E1013: Argument 1: type mismatch, expected number but got string')
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +0200411enddef
412
Bram Moolenaar94738d82020-10-21 14:25:07 +0200413def Test_col()
414 new
415 setline(1, 'asdf')
416 col([1, '$'])->assert_equal(5)
Bram Moolenaarc5809432021-03-27 21:23:30 +0100417
Yegappan Lakshmanana26f56f2021-07-03 11:58:12 +0200418 assert_fails('col(true)', 'E1174:')
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +0200419
420 CheckDefFailure(['col(10)'], 'E1013: Argument 1: type mismatch, expected string but got number')
421 CheckDefFailure(['col({a: 10})'], 'E1013: Argument 1: type mismatch, expected string but got dict<number>')
422 CheckDefFailure(['col(true)'], 'E1013: Argument 1: type mismatch, expected string but got bool')
423 bw!
Bram Moolenaarc5809432021-03-27 21:23:30 +0100424enddef
425
426def Test_confirm()
427 if !has('dialog_con') && !has('dialog_gui')
428 CheckFeature dialog_con
429 endif
430
Yegappan Lakshmanana26f56f2021-07-03 11:58:12 +0200431 assert_fails('confirm(true)', 'E1174:')
432 assert_fails('confirm("yes", true)', 'E1174:')
433 assert_fails('confirm("yes", "maybe", 2, true)', 'E1174:')
434enddef
435
436def Test_complete_info()
437 CheckDefFailure(['complete_info("")'], 'E1013: Argument 1: type mismatch, expected list<string> but got string')
438 CheckDefFailure(['complete_info({})'], 'E1013: Argument 1: type mismatch, expected list<string> but got dict<unknown>')
439 assert_equal({'pum_visible': 0, 'mode': '', 'selected': -1, 'items': []}, complete_info())
440 assert_equal({'mode': '', 'items': []}, complete_info(['mode', 'items']))
Bram Moolenaar94738d82020-10-21 14:25:07 +0200441enddef
442
443def Test_copy_return_type()
444 var l = copy([1, 2, 3])
445 var res = 0
446 for n in l
447 res += n
448 endfor
449 res->assert_equal(6)
450
451 var dl = deepcopy([1, 2, 3])
452 res = 0
453 for n in dl
454 res += n
455 endfor
456 res->assert_equal(6)
457
458 dl = deepcopy([1, 2, 3], true)
459enddef
460
461def Test_count()
462 count('ABC ABC ABC', 'b', true)->assert_equal(3)
463 count('ABC ABC ABC', 'b', false)->assert_equal(0)
464enddef
465
Bram Moolenaar9a963372020-12-21 21:58:46 +0100466def Test_cursor()
467 new
468 setline(1, range(4))
469 cursor(2, 1)
470 assert_equal(2, getcurpos()[1])
471 cursor('$', 1)
472 assert_equal(4, getcurpos()[1])
473
474 var lines =<< trim END
475 cursor('2', 1)
476 END
477 CheckDefExecAndScriptFailure(lines, 'E475:')
478enddef
479
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +0200480def Test_debugbreak()
481 CheckMSWindows
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +0200482 CheckDefFailure(['debugbreak("x")'], 'E1013: Argument 1: type mismatch, expected number but got string')
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +0200483enddef
484
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100485def Test_delete()
486 var res: bool = delete('doesnotexist')
487 assert_equal(true, res)
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +0200488
489 CheckDefFailure(['delete(10)'], 'E1013: Argument 1: type mismatch, expected string but got number')
490 CheckDefFailure(['delete("a", 10)'], 'E1013: Argument 2: type mismatch, expected string but got number')
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100491enddef
492
Yegappan Lakshmanana26f56f2021-07-03 11:58:12 +0200493def Test_diff_filler()
494 CheckDefFailure(['diff_filler([])'], 'E1013: Argument 1: type mismatch, expected string but got list<unknown>')
495 CheckDefFailure(['diff_filler(true)'], 'E1013: Argument 1: type mismatch, expected string but got bool')
496 assert_equal(0, diff_filler(1))
497 assert_equal(0, diff_filler('.'))
498enddef
499
500def Test_escape()
501 CheckDefFailure(['escape("a", 10)'], 'E1013: Argument 2: type mismatch, expected string but got number')
502 CheckDefFailure(['escape(10, " ")'], 'E1013: Argument 1: type mismatch, expected string but got number')
503 CheckDefFailure(['escape(true, false)'], 'E1013: Argument 1: type mismatch, expected string but got bool')
504 assert_equal('a\:b', escape("a:b", ":"))
505enddef
506
507def Test_eval()
508 CheckDefFailure(['eval(10)'], 'E1013: Argument 1: type mismatch, expected string but got number')
509 CheckDefFailure(['eval(null)'], 'E1013: Argument 1: type mismatch, expected string but got special')
510 assert_equal(2, eval('1 + 1'))
511enddef
512
Bram Moolenaar7bb4e742020-12-09 12:41:50 +0100513def Test_executable()
Bram Moolenaar2a9d5d32020-12-12 18:58:40 +0100514 assert_false(executable(""))
515 assert_false(executable(test_null_string()))
516
Yegappan Lakshmanana26f56f2021-07-03 11:58:12 +0200517 CheckDefExecFailure(['echo executable(123)'], 'E1013:')
518 CheckDefExecFailure(['echo executable(true)'], 'E1013:')
Bram Moolenaar7bb4e742020-12-09 12:41:50 +0100519enddef
520
Bram Moolenaarca81f0e2021-06-20 14:41:01 +0200521def Test_execute()
522 var res = execute("echo 'hello'")
523 assert_equal("\nhello", res)
524 res = execute(["echo 'here'", "echo 'there'"])
525 assert_equal("\nhere\nthere", res)
526
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +0200527 CheckDefFailure(['execute(123)'], 'E1013: Argument 1: type mismatch, expected string but got number')
528 CheckDefFailure(['execute([123])'], 'E1013: Argument 1: type mismatch, expected list<string> but got list<number>')
Bram Moolenaarca81f0e2021-06-20 14:41:01 +0200529 CheckDefExecFailure(['echo execute(["xx", 123])'], 'E492')
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +0200530 CheckDefFailure(['execute("xx", 123)'], 'E1013: Argument 2: type mismatch, expected string but got number')
Bram Moolenaarca81f0e2021-06-20 14:41:01 +0200531enddef
532
Bram Moolenaar7bb4e742020-12-09 12:41:50 +0100533def Test_exepath()
Yegappan Lakshmanana26f56f2021-07-03 11:58:12 +0200534 CheckDefExecFailure(['echo exepath(true)'], 'E1013:')
535 CheckDefExecFailure(['echo exepath(v:null)'], 'E1013:')
Bram Moolenaarfa984412021-03-25 22:15:28 +0100536 CheckDefExecFailure(['echo exepath("")'], 'E1175:')
Bram Moolenaar7bb4e742020-12-09 12:41:50 +0100537enddef
538
Yegappan Lakshmanana26f56f2021-07-03 11:58:12 +0200539def Test_exists()
540 CheckDefFailure(['exists(10)'], 'E1013: Argument 1: type mismatch, expected string but got number')
541 call assert_equal(1, exists('&tabstop'))
542enddef
543
Bram Moolenaar94738d82020-10-21 14:25:07 +0200544def Test_expand()
545 split SomeFile
546 expand('%', true, true)->assert_equal(['SomeFile'])
547 close
548enddef
549
Bram Moolenaar02795102021-05-03 21:40:26 +0200550def Test_expandcmd()
551 $FOO = "blue"
552 assert_equal("blue sky", expandcmd("`=$FOO .. ' sky'`"))
553
554 assert_equal("yes", expandcmd("`={a: 'yes'}['a']`"))
555enddef
556
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100557def Test_extend_arg_types()
Bram Moolenaarc03f5c62021-01-31 17:48:30 +0100558 g:number_one = 1
559 g:string_keep = 'keep'
560 var lines =<< trim END
561 assert_equal([1, 2, 3], extend([1, 2], [3]))
562 assert_equal([3, 1, 2], extend([1, 2], [3], 0))
563 assert_equal([1, 3, 2], extend([1, 2], [3], 1))
564 assert_equal([1, 3, 2], extend([1, 2], [3], g:number_one))
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100565
Bram Moolenaarc03f5c62021-01-31 17:48:30 +0100566 assert_equal({a: 1, b: 2, c: 3}, extend({a: 1, b: 2}, {c: 3}))
567 assert_equal({a: 1, b: 4}, extend({a: 1, b: 2}, {b: 4}))
568 assert_equal({a: 1, b: 2}, extend({a: 1, b: 2}, {b: 4}, 'keep'))
569 assert_equal({a: 1, b: 2}, extend({a: 1, b: 2}, {b: 4}, g:string_keep))
Bram Moolenaar193f6202020-11-16 20:08:35 +0100570
Bram Moolenaarc03f5c62021-01-31 17:48:30 +0100571 var res: list<dict<any>>
572 extend(res, mapnew([1, 2], (_, v) => ({})))
573 assert_equal([{}, {}], res)
574 END
575 CheckDefAndScriptSuccess(lines)
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100576
Yegappan Lakshmanan34fcb692021-05-25 20:14:00 +0200577 CheckDefFailure(['extend("a", 1)'], 'E1013: Argument 1: type mismatch, expected list<any> but got string')
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100578 CheckDefFailure(['extend([1, 2], 3)'], 'E1013: Argument 2: type mismatch, expected list<number> but got number')
579 CheckDefFailure(['extend([1, 2], ["x"])'], 'E1013: Argument 2: type mismatch, expected list<number> but got list<string>')
580 CheckDefFailure(['extend([1, 2], [3], "x")'], 'E1013: Argument 3: type mismatch, expected number but got string')
581
Bram Moolenaare0de1712020-12-02 17:36:54 +0100582 CheckDefFailure(['extend({a: 1}, 42)'], 'E1013: Argument 2: type mismatch, expected dict<number> but got number')
583 CheckDefFailure(['extend({a: 1}, {b: "x"})'], 'E1013: Argument 2: type mismatch, expected dict<number> but got dict<string>')
584 CheckDefFailure(['extend({a: 1}, {b: 2}, 1)'], 'E1013: Argument 3: type mismatch, expected string but got number')
Bram Moolenaar351ead02021-01-16 16:07:01 +0100585
586 CheckDefFailure(['extend([1], ["b"])'], 'E1013: Argument 2: type mismatch, expected list<number> but got list<string>')
Bram Moolenaare32e5162021-01-21 20:21:29 +0100587 CheckDefExecFailure(['extend([1], ["b", 1])'], 'E1013: Argument 2: type mismatch, expected list<number> but got list<any>')
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100588enddef
589
Bram Moolenaarb0e6b512021-01-12 20:23:40 +0100590def Test_extendnew()
591 assert_equal([1, 2, 'a'], extendnew([1, 2], ['a']))
592 assert_equal({one: 1, two: 'a'}, extendnew({one: 1}, {two: 'a'}))
593
594 CheckDefFailure(['extendnew({a: 1}, 42)'], 'E1013: Argument 2: type mismatch, expected dict<number> but got number')
595 CheckDefFailure(['extendnew({a: 1}, [42])'], 'E1013: Argument 2: type mismatch, expected dict<number> but got list<number>')
596 CheckDefFailure(['extendnew([1, 2], "x")'], 'E1013: Argument 2: type mismatch, expected list<number> but got string')
597 CheckDefFailure(['extendnew([1, 2], {x: 1})'], 'E1013: Argument 2: type mismatch, expected list<number> but got dict<number>')
598enddef
599
Bram Moolenaar94738d82020-10-21 14:25:07 +0200600def Test_extend_return_type()
601 var l = extend([1, 2], [3])
602 var res = 0
603 for n in l
604 res += n
605 endfor
606 res->assert_equal(6)
607enddef
608
Bram Moolenaaraa210a32021-01-02 15:41:03 +0100609func g:ExtendDict(d)
610 call extend(a:d, #{xx: 'x'})
611endfunc
612
613def Test_extend_dict_item_type()
614 var lines =<< trim END
615 var d: dict<number> = {a: 1}
616 extend(d, {b: 2})
617 END
618 CheckDefAndScriptSuccess(lines)
619
620 lines =<< trim END
621 var d: dict<number> = {a: 1}
622 extend(d, {b: 'x'})
623 END
Bram Moolenaarc03f5c62021-01-31 17:48:30 +0100624 CheckDefAndScriptFailure(lines, 'E1013: Argument 2: type mismatch, expected dict<number> but got dict<string>', 2)
Bram Moolenaaraa210a32021-01-02 15:41:03 +0100625
626 lines =<< trim END
627 var d: dict<number> = {a: 1}
628 g:ExtendDict(d)
629 END
630 CheckDefExecFailure(lines, 'E1012: Type mismatch; expected number but got string', 0)
631 CheckScriptFailure(['vim9script'] + lines, 'E1012:', 1)
632enddef
633
634func g:ExtendList(l)
635 call extend(a:l, ['x'])
636endfunc
637
638def Test_extend_list_item_type()
639 var lines =<< trim END
640 var l: list<number> = [1]
641 extend(l, [2])
642 END
643 CheckDefAndScriptSuccess(lines)
644
645 lines =<< trim END
646 var l: list<number> = [1]
647 extend(l, ['x'])
648 END
Bram Moolenaarc03f5c62021-01-31 17:48:30 +0100649 CheckDefAndScriptFailure(lines, 'E1013: Argument 2: type mismatch, expected list<number> but got list<string>', 2)
Bram Moolenaaraa210a32021-01-02 15:41:03 +0100650
651 lines =<< trim END
652 var l: list<number> = [1]
653 g:ExtendList(l)
654 END
655 CheckDefExecFailure(lines, 'E1012: Type mismatch; expected number but got string', 0)
656 CheckScriptFailure(['vim9script'] + lines, 'E1012:', 1)
657enddef
Bram Moolenaar94738d82020-10-21 14:25:07 +0200658
Bram Moolenaar93e1cae2021-03-13 21:24:56 +0100659def Test_extend_with_error_function()
660 var lines =<< trim END
661 vim9script
662 def F()
663 {
664 var m = 10
665 }
666 echo m
667 enddef
668
669 def Test()
670 var d: dict<any> = {}
671 d->extend({A: 10, Func: function('F', [])})
672 enddef
673
674 Test()
675 END
676 CheckScriptFailure(lines, 'E1001: Variable not found: m')
677enddef
678
Yegappan Lakshmanana26f56f2021-07-03 11:58:12 +0200679def Test_feedkeys()
680 CheckDefFailure(['feedkeys(10)'], 'E1013: Argument 1: type mismatch, expected string but got number')
681 CheckDefFailure(['feedkeys("x", 10)'], 'E1013: Argument 2: type mismatch, expected string but got number')
682 CheckDefFailure(['feedkeys([], {})'], 'E1013: Argument 1: type mismatch, expected string but got list<unknown>')
683 g:TestVar = 1
684 feedkeys(":g:TestVar = 789\n", 'xt')
685 assert_equal(789, g:TestVar)
686 unlet g:TestVar
687enddef
688
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +0200689def Test_indent()
690 CheckDefAndScriptFailure2(['indent([1])'], 'E1013: Argument 1: type mismatch, expected string but got list<number>', 'E745: Using a List as a Number')
691 CheckDefAndScriptFailure2(['indent(true)'], 'E1013: Argument 1: type mismatch, expected string but got bool', 'E1138: Using a Bool as a Number')
692 assert_equal(0, indent(1))
693enddef
694
695def Test_input()
696 CheckDefFailure(['input(5)'], 'E1013: Argument 1: type mismatch, expected string but got number')
697 CheckDefAndScriptFailure2(['input(["a"])'], 'E1013: Argument 1: type mismatch, expected string but got list<string>', 'E730: Using a List as a String')
698 CheckDefFailure(['input("p", 10)'], 'E1013: Argument 2: type mismatch, expected string but got number')
699 CheckDefAndScriptFailure2(['input("p", "q", 20)'], 'E1013: Argument 3: type mismatch, expected string but got number', 'E180: Invalid complete value')
700enddef
701
702def Test_inputdialog()
703 CheckDefFailure(['inputdialog(5)'], 'E1013: Argument 1: type mismatch, expected string but got number')
704 CheckDefAndScriptFailure2(['inputdialog(["a"])'], 'E1013: Argument 1: type mismatch, expected string but got list<string>', 'E730: Using a List as a String')
705 CheckDefFailure(['inputdialog("p", 10)'], 'E1013: Argument 2: type mismatch, expected string but got number')
706 CheckDefFailure(['inputdialog("p", "q", 20)'], 'E1013: Argument 3: type mismatch, expected string but got number')
707enddef
708
Bram Moolenaar64ed4d42021-01-12 21:22:31 +0100709def Test_job_info_return_type()
710 if has('job')
711 job_start(&shell)
712 var jobs = job_info()
Bram Moolenaara47e05f2021-01-12 21:49:00 +0100713 assert_equal('list<job>', typename(jobs))
714 assert_equal('dict<any>', typename(job_info(jobs[0])))
Bram Moolenaar64ed4d42021-01-12 21:22:31 +0100715 job_stop(jobs[0])
716 endif
717enddef
718
Bram Moolenaar7bb4e742020-12-09 12:41:50 +0100719def Test_filereadable()
Bram Moolenaar2a9d5d32020-12-12 18:58:40 +0100720 assert_false(filereadable(""))
721 assert_false(filereadable(test_null_string()))
722
Yegappan Lakshmanana26f56f2021-07-03 11:58:12 +0200723 CheckDefExecFailure(['echo filereadable(123)'], 'E1013:')
724 CheckDefExecFailure(['echo filereadable(true)'], 'E1013:')
Bram Moolenaar7bb4e742020-12-09 12:41:50 +0100725enddef
726
727def Test_filewritable()
Bram Moolenaar2a9d5d32020-12-12 18:58:40 +0100728 assert_false(filewritable(""))
729 assert_false(filewritable(test_null_string()))
730
Yegappan Lakshmanana26f56f2021-07-03 11:58:12 +0200731 CheckDefExecFailure(['echo filewritable(123)'], 'E1013:')
732 CheckDefExecFailure(['echo filewritable(true)'], 'E1013:')
Bram Moolenaar7bb4e742020-12-09 12:41:50 +0100733enddef
734
735def Test_finddir()
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +0200736 CheckDefAndScriptFailure2(['finddir(true)'], 'E1013: Argument 1: type mismatch, expected string but got bool', 'E1174: String required for argument 1')
737 CheckDefAndScriptFailure2(['finddir(v:null)'], 'E1013: Argument 1: type mismatch, expected string but got special', 'E1174: String required for argument 1')
Bram Moolenaarfa984412021-03-25 22:15:28 +0100738 CheckDefExecFailure(['echo finddir("")'], 'E1175:')
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +0200739 CheckDefAndScriptFailure2(['finddir("a", [])'], 'E1013: Argument 2: type mismatch, expected string but got list<unknown>', 'E730: Using a List as a String')
740 CheckDefAndScriptFailure2(['finddir("a", "b", "c")'], 'E1013: Argument 3: type mismatch, expected number but got string', 'E1030: Using a String as a Number')
Bram Moolenaar7bb4e742020-12-09 12:41:50 +0100741enddef
742
743def Test_findfile()
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +0200744 CheckDefExecFailure(['findfile(true)'], 'E1013: Argument 1: type mismatch, expected string but got bool')
745 CheckDefExecFailure(['findfile(v:null)'], 'E1013: Argument 1: type mismatch, expected string but got special')
746 CheckDefExecFailure(['findfile("")'], 'E1175:')
747 CheckDefAndScriptFailure2(['findfile("a", [])'], 'E1013: Argument 2: type mismatch, expected string but got list<unknown>', 'E730: Using a List as a String')
748 CheckDefAndScriptFailure2(['findfile("a", "b", "c")'], 'E1013: Argument 3: type mismatch, expected number but got string', 'E1030: Using a String as a Number')
Bram Moolenaar7bb4e742020-12-09 12:41:50 +0100749enddef
750
Bram Moolenaar3b690062021-02-01 20:14:51 +0100751def Test_flattennew()
752 var lines =<< trim END
753 var l = [1, [2, [3, 4]], 5]
754 call assert_equal([1, 2, 3, 4, 5], flattennew(l))
755 call assert_equal([1, [2, [3, 4]], 5], l)
756
757 call assert_equal([1, 2, [3, 4], 5], flattennew(l, 1))
758 call assert_equal([1, [2, [3, 4]], 5], l)
759 END
760 CheckDefAndScriptSuccess(lines)
761
762 lines =<< trim END
763 echo flatten([1, 2, 3])
764 END
765 CheckDefAndScriptFailure(lines, 'E1158:')
766enddef
767
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +0200768" Test for float functions argument type
769def Test_float_funcs_args()
770 CheckFeature float
771
772 # acos()
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +0200773 CheckDefFailure(['acos("a")'], 'E1013: Argument 1: type mismatch, expected number but got string')
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +0200774 # asin()
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +0200775 CheckDefFailure(['asin("a")'], 'E1013: Argument 1: type mismatch, expected number but got string')
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +0200776 # atan()
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +0200777 CheckDefFailure(['atan("a")'], 'E1013: Argument 1: type mismatch, expected number but got string')
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +0200778 # atan2()
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +0200779 CheckDefFailure(['atan2("a", 1.1)'], 'E1013: Argument 1: type mismatch, expected number but got string')
780 CheckDefFailure(['atan2(1.2, "a")'], 'E1013: Argument 2: type mismatch, expected number but got string')
781 CheckDefFailure(['atan2(1.2)'], 'E119:')
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +0200782 # ceil()
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +0200783 CheckDefFailure(['ceil("a")'], 'E1013: Argument 1: type mismatch, expected number but got string')
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +0200784 # cos()
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +0200785 CheckDefFailure(['cos("a")'], 'E1013: Argument 1: type mismatch, expected number but got string')
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +0200786 # cosh()
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +0200787 CheckDefFailure(['cosh("a")'], 'E1013: Argument 1: type mismatch, expected number but got string')
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +0200788 # exp()
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +0200789 CheckDefFailure(['exp("a")'], 'E1013: Argument 1: type mismatch, expected number but got string')
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +0200790 # float2nr()
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +0200791 CheckDefFailure(['float2nr("a")'], 'E1013: Argument 1: type mismatch, expected number but got string')
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +0200792 # floor()
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +0200793 CheckDefFailure(['floor("a")'], 'E1013: Argument 1: type mismatch, expected number but got string')
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +0200794 # fmod()
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +0200795 CheckDefFailure(['fmod(1.1, "a")'], 'E1013: Argument 2: type mismatch, expected number but got string')
796 CheckDefFailure(['fmod("a", 1.1)'], 'E1013: Argument 1: type mismatch, expected number but got string')
797 CheckDefFailure(['fmod(1.1)'], 'E119:')
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +0200798 # isinf()
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +0200799 CheckDefFailure(['isinf("a")'], 'E1013: Argument 1: type mismatch, expected number but got string')
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +0200800 # isnan()
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +0200801 CheckDefFailure(['isnan("a")'], 'E1013: Argument 1: type mismatch, expected number but got string')
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +0200802 # log()
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +0200803 CheckDefFailure(['log("a")'], 'E1013: Argument 1: type mismatch, expected number but got string')
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +0200804 # log10()
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +0200805 CheckDefFailure(['log10("a")'], 'E1013: Argument 1: type mismatch, expected number but got string')
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +0200806 # pow()
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +0200807 CheckDefFailure(['pow("a", 1.1)'], 'E1013: Argument 1: type mismatch, expected number but got string')
808 CheckDefFailure(['pow(1.1, "a")'], 'E1013: Argument 2: type mismatch, expected number but got string')
809 CheckDefFailure(['pow(1.1)'], 'E119:')
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +0200810 # round()
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +0200811 CheckDefFailure(['round("a")'], 'E1013: Argument 1: type mismatch, expected number but got string')
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +0200812 # sin()
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +0200813 CheckDefFailure(['sin("a")'], 'E1013: Argument 1: type mismatch, expected number but got string')
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +0200814 # sinh()
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +0200815 CheckDefFailure(['sinh("a")'], 'E1013: Argument 1: type mismatch, expected number but got string')
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +0200816 # sqrt()
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +0200817 CheckDefFailure(['sqrt("a")'], 'E1013: Argument 1: type mismatch, expected number but got string')
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +0200818 # tan()
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +0200819 CheckDefFailure(['tan("a")'], 'E1013: Argument 1: type mismatch, expected number but got string')
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +0200820 # tanh()
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +0200821 CheckDefFailure(['tanh("a")'], 'E1013: Argument 1: type mismatch, expected number but got string')
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +0200822 # trunc()
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +0200823 CheckDefFailure(['trunc("a")'], 'E1013: Argument 1: type mismatch, expected number but got string')
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +0200824enddef
825
Yegappan Lakshmanana26f56f2021-07-03 11:58:12 +0200826def Test_fnameescape()
827 CheckDefFailure(['fnameescape(10)'], 'E1013: Argument 1: type mismatch, expected string but got number')
828 assert_equal('\+a\%b\|', fnameescape('+a%b|'))
829enddef
830
Bram Moolenaar7bb4e742020-12-09 12:41:50 +0100831def Test_fnamemodify()
Bram Moolenaar2a9d5d32020-12-12 18:58:40 +0100832 CheckDefSuccess(['echo fnamemodify(test_null_string(), ":p")'])
833 CheckDefSuccess(['echo fnamemodify("", ":p")'])
834 CheckDefSuccess(['echo fnamemodify("file", test_null_string())'])
835 CheckDefSuccess(['echo fnamemodify("file", "")'])
836
Yegappan Lakshmanana26f56f2021-07-03 11:58:12 +0200837 CheckDefExecFailure(['echo fnamemodify(true, ":p")'], 'E1013: Argument 1: type mismatch, expected string but got bool')
838 CheckDefExecFailure(['echo fnamemodify(v:null, ":p")'], 'E1013: Argument 1: type mismatch, expected string but got special')
839 CheckDefExecFailure(['echo fnamemodify("file", true)'], 'E1013: Argument 2: type mismatch, expected string but got bool')
Bram Moolenaar7bb4e742020-12-09 12:41:50 +0100840enddef
841
Bram Moolenaar2e5910b2021-02-03 17:41:24 +0100842def Wrong_dict_key_type(items: list<number>): list<number>
843 return filter(items, (_, val) => get({[val]: 1}, 'x'))
844enddef
845
Bram Moolenaar94738d82020-10-21 14:25:07 +0200846def Test_filter_wrong_dict_key_type()
Bram Moolenaar2e5910b2021-02-03 17:41:24 +0100847 assert_fails('Wrong_dict_key_type([1, v:null, 3])', 'E1013:')
Bram Moolenaar94738d82020-10-21 14:25:07 +0200848enddef
849
850def Test_filter_return_type()
Bram Moolenaarbb8a7ce2021-04-10 20:10:26 +0200851 var l = filter([1, 2, 3], (_, _) => 1)
Bram Moolenaar94738d82020-10-21 14:25:07 +0200852 var res = 0
853 for n in l
854 res += n
855 endfor
856 res->assert_equal(6)
857enddef
858
Bram Moolenaarfc0e8f52020-12-25 20:24:51 +0100859def Test_filter_missing_argument()
860 var dict = {aa: [1], ab: [2], ac: [3], de: [4]}
Bram Moolenaarbb8a7ce2021-04-10 20:10:26 +0200861 var res = dict->filter((k, _) => k =~ 'a' && k !~ 'b')
Bram Moolenaarfc0e8f52020-12-25 20:24:51 +0100862 res->assert_equal({aa: [1], ac: [3]})
863enddef
Bram Moolenaar94738d82020-10-21 14:25:07 +0200864
Yegappan Lakshmanana26f56f2021-07-03 11:58:12 +0200865def Test_foldclosed()
866 CheckDefFailure(['foldclosed(function("min"))'], 'E1013: Argument 1: type mismatch, expected string but got func(...): any')
867 assert_equal(-1, foldclosed(1))
868 assert_equal(-1, foldclosed('$'))
869enddef
870
871def Test_foldclosedend()
872 CheckDefFailure(['foldclosedend(true)'], 'E1013: Argument 1: type mismatch, expected string but got bool')
873 assert_equal(-1, foldclosedend(1))
874 assert_equal(-1, foldclosedend('w0'))
875enddef
876
877def Test_foldlevel()
878 CheckDefFailure(['foldlevel(0z10)'], 'E1013: Argument 1: type mismatch, expected string but got blob')
879 assert_equal(0, foldlevel(1))
880 assert_equal(0, foldlevel('.'))
881enddef
882
883def Test_foldtextresult()
884 CheckDefFailure(['foldtextresult(1.1)'], 'E1013: Argument 1: type mismatch, expected string but got float')
885 assert_equal('', foldtextresult(1))
886 assert_equal('', foldtextresult('.'))
887enddef
888
Bram Moolenaar7d840e92021-05-26 21:10:11 +0200889def Test_fullcommand()
890 assert_equal('next', fullcommand('n'))
891 assert_equal('noremap', fullcommand('no'))
892 assert_equal('noremap', fullcommand('nor'))
893 assert_equal('normal', fullcommand('norm'))
894
895 assert_equal('', fullcommand('k'))
896 assert_equal('keepmarks', fullcommand('ke'))
897 assert_equal('keepmarks', fullcommand('kee'))
898 assert_equal('keepmarks', fullcommand('keep'))
899 assert_equal('keepjumps', fullcommand('keepj'))
900
901 assert_equal('dlist', fullcommand('dl'))
902 assert_equal('', fullcommand('dp'))
903 assert_equal('delete', fullcommand('del'))
904 assert_equal('', fullcommand('dell'))
905 assert_equal('', fullcommand('delp'))
906
907 assert_equal('srewind', fullcommand('sre'))
908 assert_equal('scriptnames', fullcommand('scr'))
909 assert_equal('', fullcommand('scg'))
910enddef
911
Bram Moolenaar94738d82020-10-21 14:25:07 +0200912def Test_garbagecollect()
913 garbagecollect(true)
914enddef
915
916def Test_getbufinfo()
917 var bufinfo = getbufinfo(bufnr())
918 getbufinfo('%')->assert_equal(bufinfo)
919
920 edit Xtestfile1
921 hide edit Xtestfile2
922 hide enew
Bram Moolenaare0de1712020-12-02 17:36:54 +0100923 getbufinfo({bufloaded: true, buflisted: true, bufmodified: false})
Bram Moolenaar94738d82020-10-21 14:25:07 +0200924 ->len()->assert_equal(3)
925 bwipe Xtestfile1 Xtestfile2
926enddef
927
928def Test_getbufline()
929 e SomeFile
930 var buf = bufnr()
931 e #
932 var lines = ['aaa', 'bbb', 'ccc']
933 setbufline(buf, 1, lines)
934 getbufline('#', 1, '$')->assert_equal(lines)
Bram Moolenaare6e70a12020-10-22 18:23:38 +0200935 getbufline(-1, '$', '$')->assert_equal([])
936 getbufline(-1, 1, '$')->assert_equal([])
Bram Moolenaar94738d82020-10-21 14:25:07 +0200937
938 bwipe!
939enddef
940
941def Test_getchangelist()
942 new
943 setline(1, 'some text')
944 var changelist = bufnr()->getchangelist()
945 getchangelist('%')->assert_equal(changelist)
946 bwipe!
947enddef
948
949def Test_getchar()
950 while getchar(0)
951 endwhile
952 getchar(true)->assert_equal(0)
953enddef
954
Bram Moolenaar7ad67d12021-03-10 16:08:26 +0100955def Test_getenv()
956 if getenv('does-not_exist') == ''
957 assert_report('getenv() should return null')
958 endif
959 if getenv('does-not_exist') == null
960 else
961 assert_report('getenv() should return null')
962 endif
963 $SOMEENVVAR = 'some'
964 assert_equal('some', getenv('SOMEENVVAR'))
965 unlet $SOMEENVVAR
966enddef
967
Bram Moolenaar94738d82020-10-21 14:25:07 +0200968def Test_getcompletion()
969 set wildignore=*.vim,*~
970 var l = getcompletion('run', 'file', true)
971 l->assert_equal([])
972 set wildignore&
973enddef
974
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +0200975def Test_getcurpos()
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +0200976 CheckDefFailure(['getcursorcharpos("x")'], 'E1013: Argument 1: type mismatch, expected number but got string')
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +0200977enddef
978
979def Test_getcursorcharpos()
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +0200980 CheckDefFailure(['getcursorcharpos("x")'], 'E1013: Argument 1: type mismatch, expected number but got string')
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +0200981enddef
982
983def Test_getcwd()
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +0200984 CheckDefFailure(['getcwd("x")'], 'E1013: Argument 1: type mismatch, expected number but got string')
985 CheckDefFailure(['getcwd("x", 1)'], 'E1013: Argument 1: type mismatch, expected number but got string')
986 CheckDefFailure(['getcwd(1, "x")'], 'E1013: Argument 2: type mismatch, expected number but got string')
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +0200987enddef
988
Bram Moolenaar94738d82020-10-21 14:25:07 +0200989def Test_getloclist_return_type()
990 var l = getloclist(1)
991 l->assert_equal([])
992
Bram Moolenaare0de1712020-12-02 17:36:54 +0100993 var d = getloclist(1, {items: 0})
994 d->assert_equal({items: []})
Bram Moolenaar94738d82020-10-21 14:25:07 +0200995enddef
996
Yegappan Lakshmanana26f56f2021-07-03 11:58:12 +0200997def Test_getfontname()
998 CheckDefFailure(['getfontname(10)'], 'E1013: Argument 1: type mismatch, expected string but got number')
999enddef
1000
Bram Moolenaar7bb4e742020-12-09 12:41:50 +01001001def Test_getfperm()
Bram Moolenaar2a9d5d32020-12-12 18:58:40 +01001002 assert_equal('', getfperm(""))
1003 assert_equal('', getfperm(test_null_string()))
1004
Yegappan Lakshmanana26f56f2021-07-03 11:58:12 +02001005 CheckDefExecFailure(['echo getfperm(true)'], 'E1013:')
1006 CheckDefExecFailure(['echo getfperm(v:null)'], 'E1013:')
Bram Moolenaar7bb4e742020-12-09 12:41:50 +01001007enddef
1008
1009def Test_getfsize()
Bram Moolenaar2a9d5d32020-12-12 18:58:40 +01001010 assert_equal(-1, getfsize(""))
1011 assert_equal(-1, getfsize(test_null_string()))
1012
Yegappan Lakshmanana26f56f2021-07-03 11:58:12 +02001013 CheckDefExecFailure(['echo getfsize(true)'], 'E1013:')
1014 CheckDefExecFailure(['echo getfsize(v:null)'], 'E1013:')
Bram Moolenaar7bb4e742020-12-09 12:41:50 +01001015enddef
1016
1017def Test_getftime()
Bram Moolenaar2a9d5d32020-12-12 18:58:40 +01001018 assert_equal(-1, getftime(""))
1019 assert_equal(-1, getftime(test_null_string()))
1020
Yegappan Lakshmanana26f56f2021-07-03 11:58:12 +02001021 CheckDefExecFailure(['echo getftime(true)'], 'E1013:')
1022 CheckDefExecFailure(['echo getftime(v:null)'], 'E1013:')
Bram Moolenaar7bb4e742020-12-09 12:41:50 +01001023enddef
1024
1025def Test_getftype()
Bram Moolenaar2a9d5d32020-12-12 18:58:40 +01001026 assert_equal('', getftype(""))
1027 assert_equal('', getftype(test_null_string()))
1028
Yegappan Lakshmanana26f56f2021-07-03 11:58:12 +02001029 CheckDefExecFailure(['echo getftype(true)'], 'E1013:')
1030 CheckDefExecFailure(['echo getftype(v:null)'], 'E1013:')
Bram Moolenaar7bb4e742020-12-09 12:41:50 +01001031enddef
1032
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +02001033def Test_getjumplist()
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +02001034 CheckDefFailure(['getjumplist("x")'], 'E1013: Argument 1: type mismatch, expected number but got string')
1035 CheckDefFailure(['getjumplist("x", 1)'], 'E1013: Argument 1: type mismatch, expected number but got string')
1036 CheckDefFailure(['getjumplist(1, "x")'], 'E1013: Argument 2: type mismatch, expected number but got string')
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +02001037enddef
1038
Yegappan Lakshmanana26f56f2021-07-03 11:58:12 +02001039def Test_getmarklist()
1040 CheckDefFailure(['getmarklist([])'], 'E1013: Argument 1: type mismatch, expected string but got list<unknown>')
1041 assert_equal([], getmarklist(10000))
1042 assert_fails('getmarklist("a%b@#")', 'E94:')
1043enddef
1044
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +02001045def Test_getmatches()
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +02001046 CheckDefFailure(['getmatches("x")'], 'E1013: Argument 1: type mismatch, expected number but got string')
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +02001047enddef
1048
Yegappan Lakshmanana26f56f2021-07-03 11:58:12 +02001049def Test_getpos()
1050 CheckDefFailure(['getpos(10)'], 'E1013: Argument 1: type mismatch, expected string but got number')
1051 assert_equal([0, 1, 1, 0], getpos('.'))
1052 assert_equal([0, 0, 0, 0], getpos('a'))
1053enddef
1054
1055def Test_getqflist()
1056 CheckDefFailure(['getqflist([])'], 'E1013: Argument 1: type mismatch, expected dict<any> but got list<unknown>')
1057 call assert_equal({}, getqflist({}))
1058enddef
1059
Bram Moolenaar94738d82020-10-21 14:25:07 +02001060def Test_getqflist_return_type()
1061 var l = getqflist()
1062 l->assert_equal([])
1063
Bram Moolenaare0de1712020-12-02 17:36:54 +01001064 var d = getqflist({items: 0})
1065 d->assert_equal({items: []})
Bram Moolenaar94738d82020-10-21 14:25:07 +02001066enddef
1067
1068def Test_getreg()
1069 var lines = ['aaa', 'bbb', 'ccc']
1070 setreg('a', lines)
1071 getreg('a', true, true)->assert_equal(lines)
Bram Moolenaar418a29f2021-02-10 22:23:41 +01001072 assert_fails('getreg("ab")', 'E1162:')
Bram Moolenaar94738d82020-10-21 14:25:07 +02001073enddef
1074
1075def Test_getreg_return_type()
1076 var s1: string = getreg('"')
1077 var s2: string = getreg('"', 1)
1078 var s3: list<string> = getreg('"', 1, 1)
1079enddef
1080
Bram Moolenaar418a29f2021-02-10 22:23:41 +01001081def Test_getreginfo()
1082 var text = 'abc'
1083 setreg('a', text)
1084 getreginfo('a')->assert_equal({regcontents: [text], regtype: 'v', isunnamed: false})
1085 assert_fails('getreginfo("ab")', 'E1162:')
1086enddef
1087
1088def Test_getregtype()
1089 var lines = ['aaa', 'bbb', 'ccc']
1090 setreg('a', lines)
1091 getregtype('a')->assert_equal('V')
1092 assert_fails('getregtype("ab")', 'E1162:')
1093enddef
1094
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +02001095def Test_gettabinfo()
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +02001096 CheckDefFailure(['gettabinfo("x")'], 'E1013: Argument 1: type mismatch, expected number but got string')
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +02001097enddef
1098
1099def Test_gettagstack()
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +02001100 CheckDefFailure(['gettagstack("x")'], 'E1013: Argument 1: type mismatch, expected number but got string')
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +02001101enddef
1102
Yegappan Lakshmanana26f56f2021-07-03 11:58:12 +02001103def Test_gettext()
1104 CheckDefFailure(['gettext(10)'], 'E1013: Argument 1: type mismatch, expected string but got number')
1105 assert_equal('abc', gettext("abc"))
1106enddef
1107
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +02001108def Test_getwininfo()
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +02001109 CheckDefFailure(['getwininfo("x")'], 'E1013: Argument 1: type mismatch, expected number but got string')
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +02001110enddef
1111
1112def Test_getwinpos()
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +02001113 CheckDefFailure(['getwinpos("x")'], 'E1013: Argument 1: type mismatch, expected number but got string')
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +02001114enddef
1115
Bram Moolenaar94738d82020-10-21 14:25:07 +02001116def Test_glob()
1117 glob('runtest.vim', true, true, true)->assert_equal(['runtest.vim'])
1118enddef
1119
Yegappan Lakshmanana26f56f2021-07-03 11:58:12 +02001120def Test_glob2regpat()
1121 CheckDefFailure(['glob2regpat(null)'], 'E1013: Argument 1: type mismatch, expected string but got special')
1122 assert_equal('^$', glob2regpat(''))
1123enddef
1124
Bram Moolenaar94738d82020-10-21 14:25:07 +02001125def Test_globpath()
1126 globpath('.', 'runtest.vim', true, true, true)->assert_equal(['./runtest.vim'])
1127enddef
1128
1129def Test_has()
1130 has('eval', true)->assert_equal(1)
1131enddef
1132
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +02001133def Test_has_key()
Bram Moolenaar1aeddeb2021-07-11 14:55:49 +02001134 var d = {123: 'xx'}
1135 assert_true(has_key(d, '123'))
1136 assert_true(has_key(d, 123))
1137 assert_false(has_key(d, 'x'))
1138 assert_false(has_key(d, 99))
1139
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +02001140 CheckDefAndScriptFailure2(['has_key([1, 2], "k")'], 'E1013: Argument 1: type mismatch, expected dict<any> but got list<number>', 'E715: Dictionary required')
1141 CheckDefAndScriptFailure2(['has_key({"a": 10}, ["a"])'], 'E1013: Argument 2: type mismatch, expected string but got list<string>', 'E730: Using a List as a String')
1142enddef
1143
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +02001144def Test_haslocaldir()
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +02001145 CheckDefFailure(['haslocaldir("x")'], 'E1013: Argument 1: type mismatch, expected number but got string')
1146 CheckDefFailure(['haslocaldir("x", 1)'], 'E1013: Argument 1: type mismatch, expected number but got string')
1147 CheckDefFailure(['haslocaldir(1, "x")'], 'E1013: Argument 2: type mismatch, expected number but got string')
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +02001148enddef
1149
Bram Moolenaar94738d82020-10-21 14:25:07 +02001150def Test_hasmapto()
1151 hasmapto('foobar', 'i', true)->assert_equal(0)
1152 iabbrev foo foobar
1153 hasmapto('foobar', 'i', true)->assert_equal(1)
1154 iunabbrev foo
1155enddef
1156
Yegappan Lakshmanana26f56f2021-07-03 11:58:12 +02001157def Test_histadd()
1158 CheckDefFailure(['histadd(1, "x")'], 'E1013: Argument 1: type mismatch, expected string but got number')
1159 CheckDefFailure(['histadd(":", 10)'], 'E1013: Argument 2: type mismatch, expected string but got number')
1160 histadd("search", 'skyblue')
1161 assert_equal('skyblue', histget('/', -1))
1162enddef
1163
1164def Test_histnr()
1165 CheckDefFailure(['histnr(10)'], 'E1013: Argument 1: type mismatch, expected string but got number')
1166 assert_equal(-1, histnr('abc'))
1167enddef
1168
1169def Test_hlID()
1170 CheckDefFailure(['hlID(10)'], 'E1013: Argument 1: type mismatch, expected string but got number')
1171 assert_equal(0, hlID('NonExistingHighlight'))
1172enddef
1173
1174def Test_hlexists()
1175 CheckDefFailure(['hlexists([])'], 'E1013: Argument 1: type mismatch, expected string but got list<unknown>')
1176 assert_equal(0, hlexists('NonExistingHighlight'))
1177enddef
1178
1179def Test_iconv()
1180 CheckDefFailure(['iconv(1, "from", "to")'], 'E1013: Argument 1: type mismatch, expected string but got number')
1181 CheckDefFailure(['iconv("abc", 10, "to")'], 'E1013: Argument 2: type mismatch, expected string but got number')
1182 CheckDefFailure(['iconv("abc", "from", 20)'], 'E1013: Argument 3: type mismatch, expected string but got number')
1183 assert_equal('abc', iconv('abc', 'fromenc', 'toenc'))
1184enddef
1185
Bram Moolenaar94738d82020-10-21 14:25:07 +02001186def Test_index()
1187 index(['a', 'b', 'a', 'B'], 'b', 2, true)->assert_equal(3)
1188enddef
1189
Yegappan Lakshmanana26f56f2021-07-03 11:58:12 +02001190def Test_inputlist()
1191 CheckDefFailure(['inputlist(10)'], 'E1013: Argument 1: type mismatch, expected list<string> but got number')
1192 CheckDefFailure(['inputlist("abc")'], 'E1013: Argument 1: type mismatch, expected list<string> but got string')
1193 CheckDefFailure(['inputlist([1, 2, 3])'], 'E1013: Argument 1: type mismatch, expected list<string> but got list<number>')
1194 feedkeys("2\<CR>", 't')
1195 var r: number = inputlist(['a', 'b', 'c'])
1196 assert_equal(2, r)
1197enddef
1198
1199def Test_inputsecret()
1200 CheckDefFailure(['inputsecret(10)'], 'E1013: Argument 1: type mismatch, expected string but got number')
1201 CheckDefFailure(['inputsecret("Pass:", 20)'], 'E1013: Argument 2: type mismatch, expected string but got number')
1202 feedkeys("\<CR>", 't')
1203 var ans: string = inputsecret('Pass:', '123')
1204 assert_equal('123', ans)
1205enddef
1206
Bram Moolenaar193f6202020-11-16 20:08:35 +01001207let s:number_one = 1
1208let s:number_two = 2
1209let s:string_keep = 'keep'
1210
Bram Moolenaarca174532020-10-21 16:42:22 +02001211def Test_insert()
Bram Moolenaar94738d82020-10-21 14:25:07 +02001212 var l = insert([2, 1], 3)
1213 var res = 0
1214 for n in l
1215 res += n
1216 endfor
1217 res->assert_equal(6)
Bram Moolenaarca174532020-10-21 16:42:22 +02001218
Yegappan Lakshmanan34fcb692021-05-25 20:14:00 +02001219 var m: any = []
1220 insert(m, 4)
1221 call assert_equal([4], m)
1222 extend(m, [6], 0)
1223 call assert_equal([6, 4], m)
1224
Bram Moolenaar39211cb2021-04-18 15:48:04 +02001225 var lines =<< trim END
1226 insert(test_null_list(), 123)
1227 END
1228 CheckDefExecAndScriptFailure(lines, 'E1130:', 1)
1229
1230 lines =<< trim END
1231 insert(test_null_blob(), 123)
1232 END
1233 CheckDefExecAndScriptFailure(lines, 'E1131:', 1)
1234
Bram Moolenaarca174532020-10-21 16:42:22 +02001235 assert_equal([1, 2, 3], insert([2, 3], 1))
Bram Moolenaar193f6202020-11-16 20:08:35 +01001236 assert_equal([1, 2, 3], insert([2, 3], s:number_one))
Bram Moolenaarca174532020-10-21 16:42:22 +02001237 assert_equal([1, 2, 3], insert([1, 2], 3, 2))
Bram Moolenaar193f6202020-11-16 20:08:35 +01001238 assert_equal([1, 2, 3], insert([1, 2], 3, s:number_two))
Bram Moolenaarca174532020-10-21 16:42:22 +02001239 assert_equal(['a', 'b', 'c'], insert(['b', 'c'], 'a'))
1240 assert_equal(0z1234, insert(0z34, 0x12))
Bram Moolenaar193f6202020-11-16 20:08:35 +01001241
Yegappan Lakshmanan34fcb692021-05-25 20:14:00 +02001242 CheckDefFailure(['insert("a", 1)'], 'E1013: Argument 1: type mismatch, expected list<any> but got string', 1)
Bram Moolenaarca174532020-10-21 16:42:22 +02001243 CheckDefFailure(['insert([2, 3], "a")'], 'E1013: Argument 2: type mismatch, expected number but got string', 1)
1244 CheckDefFailure(['insert([2, 3], 1, "x")'], 'E1013: Argument 3: type mismatch, expected number but got string', 1)
Bram Moolenaar94738d82020-10-21 14:25:07 +02001245enddef
1246
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +02001247def Test_invert()
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +02001248 CheckDefFailure(['invert("x")'], 'E1013: Argument 1: type mismatch, expected number but got string')
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +02001249enddef
1250
Yegappan Lakshmanana26f56f2021-07-03 11:58:12 +02001251def Test_isdirectory()
1252 CheckDefFailure(['isdirectory(1.1)'], 'E1013: Argument 1: type mismatch, expected string but got float')
1253 assert_false(isdirectory('NonExistingDir'))
1254enddef
1255
1256def Test_items()
1257 CheckDefFailure(['[]->items()'], 'E1013: Argument 1: type mismatch, expected dict<any> but got list<unknown>')
1258 assert_equal([['a', 10], ['b', 20]], {'a': 10, 'b': 20}->items())
1259 assert_equal([], {}->items())
1260enddef
1261
1262def Test_js_decode()
1263 CheckDefFailure(['js_decode(10)'], 'E1013: Argument 1: type mismatch, expected string but got number')
1264 assert_equal([1, 2], js_decode('[1,2]'))
1265enddef
1266
1267def Test_json_decode()
1268 CheckDefFailure(['json_decode(true)'], 'E1013: Argument 1: type mismatch, expected string but got bool')
1269 assert_equal(1.0, json_decode('1.0'))
1270enddef
1271
1272def Test_keys()
1273 CheckDefFailure(['keys([])'], 'E1013: Argument 1: type mismatch, expected dict<any> but got list<unknown>')
1274 assert_equal(['a'], {a: 'v'}->keys())
1275 assert_equal([], {}->keys())
1276enddef
1277
Bram Moolenaar94738d82020-10-21 14:25:07 +02001278def Test_keys_return_type()
Bram Moolenaare0de1712020-12-02 17:36:54 +01001279 const var: list<string> = {a: 1, b: 2}->keys()
Bram Moolenaar94738d82020-10-21 14:25:07 +02001280 var->assert_equal(['a', 'b'])
1281enddef
1282
Bram Moolenaarc5809432021-03-27 21:23:30 +01001283def Test_line()
Yegappan Lakshmanana26f56f2021-07-03 11:58:12 +02001284 assert_fails('line(true)', 'E1174:')
1285enddef
1286
1287def Test_line2byte()
1288 CheckDefFailure(['line2byte(true)'], 'E1013: Argument 1: type mismatch, expected string but got bool')
1289 assert_equal(-1, line2byte(1))
1290 assert_equal(-1, line2byte(10000))
1291enddef
1292
1293def Test_lispindent()
1294 CheckDefFailure(['lispindent({})'], 'E1013: Argument 1: type mismatch, expected string but got dict<unknown>')
1295 assert_equal(0, lispindent(1))
Bram Moolenaarc5809432021-03-27 21:23:30 +01001296enddef
1297
Bram Moolenaar94738d82020-10-21 14:25:07 +02001298def Test_list2str_str2list_utf8()
1299 var s = "\u3042\u3044"
1300 var l = [0x3042, 0x3044]
1301 str2list(s, true)->assert_equal(l)
1302 list2str(l, true)->assert_equal(s)
1303enddef
1304
1305def SID(): number
1306 return expand('<SID>')
1307 ->matchstr('<SNR>\zs\d\+\ze_$')
1308 ->str2nr()
1309enddef
1310
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +02001311def Test_listener_flush()
1312 CheckDefAndScriptFailure2(['listener_flush([1])'], 'E1013: Argument 1: type mismatch, expected string but got list<number>', 'E730: Using a List as a String')
1313enddef
1314
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +02001315def Test_listener_remove()
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +02001316 CheckDefAndScriptFailure2(['listener_remove("x")'], 'E1013: Argument 1: type mismatch, expected number but got string', 'E1030: Using a String as a Number')
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +02001317enddef
1318
Bram Moolenaar70250fb2021-01-16 19:01:53 +01001319def Test_map_function_arg()
1320 var lines =<< trim END
1321 def MapOne(i: number, v: string): string
1322 return i .. ':' .. v
1323 enddef
1324 var l = ['a', 'b', 'c']
1325 map(l, MapOne)
1326 assert_equal(['0:a', '1:b', '2:c'], l)
1327 END
1328 CheckDefAndScriptSuccess(lines)
Bram Moolenaar8da6d6d2021-06-05 18:15:09 +02001329
1330 lines =<< trim END
1331 range(3)->map((a, b, c) => a + b + c)
1332 END
1333 CheckDefExecAndScriptFailure(lines, 'E1190: One argument too few')
1334 lines =<< trim END
1335 range(3)->map((a, b, c, d) => a + b + c + d)
1336 END
1337 CheckDefExecAndScriptFailure(lines, 'E1190: 2 arguments too few')
Bram Moolenaar70250fb2021-01-16 19:01:53 +01001338enddef
1339
1340def Test_map_item_type()
1341 var lines =<< trim END
1342 var l = ['a', 'b', 'c']
1343 map(l, (k, v) => k .. '/' .. v )
1344 assert_equal(['0/a', '1/b', '2/c'], l)
1345 END
1346 CheckDefAndScriptSuccess(lines)
1347
1348 lines =<< trim END
1349 var l: list<number> = [0]
1350 echo map(l, (_, v) => [])
1351 END
1352 CheckDefExecAndScriptFailure(lines, 'E1012: Type mismatch; expected number but got list<unknown>', 2)
1353
1354 lines =<< trim END
1355 var l: list<number> = range(2)
1356 echo map(l, (_, v) => [])
1357 END
1358 CheckDefExecAndScriptFailure(lines, 'E1012: Type mismatch; expected number but got list<unknown>', 2)
1359
1360 lines =<< trim END
1361 var d: dict<number> = {key: 0}
1362 echo map(d, (_, v) => [])
1363 END
1364 CheckDefExecAndScriptFailure(lines, 'E1012: Type mismatch; expected number but got list<unknown>', 2)
1365enddef
1366
Bram Moolenaar94738d82020-10-21 14:25:07 +02001367def Test_maparg()
1368 var lnum = str2nr(expand('<sflnum>'))
1369 map foo bar
Bram Moolenaare0de1712020-12-02 17:36:54 +01001370 maparg('foo', '', false, true)->assert_equal({
Bram Moolenaar94738d82020-10-21 14:25:07 +02001371 lnum: lnum + 1,
1372 script: 0,
1373 mode: ' ',
1374 silent: 0,
1375 noremap: 0,
1376 lhs: 'foo',
1377 lhsraw: 'foo',
1378 nowait: 0,
1379 expr: 0,
1380 sid: SID(),
1381 rhs: 'bar',
1382 buffer: 0})
1383 unmap foo
1384enddef
1385
1386def Test_mapcheck()
1387 iabbrev foo foobar
1388 mapcheck('foo', 'i', true)->assert_equal('foobar')
1389 iunabbrev foo
1390enddef
1391
1392def Test_maparg_mapset()
1393 nnoremap <F3> :echo "hit F3"<CR>
1394 var mapsave = maparg('<F3>', 'n', false, true)
1395 mapset('n', false, mapsave)
1396
1397 nunmap <F3>
1398enddef
1399
Bram Moolenaar027c4ab2021-02-21 16:20:18 +01001400def Test_map_failure()
1401 CheckFeature job
1402
1403 var lines =<< trim END
1404 vim9script
1405 writefile([], 'Xtmpfile')
1406 silent e Xtmpfile
1407 var d = {[bufnr('%')]: {a: 0}}
1408 au BufReadPost * Func()
1409 def Func()
1410 if d->has_key('')
1411 endif
1412 eval d[expand('<abuf>')]->mapnew((_, v: dict<job>) => 0)
1413 enddef
1414 e
1415 END
1416 CheckScriptFailure(lines, 'E1013:')
1417 au! BufReadPost
1418 delete('Xtmpfile')
1419enddef
1420
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +02001421def Test_matcharg()
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +02001422 CheckDefFailure(['matcharg("x")'], 'E1013: Argument 1: type mismatch, expected number but got string')
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +02001423enddef
1424
1425def Test_matchdelete()
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +02001426 CheckDefFailure(['matchdelete("x")'], 'E1013: Argument 1: type mismatch, expected number but got string')
1427 CheckDefFailure(['matchdelete("x", 1)'], 'E1013: Argument 1: type mismatch, expected number but got string')
1428 CheckDefFailure(['matchdelete(1, "x")'], 'E1013: Argument 2: type mismatch, expected number but got string')
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +02001429enddef
1430
Bram Moolenaar9ae37052021-01-22 22:31:10 +01001431def Test_max()
1432 g:flag = true
1433 var l1: list<number> = g:flag
1434 ? [1, max([2, 3])]
1435 : [4, 5]
1436 assert_equal([1, 3], l1)
1437
1438 g:flag = false
1439 var l2: list<number> = g:flag
1440 ? [1, max([2, 3])]
1441 : [4, 5]
1442 assert_equal([4, 5], l2)
1443enddef
1444
Yegappan Lakshmanana26f56f2021-07-03 11:58:12 +02001445def Test_menu_info()
1446 CheckDefFailure(['menu_info(10)'], 'E1013: Argument 1: type mismatch, expected string but got number')
1447 CheckDefFailure(['menu_info(10, "n")'], 'E1013: Argument 1: type mismatch, expected string but got number')
1448 CheckDefFailure(['menu_info("File", 10)'], 'E1013: Argument 2: type mismatch, expected string but got number')
1449 assert_equal({}, menu_info('aMenu'))
1450enddef
1451
Bram Moolenaar9ae37052021-01-22 22:31:10 +01001452def Test_min()
1453 g:flag = true
1454 var l1: list<number> = g:flag
1455 ? [1, min([2, 3])]
1456 : [4, 5]
1457 assert_equal([1, 2], l1)
1458
1459 g:flag = false
1460 var l2: list<number> = g:flag
1461 ? [1, min([2, 3])]
1462 : [4, 5]
1463 assert_equal([4, 5], l2)
1464enddef
1465
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +02001466def Test_mkdir()
1467 CheckDefAndScriptFailure2(['mkdir(["a"])'], 'E1013: Argument 1: type mismatch, expected string but got list<string>', 'E730: Using a List as a String')
1468 CheckDefAndScriptFailure2(['mkdir("a", {})'], 'E1013: Argument 2: type mismatch, expected string but got dict<unknown>', 'E731: Using a Dictionary as a String')
1469 CheckDefAndScriptFailure2(['mkdir("a", "b", "c")'], 'E1013: Argument 3: type mismatch, expected number but got string', 'E1030: Using a String as a Number')
1470 delete('a', 'rf')
1471enddef
1472
Yegappan Lakshmanana26f56f2021-07-03 11:58:12 +02001473def Test_nextnonblank()
1474 CheckDefFailure(['nextnonblank(null)'], 'E1013: Argument 1: type mismatch, expected string but got special')
1475 assert_equal(0, nextnonblank(1))
1476enddef
1477
Bram Moolenaar94738d82020-10-21 14:25:07 +02001478def Test_nr2char()
1479 nr2char(97, true)->assert_equal('a')
1480enddef
1481
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +02001482def Test_or()
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +02001483 CheckDefFailure(['or("x", 0x2)'], 'E1013: Argument 1: type mismatch, expected number but got string')
1484 CheckDefFailure(['or(0x1, "x")'], 'E1013: Argument 2: type mismatch, expected number but got string')
1485enddef
1486
1487def Test_popup_locate()
1488 CheckDefAndScriptFailure2(['popup_locate("a", 20)'], 'E1013: Argument 1: type mismatch, expected number but got string', 'E1030: Using a String as a Number')
1489 CheckDefAndScriptFailure2(['popup_locate(10, "b")'], 'E1013: Argument 2: type mismatch, expected number but got string', 'E1030: Using a String as a Number')
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +02001490enddef
1491
Yegappan Lakshmanana26f56f2021-07-03 11:58:12 +02001492def Test_prevnonblank()
1493 CheckDefFailure(['prevnonblank(null)'], 'E1013: Argument 1: type mismatch, expected string but got special')
1494 assert_equal(0, prevnonblank(1))
1495enddef
1496
1497def Test_prompt_getprompt()
Dominique Pelle74509232021-07-03 19:27:37 +02001498 if has('channel')
1499 CheckDefFailure(['prompt_getprompt([])'], 'E1013: Argument 1: type mismatch, expected string but got list<unknown>')
1500 assert_equal('', prompt_getprompt('NonExistingBuf'))
1501 endif
Yegappan Lakshmanana26f56f2021-07-03 11:58:12 +02001502enddef
1503
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +02001504def Test_prop_find()
1505 CheckDefAndScriptFailure2(['prop_find([1, 2])'], 'E1013: Argument 1: type mismatch, expected dict<any> but got list<number>', 'E715: Dictionary required')
1506 CheckDefAndScriptFailure2(['prop_find([1, 2], "k")'], 'E1013: Argument 1: type mismatch, expected dict<any> but got list<number>', 'E715: Dictionary required')
1507 CheckDefAndScriptFailure2(['prop_find({"a": 10}, ["a"])'], 'E1013: Argument 2: type mismatch, expected string but got list<string>', 'E730: Using a List as a String')
1508enddef
1509
1510def Test_prop_type_add()
1511 CheckDefAndScriptFailure2(['prop_type_add({"a": 10}, "b")'], 'E1013: Argument 1: type mismatch, expected string but got dict<number>', 'E731: Using a Dictionary as a String')
1512 CheckDefAndScriptFailure2(['prop_type_add("a", "b")'], 'E1013: Argument 2: type mismatch, expected dict<any> but got string', 'E715: Dictionary required')
1513enddef
1514
1515def Test_prop_type_change()
1516 CheckDefAndScriptFailure2(['prop_type_change({"a": 10}, "b")'], 'E1013: Argument 1: type mismatch, expected string but got dict<number>', 'E731: Using a Dictionary as a String')
1517 CheckDefAndScriptFailure2(['prop_type_change("a", "b")'], 'E1013: Argument 2: type mismatch, expected dict<any> but got string', 'E715: Dictionary required')
1518enddef
1519
1520def Test_prop_type_delete()
1521 CheckDefAndScriptFailure2(['prop_type_delete({"a": 10})'], 'E1013: Argument 1: type mismatch, expected string but got dict<number>', 'E731: Using a Dictionary as a String')
1522 CheckDefAndScriptFailure2(['prop_type_delete({"a": 10}, "b")'], 'E1013: Argument 1: type mismatch, expected string but got dict<number>', 'E731: Using a Dictionary as a String')
1523 CheckDefAndScriptFailure2(['prop_type_delete("a", "b")'], 'E1013: Argument 2: type mismatch, expected dict<any> but got string', 'E715: Dictionary required')
1524enddef
1525
1526def Test_prop_type_get()
1527 CheckDefAndScriptFailure2(['prop_type_get({"a": 10})'], 'E1013: Argument 1: type mismatch, expected string but got dict<number>', 'E731: Using a Dictionary as a String')
1528 CheckDefAndScriptFailure2(['prop_type_get({"a": 10}, "b")'], 'E1013: Argument 1: type mismatch, expected string but got dict<number>', 'E731: Using a Dictionary as a String')
1529 CheckDefAndScriptFailure2(['prop_type_get("a", "b")'], 'E1013: Argument 2: type mismatch, expected dict<any> but got string', 'E715: Dictionary required')
1530enddef
1531
Yegappan Lakshmanana26f56f2021-07-03 11:58:12 +02001532def Test_rand()
1533 CheckDefFailure(['rand(10)'], 'E1013: Argument 1: type mismatch, expected list<number> but got number')
1534 CheckDefFailure(['rand(["a"])'], 'E1013: Argument 1: type mismatch, expected list<number> but got list<string>')
1535 assert_true(rand() >= 0)
1536 assert_true(rand(srand()) >= 0)
1537enddef
1538
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +02001539def Test_range()
1540 CheckDefAndScriptFailure2(['range("a")'], 'E1013: Argument 1: type mismatch, expected number but got string', 'E1030: Using a String as a Number')
1541 CheckDefAndScriptFailure2(['range(10, "b")'], 'E1013: Argument 2: type mismatch, expected number but got string', 'E1030: Using a String as a Number')
1542 CheckDefAndScriptFailure2(['range(10, 20, "c")'], 'E1013: Argument 3: type mismatch, expected number but got string', 'E1030: Using a String as a Number')
1543enddef
1544
Bram Moolenaar94738d82020-10-21 14:25:07 +02001545def Test_readdir()
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01001546 eval expand('sautest')->readdir((e) => e[0] !=# '.')
1547 eval expand('sautest')->readdirex((e) => e.name[0] !=# '.')
Bram Moolenaar94738d82020-10-21 14:25:07 +02001548enddef
1549
Bram Moolenaarc423ad72021-01-13 20:38:03 +01001550def Test_readblob()
1551 var blob = 0z12341234
1552 writefile(blob, 'Xreadblob')
1553 var read: blob = readblob('Xreadblob')
1554 assert_equal(blob, read)
1555
1556 var lines =<< trim END
1557 var read: list<string> = readblob('Xreadblob')
1558 END
1559 CheckDefAndScriptFailure(lines, 'E1012: Type mismatch; expected list<string> but got blob', 1)
1560 delete('Xreadblob')
1561enddef
1562
1563def Test_readfile()
1564 var text = ['aaa', 'bbb', 'ccc']
1565 writefile(text, 'Xreadfile')
1566 var read: list<string> = readfile('Xreadfile')
1567 assert_equal(text, read)
1568
1569 var lines =<< trim END
1570 var read: dict<string> = readfile('Xreadfile')
1571 END
1572 CheckDefAndScriptFailure(lines, 'E1012: Type mismatch; expected dict<string> but got list<string>', 1)
1573 delete('Xreadfile')
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +02001574
1575 CheckDefAndScriptFailure2(['readfile("a", 0z10)'], 'E1013: Argument 2: type mismatch, expected string but got blob', 'E976: Using a Blob as a String')
1576 CheckDefAndScriptFailure2(['readfile("a", "b", "c")'], 'E1013: Argument 3: type mismatch, expected number but got string', 'E1030: Using a String as a Number')
Bram Moolenaarc423ad72021-01-13 20:38:03 +01001577enddef
1578
Yegappan Lakshmanana26f56f2021-07-03 11:58:12 +02001579def Test_reltime()
1580 CheckDefFailure(['reltime("x")'], 'E1013: Argument 1: type mismatch, expected list<number> but got string')
1581 CheckDefFailure(['reltime(["x", "y"])'], 'E1013: Argument 1: type mismatch, expected list<number> but got list<string>')
1582 CheckDefFailure(['reltime([1, 2], 10)'], 'E1013: Argument 2: type mismatch, expected list<number> but got number')
1583 CheckDefFailure(['reltime([1, 2], ["a", "b"])'], 'E1013: Argument 2: type mismatch, expected list<number> but got list<string>')
1584 var start: list<any> = reltime()
1585 assert_true(type(reltime(start)) == v:t_list)
1586 var end: list<any> = reltime()
1587 assert_true(type(reltime(start, end)) == v:t_list)
1588enddef
1589
1590def Test_reltimefloat()
1591 CheckDefFailure(['reltimefloat("x")'], 'E1013: Argument 1: type mismatch, expected list<number> but got string')
1592 CheckDefFailure(['reltimefloat([1.1])'], 'E1013: Argument 1: type mismatch, expected list<number> but got list<float>')
1593 assert_true(type(reltimefloat(reltime())) == v:t_float)
1594enddef
1595
1596def Test_reltimestr()
1597 CheckDefFailure(['reltimestr(true)'], 'E1013: Argument 1: type mismatch, expected list<number> but got bool')
1598 CheckDefFailure(['reltimestr([true])'], 'E1013: Argument 1: type mismatch, expected list<number> but got list<bool>')
1599 assert_true(type(reltimestr(reltime())) == v:t_string)
1600enddef
1601
1602def Test_remote_foreground()
1603 CheckFeature clientserver
1604 # remote_foreground() doesn't fail on MS-Windows
1605 CheckNotMSWindows
Bram Moolenaard6fa7bd2021-07-05 14:10:04 +02001606 CheckEnv DISPLAY
1607
Yegappan Lakshmanana26f56f2021-07-03 11:58:12 +02001608 CheckDefFailure(['remote_foreground(10)'], 'E1013: Argument 1: type mismatch, expected string but got number')
1609 assert_fails('remote_foreground("NonExistingServer")', 'E241:')
1610enddef
1611
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +02001612def Test_remote_peek()
1613 CheckFeature clientserver
1614 CheckEnv DISPLAY
1615 CheckDefAndScriptFailure2(['remote_peek(0z10)'], 'E1013: Argument 1: type mismatch, expected string but got blob', 'E976: Using a Blob as a String')
1616 CheckDefAndScriptFailure2(['remote_peek("a5b6c7", [1])'], 'E1013: Argument 2: type mismatch, expected string but got list<number>', 'E573: Invalid server id used')
1617enddef
1618
Yegappan Lakshmanana26f56f2021-07-03 11:58:12 +02001619def Test_remote_startserver()
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +02001620 CheckFeature clientserver
1621 CheckEnv DISPLAY
Yegappan Lakshmanana26f56f2021-07-03 11:58:12 +02001622 CheckDefFailure(['remote_startserver({})'], 'E1013: Argument 1: type mismatch, expected string but got dict<unknown>')
1623enddef
1624
Bram Moolenaar94738d82020-10-21 14:25:07 +02001625def Test_remove_return_type()
Bram Moolenaare0de1712020-12-02 17:36:54 +01001626 var l = remove({one: [1, 2], two: [3, 4]}, 'one')
Bram Moolenaar94738d82020-10-21 14:25:07 +02001627 var res = 0
1628 for n in l
1629 res += n
1630 endfor
1631 res->assert_equal(3)
1632enddef
1633
Yegappan Lakshmanana26f56f2021-07-03 11:58:12 +02001634def Test_rename()
1635 CheckDefFailure(['rename(1, "b")'], 'E1013: Argument 1: type mismatch, expected string but got number')
1636 CheckDefFailure(['rename("a", 2)'], 'E1013: Argument 2: type mismatch, expected string but got number')
1637enddef
1638
1639def Test_resolve()
1640 CheckDefFailure(['resolve([])'], 'E1013: Argument 1: type mismatch, expected string but got list<unknown>')
1641 assert_equal('SomeFile', resolve('SomeFile'))
1642enddef
1643
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +02001644def Test_reverse()
1645 CheckDefAndScriptFailure2(['reverse(10)'], 'E1013: Argument 1: type mismatch, expected list<any> but got number', 'E899: Argument of reverse() must be a List or Blob')
1646 CheckDefAndScriptFailure2(['reverse("abc")'], 'E1013: Argument 1: type mismatch, expected list<any> but got string', 'E899: Argument of reverse() must be a List or Blob')
1647enddef
1648
Bram Moolenaar94738d82020-10-21 14:25:07 +02001649def Test_reverse_return_type()
1650 var l = reverse([1, 2, 3])
1651 var res = 0
1652 for n in l
1653 res += n
1654 endfor
1655 res->assert_equal(6)
1656enddef
1657
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +02001658def Test_screenattr()
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +02001659 CheckDefFailure(['screenattr("x", 1)'], 'E1013: Argument 1: type mismatch, expected number but got string')
1660 CheckDefFailure(['screenattr(1, "x")'], 'E1013: Argument 2: type mismatch, expected number but got string')
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +02001661enddef
1662
1663def Test_screenchar()
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +02001664 CheckDefFailure(['screenchar("x", 1)'], 'E1013: Argument 1: type mismatch, expected number but got string')
1665 CheckDefFailure(['screenchar(1, "x")'], 'E1013: Argument 2: type mismatch, expected number but got string')
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +02001666enddef
1667
1668def Test_screenchars()
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +02001669 CheckDefFailure(['screenchars("x", 1)'], 'E1013: Argument 1: type mismatch, expected number but got string')
1670 CheckDefFailure(['screenchars(1, "x")'], 'E1013: Argument 2: type mismatch, expected number but got string')
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +02001671enddef
1672
Yegappan Lakshmanana26f56f2021-07-03 11:58:12 +02001673def Test_screenpos()
1674 CheckDefFailure(['screenpos("a", 1, 1)'], 'E1013: Argument 1: type mismatch, expected number but got string')
1675 CheckDefFailure(['screenpos(1, "b", 1)'], 'E1013: Argument 2: type mismatch, expected number but got string')
1676 CheckDefFailure(['screenpos(1, 1, "c")'], 'E1013: Argument 3: type mismatch, expected number but got string')
1677 assert_equal({col: 1, row: 1, endcol: 1, curscol: 1}, screenpos(1, 1, 1))
1678enddef
1679
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +02001680def Test_screenstring()
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +02001681 CheckDefFailure(['screenstring("x", 1)'], 'E1013: Argument 1: type mismatch, expected number but got string')
1682 CheckDefFailure(['screenstring(1, "x")'], 'E1013: Argument 2: type mismatch, expected number but got string')
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +02001683enddef
1684
Bram Moolenaar94738d82020-10-21 14:25:07 +02001685def Test_search()
1686 new
1687 setline(1, ['foo', 'bar'])
1688 var val = 0
1689 # skip expr returns boolean
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01001690 search('bar', 'W', 0, 0, () => val == 1)->assert_equal(2)
Bram Moolenaar94738d82020-10-21 14:25:07 +02001691 :1
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01001692 search('bar', 'W', 0, 0, () => val == 0)->assert_equal(0)
Bram Moolenaar94738d82020-10-21 14:25:07 +02001693 # skip expr returns number, only 0 and 1 are accepted
1694 :1
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01001695 search('bar', 'W', 0, 0, () => 0)->assert_equal(2)
Bram Moolenaar94738d82020-10-21 14:25:07 +02001696 :1
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01001697 search('bar', 'W', 0, 0, () => 1)->assert_equal(0)
1698 assert_fails("search('bar', '', 0, 0, () => -1)", 'E1023:')
1699 assert_fails("search('bar', '', 0, 0, () => -1)", 'E1023:')
Bram Moolenaarf06ab6b2021-05-07 19:44:21 +02001700
1701 setline(1, "find this word")
1702 normal gg
1703 var col = 7
1704 assert_equal(1, search('this', '', 0, 0, 'col(".") > col'))
1705 normal 0
1706 assert_equal([1, 6], searchpos('this', '', 0, 0, 'col(".") > col'))
1707
1708 col = 5
1709 normal 0
1710 assert_equal(0, search('this', '', 0, 0, 'col(".") > col'))
1711 normal 0
1712 assert_equal([0, 0], searchpos('this', '', 0, 0, 'col(".") > col'))
1713 bwipe!
Bram Moolenaar94738d82020-10-21 14:25:07 +02001714enddef
1715
1716def Test_searchcount()
1717 new
1718 setline(1, "foo bar")
1719 :/foo
Bram Moolenaare0de1712020-12-02 17:36:54 +01001720 searchcount({recompute: true})
1721 ->assert_equal({
Bram Moolenaar94738d82020-10-21 14:25:07 +02001722 exact_match: 1,
1723 current: 1,
1724 total: 1,
1725 maxcount: 99,
1726 incomplete: 0})
1727 bwipe!
1728enddef
1729
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001730def Test_searchpair()
1731 new
1732 setline(1, "here { and } there")
Bram Moolenaarf06ab6b2021-05-07 19:44:21 +02001733
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001734 normal f{
1735 var col = 15
1736 assert_equal(1, searchpair('{', '', '}', '', 'col(".") > col'))
1737 assert_equal(12, col('.'))
Bram Moolenaarf06ab6b2021-05-07 19:44:21 +02001738 normal 0f{
1739 assert_equal([1, 12], searchpairpos('{', '', '}', '', 'col(".") > col'))
1740
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001741 col = 8
1742 normal 0f{
1743 assert_equal(0, searchpair('{', '', '}', '', 'col(".") > col'))
1744 assert_equal(6, col('.'))
Bram Moolenaarf06ab6b2021-05-07 19:44:21 +02001745 normal 0f{
1746 assert_equal([0, 0], searchpairpos('{', '', '}', '', 'col(".") > col'))
1747
Bram Moolenaarff652882021-05-16 15:24:49 +02001748 var lines =<< trim END
1749 vim9script
1750 setline(1, '()')
1751 normal gg
1752 def Fail()
1753 try
1754 searchpairpos('(', '', ')', 'nW', '[0]->map("")')
1755 catch
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001756 g:caught = 'yes'
Bram Moolenaarff652882021-05-16 15:24:49 +02001757 endtry
1758 enddef
1759 Fail()
1760 END
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001761 CheckScriptSuccess(lines)
1762 assert_equal('yes', g:caught)
Bram Moolenaarff652882021-05-16 15:24:49 +02001763
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001764 unlet g:caught
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001765 bwipe!
1766enddef
1767
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +02001768def Test_server2client()
1769 CheckFeature clientserver
1770 CheckEnv DISPLAY
1771 CheckDefAndScriptFailure2(['server2client(10, "b")'], 'E1013: Argument 1: type mismatch, expected string but got number', 'E573: Invalid server id used:')
1772 CheckDefAndScriptFailure2(['server2client("a", 10)'], 'E1013: Argument 2: type mismatch, expected string but got number', 'E573: Invalid server id used:')
1773enddef
1774
Bram Moolenaar34453202021-01-31 13:08:38 +01001775def Test_set_get_bufline()
1776 # similar to Test_setbufline_getbufline()
1777 var lines =<< trim END
1778 new
1779 var b = bufnr('%')
1780 hide
1781 assert_equal(0, setbufline(b, 1, ['foo', 'bar']))
1782 assert_equal(['foo'], getbufline(b, 1))
1783 assert_equal(['bar'], getbufline(b, '$'))
1784 assert_equal(['foo', 'bar'], getbufline(b, 1, 2))
1785 exe "bd!" b
1786 assert_equal([], getbufline(b, 1, 2))
1787
1788 split Xtest
1789 setline(1, ['a', 'b', 'c'])
1790 b = bufnr('%')
1791 wincmd w
1792
1793 assert_equal(1, setbufline(b, 5, 'x'))
1794 assert_equal(1, setbufline(b, 5, ['x']))
1795 assert_equal(1, setbufline(b, 5, []))
1796 assert_equal(1, setbufline(b, 5, test_null_list()))
1797
1798 assert_equal(1, 'x'->setbufline(bufnr('$') + 1, 1))
1799 assert_equal(1, ['x']->setbufline(bufnr('$') + 1, 1))
1800 assert_equal(1, []->setbufline(bufnr('$') + 1, 1))
1801 assert_equal(1, test_null_list()->setbufline(bufnr('$') + 1, 1))
1802
1803 assert_equal(['a', 'b', 'c'], getbufline(b, 1, '$'))
1804
1805 assert_equal(0, setbufline(b, 4, ['d', 'e']))
1806 assert_equal(['c'], b->getbufline(3))
1807 assert_equal(['d'], getbufline(b, 4))
1808 assert_equal(['e'], getbufline(b, 5))
1809 assert_equal([], getbufline(b, 6))
1810 assert_equal([], getbufline(b, 2, 1))
1811
Bram Moolenaar00385112021-02-07 14:31:06 +01001812 if has('job')
Bram Moolenaar1328bde2021-06-05 20:51:38 +02001813 setbufline(b, 2, [function('eval'), {key: 123}, string(test_null_job())])
Bram Moolenaar00385112021-02-07 14:31:06 +01001814 assert_equal(["function('eval')",
1815 "{'key': 123}",
1816 "no process"],
1817 getbufline(b, 2, 4))
1818 endif
Bram Moolenaar34453202021-01-31 13:08:38 +01001819
1820 exe 'bwipe! ' .. b
1821 END
1822 CheckDefAndScriptSuccess(lines)
1823enddef
1824
Bram Moolenaar94738d82020-10-21 14:25:07 +02001825def Test_searchdecl()
1826 searchdecl('blah', true, true)->assert_equal(1)
1827enddef
1828
1829def Test_setbufvar()
1830 setbufvar(bufnr('%'), '&syntax', 'vim')
1831 &syntax->assert_equal('vim')
1832 setbufvar(bufnr('%'), '&ts', 16)
1833 &ts->assert_equal(16)
Bram Moolenaar31a201a2021-01-03 14:47:25 +01001834 setbufvar(bufnr('%'), '&ai', true)
1835 &ai->assert_equal(true)
1836 setbufvar(bufnr('%'), '&ft', 'filetype')
1837 &ft->assert_equal('filetype')
1838
Bram Moolenaar94738d82020-10-21 14:25:07 +02001839 settabwinvar(1, 1, '&syntax', 'vam')
1840 &syntax->assert_equal('vam')
1841 settabwinvar(1, 1, '&ts', 15)
1842 &ts->assert_equal(15)
1843 setlocal ts=8
Bram Moolenaarb0d81822021-01-03 15:55:10 +01001844 settabwinvar(1, 1, '&list', false)
1845 &list->assert_equal(false)
Bram Moolenaar31a201a2021-01-03 14:47:25 +01001846 settabwinvar(1, 1, '&list', true)
1847 &list->assert_equal(true)
1848 setlocal list&
Bram Moolenaar94738d82020-10-21 14:25:07 +02001849
1850 setbufvar('%', 'myvar', 123)
1851 getbufvar('%', 'myvar')->assert_equal(123)
1852enddef
1853
Yegappan Lakshmanana26f56f2021-07-03 11:58:12 +02001854def Test_setcharsearch()
1855 CheckDefFailure(['setcharsearch("x")'], 'E1013: Argument 1: type mismatch, expected dict<any> but got string')
1856 CheckDefFailure(['setcharsearch([])'], 'E1013: Argument 1: type mismatch, expected dict<any> but got list<unknown>')
1857 var d: dict<any> = {char: 'x', forward: 1, until: 1}
1858 setcharsearch(d)
1859 assert_equal(d, getcharsearch())
1860enddef
1861
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +02001862def Test_setcmdpos()
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +02001863 CheckDefFailure(['setcmdpos("x")'], 'E1013: Argument 1: type mismatch, expected number but got string')
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +02001864enddef
1865
Yegappan Lakshmanana26f56f2021-07-03 11:58:12 +02001866def Test_setfperm()
1867 CheckDefFailure(['setfperm(1, "b")'], 'E1013: Argument 1: type mismatch, expected string but got number')
1868 CheckDefFailure(['setfperm("a", 0z10)'], 'E1013: Argument 2: type mismatch, expected string but got blob')
1869enddef
1870
Bram Moolenaar94738d82020-10-21 14:25:07 +02001871def Test_setloclist()
Bram Moolenaare0de1712020-12-02 17:36:54 +01001872 var items = [{filename: '/tmp/file', lnum: 1, valid: true}]
1873 var what = {items: items}
Bram Moolenaar94738d82020-10-21 14:25:07 +02001874 setqflist([], ' ', what)
1875 setloclist(0, [], ' ', what)
1876enddef
1877
1878def Test_setreg()
1879 setreg('a', ['aaa', 'bbb', 'ccc'])
1880 var reginfo = getreginfo('a')
1881 setreg('a', reginfo)
1882 getreginfo('a')->assert_equal(reginfo)
Bram Moolenaar418a29f2021-02-10 22:23:41 +01001883 assert_fails('setreg("ab", 0)', 'E1162:')
Bram Moolenaar94738d82020-10-21 14:25:07 +02001884enddef
1885
Yegappan Lakshmanana26f56f2021-07-03 11:58:12 +02001886def Test_sha256()
1887 CheckDefFailure(['sha256(100)'], 'E1013: Argument 1: type mismatch, expected string but got number')
1888 CheckDefFailure(['sha256(0zABCD)'], 'E1013: Argument 1: type mismatch, expected string but got blob')
1889 assert_equal('ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad', sha256('abc'))
1890enddef
1891
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +02001892def Test_shiftwidth()
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +02001893 CheckDefFailure(['shiftwidth("x")'], 'E1013: Argument 1: type mismatch, expected number but got string')
1894enddef
1895
1896def Test_sign_define()
1897 CheckDefAndScriptFailure2(['sign_define({"a": 10})'], 'E1013: Argument 1: type mismatch, expected string but got dict<number>', 'E731: Using a Dictionary as a String')
1898 CheckDefAndScriptFailure2(['sign_define({"a": 10}, "b")'], 'E1013: Argument 1: type mismatch, expected string but got dict<number>', 'E731: Using a Dictionary as a String')
1899 CheckDefAndScriptFailure2(['sign_define("a", ["b"])'], 'E1013: Argument 2: type mismatch, expected dict<any> but got list<string>', 'E715: Dictionary required')
1900enddef
1901
1902def Test_sign_undefine()
1903 CheckDefAndScriptFailure2(['sign_undefine({})'], 'E1013: Argument 1: type mismatch, expected string but got dict<unknown>', 'E731: Using a Dictionary as a String')
1904 CheckDefAndScriptFailure2(['sign_undefine([1])'], 'E1013: Argument 1: type mismatch, expected list<string> but got list<number>', 'E155: Unknown sign:')
1905enddef
1906
1907def Test_sign_unplace()
1908 CheckDefAndScriptFailure2(['sign_unplace({"a": 10})'], 'E1013: Argument 1: type mismatch, expected string but got dict<number>', 'E474: Invalid argument')
1909 CheckDefAndScriptFailure2(['sign_unplace({"a": 10}, "b")'], 'E1013: Argument 1: type mismatch, expected string but got dict<number>', 'E474: Invalid argument')
1910 CheckDefAndScriptFailure2(['sign_unplace("a", ["b"])'], 'E1013: Argument 2: type mismatch, expected dict<any> but got list<string>', 'E715: Dictionary required')
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +02001911enddef
1912
Yegappan Lakshmanana26f56f2021-07-03 11:58:12 +02001913def Test_simplify()
1914 CheckDefFailure(['simplify(100)'], 'E1013: Argument 1: type mismatch, expected string but got number')
1915 call assert_equal('NonExistingFile', simplify('NonExistingFile'))
1916enddef
1917
Bram Moolenaar6601b622021-01-13 21:47:15 +01001918def Test_slice()
1919 assert_equal('12345', slice('012345', 1))
1920 assert_equal('123', slice('012345', 1, 4))
1921 assert_equal('1234', slice('012345', 1, -1))
1922 assert_equal('1', slice('012345', 1, -4))
1923 assert_equal('', slice('012345', 1, -5))
1924 assert_equal('', slice('012345', 1, -6))
1925
1926 assert_equal([1, 2, 3, 4, 5], slice(range(6), 1))
1927 assert_equal([1, 2, 3], slice(range(6), 1, 4))
1928 assert_equal([1, 2, 3, 4], slice(range(6), 1, -1))
1929 assert_equal([1], slice(range(6), 1, -4))
1930 assert_equal([], slice(range(6), 1, -5))
1931 assert_equal([], slice(range(6), 1, -6))
1932
1933 assert_equal(0z1122334455, slice(0z001122334455, 1))
1934 assert_equal(0z112233, slice(0z001122334455, 1, 4))
1935 assert_equal(0z11223344, slice(0z001122334455, 1, -1))
1936 assert_equal(0z11, slice(0z001122334455, 1, -4))
1937 assert_equal(0z, slice(0z001122334455, 1, -5))
1938 assert_equal(0z, slice(0z001122334455, 1, -6))
1939enddef
1940
Bram Moolenaar94738d82020-10-21 14:25:07 +02001941def Test_spellsuggest()
1942 if !has('spell')
1943 MissingFeature 'spell'
1944 else
1945 spellsuggest('marrch', 1, true)->assert_equal(['March'])
1946 endif
1947enddef
1948
Yegappan Lakshmanana26f56f2021-07-03 11:58:12 +02001949def Test_sound_stop()
1950 CheckFeature sound
1951 CheckDefFailure(['sound_stop("x")'], 'E1013: Argument 1: type mismatch, expected number but got string')
1952enddef
1953
1954def Test_soundfold()
1955 CheckDefFailure(['soundfold(20)'], 'E1013: Argument 1: type mismatch, expected string but got number')
1956 assert_equal('abc', soundfold('abc'))
1957enddef
1958
Bram Moolenaar94738d82020-10-21 14:25:07 +02001959def Test_sort_return_type()
1960 var res: list<number>
1961 res = [1, 2, 3]->sort()
1962enddef
1963
1964def Test_sort_argument()
Bram Moolenaar08cf0c02020-12-05 21:47:06 +01001965 var lines =<< trim END
1966 var res = ['b', 'a', 'c']->sort('i')
1967 res->assert_equal(['a', 'b', 'c'])
1968
1969 def Compare(a: number, b: number): number
1970 return a - b
1971 enddef
1972 var l = [3, 6, 7, 1, 8, 2, 4, 5]
1973 sort(l, Compare)
1974 assert_equal([1, 2, 3, 4, 5, 6, 7, 8], l)
1975 END
1976 CheckDefAndScriptSuccess(lines)
Bram Moolenaar94738d82020-10-21 14:25:07 +02001977enddef
1978
Yegappan Lakshmanana26f56f2021-07-03 11:58:12 +02001979def Test_spellbadword()
1980 CheckDefFailure(['spellbadword(100)'], 'E1013: Argument 1: type mismatch, expected string but got number')
1981 spellbadword('good')->assert_equal(['', ''])
1982enddef
1983
Bram Moolenaar94738d82020-10-21 14:25:07 +02001984def Test_split()
1985 split(' aa bb ', '\W\+', true)->assert_equal(['', 'aa', 'bb', ''])
1986enddef
1987
Yegappan Lakshmanana26f56f2021-07-03 11:58:12 +02001988def Test_srand()
1989 CheckDefFailure(['srand("a")'], 'E1013: Argument 1: type mismatch, expected number but got string')
1990 type(srand(100))->assert_equal(v:t_list)
1991enddef
1992
1993def Test_state()
1994 CheckDefFailure(['state({})'], 'E1013: Argument 1: type mismatch, expected string but got dict<unknown>')
1995 assert_equal('', state('a'))
1996enddef
1997
Bram Moolenaar80ad3e22021-01-31 20:48:58 +01001998def Run_str2float()
1999 if !has('float')
2000 MissingFeature 'float'
2001 endif
2002 str2float("1.00")->assert_equal(1.00)
2003 str2float("2e-2")->assert_equal(0.02)
2004
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +02002005 CheckDefFailure(['str2float(123)'], 'E1013:')
Bram Moolenaar80ad3e22021-01-31 20:48:58 +01002006 CheckScriptFailure(['vim9script', 'echo str2float(123)'], 'E1024:')
2007 endif
2008enddef
2009
Bram Moolenaar94738d82020-10-21 14:25:07 +02002010def Test_str2nr()
2011 str2nr("1'000'000", 10, true)->assert_equal(1000000)
Bram Moolenaarf2b26bc2021-01-30 23:05:11 +01002012
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +02002013 CheckDefFailure(['str2nr(123)'], 'E1013:')
Bram Moolenaarf2b26bc2021-01-30 23:05:11 +01002014 CheckScriptFailure(['vim9script', 'echo str2nr(123)'], 'E1024:')
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +02002015 CheckDefFailure(['str2nr("123", "x")'], 'E1013:')
Bram Moolenaarf2b26bc2021-01-30 23:05:11 +01002016 CheckScriptFailure(['vim9script', 'echo str2nr("123", "x")'], 'E1030:')
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +02002017 CheckDefFailure(['str2nr("123", 10, "x")'], 'E1013:')
Bram Moolenaarf2b26bc2021-01-30 23:05:11 +01002018 CheckScriptFailure(['vim9script', 'echo str2nr("123", 10, "x")'], 'E1135:')
Bram Moolenaar94738d82020-10-21 14:25:07 +02002019enddef
2020
2021def Test_strchars()
2022 strchars("A\u20dd", true)->assert_equal(1)
2023enddef
2024
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +02002025def Test_stridx()
2026 CheckDefAndScriptFailure2(['stridx([1], "b")'], 'E1013: Argument 1: type mismatch, expected string but got list<number>', 'E730: Using a List as a String')
2027 CheckDefAndScriptFailure2(['stridx("a", {})'], 'E1013: Argument 2: type mismatch, expected string but got dict<unknown>', 'E731: Using a Dictionary as a String')
2028 CheckDefAndScriptFailure2(['stridx("a", "b", "c")'], 'E1013: Argument 3: type mismatch, expected number but got string', 'E1030: Using a String as a Number')
2029enddef
2030
Yegappan Lakshmanana26f56f2021-07-03 11:58:12 +02002031def Test_strlen()
2032 CheckDefFailure(['strlen([])'], 'E1013: Argument 1: type mismatch, expected string but got list<unknown>')
2033 "abc"->strlen()->assert_equal(3)
2034 strlen(99)->assert_equal(2)
2035enddef
2036
2037def Test_strptime()
2038 CheckFunction strptime
2039 CheckDefFailure(['strptime(10, "2021")'], 'E1013: Argument 1: type mismatch, expected string but got number')
2040 CheckDefFailure(['strptime("%Y", 2021)'], 'E1013: Argument 2: type mismatch, expected string but got number')
2041 # BUG: Directly calling strptime() in this function gives an "E117: Unknown
2042 # function" error on MS-Windows even with the above CheckFunction call for
2043 # strptime().
2044 #assert_true(strptime('%Y', '2021') != 0)
2045enddef
2046
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +02002047def Test_strridx()
2048 CheckDefAndScriptFailure2(['strridx([1], "b")'], 'E1013: Argument 1: type mismatch, expected string but got list<number>', 'E730: Using a List as a String')
2049 CheckDefAndScriptFailure2(['strridx("a", {})'], 'E1013: Argument 2: type mismatch, expected string but got dict<unknown>', 'E731: Using a Dictionary as a String')
2050 CheckDefAndScriptFailure2(['strridx("a", "b", "c")'], 'E1013: Argument 3: type mismatch, expected number but got string', 'E1030: Using a String as a Number')
2051enddef
2052
Yegappan Lakshmanana26f56f2021-07-03 11:58:12 +02002053def Test_strtrans()
2054 CheckDefFailure(['strtrans(20)'], 'E1013: Argument 1: type mismatch, expected string but got number')
2055 assert_equal('abc', strtrans('abc'))
2056enddef
2057
2058def Test_strwidth()
2059 CheckDefFailure(['strwidth(10)'], 'E1013: Argument 1: type mismatch, expected string but got number')
2060 CheckScriptFailure(['vim9script', 'echo strwidth(10)'], 'E1024:')
2061 assert_equal(4, strwidth('abcd'))
2062enddef
2063
Bram Moolenaar94738d82020-10-21 14:25:07 +02002064def Test_submatch()
2065 var pat = 'A\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)'
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01002066 var Rep = () => range(10)->mapnew((_, v) => submatch(v, true))->string()
Bram Moolenaar94738d82020-10-21 14:25:07 +02002067 var actual = substitute('A123456789', pat, Rep, '')
2068 var expected = "[['A123456789'], ['1'], ['2'], ['3'], ['4'], ['5'], ['6'], ['7'], ['8'], ['9']]"
2069 actual->assert_equal(expected)
2070enddef
2071
Bram Moolenaar1328bde2021-06-05 20:51:38 +02002072def Test_substitute()
2073 var res = substitute('A1234', '\d', 'X', '')
2074 assert_equal('AX234', res)
2075
2076 if has('job')
2077 assert_fails('"text"->substitute(".*", () => job_start(":"), "")', 'E908: using an invalid value as a String: job')
2078 assert_fails('"text"->substitute(".*", () => job_start(":")->job_getchannel(), "")', 'E908: using an invalid value as a String: channel')
2079 endif
2080enddef
2081
Yegappan Lakshmanana26f56f2021-07-03 11:58:12 +02002082def Test_swapinfo()
2083 CheckDefFailure(['swapinfo({})'], 'E1013: Argument 1: type mismatch, expected string but got dict<unknown>')
2084 call assert_equal({error: 'Cannot open file'}, swapinfo('x'))
2085enddef
2086
2087def Test_swapname()
2088 CheckDefFailure(['swapname([])'], 'E1013: Argument 1: type mismatch, expected string but got list<unknown>')
2089 assert_fails('swapname("NonExistingBuf")', 'E94:')
2090enddef
2091
Bram Moolenaar94738d82020-10-21 14:25:07 +02002092def Test_synID()
2093 new
2094 setline(1, "text")
2095 synID(1, 1, true)->assert_equal(0)
2096 bwipe!
2097enddef
2098
Yegappan Lakshmanana26f56f2021-07-03 11:58:12 +02002099def Test_synIDtrans()
2100 CheckDefFailure(['synIDtrans("a")'], 'E1013: Argument 1: type mismatch, expected number but got string')
2101enddef
2102
2103def Test_tabpagebuflist()
2104 CheckDefFailure(['tabpagebuflist("t")'], 'E1013: Argument 1: type mismatch, expected number but got string')
2105 assert_equal([bufnr('')], tabpagebuflist())
2106 assert_equal([bufnr('')], tabpagebuflist(1))
2107enddef
2108
2109def Test_tabpagenr()
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +02002110 CheckDefAndScriptFailure2(['tabpagenr(1)'], 'E1013: Argument 1: type mismatch, expected string but got number', 'E15: Invalid expression:')
Yegappan Lakshmanana26f56f2021-07-03 11:58:12 +02002111 assert_equal(1, tabpagenr('$'))
2112 assert_equal(1, tabpagenr())
2113enddef
2114
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +02002115def Test_taglist()
2116 CheckDefAndScriptFailure2(['taglist([1])'], 'E1013: Argument 1: type mismatch, expected string but got list<number>', 'E730: Using a List as a String')
2117 CheckDefAndScriptFailure2(['taglist("a", [2])'], 'E1013: Argument 2: type mismatch, expected string but got list<number>', 'E730: Using a List as a String')
2118enddef
2119
2120def Test_term_dumpload()
2121 CheckRunVimInTerminal
2122 CheckDefAndScriptFailure2(['term_dumpload({"a": 10})'], 'E1013: Argument 1: type mismatch, expected string but got dict<number>', 'E1174: String required for argument 1')
2123 CheckDefAndScriptFailure2(['term_dumpload({"a": 10}, "b")'], 'E1013: Argument 1: type mismatch, expected string but got dict<number>', 'E1174: String required for argument 1')
2124 CheckDefAndScriptFailure2(['term_dumpload("a", ["b"])'], 'E1013: Argument 2: type mismatch, expected dict<any> but got list<string>', 'E1206: Dictionary required for argument 2')
2125enddef
2126
2127def Test_term_getaltscreen()
2128 CheckRunVimInTerminal
2129 CheckDefAndScriptFailure2(['term_getaltscreen(true)'], 'E1013: Argument 1: type mismatch, expected string but got bool', 'E1138: Using a Bool as a Number')
2130enddef
2131
2132def Test_term_getansicolors()
2133 CheckRunVimInTerminal
2134 CheckDefAndScriptFailure2(['term_getansicolors(["a"])'], 'E1013: Argument 1: type mismatch, expected string but got list<string>', 'E745: Using a List as a Number')
2135enddef
2136
2137def Test_term_getcursor()
2138 CheckRunVimInTerminal
2139 CheckDefAndScriptFailure2(['term_getcursor({"a": 10})'], 'E1013: Argument 1: type mismatch, expected string but got dict<number>', 'E728: Using a Dictionary as a Number')
2140enddef
2141
2142def Test_term_getjob()
2143 CheckRunVimInTerminal
2144 CheckDefAndScriptFailure2(['term_getjob(0z10)'], 'E1013: Argument 1: type mismatch, expected string but got blob', 'E974: Using a Blob as a Number')
2145enddef
2146
2147def Test_term_getscrolled()
2148 CheckRunVimInTerminal
2149 CheckDefAndScriptFailure2(['term_getscrolled(1.1)'], 'E1013: Argument 1: type mismatch, expected string but got float', 'E805: Using a Float as a Number')
2150enddef
2151
2152def Test_term_getsize()
2153 CheckRunVimInTerminal
2154 CheckDefAndScriptFailure2(['term_getsize(1.1)'], 'E1013: Argument 1: type mismatch, expected string but got float', 'E805: Using a Float as a Number')
2155enddef
2156
2157def Test_term_getstatus()
2158 CheckRunVimInTerminal
2159 CheckDefAndScriptFailure2(['term_getstatus(1.1)'], 'E1013: Argument 1: type mismatch, expected string but got float', 'E805: Using a Float as a Number')
2160enddef
2161
2162def Test_term_gettitle()
2163 CheckRunVimInTerminal
2164 CheckDefAndScriptFailure2(['term_gettitle(1.1)'], 'E1013: Argument 1: type mismatch, expected string but got float', 'E805: Using a Float as a Number')
2165enddef
2166
Bram Moolenaar94738d82020-10-21 14:25:07 +02002167def Test_term_gettty()
2168 if !has('terminal')
2169 MissingFeature 'terminal'
2170 else
2171 var buf = Run_shell_in_terminal({})
2172 term_gettty(buf, true)->assert_notequal('')
2173 StopShellInTerminal(buf)
2174 endif
2175enddef
2176
2177def Test_term_start()
2178 if !has('terminal')
2179 MissingFeature 'terminal'
2180 else
2181 botright new
2182 var winnr = winnr()
Bram Moolenaare0de1712020-12-02 17:36:54 +01002183 term_start(&shell, {curwin: true})
Bram Moolenaar94738d82020-10-21 14:25:07 +02002184 winnr()->assert_equal(winnr)
2185 bwipe!
2186 endif
2187enddef
2188
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +02002189def Test_test_alloc_fail()
2190 CheckDefAndScriptFailure2(['test_alloc_fail("a", 10, 20)'], 'E1013: Argument 1: type mismatch, expected number but got string', 'E474: Invalid argument')
2191 CheckDefAndScriptFailure2(['test_alloc_fail(10, "b", 20)'], 'E1013: Argument 2: type mismatch, expected number but got string', 'E474: Invalid argument')
2192 CheckDefAndScriptFailure2(['test_alloc_fail(10, 20, "c")'], 'E1013: Argument 3: type mismatch, expected number but got string', 'E474: Invalid argument')
2193enddef
2194
2195def Test_test_feedinput()
2196 CheckDefAndScriptFailure2(['test_feedinput(test_void())'], 'E1013: Argument 1: type mismatch, expected string but got void', 'E1031: Cannot use void value')
2197 CheckDefAndScriptFailure2(['test_feedinput(["a"])'], 'E1013: Argument 1: type mismatch, expected string but got list<string>', 'E730: Using a List as a String')
2198enddef
2199
2200def Test_test_getvalue()
2201 CheckDefAndScriptFailure2(['test_getvalue(1.1)'], 'E1013: Argument 1: type mismatch, expected string but got float', 'E474: Invalid argument')
2202enddef
2203
2204def Test_test_ignore_error()
2205 CheckDefAndScriptFailure2(['test_ignore_error([])'], 'E1013: Argument 1: type mismatch, expected string but got list<unknown>', 'E474: Invalid argument')
2206 test_ignore_error('RESET')
2207enddef
2208
2209def Test_test_option_not_set()
2210 CheckDefAndScriptFailure2(['test_option_not_set([])'], 'E1013: Argument 1: type mismatch, expected string but got list<unknown>', 'E474: Invalid argument')
2211enddef
2212
2213def Test_test_setmouse()
2214 CheckDefAndScriptFailure2(['test_setmouse("a", 10)'], 'E1013: Argument 1: type mismatch, expected number but got string', 'E474: Invalid argument')
2215 CheckDefAndScriptFailure2(['test_setmouse(10, "b")'], 'E1013: Argument 2: type mismatch, expected number but got string', 'E474: Invalid argument')
2216enddef
2217
2218def Test_test_settime()
2219 CheckDefAndScriptFailure2(['test_settime([1])'], 'E1013: Argument 1: type mismatch, expected number but got list<number>', 'E745: Using a List as a Number')
2220enddef
2221
2222def Test_test_srand_seed()
2223 CheckDefAndScriptFailure2(['test_srand_seed([1])'], 'E1013: Argument 1: type mismatch, expected number but got list<number>', 'E745: Using a List as a Number')
2224 CheckDefAndScriptFailure2(['test_srand_seed("10")'], 'E1013: Argument 1: type mismatch, expected number but got string', 'E1030: Using a String as a Number')
2225enddef
2226
Yegappan Lakshmanana26f56f2021-07-03 11:58:12 +02002227def Test_timer_info()
2228 CheckDefFailure(['timer_info("id")'], 'E1013: Argument 1: type mismatch, expected number but got string')
2229 assert_equal([], timer_info(100))
2230 assert_equal([], timer_info())
2231enddef
2232
Bram Moolenaar94738d82020-10-21 14:25:07 +02002233def Test_timer_paused()
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01002234 var id = timer_start(50, () => 0)
Bram Moolenaar94738d82020-10-21 14:25:07 +02002235 timer_pause(id, true)
2236 var info = timer_info(id)
2237 info[0]['paused']->assert_equal(1)
2238 timer_stop(id)
2239enddef
2240
Yegappan Lakshmanana26f56f2021-07-03 11:58:12 +02002241def Test_timer_stop()
2242 CheckDefFailure(['timer_stop("x")'], 'E1013: Argument 1: type mismatch, expected number but got string')
2243 assert_equal(0, timer_stop(100))
2244enddef
2245
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +02002246def Test_tolower()
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +02002247 CheckDefFailure(['tolower(1)'], 'E1013: Argument 1: type mismatch, expected string but got number')
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +02002248enddef
2249
2250def Test_toupper()
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +02002251 CheckDefFailure(['toupper(1)'], 'E1013: Argument 1: type mismatch, expected string but got number')
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +02002252enddef
2253
2254def Test_tr()
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +02002255 CheckDefFailure(['tr(1, "a", "b")'], 'E1013: Argument 1: type mismatch, expected string but got number')
2256 CheckDefFailure(['tr("a", 1, "b")'], 'E1013: Argument 2: type mismatch, expected string but got number')
2257 CheckDefFailure(['tr("a", "a", 1)'], 'E1013: Argument 3: type mismatch, expected string but got number')
2258enddef
2259
2260def Test_trim()
2261 CheckDefAndScriptFailure2(['trim(["a"])'], 'E1013: Argument 1: type mismatch, expected string but got list<string>', 'E730: Using a List as a String')
2262 CheckDefAndScriptFailure2(['trim("a", ["b"])'], 'E1013: Argument 2: type mismatch, expected string but got list<string>', 'E730: Using a List as a String')
2263 CheckDefAndScriptFailure2(['trim("a", "b", "c")'], 'E1013: Argument 3: type mismatch, expected number but got string', 'E1030: Using a String as a Number')
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +02002264enddef
2265
Bram Moolenaar9da32e42021-07-09 19:53:57 +02002266def Test_typename()
2267 if has('float')
2268 assert_equal('func([unknown], [unknown]): float', typename(function('pow')))
2269 endif
2270enddef
2271
Yegappan Lakshmanana26f56f2021-07-03 11:58:12 +02002272def Test_undofile()
2273 CheckDefFailure(['undofile(10)'], 'E1013: Argument 1: type mismatch, expected string but got number')
2274 assert_equal('.abc.un~', fnamemodify(undofile('abc'), ':t'))
2275enddef
2276
2277def Test_values()
2278 CheckDefFailure(['values([])'], 'E1013: Argument 1: type mismatch, expected dict<any> but got list<unknown>')
2279 assert_equal([], {}->values())
2280 assert_equal(['sun'], {star: 'sun'}->values())
2281enddef
2282
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +02002283def Test_virtcol()
2284 CheckDefAndScriptFailure2(['virtcol(1.1)'], 'E1013: Argument 1: type mismatch, expected string but got float', 'E1174: String required for argument 1')
2285enddef
2286
Bram Moolenaar37487e12021-01-12 22:08:53 +01002287def Test_win_execute()
2288 assert_equal("\n" .. winnr(), win_execute(win_getid(), 'echo winnr()'))
2289 assert_equal('', win_execute(342343, 'echo winnr()'))
2290enddef
2291
Yegappan Lakshmanana26f56f2021-07-03 11:58:12 +02002292def Test_win_findbuf()
2293 CheckDefFailure(['win_findbuf("a")'], 'E1013: Argument 1: type mismatch, expected number but got string')
2294 assert_equal([], win_findbuf(1000))
2295 assert_equal([win_getid()], win_findbuf(bufnr('')))
2296enddef
2297
2298def Test_win_getid()
2299 CheckDefFailure(['win_getid(".")'], 'E1013: Argument 1: type mismatch, expected number but got string')
2300 CheckDefFailure(['win_getid(1, ".")'], 'E1013: Argument 2: type mismatch, expected number but got string')
2301 assert_equal(win_getid(), win_getid(1, 1))
2302enddef
2303
Bram Moolenaar94738d82020-10-21 14:25:07 +02002304def Test_win_splitmove()
2305 split
Bram Moolenaare0de1712020-12-02 17:36:54 +01002306 win_splitmove(1, 2, {vertical: true, rightbelow: true})
Bram Moolenaar94738d82020-10-21 14:25:07 +02002307 close
2308enddef
2309
Yegappan Lakshmanana26f56f2021-07-03 11:58:12 +02002310def Test_winnr()
2311 CheckDefFailure(['winnr([])'], 'E1013: Argument 1: type mismatch, expected string but got list<unknown>')
2312 assert_equal(1, winnr())
2313 assert_equal(1, winnr('$'))
2314enddef
2315
Bram Moolenaar285b15f2020-12-29 20:25:19 +01002316def Test_winrestcmd()
2317 split
2318 var cmd = winrestcmd()
2319 wincmd _
2320 exe cmd
2321 assert_equal(cmd, winrestcmd())
2322 close
2323enddef
2324
Yegappan Lakshmanana26f56f2021-07-03 11:58:12 +02002325def Test_winrestview()
2326 CheckDefFailure(['winrestview([])'], 'E1013: Argument 1: type mismatch, expected dict<any> but got list<unknown>')
2327 :%d _
2328 setline(1, 'Hello World')
2329 winrestview({lnum: 1, col: 6})
2330 assert_equal([1, 7], [line('.'), col('.')])
2331enddef
2332
Bram Moolenaar43b69b32021-01-07 20:23:33 +01002333def Test_winsaveview()
2334 var view: dict<number> = winsaveview()
2335
2336 var lines =<< trim END
2337 var view: list<number> = winsaveview()
2338 END
2339 CheckDefAndScriptFailure(lines, 'E1012: Type mismatch; expected list<number> but got dict<number>', 1)
2340enddef
2341
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +02002342def Test_win_gettype()
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +02002343 CheckDefAndScriptFailure2(['win_gettype("x")'], 'E1013: Argument 1: type mismatch, expected number but got string', 'E1030: Using a String as a Number')
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +02002344enddef
Bram Moolenaar43b69b32021-01-07 20:23:33 +01002345
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +02002346def Test_win_gotoid()
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +02002347 CheckDefAndScriptFailure2(['win_gotoid("x")'], 'E1013: Argument 1: type mismatch, expected number but got string', 'E1030: Using a String as a Number')
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +02002348enddef
Bram Moolenaar285b15f2020-12-29 20:25:19 +01002349
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +02002350def Test_win_id2tabwin()
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +02002351 CheckDefAndScriptFailure2(['win_id2tabwin("x")'], 'E1013: Argument 1: type mismatch, expected number but got string', 'E1030: Using a String as a Number')
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +02002352enddef
2353
2354def Test_win_id2win()
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +02002355 CheckDefAndScriptFailure2(['win_id2win("x")'], 'E1013: Argument 1: type mismatch, expected number but got string', 'E1030: Using a String as a Number')
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +02002356enddef
2357
2358def Test_win_screenpos()
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +02002359 CheckDefAndScriptFailure2(['win_screenpos("x")'], 'E1013: Argument 1: type mismatch, expected number but got string', 'E1030: Using a String as a Number')
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +02002360enddef
2361
2362def Test_winbufnr()
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +02002363 CheckDefAndScriptFailure2(['winbufnr("x")'], 'E1013: Argument 1: type mismatch, expected number but got string', 'E1030: Using a String as a Number')
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +02002364enddef
2365
2366def Test_winheight()
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +02002367 CheckDefAndScriptFailure2(['winheight("x")'], 'E1013: Argument 1: type mismatch, expected number but got string', 'E1030: Using a String as a Number')
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +02002368enddef
2369
2370def Test_winlayout()
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +02002371 CheckDefAndScriptFailure2(['winlayout("x")'], 'E1013: Argument 1: type mismatch, expected number but got string', 'E1030: Using a String as a Number')
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +02002372enddef
2373
2374def Test_winwidth()
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +02002375 CheckDefAndScriptFailure2(['winwidth("x")'], 'E1013: Argument 1: type mismatch, expected number but got string', 'E1030: Using a String as a Number')
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +02002376enddef
2377
2378def Test_xor()
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +02002379 CheckDefAndScriptFailure2(['xor("x", 0x2)'], 'E1013: Argument 1: type mismatch, expected number but got string', 'E1030: Using a String as a Number')
2380 CheckDefAndScriptFailure2(['xor(0x1, "x")'], 'E1013: Argument 2: type mismatch, expected number but got string', 'E1030: Using a String as a Number')
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +02002381enddef
Bram Moolenaar94738d82020-10-21 14:25:07 +02002382
2383" vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker