blob: 38bd8271c8eb0683267710e3fb5c9e685a8de640 [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 Moolenaar94738d82020-10-21 14:25:07 +0200107enddef
108
109def Test_add_blob()
110 var b1: blob = 0z12
111 add(b1, 0x34)
112 assert_equal(0z1234, b1)
113
114 var b2: blob # defaults to empty blob
115 add(b2, 0x67)
116 assert_equal(0z67, b2)
117
118 var lines =<< trim END
119 var b: blob
120 add(b, "x")
121 END
122 CheckDefFailure(lines, 'E1012:', 2)
123
124 lines =<< trim END
Bram Moolenaarb7c21af2021-04-18 14:12:31 +0200125 add(test_null_blob(), 123)
126 END
127 CheckDefExecAndScriptFailure(lines, 'E1131:', 1)
128
129 lines =<< trim END
Bram Moolenaar94738d82020-10-21 14:25:07 +0200130 var b: blob = test_null_blob()
131 add(b, 123)
132 END
133 CheckDefExecFailure(lines, 'E1131:', 2)
Bram Moolenaarb7c21af2021-04-18 14:12:31 +0200134
135 # Getting variable with NULL blob allocates a new blob at script level
136 lines =<< trim END
137 vim9script
138 var b: blob = test_null_blob()
139 add(b, 123)
140 END
141 CheckScriptSuccess(lines)
Bram Moolenaar94738d82020-10-21 14:25:07 +0200142enddef
143
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +0200144def Test_and()
145 CheckDefFailure(['echo and("x", 0x2)'], 'E1013: Argument 1: type mismatch, expected number but got string')
146 CheckDefFailure(['echo and(0x1, "x")'], 'E1013: Argument 2: type mismatch, expected number but got string')
147enddef
148
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100149def Test_append()
150 new
151 setline(1, range(3))
152 var res1: number = append(1, 'one')
153 assert_equal(0, res1)
154 var res2: bool = append(3, 'two')
155 assert_equal(false, res2)
156 assert_equal(['0', 'one', '1', 'two', '2'], getline(1, 6))
Bram Moolenaarb2ac7d02021-03-28 15:46:16 +0200157
158 append(0, 'zero')
159 assert_equal('zero', getline(1))
160 bwipe!
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100161enddef
162
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +0200163def Test_argc()
164 CheckDefFailure(['echo argc("x")'], 'E1013: Argument 1: type mismatch, expected number but got string')
165enddef
166
167def Test_arglistid()
168 CheckDefFailure(['echo arglistid("x")'], 'E1013: Argument 1: type mismatch, expected number but got string')
169 CheckDefFailure(['echo arglistid(1, "y")'], 'E1013: Argument 2: type mismatch, expected number but got string')
170 CheckDefFailure(['echo arglistid("x", "y")'], 'E1013: Argument 1: type mismatch, expected number but got string')
171enddef
172
173def Test_argv()
174 CheckDefFailure(['echo argv("x")'], 'E1013: Argument 1: type mismatch, expected number but got string')
175 CheckDefFailure(['echo argv(1, "x")'], 'E1013: Argument 2: type mismatch, expected number but got string')
176 CheckDefFailure(['echo argv("x", "y")'], 'E1013: Argument 1: type mismatch, expected number but got string')
177enddef
178
Bram Moolenaar32105ae2021-03-27 18:59:25 +0100179def Test_balloon_show()
180 CheckGui
181 CheckFeature balloon_eval
182
Yegappan Lakshmanana26f56f2021-07-03 11:58:12 +0200183 assert_fails('balloon_show(10)', 'E1174:')
Bram Moolenaar32105ae2021-03-27 18:59:25 +0100184 assert_fails('balloon_show(true)', 'E1174:')
185enddef
186
187def Test_balloon_split()
Bram Moolenaar7b45d462021-03-27 19:09:02 +0100188 CheckFeature balloon_eval_term
Bram Moolenaar32105ae2021-03-27 18:59:25 +0100189
Yegappan Lakshmanana26f56f2021-07-03 11:58:12 +0200190 assert_fails('balloon_split([])', 'E1174:')
Bram Moolenaar32105ae2021-03-27 18:59:25 +0100191 assert_fails('balloon_split(true)', 'E1174:')
192enddef
193
Bram Moolenaarf28f2ac2021-03-22 22:21:26 +0100194def Test_browse()
195 CheckFeature browse
196
197 var lines =<< trim END
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +0100198 browse(1, 2, 3, 4)
Bram Moolenaarf28f2ac2021-03-22 22:21:26 +0100199 END
200 CheckDefExecAndScriptFailure(lines, 'E1174: String required for argument 2')
201 lines =<< trim END
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +0100202 browse(1, 'title', 3, 4)
Bram Moolenaarf28f2ac2021-03-22 22:21:26 +0100203 END
204 CheckDefExecAndScriptFailure(lines, 'E1174: String required for argument 3')
205 lines =<< trim END
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +0100206 browse(1, 'title', 'dir', 4)
Bram Moolenaarf28f2ac2021-03-22 22:21:26 +0100207 END
208 CheckDefExecAndScriptFailure(lines, 'E1174: String required for argument 4')
209enddef
210
Yegappan Lakshmanana26f56f2021-07-03 11:58:12 +0200211def Test_bufadd()
212 assert_fails('bufadd([])', 'E730:')
213enddef
214
Bram Moolenaar32105ae2021-03-27 18:59:25 +0100215def Test_bufexists()
Yegappan Lakshmanana26f56f2021-07-03 11:58:12 +0200216 assert_fails('bufexists(true)', 'E1174:')
Bram Moolenaar32105ae2021-03-27 18:59:25 +0100217enddef
218
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100219def Test_buflisted()
220 var res: bool = buflisted('asdf')
221 assert_equal(false, res)
Yegappan Lakshmanana26f56f2021-07-03 11:58:12 +0200222 assert_fails('buflisted(true)', 'E1174:')
223 assert_fails('buflisted([])', 'E1174:')
224enddef
225
226def Test_bufload()
227 assert_fails('bufload([])', 'E730:')
228enddef
229
230def Test_bufloaded()
231 assert_fails('bufloaded(true)', 'E1174:')
232 assert_fails('bufloaded([])', 'E1174:')
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100233enddef
234
Bram Moolenaar94738d82020-10-21 14:25:07 +0200235def Test_bufname()
236 split SomeFile
237 bufname('%')->assert_equal('SomeFile')
238 edit OtherFile
239 bufname('#')->assert_equal('SomeFile')
240 close
Yegappan Lakshmanana26f56f2021-07-03 11:58:12 +0200241 assert_fails('bufname(true)', 'E1138:')
242 assert_fails('bufname([])', 'E745:')
Bram Moolenaar94738d82020-10-21 14:25:07 +0200243enddef
244
245def Test_bufnr()
246 var buf = bufnr()
247 bufnr('%')->assert_equal(buf)
248
249 buf = bufnr('Xdummy', true)
250 buf->assert_notequal(-1)
251 exe 'bwipe! ' .. buf
252enddef
253
254def Test_bufwinid()
255 var origwin = win_getid()
256 below split SomeFile
257 var SomeFileID = win_getid()
258 below split OtherFile
259 below split SomeFile
260 bufwinid('SomeFile')->assert_equal(SomeFileID)
261
262 win_gotoid(origwin)
263 only
264 bwipe SomeFile
265 bwipe OtherFile
Bram Moolenaar32105ae2021-03-27 18:59:25 +0100266
Yegappan Lakshmanana26f56f2021-07-03 11:58:12 +0200267 assert_fails('bufwinid(true)', 'E1138:')
268 assert_fails('bufwinid([])', 'E745:')
269enddef
270
271def Test_bufwinnr()
272 assert_fails('bufwinnr(true)', 'E1138:')
273 assert_fails('bufwinnr([])', 'E745:')
274enddef
275
276def Test_byte2line()
277 CheckDefFailure(['byte2line("1")'], 'E1013: Argument 1: type mismatch, expected number but got string')
278 CheckDefFailure(['byte2line([])'], 'E1013: Argument 1: type mismatch, expected number but got list<unknown>')
279 assert_equal(-1, byte2line(0))
Bram Moolenaar94738d82020-10-21 14:25:07 +0200280enddef
281
282def Test_call_call()
283 var l = [3, 2, 1]
284 call('reverse', [l])
285 l->assert_equal([1, 2, 3])
286enddef
287
Bram Moolenaarc5809432021-03-27 21:23:30 +0100288def Test_ch_logfile()
Bram Moolenaar886e5e72021-04-05 13:36:34 +0200289 if !has('channel')
290 CheckFeature channel
291 endif
Yegappan Lakshmanana26f56f2021-07-03 11:58:12 +0200292 assert_fails('ch_logfile(true)', 'E1174:')
293 assert_fails('ch_logfile("foo", true)', 'E1174:')
Bram Moolenaarc5809432021-03-27 21:23:30 +0100294enddef
295
Bram Moolenaar94738d82020-10-21 14:25:07 +0200296def Test_char2nr()
297 char2nr('あ', true)->assert_equal(12354)
Bram Moolenaarc5809432021-03-27 21:23:30 +0100298
Yegappan Lakshmanana26f56f2021-07-03 11:58:12 +0200299 assert_fails('char2nr(true)', 'E1174:')
Bram Moolenaarc5809432021-03-27 21:23:30 +0100300enddef
301
302def Test_charclass()
Yegappan Lakshmanana26f56f2021-07-03 11:58:12 +0200303 assert_fails('charclass(true)', 'E1174:')
Bram Moolenaarc5809432021-03-27 21:23:30 +0100304enddef
305
306def Test_chdir()
Yegappan Lakshmanana26f56f2021-07-03 11:58:12 +0200307 assert_fails('chdir(true)', 'E1174:')
308enddef
309
310def Test_cindent()
311 CheckDefFailure(['cindent([])'], 'E1013: Argument 1: type mismatch, expected string but got list<unknown>')
312 CheckDefFailure(['cindent(null)'], 'E1013: Argument 1: type mismatch, expected string but got special')
313 assert_equal(-1, cindent(0))
314 assert_equal(0, cindent('.'))
Bram Moolenaar94738d82020-10-21 14:25:07 +0200315enddef
316
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +0200317def Test_clearmatches()
318 CheckDefFailure(['echo clearmatches("x")'], 'E1013: Argument 1: type mismatch, expected number but got string')
319enddef
320
Bram Moolenaar94738d82020-10-21 14:25:07 +0200321def Test_col()
322 new
323 setline(1, 'asdf')
324 col([1, '$'])->assert_equal(5)
Bram Moolenaarc5809432021-03-27 21:23:30 +0100325
Yegappan Lakshmanana26f56f2021-07-03 11:58:12 +0200326 assert_fails('col(true)', 'E1174:')
Bram Moolenaarc5809432021-03-27 21:23:30 +0100327enddef
328
329def Test_confirm()
330 if !has('dialog_con') && !has('dialog_gui')
331 CheckFeature dialog_con
332 endif
333
Yegappan Lakshmanana26f56f2021-07-03 11:58:12 +0200334 assert_fails('confirm(true)', 'E1174:')
335 assert_fails('confirm("yes", true)', 'E1174:')
336 assert_fails('confirm("yes", "maybe", 2, true)', 'E1174:')
337enddef
338
339def Test_complete_info()
340 CheckDefFailure(['complete_info("")'], 'E1013: Argument 1: type mismatch, expected list<string> but got string')
341 CheckDefFailure(['complete_info({})'], 'E1013: Argument 1: type mismatch, expected list<string> but got dict<unknown>')
342 assert_equal({'pum_visible': 0, 'mode': '', 'selected': -1, 'items': []}, complete_info())
343 assert_equal({'mode': '', 'items': []}, complete_info(['mode', 'items']))
Bram Moolenaar94738d82020-10-21 14:25:07 +0200344enddef
345
346def Test_copy_return_type()
347 var l = copy([1, 2, 3])
348 var res = 0
349 for n in l
350 res += n
351 endfor
352 res->assert_equal(6)
353
354 var dl = deepcopy([1, 2, 3])
355 res = 0
356 for n in dl
357 res += n
358 endfor
359 res->assert_equal(6)
360
361 dl = deepcopy([1, 2, 3], true)
362enddef
363
364def Test_count()
365 count('ABC ABC ABC', 'b', true)->assert_equal(3)
366 count('ABC ABC ABC', 'b', false)->assert_equal(0)
367enddef
368
Bram Moolenaar9a963372020-12-21 21:58:46 +0100369def Test_cursor()
370 new
371 setline(1, range(4))
372 cursor(2, 1)
373 assert_equal(2, getcurpos()[1])
374 cursor('$', 1)
375 assert_equal(4, getcurpos()[1])
376
377 var lines =<< trim END
378 cursor('2', 1)
379 END
380 CheckDefExecAndScriptFailure(lines, 'E475:')
381enddef
382
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +0200383def Test_debugbreak()
384 CheckMSWindows
385 CheckDefFailure(['echo debugbreak("x")'], 'E1013: Argument 1: type mismatch, expected number but got string')
386enddef
387
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100388def Test_delete()
389 var res: bool = delete('doesnotexist')
390 assert_equal(true, res)
391enddef
392
Yegappan Lakshmanana26f56f2021-07-03 11:58:12 +0200393def Test_diff_filler()
394 CheckDefFailure(['diff_filler([])'], 'E1013: Argument 1: type mismatch, expected string but got list<unknown>')
395 CheckDefFailure(['diff_filler(true)'], 'E1013: Argument 1: type mismatch, expected string but got bool')
396 assert_equal(0, diff_filler(1))
397 assert_equal(0, diff_filler('.'))
398enddef
399
400def Test_escape()
401 CheckDefFailure(['escape("a", 10)'], 'E1013: Argument 2: type mismatch, expected string but got number')
402 CheckDefFailure(['escape(10, " ")'], 'E1013: Argument 1: type mismatch, expected string but got number')
403 CheckDefFailure(['escape(true, false)'], 'E1013: Argument 1: type mismatch, expected string but got bool')
404 assert_equal('a\:b', escape("a:b", ":"))
405enddef
406
407def Test_eval()
408 CheckDefFailure(['eval(10)'], 'E1013: Argument 1: type mismatch, expected string but got number')
409 CheckDefFailure(['eval(null)'], 'E1013: Argument 1: type mismatch, expected string but got special')
410 assert_equal(2, eval('1 + 1'))
411enddef
412
Bram Moolenaar7bb4e742020-12-09 12:41:50 +0100413def Test_executable()
Bram Moolenaar2a9d5d32020-12-12 18:58:40 +0100414 assert_false(executable(""))
415 assert_false(executable(test_null_string()))
416
Yegappan Lakshmanana26f56f2021-07-03 11:58:12 +0200417 CheckDefExecFailure(['echo executable(123)'], 'E1013:')
418 CheckDefExecFailure(['echo executable(true)'], 'E1013:')
Bram Moolenaar7bb4e742020-12-09 12:41:50 +0100419enddef
420
Bram Moolenaarca81f0e2021-06-20 14:41:01 +0200421def Test_execute()
422 var res = execute("echo 'hello'")
423 assert_equal("\nhello", res)
424 res = execute(["echo 'here'", "echo 'there'"])
425 assert_equal("\nhere\nthere", res)
426
427 CheckDefFailure(['echo execute(123)'], 'E1013: Argument 1: type mismatch, expected string but got number')
428 CheckDefFailure(['echo execute([123])'], 'E1013: Argument 1: type mismatch, expected list<string> but got list<number>')
429 CheckDefExecFailure(['echo execute(["xx", 123])'], 'E492')
430 CheckDefFailure(['echo execute("xx", 123)'], 'E1013: Argument 2: type mismatch, expected string but got number')
431enddef
432
Bram Moolenaar7bb4e742020-12-09 12:41:50 +0100433def Test_exepath()
Yegappan Lakshmanana26f56f2021-07-03 11:58:12 +0200434 CheckDefExecFailure(['echo exepath(true)'], 'E1013:')
435 CheckDefExecFailure(['echo exepath(v:null)'], 'E1013:')
Bram Moolenaarfa984412021-03-25 22:15:28 +0100436 CheckDefExecFailure(['echo exepath("")'], 'E1175:')
Bram Moolenaar7bb4e742020-12-09 12:41:50 +0100437enddef
438
Yegappan Lakshmanana26f56f2021-07-03 11:58:12 +0200439def Test_exists()
440 CheckDefFailure(['exists(10)'], 'E1013: Argument 1: type mismatch, expected string but got number')
441 call assert_equal(1, exists('&tabstop'))
442enddef
443
Bram Moolenaar94738d82020-10-21 14:25:07 +0200444def Test_expand()
445 split SomeFile
446 expand('%', true, true)->assert_equal(['SomeFile'])
447 close
448enddef
449
Bram Moolenaar02795102021-05-03 21:40:26 +0200450def Test_expandcmd()
451 $FOO = "blue"
452 assert_equal("blue sky", expandcmd("`=$FOO .. ' sky'`"))
453
454 assert_equal("yes", expandcmd("`={a: 'yes'}['a']`"))
455enddef
456
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100457def Test_extend_arg_types()
Bram Moolenaarc03f5c62021-01-31 17:48:30 +0100458 g:number_one = 1
459 g:string_keep = 'keep'
460 var lines =<< trim END
461 assert_equal([1, 2, 3], extend([1, 2], [3]))
462 assert_equal([3, 1, 2], extend([1, 2], [3], 0))
463 assert_equal([1, 3, 2], extend([1, 2], [3], 1))
464 assert_equal([1, 3, 2], extend([1, 2], [3], g:number_one))
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100465
Bram Moolenaarc03f5c62021-01-31 17:48:30 +0100466 assert_equal({a: 1, b: 2, c: 3}, extend({a: 1, b: 2}, {c: 3}))
467 assert_equal({a: 1, b: 4}, extend({a: 1, b: 2}, {b: 4}))
468 assert_equal({a: 1, b: 2}, extend({a: 1, b: 2}, {b: 4}, 'keep'))
469 assert_equal({a: 1, b: 2}, extend({a: 1, b: 2}, {b: 4}, g:string_keep))
Bram Moolenaar193f6202020-11-16 20:08:35 +0100470
Bram Moolenaarc03f5c62021-01-31 17:48:30 +0100471 var res: list<dict<any>>
472 extend(res, mapnew([1, 2], (_, v) => ({})))
473 assert_equal([{}, {}], res)
474 END
475 CheckDefAndScriptSuccess(lines)
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100476
Yegappan Lakshmanan34fcb692021-05-25 20:14:00 +0200477 CheckDefFailure(['extend("a", 1)'], 'E1013: Argument 1: type mismatch, expected list<any> but got string')
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100478 CheckDefFailure(['extend([1, 2], 3)'], 'E1013: Argument 2: type mismatch, expected list<number> but got number')
479 CheckDefFailure(['extend([1, 2], ["x"])'], 'E1013: Argument 2: type mismatch, expected list<number> but got list<string>')
480 CheckDefFailure(['extend([1, 2], [3], "x")'], 'E1013: Argument 3: type mismatch, expected number but got string')
481
Bram Moolenaare0de1712020-12-02 17:36:54 +0100482 CheckDefFailure(['extend({a: 1}, 42)'], 'E1013: Argument 2: type mismatch, expected dict<number> but got number')
483 CheckDefFailure(['extend({a: 1}, {b: "x"})'], 'E1013: Argument 2: type mismatch, expected dict<number> but got dict<string>')
484 CheckDefFailure(['extend({a: 1}, {b: 2}, 1)'], 'E1013: Argument 3: type mismatch, expected string but got number')
Bram Moolenaar351ead02021-01-16 16:07:01 +0100485
486 CheckDefFailure(['extend([1], ["b"])'], 'E1013: Argument 2: type mismatch, expected list<number> but got list<string>')
Bram Moolenaare32e5162021-01-21 20:21:29 +0100487 CheckDefExecFailure(['extend([1], ["b", 1])'], 'E1013: Argument 2: type mismatch, expected list<number> but got list<any>')
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100488enddef
489
Bram Moolenaarb0e6b512021-01-12 20:23:40 +0100490def Test_extendnew()
491 assert_equal([1, 2, 'a'], extendnew([1, 2], ['a']))
492 assert_equal({one: 1, two: 'a'}, extendnew({one: 1}, {two: 'a'}))
493
494 CheckDefFailure(['extendnew({a: 1}, 42)'], 'E1013: Argument 2: type mismatch, expected dict<number> but got number')
495 CheckDefFailure(['extendnew({a: 1}, [42])'], 'E1013: Argument 2: type mismatch, expected dict<number> but got list<number>')
496 CheckDefFailure(['extendnew([1, 2], "x")'], 'E1013: Argument 2: type mismatch, expected list<number> but got string')
497 CheckDefFailure(['extendnew([1, 2], {x: 1})'], 'E1013: Argument 2: type mismatch, expected list<number> but got dict<number>')
498enddef
499
Bram Moolenaar94738d82020-10-21 14:25:07 +0200500def Test_extend_return_type()
501 var l = extend([1, 2], [3])
502 var res = 0
503 for n in l
504 res += n
505 endfor
506 res->assert_equal(6)
507enddef
508
Bram Moolenaaraa210a32021-01-02 15:41:03 +0100509func g:ExtendDict(d)
510 call extend(a:d, #{xx: 'x'})
511endfunc
512
513def Test_extend_dict_item_type()
514 var lines =<< trim END
515 var d: dict<number> = {a: 1}
516 extend(d, {b: 2})
517 END
518 CheckDefAndScriptSuccess(lines)
519
520 lines =<< trim END
521 var d: dict<number> = {a: 1}
522 extend(d, {b: 'x'})
523 END
Bram Moolenaarc03f5c62021-01-31 17:48:30 +0100524 CheckDefAndScriptFailure(lines, 'E1013: Argument 2: type mismatch, expected dict<number> but got dict<string>', 2)
Bram Moolenaaraa210a32021-01-02 15:41:03 +0100525
526 lines =<< trim END
527 var d: dict<number> = {a: 1}
528 g:ExtendDict(d)
529 END
530 CheckDefExecFailure(lines, 'E1012: Type mismatch; expected number but got string', 0)
531 CheckScriptFailure(['vim9script'] + lines, 'E1012:', 1)
532enddef
533
534func g:ExtendList(l)
535 call extend(a:l, ['x'])
536endfunc
537
538def Test_extend_list_item_type()
539 var lines =<< trim END
540 var l: list<number> = [1]
541 extend(l, [2])
542 END
543 CheckDefAndScriptSuccess(lines)
544
545 lines =<< trim END
546 var l: list<number> = [1]
547 extend(l, ['x'])
548 END
Bram Moolenaarc03f5c62021-01-31 17:48:30 +0100549 CheckDefAndScriptFailure(lines, 'E1013: Argument 2: type mismatch, expected list<number> but got list<string>', 2)
Bram Moolenaaraa210a32021-01-02 15:41:03 +0100550
551 lines =<< trim END
552 var l: list<number> = [1]
553 g:ExtendList(l)
554 END
555 CheckDefExecFailure(lines, 'E1012: Type mismatch; expected number but got string', 0)
556 CheckScriptFailure(['vim9script'] + lines, 'E1012:', 1)
557enddef
Bram Moolenaar94738d82020-10-21 14:25:07 +0200558
Bram Moolenaar93e1cae2021-03-13 21:24:56 +0100559def Test_extend_with_error_function()
560 var lines =<< trim END
561 vim9script
562 def F()
563 {
564 var m = 10
565 }
566 echo m
567 enddef
568
569 def Test()
570 var d: dict<any> = {}
571 d->extend({A: 10, Func: function('F', [])})
572 enddef
573
574 Test()
575 END
576 CheckScriptFailure(lines, 'E1001: Variable not found: m')
577enddef
578
Yegappan Lakshmanana26f56f2021-07-03 11:58:12 +0200579def Test_feedkeys()
580 CheckDefFailure(['feedkeys(10)'], 'E1013: Argument 1: type mismatch, expected string but got number')
581 CheckDefFailure(['feedkeys("x", 10)'], 'E1013: Argument 2: type mismatch, expected string but got number')
582 CheckDefFailure(['feedkeys([], {})'], 'E1013: Argument 1: type mismatch, expected string but got list<unknown>')
583 g:TestVar = 1
584 feedkeys(":g:TestVar = 789\n", 'xt')
585 assert_equal(789, g:TestVar)
586 unlet g:TestVar
587enddef
588
Bram Moolenaar64ed4d42021-01-12 21:22:31 +0100589def Test_job_info_return_type()
590 if has('job')
591 job_start(&shell)
592 var jobs = job_info()
Bram Moolenaara47e05f2021-01-12 21:49:00 +0100593 assert_equal('list<job>', typename(jobs))
594 assert_equal('dict<any>', typename(job_info(jobs[0])))
Bram Moolenaar64ed4d42021-01-12 21:22:31 +0100595 job_stop(jobs[0])
596 endif
597enddef
598
Bram Moolenaar7bb4e742020-12-09 12:41:50 +0100599def Test_filereadable()
Bram Moolenaar2a9d5d32020-12-12 18:58:40 +0100600 assert_false(filereadable(""))
601 assert_false(filereadable(test_null_string()))
602
Yegappan Lakshmanana26f56f2021-07-03 11:58:12 +0200603 CheckDefExecFailure(['echo filereadable(123)'], 'E1013:')
604 CheckDefExecFailure(['echo filereadable(true)'], 'E1013:')
Bram Moolenaar7bb4e742020-12-09 12:41:50 +0100605enddef
606
607def Test_filewritable()
Bram Moolenaar2a9d5d32020-12-12 18:58:40 +0100608 assert_false(filewritable(""))
609 assert_false(filewritable(test_null_string()))
610
Yegappan Lakshmanana26f56f2021-07-03 11:58:12 +0200611 CheckDefExecFailure(['echo filewritable(123)'], 'E1013:')
612 CheckDefExecFailure(['echo filewritable(true)'], 'E1013:')
Bram Moolenaar7bb4e742020-12-09 12:41:50 +0100613enddef
614
615def Test_finddir()
Bram Moolenaarf28f2ac2021-03-22 22:21:26 +0100616 CheckDefExecFailure(['echo finddir(true)'], 'E1174:')
617 CheckDefExecFailure(['echo finddir(v:null)'], 'E1174:')
Bram Moolenaarfa984412021-03-25 22:15:28 +0100618 CheckDefExecFailure(['echo finddir("")'], 'E1175:')
Bram Moolenaar7bb4e742020-12-09 12:41:50 +0100619enddef
620
621def Test_findfile()
Bram Moolenaarf28f2ac2021-03-22 22:21:26 +0100622 CheckDefExecFailure(['echo findfile(true)'], 'E1174:')
623 CheckDefExecFailure(['echo findfile(v:null)'], 'E1174:')
Bram Moolenaarfa984412021-03-25 22:15:28 +0100624 CheckDefExecFailure(['echo findfile("")'], 'E1175:')
Bram Moolenaar7bb4e742020-12-09 12:41:50 +0100625enddef
626
Bram Moolenaar3b690062021-02-01 20:14:51 +0100627def Test_flattennew()
628 var lines =<< trim END
629 var l = [1, [2, [3, 4]], 5]
630 call assert_equal([1, 2, 3, 4, 5], flattennew(l))
631 call assert_equal([1, [2, [3, 4]], 5], l)
632
633 call assert_equal([1, 2, [3, 4], 5], flattennew(l, 1))
634 call assert_equal([1, [2, [3, 4]], 5], l)
635 END
636 CheckDefAndScriptSuccess(lines)
637
638 lines =<< trim END
639 echo flatten([1, 2, 3])
640 END
641 CheckDefAndScriptFailure(lines, 'E1158:')
642enddef
643
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +0200644" Test for float functions argument type
645def Test_float_funcs_args()
646 CheckFeature float
647
648 # acos()
649 CheckDefFailure(['echo acos("a")'], 'E1013: Argument 1: type mismatch, expected number but got string')
650 # asin()
651 CheckDefFailure(['echo asin("a")'], 'E1013: Argument 1: type mismatch, expected number but got string')
652 # atan()
653 CheckDefFailure(['echo atan("a")'], 'E1013: Argument 1: type mismatch, expected number but got string')
654 # atan2()
655 CheckDefFailure(['echo atan2("a", 1.1)'], 'E1013: Argument 1: type mismatch, expected number but got string')
656 CheckDefFailure(['echo atan2(1.2, "a")'], 'E1013: Argument 2: type mismatch, expected number but got string')
657 CheckDefFailure(['echo atan2(1.2)'], 'E119:')
658 # ceil()
659 CheckDefFailure(['echo ceil("a")'], 'E1013: Argument 1: type mismatch, expected number but got string')
660 # cos()
661 CheckDefFailure(['echo cos("a")'], 'E1013: Argument 1: type mismatch, expected number but got string')
662 # cosh()
663 CheckDefFailure(['echo cosh("a")'], 'E1013: Argument 1: type mismatch, expected number but got string')
664 # exp()
665 CheckDefFailure(['echo exp("a")'], 'E1013: Argument 1: type mismatch, expected number but got string')
666 # float2nr()
667 CheckDefFailure(['echo float2nr("a")'], 'E1013: Argument 1: type mismatch, expected number but got string')
668 # floor()
669 CheckDefFailure(['echo floor("a")'], 'E1013: Argument 1: type mismatch, expected number but got string')
670 # fmod()
671 CheckDefFailure(['echo fmod(1.1, "a")'], 'E1013: Argument 2: type mismatch, expected number but got string')
672 CheckDefFailure(['echo fmod("a", 1.1)'], 'E1013: Argument 1: type mismatch, expected number but got string')
673 CheckDefFailure(['echo fmod(1.1)'], 'E119:')
674 # isinf()
675 CheckDefFailure(['echo isinf("a")'], 'E1013: Argument 1: type mismatch, expected number but got string')
676 # isnan()
677 CheckDefFailure(['echo isnan("a")'], 'E1013: Argument 1: type mismatch, expected number but got string')
678 # log()
679 CheckDefFailure(['echo log("a")'], 'E1013: Argument 1: type mismatch, expected number but got string')
680 # log10()
681 CheckDefFailure(['echo log10("a")'], 'E1013: Argument 1: type mismatch, expected number but got string')
682 # pow()
683 CheckDefFailure(['echo pow("a", 1.1)'], 'E1013: Argument 1: type mismatch, expected number but got string')
684 CheckDefFailure(['echo pow(1.1, "a")'], 'E1013: Argument 2: type mismatch, expected number but got string')
685 CheckDefFailure(['echo pow(1.1)'], 'E119:')
686 # round()
687 CheckDefFailure(['echo round("a")'], 'E1013: Argument 1: type mismatch, expected number but got string')
688 # sin()
689 CheckDefFailure(['echo sin("a")'], 'E1013: Argument 1: type mismatch, expected number but got string')
690 # sinh()
691 CheckDefFailure(['echo sinh("a")'], 'E1013: Argument 1: type mismatch, expected number but got string')
692 # sqrt()
693 CheckDefFailure(['echo sqrt("a")'], 'E1013: Argument 1: type mismatch, expected number but got string')
694 # tan()
695 CheckDefFailure(['echo tan("a")'], 'E1013: Argument 1: type mismatch, expected number but got string')
696 # tanh()
697 CheckDefFailure(['echo tanh("a")'], 'E1013: Argument 1: type mismatch, expected number but got string')
698 # trunc()
699 CheckDefFailure(['echo trunc("a")'], 'E1013: Argument 1: type mismatch, expected number but got string')
700enddef
701
Yegappan Lakshmanana26f56f2021-07-03 11:58:12 +0200702def Test_fnameescape()
703 CheckDefFailure(['fnameescape(10)'], 'E1013: Argument 1: type mismatch, expected string but got number')
704 assert_equal('\+a\%b\|', fnameescape('+a%b|'))
705enddef
706
Bram Moolenaar7bb4e742020-12-09 12:41:50 +0100707def Test_fnamemodify()
Bram Moolenaar2a9d5d32020-12-12 18:58:40 +0100708 CheckDefSuccess(['echo fnamemodify(test_null_string(), ":p")'])
709 CheckDefSuccess(['echo fnamemodify("", ":p")'])
710 CheckDefSuccess(['echo fnamemodify("file", test_null_string())'])
711 CheckDefSuccess(['echo fnamemodify("file", "")'])
712
Yegappan Lakshmanana26f56f2021-07-03 11:58:12 +0200713 CheckDefExecFailure(['echo fnamemodify(true, ":p")'], 'E1013: Argument 1: type mismatch, expected string but got bool')
714 CheckDefExecFailure(['echo fnamemodify(v:null, ":p")'], 'E1013: Argument 1: type mismatch, expected string but got special')
715 CheckDefExecFailure(['echo fnamemodify("file", true)'], 'E1013: Argument 2: type mismatch, expected string but got bool')
Bram Moolenaar7bb4e742020-12-09 12:41:50 +0100716enddef
717
Bram Moolenaar2e5910b2021-02-03 17:41:24 +0100718def Wrong_dict_key_type(items: list<number>): list<number>
719 return filter(items, (_, val) => get({[val]: 1}, 'x'))
720enddef
721
Bram Moolenaar94738d82020-10-21 14:25:07 +0200722def Test_filter_wrong_dict_key_type()
Bram Moolenaar2e5910b2021-02-03 17:41:24 +0100723 assert_fails('Wrong_dict_key_type([1, v:null, 3])', 'E1013:')
Bram Moolenaar94738d82020-10-21 14:25:07 +0200724enddef
725
726def Test_filter_return_type()
Bram Moolenaarbb8a7ce2021-04-10 20:10:26 +0200727 var l = filter([1, 2, 3], (_, _) => 1)
Bram Moolenaar94738d82020-10-21 14:25:07 +0200728 var res = 0
729 for n in l
730 res += n
731 endfor
732 res->assert_equal(6)
733enddef
734
Bram Moolenaarfc0e8f52020-12-25 20:24:51 +0100735def Test_filter_missing_argument()
736 var dict = {aa: [1], ab: [2], ac: [3], de: [4]}
Bram Moolenaarbb8a7ce2021-04-10 20:10:26 +0200737 var res = dict->filter((k, _) => k =~ 'a' && k !~ 'b')
Bram Moolenaarfc0e8f52020-12-25 20:24:51 +0100738 res->assert_equal({aa: [1], ac: [3]})
739enddef
Bram Moolenaar94738d82020-10-21 14:25:07 +0200740
Yegappan Lakshmanana26f56f2021-07-03 11:58:12 +0200741def Test_foldclosed()
742 CheckDefFailure(['foldclosed(function("min"))'], 'E1013: Argument 1: type mismatch, expected string but got func(...): any')
743 assert_equal(-1, foldclosed(1))
744 assert_equal(-1, foldclosed('$'))
745enddef
746
747def Test_foldclosedend()
748 CheckDefFailure(['foldclosedend(true)'], 'E1013: Argument 1: type mismatch, expected string but got bool')
749 assert_equal(-1, foldclosedend(1))
750 assert_equal(-1, foldclosedend('w0'))
751enddef
752
753def Test_foldlevel()
754 CheckDefFailure(['foldlevel(0z10)'], 'E1013: Argument 1: type mismatch, expected string but got blob')
755 assert_equal(0, foldlevel(1))
756 assert_equal(0, foldlevel('.'))
757enddef
758
759def Test_foldtextresult()
760 CheckDefFailure(['foldtextresult(1.1)'], 'E1013: Argument 1: type mismatch, expected string but got float')
761 assert_equal('', foldtextresult(1))
762 assert_equal('', foldtextresult('.'))
763enddef
764
Bram Moolenaar7d840e92021-05-26 21:10:11 +0200765def Test_fullcommand()
766 assert_equal('next', fullcommand('n'))
767 assert_equal('noremap', fullcommand('no'))
768 assert_equal('noremap', fullcommand('nor'))
769 assert_equal('normal', fullcommand('norm'))
770
771 assert_equal('', fullcommand('k'))
772 assert_equal('keepmarks', fullcommand('ke'))
773 assert_equal('keepmarks', fullcommand('kee'))
774 assert_equal('keepmarks', fullcommand('keep'))
775 assert_equal('keepjumps', fullcommand('keepj'))
776
777 assert_equal('dlist', fullcommand('dl'))
778 assert_equal('', fullcommand('dp'))
779 assert_equal('delete', fullcommand('del'))
780 assert_equal('', fullcommand('dell'))
781 assert_equal('', fullcommand('delp'))
782
783 assert_equal('srewind', fullcommand('sre'))
784 assert_equal('scriptnames', fullcommand('scr'))
785 assert_equal('', fullcommand('scg'))
786enddef
787
Bram Moolenaar94738d82020-10-21 14:25:07 +0200788def Test_garbagecollect()
789 garbagecollect(true)
790enddef
791
792def Test_getbufinfo()
793 var bufinfo = getbufinfo(bufnr())
794 getbufinfo('%')->assert_equal(bufinfo)
795
796 edit Xtestfile1
797 hide edit Xtestfile2
798 hide enew
Bram Moolenaare0de1712020-12-02 17:36:54 +0100799 getbufinfo({bufloaded: true, buflisted: true, bufmodified: false})
Bram Moolenaar94738d82020-10-21 14:25:07 +0200800 ->len()->assert_equal(3)
801 bwipe Xtestfile1 Xtestfile2
802enddef
803
804def Test_getbufline()
805 e SomeFile
806 var buf = bufnr()
807 e #
808 var lines = ['aaa', 'bbb', 'ccc']
809 setbufline(buf, 1, lines)
810 getbufline('#', 1, '$')->assert_equal(lines)
Bram Moolenaare6e70a12020-10-22 18:23:38 +0200811 getbufline(-1, '$', '$')->assert_equal([])
812 getbufline(-1, 1, '$')->assert_equal([])
Bram Moolenaar94738d82020-10-21 14:25:07 +0200813
814 bwipe!
815enddef
816
817def Test_getchangelist()
818 new
819 setline(1, 'some text')
820 var changelist = bufnr()->getchangelist()
821 getchangelist('%')->assert_equal(changelist)
822 bwipe!
823enddef
824
825def Test_getchar()
826 while getchar(0)
827 endwhile
828 getchar(true)->assert_equal(0)
829enddef
830
Bram Moolenaar7ad67d12021-03-10 16:08:26 +0100831def Test_getenv()
832 if getenv('does-not_exist') == ''
833 assert_report('getenv() should return null')
834 endif
835 if getenv('does-not_exist') == null
836 else
837 assert_report('getenv() should return null')
838 endif
839 $SOMEENVVAR = 'some'
840 assert_equal('some', getenv('SOMEENVVAR'))
841 unlet $SOMEENVVAR
842enddef
843
Bram Moolenaar94738d82020-10-21 14:25:07 +0200844def Test_getcompletion()
845 set wildignore=*.vim,*~
846 var l = getcompletion('run', 'file', true)
847 l->assert_equal([])
848 set wildignore&
849enddef
850
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +0200851def Test_getcurpos()
852 CheckDefFailure(['echo getcursorcharpos("x")'], 'E1013: Argument 1: type mismatch, expected number but got string')
853enddef
854
855def Test_getcursorcharpos()
856 CheckDefFailure(['echo getcursorcharpos("x")'], 'E1013: Argument 1: type mismatch, expected number but got string')
857enddef
858
859def Test_getcwd()
860 CheckDefFailure(['echo getcwd("x")'], 'E1013: Argument 1: type mismatch, expected number but got string')
861 CheckDefFailure(['echo getcwd("x", 1)'], 'E1013: Argument 1: type mismatch, expected number but got string')
862 CheckDefFailure(['echo getcwd(1, "x")'], 'E1013: Argument 2: type mismatch, expected number but got string')
863enddef
864
Bram Moolenaar94738d82020-10-21 14:25:07 +0200865def Test_getloclist_return_type()
866 var l = getloclist(1)
867 l->assert_equal([])
868
Bram Moolenaare0de1712020-12-02 17:36:54 +0100869 var d = getloclist(1, {items: 0})
870 d->assert_equal({items: []})
Bram Moolenaar94738d82020-10-21 14:25:07 +0200871enddef
872
Yegappan Lakshmanana26f56f2021-07-03 11:58:12 +0200873def Test_getfontname()
874 CheckDefFailure(['getfontname(10)'], 'E1013: Argument 1: type mismatch, expected string but got number')
875enddef
876
Bram Moolenaar7bb4e742020-12-09 12:41:50 +0100877def Test_getfperm()
Bram Moolenaar2a9d5d32020-12-12 18:58:40 +0100878 assert_equal('', getfperm(""))
879 assert_equal('', getfperm(test_null_string()))
880
Yegappan Lakshmanana26f56f2021-07-03 11:58:12 +0200881 CheckDefExecFailure(['echo getfperm(true)'], 'E1013:')
882 CheckDefExecFailure(['echo getfperm(v:null)'], 'E1013:')
Bram Moolenaar7bb4e742020-12-09 12:41:50 +0100883enddef
884
885def Test_getfsize()
Bram Moolenaar2a9d5d32020-12-12 18:58:40 +0100886 assert_equal(-1, getfsize(""))
887 assert_equal(-1, getfsize(test_null_string()))
888
Yegappan Lakshmanana26f56f2021-07-03 11:58:12 +0200889 CheckDefExecFailure(['echo getfsize(true)'], 'E1013:')
890 CheckDefExecFailure(['echo getfsize(v:null)'], 'E1013:')
Bram Moolenaar7bb4e742020-12-09 12:41:50 +0100891enddef
892
893def Test_getftime()
Bram Moolenaar2a9d5d32020-12-12 18:58:40 +0100894 assert_equal(-1, getftime(""))
895 assert_equal(-1, getftime(test_null_string()))
896
Yegappan Lakshmanana26f56f2021-07-03 11:58:12 +0200897 CheckDefExecFailure(['echo getftime(true)'], 'E1013:')
898 CheckDefExecFailure(['echo getftime(v:null)'], 'E1013:')
Bram Moolenaar7bb4e742020-12-09 12:41:50 +0100899enddef
900
901def Test_getftype()
Bram Moolenaar2a9d5d32020-12-12 18:58:40 +0100902 assert_equal('', getftype(""))
903 assert_equal('', getftype(test_null_string()))
904
Yegappan Lakshmanana26f56f2021-07-03 11:58:12 +0200905 CheckDefExecFailure(['echo getftype(true)'], 'E1013:')
906 CheckDefExecFailure(['echo getftype(v:null)'], 'E1013:')
Bram Moolenaar7bb4e742020-12-09 12:41:50 +0100907enddef
908
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +0200909def Test_getjumplist()
910 CheckDefFailure(['echo getjumplist("x")'], 'E1013: Argument 1: type mismatch, expected number but got string')
911 CheckDefFailure(['echo getjumplist("x", 1)'], 'E1013: Argument 1: type mismatch, expected number but got string')
912 CheckDefFailure(['echo getjumplist(1, "x")'], 'E1013: Argument 2: type mismatch, expected number but got string')
913enddef
914
Yegappan Lakshmanana26f56f2021-07-03 11:58:12 +0200915def Test_getmarklist()
916 CheckDefFailure(['getmarklist([])'], 'E1013: Argument 1: type mismatch, expected string but got list<unknown>')
917 assert_equal([], getmarklist(10000))
918 assert_fails('getmarklist("a%b@#")', 'E94:')
919enddef
920
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +0200921def Test_getmatches()
922 CheckDefFailure(['echo getmatches("x")'], 'E1013: Argument 1: type mismatch, expected number but got string')
923enddef
924
Yegappan Lakshmanana26f56f2021-07-03 11:58:12 +0200925def Test_getpos()
926 CheckDefFailure(['getpos(10)'], 'E1013: Argument 1: type mismatch, expected string but got number')
927 assert_equal([0, 1, 1, 0], getpos('.'))
928 assert_equal([0, 0, 0, 0], getpos('a'))
929enddef
930
931def Test_getqflist()
932 CheckDefFailure(['getqflist([])'], 'E1013: Argument 1: type mismatch, expected dict<any> but got list<unknown>')
933 call assert_equal({}, getqflist({}))
934enddef
935
Bram Moolenaar94738d82020-10-21 14:25:07 +0200936def Test_getqflist_return_type()
937 var l = getqflist()
938 l->assert_equal([])
939
Bram Moolenaare0de1712020-12-02 17:36:54 +0100940 var d = getqflist({items: 0})
941 d->assert_equal({items: []})
Bram Moolenaar94738d82020-10-21 14:25:07 +0200942enddef
943
944def Test_getreg()
945 var lines = ['aaa', 'bbb', 'ccc']
946 setreg('a', lines)
947 getreg('a', true, true)->assert_equal(lines)
Bram Moolenaar418a29f2021-02-10 22:23:41 +0100948 assert_fails('getreg("ab")', 'E1162:')
Bram Moolenaar94738d82020-10-21 14:25:07 +0200949enddef
950
951def Test_getreg_return_type()
952 var s1: string = getreg('"')
953 var s2: string = getreg('"', 1)
954 var s3: list<string> = getreg('"', 1, 1)
955enddef
956
Bram Moolenaar418a29f2021-02-10 22:23:41 +0100957def Test_getreginfo()
958 var text = 'abc'
959 setreg('a', text)
960 getreginfo('a')->assert_equal({regcontents: [text], regtype: 'v', isunnamed: false})
961 assert_fails('getreginfo("ab")', 'E1162:')
962enddef
963
964def Test_getregtype()
965 var lines = ['aaa', 'bbb', 'ccc']
966 setreg('a', lines)
967 getregtype('a')->assert_equal('V')
968 assert_fails('getregtype("ab")', 'E1162:')
969enddef
970
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +0200971def Test_gettabinfo()
972 CheckDefFailure(['echo gettabinfo("x")'], 'E1013: Argument 1: type mismatch, expected number but got string')
973enddef
974
975def Test_gettagstack()
976 CheckDefFailure(['echo gettagstack("x")'], 'E1013: Argument 1: type mismatch, expected number but got string')
977enddef
978
Yegappan Lakshmanana26f56f2021-07-03 11:58:12 +0200979def Test_gettext()
980 CheckDefFailure(['gettext(10)'], 'E1013: Argument 1: type mismatch, expected string but got number')
981 assert_equal('abc', gettext("abc"))
982enddef
983
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +0200984def Test_getwininfo()
985 CheckDefFailure(['echo getwininfo("x")'], 'E1013: Argument 1: type mismatch, expected number but got string')
986enddef
987
988def Test_getwinpos()
989 CheckDefFailure(['echo getwinpos("x")'], 'E1013: Argument 1: type mismatch, expected number but got string')
990enddef
991
Bram Moolenaar94738d82020-10-21 14:25:07 +0200992def Test_glob()
993 glob('runtest.vim', true, true, true)->assert_equal(['runtest.vim'])
994enddef
995
Yegappan Lakshmanana26f56f2021-07-03 11:58:12 +0200996def Test_glob2regpat()
997 CheckDefFailure(['glob2regpat(null)'], 'E1013: Argument 1: type mismatch, expected string but got special')
998 assert_equal('^$', glob2regpat(''))
999enddef
1000
Bram Moolenaar94738d82020-10-21 14:25:07 +02001001def Test_globpath()
1002 globpath('.', 'runtest.vim', true, true, true)->assert_equal(['./runtest.vim'])
1003enddef
1004
1005def Test_has()
1006 has('eval', true)->assert_equal(1)
1007enddef
1008
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +02001009def Test_haslocaldir()
1010 CheckDefFailure(['echo haslocaldir("x")'], 'E1013: Argument 1: type mismatch, expected number but got string')
1011 CheckDefFailure(['echo haslocaldir("x", 1)'], 'E1013: Argument 1: type mismatch, expected number but got string')
1012 CheckDefFailure(['echo haslocaldir(1, "x")'], 'E1013: Argument 2: type mismatch, expected number but got string')
1013enddef
1014
Bram Moolenaar94738d82020-10-21 14:25:07 +02001015def Test_hasmapto()
1016 hasmapto('foobar', 'i', true)->assert_equal(0)
1017 iabbrev foo foobar
1018 hasmapto('foobar', 'i', true)->assert_equal(1)
1019 iunabbrev foo
1020enddef
1021
Yegappan Lakshmanana26f56f2021-07-03 11:58:12 +02001022def Test_histadd()
1023 CheckDefFailure(['histadd(1, "x")'], 'E1013: Argument 1: type mismatch, expected string but got number')
1024 CheckDefFailure(['histadd(":", 10)'], 'E1013: Argument 2: type mismatch, expected string but got number')
1025 histadd("search", 'skyblue')
1026 assert_equal('skyblue', histget('/', -1))
1027enddef
1028
1029def Test_histnr()
1030 CheckDefFailure(['histnr(10)'], 'E1013: Argument 1: type mismatch, expected string but got number')
1031 assert_equal(-1, histnr('abc'))
1032enddef
1033
1034def Test_hlID()
1035 CheckDefFailure(['hlID(10)'], 'E1013: Argument 1: type mismatch, expected string but got number')
1036 assert_equal(0, hlID('NonExistingHighlight'))
1037enddef
1038
1039def Test_hlexists()
1040 CheckDefFailure(['hlexists([])'], 'E1013: Argument 1: type mismatch, expected string but got list<unknown>')
1041 assert_equal(0, hlexists('NonExistingHighlight'))
1042enddef
1043
1044def Test_iconv()
1045 CheckDefFailure(['iconv(1, "from", "to")'], 'E1013: Argument 1: type mismatch, expected string but got number')
1046 CheckDefFailure(['iconv("abc", 10, "to")'], 'E1013: Argument 2: type mismatch, expected string but got number')
1047 CheckDefFailure(['iconv("abc", "from", 20)'], 'E1013: Argument 3: type mismatch, expected string but got number')
1048 assert_equal('abc', iconv('abc', 'fromenc', 'toenc'))
1049enddef
1050
Bram Moolenaar94738d82020-10-21 14:25:07 +02001051def Test_index()
1052 index(['a', 'b', 'a', 'B'], 'b', 2, true)->assert_equal(3)
1053enddef
1054
Yegappan Lakshmanana26f56f2021-07-03 11:58:12 +02001055def Test_inputlist()
1056 CheckDefFailure(['inputlist(10)'], 'E1013: Argument 1: type mismatch, expected list<string> but got number')
1057 CheckDefFailure(['inputlist("abc")'], 'E1013: Argument 1: type mismatch, expected list<string> but got string')
1058 CheckDefFailure(['inputlist([1, 2, 3])'], 'E1013: Argument 1: type mismatch, expected list<string> but got list<number>')
1059 feedkeys("2\<CR>", 't')
1060 var r: number = inputlist(['a', 'b', 'c'])
1061 assert_equal(2, r)
1062enddef
1063
1064def Test_inputsecret()
1065 CheckDefFailure(['inputsecret(10)'], 'E1013: Argument 1: type mismatch, expected string but got number')
1066 CheckDefFailure(['inputsecret("Pass:", 20)'], 'E1013: Argument 2: type mismatch, expected string but got number')
1067 feedkeys("\<CR>", 't')
1068 var ans: string = inputsecret('Pass:', '123')
1069 assert_equal('123', ans)
1070enddef
1071
Bram Moolenaar193f6202020-11-16 20:08:35 +01001072let s:number_one = 1
1073let s:number_two = 2
1074let s:string_keep = 'keep'
1075
Bram Moolenaarca174532020-10-21 16:42:22 +02001076def Test_insert()
Bram Moolenaar94738d82020-10-21 14:25:07 +02001077 var l = insert([2, 1], 3)
1078 var res = 0
1079 for n in l
1080 res += n
1081 endfor
1082 res->assert_equal(6)
Bram Moolenaarca174532020-10-21 16:42:22 +02001083
Yegappan Lakshmanan34fcb692021-05-25 20:14:00 +02001084 var m: any = []
1085 insert(m, 4)
1086 call assert_equal([4], m)
1087 extend(m, [6], 0)
1088 call assert_equal([6, 4], m)
1089
Bram Moolenaar39211cb2021-04-18 15:48:04 +02001090 var lines =<< trim END
1091 insert(test_null_list(), 123)
1092 END
1093 CheckDefExecAndScriptFailure(lines, 'E1130:', 1)
1094
1095 lines =<< trim END
1096 insert(test_null_blob(), 123)
1097 END
1098 CheckDefExecAndScriptFailure(lines, 'E1131:', 1)
1099
Bram Moolenaarca174532020-10-21 16:42:22 +02001100 assert_equal([1, 2, 3], insert([2, 3], 1))
Bram Moolenaar193f6202020-11-16 20:08:35 +01001101 assert_equal([1, 2, 3], insert([2, 3], s:number_one))
Bram Moolenaarca174532020-10-21 16:42:22 +02001102 assert_equal([1, 2, 3], insert([1, 2], 3, 2))
Bram Moolenaar193f6202020-11-16 20:08:35 +01001103 assert_equal([1, 2, 3], insert([1, 2], 3, s:number_two))
Bram Moolenaarca174532020-10-21 16:42:22 +02001104 assert_equal(['a', 'b', 'c'], insert(['b', 'c'], 'a'))
1105 assert_equal(0z1234, insert(0z34, 0x12))
Bram Moolenaar193f6202020-11-16 20:08:35 +01001106
Yegappan Lakshmanan34fcb692021-05-25 20:14:00 +02001107 CheckDefFailure(['insert("a", 1)'], 'E1013: Argument 1: type mismatch, expected list<any> but got string', 1)
Bram Moolenaarca174532020-10-21 16:42:22 +02001108 CheckDefFailure(['insert([2, 3], "a")'], 'E1013: Argument 2: type mismatch, expected number but got string', 1)
1109 CheckDefFailure(['insert([2, 3], 1, "x")'], 'E1013: Argument 3: type mismatch, expected number but got string', 1)
Bram Moolenaar94738d82020-10-21 14:25:07 +02001110enddef
1111
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +02001112def Test_invert()
1113 CheckDefFailure(['echo invert("x")'], 'E1013: Argument 1: type mismatch, expected number but got string')
1114enddef
1115
Yegappan Lakshmanana26f56f2021-07-03 11:58:12 +02001116def Test_isdirectory()
1117 CheckDefFailure(['isdirectory(1.1)'], 'E1013: Argument 1: type mismatch, expected string but got float')
1118 assert_false(isdirectory('NonExistingDir'))
1119enddef
1120
1121def Test_items()
1122 CheckDefFailure(['[]->items()'], 'E1013: Argument 1: type mismatch, expected dict<any> but got list<unknown>')
1123 assert_equal([['a', 10], ['b', 20]], {'a': 10, 'b': 20}->items())
1124 assert_equal([], {}->items())
1125enddef
1126
1127def Test_js_decode()
1128 CheckDefFailure(['js_decode(10)'], 'E1013: Argument 1: type mismatch, expected string but got number')
1129 assert_equal([1, 2], js_decode('[1,2]'))
1130enddef
1131
1132def Test_json_decode()
1133 CheckDefFailure(['json_decode(true)'], 'E1013: Argument 1: type mismatch, expected string but got bool')
1134 assert_equal(1.0, json_decode('1.0'))
1135enddef
1136
1137def Test_keys()
1138 CheckDefFailure(['keys([])'], 'E1013: Argument 1: type mismatch, expected dict<any> but got list<unknown>')
1139 assert_equal(['a'], {a: 'v'}->keys())
1140 assert_equal([], {}->keys())
1141enddef
1142
Bram Moolenaar94738d82020-10-21 14:25:07 +02001143def Test_keys_return_type()
Bram Moolenaare0de1712020-12-02 17:36:54 +01001144 const var: list<string> = {a: 1, b: 2}->keys()
Bram Moolenaar94738d82020-10-21 14:25:07 +02001145 var->assert_equal(['a', 'b'])
1146enddef
1147
Bram Moolenaarc5809432021-03-27 21:23:30 +01001148def Test_line()
Yegappan Lakshmanana26f56f2021-07-03 11:58:12 +02001149 assert_fails('line(true)', 'E1174:')
1150enddef
1151
1152def Test_line2byte()
1153 CheckDefFailure(['line2byte(true)'], 'E1013: Argument 1: type mismatch, expected string but got bool')
1154 assert_equal(-1, line2byte(1))
1155 assert_equal(-1, line2byte(10000))
1156enddef
1157
1158def Test_lispindent()
1159 CheckDefFailure(['lispindent({})'], 'E1013: Argument 1: type mismatch, expected string but got dict<unknown>')
1160 assert_equal(0, lispindent(1))
Bram Moolenaarc5809432021-03-27 21:23:30 +01001161enddef
1162
Bram Moolenaar94738d82020-10-21 14:25:07 +02001163def Test_list2str_str2list_utf8()
1164 var s = "\u3042\u3044"
1165 var l = [0x3042, 0x3044]
1166 str2list(s, true)->assert_equal(l)
1167 list2str(l, true)->assert_equal(s)
1168enddef
1169
1170def SID(): number
1171 return expand('<SID>')
1172 ->matchstr('<SNR>\zs\d\+\ze_$')
1173 ->str2nr()
1174enddef
1175
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +02001176def Test_listener_remove()
1177 CheckDefFailure(['echo listener_remove("x")'], 'E1013: Argument 1: type mismatch, expected number but got string')
1178enddef
1179
Bram Moolenaar70250fb2021-01-16 19:01:53 +01001180def Test_map_function_arg()
1181 var lines =<< trim END
1182 def MapOne(i: number, v: string): string
1183 return i .. ':' .. v
1184 enddef
1185 var l = ['a', 'b', 'c']
1186 map(l, MapOne)
1187 assert_equal(['0:a', '1:b', '2:c'], l)
1188 END
1189 CheckDefAndScriptSuccess(lines)
Bram Moolenaar8da6d6d2021-06-05 18:15:09 +02001190
1191 lines =<< trim END
1192 range(3)->map((a, b, c) => a + b + c)
1193 END
1194 CheckDefExecAndScriptFailure(lines, 'E1190: One argument too few')
1195 lines =<< trim END
1196 range(3)->map((a, b, c, d) => a + b + c + d)
1197 END
1198 CheckDefExecAndScriptFailure(lines, 'E1190: 2 arguments too few')
Bram Moolenaar70250fb2021-01-16 19:01:53 +01001199enddef
1200
1201def Test_map_item_type()
1202 var lines =<< trim END
1203 var l = ['a', 'b', 'c']
1204 map(l, (k, v) => k .. '/' .. v )
1205 assert_equal(['0/a', '1/b', '2/c'], l)
1206 END
1207 CheckDefAndScriptSuccess(lines)
1208
1209 lines =<< trim END
1210 var l: list<number> = [0]
1211 echo map(l, (_, v) => [])
1212 END
1213 CheckDefExecAndScriptFailure(lines, 'E1012: Type mismatch; expected number but got list<unknown>', 2)
1214
1215 lines =<< trim END
1216 var l: list<number> = range(2)
1217 echo map(l, (_, v) => [])
1218 END
1219 CheckDefExecAndScriptFailure(lines, 'E1012: Type mismatch; expected number but got list<unknown>', 2)
1220
1221 lines =<< trim END
1222 var d: dict<number> = {key: 0}
1223 echo map(d, (_, v) => [])
1224 END
1225 CheckDefExecAndScriptFailure(lines, 'E1012: Type mismatch; expected number but got list<unknown>', 2)
1226enddef
1227
Bram Moolenaar94738d82020-10-21 14:25:07 +02001228def Test_maparg()
1229 var lnum = str2nr(expand('<sflnum>'))
1230 map foo bar
Bram Moolenaare0de1712020-12-02 17:36:54 +01001231 maparg('foo', '', false, true)->assert_equal({
Bram Moolenaar94738d82020-10-21 14:25:07 +02001232 lnum: lnum + 1,
1233 script: 0,
1234 mode: ' ',
1235 silent: 0,
1236 noremap: 0,
1237 lhs: 'foo',
1238 lhsraw: 'foo',
1239 nowait: 0,
1240 expr: 0,
1241 sid: SID(),
1242 rhs: 'bar',
1243 buffer: 0})
1244 unmap foo
1245enddef
1246
1247def Test_mapcheck()
1248 iabbrev foo foobar
1249 mapcheck('foo', 'i', true)->assert_equal('foobar')
1250 iunabbrev foo
1251enddef
1252
1253def Test_maparg_mapset()
1254 nnoremap <F3> :echo "hit F3"<CR>
1255 var mapsave = maparg('<F3>', 'n', false, true)
1256 mapset('n', false, mapsave)
1257
1258 nunmap <F3>
1259enddef
1260
Bram Moolenaar027c4ab2021-02-21 16:20:18 +01001261def Test_map_failure()
1262 CheckFeature job
1263
1264 var lines =<< trim END
1265 vim9script
1266 writefile([], 'Xtmpfile')
1267 silent e Xtmpfile
1268 var d = {[bufnr('%')]: {a: 0}}
1269 au BufReadPost * Func()
1270 def Func()
1271 if d->has_key('')
1272 endif
1273 eval d[expand('<abuf>')]->mapnew((_, v: dict<job>) => 0)
1274 enddef
1275 e
1276 END
1277 CheckScriptFailure(lines, 'E1013:')
1278 au! BufReadPost
1279 delete('Xtmpfile')
1280enddef
1281
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +02001282def Test_matcharg()
1283 CheckDefFailure(['echo matcharg("x")'], 'E1013: Argument 1: type mismatch, expected number but got string')
1284enddef
1285
1286def Test_matchdelete()
1287 CheckDefFailure(['echo matchdelete("x")'], 'E1013: Argument 1: type mismatch, expected number but got string')
1288 CheckDefFailure(['echo matchdelete("x", 1)'], 'E1013: Argument 1: type mismatch, expected number but got string')
1289 CheckDefFailure(['echo matchdelete(1, "x")'], 'E1013: Argument 2: type mismatch, expected number but got string')
1290enddef
1291
Bram Moolenaar9ae37052021-01-22 22:31:10 +01001292def Test_max()
1293 g:flag = true
1294 var l1: list<number> = g:flag
1295 ? [1, max([2, 3])]
1296 : [4, 5]
1297 assert_equal([1, 3], l1)
1298
1299 g:flag = false
1300 var l2: list<number> = g:flag
1301 ? [1, max([2, 3])]
1302 : [4, 5]
1303 assert_equal([4, 5], l2)
1304enddef
1305
Yegappan Lakshmanana26f56f2021-07-03 11:58:12 +02001306def Test_menu_info()
1307 CheckDefFailure(['menu_info(10)'], 'E1013: Argument 1: type mismatch, expected string but got number')
1308 CheckDefFailure(['menu_info(10, "n")'], 'E1013: Argument 1: type mismatch, expected string but got number')
1309 CheckDefFailure(['menu_info("File", 10)'], 'E1013: Argument 2: type mismatch, expected string but got number')
1310 assert_equal({}, menu_info('aMenu'))
1311enddef
1312
Bram Moolenaar9ae37052021-01-22 22:31:10 +01001313def Test_min()
1314 g:flag = true
1315 var l1: list<number> = g:flag
1316 ? [1, min([2, 3])]
1317 : [4, 5]
1318 assert_equal([1, 2], l1)
1319
1320 g:flag = false
1321 var l2: list<number> = g:flag
1322 ? [1, min([2, 3])]
1323 : [4, 5]
1324 assert_equal([4, 5], l2)
1325enddef
1326
Yegappan Lakshmanana26f56f2021-07-03 11:58:12 +02001327def Test_nextnonblank()
1328 CheckDefFailure(['nextnonblank(null)'], 'E1013: Argument 1: type mismatch, expected string but got special')
1329 assert_equal(0, nextnonblank(1))
1330enddef
1331
Bram Moolenaar94738d82020-10-21 14:25:07 +02001332def Test_nr2char()
1333 nr2char(97, true)->assert_equal('a')
1334enddef
1335
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +02001336def Test_or()
1337 CheckDefFailure(['echo or("x", 0x2)'], 'E1013: Argument 1: type mismatch, expected number but got string')
1338 CheckDefFailure(['echo or(0x1, "x")'], 'E1013: Argument 2: type mismatch, expected number but got string')
1339enddef
1340
Yegappan Lakshmanana26f56f2021-07-03 11:58:12 +02001341def Test_prevnonblank()
1342 CheckDefFailure(['prevnonblank(null)'], 'E1013: Argument 1: type mismatch, expected string but got special')
1343 assert_equal(0, prevnonblank(1))
1344enddef
1345
1346def Test_prompt_getprompt()
Dominique Pelle74509232021-07-03 19:27:37 +02001347 if has('channel')
1348 CheckDefFailure(['prompt_getprompt([])'], 'E1013: Argument 1: type mismatch, expected string but got list<unknown>')
1349 assert_equal('', prompt_getprompt('NonExistingBuf'))
1350 endif
Yegappan Lakshmanana26f56f2021-07-03 11:58:12 +02001351enddef
1352
1353def Test_rand()
1354 CheckDefFailure(['rand(10)'], 'E1013: Argument 1: type mismatch, expected list<number> but got number')
1355 CheckDefFailure(['rand(["a"])'], 'E1013: Argument 1: type mismatch, expected list<number> but got list<string>')
1356 assert_true(rand() >= 0)
1357 assert_true(rand(srand()) >= 0)
1358enddef
1359
Bram Moolenaar94738d82020-10-21 14:25:07 +02001360def Test_readdir()
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01001361 eval expand('sautest')->readdir((e) => e[0] !=# '.')
1362 eval expand('sautest')->readdirex((e) => e.name[0] !=# '.')
Bram Moolenaar94738d82020-10-21 14:25:07 +02001363enddef
1364
Bram Moolenaarc423ad72021-01-13 20:38:03 +01001365def Test_readblob()
1366 var blob = 0z12341234
1367 writefile(blob, 'Xreadblob')
1368 var read: blob = readblob('Xreadblob')
1369 assert_equal(blob, read)
1370
1371 var lines =<< trim END
1372 var read: list<string> = readblob('Xreadblob')
1373 END
1374 CheckDefAndScriptFailure(lines, 'E1012: Type mismatch; expected list<string> but got blob', 1)
1375 delete('Xreadblob')
1376enddef
1377
1378def Test_readfile()
1379 var text = ['aaa', 'bbb', 'ccc']
1380 writefile(text, 'Xreadfile')
1381 var read: list<string> = readfile('Xreadfile')
1382 assert_equal(text, read)
1383
1384 var lines =<< trim END
1385 var read: dict<string> = readfile('Xreadfile')
1386 END
1387 CheckDefAndScriptFailure(lines, 'E1012: Type mismatch; expected dict<string> but got list<string>', 1)
1388 delete('Xreadfile')
1389enddef
1390
Yegappan Lakshmanana26f56f2021-07-03 11:58:12 +02001391def Test_reltime()
1392 CheckDefFailure(['reltime("x")'], 'E1013: Argument 1: type mismatch, expected list<number> but got string')
1393 CheckDefFailure(['reltime(["x", "y"])'], 'E1013: Argument 1: type mismatch, expected list<number> but got list<string>')
1394 CheckDefFailure(['reltime([1, 2], 10)'], 'E1013: Argument 2: type mismatch, expected list<number> but got number')
1395 CheckDefFailure(['reltime([1, 2], ["a", "b"])'], 'E1013: Argument 2: type mismatch, expected list<number> but got list<string>')
1396 var start: list<any> = reltime()
1397 assert_true(type(reltime(start)) == v:t_list)
1398 var end: list<any> = reltime()
1399 assert_true(type(reltime(start, end)) == v:t_list)
1400enddef
1401
1402def Test_reltimefloat()
1403 CheckDefFailure(['reltimefloat("x")'], 'E1013: Argument 1: type mismatch, expected list<number> but got string')
1404 CheckDefFailure(['reltimefloat([1.1])'], 'E1013: Argument 1: type mismatch, expected list<number> but got list<float>')
1405 assert_true(type(reltimefloat(reltime())) == v:t_float)
1406enddef
1407
1408def Test_reltimestr()
1409 CheckDefFailure(['reltimestr(true)'], 'E1013: Argument 1: type mismatch, expected list<number> but got bool')
1410 CheckDefFailure(['reltimestr([true])'], 'E1013: Argument 1: type mismatch, expected list<number> but got list<bool>')
1411 assert_true(type(reltimestr(reltime())) == v:t_string)
1412enddef
1413
1414def Test_remote_foreground()
1415 CheckFeature clientserver
1416 # remote_foreground() doesn't fail on MS-Windows
1417 CheckNotMSWindows
1418 CheckDefFailure(['remote_foreground(10)'], 'E1013: Argument 1: type mismatch, expected string but got number')
1419 assert_fails('remote_foreground("NonExistingServer")', 'E241:')
1420enddef
1421
1422def Test_remote_startserver()
1423 CheckDefFailure(['remote_startserver({})'], 'E1013: Argument 1: type mismatch, expected string but got dict<unknown>')
1424enddef
1425
Bram Moolenaar94738d82020-10-21 14:25:07 +02001426def Test_remove_return_type()
Bram Moolenaare0de1712020-12-02 17:36:54 +01001427 var l = remove({one: [1, 2], two: [3, 4]}, 'one')
Bram Moolenaar94738d82020-10-21 14:25:07 +02001428 var res = 0
1429 for n in l
1430 res += n
1431 endfor
1432 res->assert_equal(3)
1433enddef
1434
Yegappan Lakshmanana26f56f2021-07-03 11:58:12 +02001435def Test_rename()
1436 CheckDefFailure(['rename(1, "b")'], 'E1013: Argument 1: type mismatch, expected string but got number')
1437 CheckDefFailure(['rename("a", 2)'], 'E1013: Argument 2: type mismatch, expected string but got number')
1438enddef
1439
1440def Test_resolve()
1441 CheckDefFailure(['resolve([])'], 'E1013: Argument 1: type mismatch, expected string but got list<unknown>')
1442 assert_equal('SomeFile', resolve('SomeFile'))
1443enddef
1444
Bram Moolenaar94738d82020-10-21 14:25:07 +02001445def Test_reverse_return_type()
1446 var l = reverse([1, 2, 3])
1447 var res = 0
1448 for n in l
1449 res += n
1450 endfor
1451 res->assert_equal(6)
1452enddef
1453
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +02001454def Test_screenattr()
1455 CheckDefFailure(['echo screenattr("x", 1)'], 'E1013: Argument 1: type mismatch, expected number but got string')
1456 CheckDefFailure(['echo screenattr(1, "x")'], 'E1013: Argument 2: type mismatch, expected number but got string')
1457enddef
1458
1459def Test_screenchar()
1460 CheckDefFailure(['echo screenchar("x", 1)'], 'E1013: Argument 1: type mismatch, expected number but got string')
1461 CheckDefFailure(['echo screenchar(1, "x")'], 'E1013: Argument 2: type mismatch, expected number but got string')
1462enddef
1463
1464def Test_screenchars()
1465 CheckDefFailure(['echo screenchars("x", 1)'], 'E1013: Argument 1: type mismatch, expected number but got string')
1466 CheckDefFailure(['echo screenchars(1, "x")'], 'E1013: Argument 2: type mismatch, expected number but got string')
1467enddef
1468
Yegappan Lakshmanana26f56f2021-07-03 11:58:12 +02001469def Test_screenpos()
1470 CheckDefFailure(['screenpos("a", 1, 1)'], 'E1013: Argument 1: type mismatch, expected number but got string')
1471 CheckDefFailure(['screenpos(1, "b", 1)'], 'E1013: Argument 2: type mismatch, expected number but got string')
1472 CheckDefFailure(['screenpos(1, 1, "c")'], 'E1013: Argument 3: type mismatch, expected number but got string')
1473 assert_equal({col: 1, row: 1, endcol: 1, curscol: 1}, screenpos(1, 1, 1))
1474enddef
1475
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +02001476def Test_screenstring()
1477 CheckDefFailure(['echo screenstring("x", 1)'], 'E1013: Argument 1: type mismatch, expected number but got string')
1478 CheckDefFailure(['echo screenstring(1, "x")'], 'E1013: Argument 2: type mismatch, expected number but got string')
1479enddef
1480
Bram Moolenaar94738d82020-10-21 14:25:07 +02001481def Test_search()
1482 new
1483 setline(1, ['foo', 'bar'])
1484 var val = 0
1485 # skip expr returns boolean
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01001486 search('bar', 'W', 0, 0, () => val == 1)->assert_equal(2)
Bram Moolenaar94738d82020-10-21 14:25:07 +02001487 :1
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01001488 search('bar', 'W', 0, 0, () => val == 0)->assert_equal(0)
Bram Moolenaar94738d82020-10-21 14:25:07 +02001489 # skip expr returns number, only 0 and 1 are accepted
1490 :1
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01001491 search('bar', 'W', 0, 0, () => 0)->assert_equal(2)
Bram Moolenaar94738d82020-10-21 14:25:07 +02001492 :1
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01001493 search('bar', 'W', 0, 0, () => 1)->assert_equal(0)
1494 assert_fails("search('bar', '', 0, 0, () => -1)", 'E1023:')
1495 assert_fails("search('bar', '', 0, 0, () => -1)", 'E1023:')
Bram Moolenaarf06ab6b2021-05-07 19:44:21 +02001496
1497 setline(1, "find this word")
1498 normal gg
1499 var col = 7
1500 assert_equal(1, search('this', '', 0, 0, 'col(".") > col'))
1501 normal 0
1502 assert_equal([1, 6], searchpos('this', '', 0, 0, 'col(".") > col'))
1503
1504 col = 5
1505 normal 0
1506 assert_equal(0, search('this', '', 0, 0, 'col(".") > col'))
1507 normal 0
1508 assert_equal([0, 0], searchpos('this', '', 0, 0, 'col(".") > col'))
1509 bwipe!
Bram Moolenaar94738d82020-10-21 14:25:07 +02001510enddef
1511
1512def Test_searchcount()
1513 new
1514 setline(1, "foo bar")
1515 :/foo
Bram Moolenaare0de1712020-12-02 17:36:54 +01001516 searchcount({recompute: true})
1517 ->assert_equal({
Bram Moolenaar94738d82020-10-21 14:25:07 +02001518 exact_match: 1,
1519 current: 1,
1520 total: 1,
1521 maxcount: 99,
1522 incomplete: 0})
1523 bwipe!
1524enddef
1525
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001526def Test_searchpair()
1527 new
1528 setline(1, "here { and } there")
Bram Moolenaarf06ab6b2021-05-07 19:44:21 +02001529
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001530 normal f{
1531 var col = 15
1532 assert_equal(1, searchpair('{', '', '}', '', 'col(".") > col'))
1533 assert_equal(12, col('.'))
Bram Moolenaarf06ab6b2021-05-07 19:44:21 +02001534 normal 0f{
1535 assert_equal([1, 12], searchpairpos('{', '', '}', '', 'col(".") > col'))
1536
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001537 col = 8
1538 normal 0f{
1539 assert_equal(0, searchpair('{', '', '}', '', 'col(".") > col'))
1540 assert_equal(6, col('.'))
Bram Moolenaarf06ab6b2021-05-07 19:44:21 +02001541 normal 0f{
1542 assert_equal([0, 0], searchpairpos('{', '', '}', '', 'col(".") > col'))
1543
Bram Moolenaarff652882021-05-16 15:24:49 +02001544 var lines =<< trim END
1545 vim9script
1546 setline(1, '()')
1547 normal gg
1548 def Fail()
1549 try
1550 searchpairpos('(', '', ')', 'nW', '[0]->map("")')
1551 catch
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001552 g:caught = 'yes'
Bram Moolenaarff652882021-05-16 15:24:49 +02001553 endtry
1554 enddef
1555 Fail()
1556 END
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001557 CheckScriptSuccess(lines)
1558 assert_equal('yes', g:caught)
Bram Moolenaarff652882021-05-16 15:24:49 +02001559
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001560 unlet g:caught
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001561 bwipe!
1562enddef
1563
Bram Moolenaar34453202021-01-31 13:08:38 +01001564def Test_set_get_bufline()
1565 # similar to Test_setbufline_getbufline()
1566 var lines =<< trim END
1567 new
1568 var b = bufnr('%')
1569 hide
1570 assert_equal(0, setbufline(b, 1, ['foo', 'bar']))
1571 assert_equal(['foo'], getbufline(b, 1))
1572 assert_equal(['bar'], getbufline(b, '$'))
1573 assert_equal(['foo', 'bar'], getbufline(b, 1, 2))
1574 exe "bd!" b
1575 assert_equal([], getbufline(b, 1, 2))
1576
1577 split Xtest
1578 setline(1, ['a', 'b', 'c'])
1579 b = bufnr('%')
1580 wincmd w
1581
1582 assert_equal(1, setbufline(b, 5, 'x'))
1583 assert_equal(1, setbufline(b, 5, ['x']))
1584 assert_equal(1, setbufline(b, 5, []))
1585 assert_equal(1, setbufline(b, 5, test_null_list()))
1586
1587 assert_equal(1, 'x'->setbufline(bufnr('$') + 1, 1))
1588 assert_equal(1, ['x']->setbufline(bufnr('$') + 1, 1))
1589 assert_equal(1, []->setbufline(bufnr('$') + 1, 1))
1590 assert_equal(1, test_null_list()->setbufline(bufnr('$') + 1, 1))
1591
1592 assert_equal(['a', 'b', 'c'], getbufline(b, 1, '$'))
1593
1594 assert_equal(0, setbufline(b, 4, ['d', 'e']))
1595 assert_equal(['c'], b->getbufline(3))
1596 assert_equal(['d'], getbufline(b, 4))
1597 assert_equal(['e'], getbufline(b, 5))
1598 assert_equal([], getbufline(b, 6))
1599 assert_equal([], getbufline(b, 2, 1))
1600
Bram Moolenaar00385112021-02-07 14:31:06 +01001601 if has('job')
Bram Moolenaar1328bde2021-06-05 20:51:38 +02001602 setbufline(b, 2, [function('eval'), {key: 123}, string(test_null_job())])
Bram Moolenaar00385112021-02-07 14:31:06 +01001603 assert_equal(["function('eval')",
1604 "{'key': 123}",
1605 "no process"],
1606 getbufline(b, 2, 4))
1607 endif
Bram Moolenaar34453202021-01-31 13:08:38 +01001608
1609 exe 'bwipe! ' .. b
1610 END
1611 CheckDefAndScriptSuccess(lines)
1612enddef
1613
Bram Moolenaar94738d82020-10-21 14:25:07 +02001614def Test_searchdecl()
1615 searchdecl('blah', true, true)->assert_equal(1)
1616enddef
1617
1618def Test_setbufvar()
1619 setbufvar(bufnr('%'), '&syntax', 'vim')
1620 &syntax->assert_equal('vim')
1621 setbufvar(bufnr('%'), '&ts', 16)
1622 &ts->assert_equal(16)
Bram Moolenaar31a201a2021-01-03 14:47:25 +01001623 setbufvar(bufnr('%'), '&ai', true)
1624 &ai->assert_equal(true)
1625 setbufvar(bufnr('%'), '&ft', 'filetype')
1626 &ft->assert_equal('filetype')
1627
Bram Moolenaar94738d82020-10-21 14:25:07 +02001628 settabwinvar(1, 1, '&syntax', 'vam')
1629 &syntax->assert_equal('vam')
1630 settabwinvar(1, 1, '&ts', 15)
1631 &ts->assert_equal(15)
1632 setlocal ts=8
Bram Moolenaarb0d81822021-01-03 15:55:10 +01001633 settabwinvar(1, 1, '&list', false)
1634 &list->assert_equal(false)
Bram Moolenaar31a201a2021-01-03 14:47:25 +01001635 settabwinvar(1, 1, '&list', true)
1636 &list->assert_equal(true)
1637 setlocal list&
Bram Moolenaar94738d82020-10-21 14:25:07 +02001638
1639 setbufvar('%', 'myvar', 123)
1640 getbufvar('%', 'myvar')->assert_equal(123)
1641enddef
1642
Yegappan Lakshmanana26f56f2021-07-03 11:58:12 +02001643def Test_setcharsearch()
1644 CheckDefFailure(['setcharsearch("x")'], 'E1013: Argument 1: type mismatch, expected dict<any> but got string')
1645 CheckDefFailure(['setcharsearch([])'], 'E1013: Argument 1: type mismatch, expected dict<any> but got list<unknown>')
1646 var d: dict<any> = {char: 'x', forward: 1, until: 1}
1647 setcharsearch(d)
1648 assert_equal(d, getcharsearch())
1649enddef
1650
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +02001651def Test_setcmdpos()
1652 CheckDefFailure(['echo setcmdpos("x")'], 'E1013: Argument 1: type mismatch, expected number but got string')
1653enddef
1654
Yegappan Lakshmanana26f56f2021-07-03 11:58:12 +02001655def Test_setfperm()
1656 CheckDefFailure(['setfperm(1, "b")'], 'E1013: Argument 1: type mismatch, expected string but got number')
1657 CheckDefFailure(['setfperm("a", 0z10)'], 'E1013: Argument 2: type mismatch, expected string but got blob')
1658enddef
1659
Bram Moolenaar94738d82020-10-21 14:25:07 +02001660def Test_setloclist()
Bram Moolenaare0de1712020-12-02 17:36:54 +01001661 var items = [{filename: '/tmp/file', lnum: 1, valid: true}]
1662 var what = {items: items}
Bram Moolenaar94738d82020-10-21 14:25:07 +02001663 setqflist([], ' ', what)
1664 setloclist(0, [], ' ', what)
1665enddef
1666
1667def Test_setreg()
1668 setreg('a', ['aaa', 'bbb', 'ccc'])
1669 var reginfo = getreginfo('a')
1670 setreg('a', reginfo)
1671 getreginfo('a')->assert_equal(reginfo)
Bram Moolenaar418a29f2021-02-10 22:23:41 +01001672 assert_fails('setreg("ab", 0)', 'E1162:')
Bram Moolenaar94738d82020-10-21 14:25:07 +02001673enddef
1674
Yegappan Lakshmanana26f56f2021-07-03 11:58:12 +02001675def Test_sha256()
1676 CheckDefFailure(['sha256(100)'], 'E1013: Argument 1: type mismatch, expected string but got number')
1677 CheckDefFailure(['sha256(0zABCD)'], 'E1013: Argument 1: type mismatch, expected string but got blob')
1678 assert_equal('ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad', sha256('abc'))
1679enddef
1680
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +02001681def Test_shiftwidth()
1682 CheckDefFailure(['echo shiftwidth("x")'], 'E1013: Argument 1: type mismatch, expected number but got string')
1683enddef
1684
Yegappan Lakshmanana26f56f2021-07-03 11:58:12 +02001685def Test_simplify()
1686 CheckDefFailure(['simplify(100)'], 'E1013: Argument 1: type mismatch, expected string but got number')
1687 call assert_equal('NonExistingFile', simplify('NonExistingFile'))
1688enddef
1689
Bram Moolenaar6601b622021-01-13 21:47:15 +01001690def Test_slice()
1691 assert_equal('12345', slice('012345', 1))
1692 assert_equal('123', slice('012345', 1, 4))
1693 assert_equal('1234', slice('012345', 1, -1))
1694 assert_equal('1', slice('012345', 1, -4))
1695 assert_equal('', slice('012345', 1, -5))
1696 assert_equal('', slice('012345', 1, -6))
1697
1698 assert_equal([1, 2, 3, 4, 5], slice(range(6), 1))
1699 assert_equal([1, 2, 3], slice(range(6), 1, 4))
1700 assert_equal([1, 2, 3, 4], slice(range(6), 1, -1))
1701 assert_equal([1], slice(range(6), 1, -4))
1702 assert_equal([], slice(range(6), 1, -5))
1703 assert_equal([], slice(range(6), 1, -6))
1704
1705 assert_equal(0z1122334455, slice(0z001122334455, 1))
1706 assert_equal(0z112233, slice(0z001122334455, 1, 4))
1707 assert_equal(0z11223344, slice(0z001122334455, 1, -1))
1708 assert_equal(0z11, slice(0z001122334455, 1, -4))
1709 assert_equal(0z, slice(0z001122334455, 1, -5))
1710 assert_equal(0z, slice(0z001122334455, 1, -6))
1711enddef
1712
Bram Moolenaar94738d82020-10-21 14:25:07 +02001713def Test_spellsuggest()
1714 if !has('spell')
1715 MissingFeature 'spell'
1716 else
1717 spellsuggest('marrch', 1, true)->assert_equal(['March'])
1718 endif
1719enddef
1720
Yegappan Lakshmanana26f56f2021-07-03 11:58:12 +02001721def Test_sound_stop()
1722 CheckFeature sound
1723 CheckDefFailure(['sound_stop("x")'], 'E1013: Argument 1: type mismatch, expected number but got string')
1724enddef
1725
1726def Test_soundfold()
1727 CheckDefFailure(['soundfold(20)'], 'E1013: Argument 1: type mismatch, expected string but got number')
1728 assert_equal('abc', soundfold('abc'))
1729enddef
1730
Bram Moolenaar94738d82020-10-21 14:25:07 +02001731def Test_sort_return_type()
1732 var res: list<number>
1733 res = [1, 2, 3]->sort()
1734enddef
1735
1736def Test_sort_argument()
Bram Moolenaar08cf0c02020-12-05 21:47:06 +01001737 var lines =<< trim END
1738 var res = ['b', 'a', 'c']->sort('i')
1739 res->assert_equal(['a', 'b', 'c'])
1740
1741 def Compare(a: number, b: number): number
1742 return a - b
1743 enddef
1744 var l = [3, 6, 7, 1, 8, 2, 4, 5]
1745 sort(l, Compare)
1746 assert_equal([1, 2, 3, 4, 5, 6, 7, 8], l)
1747 END
1748 CheckDefAndScriptSuccess(lines)
Bram Moolenaar94738d82020-10-21 14:25:07 +02001749enddef
1750
Yegappan Lakshmanana26f56f2021-07-03 11:58:12 +02001751def Test_spellbadword()
1752 CheckDefFailure(['spellbadword(100)'], 'E1013: Argument 1: type mismatch, expected string but got number')
1753 spellbadword('good')->assert_equal(['', ''])
1754enddef
1755
Bram Moolenaar94738d82020-10-21 14:25:07 +02001756def Test_split()
1757 split(' aa bb ', '\W\+', true)->assert_equal(['', 'aa', 'bb', ''])
1758enddef
1759
Yegappan Lakshmanana26f56f2021-07-03 11:58:12 +02001760def Test_srand()
1761 CheckDefFailure(['srand("a")'], 'E1013: Argument 1: type mismatch, expected number but got string')
1762 type(srand(100))->assert_equal(v:t_list)
1763enddef
1764
1765def Test_state()
1766 CheckDefFailure(['state({})'], 'E1013: Argument 1: type mismatch, expected string but got dict<unknown>')
1767 assert_equal('', state('a'))
1768enddef
1769
Bram Moolenaar80ad3e22021-01-31 20:48:58 +01001770def Run_str2float()
1771 if !has('float')
1772 MissingFeature 'float'
1773 endif
1774 str2float("1.00")->assert_equal(1.00)
1775 str2float("2e-2")->assert_equal(0.02)
1776
1777 CheckDefFailure(['echo str2float(123)'], 'E1013:')
1778 CheckScriptFailure(['vim9script', 'echo str2float(123)'], 'E1024:')
1779 endif
1780enddef
1781
Bram Moolenaar94738d82020-10-21 14:25:07 +02001782def Test_str2nr()
1783 str2nr("1'000'000", 10, true)->assert_equal(1000000)
Bram Moolenaarf2b26bc2021-01-30 23:05:11 +01001784
1785 CheckDefFailure(['echo str2nr(123)'], 'E1013:')
1786 CheckScriptFailure(['vim9script', 'echo str2nr(123)'], 'E1024:')
1787 CheckDefFailure(['echo str2nr("123", "x")'], 'E1013:')
1788 CheckScriptFailure(['vim9script', 'echo str2nr("123", "x")'], 'E1030:')
1789 CheckDefFailure(['echo str2nr("123", 10, "x")'], 'E1013:')
1790 CheckScriptFailure(['vim9script', 'echo str2nr("123", 10, "x")'], 'E1135:')
Bram Moolenaar94738d82020-10-21 14:25:07 +02001791enddef
1792
1793def Test_strchars()
1794 strchars("A\u20dd", true)->assert_equal(1)
1795enddef
1796
Yegappan Lakshmanana26f56f2021-07-03 11:58:12 +02001797def Test_strlen()
1798 CheckDefFailure(['strlen([])'], 'E1013: Argument 1: type mismatch, expected string but got list<unknown>')
1799 "abc"->strlen()->assert_equal(3)
1800 strlen(99)->assert_equal(2)
1801enddef
1802
1803def Test_strptime()
1804 CheckFunction strptime
1805 CheckDefFailure(['strptime(10, "2021")'], 'E1013: Argument 1: type mismatch, expected string but got number')
1806 CheckDefFailure(['strptime("%Y", 2021)'], 'E1013: Argument 2: type mismatch, expected string but got number')
1807 # BUG: Directly calling strptime() in this function gives an "E117: Unknown
1808 # function" error on MS-Windows even with the above CheckFunction call for
1809 # strptime().
1810 #assert_true(strptime('%Y', '2021') != 0)
1811enddef
1812
1813def Test_strtrans()
1814 CheckDefFailure(['strtrans(20)'], 'E1013: Argument 1: type mismatch, expected string but got number')
1815 assert_equal('abc', strtrans('abc'))
1816enddef
1817
1818def Test_strwidth()
1819 CheckDefFailure(['strwidth(10)'], 'E1013: Argument 1: type mismatch, expected string but got number')
1820 CheckScriptFailure(['vim9script', 'echo strwidth(10)'], 'E1024:')
1821 assert_equal(4, strwidth('abcd'))
1822enddef
1823
Bram Moolenaar94738d82020-10-21 14:25:07 +02001824def Test_submatch()
1825 var pat = 'A\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)'
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001826 var Rep = () => range(10)->mapnew((_, v) => submatch(v, true))->string()
Bram Moolenaar94738d82020-10-21 14:25:07 +02001827 var actual = substitute('A123456789', pat, Rep, '')
1828 var expected = "[['A123456789'], ['1'], ['2'], ['3'], ['4'], ['5'], ['6'], ['7'], ['8'], ['9']]"
1829 actual->assert_equal(expected)
1830enddef
1831
Bram Moolenaar1328bde2021-06-05 20:51:38 +02001832def Test_substitute()
1833 var res = substitute('A1234', '\d', 'X', '')
1834 assert_equal('AX234', res)
1835
1836 if has('job')
1837 assert_fails('"text"->substitute(".*", () => job_start(":"), "")', 'E908: using an invalid value as a String: job')
1838 assert_fails('"text"->substitute(".*", () => job_start(":")->job_getchannel(), "")', 'E908: using an invalid value as a String: channel')
1839 endif
1840enddef
1841
Yegappan Lakshmanana26f56f2021-07-03 11:58:12 +02001842def Test_swapinfo()
1843 CheckDefFailure(['swapinfo({})'], 'E1013: Argument 1: type mismatch, expected string but got dict<unknown>')
1844 call assert_equal({error: 'Cannot open file'}, swapinfo('x'))
1845enddef
1846
1847def Test_swapname()
1848 CheckDefFailure(['swapname([])'], 'E1013: Argument 1: type mismatch, expected string but got list<unknown>')
1849 assert_fails('swapname("NonExistingBuf")', 'E94:')
1850enddef
1851
Bram Moolenaar94738d82020-10-21 14:25:07 +02001852def Test_synID()
1853 new
1854 setline(1, "text")
1855 synID(1, 1, true)->assert_equal(0)
1856 bwipe!
1857enddef
1858
Yegappan Lakshmanana26f56f2021-07-03 11:58:12 +02001859def Test_synIDtrans()
1860 CheckDefFailure(['synIDtrans("a")'], 'E1013: Argument 1: type mismatch, expected number but got string')
1861enddef
1862
1863def Test_tabpagebuflist()
1864 CheckDefFailure(['tabpagebuflist("t")'], 'E1013: Argument 1: type mismatch, expected number but got string')
1865 assert_equal([bufnr('')], tabpagebuflist())
1866 assert_equal([bufnr('')], tabpagebuflist(1))
1867enddef
1868
1869def Test_tabpagenr()
1870 CheckDefFailure(['tabpagenr(1)'], 'E1013: Argument 1: type mismatch, expected string but got number')
1871 assert_equal(1, tabpagenr('$'))
1872 assert_equal(1, tabpagenr())
1873enddef
1874
Bram Moolenaar94738d82020-10-21 14:25:07 +02001875def Test_term_gettty()
1876 if !has('terminal')
1877 MissingFeature 'terminal'
1878 else
1879 var buf = Run_shell_in_terminal({})
1880 term_gettty(buf, true)->assert_notequal('')
1881 StopShellInTerminal(buf)
1882 endif
1883enddef
1884
1885def Test_term_start()
1886 if !has('terminal')
1887 MissingFeature 'terminal'
1888 else
1889 botright new
1890 var winnr = winnr()
Bram Moolenaare0de1712020-12-02 17:36:54 +01001891 term_start(&shell, {curwin: true})
Bram Moolenaar94738d82020-10-21 14:25:07 +02001892 winnr()->assert_equal(winnr)
1893 bwipe!
1894 endif
1895enddef
1896
Yegappan Lakshmanana26f56f2021-07-03 11:58:12 +02001897def Test_timer_info()
1898 CheckDefFailure(['timer_info("id")'], 'E1013: Argument 1: type mismatch, expected number but got string')
1899 assert_equal([], timer_info(100))
1900 assert_equal([], timer_info())
1901enddef
1902
Bram Moolenaar94738d82020-10-21 14:25:07 +02001903def Test_timer_paused()
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01001904 var id = timer_start(50, () => 0)
Bram Moolenaar94738d82020-10-21 14:25:07 +02001905 timer_pause(id, true)
1906 var info = timer_info(id)
1907 info[0]['paused']->assert_equal(1)
1908 timer_stop(id)
1909enddef
1910
Yegappan Lakshmanana26f56f2021-07-03 11:58:12 +02001911def Test_timer_stop()
1912 CheckDefFailure(['timer_stop("x")'], 'E1013: Argument 1: type mismatch, expected number but got string')
1913 assert_equal(0, timer_stop(100))
1914enddef
1915
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +02001916def Test_tolower()
1917 CheckDefFailure(['echo tolower(1)'], 'E1013: Argument 1: type mismatch, expected string but got number')
1918enddef
1919
1920def Test_toupper()
1921 CheckDefFailure(['echo toupper(1)'], 'E1013: Argument 1: type mismatch, expected string but got number')
1922enddef
1923
1924def Test_tr()
1925 CheckDefFailure(['echo tr(1, "a", "b")'], 'E1013: Argument 1: type mismatch, expected string but got number')
1926 CheckDefFailure(['echo tr("a", 1, "b")'], 'E1013: Argument 2: type mismatch, expected string but got number')
1927 CheckDefFailure(['echo tr("a", "a", 1)'], 'E1013: Argument 3: type mismatch, expected string but got number')
1928enddef
1929
Yegappan Lakshmanana26f56f2021-07-03 11:58:12 +02001930def Test_undofile()
1931 CheckDefFailure(['undofile(10)'], 'E1013: Argument 1: type mismatch, expected string but got number')
1932 assert_equal('.abc.un~', fnamemodify(undofile('abc'), ':t'))
1933enddef
1934
1935def Test_values()
1936 CheckDefFailure(['values([])'], 'E1013: Argument 1: type mismatch, expected dict<any> but got list<unknown>')
1937 assert_equal([], {}->values())
1938 assert_equal(['sun'], {star: 'sun'}->values())
1939enddef
1940
Bram Moolenaar37487e12021-01-12 22:08:53 +01001941def Test_win_execute()
1942 assert_equal("\n" .. winnr(), win_execute(win_getid(), 'echo winnr()'))
1943 assert_equal('', win_execute(342343, 'echo winnr()'))
1944enddef
1945
Yegappan Lakshmanana26f56f2021-07-03 11:58:12 +02001946def Test_win_findbuf()
1947 CheckDefFailure(['win_findbuf("a")'], 'E1013: Argument 1: type mismatch, expected number but got string')
1948 assert_equal([], win_findbuf(1000))
1949 assert_equal([win_getid()], win_findbuf(bufnr('')))
1950enddef
1951
1952def Test_win_getid()
1953 CheckDefFailure(['win_getid(".")'], 'E1013: Argument 1: type mismatch, expected number but got string')
1954 CheckDefFailure(['win_getid(1, ".")'], 'E1013: Argument 2: type mismatch, expected number but got string')
1955 assert_equal(win_getid(), win_getid(1, 1))
1956enddef
1957
Bram Moolenaar94738d82020-10-21 14:25:07 +02001958def Test_win_splitmove()
1959 split
Bram Moolenaare0de1712020-12-02 17:36:54 +01001960 win_splitmove(1, 2, {vertical: true, rightbelow: true})
Bram Moolenaar94738d82020-10-21 14:25:07 +02001961 close
1962enddef
1963
Yegappan Lakshmanana26f56f2021-07-03 11:58:12 +02001964def Test_winnr()
1965 CheckDefFailure(['winnr([])'], 'E1013: Argument 1: type mismatch, expected string but got list<unknown>')
1966 assert_equal(1, winnr())
1967 assert_equal(1, winnr('$'))
1968enddef
1969
Bram Moolenaar285b15f2020-12-29 20:25:19 +01001970def Test_winrestcmd()
1971 split
1972 var cmd = winrestcmd()
1973 wincmd _
1974 exe cmd
1975 assert_equal(cmd, winrestcmd())
1976 close
1977enddef
1978
Yegappan Lakshmanana26f56f2021-07-03 11:58:12 +02001979def Test_winrestview()
1980 CheckDefFailure(['winrestview([])'], 'E1013: Argument 1: type mismatch, expected dict<any> but got list<unknown>')
1981 :%d _
1982 setline(1, 'Hello World')
1983 winrestview({lnum: 1, col: 6})
1984 assert_equal([1, 7], [line('.'), col('.')])
1985enddef
1986
Bram Moolenaar43b69b32021-01-07 20:23:33 +01001987def Test_winsaveview()
1988 var view: dict<number> = winsaveview()
1989
1990 var lines =<< trim END
1991 var view: list<number> = winsaveview()
1992 END
1993 CheckDefAndScriptFailure(lines, 'E1012: Type mismatch; expected list<number> but got dict<number>', 1)
1994enddef
1995
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +02001996def Test_win_gettype()
1997 CheckDefFailure(['echo win_gettype("x")'], 'E1013: Argument 1: type mismatch, expected number but got string')
1998enddef
Bram Moolenaar43b69b32021-01-07 20:23:33 +01001999
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +02002000def Test_win_gotoid()
2001 CheckDefFailure(['echo win_gotoid("x")'], 'E1013: Argument 1: type mismatch, expected number but got string')
2002enddef
Bram Moolenaar285b15f2020-12-29 20:25:19 +01002003
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +02002004def Test_win_id2tabwin()
2005 CheckDefFailure(['echo win_id2tabwin("x")'], 'E1013: Argument 1: type mismatch, expected number but got string')
2006enddef
2007
2008def Test_win_id2win()
2009 CheckDefFailure(['echo win_id2win("x")'], 'E1013: Argument 1: type mismatch, expected number but got string')
2010enddef
2011
2012def Test_win_screenpos()
2013 CheckDefFailure(['echo win_screenpos("x")'], 'E1013: Argument 1: type mismatch, expected number but got string')
2014enddef
2015
2016def Test_winbufnr()
2017 CheckDefFailure(['echo winbufnr("x")'], 'E1013: Argument 1: type mismatch, expected number but got string')
2018enddef
2019
2020def Test_winheight()
2021 CheckDefFailure(['echo winheight("x")'], 'E1013: Argument 1: type mismatch, expected number but got string')
2022enddef
2023
2024def Test_winlayout()
2025 CheckDefFailure(['echo winlayout("x")'], 'E1013: Argument 1: type mismatch, expected number but got string')
2026enddef
2027
2028def Test_winwidth()
2029 CheckDefFailure(['echo winwidth("x")'], 'E1013: Argument 1: type mismatch, expected number but got string')
2030enddef
2031
2032def Test_xor()
2033 CheckDefFailure(['echo xor("x", 0x2)'], 'E1013: Argument 1: type mismatch, expected number but got string')
2034 CheckDefFailure(['echo xor(0x1, "x")'], 'E1013: Argument 2: type mismatch, expected number but got string')
2035enddef
Bram Moolenaar94738d82020-10-21 14:25:07 +02002036
2037" vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker