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