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