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