Mercurial > vim
comparison src/testdir/test_vim9_script.vim @ 19912:d4fa9db88d16 v8.2.0512
patch 8.2.0512: Vim9: no optional arguments in func type
Commit: https://github.com/vim/vim/commit/5deeb3f1f9db4eabd36e99cbf857fe376eb37e10
Author: Bram Moolenaar <Bram@vim.org>
Date: Sun Apr 5 17:08:17 2020 +0200
patch 8.2.0512: Vim9: no optional arguments in func type
Problem: Vim9: no optional arguments in func type.
Solution: Check for question mark after type. Find function reference
without function().
author | Bram Moolenaar <Bram@vim.org> |
---|---|
date | Sun, 05 Apr 2020 17:15:25 +0200 |
parents | bd4f91762d0f |
children | 2c4d9ca33769 |
comparison
equal
deleted
inserted
replaced
19911:70edfdef3b83 | 19912:d4fa9db88d16 |
---|---|
19 def Test_syntax() | 19 def Test_syntax() |
20 let var = 234 | 20 let var = 234 |
21 let other: list<string> = ['asdf'] | 21 let other: list<string> = ['asdf'] |
22 enddef | 22 enddef |
23 | 23 |
24 func Test_def_basic() | |
25 def SomeFunc(): string | |
26 return 'yes' | |
27 enddef | |
28 call assert_equal('yes', SomeFunc()) | |
29 endfunc | |
30 | |
31 let s:appendToMe = 'xxx' | 24 let s:appendToMe = 'xxx' |
32 let s:addToMe = 111 | 25 let s:addToMe = 111 |
33 let g:existing = 'yes' | 26 let g:existing = 'yes' |
34 let g:inc_counter = 1 | 27 let g:inc_counter = 1 |
35 let $SOME_ENV_VAR = 'some' | 28 let $SOME_ENV_VAR = 'some' |
64 let job2: job = job_start('willfail') | 57 let job2: job = job_start('willfail') |
65 endif | 58 endif |
66 if has('float') | 59 if has('float') |
67 let float1: float = 3.4 | 60 let float1: float = 3.4 |
68 endif | 61 endif |
69 let funky1: func | 62 let Funky1: func |
70 let funky2: func = function('len') | 63 let Funky2: func = function('len') |
71 let party2: func = funcref('Test_syntax') | 64 let Party2: func = funcref('Test_syntax') |
72 | 65 |
73 " type becomes list<any> | 66 " type becomes list<any> |
74 let somelist = rand() > 0 ? [1, 2, 3] : ['a', 'b', 'c'] | 67 let somelist = rand() > 0 ? [1, 2, 3] : ['a', 'b', 'c'] |
75 " type becomes dict<any> | 68 " type becomes dict<any> |
76 let somedict = rand() > 0 ? #{a: 1, b: 2} : #{a: 'a', b: 'b'} | 69 let somedict = rand() > 0 ? #{a: 1, b: 2} : #{a: 'a', b: 'b'} |
132 v:errmsg = 'none' | 125 v:errmsg = 'none' |
133 v:errmsg ..= 'again' | 126 v:errmsg ..= 'again' |
134 assert_equal('noneagain', v:errmsg) | 127 assert_equal('noneagain', v:errmsg) |
135 call CheckDefFailure(['v:errmsg += "more"'], 'E1013:') | 128 call CheckDefFailure(['v:errmsg += "more"'], 'E1013:') |
136 call CheckDefFailure(['v:errmsg += 123'], 'E1013:') | 129 call CheckDefFailure(['v:errmsg += 123'], 'E1013:') |
130 enddef | |
131 | |
132 def Test_assignment_default() | |
137 | 133 |
138 " Test default values. | 134 " Test default values. |
139 let thebool: bool | 135 let thebool: bool |
140 assert_equal(v:false, thebool) | 136 assert_equal(v:false, thebool) |
141 | 137 |
151 assert_equal('', thestring) | 147 assert_equal('', thestring) |
152 | 148 |
153 let theblob: blob | 149 let theblob: blob |
154 assert_equal(0z, theblob) | 150 assert_equal(0z, theblob) |
155 | 151 |
156 let thefunc: func | 152 let Thefunc: func |
157 assert_equal(test_null_function(), thefunc) | 153 assert_equal(test_null_function(), Thefunc) |
158 | 154 |
159 let thelist: list<any> | 155 let thelist: list<any> |
160 assert_equal([], thelist) | 156 assert_equal([], thelist) |
161 | 157 |
162 let thedict: dict<any> | 158 let thedict: dict<any> |
261 def Test_cmd_modifier() | 257 def Test_cmd_modifier() |
262 tab echo '0' | 258 tab echo '0' |
263 call CheckDefFailure(['5tab echo 3'], 'E16:') | 259 call CheckDefFailure(['5tab echo 3'], 'E16:') |
264 enddef | 260 enddef |
265 | 261 |
266 | |
267 def ReturnString(): string | |
268 return 'string' | |
269 enddef | |
270 | |
271 def ReturnNumber(): number | |
272 return 123 | |
273 enddef | |
274 | |
275 let g:notNumber = 'string' | |
276 | |
277 def ReturnGlobal(): number | |
278 return g:notNumber | |
279 enddef | |
280 | |
281 def Test_return_something() | |
282 assert_equal('string', ReturnString()) | |
283 assert_equal(123, ReturnNumber()) | |
284 assert_fails('call ReturnGlobal()', 'E1029: Expected number but got string') | |
285 enddef | |
286 | |
287 let s:nothing = 0 | |
288 def ReturnNothing() | |
289 s:nothing = 1 | |
290 if true | |
291 return | |
292 endif | |
293 s:nothing = 2 | |
294 enddef | |
295 | |
296 def Test_return_nothing() | |
297 ReturnNothing() | |
298 assert_equal(1, s:nothing) | |
299 enddef | |
300 | |
301 func Increment() | |
302 let g:counter += 1 | |
303 endfunc | |
304 | |
305 def Test_call_ufunc_count() | |
306 g:counter = 1 | |
307 Increment() | |
308 Increment() | |
309 Increment() | |
310 " works with and without :call | |
311 assert_equal(4, g:counter) | |
312 call assert_equal(4, g:counter) | |
313 unlet g:counter | |
314 enddef | |
315 | |
316 def MyVarargs(arg: string, ...rest: list<string>): string | |
317 let res = arg | |
318 for s in rest | |
319 res ..= ',' .. s | |
320 endfor | |
321 return res | |
322 enddef | |
323 | |
324 def Test_call_varargs() | |
325 assert_equal('one', MyVarargs('one')) | |
326 assert_equal('one,two', MyVarargs('one', 'two')) | |
327 assert_equal('one,two,three', MyVarargs('one', 'two', 'three')) | |
328 enddef | |
329 | |
330 def MyDefaultArgs(name = 'string'): string | |
331 return name | |
332 enddef | |
333 | |
334 def Test_call_default_args() | |
335 assert_equal('string', MyDefaultArgs()) | |
336 assert_equal('one', MyDefaultArgs('one')) | |
337 assert_fails('call MyDefaultArgs("one", "two")', 'E118:') | |
338 | |
339 call CheckScriptFailure(['def Func(arg: number = asdf)', 'enddef'], 'E1001:') | |
340 enddef | |
341 | |
342 func Test_call_default_args_from_func() | |
343 call assert_equal('string', MyDefaultArgs()) | |
344 call assert_equal('one', MyDefaultArgs('one')) | |
345 call assert_fails('call MyDefaultArgs("one", "two")', 'E118:') | |
346 endfunc | |
347 | |
348 func TakesOneArg(arg) | |
349 echo a:arg | |
350 endfunc | |
351 | |
352 def Test_call_wrong_args() | |
353 call CheckDefFailure(['TakesOneArg()'], 'E119:') | |
354 call CheckDefFailure(['TakesOneArg(11, 22)'], 'E118:') | |
355 call CheckDefFailure(['bufnr(xxx)'], 'E1001:') | |
356 enddef | |
357 | |
358 " Default arg and varargs | |
359 def MyDefVarargs(one: string, two = 'foo', ...rest: list<string>): string | |
360 let res = one .. ',' .. two | |
361 for s in rest | |
362 res ..= ',' .. s | |
363 endfor | |
364 return res | |
365 enddef | |
366 | |
367 def Test_call_def_varargs() | |
368 call assert_fails('call MyDefVarargs()', 'E119:') | |
369 assert_equal('one,foo', MyDefVarargs('one')) | |
370 assert_equal('one,two', MyDefVarargs('one', 'two')) | |
371 assert_equal('one,two,three', MyDefVarargs('one', 'two', 'three')) | |
372 enddef | |
373 | |
374 def Test_using_var_as_arg() | |
375 call writefile(['def Func(x: number)', 'let x = 234', 'enddef'], 'Xdef') | |
376 call assert_fails('so Xdef', 'E1006:') | |
377 call delete('Xdef') | |
378 enddef | |
379 | |
380 def Test_call_func_defined_later() | |
381 call assert_equal('one', DefinedLater('one')) | |
382 call assert_fails('call NotDefined("one")', 'E117:') | |
383 enddef | |
384 | |
385 func DefinedLater(arg) | |
386 return a:arg | |
387 endfunc | |
388 | |
389 def FuncWithForwardCall() | |
390 return DefinedEvenLater("yes") | |
391 enddef | |
392 | |
393 def DefinedEvenLater(arg: string): string | |
394 return arg | |
395 enddef | |
396 | |
397 def Test_error_in_nested_function() | |
398 " Error in called function requires unwinding the call stack. | |
399 assert_fails('call FuncWithForwardCall()', 'E1029') | |
400 enddef | |
401 | |
402 def Test_return_type_wrong() | |
403 CheckScriptFailure(['def Func(): number', 'return "a"', 'enddef'], 'expected number but got string') | |
404 CheckScriptFailure(['def Func(): string', 'return 1', 'enddef'], 'expected string but got number') | |
405 CheckScriptFailure(['def Func(): void', 'return "a"', 'enddef'], 'expected void but got string') | |
406 CheckScriptFailure(['def Func()', 'return "a"', 'enddef'], 'expected void but got string') | |
407 | |
408 CheckScriptFailure(['def Func(): number', 'return', 'enddef'], 'E1003:') | |
409 | |
410 CheckScriptFailure(['def Func(): list', 'return []', 'enddef'], 'E1008:') | |
411 CheckScriptFailure(['def Func(): dict', 'return {}', 'enddef'], 'E1008:') | |
412 enddef | |
413 | |
414 def Test_arg_type_wrong() | |
415 CheckScriptFailure(['def Func3(items: list)', 'echo "a"', 'enddef'], 'E1008: Missing <type>') | |
416 enddef | |
417 | 262 |
418 def Test_try_catch() | 263 def Test_try_catch() |
419 let l = [] | 264 let l = [] |
420 try | 265 try |
421 add(l, '1') | 266 add(l, '1') |
730 | 575 |
731 assert_fails('vim9script', 'E1038') | 576 assert_fails('vim9script', 'E1038') |
732 assert_fails('export something', 'E1042') | 577 assert_fails('export something', 'E1042') |
733 enddef | 578 enddef |
734 | 579 |
735 def Test_vim9script_call() | |
736 let lines =<< trim END | |
737 vim9script | |
738 let var = '' | |
739 def MyFunc(arg: string) | |
740 var = arg | |
741 enddef | |
742 MyFunc('foobar') | |
743 assert_equal('foobar', var) | |
744 | |
745 let str = 'barfoo' | |
746 str->MyFunc() | |
747 assert_equal('barfoo', var) | |
748 | |
749 let g:value = 'value' | |
750 g:value->MyFunc() | |
751 assert_equal('value', var) | |
752 | |
753 let listvar = [] | |
754 def ListFunc(arg: list<number>) | |
755 listvar = arg | |
756 enddef | |
757 [1, 2, 3]->ListFunc() | |
758 assert_equal([1, 2, 3], listvar) | |
759 | |
760 let dictvar = {} | |
761 def DictFunc(arg: dict<number>) | |
762 dictvar = arg | |
763 enddef | |
764 {'a': 1, 'b': 2}->DictFunc() | |
765 assert_equal(#{a: 1, b: 2}, dictvar) | |
766 def CompiledDict() | |
767 {'a': 3, 'b': 4}->DictFunc() | |
768 enddef | |
769 CompiledDict() | |
770 assert_equal(#{a: 3, b: 4}, dictvar) | |
771 | |
772 #{a: 3, b: 4}->DictFunc() | |
773 assert_equal(#{a: 3, b: 4}, dictvar) | |
774 | |
775 ('text')->MyFunc() | |
776 assert_equal('text', var) | |
777 ("some")->MyFunc() | |
778 assert_equal('some', var) | |
779 END | |
780 writefile(lines, 'Xcall.vim') | |
781 source Xcall.vim | |
782 delete('Xcall.vim') | |
783 enddef | |
784 | |
785 def Test_vim9script_call_fail_decl() | |
786 let lines =<< trim END | |
787 vim9script | |
788 let var = '' | |
789 def MyFunc(arg: string) | |
790 let var = 123 | |
791 enddef | |
792 END | |
793 writefile(lines, 'Xcall_decl.vim') | |
794 assert_fails('source Xcall_decl.vim', 'E1054:') | |
795 delete('Xcall_decl.vim') | |
796 enddef | |
797 | |
798 def Test_vim9script_call_fail_const() | |
799 let lines =<< trim END | |
800 vim9script | |
801 const var = '' | |
802 def MyFunc(arg: string) | |
803 var = 'asdf' | |
804 enddef | |
805 END | |
806 writefile(lines, 'Xcall_const.vim') | |
807 assert_fails('source Xcall_const.vim', 'E46:') | |
808 delete('Xcall_const.vim') | |
809 enddef | |
810 | |
811 def Test_vim9script_reload() | 580 def Test_vim9script_reload() |
812 let lines =<< trim END | 581 let lines =<< trim END |
813 vim9script | 582 vim9script |
814 const var = '' | 583 const var = '' |
815 let valone = 1234 | 584 let valone = 1234 |
924 l->add(5) | 693 l->add(5) |
925 l->insert(99, 1) | 694 l->insert(99, 1) |
926 assert_equal([2, 99, 3, 4, 5], l) | 695 assert_equal([2, 99, 3, 4, 5], l) |
927 enddef | 696 enddef |
928 | 697 |
929 " Test that inside :function a Python function can be defined, :def is not | |
930 " recognized. | |
931 func Test_function_python() | |
932 CheckFeature python3 | |
933 let py = 'python3' | |
934 execute py "<< EOF" | |
935 def do_something(): | |
936 return 1 | |
937 EOF | |
938 endfunc | |
939 | |
940 def IfElse(what: number): string | 698 def IfElse(what: number): string |
941 let res = '' | 699 let res = '' |
942 if what == 1 | 700 if what == 1 |
943 res = "one" | 701 res = "one" |
944 elseif what == 2 | 702 elseif what == 2 |
1083 def Test_if_const_expr_fails() | 841 def Test_if_const_expr_fails() |
1084 call CheckDefFailure(['if "aaa" == "bbb'], 'E114:') | 842 call CheckDefFailure(['if "aaa" == "bbb'], 'E114:') |
1085 call CheckDefFailure(["if 'aaa' == 'bbb"], 'E115:') | 843 call CheckDefFailure(["if 'aaa' == 'bbb"], 'E115:') |
1086 call CheckDefFailure(["if has('aaa'"], 'E110:') | 844 call CheckDefFailure(["if has('aaa'"], 'E110:') |
1087 call CheckDefFailure(["if has('aaa') ? true false"], 'E109:') | 845 call CheckDefFailure(["if has('aaa') ? true false"], 'E109:') |
1088 enddef | |
1089 | |
1090 def Test_delfunc() | |
1091 let lines =<< trim END | |
1092 vim9script | |
1093 def GoneSoon() | |
1094 echo 'hello' | |
1095 enddef | |
1096 | |
1097 def CallGoneSoon() | |
1098 GoneSoon() | |
1099 enddef | |
1100 | |
1101 delfunc GoneSoon | |
1102 CallGoneSoon() | |
1103 END | |
1104 writefile(lines, 'XToDelFunc') | |
1105 assert_fails('so XToDelFunc', 'E933') | |
1106 assert_fails('so XToDelFunc', 'E933') | |
1107 | |
1108 delete('XToDelFunc') | |
1109 enddef | 846 enddef |
1110 | 847 |
1111 def Test_execute_cmd() | 848 def Test_execute_cmd() |
1112 new | 849 new |
1113 setline(1, 'default') | 850 setline(1, 'default') |
1220 assert_equal(100, x) | 957 assert_equal(100, x) |
1221 endtry | 958 endtry |
1222 assert_true(caught, 'should have caught an exception') | 959 assert_true(caught, 'should have caught an exception') |
1223 enddef | 960 enddef |
1224 | 961 |
1225 def Test_redef_failure() | |
1226 call writefile(['def Func0(): string', 'return "Func0"', 'enddef'], 'Xdef') | |
1227 so Xdef | |
1228 call writefile(['def Func1(): string', 'return "Func1"', 'enddef'], 'Xdef') | |
1229 so Xdef | |
1230 call writefile(['def! Func0(): string', 'enddef'], 'Xdef') | |
1231 call assert_fails('so Xdef', 'E1027:') | |
1232 call writefile(['def Func2(): string', 'return "Func2"', 'enddef'], 'Xdef') | |
1233 so Xdef | |
1234 call delete('Xdef') | |
1235 | |
1236 call assert_equal(0, Func0()) | |
1237 call assert_equal('Func1', Func1()) | |
1238 call assert_equal('Func2', Func2()) | |
1239 | |
1240 delfunc! Func0 | |
1241 delfunc! Func1 | |
1242 delfunc! Func2 | |
1243 enddef | |
1244 | |
1245 " Test for internal functions returning different types | |
1246 func Test_InternalFuncRetType() | |
1247 let lines =<< trim END | |
1248 def RetFloat(): float | |
1249 return ceil(1.456) | |
1250 enddef | |
1251 | |
1252 def RetListAny(): list<any> | |
1253 return items({'k' : 'v'}) | |
1254 enddef | |
1255 | |
1256 def RetListString(): list<string> | |
1257 return split('a:b:c', ':') | |
1258 enddef | |
1259 | |
1260 def RetListDictAny(): list<dict<any>> | |
1261 return getbufinfo() | |
1262 enddef | |
1263 | |
1264 def RetDictNumber(): dict<number> | |
1265 return wordcount() | |
1266 enddef | |
1267 | |
1268 def RetDictString(): dict<string> | |
1269 return environ() | |
1270 enddef | |
1271 END | |
1272 call writefile(lines, 'Xscript') | |
1273 source Xscript | |
1274 | |
1275 call assert_equal(2.0, RetFloat()) | |
1276 call assert_equal([['k', 'v']], RetListAny()) | |
1277 call assert_equal(['a', 'b', 'c'], RetListString()) | |
1278 call assert_notequal([], RetListDictAny()) | |
1279 call assert_notequal({}, RetDictNumber()) | |
1280 call assert_notequal({}, RetDictString()) | |
1281 call delete('Xscript') | |
1282 endfunc | |
1283 | |
1284 " Test for passing too many or too few arguments to internal functions | |
1285 func Test_internalfunc_arg_error() | |
1286 let l =<< trim END | |
1287 def! FArgErr(): float | |
1288 return ceil(1.1, 2) | |
1289 enddef | |
1290 END | |
1291 call writefile(l, 'Xinvalidarg') | |
1292 call assert_fails('so Xinvalidarg', 'E118:') | |
1293 let l =<< trim END | |
1294 def! FArgErr(): float | |
1295 return ceil() | |
1296 enddef | |
1297 END | |
1298 call writefile(l, 'Xinvalidarg') | |
1299 call assert_fails('so Xinvalidarg', 'E119:') | |
1300 call delete('Xinvalidarg') | |
1301 endfunc | |
1302 | 962 |
1303 " Keep this last, it messes up highlighting. | 963 " Keep this last, it messes up highlighting. |
1304 def Test_substitute_cmd() | 964 def Test_substitute_cmd() |
1305 new | 965 new |
1306 setline(1, 'something') | 966 setline(1, 'something') |