Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 1 | " Test various aspects of the Vim9 script language. |
| 2 | |
| 3 | source check.vim |
| 4 | source view_util.vim |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 5 | source vim9.vim |
Bram Moolenaar | 47e7d70 | 2020-07-05 18:18:42 +0200 | [diff] [blame] | 6 | source screendump.vim |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 7 | |
| 8 | func Test_def_basic() |
| 9 | def SomeFunc(): string |
| 10 | return 'yes' |
| 11 | enddef |
| 12 | call assert_equal('yes', SomeFunc()) |
| 13 | endfunc |
| 14 | |
| 15 | def ReturnString(): string |
| 16 | return 'string' |
| 17 | enddef |
| 18 | |
| 19 | def ReturnNumber(): number |
| 20 | return 123 |
| 21 | enddef |
| 22 | |
| 23 | let g:notNumber = 'string' |
| 24 | |
| 25 | def ReturnGlobal(): number |
| 26 | return g:notNumber |
| 27 | enddef |
| 28 | |
| 29 | def Test_return_something() |
| 30 | assert_equal('string', ReturnString()) |
| 31 | assert_equal(123, ReturnNumber()) |
| 32 | assert_fails('call ReturnGlobal()', 'E1029: Expected number but got string') |
| 33 | enddef |
| 34 | |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 35 | def Test_missing_return() |
| 36 | CheckDefFailure(['def Missing(): number', |
| 37 | ' if g:cond', |
| 38 | ' echo "no return"', |
| 39 | ' else', |
| 40 | ' return 0', |
| 41 | ' endif' |
| 42 | 'enddef'], 'E1027:') |
| 43 | CheckDefFailure(['def Missing(): number', |
| 44 | ' if g:cond', |
| 45 | ' return 1', |
| 46 | ' else', |
| 47 | ' echo "no return"', |
| 48 | ' endif' |
| 49 | 'enddef'], 'E1027:') |
| 50 | CheckDefFailure(['def Missing(): number', |
| 51 | ' if g:cond', |
| 52 | ' return 1', |
| 53 | ' else', |
| 54 | ' return 2', |
| 55 | ' endif' |
| 56 | ' return 3' |
| 57 | 'enddef'], 'E1095:') |
| 58 | enddef |
| 59 | |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 60 | let s:nothing = 0 |
| 61 | def ReturnNothing() |
| 62 | s:nothing = 1 |
| 63 | if true |
| 64 | return |
| 65 | endif |
| 66 | s:nothing = 2 |
| 67 | enddef |
| 68 | |
| 69 | def Test_return_nothing() |
| 70 | ReturnNothing() |
| 71 | assert_equal(1, s:nothing) |
| 72 | enddef |
| 73 | |
| 74 | func Increment() |
| 75 | let g:counter += 1 |
| 76 | endfunc |
| 77 | |
| 78 | def Test_call_ufunc_count() |
| 79 | g:counter = 1 |
| 80 | Increment() |
| 81 | Increment() |
| 82 | Increment() |
| 83 | " works with and without :call |
| 84 | assert_equal(4, g:counter) |
| 85 | call assert_equal(4, g:counter) |
| 86 | unlet g:counter |
| 87 | enddef |
| 88 | |
| 89 | def MyVarargs(arg: string, ...rest: list<string>): string |
| 90 | let res = arg |
| 91 | for s in rest |
| 92 | res ..= ',' .. s |
| 93 | endfor |
| 94 | return res |
| 95 | enddef |
| 96 | |
| 97 | def Test_call_varargs() |
| 98 | assert_equal('one', MyVarargs('one')) |
| 99 | assert_equal('one,two', MyVarargs('one', 'two')) |
| 100 | assert_equal('one,two,three', MyVarargs('one', 'two', 'three')) |
| 101 | enddef |
| 102 | |
| 103 | def MyDefaultArgs(name = 'string'): string |
| 104 | return name |
| 105 | enddef |
| 106 | |
| 107 | def Test_call_default_args() |
| 108 | assert_equal('string', MyDefaultArgs()) |
| 109 | assert_equal('one', MyDefaultArgs('one')) |
| 110 | assert_fails('call MyDefaultArgs("one", "two")', 'E118:') |
| 111 | |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 112 | CheckScriptFailure(['def Func(arg: number = asdf)', 'enddef', 'defcompile'], 'E1001:') |
| 113 | CheckScriptFailure(['def Func(arg: number = "text")', 'enddef', 'defcompile'], 'E1013: argument 1: type mismatch, expected number but got string') |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 114 | enddef |
| 115 | |
| 116 | def Test_nested_function() |
| 117 | def Nested(arg: string): string |
| 118 | return 'nested ' .. arg |
| 119 | enddef |
| 120 | assert_equal('nested function', Nested('function')) |
| 121 | |
Bram Moolenaar | 0e65d3d | 2020-05-05 17:53:16 +0200 | [diff] [blame] | 122 | CheckDefFailure(['def Nested()', 'enddef', 'Nested(66)'], 'E118:') |
| 123 | CheckDefFailure(['def Nested(arg: string)', 'enddef', 'Nested()'], 'E119:') |
| 124 | |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 125 | CheckDefFailure(['func Nested()', 'endfunc'], 'E1086:') |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 126 | enddef |
| 127 | |
| 128 | func Test_call_default_args_from_func() |
| 129 | call assert_equal('string', MyDefaultArgs()) |
| 130 | call assert_equal('one', MyDefaultArgs('one')) |
| 131 | call assert_fails('call MyDefaultArgs("one", "two")', 'E118:') |
| 132 | endfunc |
| 133 | |
| 134 | func TakesOneArg(arg) |
| 135 | echo a:arg |
| 136 | endfunc |
| 137 | |
| 138 | def Test_call_wrong_args() |
| 139 | call CheckDefFailure(['TakesOneArg()'], 'E119:') |
| 140 | call CheckDefFailure(['TakesOneArg(11, 22)'], 'E118:') |
| 141 | call CheckDefFailure(['bufnr(xxx)'], 'E1001:') |
Bram Moolenaar | 1c0d44f | 2020-05-02 19:04:58 +0200 | [diff] [blame] | 142 | call CheckScriptFailure(['def Func(Ref: func(s: string))'], 'E475:') |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 143 | enddef |
| 144 | |
| 145 | " Default arg and varargs |
| 146 | def MyDefVarargs(one: string, two = 'foo', ...rest: list<string>): string |
| 147 | let res = one .. ',' .. two |
| 148 | for s in rest |
| 149 | res ..= ',' .. s |
| 150 | endfor |
| 151 | return res |
| 152 | enddef |
| 153 | |
| 154 | def Test_call_def_varargs() |
| 155 | call assert_fails('call MyDefVarargs()', 'E119:') |
| 156 | assert_equal('one,foo', MyDefVarargs('one')) |
| 157 | assert_equal('one,two', MyDefVarargs('one', 'two')) |
| 158 | assert_equal('one,two,three', MyDefVarargs('one', 'two', 'three')) |
Bram Moolenaar | 49cf7cc | 2020-04-07 22:45:00 +0200 | [diff] [blame] | 159 | call CheckDefFailure(['MyDefVarargs("one", 22)'], 'E1013: argument 2: type mismatch, expected string but got number') |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 160 | enddef |
| 161 | |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 162 | let s:value = '' |
| 163 | |
| 164 | def FuncOneDefArg(opt = 'text') |
| 165 | s:value = opt |
| 166 | enddef |
| 167 | |
| 168 | def FuncTwoDefArg(nr = 123, opt = 'text'): string |
| 169 | return nr .. opt |
| 170 | enddef |
| 171 | |
| 172 | def FuncVarargs(...arg: list<string>): string |
| 173 | return join(arg, ',') |
| 174 | enddef |
| 175 | |
| 176 | def Test_func_type_varargs() |
| 177 | let RefDefArg: func(?string) |
| 178 | RefDefArg = FuncOneDefArg |
| 179 | RefDefArg() |
| 180 | assert_equal('text', s:value) |
| 181 | RefDefArg('some') |
| 182 | assert_equal('some', s:value) |
| 183 | |
| 184 | let RefDef2Arg: func(?number, ?string): string |
| 185 | RefDef2Arg = FuncTwoDefArg |
| 186 | assert_equal('123text', RefDef2Arg()) |
| 187 | assert_equal('99text', RefDef2Arg(99)) |
| 188 | assert_equal('77some', RefDef2Arg(77, 'some')) |
| 189 | |
| 190 | call CheckDefFailure(['let RefWrong: func(string?)'], 'E1010:') |
| 191 | call CheckDefFailure(['let RefWrong: func(?string, string)'], 'E1007:') |
| 192 | |
| 193 | let RefVarargs: func(...list<string>): string |
| 194 | RefVarargs = FuncVarargs |
| 195 | assert_equal('', RefVarargs()) |
| 196 | assert_equal('one', RefVarargs('one')) |
| 197 | assert_equal('one,two', RefVarargs('one', 'two')) |
| 198 | |
| 199 | call CheckDefFailure(['let RefWrong: func(...list<string>, string)'], 'E110:') |
| 200 | call CheckDefFailure(['let RefWrong: func(...list<string>, ?string)'], 'E110:') |
| 201 | enddef |
| 202 | |
Bram Moolenaar | 0b76b42 | 2020-04-07 22:05:08 +0200 | [diff] [blame] | 203 | " Only varargs |
| 204 | def MyVarargsOnly(...args: list<string>): string |
| 205 | return join(args, ',') |
| 206 | enddef |
| 207 | |
| 208 | def Test_call_varargs_only() |
| 209 | assert_equal('', MyVarargsOnly()) |
| 210 | assert_equal('one', MyVarargsOnly('one')) |
| 211 | assert_equal('one,two', MyVarargsOnly('one', 'two')) |
| 212 | call CheckDefFailure(['MyVarargsOnly(1)'], 'E1013: argument 1: type mismatch, expected string but got number') |
| 213 | call CheckDefFailure(['MyVarargsOnly("one", 2)'], 'E1013: argument 2: type mismatch, expected string but got number') |
| 214 | enddef |
| 215 | |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 216 | def Test_using_var_as_arg() |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 217 | call writefile(['def Func(x: number)', 'let x = 234', 'enddef', 'defcompile'], 'Xdef') |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 218 | call assert_fails('so Xdef', 'E1006:') |
| 219 | call delete('Xdef') |
| 220 | enddef |
| 221 | |
Bram Moolenaar | cb2bdb1 | 2020-05-10 22:53:56 +0200 | [diff] [blame] | 222 | def DictArg(arg: dict<string>) |
| 223 | arg['key'] = 'value' |
| 224 | enddef |
| 225 | |
| 226 | def ListArg(arg: list<string>) |
| 227 | arg[0] = 'value' |
| 228 | enddef |
| 229 | |
| 230 | def Test_assign_to_argument() |
| 231 | " works for dict and list |
| 232 | let d: dict<string> = {} |
| 233 | DictArg(d) |
| 234 | assert_equal('value', d['key']) |
| 235 | let l: list<string> = [] |
| 236 | ListArg(l) |
| 237 | assert_equal('value', l[0]) |
| 238 | |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 239 | call CheckScriptFailure(['def Func(arg: number)', 'arg = 3', 'enddef', 'defcompile'], 'E1090:') |
Bram Moolenaar | cb2bdb1 | 2020-05-10 22:53:56 +0200 | [diff] [blame] | 240 | enddef |
| 241 | |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 242 | def Test_call_func_defined_later() |
Bram Moolenaar | 1df8b3f | 2020-04-23 18:13:23 +0200 | [diff] [blame] | 243 | call assert_equal('one', g:DefinedLater('one')) |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 244 | call assert_fails('call NotDefined("one")', 'E117:') |
| 245 | enddef |
| 246 | |
Bram Moolenaar | 1df8b3f | 2020-04-23 18:13:23 +0200 | [diff] [blame] | 247 | func DefinedLater(arg) |
| 248 | return a:arg |
| 249 | endfunc |
| 250 | |
| 251 | def Test_call_funcref() |
| 252 | assert_equal(3, g:SomeFunc('abc')) |
| 253 | assert_fails('NotAFunc()', 'E117:') |
| 254 | assert_fails('g:NotAFunc()', 'E117:') |
| 255 | enddef |
| 256 | |
| 257 | let SomeFunc = function('len') |
| 258 | let NotAFunc = 'text' |
| 259 | |
Bram Moolenaar | 99aaf0c | 2020-04-12 14:39:53 +0200 | [diff] [blame] | 260 | def CombineFuncrefTypes() |
| 261 | " same arguments, different return type |
| 262 | let Ref1: func(bool): string |
| 263 | let Ref2: func(bool): number |
| 264 | let Ref3: func(bool): any |
| 265 | Ref3 = g:cond ? Ref1 : Ref2 |
| 266 | |
| 267 | " different number of arguments |
| 268 | let Refa1: func(bool): number |
| 269 | let Refa2: func(bool, number): number |
| 270 | let Refa3: func: number |
| 271 | Refa3 = g:cond ? Refa1 : Refa2 |
| 272 | |
| 273 | " different argument types |
| 274 | let Refb1: func(bool, string): number |
| 275 | let Refb2: func(string, number): number |
| 276 | let Refb3: func(any, any): number |
| 277 | Refb3 = g:cond ? Refb1 : Refb2 |
| 278 | enddef |
| 279 | |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 280 | def FuncWithForwardCall() |
Bram Moolenaar | 1df8b3f | 2020-04-23 18:13:23 +0200 | [diff] [blame] | 281 | return g:DefinedEvenLater("yes") |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 282 | enddef |
| 283 | |
| 284 | def DefinedEvenLater(arg: string): string |
| 285 | return arg |
| 286 | enddef |
| 287 | |
| 288 | def Test_error_in_nested_function() |
| 289 | " Error in called function requires unwinding the call stack. |
Bram Moolenaar | 05a5551 | 2020-07-05 15:52:19 +0200 | [diff] [blame] | 290 | assert_fails('call FuncWithForwardCall()', 'E1096') |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 291 | enddef |
| 292 | |
| 293 | def Test_return_type_wrong() |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 294 | CheckScriptFailure(['def Func(): number', 'return "a"', 'enddef', 'defcompile'], 'expected number but got string') |
| 295 | CheckScriptFailure(['def Func(): string', 'return 1', 'enddef', 'defcompile'], 'expected string but got number') |
Bram Moolenaar | 05a5551 | 2020-07-05 15:52:19 +0200 | [diff] [blame] | 296 | CheckScriptFailure(['def Func(): void', 'return "a"', 'enddef', 'defcompile'], 'E1096: Returning a value in a function without a return type') |
| 297 | CheckScriptFailure(['def Func()', 'return "a"', 'enddef', 'defcompile'], 'E1096: Returning a value in a function without a return type') |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 298 | |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 299 | CheckScriptFailure(['def Func(): number', 'return', 'enddef', 'defcompile'], 'E1003:') |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 300 | |
| 301 | CheckScriptFailure(['def Func(): list', 'return []', 'enddef'], 'E1008:') |
| 302 | CheckScriptFailure(['def Func(): dict', 'return {}', 'enddef'], 'E1008:') |
Bram Moolenaar | ee4e0c1 | 2020-04-06 21:35:05 +0200 | [diff] [blame] | 303 | CheckScriptFailure(['def Func()', 'return 1'], 'E1057:') |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 304 | enddef |
| 305 | |
| 306 | def Test_arg_type_wrong() |
| 307 | CheckScriptFailure(['def Func3(items: list)', 'echo "a"', 'enddef'], 'E1008: Missing <type>') |
Bram Moolenaar | ee4e0c1 | 2020-04-06 21:35:05 +0200 | [diff] [blame] | 308 | CheckScriptFailure(['def Func4(...)', 'echo "a"', 'enddef'], 'E1055: Missing name after ...') |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 309 | CheckScriptFailure(['def Func5(items:string)', 'echo "a"'], 'E1069:') |
Bram Moolenaar | 6e94978 | 2020-04-13 17:21:00 +0200 | [diff] [blame] | 310 | CheckScriptFailure(['def Func5(items)', 'echo "a"'], 'E1077:') |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 311 | enddef |
| 312 | |
| 313 | def Test_vim9script_call() |
| 314 | let lines =<< trim END |
| 315 | vim9script |
| 316 | let var = '' |
| 317 | def MyFunc(arg: string) |
| 318 | var = arg |
| 319 | enddef |
| 320 | MyFunc('foobar') |
| 321 | assert_equal('foobar', var) |
| 322 | |
| 323 | let str = 'barfoo' |
| 324 | str->MyFunc() |
| 325 | assert_equal('barfoo', var) |
| 326 | |
Bram Moolenaar | 6797966 | 2020-06-20 22:50:47 +0200 | [diff] [blame] | 327 | g:value = 'value' |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 328 | g:value->MyFunc() |
| 329 | assert_equal('value', var) |
| 330 | |
| 331 | let listvar = [] |
| 332 | def ListFunc(arg: list<number>) |
| 333 | listvar = arg |
| 334 | enddef |
| 335 | [1, 2, 3]->ListFunc() |
| 336 | assert_equal([1, 2, 3], listvar) |
| 337 | |
| 338 | let dictvar = {} |
| 339 | def DictFunc(arg: dict<number>) |
| 340 | dictvar = arg |
| 341 | enddef |
| 342 | {'a': 1, 'b': 2}->DictFunc() |
| 343 | assert_equal(#{a: 1, b: 2}, dictvar) |
| 344 | def CompiledDict() |
| 345 | {'a': 3, 'b': 4}->DictFunc() |
| 346 | enddef |
| 347 | CompiledDict() |
| 348 | assert_equal(#{a: 3, b: 4}, dictvar) |
| 349 | |
| 350 | #{a: 3, b: 4}->DictFunc() |
| 351 | assert_equal(#{a: 3, b: 4}, dictvar) |
| 352 | |
| 353 | ('text')->MyFunc() |
| 354 | assert_equal('text', var) |
| 355 | ("some")->MyFunc() |
| 356 | assert_equal('some', var) |
Bram Moolenaar | e6b5324 | 2020-07-01 17:28:33 +0200 | [diff] [blame] | 357 | |
Bram Moolenaar | 3d48e25 | 2020-07-15 14:15:52 +0200 | [diff] [blame] | 358 | 'asdfasdf'->MyFunc() |
| 359 | assert_equal('asdfasdf', var) |
| 360 | |
| 361 | def UseString() |
| 362 | 'xyork'->MyFunc() |
| 363 | enddef |
| 364 | UseString() |
| 365 | assert_equal('xyork', var) |
| 366 | |
Bram Moolenaar | e6b5324 | 2020-07-01 17:28:33 +0200 | [diff] [blame] | 367 | MyFunc( |
| 368 | 'continued' |
| 369 | ) |
| 370 | assert_equal('continued', |
| 371 | var |
| 372 | ) |
| 373 | |
| 374 | call MyFunc( |
| 375 | 'more' |
| 376 | .. |
| 377 | 'lines' |
| 378 | ) |
| 379 | assert_equal( |
| 380 | 'morelines', |
| 381 | var) |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 382 | END |
| 383 | writefile(lines, 'Xcall.vim') |
| 384 | source Xcall.vim |
| 385 | delete('Xcall.vim') |
| 386 | enddef |
| 387 | |
| 388 | def Test_vim9script_call_fail_decl() |
| 389 | let lines =<< trim END |
| 390 | vim9script |
| 391 | let var = '' |
| 392 | def MyFunc(arg: string) |
| 393 | let var = 123 |
| 394 | enddef |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 395 | defcompile |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 396 | END |
| 397 | writefile(lines, 'Xcall_decl.vim') |
| 398 | assert_fails('source Xcall_decl.vim', 'E1054:') |
| 399 | delete('Xcall_decl.vim') |
| 400 | enddef |
| 401 | |
| 402 | def Test_vim9script_call_fail_const() |
| 403 | let lines =<< trim END |
| 404 | vim9script |
| 405 | const var = '' |
| 406 | def MyFunc(arg: string) |
| 407 | var = 'asdf' |
| 408 | enddef |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 409 | defcompile |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 410 | END |
| 411 | writefile(lines, 'Xcall_const.vim') |
| 412 | assert_fails('source Xcall_const.vim', 'E46:') |
| 413 | delete('Xcall_const.vim') |
| 414 | enddef |
| 415 | |
| 416 | " Test that inside :function a Python function can be defined, :def is not |
| 417 | " recognized. |
| 418 | func Test_function_python() |
| 419 | CheckFeature python3 |
| 420 | let py = 'python3' |
| 421 | execute py "<< EOF" |
| 422 | def do_something(): |
| 423 | return 1 |
| 424 | EOF |
| 425 | endfunc |
| 426 | |
| 427 | def Test_delfunc() |
| 428 | let lines =<< trim END |
| 429 | vim9script |
Bram Moolenaar | 4c17ad9 | 2020-04-27 22:47:51 +0200 | [diff] [blame] | 430 | def g:GoneSoon() |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 431 | echo 'hello' |
| 432 | enddef |
| 433 | |
| 434 | def CallGoneSoon() |
| 435 | GoneSoon() |
| 436 | enddef |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 437 | defcompile |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 438 | |
Bram Moolenaar | 4c17ad9 | 2020-04-27 22:47:51 +0200 | [diff] [blame] | 439 | delfunc g:GoneSoon |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 440 | CallGoneSoon() |
| 441 | END |
| 442 | writefile(lines, 'XToDelFunc') |
| 443 | assert_fails('so XToDelFunc', 'E933') |
| 444 | assert_fails('so XToDelFunc', 'E933') |
| 445 | |
| 446 | delete('XToDelFunc') |
| 447 | enddef |
| 448 | |
| 449 | def Test_redef_failure() |
| 450 | call writefile(['def Func0(): string', 'return "Func0"', 'enddef'], 'Xdef') |
| 451 | so Xdef |
| 452 | call writefile(['def Func1(): string', 'return "Func1"', 'enddef'], 'Xdef') |
| 453 | so Xdef |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 454 | call writefile(['def! Func0(): string', 'enddef', 'defcompile'], 'Xdef') |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 455 | call assert_fails('so Xdef', 'E1027:') |
| 456 | call writefile(['def Func2(): string', 'return "Func2"', 'enddef'], 'Xdef') |
| 457 | so Xdef |
| 458 | call delete('Xdef') |
| 459 | |
Bram Moolenaar | 1df8b3f | 2020-04-23 18:13:23 +0200 | [diff] [blame] | 460 | call assert_equal(0, g:Func0()) |
| 461 | call assert_equal('Func1', g:Func1()) |
| 462 | call assert_equal('Func2', g:Func2()) |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 463 | |
| 464 | delfunc! Func0 |
| 465 | delfunc! Func1 |
| 466 | delfunc! Func2 |
| 467 | enddef |
| 468 | |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 469 | def Test_vim9script_func() |
| 470 | let lines =<< trim END |
| 471 | vim9script |
| 472 | func Func(arg) |
| 473 | echo a:arg |
| 474 | endfunc |
| 475 | Func('text') |
| 476 | END |
| 477 | writefile(lines, 'XVim9Func') |
| 478 | so XVim9Func |
| 479 | |
| 480 | delete('XVim9Func') |
| 481 | enddef |
| 482 | |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 483 | " Test for internal functions returning different types |
| 484 | func Test_InternalFuncRetType() |
| 485 | let lines =<< trim END |
| 486 | def RetFloat(): float |
| 487 | return ceil(1.456) |
| 488 | enddef |
| 489 | |
| 490 | def RetListAny(): list<any> |
| 491 | return items({'k' : 'v'}) |
| 492 | enddef |
| 493 | |
| 494 | def RetListString(): list<string> |
| 495 | return split('a:b:c', ':') |
| 496 | enddef |
| 497 | |
| 498 | def RetListDictAny(): list<dict<any>> |
| 499 | return getbufinfo() |
| 500 | enddef |
| 501 | |
| 502 | def RetDictNumber(): dict<number> |
| 503 | return wordcount() |
| 504 | enddef |
| 505 | |
| 506 | def RetDictString(): dict<string> |
| 507 | return environ() |
| 508 | enddef |
| 509 | END |
| 510 | call writefile(lines, 'Xscript') |
| 511 | source Xscript |
| 512 | |
| 513 | call assert_equal(2.0, RetFloat()) |
| 514 | call assert_equal([['k', 'v']], RetListAny()) |
| 515 | call assert_equal(['a', 'b', 'c'], RetListString()) |
| 516 | call assert_notequal([], RetListDictAny()) |
| 517 | call assert_notequal({}, RetDictNumber()) |
| 518 | call assert_notequal({}, RetDictString()) |
| 519 | call delete('Xscript') |
| 520 | endfunc |
| 521 | |
| 522 | " Test for passing too many or too few arguments to internal functions |
| 523 | func Test_internalfunc_arg_error() |
| 524 | let l =<< trim END |
| 525 | def! FArgErr(): float |
| 526 | return ceil(1.1, 2) |
| 527 | enddef |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 528 | defcompile |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 529 | END |
| 530 | call writefile(l, 'Xinvalidarg') |
| 531 | call assert_fails('so Xinvalidarg', 'E118:') |
| 532 | let l =<< trim END |
| 533 | def! FArgErr(): float |
| 534 | return ceil() |
| 535 | enddef |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 536 | defcompile |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 537 | END |
| 538 | call writefile(l, 'Xinvalidarg') |
| 539 | call assert_fails('so Xinvalidarg', 'E119:') |
| 540 | call delete('Xinvalidarg') |
| 541 | endfunc |
| 542 | |
| 543 | let s:funcResult = 0 |
| 544 | |
| 545 | def FuncNoArgNoRet() |
| 546 | funcResult = 11 |
| 547 | enddef |
| 548 | |
| 549 | def FuncNoArgRetNumber(): number |
| 550 | funcResult = 22 |
| 551 | return 1234 |
| 552 | enddef |
| 553 | |
Bram Moolenaar | ec5929d | 2020-04-07 20:53:39 +0200 | [diff] [blame] | 554 | def FuncNoArgRetString(): string |
| 555 | funcResult = 45 |
| 556 | return 'text' |
| 557 | enddef |
| 558 | |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 559 | def FuncOneArgNoRet(arg: number) |
| 560 | funcResult = arg |
| 561 | enddef |
| 562 | |
| 563 | def FuncOneArgRetNumber(arg: number): number |
| 564 | funcResult = arg |
| 565 | return arg |
| 566 | enddef |
| 567 | |
Bram Moolenaar | 08938ee | 2020-04-11 23:17:17 +0200 | [diff] [blame] | 568 | def FuncTwoArgNoRet(one: bool, two: number) |
| 569 | funcResult = two |
| 570 | enddef |
| 571 | |
Bram Moolenaar | ec5929d | 2020-04-07 20:53:39 +0200 | [diff] [blame] | 572 | def FuncOneArgRetString(arg: string): string |
| 573 | return arg |
| 574 | enddef |
| 575 | |
Bram Moolenaar | 8922860 | 2020-04-05 22:14:54 +0200 | [diff] [blame] | 576 | def FuncOneArgRetAny(arg: any): any |
| 577 | return arg |
| 578 | enddef |
| 579 | |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 580 | def Test_func_type() |
| 581 | let Ref1: func() |
| 582 | funcResult = 0 |
| 583 | Ref1 = FuncNoArgNoRet |
| 584 | Ref1() |
| 585 | assert_equal(11, funcResult) |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 586 | |
| 587 | let Ref2: func |
| 588 | funcResult = 0 |
| 589 | Ref2 = FuncNoArgNoRet |
| 590 | Ref2() |
| 591 | assert_equal(11, funcResult) |
| 592 | |
| 593 | funcResult = 0 |
| 594 | Ref2 = FuncOneArgNoRet |
| 595 | Ref2(12) |
| 596 | assert_equal(12, funcResult) |
| 597 | |
| 598 | funcResult = 0 |
| 599 | Ref2 = FuncNoArgRetNumber |
| 600 | assert_equal(1234, Ref2()) |
| 601 | assert_equal(22, funcResult) |
| 602 | |
| 603 | funcResult = 0 |
| 604 | Ref2 = FuncOneArgRetNumber |
| 605 | assert_equal(13, Ref2(13)) |
| 606 | assert_equal(13, funcResult) |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 607 | enddef |
| 608 | |
Bram Moolenaar | 9978d47 | 2020-07-05 16:01:56 +0200 | [diff] [blame] | 609 | def Test_repeat_return_type() |
| 610 | let res = 0 |
| 611 | for n in repeat([1], 3) |
| 612 | res += n |
| 613 | endfor |
| 614 | assert_equal(3, res) |
Bram Moolenaar | fce82b3 | 2020-07-05 16:07:21 +0200 | [diff] [blame] | 615 | |
| 616 | res = 0 |
| 617 | for n in add([1, 2], 3) |
| 618 | res += n |
| 619 | endfor |
| 620 | assert_equal(6, res) |
Bram Moolenaar | 9978d47 | 2020-07-05 16:01:56 +0200 | [diff] [blame] | 621 | enddef |
| 622 | |
Bram Moolenaar | 846178a | 2020-07-05 17:04:13 +0200 | [diff] [blame] | 623 | def Test_argv_return_type() |
| 624 | next fileone filetwo |
| 625 | let res = '' |
| 626 | for name in argv() |
| 627 | res ..= name |
| 628 | endfor |
| 629 | assert_equal('fileonefiletwo', res) |
| 630 | enddef |
| 631 | |
Bram Moolenaar | ec5929d | 2020-04-07 20:53:39 +0200 | [diff] [blame] | 632 | def Test_func_type_part() |
| 633 | let RefVoid: func: void |
| 634 | RefVoid = FuncNoArgNoRet |
| 635 | RefVoid = FuncOneArgNoRet |
| 636 | CheckDefFailure(['let RefVoid: func: void', 'RefVoid = FuncNoArgRetNumber'], 'E1013: type mismatch, expected func() but got func(): number') |
Bram Moolenaar | 6ff71d8 | 2020-05-24 23:45:24 +0200 | [diff] [blame] | 637 | CheckDefFailure(['let RefVoid: func: void', 'RefVoid = FuncNoArgRetString'], 'E1013: type mismatch, expected func() but got func(): string') |
Bram Moolenaar | ec5929d | 2020-04-07 20:53:39 +0200 | [diff] [blame] | 638 | |
| 639 | let RefAny: func(): any |
| 640 | RefAny = FuncNoArgRetNumber |
| 641 | RefAny = FuncNoArgRetString |
| 642 | CheckDefFailure(['let RefAny: func(): any', 'RefAny = FuncNoArgNoRet'], 'E1013: type mismatch, expected func(): any but got func()') |
| 643 | CheckDefFailure(['let RefAny: func(): any', 'RefAny = FuncOneArgNoRet'], 'E1013: type mismatch, expected func(): any but got func(number)') |
| 644 | |
| 645 | let RefNr: func: number |
| 646 | RefNr = FuncNoArgRetNumber |
| 647 | RefNr = FuncOneArgRetNumber |
| 648 | CheckDefFailure(['let RefNr: func: number', 'RefNr = FuncNoArgNoRet'], 'E1013: type mismatch, expected func(): number but got func()') |
Bram Moolenaar | 6ff71d8 | 2020-05-24 23:45:24 +0200 | [diff] [blame] | 649 | CheckDefFailure(['let RefNr: func: number', 'RefNr = FuncNoArgRetString'], 'E1013: type mismatch, expected func(): number but got func(): string') |
Bram Moolenaar | ec5929d | 2020-04-07 20:53:39 +0200 | [diff] [blame] | 650 | |
| 651 | let RefStr: func: string |
| 652 | RefStr = FuncNoArgRetString |
| 653 | RefStr = FuncOneArgRetString |
| 654 | CheckDefFailure(['let RefStr: func: string', 'RefStr = FuncNoArgNoRet'], 'E1013: type mismatch, expected func(): string but got func()') |
| 655 | CheckDefFailure(['let RefStr: func: string', 'RefStr = FuncNoArgRetNumber'], 'E1013: type mismatch, expected func(): string but got func(): number') |
| 656 | enddef |
| 657 | |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 658 | def Test_func_type_fails() |
| 659 | CheckDefFailure(['let ref1: func()'], 'E704:') |
| 660 | |
| 661 | CheckDefFailure(['let Ref1: func()', 'Ref1 = FuncNoArgRetNumber'], 'E1013: type mismatch, expected func() but got func(): number') |
| 662 | CheckDefFailure(['let Ref1: func()', 'Ref1 = FuncOneArgNoRet'], 'E1013: type mismatch, expected func() but got func(number)') |
| 663 | CheckDefFailure(['let Ref1: func()', 'Ref1 = FuncOneArgRetNumber'], 'E1013: type mismatch, expected func() but got func(number): number') |
Bram Moolenaar | 6ff71d8 | 2020-05-24 23:45:24 +0200 | [diff] [blame] | 664 | CheckDefFailure(['let Ref1: func(bool)', 'Ref1 = FuncTwoArgNoRet'], 'E1013: type mismatch, expected func(bool) but got func(bool, number)') |
| 665 | CheckDefFailure(['let Ref1: func(?bool)', 'Ref1 = FuncTwoArgNoRet'], 'E1013: type mismatch, expected func(?bool) but got func(bool, number)') |
| 666 | CheckDefFailure(['let Ref1: func(...bool)', 'Ref1 = FuncTwoArgNoRet'], 'E1013: type mismatch, expected func(...bool) but got func(bool, number)') |
Bram Moolenaar | 08938ee | 2020-04-11 23:17:17 +0200 | [diff] [blame] | 667 | |
| 668 | call CheckDefFailure(['let RefWrong: func(string ,number)'], 'E1068:') |
| 669 | call CheckDefFailure(['let RefWrong: func(string,number)'], 'E1069:') |
| 670 | call CheckDefFailure(['let RefWrong: func(bool, bool, bool, bool, bool, bool, bool, bool, bool, bool, bool, bool, bool, bool, bool, bool, bool, bool, bool, bool)'], 'E740:') |
| 671 | call CheckDefFailure(['let RefWrong: func(bool):string'], 'E1069:') |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 672 | enddef |
| 673 | |
Bram Moolenaar | 8922860 | 2020-04-05 22:14:54 +0200 | [diff] [blame] | 674 | def Test_func_return_type() |
| 675 | let nr: number |
| 676 | nr = FuncNoArgRetNumber() |
| 677 | assert_equal(1234, nr) |
| 678 | |
| 679 | nr = FuncOneArgRetAny(122) |
| 680 | assert_equal(122, nr) |
| 681 | |
| 682 | let str: string |
| 683 | str = FuncOneArgRetAny('yes') |
| 684 | assert_equal('yes', str) |
| 685 | |
| 686 | CheckDefFailure(['let str: string', 'str = FuncNoArgRetNumber()'], 'E1013: type mismatch, expected string but got number') |
| 687 | enddef |
| 688 | |
Bram Moolenaar | 5e774c7 | 2020-04-12 21:53:00 +0200 | [diff] [blame] | 689 | def MultiLine( |
| 690 | arg1: string, |
| 691 | arg2 = 1234, |
| 692 | ...rest: list<string> |
| 693 | ): string |
| 694 | return arg1 .. arg2 .. join(rest, '-') |
| 695 | enddef |
| 696 | |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 697 | def MultiLineComment( |
| 698 | arg1: string, # comment |
| 699 | arg2 = 1234, # comment |
| 700 | ...rest: list<string> # comment |
| 701 | ): string # comment |
| 702 | return arg1 .. arg2 .. join(rest, '-') |
| 703 | enddef |
| 704 | |
Bram Moolenaar | 5e774c7 | 2020-04-12 21:53:00 +0200 | [diff] [blame] | 705 | def Test_multiline() |
| 706 | assert_equal('text1234', MultiLine('text')) |
| 707 | assert_equal('text777', MultiLine('text', 777)) |
| 708 | assert_equal('text777one', MultiLine('text', 777, 'one')) |
| 709 | assert_equal('text777one-two', MultiLine('text', 777, 'one', 'two')) |
| 710 | enddef |
| 711 | |
Bram Moolenaar | 23e0325 | 2020-04-12 22:22:31 +0200 | [diff] [blame] | 712 | func Test_multiline_not_vim9() |
| 713 | call assert_equal('text1234', MultiLine('text')) |
| 714 | call assert_equal('text777', MultiLine('text', 777)) |
| 715 | call assert_equal('text777one', MultiLine('text', 777, 'one')) |
| 716 | call assert_equal('text777one-two', MultiLine('text', 777, 'one', 'two')) |
| 717 | endfunc |
| 718 | |
Bram Moolenaar | 5e774c7 | 2020-04-12 21:53:00 +0200 | [diff] [blame] | 719 | |
Bram Moolenaar | ee4e0c1 | 2020-04-06 21:35:05 +0200 | [diff] [blame] | 720 | " When using CheckScriptFailure() for the below test, E1010 is generated instead |
| 721 | " of E1056. |
| 722 | func Test_E1056_1059() |
| 723 | let caught_1056 = 0 |
| 724 | try |
| 725 | def F(): |
| 726 | return 1 |
| 727 | enddef |
| 728 | catch /E1056:/ |
| 729 | let caught_1056 = 1 |
| 730 | endtry |
| 731 | call assert_equal(1, caught_1056) |
| 732 | |
| 733 | let caught_1059 = 0 |
| 734 | try |
| 735 | def F5(items : list) |
| 736 | echo 'a' |
| 737 | enddef |
| 738 | catch /E1059:/ |
| 739 | let caught_1059 = 1 |
| 740 | endtry |
| 741 | call assert_equal(1, caught_1059) |
| 742 | endfunc |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 743 | |
Bram Moolenaar | 015f426 | 2020-05-05 21:25:22 +0200 | [diff] [blame] | 744 | func DelMe() |
| 745 | echo 'DelMe' |
| 746 | endfunc |
| 747 | |
| 748 | def Test_deleted_function() |
| 749 | CheckDefExecFailure([ |
| 750 | 'let RefMe: func = function("g:DelMe")', |
| 751 | 'delfunc g:DelMe', |
| 752 | 'echo RefMe()'], 'E117:') |
| 753 | enddef |
| 754 | |
| 755 | def Test_unknown_function() |
| 756 | CheckDefExecFailure([ |
| 757 | 'let Ref: func = function("NotExist")', |
Bram Moolenaar | 9b7bf9e | 2020-07-11 22:14:59 +0200 | [diff] [blame] | 758 | 'delfunc g:NotExist'], 'E700:') |
Bram Moolenaar | 015f426 | 2020-05-05 21:25:22 +0200 | [diff] [blame] | 759 | enddef |
| 760 | |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 761 | def RefFunc(Ref: func(string): string): string |
| 762 | return Ref('more') |
| 763 | enddef |
| 764 | |
| 765 | def Test_closure_simple() |
| 766 | let local = 'some ' |
| 767 | assert_equal('some more', RefFunc({s -> local .. s})) |
| 768 | enddef |
| 769 | |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 770 | def MakeRef() |
| 771 | let local = 'some ' |
| 772 | g:Ref = {s -> local .. s} |
| 773 | enddef |
| 774 | |
| 775 | def Test_closure_ref_after_return() |
| 776 | MakeRef() |
| 777 | assert_equal('some thing', g:Ref('thing')) |
| 778 | unlet g:Ref |
| 779 | enddef |
| 780 | |
Bram Moolenaar | 5adc55c | 2020-05-02 23:12:58 +0200 | [diff] [blame] | 781 | def MakeTwoRefs() |
| 782 | let local = ['some'] |
| 783 | g:Extend = {s -> local->add(s)} |
| 784 | g:Read = {-> local} |
| 785 | enddef |
| 786 | |
| 787 | def Test_closure_two_refs() |
| 788 | MakeTwoRefs() |
| 789 | assert_equal('some', join(g:Read(), ' ')) |
| 790 | g:Extend('more') |
| 791 | assert_equal('some more', join(g:Read(), ' ')) |
| 792 | g:Extend('even') |
| 793 | assert_equal('some more even', join(g:Read(), ' ')) |
| 794 | |
| 795 | unlet g:Extend |
| 796 | unlet g:Read |
| 797 | enddef |
| 798 | |
Bram Moolenaar | 5adc55c | 2020-05-02 23:12:58 +0200 | [diff] [blame] | 799 | def ReadRef(Ref: func(): list<string>): string |
| 800 | return join(Ref(), ' ') |
| 801 | enddef |
| 802 | |
| 803 | def ExtendRef(Ref: func(string), add: string) |
| 804 | Ref(add) |
| 805 | enddef |
| 806 | |
| 807 | def Test_closure_two_indirect_refs() |
Bram Moolenaar | f7779c6 | 2020-05-03 15:38:16 +0200 | [diff] [blame] | 808 | MakeTwoRefs() |
Bram Moolenaar | 5adc55c | 2020-05-02 23:12:58 +0200 | [diff] [blame] | 809 | assert_equal('some', ReadRef(g:Read)) |
| 810 | ExtendRef(g:Extend, 'more') |
| 811 | assert_equal('some more', ReadRef(g:Read)) |
| 812 | ExtendRef(g:Extend, 'even') |
| 813 | assert_equal('some more even', ReadRef(g:Read)) |
| 814 | |
| 815 | unlet g:Extend |
| 816 | unlet g:Read |
| 817 | enddef |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 818 | |
Bram Moolenaar | 2fd4cd7 | 2020-05-03 22:30:49 +0200 | [diff] [blame] | 819 | def MakeArgRefs(theArg: string) |
| 820 | let local = 'loc_val' |
| 821 | g:UseArg = {s -> theArg .. '/' .. local .. '/' .. s} |
| 822 | enddef |
| 823 | |
| 824 | def MakeArgRefsVarargs(theArg: string, ...rest: list<string>) |
| 825 | let local = 'the_loc' |
| 826 | g:UseVararg = {s -> theArg .. '/' .. local .. '/' .. s .. '/' .. join(rest)} |
| 827 | enddef |
| 828 | |
| 829 | def Test_closure_using_argument() |
| 830 | MakeArgRefs('arg_val') |
| 831 | assert_equal('arg_val/loc_val/call_val', g:UseArg('call_val')) |
| 832 | |
| 833 | MakeArgRefsVarargs('arg_val', 'one', 'two') |
| 834 | assert_equal('arg_val/the_loc/call_val/one two', g:UseVararg('call_val')) |
| 835 | |
| 836 | unlet g:UseArg |
| 837 | unlet g:UseVararg |
| 838 | enddef |
| 839 | |
Bram Moolenaar | b68b346 | 2020-05-06 21:06:30 +0200 | [diff] [blame] | 840 | def MakeGetAndAppendRefs() |
| 841 | let local = 'a' |
| 842 | |
| 843 | def Append(arg: string) |
| 844 | local ..= arg |
| 845 | enddef |
| 846 | g:Append = Append |
| 847 | |
| 848 | def Get(): string |
| 849 | return local |
| 850 | enddef |
| 851 | g:Get = Get |
| 852 | enddef |
| 853 | |
| 854 | def Test_closure_append_get() |
| 855 | MakeGetAndAppendRefs() |
| 856 | assert_equal('a', g:Get()) |
| 857 | g:Append('-b') |
| 858 | assert_equal('a-b', g:Get()) |
| 859 | g:Append('-c') |
| 860 | assert_equal('a-b-c', g:Get()) |
| 861 | |
| 862 | unlet g:Append |
| 863 | unlet g:Get |
| 864 | enddef |
| 865 | |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 866 | def Test_nested_closure() |
| 867 | let local = 'text' |
| 868 | def Closure(arg: string): string |
| 869 | return local .. arg |
| 870 | enddef |
| 871 | assert_equal('text!!!', Closure('!!!')) |
| 872 | enddef |
| 873 | |
Bram Moolenaar | 6f5b6df | 2020-05-16 21:20:12 +0200 | [diff] [blame] | 874 | func GetResult(Ref) |
| 875 | return a:Ref('some') |
| 876 | endfunc |
| 877 | |
| 878 | def Test_call_closure_not_compiled() |
| 879 | let text = 'text' |
| 880 | g:Ref = {s -> s .. text} |
| 881 | assert_equal('sometext', GetResult(g:Ref)) |
| 882 | enddef |
| 883 | |
Bram Moolenaar | 865af6b | 2020-06-18 18:45:49 +0200 | [diff] [blame] | 884 | def Test_sort_return_type() |
| 885 | let res: list<number> |
| 886 | res = [1, 2, 3]->sort() |
| 887 | enddef |
| 888 | |
Bram Moolenaar | f151ad1 | 2020-06-30 13:38:01 +0200 | [diff] [blame] | 889 | def Test_getqflist_return_type() |
| 890 | let l = getqflist() |
| 891 | assert_equal([], l) |
| 892 | |
| 893 | let d = getqflist(#{items: 0}) |
| 894 | assert_equal(#{items: []}, d) |
| 895 | enddef |
| 896 | |
| 897 | def Test_getloclist_return_type() |
| 898 | let l = getloclist(1) |
| 899 | assert_equal([], l) |
| 900 | |
| 901 | let d = getloclist(1, #{items: 0}) |
| 902 | assert_equal(#{items: []}, d) |
| 903 | enddef |
| 904 | |
Bram Moolenaar | a66ba01 | 2020-07-05 18:41:08 +0200 | [diff] [blame] | 905 | def Test_copy_return_type() |
| 906 | let l = copy([1, 2, 3]) |
| 907 | let res = 0 |
| 908 | for n in l |
| 909 | res += n |
| 910 | endfor |
| 911 | assert_equal(6, res) |
| 912 | |
| 913 | let dl = deepcopy([1, 2, 3]) |
| 914 | res = 0 |
| 915 | for n in dl |
| 916 | res += n |
| 917 | endfor |
| 918 | assert_equal(6, res) |
| 919 | enddef |
| 920 | |
Bram Moolenaar | b3c019c | 2020-07-05 20:08:39 +0200 | [diff] [blame] | 921 | def Test_extend_return_type() |
| 922 | let l = extend([1, 2], [3]) |
| 923 | let res = 0 |
| 924 | for n in l |
| 925 | res += n |
| 926 | endfor |
| 927 | assert_equal(6, res) |
| 928 | enddef |
| 929 | |
Bram Moolenaar | 252e88a | 2020-07-05 20:47:18 +0200 | [diff] [blame] | 930 | def Test_insert_return_type() |
| 931 | let l = insert([2, 1], 3) |
| 932 | let res = 0 |
| 933 | for n in l |
| 934 | res += n |
| 935 | endfor |
| 936 | assert_equal(6, res) |
| 937 | enddef |
| 938 | |
Bram Moolenaar | 6762735 | 2020-07-05 21:10:24 +0200 | [diff] [blame] | 939 | def Test_reverse_return_type() |
| 940 | let l = reverse([1, 2, 3]) |
| 941 | let res = 0 |
| 942 | for n in l |
| 943 | res += n |
| 944 | endfor |
| 945 | assert_equal(6, res) |
| 946 | enddef |
| 947 | |
Bram Moolenaar | ad7c249 | 2020-07-05 20:55:29 +0200 | [diff] [blame] | 948 | def Test_remove_return_type() |
| 949 | let l = remove(#{one: [1, 2], two: [3, 4]}, 'one') |
| 950 | let res = 0 |
| 951 | for n in l |
| 952 | res += n |
| 953 | endfor |
| 954 | assert_equal(3, res) |
| 955 | enddef |
| 956 | |
Bram Moolenaar | 0d94ad6 | 2020-07-05 20:16:41 +0200 | [diff] [blame] | 957 | def Test_filter_return_type() |
| 958 | let l = filter([1, 2, 3], {-> 1}) |
| 959 | let res = 0 |
| 960 | for n in l |
| 961 | res += n |
| 962 | endfor |
| 963 | assert_equal(6, res) |
| 964 | enddef |
| 965 | |
Bram Moolenaar | f1a2368 | 2020-07-13 18:55:48 +0200 | [diff] [blame] | 966 | def Wrong_dict_key_type(items: list<number>): list<number> |
| 967 | return filter(items, {_, val -> get({val: 1}, 'x')}) |
| 968 | enddef |
| 969 | |
| 970 | def Test_wrong_dict_key_type() |
| 971 | assert_fails('Wrong_dict_key_type([1, 2, 3])', 'E1029:') |
| 972 | enddef |
| 973 | |
Bram Moolenaar | acd4c5e | 2020-06-22 19:39:03 +0200 | [diff] [blame] | 974 | def Line_continuation_in_def(dir: string = ''): string |
| 975 | let path: string = empty(dir) |
| 976 | \ ? 'empty' |
| 977 | \ : 'full' |
| 978 | return path |
| 979 | enddef |
| 980 | |
| 981 | def Test_line_continuation_in_def() |
| 982 | assert_equal('full', Line_continuation_in_def('.')) |
| 983 | enddef |
| 984 | |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 985 | def Line_continuation_in_lambda(): list<number> |
| 986 | let x = range(97, 100) |
Bram Moolenaar | 914e7ea | 2020-07-11 15:20:48 +0200 | [diff] [blame] | 987 | ->map({_, v -> nr2char(v) |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 988 | ->toupper()}) |
| 989 | ->reverse() |
| 990 | return x |
| 991 | enddef |
| 992 | |
| 993 | def Test_line_continuation_in_lambda() |
| 994 | assert_equal(['D', 'C', 'B', 'A'], Line_continuation_in_lambda()) |
| 995 | enddef |
| 996 | |
Bram Moolenaar | 8f510af | 2020-07-05 18:48:23 +0200 | [diff] [blame] | 997 | func Test_silent_echo() |
Bram Moolenaar | 47e7d70 | 2020-07-05 18:18:42 +0200 | [diff] [blame] | 998 | CheckScreendump |
| 999 | |
| 1000 | let lines =<< trim END |
| 1001 | vim9script |
| 1002 | def EchoNothing() |
| 1003 | silent echo '' |
| 1004 | enddef |
| 1005 | defcompile |
| 1006 | END |
Bram Moolenaar | 8f510af | 2020-07-05 18:48:23 +0200 | [diff] [blame] | 1007 | call writefile(lines, 'XTest_silent_echo') |
Bram Moolenaar | 47e7d70 | 2020-07-05 18:18:42 +0200 | [diff] [blame] | 1008 | |
| 1009 | " Check that the balloon shows up after a mouse move |
| 1010 | let buf = RunVimInTerminal('-S XTest_silent_echo', {'rows': 6}) |
Bram Moolenaar | 8f510af | 2020-07-05 18:48:23 +0200 | [diff] [blame] | 1011 | call term_sendkeys(buf, ":abc") |
Bram Moolenaar | 47e7d70 | 2020-07-05 18:18:42 +0200 | [diff] [blame] | 1012 | call VerifyScreenDump(buf, 'Test_vim9_silent_echo', {}) |
| 1013 | |
| 1014 | " clean up |
| 1015 | call StopVimInTerminal(buf) |
| 1016 | call delete('XTest_silent_echo') |
Bram Moolenaar | 8f510af | 2020-07-05 18:48:23 +0200 | [diff] [blame] | 1017 | endfunc |
Bram Moolenaar | 47e7d70 | 2020-07-05 18:18:42 +0200 | [diff] [blame] | 1018 | |
Bram Moolenaar | 985116a | 2020-07-12 17:31:09 +0200 | [diff] [blame] | 1019 | def Fibonacci(n: number): number |
| 1020 | if n < 2 |
| 1021 | return n |
| 1022 | else |
| 1023 | return Fibonacci(n - 1) + Fibonacci(n - 2) |
| 1024 | endif |
| 1025 | enddef |
| 1026 | |
| 1027 | def Test_recursive_call() |
| 1028 | assert_equal(6765, Fibonacci(20)) |
| 1029 | enddef |
| 1030 | |
Bram Moolenaar | 08f7a41 | 2020-07-13 20:41:08 +0200 | [diff] [blame] | 1031 | def TreeWalk(dir: string): list<any> |
| 1032 | return readdir(dir)->map({_, val -> |
| 1033 | fnamemodify(dir .. '/' .. val, ':p')->isdirectory() |
| 1034 | ? {val : TreeWalk(dir .. '/' .. val)} |
| 1035 | : val |
| 1036 | }) |
| 1037 | enddef |
| 1038 | |
| 1039 | def Test_closure_in_map() |
| 1040 | mkdir('XclosureDir/tdir', 'p') |
| 1041 | writefile(['111'], 'XclosureDir/file1') |
| 1042 | writefile(['222'], 'XclosureDir/file2') |
| 1043 | writefile(['333'], 'XclosureDir/tdir/file3') |
| 1044 | |
| 1045 | assert_equal(['file1', 'file2', {'tdir': ['file3']}], TreeWalk('XclosureDir')) |
| 1046 | |
| 1047 | delete('XclosureDir', 'rf') |
| 1048 | enddef |
| 1049 | |
Bram Moolenaar | f7779c6 | 2020-05-03 15:38:16 +0200 | [diff] [blame] | 1050 | |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 1051 | " vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker |