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 | 1df8b3f | 2020-04-23 18:13:23 +0200 | [diff] [blame] | 353 | enddef |
| 354 | |
| 355 | let SomeFunc = function('len') |
| 356 | let NotAFunc = 'text' |
| 357 | |
Bram Moolenaar | 99aaf0c | 2020-04-12 14:39:53 +0200 | [diff] [blame] | 358 | def CombineFuncrefTypes() |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 359 | # same arguments, different return type |
Bram Moolenaar | 99aaf0c | 2020-04-12 14:39:53 +0200 | [diff] [blame] | 360 | let Ref1: func(bool): string |
| 361 | let Ref2: func(bool): number |
| 362 | let Ref3: func(bool): any |
| 363 | Ref3 = g:cond ? Ref1 : Ref2 |
| 364 | |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 365 | # different number of arguments |
Bram Moolenaar | 99aaf0c | 2020-04-12 14:39:53 +0200 | [diff] [blame] | 366 | let Refa1: func(bool): number |
| 367 | let Refa2: func(bool, number): number |
| 368 | let Refa3: func: number |
| 369 | Refa3 = g:cond ? Refa1 : Refa2 |
| 370 | |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 371 | # different argument types |
Bram Moolenaar | 99aaf0c | 2020-04-12 14:39:53 +0200 | [diff] [blame] | 372 | let Refb1: func(bool, string): number |
| 373 | let Refb2: func(string, number): number |
| 374 | let Refb3: func(any, any): number |
| 375 | Refb3 = g:cond ? Refb1 : Refb2 |
| 376 | enddef |
| 377 | |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 378 | def FuncWithForwardCall() |
Bram Moolenaar | 1df8b3f | 2020-04-23 18:13:23 +0200 | [diff] [blame] | 379 | return g:DefinedEvenLater("yes") |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 380 | enddef |
| 381 | |
| 382 | def DefinedEvenLater(arg: string): string |
| 383 | return arg |
| 384 | enddef |
| 385 | |
| 386 | def Test_error_in_nested_function() |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 387 | # Error in called function requires unwinding the call stack. |
Bram Moolenaar | 05a5551 | 2020-07-05 15:52:19 +0200 | [diff] [blame] | 388 | assert_fails('call FuncWithForwardCall()', 'E1096') |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 389 | enddef |
| 390 | |
| 391 | def Test_return_type_wrong() |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 392 | CheckScriptFailure(['def Func(): number', 'return "a"', 'enddef', 'defcompile'], 'expected number but got string') |
| 393 | 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] | 394 | CheckScriptFailure(['def Func(): void', 'return "a"', 'enddef', 'defcompile'], 'E1096: Returning a value in a function without a return type') |
| 395 | 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] | 396 | |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 397 | CheckScriptFailure(['def Func(): number', 'return', 'enddef', 'defcompile'], 'E1003:') |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 398 | |
| 399 | CheckScriptFailure(['def Func(): list', 'return []', 'enddef'], 'E1008:') |
| 400 | CheckScriptFailure(['def Func(): dict', 'return {}', 'enddef'], 'E1008:') |
Bram Moolenaar | ee4e0c1 | 2020-04-06 21:35:05 +0200 | [diff] [blame] | 401 | CheckScriptFailure(['def Func()', 'return 1'], 'E1057:') |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 402 | enddef |
| 403 | |
| 404 | def Test_arg_type_wrong() |
| 405 | CheckScriptFailure(['def Func3(items: list)', 'echo "a"', 'enddef'], 'E1008: Missing <type>') |
Bram Moolenaar | ee4e0c1 | 2020-04-06 21:35:05 +0200 | [diff] [blame] | 406 | CheckScriptFailure(['def Func4(...)', 'echo "a"', 'enddef'], 'E1055: Missing name after ...') |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 407 | CheckScriptFailure(['def Func5(items:string)', 'echo "a"'], 'E1069:') |
Bram Moolenaar | 6e94978 | 2020-04-13 17:21:00 +0200 | [diff] [blame] | 408 | CheckScriptFailure(['def Func5(items)', 'echo "a"'], 'E1077:') |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 409 | enddef |
| 410 | |
| 411 | def Test_vim9script_call() |
| 412 | let lines =<< trim END |
| 413 | vim9script |
| 414 | let var = '' |
| 415 | def MyFunc(arg: string) |
| 416 | var = arg |
| 417 | enddef |
| 418 | MyFunc('foobar') |
| 419 | assert_equal('foobar', var) |
| 420 | |
| 421 | let str = 'barfoo' |
| 422 | str->MyFunc() |
| 423 | assert_equal('barfoo', var) |
| 424 | |
Bram Moolenaar | 6797966 | 2020-06-20 22:50:47 +0200 | [diff] [blame] | 425 | g:value = 'value' |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 426 | g:value->MyFunc() |
| 427 | assert_equal('value', var) |
| 428 | |
| 429 | let listvar = [] |
| 430 | def ListFunc(arg: list<number>) |
| 431 | listvar = arg |
| 432 | enddef |
| 433 | [1, 2, 3]->ListFunc() |
| 434 | assert_equal([1, 2, 3], listvar) |
| 435 | |
| 436 | let dictvar = {} |
| 437 | def DictFunc(arg: dict<number>) |
| 438 | dictvar = arg |
| 439 | enddef |
| 440 | {'a': 1, 'b': 2}->DictFunc() |
| 441 | assert_equal(#{a: 1, b: 2}, dictvar) |
| 442 | def CompiledDict() |
| 443 | {'a': 3, 'b': 4}->DictFunc() |
| 444 | enddef |
| 445 | CompiledDict() |
| 446 | assert_equal(#{a: 3, b: 4}, dictvar) |
| 447 | |
| 448 | #{a: 3, b: 4}->DictFunc() |
| 449 | assert_equal(#{a: 3, b: 4}, dictvar) |
| 450 | |
| 451 | ('text')->MyFunc() |
| 452 | assert_equal('text', var) |
| 453 | ("some")->MyFunc() |
| 454 | assert_equal('some', var) |
Bram Moolenaar | e6b5324 | 2020-07-01 17:28:33 +0200 | [diff] [blame] | 455 | |
Bram Moolenaar | 13e12b8 | 2020-07-24 18:47:22 +0200 | [diff] [blame] | 456 | # line starting with single quote is not a mark |
Bram Moolenaar | 3d48e25 | 2020-07-15 14:15:52 +0200 | [diff] [blame] | 457 | 'asdfasdf'->MyFunc() |
| 458 | assert_equal('asdfasdf', var) |
| 459 | |
| 460 | def UseString() |
| 461 | 'xyork'->MyFunc() |
| 462 | enddef |
| 463 | UseString() |
| 464 | assert_equal('xyork', var) |
| 465 | |
Bram Moolenaar | 13e12b8 | 2020-07-24 18:47:22 +0200 | [diff] [blame] | 466 | # prepending a colon makes it a mark |
| 467 | new |
| 468 | setline(1, ['aaa', 'bbb', 'ccc']) |
| 469 | normal! 3Gmt1G |
| 470 | :'t |
| 471 | assert_equal(3, getcurpos()[1]) |
| 472 | bwipe! |
| 473 | |
Bram Moolenaar | e6b5324 | 2020-07-01 17:28:33 +0200 | [diff] [blame] | 474 | MyFunc( |
| 475 | 'continued' |
| 476 | ) |
| 477 | assert_equal('continued', |
| 478 | var |
| 479 | ) |
| 480 | |
| 481 | call MyFunc( |
| 482 | 'more' |
| 483 | .. |
| 484 | 'lines' |
| 485 | ) |
| 486 | assert_equal( |
| 487 | 'morelines', |
| 488 | var) |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 489 | END |
| 490 | writefile(lines, 'Xcall.vim') |
| 491 | source Xcall.vim |
| 492 | delete('Xcall.vim') |
| 493 | enddef |
| 494 | |
| 495 | def Test_vim9script_call_fail_decl() |
| 496 | let lines =<< trim END |
| 497 | vim9script |
| 498 | let var = '' |
| 499 | def MyFunc(arg: string) |
| 500 | let var = 123 |
| 501 | enddef |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 502 | defcompile |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 503 | END |
Bram Moolenaar | 6c4bfe4 | 2020-07-23 18:26:30 +0200 | [diff] [blame] | 504 | CheckScriptFailure(lines, 'E1054:') |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 505 | enddef |
| 506 | |
Bram Moolenaar | 65b9545 | 2020-07-19 14:03:09 +0200 | [diff] [blame] | 507 | def Test_vim9script_call_fail_type() |
| 508 | let lines =<< trim END |
| 509 | vim9script |
| 510 | def MyFunc(arg: string) |
| 511 | echo arg |
| 512 | enddef |
| 513 | MyFunc(1234) |
| 514 | END |
| 515 | CheckScriptFailure(lines, 'E1013: type mismatch, expected string but got number') |
| 516 | enddef |
| 517 | |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 518 | def Test_vim9script_call_fail_const() |
| 519 | let lines =<< trim END |
| 520 | vim9script |
| 521 | const var = '' |
| 522 | def MyFunc(arg: string) |
| 523 | var = 'asdf' |
| 524 | enddef |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 525 | defcompile |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 526 | END |
| 527 | writefile(lines, 'Xcall_const.vim') |
| 528 | assert_fails('source Xcall_const.vim', 'E46:') |
| 529 | delete('Xcall_const.vim') |
| 530 | enddef |
| 531 | |
| 532 | " Test that inside :function a Python function can be defined, :def is not |
| 533 | " recognized. |
| 534 | func Test_function_python() |
| 535 | CheckFeature python3 |
| 536 | let py = 'python3' |
| 537 | execute py "<< EOF" |
| 538 | def do_something(): |
| 539 | return 1 |
| 540 | EOF |
| 541 | endfunc |
| 542 | |
| 543 | def Test_delfunc() |
| 544 | let lines =<< trim END |
| 545 | vim9script |
Bram Moolenaar | 4c17ad9 | 2020-04-27 22:47:51 +0200 | [diff] [blame] | 546 | def g:GoneSoon() |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 547 | echo 'hello' |
| 548 | enddef |
| 549 | |
| 550 | def CallGoneSoon() |
| 551 | GoneSoon() |
| 552 | enddef |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 553 | defcompile |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 554 | |
Bram Moolenaar | 4c17ad9 | 2020-04-27 22:47:51 +0200 | [diff] [blame] | 555 | delfunc g:GoneSoon |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 556 | CallGoneSoon() |
| 557 | END |
| 558 | writefile(lines, 'XToDelFunc') |
| 559 | assert_fails('so XToDelFunc', 'E933') |
| 560 | assert_fails('so XToDelFunc', 'E933') |
| 561 | |
| 562 | delete('XToDelFunc') |
| 563 | enddef |
| 564 | |
| 565 | def Test_redef_failure() |
| 566 | call writefile(['def Func0(): string', 'return "Func0"', 'enddef'], 'Xdef') |
| 567 | so Xdef |
| 568 | call writefile(['def Func1(): string', 'return "Func1"', 'enddef'], 'Xdef') |
| 569 | so Xdef |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 570 | call writefile(['def! Func0(): string', 'enddef', 'defcompile'], 'Xdef') |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 571 | call assert_fails('so Xdef', 'E1027:') |
| 572 | call writefile(['def Func2(): string', 'return "Func2"', 'enddef'], 'Xdef') |
| 573 | so Xdef |
| 574 | call delete('Xdef') |
| 575 | |
Bram Moolenaar | 1df8b3f | 2020-04-23 18:13:23 +0200 | [diff] [blame] | 576 | call assert_equal(0, g:Func0()) |
| 577 | call assert_equal('Func1', g:Func1()) |
| 578 | call assert_equal('Func2', g:Func2()) |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 579 | |
| 580 | delfunc! Func0 |
| 581 | delfunc! Func1 |
| 582 | delfunc! Func2 |
| 583 | enddef |
| 584 | |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 585 | def Test_vim9script_func() |
| 586 | let lines =<< trim END |
| 587 | vim9script |
| 588 | func Func(arg) |
| 589 | echo a:arg |
| 590 | endfunc |
| 591 | Func('text') |
| 592 | END |
| 593 | writefile(lines, 'XVim9Func') |
| 594 | so XVim9Func |
| 595 | |
| 596 | delete('XVim9Func') |
| 597 | enddef |
| 598 | |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 599 | " Test for internal functions returning different types |
| 600 | func Test_InternalFuncRetType() |
| 601 | let lines =<< trim END |
| 602 | def RetFloat(): float |
| 603 | return ceil(1.456) |
| 604 | enddef |
| 605 | |
| 606 | def RetListAny(): list<any> |
| 607 | return items({'k' : 'v'}) |
| 608 | enddef |
| 609 | |
| 610 | def RetListString(): list<string> |
| 611 | return split('a:b:c', ':') |
| 612 | enddef |
| 613 | |
| 614 | def RetListDictAny(): list<dict<any>> |
| 615 | return getbufinfo() |
| 616 | enddef |
| 617 | |
| 618 | def RetDictNumber(): dict<number> |
| 619 | return wordcount() |
| 620 | enddef |
| 621 | |
| 622 | def RetDictString(): dict<string> |
| 623 | return environ() |
| 624 | enddef |
| 625 | END |
| 626 | call writefile(lines, 'Xscript') |
| 627 | source Xscript |
| 628 | |
| 629 | call assert_equal(2.0, RetFloat()) |
| 630 | call assert_equal([['k', 'v']], RetListAny()) |
| 631 | call assert_equal(['a', 'b', 'c'], RetListString()) |
| 632 | call assert_notequal([], RetListDictAny()) |
| 633 | call assert_notequal({}, RetDictNumber()) |
| 634 | call assert_notequal({}, RetDictString()) |
| 635 | call delete('Xscript') |
| 636 | endfunc |
| 637 | |
| 638 | " Test for passing too many or too few arguments to internal functions |
| 639 | func Test_internalfunc_arg_error() |
| 640 | let l =<< trim END |
| 641 | def! FArgErr(): float |
| 642 | return ceil(1.1, 2) |
| 643 | enddef |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 644 | defcompile |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 645 | END |
| 646 | call writefile(l, 'Xinvalidarg') |
| 647 | call assert_fails('so Xinvalidarg', 'E118:') |
| 648 | let l =<< trim END |
| 649 | def! FArgErr(): float |
| 650 | return ceil() |
| 651 | enddef |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 652 | defcompile |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 653 | END |
| 654 | call writefile(l, 'Xinvalidarg') |
| 655 | call assert_fails('so Xinvalidarg', 'E119:') |
| 656 | call delete('Xinvalidarg') |
| 657 | endfunc |
| 658 | |
| 659 | let s:funcResult = 0 |
| 660 | |
| 661 | def FuncNoArgNoRet() |
| 662 | funcResult = 11 |
| 663 | enddef |
| 664 | |
| 665 | def FuncNoArgRetNumber(): number |
| 666 | funcResult = 22 |
| 667 | return 1234 |
| 668 | enddef |
| 669 | |
Bram Moolenaar | ec5929d | 2020-04-07 20:53:39 +0200 | [diff] [blame] | 670 | def FuncNoArgRetString(): string |
| 671 | funcResult = 45 |
| 672 | return 'text' |
| 673 | enddef |
| 674 | |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 675 | def FuncOneArgNoRet(arg: number) |
| 676 | funcResult = arg |
| 677 | enddef |
| 678 | |
| 679 | def FuncOneArgRetNumber(arg: number): number |
| 680 | funcResult = arg |
| 681 | return arg |
| 682 | enddef |
| 683 | |
Bram Moolenaar | 08938ee | 2020-04-11 23:17:17 +0200 | [diff] [blame] | 684 | def FuncTwoArgNoRet(one: bool, two: number) |
| 685 | funcResult = two |
| 686 | enddef |
| 687 | |
Bram Moolenaar | ec5929d | 2020-04-07 20:53:39 +0200 | [diff] [blame] | 688 | def FuncOneArgRetString(arg: string): string |
| 689 | return arg |
| 690 | enddef |
| 691 | |
Bram Moolenaar | 8922860 | 2020-04-05 22:14:54 +0200 | [diff] [blame] | 692 | def FuncOneArgRetAny(arg: any): any |
| 693 | return arg |
| 694 | enddef |
| 695 | |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 696 | def Test_func_type() |
| 697 | let Ref1: func() |
| 698 | funcResult = 0 |
| 699 | Ref1 = FuncNoArgNoRet |
| 700 | Ref1() |
| 701 | assert_equal(11, funcResult) |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 702 | |
| 703 | let Ref2: func |
| 704 | funcResult = 0 |
| 705 | Ref2 = FuncNoArgNoRet |
| 706 | Ref2() |
| 707 | assert_equal(11, funcResult) |
| 708 | |
| 709 | funcResult = 0 |
| 710 | Ref2 = FuncOneArgNoRet |
| 711 | Ref2(12) |
| 712 | assert_equal(12, funcResult) |
| 713 | |
| 714 | funcResult = 0 |
| 715 | Ref2 = FuncNoArgRetNumber |
| 716 | assert_equal(1234, Ref2()) |
| 717 | assert_equal(22, funcResult) |
| 718 | |
| 719 | funcResult = 0 |
| 720 | Ref2 = FuncOneArgRetNumber |
| 721 | assert_equal(13, Ref2(13)) |
| 722 | assert_equal(13, funcResult) |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 723 | enddef |
| 724 | |
Bram Moolenaar | 9978d47 | 2020-07-05 16:01:56 +0200 | [diff] [blame] | 725 | def Test_repeat_return_type() |
| 726 | let res = 0 |
| 727 | for n in repeat([1], 3) |
| 728 | res += n |
| 729 | endfor |
| 730 | assert_equal(3, res) |
Bram Moolenaar | fce82b3 | 2020-07-05 16:07:21 +0200 | [diff] [blame] | 731 | |
| 732 | res = 0 |
| 733 | for n in add([1, 2], 3) |
| 734 | res += n |
| 735 | endfor |
| 736 | assert_equal(6, res) |
Bram Moolenaar | 9978d47 | 2020-07-05 16:01:56 +0200 | [diff] [blame] | 737 | enddef |
| 738 | |
Bram Moolenaar | 846178a | 2020-07-05 17:04:13 +0200 | [diff] [blame] | 739 | def Test_argv_return_type() |
| 740 | next fileone filetwo |
| 741 | let res = '' |
| 742 | for name in argv() |
| 743 | res ..= name |
| 744 | endfor |
| 745 | assert_equal('fileonefiletwo', res) |
| 746 | enddef |
| 747 | |
Bram Moolenaar | ec5929d | 2020-04-07 20:53:39 +0200 | [diff] [blame] | 748 | def Test_func_type_part() |
| 749 | let RefVoid: func: void |
| 750 | RefVoid = FuncNoArgNoRet |
| 751 | RefVoid = FuncOneArgNoRet |
| 752 | 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] | 753 | 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] | 754 | |
| 755 | let RefAny: func(): any |
| 756 | RefAny = FuncNoArgRetNumber |
| 757 | RefAny = FuncNoArgRetString |
| 758 | CheckDefFailure(['let RefAny: func(): any', 'RefAny = FuncNoArgNoRet'], 'E1013: type mismatch, expected func(): any but got func()') |
| 759 | CheckDefFailure(['let RefAny: func(): any', 'RefAny = FuncOneArgNoRet'], 'E1013: type mismatch, expected func(): any but got func(number)') |
| 760 | |
| 761 | let RefNr: func: number |
| 762 | RefNr = FuncNoArgRetNumber |
| 763 | RefNr = FuncOneArgRetNumber |
| 764 | 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] | 765 | 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] | 766 | |
| 767 | let RefStr: func: string |
| 768 | RefStr = FuncNoArgRetString |
| 769 | RefStr = FuncOneArgRetString |
| 770 | CheckDefFailure(['let RefStr: func: string', 'RefStr = FuncNoArgNoRet'], 'E1013: type mismatch, expected func(): string but got func()') |
| 771 | CheckDefFailure(['let RefStr: func: string', 'RefStr = FuncNoArgRetNumber'], 'E1013: type mismatch, expected func(): string but got func(): number') |
| 772 | enddef |
| 773 | |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 774 | def Test_func_type_fails() |
| 775 | CheckDefFailure(['let ref1: func()'], 'E704:') |
| 776 | |
| 777 | CheckDefFailure(['let Ref1: func()', 'Ref1 = FuncNoArgRetNumber'], 'E1013: type mismatch, expected func() but got func(): number') |
| 778 | CheckDefFailure(['let Ref1: func()', 'Ref1 = FuncOneArgNoRet'], 'E1013: type mismatch, expected func() but got func(number)') |
| 779 | 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] | 780 | CheckDefFailure(['let Ref1: func(bool)', 'Ref1 = FuncTwoArgNoRet'], 'E1013: type mismatch, expected func(bool) but got func(bool, number)') |
| 781 | CheckDefFailure(['let Ref1: func(?bool)', 'Ref1 = FuncTwoArgNoRet'], 'E1013: type mismatch, expected func(?bool) but got func(bool, number)') |
| 782 | 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] | 783 | |
| 784 | call CheckDefFailure(['let RefWrong: func(string ,number)'], 'E1068:') |
| 785 | call CheckDefFailure(['let RefWrong: func(string,number)'], 'E1069:') |
| 786 | 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:') |
| 787 | call CheckDefFailure(['let RefWrong: func(bool):string'], 'E1069:') |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 788 | enddef |
| 789 | |
Bram Moolenaar | 8922860 | 2020-04-05 22:14:54 +0200 | [diff] [blame] | 790 | def Test_func_return_type() |
| 791 | let nr: number |
| 792 | nr = FuncNoArgRetNumber() |
| 793 | assert_equal(1234, nr) |
| 794 | |
| 795 | nr = FuncOneArgRetAny(122) |
| 796 | assert_equal(122, nr) |
| 797 | |
| 798 | let str: string |
| 799 | str = FuncOneArgRetAny('yes') |
| 800 | assert_equal('yes', str) |
| 801 | |
| 802 | CheckDefFailure(['let str: string', 'str = FuncNoArgRetNumber()'], 'E1013: type mismatch, expected string but got number') |
| 803 | enddef |
| 804 | |
Bram Moolenaar | 5e774c7 | 2020-04-12 21:53:00 +0200 | [diff] [blame] | 805 | def MultiLine( |
| 806 | arg1: string, |
| 807 | arg2 = 1234, |
| 808 | ...rest: list<string> |
| 809 | ): string |
| 810 | return arg1 .. arg2 .. join(rest, '-') |
| 811 | enddef |
| 812 | |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 813 | def MultiLineComment( |
| 814 | arg1: string, # comment |
| 815 | arg2 = 1234, # comment |
| 816 | ...rest: list<string> # comment |
| 817 | ): string # comment |
| 818 | return arg1 .. arg2 .. join(rest, '-') |
| 819 | enddef |
| 820 | |
Bram Moolenaar | 5e774c7 | 2020-04-12 21:53:00 +0200 | [diff] [blame] | 821 | def Test_multiline() |
| 822 | assert_equal('text1234', MultiLine('text')) |
| 823 | assert_equal('text777', MultiLine('text', 777)) |
| 824 | assert_equal('text777one', MultiLine('text', 777, 'one')) |
| 825 | assert_equal('text777one-two', MultiLine('text', 777, 'one', 'two')) |
| 826 | enddef |
| 827 | |
Bram Moolenaar | 23e0325 | 2020-04-12 22:22:31 +0200 | [diff] [blame] | 828 | func Test_multiline_not_vim9() |
| 829 | call assert_equal('text1234', MultiLine('text')) |
| 830 | call assert_equal('text777', MultiLine('text', 777)) |
| 831 | call assert_equal('text777one', MultiLine('text', 777, 'one')) |
| 832 | call assert_equal('text777one-two', MultiLine('text', 777, 'one', 'two')) |
| 833 | endfunc |
| 834 | |
Bram Moolenaar | 5e774c7 | 2020-04-12 21:53:00 +0200 | [diff] [blame] | 835 | |
Bram Moolenaar | ee4e0c1 | 2020-04-06 21:35:05 +0200 | [diff] [blame] | 836 | " When using CheckScriptFailure() for the below test, E1010 is generated instead |
| 837 | " of E1056. |
| 838 | func Test_E1056_1059() |
| 839 | let caught_1056 = 0 |
| 840 | try |
| 841 | def F(): |
| 842 | return 1 |
| 843 | enddef |
| 844 | catch /E1056:/ |
| 845 | let caught_1056 = 1 |
| 846 | endtry |
| 847 | call assert_equal(1, caught_1056) |
| 848 | |
| 849 | let caught_1059 = 0 |
| 850 | try |
| 851 | def F5(items : list) |
| 852 | echo 'a' |
| 853 | enddef |
| 854 | catch /E1059:/ |
| 855 | let caught_1059 = 1 |
| 856 | endtry |
| 857 | call assert_equal(1, caught_1059) |
| 858 | endfunc |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 859 | |
Bram Moolenaar | 015f426 | 2020-05-05 21:25:22 +0200 | [diff] [blame] | 860 | func DelMe() |
| 861 | echo 'DelMe' |
| 862 | endfunc |
| 863 | |
| 864 | def Test_deleted_function() |
| 865 | CheckDefExecFailure([ |
| 866 | 'let RefMe: func = function("g:DelMe")', |
| 867 | 'delfunc g:DelMe', |
| 868 | 'echo RefMe()'], 'E117:') |
| 869 | enddef |
| 870 | |
| 871 | def Test_unknown_function() |
| 872 | CheckDefExecFailure([ |
| 873 | 'let Ref: func = function("NotExist")', |
Bram Moolenaar | 9b7bf9e | 2020-07-11 22:14:59 +0200 | [diff] [blame] | 874 | 'delfunc g:NotExist'], 'E700:') |
Bram Moolenaar | 015f426 | 2020-05-05 21:25:22 +0200 | [diff] [blame] | 875 | enddef |
| 876 | |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 877 | def RefFunc(Ref: func(string): string): string |
| 878 | return Ref('more') |
| 879 | enddef |
| 880 | |
| 881 | def Test_closure_simple() |
| 882 | let local = 'some ' |
| 883 | assert_equal('some more', RefFunc({s -> local .. s})) |
| 884 | enddef |
| 885 | |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 886 | def MakeRef() |
| 887 | let local = 'some ' |
| 888 | g:Ref = {s -> local .. s} |
| 889 | enddef |
| 890 | |
| 891 | def Test_closure_ref_after_return() |
| 892 | MakeRef() |
| 893 | assert_equal('some thing', g:Ref('thing')) |
| 894 | unlet g:Ref |
| 895 | enddef |
| 896 | |
Bram Moolenaar | 5adc55c | 2020-05-02 23:12:58 +0200 | [diff] [blame] | 897 | def MakeTwoRefs() |
| 898 | let local = ['some'] |
| 899 | g:Extend = {s -> local->add(s)} |
| 900 | g:Read = {-> local} |
| 901 | enddef |
| 902 | |
| 903 | def Test_closure_two_refs() |
| 904 | MakeTwoRefs() |
| 905 | assert_equal('some', join(g:Read(), ' ')) |
| 906 | g:Extend('more') |
| 907 | assert_equal('some more', join(g:Read(), ' ')) |
| 908 | g:Extend('even') |
| 909 | assert_equal('some more even', join(g:Read(), ' ')) |
| 910 | |
| 911 | unlet g:Extend |
| 912 | unlet g:Read |
| 913 | enddef |
| 914 | |
Bram Moolenaar | 5adc55c | 2020-05-02 23:12:58 +0200 | [diff] [blame] | 915 | def ReadRef(Ref: func(): list<string>): string |
| 916 | return join(Ref(), ' ') |
| 917 | enddef |
| 918 | |
| 919 | def ExtendRef(Ref: func(string), add: string) |
| 920 | Ref(add) |
| 921 | enddef |
| 922 | |
| 923 | def Test_closure_two_indirect_refs() |
Bram Moolenaar | f7779c6 | 2020-05-03 15:38:16 +0200 | [diff] [blame] | 924 | MakeTwoRefs() |
Bram Moolenaar | 5adc55c | 2020-05-02 23:12:58 +0200 | [diff] [blame] | 925 | assert_equal('some', ReadRef(g:Read)) |
| 926 | ExtendRef(g:Extend, 'more') |
| 927 | assert_equal('some more', ReadRef(g:Read)) |
| 928 | ExtendRef(g:Extend, 'even') |
| 929 | assert_equal('some more even', ReadRef(g:Read)) |
| 930 | |
| 931 | unlet g:Extend |
| 932 | unlet g:Read |
| 933 | enddef |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 934 | |
Bram Moolenaar | 2fd4cd7 | 2020-05-03 22:30:49 +0200 | [diff] [blame] | 935 | def MakeArgRefs(theArg: string) |
| 936 | let local = 'loc_val' |
| 937 | g:UseArg = {s -> theArg .. '/' .. local .. '/' .. s} |
| 938 | enddef |
| 939 | |
| 940 | def MakeArgRefsVarargs(theArg: string, ...rest: list<string>) |
| 941 | let local = 'the_loc' |
| 942 | g:UseVararg = {s -> theArg .. '/' .. local .. '/' .. s .. '/' .. join(rest)} |
| 943 | enddef |
| 944 | |
| 945 | def Test_closure_using_argument() |
| 946 | MakeArgRefs('arg_val') |
| 947 | assert_equal('arg_val/loc_val/call_val', g:UseArg('call_val')) |
| 948 | |
| 949 | MakeArgRefsVarargs('arg_val', 'one', 'two') |
| 950 | assert_equal('arg_val/the_loc/call_val/one two', g:UseVararg('call_val')) |
| 951 | |
| 952 | unlet g:UseArg |
| 953 | unlet g:UseVararg |
| 954 | enddef |
| 955 | |
Bram Moolenaar | b68b346 | 2020-05-06 21:06:30 +0200 | [diff] [blame] | 956 | def MakeGetAndAppendRefs() |
| 957 | let local = 'a' |
| 958 | |
| 959 | def Append(arg: string) |
| 960 | local ..= arg |
| 961 | enddef |
| 962 | g:Append = Append |
| 963 | |
| 964 | def Get(): string |
| 965 | return local |
| 966 | enddef |
| 967 | g:Get = Get |
| 968 | enddef |
| 969 | |
| 970 | def Test_closure_append_get() |
| 971 | MakeGetAndAppendRefs() |
| 972 | assert_equal('a', g:Get()) |
| 973 | g:Append('-b') |
| 974 | assert_equal('a-b', g:Get()) |
| 975 | g:Append('-c') |
| 976 | assert_equal('a-b-c', g:Get()) |
| 977 | |
| 978 | unlet g:Append |
| 979 | unlet g:Get |
| 980 | enddef |
| 981 | |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 982 | def Test_nested_closure() |
| 983 | let local = 'text' |
| 984 | def Closure(arg: string): string |
| 985 | return local .. arg |
| 986 | enddef |
| 987 | assert_equal('text!!!', Closure('!!!')) |
| 988 | enddef |
| 989 | |
Bram Moolenaar | 6f5b6df | 2020-05-16 21:20:12 +0200 | [diff] [blame] | 990 | func GetResult(Ref) |
| 991 | return a:Ref('some') |
| 992 | endfunc |
| 993 | |
| 994 | def Test_call_closure_not_compiled() |
| 995 | let text = 'text' |
| 996 | g:Ref = {s -> s .. text} |
| 997 | assert_equal('sometext', GetResult(g:Ref)) |
| 998 | enddef |
| 999 | |
Bram Moolenaar | 865af6b | 2020-06-18 18:45:49 +0200 | [diff] [blame] | 1000 | def Test_sort_return_type() |
| 1001 | let res: list<number> |
| 1002 | res = [1, 2, 3]->sort() |
| 1003 | enddef |
| 1004 | |
Bram Moolenaar | f151ad1 | 2020-06-30 13:38:01 +0200 | [diff] [blame] | 1005 | def Test_getqflist_return_type() |
| 1006 | let l = getqflist() |
| 1007 | assert_equal([], l) |
| 1008 | |
| 1009 | let d = getqflist(#{items: 0}) |
| 1010 | assert_equal(#{items: []}, d) |
| 1011 | enddef |
| 1012 | |
| 1013 | def Test_getloclist_return_type() |
| 1014 | let l = getloclist(1) |
| 1015 | assert_equal([], l) |
| 1016 | |
| 1017 | let d = getloclist(1, #{items: 0}) |
| 1018 | assert_equal(#{items: []}, d) |
| 1019 | enddef |
| 1020 | |
Bram Moolenaar | a66ba01 | 2020-07-05 18:41:08 +0200 | [diff] [blame] | 1021 | def Test_copy_return_type() |
| 1022 | let l = copy([1, 2, 3]) |
| 1023 | let res = 0 |
| 1024 | for n in l |
| 1025 | res += n |
| 1026 | endfor |
| 1027 | assert_equal(6, res) |
| 1028 | |
| 1029 | let dl = deepcopy([1, 2, 3]) |
| 1030 | res = 0 |
| 1031 | for n in dl |
| 1032 | res += n |
| 1033 | endfor |
| 1034 | assert_equal(6, res) |
| 1035 | enddef |
| 1036 | |
Bram Moolenaar | b3c019c | 2020-07-05 20:08:39 +0200 | [diff] [blame] | 1037 | def Test_extend_return_type() |
| 1038 | let l = extend([1, 2], [3]) |
| 1039 | let res = 0 |
| 1040 | for n in l |
| 1041 | res += n |
| 1042 | endfor |
| 1043 | assert_equal(6, res) |
| 1044 | enddef |
| 1045 | |
Bram Moolenaar | 252e88a | 2020-07-05 20:47:18 +0200 | [diff] [blame] | 1046 | def Test_insert_return_type() |
| 1047 | let l = insert([2, 1], 3) |
| 1048 | let res = 0 |
| 1049 | for n in l |
| 1050 | res += n |
| 1051 | endfor |
| 1052 | assert_equal(6, res) |
| 1053 | enddef |
| 1054 | |
Bram Moolenaar | 6762735 | 2020-07-05 21:10:24 +0200 | [diff] [blame] | 1055 | def Test_reverse_return_type() |
| 1056 | let l = reverse([1, 2, 3]) |
| 1057 | let res = 0 |
| 1058 | for n in l |
| 1059 | res += n |
| 1060 | endfor |
| 1061 | assert_equal(6, res) |
| 1062 | enddef |
| 1063 | |
Bram Moolenaar | ad7c249 | 2020-07-05 20:55:29 +0200 | [diff] [blame] | 1064 | def Test_remove_return_type() |
| 1065 | let l = remove(#{one: [1, 2], two: [3, 4]}, 'one') |
| 1066 | let res = 0 |
| 1067 | for n in l |
| 1068 | res += n |
| 1069 | endfor |
| 1070 | assert_equal(3, res) |
| 1071 | enddef |
| 1072 | |
Bram Moolenaar | 0d94ad6 | 2020-07-05 20:16:41 +0200 | [diff] [blame] | 1073 | def Test_filter_return_type() |
| 1074 | let l = filter([1, 2, 3], {-> 1}) |
| 1075 | let res = 0 |
| 1076 | for n in l |
| 1077 | res += n |
| 1078 | endfor |
| 1079 | assert_equal(6, res) |
| 1080 | enddef |
| 1081 | |
Bram Moolenaar | f1a2368 | 2020-07-13 18:55:48 +0200 | [diff] [blame] | 1082 | def Wrong_dict_key_type(items: list<number>): list<number> |
| 1083 | return filter(items, {_, val -> get({val: 1}, 'x')}) |
| 1084 | enddef |
| 1085 | |
| 1086 | def Test_wrong_dict_key_type() |
| 1087 | assert_fails('Wrong_dict_key_type([1, 2, 3])', 'E1029:') |
| 1088 | enddef |
| 1089 | |
Bram Moolenaar | acd4c5e | 2020-06-22 19:39:03 +0200 | [diff] [blame] | 1090 | def Line_continuation_in_def(dir: string = ''): string |
| 1091 | let path: string = empty(dir) |
| 1092 | \ ? 'empty' |
| 1093 | \ : 'full' |
| 1094 | return path |
| 1095 | enddef |
| 1096 | |
| 1097 | def Test_line_continuation_in_def() |
| 1098 | assert_equal('full', Line_continuation_in_def('.')) |
| 1099 | enddef |
| 1100 | |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 1101 | def Line_continuation_in_lambda(): list<number> |
| 1102 | let x = range(97, 100) |
Bram Moolenaar | 914e7ea | 2020-07-11 15:20:48 +0200 | [diff] [blame] | 1103 | ->map({_, v -> nr2char(v) |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 1104 | ->toupper()}) |
| 1105 | ->reverse() |
| 1106 | return x |
| 1107 | enddef |
| 1108 | |
| 1109 | def Test_line_continuation_in_lambda() |
| 1110 | assert_equal(['D', 'C', 'B', 'A'], Line_continuation_in_lambda()) |
| 1111 | enddef |
| 1112 | |
Bram Moolenaar | 8f510af | 2020-07-05 18:48:23 +0200 | [diff] [blame] | 1113 | func Test_silent_echo() |
Bram Moolenaar | 47e7d70 | 2020-07-05 18:18:42 +0200 | [diff] [blame] | 1114 | CheckScreendump |
| 1115 | |
| 1116 | let lines =<< trim END |
| 1117 | vim9script |
| 1118 | def EchoNothing() |
| 1119 | silent echo '' |
| 1120 | enddef |
| 1121 | defcompile |
| 1122 | END |
Bram Moolenaar | 8f510af | 2020-07-05 18:48:23 +0200 | [diff] [blame] | 1123 | call writefile(lines, 'XTest_silent_echo') |
Bram Moolenaar | 47e7d70 | 2020-07-05 18:18:42 +0200 | [diff] [blame] | 1124 | |
| 1125 | " Check that the balloon shows up after a mouse move |
| 1126 | let buf = RunVimInTerminal('-S XTest_silent_echo', {'rows': 6}) |
Bram Moolenaar | 8f510af | 2020-07-05 18:48:23 +0200 | [diff] [blame] | 1127 | call term_sendkeys(buf, ":abc") |
Bram Moolenaar | 47e7d70 | 2020-07-05 18:18:42 +0200 | [diff] [blame] | 1128 | call VerifyScreenDump(buf, 'Test_vim9_silent_echo', {}) |
| 1129 | |
| 1130 | " clean up |
| 1131 | call StopVimInTerminal(buf) |
| 1132 | call delete('XTest_silent_echo') |
Bram Moolenaar | 8f510af | 2020-07-05 18:48:23 +0200 | [diff] [blame] | 1133 | endfunc |
Bram Moolenaar | 47e7d70 | 2020-07-05 18:18:42 +0200 | [diff] [blame] | 1134 | |
Bram Moolenaar | 985116a | 2020-07-12 17:31:09 +0200 | [diff] [blame] | 1135 | def Fibonacci(n: number): number |
| 1136 | if n < 2 |
| 1137 | return n |
| 1138 | else |
| 1139 | return Fibonacci(n - 1) + Fibonacci(n - 2) |
| 1140 | endif |
| 1141 | enddef |
| 1142 | |
| 1143 | def Test_recursive_call() |
| 1144 | assert_equal(6765, Fibonacci(20)) |
| 1145 | enddef |
| 1146 | |
Bram Moolenaar | 08f7a41 | 2020-07-13 20:41:08 +0200 | [diff] [blame] | 1147 | def TreeWalk(dir: string): list<any> |
| 1148 | return readdir(dir)->map({_, val -> |
| 1149 | fnamemodify(dir .. '/' .. val, ':p')->isdirectory() |
| 1150 | ? {val : TreeWalk(dir .. '/' .. val)} |
| 1151 | : val |
| 1152 | }) |
| 1153 | enddef |
| 1154 | |
| 1155 | def Test_closure_in_map() |
| 1156 | mkdir('XclosureDir/tdir', 'p') |
| 1157 | writefile(['111'], 'XclosureDir/file1') |
| 1158 | writefile(['222'], 'XclosureDir/file2') |
| 1159 | writefile(['333'], 'XclosureDir/tdir/file3') |
| 1160 | |
| 1161 | assert_equal(['file1', 'file2', {'tdir': ['file3']}], TreeWalk('XclosureDir')) |
| 1162 | |
| 1163 | delete('XclosureDir', 'rf') |
| 1164 | enddef |
| 1165 | |
Bram Moolenaar | a90afb9 | 2020-07-15 22:38:56 +0200 | [diff] [blame] | 1166 | def Test_partial_call() |
| 1167 | let Xsetlist = function('setloclist', [0]) |
| 1168 | Xsetlist([], ' ', {'title': 'test'}) |
| 1169 | assert_equal({'title': 'test'}, getloclist(0, {'title': 1})) |
| 1170 | |
| 1171 | Xsetlist = function('setloclist', [0, [], ' ']) |
| 1172 | Xsetlist({'title': 'test'}) |
| 1173 | assert_equal({'title': 'test'}, getloclist(0, {'title': 1})) |
| 1174 | |
| 1175 | Xsetlist = function('setqflist') |
| 1176 | Xsetlist([], ' ', {'title': 'test'}) |
| 1177 | assert_equal({'title': 'test'}, getqflist({'title': 1})) |
| 1178 | |
| 1179 | Xsetlist = function('setqflist', [[], ' ']) |
| 1180 | Xsetlist({'title': 'test'}) |
| 1181 | assert_equal({'title': 'test'}, getqflist({'title': 1})) |
| 1182 | enddef |
| 1183 | |
Bram Moolenaar | f7779c6 | 2020-05-03 15:38:16 +0200 | [diff] [blame] | 1184 | |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 1185 | " vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker |