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