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 |
Bram Moolenaar | ad30470 | 2020-09-06 18:22:53 +0200 | [diff] [blame] | 4 | source term_util.vim |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 5 | source view_util.vim |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 6 | source vim9.vim |
Bram Moolenaar | 47e7d70 | 2020-07-05 18:18:42 +0200 | [diff] [blame] | 7 | source screendump.vim |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 8 | |
| 9 | func Test_def_basic() |
| 10 | def SomeFunc(): string |
| 11 | return 'yes' |
| 12 | enddef |
| 13 | call assert_equal('yes', SomeFunc()) |
| 14 | endfunc |
| 15 | |
| 16 | def ReturnString(): string |
| 17 | return 'string' |
| 18 | enddef |
| 19 | |
| 20 | def ReturnNumber(): number |
| 21 | return 123 |
| 22 | enddef |
| 23 | |
| 24 | let g:notNumber = 'string' |
| 25 | |
| 26 | def ReturnGlobal(): number |
| 27 | return g:notNumber |
| 28 | enddef |
| 29 | |
| 30 | def Test_return_something() |
| 31 | assert_equal('string', ReturnString()) |
| 32 | assert_equal(123, ReturnNumber()) |
Bram Moolenaar | 9bd5d87 | 2020-09-06 21:47:48 +0200 | [diff] [blame] | 33 | assert_fails('ReturnGlobal()', 'E1029: Expected number but got string', '', 1, 'ReturnGlobal') |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 34 | enddef |
| 35 | |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 36 | def Test_missing_return() |
| 37 | CheckDefFailure(['def Missing(): number', |
| 38 | ' if g:cond', |
| 39 | ' echo "no return"', |
| 40 | ' else', |
| 41 | ' return 0', |
| 42 | ' endif' |
| 43 | 'enddef'], 'E1027:') |
| 44 | CheckDefFailure(['def Missing(): number', |
| 45 | ' if g:cond', |
| 46 | ' return 1', |
| 47 | ' else', |
| 48 | ' echo "no return"', |
| 49 | ' endif' |
| 50 | 'enddef'], 'E1027:') |
| 51 | CheckDefFailure(['def Missing(): number', |
| 52 | ' if g:cond', |
| 53 | ' return 1', |
| 54 | ' else', |
| 55 | ' return 2', |
| 56 | ' endif' |
| 57 | ' return 3' |
| 58 | 'enddef'], 'E1095:') |
| 59 | enddef |
| 60 | |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 61 | let s:nothing = 0 |
| 62 | def ReturnNothing() |
| 63 | s:nothing = 1 |
| 64 | if true |
| 65 | return |
| 66 | endif |
| 67 | s:nothing = 2 |
| 68 | enddef |
| 69 | |
| 70 | def Test_return_nothing() |
| 71 | ReturnNothing() |
| 72 | assert_equal(1, s:nothing) |
| 73 | enddef |
| 74 | |
| 75 | func Increment() |
| 76 | let g:counter += 1 |
| 77 | endfunc |
| 78 | |
| 79 | def Test_call_ufunc_count() |
| 80 | g:counter = 1 |
| 81 | Increment() |
| 82 | Increment() |
| 83 | Increment() |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 84 | # works with and without :call |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 85 | assert_equal(4, g:counter) |
| 86 | call assert_equal(4, g:counter) |
| 87 | unlet g:counter |
| 88 | enddef |
| 89 | |
| 90 | def MyVarargs(arg: string, ...rest: list<string>): string |
| 91 | let res = arg |
| 92 | for s in rest |
| 93 | res ..= ',' .. s |
| 94 | endfor |
| 95 | return res |
| 96 | enddef |
| 97 | |
| 98 | def Test_call_varargs() |
| 99 | assert_equal('one', MyVarargs('one')) |
| 100 | assert_equal('one,two', MyVarargs('one', 'two')) |
| 101 | assert_equal('one,two,three', MyVarargs('one', 'two', 'three')) |
| 102 | enddef |
| 103 | |
| 104 | def MyDefaultArgs(name = 'string'): string |
| 105 | return name |
| 106 | enddef |
| 107 | |
Bram Moolenaar | e30f64b | 2020-07-15 19:48:20 +0200 | [diff] [blame] | 108 | def MyDefaultSecond(name: string, second: bool = true): string |
| 109 | return second ? name : 'none' |
| 110 | enddef |
| 111 | |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 112 | def Test_call_default_args() |
| 113 | assert_equal('string', MyDefaultArgs()) |
| 114 | assert_equal('one', MyDefaultArgs('one')) |
Bram Moolenaar | 9bd5d87 | 2020-09-06 21:47:48 +0200 | [diff] [blame] | 115 | assert_fails('MyDefaultArgs("one", "two")', 'E118:', '', 3, 'Test_call_default_args') |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 116 | |
Bram Moolenaar | e30f64b | 2020-07-15 19:48:20 +0200 | [diff] [blame] | 117 | assert_equal('test', MyDefaultSecond('test')) |
| 118 | assert_equal('test', MyDefaultSecond('test', true)) |
| 119 | assert_equal('none', MyDefaultSecond('test', false)) |
| 120 | |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 121 | CheckScriptFailure(['def Func(arg: number = asdf)', 'enddef', 'defcompile'], 'E1001:') |
| 122 | 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] | 123 | enddef |
| 124 | |
| 125 | def Test_nested_function() |
| 126 | def Nested(arg: string): string |
| 127 | return 'nested ' .. arg |
| 128 | enddef |
| 129 | assert_equal('nested function', Nested('function')) |
| 130 | |
Bram Moolenaar | 0e65d3d | 2020-05-05 17:53:16 +0200 | [diff] [blame] | 131 | CheckDefFailure(['def Nested()', 'enddef', 'Nested(66)'], 'E118:') |
| 132 | CheckDefFailure(['def Nested(arg: string)', 'enddef', 'Nested()'], 'E119:') |
| 133 | |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 134 | CheckDefFailure(['func Nested()', 'endfunc'], 'E1086:') |
Bram Moolenaar | bcbf413 | 2020-08-01 22:35:13 +0200 | [diff] [blame] | 135 | CheckDefFailure(['def s:Nested()', 'enddef'], 'E1075:') |
| 136 | CheckDefFailure(['def b:Nested()', 'enddef'], 'E1075:') |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 137 | enddef |
| 138 | |
Bram Moolenaar | af8edbb | 2020-08-01 00:03:09 +0200 | [diff] [blame] | 139 | func Test_call_default_args_from_func() |
| 140 | call assert_equal('string', MyDefaultArgs()) |
| 141 | call assert_equal('one', MyDefaultArgs('one')) |
Bram Moolenaar | 9bd5d87 | 2020-09-06 21:47:48 +0200 | [diff] [blame] | 142 | call assert_fails('call MyDefaultArgs("one", "two")', 'E118:', '', 3, 'Test_call_default_args_from_func') |
Bram Moolenaar | af8edbb | 2020-08-01 00:03:09 +0200 | [diff] [blame] | 143 | endfunc |
| 144 | |
Bram Moolenaar | 38ddf33 | 2020-07-31 22:05:04 +0200 | [diff] [blame] | 145 | def Test_nested_global_function() |
| 146 | let lines =<< trim END |
| 147 | vim9script |
| 148 | def Outer() |
| 149 | def g:Inner(): string |
| 150 | return 'inner' |
| 151 | enddef |
| 152 | enddef |
Bram Moolenaar | af8edbb | 2020-08-01 00:03:09 +0200 | [diff] [blame] | 153 | defcompile |
| 154 | Outer() |
| 155 | assert_equal('inner', g:Inner()) |
| 156 | delfunc g:Inner |
| 157 | Outer() |
| 158 | assert_equal('inner', g:Inner()) |
| 159 | delfunc g:Inner |
| 160 | Outer() |
| 161 | assert_equal('inner', g:Inner()) |
| 162 | delfunc g:Inner |
Bram Moolenaar | 38ddf33 | 2020-07-31 22:05:04 +0200 | [diff] [blame] | 163 | END |
| 164 | CheckScriptSuccess(lines) |
Bram Moolenaar | 2c79e9d | 2020-08-01 18:57:52 +0200 | [diff] [blame] | 165 | |
| 166 | lines =<< trim END |
| 167 | vim9script |
| 168 | def Outer() |
| 169 | def g:Inner(): string |
| 170 | return 'inner' |
| 171 | enddef |
| 172 | enddef |
| 173 | defcompile |
| 174 | Outer() |
| 175 | Outer() |
| 176 | END |
| 177 | CheckScriptFailure(lines, "E122:") |
Bram Moolenaar | ad486a0 | 2020-08-01 23:22:18 +0200 | [diff] [blame] | 178 | |
| 179 | lines =<< trim END |
| 180 | vim9script |
| 181 | def Func() |
| 182 | echo 'script' |
| 183 | enddef |
| 184 | def Outer() |
| 185 | def Func() |
| 186 | echo 'inner' |
| 187 | enddef |
| 188 | enddef |
| 189 | defcompile |
| 190 | END |
| 191 | CheckScriptFailure(lines, "E1073:") |
Bram Moolenaar | 38ddf33 | 2020-07-31 22:05:04 +0200 | [diff] [blame] | 192 | enddef |
| 193 | |
Bram Moolenaar | 333894b | 2020-08-01 18:53:07 +0200 | [diff] [blame] | 194 | def Test_global_local_function() |
| 195 | let lines =<< trim END |
| 196 | vim9script |
| 197 | def g:Func(): string |
| 198 | return 'global' |
| 199 | enddef |
| 200 | def Func(): string |
| 201 | return 'local' |
| 202 | enddef |
| 203 | assert_equal('global', g:Func()) |
| 204 | assert_equal('local', Func()) |
| 205 | END |
| 206 | CheckScriptSuccess(lines) |
Bram Moolenaar | 035d6e9 | 2020-08-11 22:30:42 +0200 | [diff] [blame] | 207 | |
| 208 | lines =<< trim END |
| 209 | vim9script |
| 210 | def g:Funcy() |
| 211 | echo 'funcy' |
| 212 | enddef |
| 213 | s:Funcy() |
| 214 | END |
| 215 | CheckScriptFailure(lines, 'E117:') |
Bram Moolenaar | 333894b | 2020-08-01 18:53:07 +0200 | [diff] [blame] | 216 | enddef |
| 217 | |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 218 | func TakesOneArg(arg) |
| 219 | echo a:arg |
| 220 | endfunc |
| 221 | |
| 222 | def Test_call_wrong_args() |
Bram Moolenaar | d2c6170 | 2020-09-06 15:58:36 +0200 | [diff] [blame] | 223 | CheckDefFailure(['TakesOneArg()'], 'E119:') |
| 224 | CheckDefFailure(['TakesOneArg(11, 22)'], 'E118:') |
| 225 | CheckDefFailure(['bufnr(xxx)'], 'E1001:') |
| 226 | CheckScriptFailure(['def Func(Ref: func(s: string))'], 'E475:') |
Bram Moolenaar | ee8580e | 2020-08-28 17:19:07 +0200 | [diff] [blame] | 227 | |
| 228 | let lines =<< trim END |
| 229 | vim9script |
| 230 | def Func(s: string) |
| 231 | echo s |
| 232 | enddef |
| 233 | Func([]) |
| 234 | END |
Bram Moolenaar | 8b565c2 | 2020-08-30 23:24:20 +0200 | [diff] [blame] | 235 | call CheckScriptFailure(lines, 'E1013: argument 1: type mismatch, expected string but got list<unknown>', 5) |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 236 | enddef |
| 237 | |
| 238 | " Default arg and varargs |
| 239 | def MyDefVarargs(one: string, two = 'foo', ...rest: list<string>): string |
| 240 | let res = one .. ',' .. two |
| 241 | for s in rest |
| 242 | res ..= ',' .. s |
| 243 | endfor |
| 244 | return res |
| 245 | enddef |
| 246 | |
| 247 | def Test_call_def_varargs() |
Bram Moolenaar | 9bd5d87 | 2020-09-06 21:47:48 +0200 | [diff] [blame] | 248 | assert_fails('MyDefVarargs()', 'E119:', '', 1, 'Test_call_def_varargs') |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 249 | assert_equal('one,foo', MyDefVarargs('one')) |
| 250 | assert_equal('one,two', MyDefVarargs('one', 'two')) |
| 251 | assert_equal('one,two,three', MyDefVarargs('one', 'two', 'three')) |
Bram Moolenaar | 24aa48b | 2020-07-25 16:33:02 +0200 | [diff] [blame] | 252 | CheckDefFailure(['MyDefVarargs("one", 22)'], |
| 253 | 'E1013: argument 2: type mismatch, expected string but got number') |
| 254 | CheckDefFailure(['MyDefVarargs("one", "two", 123)'], |
| 255 | 'E1013: argument 3: type mismatch, expected string but got number') |
| 256 | |
| 257 | let lines =<< trim END |
| 258 | vim9script |
| 259 | def Func(...l: list<string>) |
| 260 | echo l |
| 261 | enddef |
| 262 | Func('a', 'b', 'c') |
| 263 | END |
| 264 | CheckScriptSuccess(lines) |
| 265 | |
| 266 | lines =<< trim END |
| 267 | vim9script |
| 268 | def Func(...l: list<string>) |
| 269 | echo l |
| 270 | enddef |
| 271 | Func() |
| 272 | END |
| 273 | CheckScriptSuccess(lines) |
| 274 | |
| 275 | lines =<< trim END |
| 276 | vim9script |
| 277 | def Func(...l: list<string>) |
| 278 | echo l |
| 279 | enddef |
| 280 | Func(1, 2, 3) |
| 281 | END |
Bram Moolenaar | 8b565c2 | 2020-08-30 23:24:20 +0200 | [diff] [blame] | 282 | CheckScriptFailure(lines, 'E1013: argument 1: type mismatch') |
Bram Moolenaar | 24aa48b | 2020-07-25 16:33:02 +0200 | [diff] [blame] | 283 | |
| 284 | lines =<< trim END |
| 285 | vim9script |
| 286 | def Func(...l: list<string>) |
| 287 | echo l |
| 288 | enddef |
| 289 | Func('a', 9) |
| 290 | END |
Bram Moolenaar | 8b565c2 | 2020-08-30 23:24:20 +0200 | [diff] [blame] | 291 | CheckScriptFailure(lines, 'E1013: argument 2: type mismatch') |
Bram Moolenaar | 24aa48b | 2020-07-25 16:33:02 +0200 | [diff] [blame] | 292 | |
| 293 | lines =<< trim END |
| 294 | vim9script |
| 295 | def Func(...l: list<string>) |
| 296 | echo l |
| 297 | enddef |
| 298 | Func(1, 'a') |
| 299 | END |
Bram Moolenaar | 8b565c2 | 2020-08-30 23:24:20 +0200 | [diff] [blame] | 300 | CheckScriptFailure(lines, 'E1013: argument 1: type mismatch') |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 301 | enddef |
| 302 | |
Bram Moolenaar | 575f24b | 2020-08-12 14:21:11 +0200 | [diff] [blame] | 303 | def Test_call_call() |
| 304 | let l = [3, 2, 1] |
| 305 | call('reverse', [l]) |
| 306 | assert_equal([1, 2, 3], l) |
| 307 | enddef |
| 308 | |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 309 | let s:value = '' |
| 310 | |
| 311 | def FuncOneDefArg(opt = 'text') |
| 312 | s:value = opt |
| 313 | enddef |
| 314 | |
| 315 | def FuncTwoDefArg(nr = 123, opt = 'text'): string |
| 316 | return nr .. opt |
| 317 | enddef |
| 318 | |
| 319 | def FuncVarargs(...arg: list<string>): string |
| 320 | return join(arg, ',') |
| 321 | enddef |
| 322 | |
| 323 | def Test_func_type_varargs() |
| 324 | let RefDefArg: func(?string) |
| 325 | RefDefArg = FuncOneDefArg |
| 326 | RefDefArg() |
| 327 | assert_equal('text', s:value) |
| 328 | RefDefArg('some') |
| 329 | assert_equal('some', s:value) |
| 330 | |
| 331 | let RefDef2Arg: func(?number, ?string): string |
| 332 | RefDef2Arg = FuncTwoDefArg |
| 333 | assert_equal('123text', RefDef2Arg()) |
| 334 | assert_equal('99text', RefDef2Arg(99)) |
| 335 | assert_equal('77some', RefDef2Arg(77, 'some')) |
| 336 | |
Bram Moolenaar | d2c6170 | 2020-09-06 15:58:36 +0200 | [diff] [blame] | 337 | CheckDefFailure(['let RefWrong: func(string?)'], 'E1010:') |
| 338 | CheckDefFailure(['let RefWrong: func(?string, string)'], 'E1007:') |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 339 | |
| 340 | let RefVarargs: func(...list<string>): string |
| 341 | RefVarargs = FuncVarargs |
| 342 | assert_equal('', RefVarargs()) |
| 343 | assert_equal('one', RefVarargs('one')) |
| 344 | assert_equal('one,two', RefVarargs('one', 'two')) |
| 345 | |
Bram Moolenaar | d2c6170 | 2020-09-06 15:58:36 +0200 | [diff] [blame] | 346 | CheckDefFailure(['let RefWrong: func(...list<string>, string)'], 'E110:') |
| 347 | CheckDefFailure(['let RefWrong: func(...list<string>, ?string)'], 'E110:') |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 348 | enddef |
| 349 | |
Bram Moolenaar | 0b76b42 | 2020-04-07 22:05:08 +0200 | [diff] [blame] | 350 | " Only varargs |
| 351 | def MyVarargsOnly(...args: list<string>): string |
| 352 | return join(args, ',') |
| 353 | enddef |
| 354 | |
| 355 | def Test_call_varargs_only() |
| 356 | assert_equal('', MyVarargsOnly()) |
| 357 | assert_equal('one', MyVarargsOnly('one')) |
| 358 | assert_equal('one,two', MyVarargsOnly('one', 'two')) |
Bram Moolenaar | d2c6170 | 2020-09-06 15:58:36 +0200 | [diff] [blame] | 359 | CheckDefFailure(['MyVarargsOnly(1)'], 'E1013: argument 1: type mismatch, expected string but got number') |
| 360 | CheckDefFailure(['MyVarargsOnly("one", 2)'], 'E1013: argument 2: type mismatch, expected string but got number') |
Bram Moolenaar | 0b76b42 | 2020-04-07 22:05:08 +0200 | [diff] [blame] | 361 | enddef |
| 362 | |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 363 | def Test_using_var_as_arg() |
Bram Moolenaar | d2c6170 | 2020-09-06 15:58:36 +0200 | [diff] [blame] | 364 | writefile(['def Func(x: number)', 'let x = 234', 'enddef', 'defcompile'], 'Xdef') |
Bram Moolenaar | 9bd5d87 | 2020-09-06 21:47:48 +0200 | [diff] [blame] | 365 | assert_fails('so Xdef', 'E1006:', '', 1, 'Func') |
Bram Moolenaar | d2c6170 | 2020-09-06 15:58:36 +0200 | [diff] [blame] | 366 | delete('Xdef') |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 367 | enddef |
| 368 | |
Bram Moolenaar | cb2bdb1 | 2020-05-10 22:53:56 +0200 | [diff] [blame] | 369 | def DictArg(arg: dict<string>) |
| 370 | arg['key'] = 'value' |
| 371 | enddef |
| 372 | |
| 373 | def ListArg(arg: list<string>) |
| 374 | arg[0] = 'value' |
| 375 | enddef |
| 376 | |
| 377 | def Test_assign_to_argument() |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 378 | # works for dict and list |
Bram Moolenaar | cb2bdb1 | 2020-05-10 22:53:56 +0200 | [diff] [blame] | 379 | let d: dict<string> = {} |
| 380 | DictArg(d) |
| 381 | assert_equal('value', d['key']) |
| 382 | let l: list<string> = [] |
| 383 | ListArg(l) |
| 384 | assert_equal('value', l[0]) |
| 385 | |
Bram Moolenaar | d2c6170 | 2020-09-06 15:58:36 +0200 | [diff] [blame] | 386 | CheckScriptFailure(['def Func(arg: number)', 'arg = 3', 'enddef', 'defcompile'], 'E1090:') |
Bram Moolenaar | cb2bdb1 | 2020-05-10 22:53:56 +0200 | [diff] [blame] | 387 | enddef |
| 388 | |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 389 | def Test_call_func_defined_later() |
Bram Moolenaar | d2c6170 | 2020-09-06 15:58:36 +0200 | [diff] [blame] | 390 | assert_equal('one', g:DefinedLater('one')) |
Bram Moolenaar | 9bd5d87 | 2020-09-06 21:47:48 +0200 | [diff] [blame] | 391 | assert_fails('NotDefined("one")', 'E117:', '', 2, 'Test_call_func_defined_later') |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 392 | enddef |
| 393 | |
Bram Moolenaar | 1df8b3f | 2020-04-23 18:13:23 +0200 | [diff] [blame] | 394 | func DefinedLater(arg) |
| 395 | return a:arg |
| 396 | endfunc |
| 397 | |
| 398 | def Test_call_funcref() |
| 399 | assert_equal(3, g:SomeFunc('abc')) |
Bram Moolenaar | 9bd5d87 | 2020-09-06 21:47:48 +0200 | [diff] [blame] | 400 | assert_fails('NotAFunc()', 'E117:', '', 2, 'Test_call_funcref') # comment after call |
| 401 | assert_fails('g:NotAFunc()', 'E117:', '', 3, 'Test_call_funcref') |
Bram Moolenaar | 2f1980f | 2020-07-22 19:30:06 +0200 | [diff] [blame] | 402 | |
| 403 | let lines =<< trim END |
| 404 | vim9script |
| 405 | def RetNumber(): number |
| 406 | return 123 |
| 407 | enddef |
| 408 | let Funcref: func: number = function('RetNumber') |
| 409 | assert_equal(123, Funcref()) |
| 410 | END |
| 411 | CheckScriptSuccess(lines) |
Bram Moolenaar | 0f60e80 | 2020-07-22 20:16:11 +0200 | [diff] [blame] | 412 | |
| 413 | lines =<< trim END |
| 414 | vim9script |
| 415 | def RetNumber(): number |
| 416 | return 123 |
| 417 | enddef |
| 418 | def Bar(F: func: number): number |
| 419 | return F() |
| 420 | enddef |
| 421 | let Funcref = function('RetNumber') |
| 422 | assert_equal(123, Bar(Funcref)) |
| 423 | END |
| 424 | CheckScriptSuccess(lines) |
Bram Moolenaar | bfba865 | 2020-07-23 20:09:10 +0200 | [diff] [blame] | 425 | |
| 426 | lines =<< trim END |
| 427 | vim9script |
| 428 | def UseNumber(nr: number) |
| 429 | echo nr |
| 430 | enddef |
| 431 | let Funcref: func(number) = function('UseNumber') |
| 432 | Funcref(123) |
| 433 | END |
| 434 | CheckScriptSuccess(lines) |
Bram Moolenaar | b8070e3 | 2020-07-23 20:56:04 +0200 | [diff] [blame] | 435 | |
| 436 | lines =<< trim END |
| 437 | vim9script |
| 438 | def UseNumber(nr: number) |
| 439 | echo nr |
| 440 | enddef |
| 441 | let Funcref: func(string) = function('UseNumber') |
| 442 | END |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 443 | CheckScriptFailure(lines, 'E1012: type mismatch, expected func(string) but got func(number)') |
Bram Moolenaar | 4fc224c | 2020-07-26 17:56:25 +0200 | [diff] [blame] | 444 | |
| 445 | lines =<< trim END |
| 446 | vim9script |
| 447 | def EchoNr(nr = 34) |
| 448 | g:echo = nr |
| 449 | enddef |
| 450 | let Funcref: func(?number) = function('EchoNr') |
| 451 | Funcref() |
| 452 | assert_equal(34, g:echo) |
| 453 | Funcref(123) |
| 454 | assert_equal(123, g:echo) |
| 455 | END |
| 456 | CheckScriptSuccess(lines) |
Bram Moolenaar | ace6132 | 2020-07-26 18:16:58 +0200 | [diff] [blame] | 457 | |
| 458 | lines =<< trim END |
| 459 | vim9script |
| 460 | def EchoList(...l: list<number>) |
| 461 | g:echo = l |
| 462 | enddef |
| 463 | let Funcref: func(...list<number>) = function('EchoList') |
| 464 | Funcref() |
| 465 | assert_equal([], g:echo) |
| 466 | Funcref(1, 2, 3) |
| 467 | assert_equal([1, 2, 3], g:echo) |
| 468 | END |
| 469 | CheckScriptSuccess(lines) |
Bram Moolenaar | 01865ad | 2020-07-26 18:33:09 +0200 | [diff] [blame] | 470 | |
| 471 | lines =<< trim END |
| 472 | vim9script |
| 473 | def OptAndVar(nr: number, opt = 12, ...l: list<number>): number |
| 474 | g:optarg = opt |
| 475 | g:listarg = l |
| 476 | return nr |
| 477 | enddef |
| 478 | let Funcref: func(number, ?number, ...list<number>): number = function('OptAndVar') |
| 479 | assert_equal(10, Funcref(10)) |
| 480 | assert_equal(12, g:optarg) |
| 481 | assert_equal([], g:listarg) |
| 482 | |
| 483 | assert_equal(11, Funcref(11, 22)) |
| 484 | assert_equal(22, g:optarg) |
| 485 | assert_equal([], g:listarg) |
| 486 | |
| 487 | assert_equal(17, Funcref(17, 18, 1, 2, 3)) |
| 488 | assert_equal(18, g:optarg) |
| 489 | assert_equal([1, 2, 3], g:listarg) |
| 490 | END |
| 491 | CheckScriptSuccess(lines) |
Bram Moolenaar | 1df8b3f | 2020-04-23 18:13:23 +0200 | [diff] [blame] | 492 | enddef |
| 493 | |
| 494 | let SomeFunc = function('len') |
| 495 | let NotAFunc = 'text' |
| 496 | |
Bram Moolenaar | 99aaf0c | 2020-04-12 14:39:53 +0200 | [diff] [blame] | 497 | def CombineFuncrefTypes() |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 498 | # same arguments, different return type |
Bram Moolenaar | 99aaf0c | 2020-04-12 14:39:53 +0200 | [diff] [blame] | 499 | let Ref1: func(bool): string |
| 500 | let Ref2: func(bool): number |
| 501 | let Ref3: func(bool): any |
| 502 | Ref3 = g:cond ? Ref1 : Ref2 |
| 503 | |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 504 | # different number of arguments |
Bram Moolenaar | 99aaf0c | 2020-04-12 14:39:53 +0200 | [diff] [blame] | 505 | let Refa1: func(bool): number |
| 506 | let Refa2: func(bool, number): number |
| 507 | let Refa3: func: number |
| 508 | Refa3 = g:cond ? Refa1 : Refa2 |
| 509 | |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 510 | # different argument types |
Bram Moolenaar | 99aaf0c | 2020-04-12 14:39:53 +0200 | [diff] [blame] | 511 | let Refb1: func(bool, string): number |
| 512 | let Refb2: func(string, number): number |
| 513 | let Refb3: func(any, any): number |
| 514 | Refb3 = g:cond ? Refb1 : Refb2 |
| 515 | enddef |
| 516 | |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 517 | def FuncWithForwardCall() |
Bram Moolenaar | 1df8b3f | 2020-04-23 18:13:23 +0200 | [diff] [blame] | 518 | return g:DefinedEvenLater("yes") |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 519 | enddef |
| 520 | |
| 521 | def DefinedEvenLater(arg: string): string |
| 522 | return arg |
| 523 | enddef |
| 524 | |
| 525 | def Test_error_in_nested_function() |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 526 | # Error in called function requires unwinding the call stack. |
Bram Moolenaar | 44d6652 | 2020-09-06 22:26:57 +0200 | [diff] [blame] | 527 | assert_fails('FuncWithForwardCall()', 'E1096:', '', 1, 'FuncWithForwardCall') |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 528 | enddef |
| 529 | |
| 530 | def Test_return_type_wrong() |
Bram Moolenaar | 5a849da | 2020-08-08 16:47:30 +0200 | [diff] [blame] | 531 | CheckScriptFailure([ |
| 532 | 'def Func(): number', |
| 533 | 'return "a"', |
| 534 | 'enddef', |
| 535 | 'defcompile'], 'expected number but got string') |
| 536 | CheckScriptFailure([ |
| 537 | 'def Func(): string', |
| 538 | 'return 1', |
| 539 | 'enddef', |
| 540 | 'defcompile'], 'expected string but got number') |
| 541 | CheckScriptFailure([ |
| 542 | 'def Func(): void', |
| 543 | 'return "a"', |
| 544 | 'enddef', |
| 545 | 'defcompile'], |
| 546 | 'E1096: Returning a value in a function without a return type') |
| 547 | CheckScriptFailure([ |
| 548 | 'def Func()', |
| 549 | 'return "a"', |
| 550 | 'enddef', |
| 551 | 'defcompile'], |
| 552 | 'E1096: Returning a value in a function without a return type') |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 553 | |
Bram Moolenaar | 5a849da | 2020-08-08 16:47:30 +0200 | [diff] [blame] | 554 | CheckScriptFailure([ |
| 555 | 'def Func(): number', |
| 556 | 'return', |
| 557 | 'enddef', |
| 558 | 'defcompile'], 'E1003:') |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 559 | |
| 560 | CheckScriptFailure(['def Func(): list', 'return []', 'enddef'], 'E1008:') |
| 561 | CheckScriptFailure(['def Func(): dict', 'return {}', 'enddef'], 'E1008:') |
Bram Moolenaar | ee4e0c1 | 2020-04-06 21:35:05 +0200 | [diff] [blame] | 562 | CheckScriptFailure(['def Func()', 'return 1'], 'E1057:') |
Bram Moolenaar | 5a849da | 2020-08-08 16:47:30 +0200 | [diff] [blame] | 563 | |
| 564 | CheckScriptFailure([ |
| 565 | 'vim9script', |
| 566 | 'def FuncB()', |
| 567 | ' return 123', |
| 568 | 'enddef', |
| 569 | 'def FuncA()', |
| 570 | ' FuncB()', |
| 571 | 'enddef', |
| 572 | 'defcompile'], 'E1096:') |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 573 | enddef |
| 574 | |
| 575 | def Test_arg_type_wrong() |
| 576 | CheckScriptFailure(['def Func3(items: list)', 'echo "a"', 'enddef'], 'E1008: Missing <type>') |
Bram Moolenaar | ee4e0c1 | 2020-04-06 21:35:05 +0200 | [diff] [blame] | 577 | CheckScriptFailure(['def Func4(...)', 'echo "a"', 'enddef'], 'E1055: Missing name after ...') |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 578 | CheckScriptFailure(['def Func5(items:string)', 'echo "a"'], 'E1069:') |
Bram Moolenaar | 6e94978 | 2020-04-13 17:21:00 +0200 | [diff] [blame] | 579 | CheckScriptFailure(['def Func5(items)', 'echo "a"'], 'E1077:') |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 580 | enddef |
| 581 | |
| 582 | def Test_vim9script_call() |
| 583 | let lines =<< trim END |
| 584 | vim9script |
| 585 | let var = '' |
| 586 | def MyFunc(arg: string) |
| 587 | var = arg |
| 588 | enddef |
| 589 | MyFunc('foobar') |
| 590 | assert_equal('foobar', var) |
| 591 | |
| 592 | let str = 'barfoo' |
| 593 | str->MyFunc() |
| 594 | assert_equal('barfoo', var) |
| 595 | |
Bram Moolenaar | 6797966 | 2020-06-20 22:50:47 +0200 | [diff] [blame] | 596 | g:value = 'value' |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 597 | g:value->MyFunc() |
| 598 | assert_equal('value', var) |
| 599 | |
| 600 | let listvar = [] |
| 601 | def ListFunc(arg: list<number>) |
| 602 | listvar = arg |
| 603 | enddef |
| 604 | [1, 2, 3]->ListFunc() |
| 605 | assert_equal([1, 2, 3], listvar) |
| 606 | |
| 607 | let dictvar = {} |
| 608 | def DictFunc(arg: dict<number>) |
| 609 | dictvar = arg |
| 610 | enddef |
| 611 | {'a': 1, 'b': 2}->DictFunc() |
| 612 | assert_equal(#{a: 1, b: 2}, dictvar) |
| 613 | def CompiledDict() |
| 614 | {'a': 3, 'b': 4}->DictFunc() |
| 615 | enddef |
| 616 | CompiledDict() |
| 617 | assert_equal(#{a: 3, b: 4}, dictvar) |
| 618 | |
| 619 | #{a: 3, b: 4}->DictFunc() |
| 620 | assert_equal(#{a: 3, b: 4}, dictvar) |
| 621 | |
| 622 | ('text')->MyFunc() |
| 623 | assert_equal('text', var) |
| 624 | ("some")->MyFunc() |
| 625 | assert_equal('some', var) |
Bram Moolenaar | e6b5324 | 2020-07-01 17:28:33 +0200 | [diff] [blame] | 626 | |
Bram Moolenaar | 13e12b8 | 2020-07-24 18:47:22 +0200 | [diff] [blame] | 627 | # line starting with single quote is not a mark |
Bram Moolenaar | 1040956 | 2020-07-29 20:00:38 +0200 | [diff] [blame] | 628 | # line starting with double quote can be a method call |
Bram Moolenaar | 3d48e25 | 2020-07-15 14:15:52 +0200 | [diff] [blame] | 629 | 'asdfasdf'->MyFunc() |
| 630 | assert_equal('asdfasdf', var) |
Bram Moolenaar | 1040956 | 2020-07-29 20:00:38 +0200 | [diff] [blame] | 631 | "xyz"->MyFunc() |
| 632 | assert_equal('xyz', var) |
Bram Moolenaar | 3d48e25 | 2020-07-15 14:15:52 +0200 | [diff] [blame] | 633 | |
| 634 | def UseString() |
| 635 | 'xyork'->MyFunc() |
| 636 | enddef |
| 637 | UseString() |
| 638 | assert_equal('xyork', var) |
| 639 | |
Bram Moolenaar | 1040956 | 2020-07-29 20:00:38 +0200 | [diff] [blame] | 640 | def UseString2() |
| 641 | "knife"->MyFunc() |
| 642 | enddef |
| 643 | UseString2() |
| 644 | assert_equal('knife', var) |
| 645 | |
Bram Moolenaar | 13e12b8 | 2020-07-24 18:47:22 +0200 | [diff] [blame] | 646 | # prepending a colon makes it a mark |
| 647 | new |
| 648 | setline(1, ['aaa', 'bbb', 'ccc']) |
| 649 | normal! 3Gmt1G |
| 650 | :'t |
| 651 | assert_equal(3, getcurpos()[1]) |
| 652 | bwipe! |
| 653 | |
Bram Moolenaar | e6b5324 | 2020-07-01 17:28:33 +0200 | [diff] [blame] | 654 | MyFunc( |
| 655 | 'continued' |
| 656 | ) |
| 657 | assert_equal('continued', |
| 658 | var |
| 659 | ) |
| 660 | |
| 661 | call MyFunc( |
| 662 | 'more' |
| 663 | .. |
| 664 | 'lines' |
| 665 | ) |
| 666 | assert_equal( |
| 667 | 'morelines', |
| 668 | var) |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 669 | END |
| 670 | writefile(lines, 'Xcall.vim') |
| 671 | source Xcall.vim |
| 672 | delete('Xcall.vim') |
| 673 | enddef |
| 674 | |
| 675 | def Test_vim9script_call_fail_decl() |
| 676 | let lines =<< trim END |
| 677 | vim9script |
| 678 | let var = '' |
| 679 | def MyFunc(arg: string) |
| 680 | let var = 123 |
| 681 | enddef |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 682 | defcompile |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 683 | END |
Bram Moolenaar | 6c4bfe4 | 2020-07-23 18:26:30 +0200 | [diff] [blame] | 684 | CheckScriptFailure(lines, 'E1054:') |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 685 | enddef |
| 686 | |
Bram Moolenaar | 65b9545 | 2020-07-19 14:03:09 +0200 | [diff] [blame] | 687 | def Test_vim9script_call_fail_type() |
| 688 | let lines =<< trim END |
| 689 | vim9script |
| 690 | def MyFunc(arg: string) |
| 691 | echo arg |
| 692 | enddef |
| 693 | MyFunc(1234) |
| 694 | END |
Bram Moolenaar | 8b565c2 | 2020-08-30 23:24:20 +0200 | [diff] [blame] | 695 | CheckScriptFailure(lines, 'E1013: argument 1: type mismatch, expected string but got number') |
Bram Moolenaar | 65b9545 | 2020-07-19 14:03:09 +0200 | [diff] [blame] | 696 | enddef |
| 697 | |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 698 | def Test_vim9script_call_fail_const() |
| 699 | let lines =<< trim END |
| 700 | vim9script |
| 701 | const var = '' |
| 702 | def MyFunc(arg: string) |
| 703 | var = 'asdf' |
| 704 | enddef |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 705 | defcompile |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 706 | END |
| 707 | writefile(lines, 'Xcall_const.vim') |
Bram Moolenaar | 9bd5d87 | 2020-09-06 21:47:48 +0200 | [diff] [blame] | 708 | assert_fails('source Xcall_const.vim', 'E46:', '', 1, 'MyFunc') |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 709 | delete('Xcall_const.vim') |
| 710 | enddef |
| 711 | |
| 712 | " Test that inside :function a Python function can be defined, :def is not |
| 713 | " recognized. |
| 714 | func Test_function_python() |
| 715 | CheckFeature python3 |
| 716 | let py = 'python3' |
| 717 | execute py "<< EOF" |
| 718 | def do_something(): |
| 719 | return 1 |
| 720 | EOF |
| 721 | endfunc |
| 722 | |
| 723 | def Test_delfunc() |
| 724 | let lines =<< trim END |
| 725 | vim9script |
Bram Moolenaar | 4c17ad9 | 2020-04-27 22:47:51 +0200 | [diff] [blame] | 726 | def g:GoneSoon() |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 727 | echo 'hello' |
| 728 | enddef |
| 729 | |
| 730 | def CallGoneSoon() |
| 731 | GoneSoon() |
| 732 | enddef |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 733 | defcompile |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 734 | |
Bram Moolenaar | 4c17ad9 | 2020-04-27 22:47:51 +0200 | [diff] [blame] | 735 | delfunc g:GoneSoon |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 736 | CallGoneSoon() |
| 737 | END |
| 738 | writefile(lines, 'XToDelFunc') |
Bram Moolenaar | 9bd5d87 | 2020-09-06 21:47:48 +0200 | [diff] [blame] | 739 | assert_fails('so XToDelFunc', 'E933:', '', 1, 'CallGoneSoon') |
| 740 | assert_fails('so XToDelFunc', 'E933:', '', 1, 'CallGoneSoon') |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 741 | |
| 742 | delete('XToDelFunc') |
| 743 | enddef |
| 744 | |
| 745 | def Test_redef_failure() |
Bram Moolenaar | d2c6170 | 2020-09-06 15:58:36 +0200 | [diff] [blame] | 746 | writefile(['def Func0(): string', 'return "Func0"', 'enddef'], 'Xdef') |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 747 | so Xdef |
Bram Moolenaar | d2c6170 | 2020-09-06 15:58:36 +0200 | [diff] [blame] | 748 | writefile(['def Func1(): string', 'return "Func1"', 'enddef'], 'Xdef') |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 749 | so Xdef |
Bram Moolenaar | d2c6170 | 2020-09-06 15:58:36 +0200 | [diff] [blame] | 750 | writefile(['def! Func0(): string', 'enddef', 'defcompile'], 'Xdef') |
Bram Moolenaar | 9bd5d87 | 2020-09-06 21:47:48 +0200 | [diff] [blame] | 751 | assert_fails('so Xdef', 'E1027:', '', 1, 'Func0') |
Bram Moolenaar | d2c6170 | 2020-09-06 15:58:36 +0200 | [diff] [blame] | 752 | writefile(['def Func2(): string', 'return "Func2"', 'enddef'], 'Xdef') |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 753 | so Xdef |
Bram Moolenaar | d2c6170 | 2020-09-06 15:58:36 +0200 | [diff] [blame] | 754 | delete('Xdef') |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 755 | |
Bram Moolenaar | d2c6170 | 2020-09-06 15:58:36 +0200 | [diff] [blame] | 756 | assert_equal(0, g:Func0()) |
| 757 | assert_equal('Func1', g:Func1()) |
| 758 | assert_equal('Func2', g:Func2()) |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 759 | |
| 760 | delfunc! Func0 |
| 761 | delfunc! Func1 |
| 762 | delfunc! Func2 |
| 763 | enddef |
| 764 | |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 765 | def Test_vim9script_func() |
| 766 | let lines =<< trim END |
| 767 | vim9script |
| 768 | func Func(arg) |
| 769 | echo a:arg |
| 770 | endfunc |
| 771 | Func('text') |
| 772 | END |
| 773 | writefile(lines, 'XVim9Func') |
| 774 | so XVim9Func |
| 775 | |
| 776 | delete('XVim9Func') |
| 777 | enddef |
| 778 | |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 779 | " Test for internal functions returning different types |
| 780 | func Test_InternalFuncRetType() |
| 781 | let lines =<< trim END |
| 782 | def RetFloat(): float |
| 783 | return ceil(1.456) |
| 784 | enddef |
| 785 | |
| 786 | def RetListAny(): list<any> |
Bram Moolenaar | 17a836c | 2020-08-12 17:35:58 +0200 | [diff] [blame] | 787 | return items({'k': 'v'}) |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 788 | enddef |
| 789 | |
| 790 | def RetListString(): list<string> |
| 791 | return split('a:b:c', ':') |
| 792 | enddef |
| 793 | |
| 794 | def RetListDictAny(): list<dict<any>> |
| 795 | return getbufinfo() |
| 796 | enddef |
| 797 | |
| 798 | def RetDictNumber(): dict<number> |
| 799 | return wordcount() |
| 800 | enddef |
| 801 | |
| 802 | def RetDictString(): dict<string> |
| 803 | return environ() |
| 804 | enddef |
| 805 | END |
| 806 | call writefile(lines, 'Xscript') |
| 807 | source Xscript |
| 808 | |
| 809 | call assert_equal(2.0, RetFloat()) |
| 810 | call assert_equal([['k', 'v']], RetListAny()) |
| 811 | call assert_equal(['a', 'b', 'c'], RetListString()) |
| 812 | call assert_notequal([], RetListDictAny()) |
| 813 | call assert_notequal({}, RetDictNumber()) |
| 814 | call assert_notequal({}, RetDictString()) |
| 815 | call delete('Xscript') |
| 816 | endfunc |
| 817 | |
| 818 | " Test for passing too many or too few arguments to internal functions |
| 819 | func Test_internalfunc_arg_error() |
| 820 | let l =<< trim END |
| 821 | def! FArgErr(): float |
| 822 | return ceil(1.1, 2) |
| 823 | enddef |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 824 | defcompile |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 825 | END |
| 826 | call writefile(l, 'Xinvalidarg') |
Bram Moolenaar | 9bd5d87 | 2020-09-06 21:47:48 +0200 | [diff] [blame] | 827 | call assert_fails('so Xinvalidarg', 'E118:', '', 1, 'FArgErr') |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 828 | let l =<< trim END |
| 829 | def! FArgErr(): float |
| 830 | return ceil() |
| 831 | enddef |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 832 | defcompile |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 833 | END |
| 834 | call writefile(l, 'Xinvalidarg') |
Bram Moolenaar | 9bd5d87 | 2020-09-06 21:47:48 +0200 | [diff] [blame] | 835 | call assert_fails('so Xinvalidarg', 'E119:', '', 1, 'FArgErr') |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 836 | call delete('Xinvalidarg') |
| 837 | endfunc |
| 838 | |
| 839 | let s:funcResult = 0 |
| 840 | |
| 841 | def FuncNoArgNoRet() |
Bram Moolenaar | 5390099 | 2020-08-22 19:02:02 +0200 | [diff] [blame] | 842 | s:funcResult = 11 |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 843 | enddef |
| 844 | |
| 845 | def FuncNoArgRetNumber(): number |
Bram Moolenaar | 5390099 | 2020-08-22 19:02:02 +0200 | [diff] [blame] | 846 | s:funcResult = 22 |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 847 | return 1234 |
| 848 | enddef |
| 849 | |
Bram Moolenaar | ec5929d | 2020-04-07 20:53:39 +0200 | [diff] [blame] | 850 | def FuncNoArgRetString(): string |
Bram Moolenaar | 5390099 | 2020-08-22 19:02:02 +0200 | [diff] [blame] | 851 | s:funcResult = 45 |
Bram Moolenaar | ec5929d | 2020-04-07 20:53:39 +0200 | [diff] [blame] | 852 | return 'text' |
| 853 | enddef |
| 854 | |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 855 | def FuncOneArgNoRet(arg: number) |
Bram Moolenaar | 5390099 | 2020-08-22 19:02:02 +0200 | [diff] [blame] | 856 | s:funcResult = arg |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 857 | enddef |
| 858 | |
| 859 | def FuncOneArgRetNumber(arg: number): number |
Bram Moolenaar | 5390099 | 2020-08-22 19:02:02 +0200 | [diff] [blame] | 860 | s:funcResult = arg |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 861 | return arg |
| 862 | enddef |
| 863 | |
Bram Moolenaar | 08938ee | 2020-04-11 23:17:17 +0200 | [diff] [blame] | 864 | def FuncTwoArgNoRet(one: bool, two: number) |
Bram Moolenaar | 5390099 | 2020-08-22 19:02:02 +0200 | [diff] [blame] | 865 | s:funcResult = two |
Bram Moolenaar | 08938ee | 2020-04-11 23:17:17 +0200 | [diff] [blame] | 866 | enddef |
| 867 | |
Bram Moolenaar | ec5929d | 2020-04-07 20:53:39 +0200 | [diff] [blame] | 868 | def FuncOneArgRetString(arg: string): string |
| 869 | return arg |
| 870 | enddef |
| 871 | |
Bram Moolenaar | 8922860 | 2020-04-05 22:14:54 +0200 | [diff] [blame] | 872 | def FuncOneArgRetAny(arg: any): any |
| 873 | return arg |
| 874 | enddef |
| 875 | |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 876 | def Test_func_type() |
| 877 | let Ref1: func() |
Bram Moolenaar | 5390099 | 2020-08-22 19:02:02 +0200 | [diff] [blame] | 878 | s:funcResult = 0 |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 879 | Ref1 = FuncNoArgNoRet |
| 880 | Ref1() |
Bram Moolenaar | 5390099 | 2020-08-22 19:02:02 +0200 | [diff] [blame] | 881 | assert_equal(11, s:funcResult) |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 882 | |
| 883 | let Ref2: func |
Bram Moolenaar | 5390099 | 2020-08-22 19:02:02 +0200 | [diff] [blame] | 884 | s:funcResult = 0 |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 885 | Ref2 = FuncNoArgNoRet |
| 886 | Ref2() |
Bram Moolenaar | 5390099 | 2020-08-22 19:02:02 +0200 | [diff] [blame] | 887 | assert_equal(11, s:funcResult) |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 888 | |
Bram Moolenaar | 5390099 | 2020-08-22 19:02:02 +0200 | [diff] [blame] | 889 | s:funcResult = 0 |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 890 | Ref2 = FuncOneArgNoRet |
| 891 | Ref2(12) |
Bram Moolenaar | 5390099 | 2020-08-22 19:02:02 +0200 | [diff] [blame] | 892 | assert_equal(12, s:funcResult) |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 893 | |
Bram Moolenaar | 5390099 | 2020-08-22 19:02:02 +0200 | [diff] [blame] | 894 | s:funcResult = 0 |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 895 | Ref2 = FuncNoArgRetNumber |
| 896 | assert_equal(1234, Ref2()) |
Bram Moolenaar | 5390099 | 2020-08-22 19:02:02 +0200 | [diff] [blame] | 897 | assert_equal(22, s:funcResult) |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 898 | |
Bram Moolenaar | 5390099 | 2020-08-22 19:02:02 +0200 | [diff] [blame] | 899 | s:funcResult = 0 |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 900 | Ref2 = FuncOneArgRetNumber |
| 901 | assert_equal(13, Ref2(13)) |
Bram Moolenaar | 5390099 | 2020-08-22 19:02:02 +0200 | [diff] [blame] | 902 | assert_equal(13, s:funcResult) |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 903 | enddef |
| 904 | |
Bram Moolenaar | 9978d47 | 2020-07-05 16:01:56 +0200 | [diff] [blame] | 905 | def Test_repeat_return_type() |
| 906 | let res = 0 |
| 907 | for n in repeat([1], 3) |
| 908 | res += n |
| 909 | endfor |
| 910 | assert_equal(3, res) |
Bram Moolenaar | fce82b3 | 2020-07-05 16:07:21 +0200 | [diff] [blame] | 911 | |
| 912 | res = 0 |
| 913 | for n in add([1, 2], 3) |
| 914 | res += n |
| 915 | endfor |
| 916 | assert_equal(6, res) |
Bram Moolenaar | 9978d47 | 2020-07-05 16:01:56 +0200 | [diff] [blame] | 917 | enddef |
| 918 | |
Bram Moolenaar | 846178a | 2020-07-05 17:04:13 +0200 | [diff] [blame] | 919 | def Test_argv_return_type() |
| 920 | next fileone filetwo |
| 921 | let res = '' |
| 922 | for name in argv() |
| 923 | res ..= name |
| 924 | endfor |
| 925 | assert_equal('fileonefiletwo', res) |
| 926 | enddef |
| 927 | |
Bram Moolenaar | ec5929d | 2020-04-07 20:53:39 +0200 | [diff] [blame] | 928 | def Test_func_type_part() |
| 929 | let RefVoid: func: void |
| 930 | RefVoid = FuncNoArgNoRet |
| 931 | RefVoid = FuncOneArgNoRet |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 932 | CheckDefFailure(['let RefVoid: func: void', 'RefVoid = FuncNoArgRetNumber'], 'E1012: type mismatch, expected func() but got func(): number') |
| 933 | CheckDefFailure(['let RefVoid: func: void', 'RefVoid = FuncNoArgRetString'], 'E1012: type mismatch, expected func() but got func(): string') |
Bram Moolenaar | ec5929d | 2020-04-07 20:53:39 +0200 | [diff] [blame] | 934 | |
| 935 | let RefAny: func(): any |
| 936 | RefAny = FuncNoArgRetNumber |
| 937 | RefAny = FuncNoArgRetString |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 938 | CheckDefFailure(['let RefAny: func(): any', 'RefAny = FuncNoArgNoRet'], 'E1012: type mismatch, expected func(): any but got func()') |
| 939 | CheckDefFailure(['let RefAny: func(): any', 'RefAny = FuncOneArgNoRet'], 'E1012: type mismatch, expected func(): any but got func(number)') |
Bram Moolenaar | ec5929d | 2020-04-07 20:53:39 +0200 | [diff] [blame] | 940 | |
| 941 | let RefNr: func: number |
| 942 | RefNr = FuncNoArgRetNumber |
| 943 | RefNr = FuncOneArgRetNumber |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 944 | CheckDefFailure(['let RefNr: func: number', 'RefNr = FuncNoArgNoRet'], 'E1012: type mismatch, expected func(): number but got func()') |
| 945 | CheckDefFailure(['let RefNr: func: number', 'RefNr = FuncNoArgRetString'], 'E1012: type mismatch, expected func(): number but got func(): string') |
Bram Moolenaar | ec5929d | 2020-04-07 20:53:39 +0200 | [diff] [blame] | 946 | |
| 947 | let RefStr: func: string |
| 948 | RefStr = FuncNoArgRetString |
| 949 | RefStr = FuncOneArgRetString |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 950 | CheckDefFailure(['let RefStr: func: string', 'RefStr = FuncNoArgNoRet'], 'E1012: type mismatch, expected func(): string but got func()') |
| 951 | CheckDefFailure(['let RefStr: func: string', 'RefStr = FuncNoArgRetNumber'], 'E1012: type mismatch, expected func(): string but got func(): number') |
Bram Moolenaar | ec5929d | 2020-04-07 20:53:39 +0200 | [diff] [blame] | 952 | enddef |
| 953 | |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 954 | def Test_func_type_fails() |
| 955 | CheckDefFailure(['let ref1: func()'], 'E704:') |
| 956 | |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 957 | CheckDefFailure(['let Ref1: func()', 'Ref1 = FuncNoArgRetNumber'], 'E1012: type mismatch, expected func() but got func(): number') |
| 958 | CheckDefFailure(['let Ref1: func()', 'Ref1 = FuncOneArgNoRet'], 'E1012: type mismatch, expected func() but got func(number)') |
| 959 | CheckDefFailure(['let Ref1: func()', 'Ref1 = FuncOneArgRetNumber'], 'E1012: type mismatch, expected func() but got func(number): number') |
| 960 | CheckDefFailure(['let Ref1: func(bool)', 'Ref1 = FuncTwoArgNoRet'], 'E1012: type mismatch, expected func(bool) but got func(bool, number)') |
| 961 | CheckDefFailure(['let Ref1: func(?bool)', 'Ref1 = FuncTwoArgNoRet'], 'E1012: type mismatch, expected func(?bool) but got func(bool, number)') |
| 962 | CheckDefFailure(['let Ref1: func(...bool)', 'Ref1 = FuncTwoArgNoRet'], 'E1012: type mismatch, expected func(...bool) but got func(bool, number)') |
Bram Moolenaar | 08938ee | 2020-04-11 23:17:17 +0200 | [diff] [blame] | 963 | |
Bram Moolenaar | d2c6170 | 2020-09-06 15:58:36 +0200 | [diff] [blame] | 964 | CheckDefFailure(['let RefWrong: func(string ,number)'], 'E1068:') |
| 965 | CheckDefFailure(['let RefWrong: func(string,number)'], 'E1069:') |
| 966 | CheckDefFailure(['let RefWrong: func(bool, bool, bool, bool, bool, bool, bool, bool, bool, bool, bool, bool, bool, bool, bool, bool, bool, bool, bool, bool)'], 'E1005:') |
| 967 | CheckDefFailure(['let RefWrong: func(bool):string'], 'E1069:') |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 968 | enddef |
| 969 | |
Bram Moolenaar | 8922860 | 2020-04-05 22:14:54 +0200 | [diff] [blame] | 970 | def Test_func_return_type() |
| 971 | let nr: number |
| 972 | nr = FuncNoArgRetNumber() |
| 973 | assert_equal(1234, nr) |
| 974 | |
| 975 | nr = FuncOneArgRetAny(122) |
| 976 | assert_equal(122, nr) |
| 977 | |
| 978 | let str: string |
| 979 | str = FuncOneArgRetAny('yes') |
| 980 | assert_equal('yes', str) |
| 981 | |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 982 | CheckDefFailure(['let str: string', 'str = FuncNoArgRetNumber()'], 'E1012: type mismatch, expected string but got number') |
Bram Moolenaar | 8922860 | 2020-04-05 22:14:54 +0200 | [diff] [blame] | 983 | enddef |
| 984 | |
Bram Moolenaar | 5e774c7 | 2020-04-12 21:53:00 +0200 | [diff] [blame] | 985 | def MultiLine( |
| 986 | arg1: string, |
| 987 | arg2 = 1234, |
| 988 | ...rest: list<string> |
| 989 | ): string |
| 990 | return arg1 .. arg2 .. join(rest, '-') |
| 991 | enddef |
| 992 | |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 993 | def MultiLineComment( |
| 994 | arg1: string, # comment |
| 995 | arg2 = 1234, # comment |
| 996 | ...rest: list<string> # comment |
| 997 | ): string # comment |
| 998 | return arg1 .. arg2 .. join(rest, '-') |
| 999 | enddef |
| 1000 | |
Bram Moolenaar | 5e774c7 | 2020-04-12 21:53:00 +0200 | [diff] [blame] | 1001 | def Test_multiline() |
| 1002 | assert_equal('text1234', MultiLine('text')) |
| 1003 | assert_equal('text777', MultiLine('text', 777)) |
| 1004 | assert_equal('text777one', MultiLine('text', 777, 'one')) |
| 1005 | assert_equal('text777one-two', MultiLine('text', 777, 'one', 'two')) |
| 1006 | enddef |
| 1007 | |
Bram Moolenaar | 23e0325 | 2020-04-12 22:22:31 +0200 | [diff] [blame] | 1008 | func Test_multiline_not_vim9() |
| 1009 | call assert_equal('text1234', MultiLine('text')) |
| 1010 | call assert_equal('text777', MultiLine('text', 777)) |
| 1011 | call assert_equal('text777one', MultiLine('text', 777, 'one')) |
| 1012 | call assert_equal('text777one-two', MultiLine('text', 777, 'one', 'two')) |
| 1013 | endfunc |
| 1014 | |
Bram Moolenaar | 5e774c7 | 2020-04-12 21:53:00 +0200 | [diff] [blame] | 1015 | |
Bram Moolenaar | ee4e0c1 | 2020-04-06 21:35:05 +0200 | [diff] [blame] | 1016 | " When using CheckScriptFailure() for the below test, E1010 is generated instead |
| 1017 | " of E1056. |
| 1018 | func Test_E1056_1059() |
| 1019 | let caught_1056 = 0 |
| 1020 | try |
| 1021 | def F(): |
| 1022 | return 1 |
| 1023 | enddef |
| 1024 | catch /E1056:/ |
| 1025 | let caught_1056 = 1 |
| 1026 | endtry |
| 1027 | call assert_equal(1, caught_1056) |
| 1028 | |
| 1029 | let caught_1059 = 0 |
| 1030 | try |
| 1031 | def F5(items : list) |
| 1032 | echo 'a' |
| 1033 | enddef |
| 1034 | catch /E1059:/ |
| 1035 | let caught_1059 = 1 |
| 1036 | endtry |
| 1037 | call assert_equal(1, caught_1059) |
| 1038 | endfunc |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 1039 | |
Bram Moolenaar | 015f426 | 2020-05-05 21:25:22 +0200 | [diff] [blame] | 1040 | func DelMe() |
| 1041 | echo 'DelMe' |
| 1042 | endfunc |
| 1043 | |
Bram Moolenaar | bf8feb5 | 2020-08-08 14:26:31 +0200 | [diff] [blame] | 1044 | def Test_error_reporting() |
| 1045 | # comment lines at the start of the function |
| 1046 | let lines =<< trim END |
| 1047 | " comment |
| 1048 | def Func() |
| 1049 | # comment |
| 1050 | # comment |
| 1051 | invalid |
| 1052 | enddef |
| 1053 | defcompile |
| 1054 | END |
| 1055 | call writefile(lines, 'Xdef') |
| 1056 | try |
| 1057 | source Xdef |
Bram Moolenaar | 7517ffd | 2020-08-14 18:35:07 +0200 | [diff] [blame] | 1058 | assert_report('should have failed') |
Bram Moolenaar | bf8feb5 | 2020-08-08 14:26:31 +0200 | [diff] [blame] | 1059 | catch /E476:/ |
| 1060 | assert_match('Invalid command: invalid', v:exception) |
| 1061 | assert_match(', line 3$', v:throwpoint) |
| 1062 | endtry |
| 1063 | |
| 1064 | # comment lines after the start of the function |
| 1065 | lines =<< trim END |
| 1066 | " comment |
| 1067 | def Func() |
| 1068 | let x = 1234 |
| 1069 | # comment |
| 1070 | # comment |
| 1071 | invalid |
| 1072 | enddef |
| 1073 | defcompile |
| 1074 | END |
| 1075 | call writefile(lines, 'Xdef') |
| 1076 | try |
| 1077 | source Xdef |
Bram Moolenaar | 7517ffd | 2020-08-14 18:35:07 +0200 | [diff] [blame] | 1078 | assert_report('should have failed') |
Bram Moolenaar | bf8feb5 | 2020-08-08 14:26:31 +0200 | [diff] [blame] | 1079 | catch /E476:/ |
| 1080 | assert_match('Invalid command: invalid', v:exception) |
| 1081 | assert_match(', line 4$', v:throwpoint) |
| 1082 | endtry |
| 1083 | |
Bram Moolenaar | 7517ffd | 2020-08-14 18:35:07 +0200 | [diff] [blame] | 1084 | lines =<< trim END |
| 1085 | vim9script |
| 1086 | def Func() |
| 1087 | let db = #{foo: 1, bar: 2} |
| 1088 | # comment |
| 1089 | let x = db.asdf |
| 1090 | enddef |
| 1091 | defcompile |
| 1092 | Func() |
| 1093 | END |
| 1094 | call writefile(lines, 'Xdef') |
| 1095 | try |
| 1096 | source Xdef |
| 1097 | assert_report('should have failed') |
| 1098 | catch /E716:/ |
| 1099 | assert_match('_Func, line 3$', v:throwpoint) |
| 1100 | endtry |
| 1101 | |
Bram Moolenaar | bf8feb5 | 2020-08-08 14:26:31 +0200 | [diff] [blame] | 1102 | call delete('Xdef') |
| 1103 | enddef |
| 1104 | |
Bram Moolenaar | 015f426 | 2020-05-05 21:25:22 +0200 | [diff] [blame] | 1105 | def Test_deleted_function() |
| 1106 | CheckDefExecFailure([ |
| 1107 | 'let RefMe: func = function("g:DelMe")', |
| 1108 | 'delfunc g:DelMe', |
| 1109 | 'echo RefMe()'], 'E117:') |
| 1110 | enddef |
| 1111 | |
| 1112 | def Test_unknown_function() |
| 1113 | CheckDefExecFailure([ |
| 1114 | 'let Ref: func = function("NotExist")', |
Bram Moolenaar | 9b7bf9e | 2020-07-11 22:14:59 +0200 | [diff] [blame] | 1115 | 'delfunc g:NotExist'], 'E700:') |
Bram Moolenaar | 015f426 | 2020-05-05 21:25:22 +0200 | [diff] [blame] | 1116 | enddef |
| 1117 | |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 1118 | def RefFunc(Ref: func(string): string): string |
| 1119 | return Ref('more') |
| 1120 | enddef |
| 1121 | |
| 1122 | def Test_closure_simple() |
| 1123 | let local = 'some ' |
| 1124 | assert_equal('some more', RefFunc({s -> local .. s})) |
| 1125 | enddef |
| 1126 | |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 1127 | def MakeRef() |
| 1128 | let local = 'some ' |
| 1129 | g:Ref = {s -> local .. s} |
| 1130 | enddef |
| 1131 | |
| 1132 | def Test_closure_ref_after_return() |
| 1133 | MakeRef() |
| 1134 | assert_equal('some thing', g:Ref('thing')) |
| 1135 | unlet g:Ref |
| 1136 | enddef |
| 1137 | |
Bram Moolenaar | 5adc55c | 2020-05-02 23:12:58 +0200 | [diff] [blame] | 1138 | def MakeTwoRefs() |
| 1139 | let local = ['some'] |
| 1140 | g:Extend = {s -> local->add(s)} |
| 1141 | g:Read = {-> local} |
| 1142 | enddef |
| 1143 | |
| 1144 | def Test_closure_two_refs() |
| 1145 | MakeTwoRefs() |
| 1146 | assert_equal('some', join(g:Read(), ' ')) |
| 1147 | g:Extend('more') |
| 1148 | assert_equal('some more', join(g:Read(), ' ')) |
| 1149 | g:Extend('even') |
| 1150 | assert_equal('some more even', join(g:Read(), ' ')) |
| 1151 | |
| 1152 | unlet g:Extend |
| 1153 | unlet g:Read |
| 1154 | enddef |
| 1155 | |
Bram Moolenaar | 5adc55c | 2020-05-02 23:12:58 +0200 | [diff] [blame] | 1156 | def ReadRef(Ref: func(): list<string>): string |
| 1157 | return join(Ref(), ' ') |
| 1158 | enddef |
| 1159 | |
| 1160 | def ExtendRef(Ref: func(string), add: string) |
| 1161 | Ref(add) |
| 1162 | enddef |
| 1163 | |
| 1164 | def Test_closure_two_indirect_refs() |
Bram Moolenaar | f7779c6 | 2020-05-03 15:38:16 +0200 | [diff] [blame] | 1165 | MakeTwoRefs() |
Bram Moolenaar | 5adc55c | 2020-05-02 23:12:58 +0200 | [diff] [blame] | 1166 | assert_equal('some', ReadRef(g:Read)) |
| 1167 | ExtendRef(g:Extend, 'more') |
| 1168 | assert_equal('some more', ReadRef(g:Read)) |
| 1169 | ExtendRef(g:Extend, 'even') |
| 1170 | assert_equal('some more even', ReadRef(g:Read)) |
| 1171 | |
| 1172 | unlet g:Extend |
| 1173 | unlet g:Read |
| 1174 | enddef |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 1175 | |
Bram Moolenaar | 2fd4cd7 | 2020-05-03 22:30:49 +0200 | [diff] [blame] | 1176 | def MakeArgRefs(theArg: string) |
| 1177 | let local = 'loc_val' |
| 1178 | g:UseArg = {s -> theArg .. '/' .. local .. '/' .. s} |
| 1179 | enddef |
| 1180 | |
| 1181 | def MakeArgRefsVarargs(theArg: string, ...rest: list<string>) |
| 1182 | let local = 'the_loc' |
| 1183 | g:UseVararg = {s -> theArg .. '/' .. local .. '/' .. s .. '/' .. join(rest)} |
| 1184 | enddef |
| 1185 | |
| 1186 | def Test_closure_using_argument() |
| 1187 | MakeArgRefs('arg_val') |
| 1188 | assert_equal('arg_val/loc_val/call_val', g:UseArg('call_val')) |
| 1189 | |
| 1190 | MakeArgRefsVarargs('arg_val', 'one', 'two') |
| 1191 | assert_equal('arg_val/the_loc/call_val/one two', g:UseVararg('call_val')) |
| 1192 | |
| 1193 | unlet g:UseArg |
| 1194 | unlet g:UseVararg |
| 1195 | enddef |
| 1196 | |
Bram Moolenaar | b68b346 | 2020-05-06 21:06:30 +0200 | [diff] [blame] | 1197 | def MakeGetAndAppendRefs() |
| 1198 | let local = 'a' |
| 1199 | |
| 1200 | def Append(arg: string) |
| 1201 | local ..= arg |
| 1202 | enddef |
| 1203 | g:Append = Append |
| 1204 | |
| 1205 | def Get(): string |
| 1206 | return local |
| 1207 | enddef |
| 1208 | g:Get = Get |
| 1209 | enddef |
| 1210 | |
| 1211 | def Test_closure_append_get() |
| 1212 | MakeGetAndAppendRefs() |
| 1213 | assert_equal('a', g:Get()) |
| 1214 | g:Append('-b') |
| 1215 | assert_equal('a-b', g:Get()) |
| 1216 | g:Append('-c') |
| 1217 | assert_equal('a-b-c', g:Get()) |
| 1218 | |
| 1219 | unlet g:Append |
| 1220 | unlet g:Get |
| 1221 | enddef |
| 1222 | |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 1223 | def Test_nested_closure() |
| 1224 | let local = 'text' |
| 1225 | def Closure(arg: string): string |
| 1226 | return local .. arg |
| 1227 | enddef |
| 1228 | assert_equal('text!!!', Closure('!!!')) |
| 1229 | enddef |
| 1230 | |
Bram Moolenaar | 6f5b6df | 2020-05-16 21:20:12 +0200 | [diff] [blame] | 1231 | func GetResult(Ref) |
| 1232 | return a:Ref('some') |
| 1233 | endfunc |
| 1234 | |
| 1235 | def Test_call_closure_not_compiled() |
| 1236 | let text = 'text' |
| 1237 | g:Ref = {s -> s .. text} |
| 1238 | assert_equal('sometext', GetResult(g:Ref)) |
| 1239 | enddef |
| 1240 | |
Bram Moolenaar | 865af6b | 2020-06-18 18:45:49 +0200 | [diff] [blame] | 1241 | def Test_sort_return_type() |
| 1242 | let res: list<number> |
| 1243 | res = [1, 2, 3]->sort() |
| 1244 | enddef |
| 1245 | |
Bram Moolenaar | f151ad1 | 2020-06-30 13:38:01 +0200 | [diff] [blame] | 1246 | def Test_getqflist_return_type() |
| 1247 | let l = getqflist() |
| 1248 | assert_equal([], l) |
| 1249 | |
| 1250 | let d = getqflist(#{items: 0}) |
| 1251 | assert_equal(#{items: []}, d) |
| 1252 | enddef |
| 1253 | |
| 1254 | def Test_getloclist_return_type() |
| 1255 | let l = getloclist(1) |
| 1256 | assert_equal([], l) |
| 1257 | |
| 1258 | let d = getloclist(1, #{items: 0}) |
| 1259 | assert_equal(#{items: []}, d) |
| 1260 | enddef |
| 1261 | |
Bram Moolenaar | a66ba01 | 2020-07-05 18:41:08 +0200 | [diff] [blame] | 1262 | def Test_copy_return_type() |
| 1263 | let l = copy([1, 2, 3]) |
| 1264 | let res = 0 |
| 1265 | for n in l |
| 1266 | res += n |
| 1267 | endfor |
| 1268 | assert_equal(6, res) |
| 1269 | |
| 1270 | let dl = deepcopy([1, 2, 3]) |
| 1271 | res = 0 |
| 1272 | for n in dl |
| 1273 | res += n |
| 1274 | endfor |
| 1275 | assert_equal(6, res) |
Bram Moolenaar | 44b4a24 | 2020-09-05 17:18:28 +0200 | [diff] [blame] | 1276 | |
| 1277 | dl = deepcopy([1, 2, 3], true) |
Bram Moolenaar | a66ba01 | 2020-07-05 18:41:08 +0200 | [diff] [blame] | 1278 | enddef |
| 1279 | |
Bram Moolenaar | b3c019c | 2020-07-05 20:08:39 +0200 | [diff] [blame] | 1280 | def Test_extend_return_type() |
| 1281 | let l = extend([1, 2], [3]) |
| 1282 | let res = 0 |
| 1283 | for n in l |
| 1284 | res += n |
| 1285 | endfor |
| 1286 | assert_equal(6, res) |
| 1287 | enddef |
| 1288 | |
Bram Moolenaar | 2df4731 | 2020-09-05 17:30:44 +0200 | [diff] [blame] | 1289 | def Test_garbagecollect() |
| 1290 | garbagecollect(true) |
| 1291 | enddef |
| 1292 | |
Bram Moolenaar | 252e88a | 2020-07-05 20:47:18 +0200 | [diff] [blame] | 1293 | def Test_insert_return_type() |
| 1294 | let l = insert([2, 1], 3) |
| 1295 | let res = 0 |
| 1296 | for n in l |
| 1297 | res += n |
| 1298 | endfor |
| 1299 | assert_equal(6, res) |
| 1300 | enddef |
| 1301 | |
Bram Moolenaar | 32f335f | 2020-08-14 18:56:45 +0200 | [diff] [blame] | 1302 | def Test_keys_return_type() |
| 1303 | const var: list<string> = #{a: 1, b: 2}->keys() |
| 1304 | assert_equal(['a', 'b'], var) |
| 1305 | enddef |
| 1306 | |
Bram Moolenaar | 6762735 | 2020-07-05 21:10:24 +0200 | [diff] [blame] | 1307 | def Test_reverse_return_type() |
| 1308 | let l = reverse([1, 2, 3]) |
| 1309 | let res = 0 |
| 1310 | for n in l |
| 1311 | res += n |
| 1312 | endfor |
| 1313 | assert_equal(6, res) |
| 1314 | enddef |
| 1315 | |
Bram Moolenaar | ad7c249 | 2020-07-05 20:55:29 +0200 | [diff] [blame] | 1316 | def Test_remove_return_type() |
| 1317 | let l = remove(#{one: [1, 2], two: [3, 4]}, 'one') |
| 1318 | let res = 0 |
| 1319 | for n in l |
| 1320 | res += n |
| 1321 | endfor |
| 1322 | assert_equal(3, res) |
| 1323 | enddef |
| 1324 | |
Bram Moolenaar | 0d94ad6 | 2020-07-05 20:16:41 +0200 | [diff] [blame] | 1325 | def Test_filter_return_type() |
| 1326 | let l = filter([1, 2, 3], {-> 1}) |
| 1327 | let res = 0 |
| 1328 | for n in l |
| 1329 | res += n |
| 1330 | endfor |
| 1331 | assert_equal(6, res) |
| 1332 | enddef |
| 1333 | |
Bram Moolenaar | f39397e | 2020-08-17 22:21:36 +0200 | [diff] [blame] | 1334 | def Test_bufnr() |
| 1335 | let buf = bufnr() |
| 1336 | assert_equal(buf, bufnr('%')) |
Bram Moolenaar | fe136c9 | 2020-09-04 18:35:26 +0200 | [diff] [blame] | 1337 | |
| 1338 | buf = bufnr('Xdummy', true) |
| 1339 | assert_notequal(-1, buf) |
| 1340 | exe 'bwipe! ' .. buf |
Bram Moolenaar | f39397e | 2020-08-17 22:21:36 +0200 | [diff] [blame] | 1341 | enddef |
| 1342 | |
Bram Moolenaar | ec65d77 | 2020-08-20 22:29:12 +0200 | [diff] [blame] | 1343 | def Test_col() |
| 1344 | new |
| 1345 | setline(1, 'asdf') |
| 1346 | assert_equal(5, col([1, '$'])) |
| 1347 | enddef |
| 1348 | |
Bram Moolenaar | 24f7750 | 2020-09-04 19:50:57 +0200 | [diff] [blame] | 1349 | def Test_char2nr() |
| 1350 | assert_equal(12354, char2nr('あ', true)) |
| 1351 | enddef |
| 1352 | |
Bram Moolenaar | 3d945cc | 2020-08-06 21:26:59 +0200 | [diff] [blame] | 1353 | def Test_getreg_return_type() |
| 1354 | let s1: string = getreg('"') |
| 1355 | let s2: string = getreg('"', 1) |
| 1356 | let s3: list<string> = getreg('"', 1, 1) |
| 1357 | enddef |
| 1358 | |
Bram Moolenaar | f1a2368 | 2020-07-13 18:55:48 +0200 | [diff] [blame] | 1359 | def Wrong_dict_key_type(items: list<number>): list<number> |
| 1360 | return filter(items, {_, val -> get({val: 1}, 'x')}) |
| 1361 | enddef |
| 1362 | |
| 1363 | def Test_wrong_dict_key_type() |
| 1364 | assert_fails('Wrong_dict_key_type([1, 2, 3])', 'E1029:') |
| 1365 | enddef |
| 1366 | |
Bram Moolenaar | acd4c5e | 2020-06-22 19:39:03 +0200 | [diff] [blame] | 1367 | def Line_continuation_in_def(dir: string = ''): string |
| 1368 | let path: string = empty(dir) |
| 1369 | \ ? 'empty' |
| 1370 | \ : 'full' |
| 1371 | return path |
| 1372 | enddef |
| 1373 | |
| 1374 | def Test_line_continuation_in_def() |
| 1375 | assert_equal('full', Line_continuation_in_def('.')) |
| 1376 | enddef |
| 1377 | |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 1378 | def Line_continuation_in_lambda(): list<number> |
| 1379 | let x = range(97, 100) |
Bram Moolenaar | 914e7ea | 2020-07-11 15:20:48 +0200 | [diff] [blame] | 1380 | ->map({_, v -> nr2char(v) |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 1381 | ->toupper()}) |
| 1382 | ->reverse() |
| 1383 | return x |
| 1384 | enddef |
| 1385 | |
| 1386 | def Test_line_continuation_in_lambda() |
| 1387 | assert_equal(['D', 'C', 'B', 'A'], Line_continuation_in_lambda()) |
| 1388 | enddef |
| 1389 | |
Bram Moolenaar | 8f510af | 2020-07-05 18:48:23 +0200 | [diff] [blame] | 1390 | func Test_silent_echo() |
Bram Moolenaar | 47e7d70 | 2020-07-05 18:18:42 +0200 | [diff] [blame] | 1391 | CheckScreendump |
| 1392 | |
| 1393 | let lines =<< trim END |
| 1394 | vim9script |
| 1395 | def EchoNothing() |
| 1396 | silent echo '' |
| 1397 | enddef |
| 1398 | defcompile |
| 1399 | END |
Bram Moolenaar | 8f510af | 2020-07-05 18:48:23 +0200 | [diff] [blame] | 1400 | call writefile(lines, 'XTest_silent_echo') |
Bram Moolenaar | 47e7d70 | 2020-07-05 18:18:42 +0200 | [diff] [blame] | 1401 | |
| 1402 | " Check that the balloon shows up after a mouse move |
| 1403 | let buf = RunVimInTerminal('-S XTest_silent_echo', {'rows': 6}) |
Bram Moolenaar | 8f510af | 2020-07-05 18:48:23 +0200 | [diff] [blame] | 1404 | call term_sendkeys(buf, ":abc") |
Bram Moolenaar | 47e7d70 | 2020-07-05 18:18:42 +0200 | [diff] [blame] | 1405 | call VerifyScreenDump(buf, 'Test_vim9_silent_echo', {}) |
| 1406 | |
| 1407 | " clean up |
| 1408 | call StopVimInTerminal(buf) |
| 1409 | call delete('XTest_silent_echo') |
Bram Moolenaar | 8f510af | 2020-07-05 18:48:23 +0200 | [diff] [blame] | 1410 | endfunc |
Bram Moolenaar | 47e7d70 | 2020-07-05 18:18:42 +0200 | [diff] [blame] | 1411 | |
Bram Moolenaar | 4b9bd69 | 2020-09-05 21:57:53 +0200 | [diff] [blame] | 1412 | """"""" builtin functions that behave differently in Vim9 |
Bram Moolenaar | e15eebd | 2020-08-18 19:11:38 +0200 | [diff] [blame] | 1413 | |
Bram Moolenaar | 4b9bd69 | 2020-09-05 21:57:53 +0200 | [diff] [blame] | 1414 | def Test_bufname() |
| 1415 | split SomeFile |
| 1416 | assert_equal('SomeFile', bufname('%')) |
| 1417 | edit OtherFile |
| 1418 | assert_equal('SomeFile', bufname('#')) |
| 1419 | close |
Bram Moolenaar | 191929b | 2020-08-19 21:20:49 +0200 | [diff] [blame] | 1420 | enddef |
| 1421 | |
Bram Moolenaar | a5d3841 | 2020-09-02 21:02:35 +0200 | [diff] [blame] | 1422 | def Test_bufwinid() |
| 1423 | let origwin = win_getid() |
| 1424 | below split SomeFile |
| 1425 | let SomeFileID = win_getid() |
| 1426 | below split OtherFile |
| 1427 | below split SomeFile |
| 1428 | assert_equal(SomeFileID, bufwinid('SomeFile')) |
| 1429 | |
| 1430 | win_gotoid(origwin) |
| 1431 | only |
| 1432 | bwipe SomeFile |
| 1433 | bwipe OtherFile |
| 1434 | enddef |
| 1435 | |
Bram Moolenaar | 4b9bd69 | 2020-09-05 21:57:53 +0200 | [diff] [blame] | 1436 | def Test_count() |
| 1437 | assert_equal(3, count('ABC ABC ABC', 'b', true)) |
| 1438 | assert_equal(0, count('ABC ABC ABC', 'b', false)) |
| 1439 | enddef |
| 1440 | |
| 1441 | def Test_expand() |
| 1442 | split SomeFile |
| 1443 | assert_equal(['SomeFile'], expand('%', true, true)) |
| 1444 | close |
| 1445 | enddef |
| 1446 | |
| 1447 | def Test_getbufinfo() |
| 1448 | let bufinfo = getbufinfo(bufnr()) |
| 1449 | assert_equal(bufinfo, getbufinfo('%')) |
| 1450 | |
| 1451 | edit Xtestfile1 |
| 1452 | hide edit Xtestfile2 |
| 1453 | hide enew |
| 1454 | getbufinfo(#{bufloaded: true, buflisted: true, bufmodified: false}) |
| 1455 | ->len()->assert_equal(3) |
| 1456 | bwipe Xtestfile1 Xtestfile2 |
| 1457 | enddef |
| 1458 | |
Bram Moolenaar | a5d3841 | 2020-09-02 21:02:35 +0200 | [diff] [blame] | 1459 | def Test_getbufline() |
| 1460 | e SomeFile |
| 1461 | let buf = bufnr() |
| 1462 | e # |
| 1463 | let lines = ['aaa', 'bbb', 'ccc'] |
| 1464 | setbufline(buf, 1, lines) |
| 1465 | assert_equal(lines, getbufline('#', 1, '$')) |
| 1466 | |
| 1467 | bwipe! |
| 1468 | enddef |
| 1469 | |
| 1470 | def Test_getchangelist() |
| 1471 | new |
| 1472 | setline(1, 'some text') |
| 1473 | let changelist = bufnr()->getchangelist() |
| 1474 | assert_equal(changelist, getchangelist('%')) |
| 1475 | bwipe! |
| 1476 | enddef |
| 1477 | |
Bram Moolenaar | c08cc72 | 2020-09-05 17:51:23 +0200 | [diff] [blame] | 1478 | def Test_getchar() |
Bram Moolenaar | 636c5d5 | 2020-09-05 18:48:57 +0200 | [diff] [blame] | 1479 | while getchar(0) |
| 1480 | endwhile |
Bram Moolenaar | c08cc72 | 2020-09-05 17:51:23 +0200 | [diff] [blame] | 1481 | assert_equal(0, getchar(true)) |
| 1482 | enddef |
| 1483 | |
Bram Moolenaar | d217a87 | 2020-09-05 18:31:33 +0200 | [diff] [blame] | 1484 | def Test_getcompletion() |
| 1485 | set wildignore=*.vim,*~ |
| 1486 | let l = getcompletion('run', 'file', true) |
| 1487 | assert_equal([], l) |
| 1488 | set wildignore& |
| 1489 | enddef |
| 1490 | |
Bram Moolenaar | 67ff97d | 2020-09-02 21:45:54 +0200 | [diff] [blame] | 1491 | def Test_getreg() |
| 1492 | let lines = ['aaa', 'bbb', 'ccc'] |
| 1493 | setreg('a', lines) |
| 1494 | assert_equal(lines, getreg('a', true, true)) |
| 1495 | enddef |
| 1496 | |
Bram Moolenaar | 5892ea1 | 2020-09-02 21:53:11 +0200 | [diff] [blame] | 1497 | def Test_glob() |
| 1498 | assert_equal(['runtest.vim'], glob('runtest.vim', true, true, true)) |
| 1499 | enddef |
| 1500 | |
Bram Moolenaar | f966ce5 | 2020-09-02 21:57:07 +0200 | [diff] [blame] | 1501 | def Test_globpath() |
| 1502 | assert_equal(['./runtest.vim'], globpath('.', 'runtest.vim', true, true, true)) |
| 1503 | enddef |
| 1504 | |
Bram Moolenaar | 4b9bd69 | 2020-09-05 21:57:53 +0200 | [diff] [blame] | 1505 | def Test_has() |
| 1506 | assert_equal(1, has('eval', true)) |
| 1507 | enddef |
| 1508 | |
Bram Moolenaar | 04d594b | 2020-09-02 22:25:35 +0200 | [diff] [blame] | 1509 | def Test_hasmapto() |
| 1510 | assert_equal(0, hasmapto('foobar', 'i', true)) |
| 1511 | iabbrev foo foobar |
| 1512 | assert_equal(1, hasmapto('foobar', 'i', true)) |
| 1513 | iunabbrev foo |
| 1514 | enddef |
| 1515 | |
Bram Moolenaar | 4b9bd69 | 2020-09-05 21:57:53 +0200 | [diff] [blame] | 1516 | def Test_index() |
| 1517 | assert_equal(3, index(['a', 'b', 'a', 'B'], 'b', 2, true)) |
| 1518 | enddef |
| 1519 | |
| 1520 | def Test_list2str_str2list_utf8() |
| 1521 | let s = "\u3042\u3044" |
| 1522 | let l = [0x3042, 0x3044] |
| 1523 | assert_equal(l, str2list(s, true)) |
| 1524 | assert_equal(s, list2str(l, true)) |
| 1525 | enddef |
| 1526 | |
Bram Moolenaar | 04d594b | 2020-09-02 22:25:35 +0200 | [diff] [blame] | 1527 | def SID(): number |
| 1528 | return expand('<SID>') |
| 1529 | ->matchstr('<SNR>\zs\d\+\ze_$') |
| 1530 | ->str2nr() |
| 1531 | enddef |
| 1532 | |
| 1533 | def Test_maparg() |
| 1534 | let lnum = str2nr(expand('<sflnum>')) |
| 1535 | map foo bar |
| 1536 | assert_equal(#{ |
| 1537 | lnum: lnum + 1, |
| 1538 | script: 0, |
| 1539 | mode: ' ', |
| 1540 | silent: 0, |
| 1541 | noremap: 0, |
| 1542 | lhs: 'foo', |
| 1543 | lhsraw: 'foo', |
| 1544 | nowait: 0, |
| 1545 | expr: 0, |
| 1546 | sid: SID(), |
| 1547 | rhs: 'bar', |
| 1548 | buffer: 0}, |
| 1549 | maparg('foo', '', false, true)) |
| 1550 | unmap foo |
| 1551 | enddef |
| 1552 | |
| 1553 | def Test_mapcheck() |
| 1554 | iabbrev foo foobar |
| 1555 | assert_equal('foobar', mapcheck('foo', 'i', true)) |
| 1556 | iunabbrev foo |
| 1557 | enddef |
| 1558 | |
Bram Moolenaar | 4b9bd69 | 2020-09-05 21:57:53 +0200 | [diff] [blame] | 1559 | def Test_nr2char() |
| 1560 | assert_equal('a', nr2char(97, true)) |
| 1561 | enddef |
| 1562 | |
| 1563 | def Test_readdir() |
| 1564 | eval expand('sautest')->readdir({e -> e[0] !=# '.'}) |
| 1565 | eval expand('sautest')->readdirex({e -> e.name[0] !=# '.'}) |
| 1566 | enddef |
| 1567 | |
| 1568 | def Test_search() |
| 1569 | new |
| 1570 | setline(1, ['foo', 'bar']) |
| 1571 | let val = 0 |
| 1572 | # skip expr returns boolean |
| 1573 | assert_equal(2, search('bar', 'W', 0, 0, {-> val == 1})) |
| 1574 | :1 |
| 1575 | assert_equal(0, search('bar', 'W', 0, 0, {-> val == 0})) |
| 1576 | # skip expr returns number, only 0 and 1 are accepted |
| 1577 | :1 |
| 1578 | assert_equal(2, search('bar', 'W', 0, 0, {-> 0})) |
| 1579 | :1 |
| 1580 | assert_equal(0, search('bar', 'W', 0, 0, {-> 1})) |
| 1581 | assert_fails("search('bar', '', 0, 0, {-> -1})", 'E1023:') |
| 1582 | assert_fails("search('bar', '', 0, 0, {-> -1})", 'E1023:') |
| 1583 | enddef |
| 1584 | |
| 1585 | def Test_searchcount() |
| 1586 | new |
| 1587 | setline(1, "foo bar") |
| 1588 | :/foo |
| 1589 | assert_equal(#{ |
| 1590 | exact_match: 1, |
| 1591 | current: 1, |
| 1592 | total: 1, |
| 1593 | maxcount: 99, |
| 1594 | incomplete: 0, |
| 1595 | }, searchcount(#{recompute: true})) |
| 1596 | bwipe! |
| 1597 | enddef |
| 1598 | |
| 1599 | def Test_searchdecl() |
| 1600 | assert_equal(1, searchdecl('blah', true, true)) |
| 1601 | enddef |
| 1602 | |
| 1603 | def Test_setbufvar() |
| 1604 | setbufvar(bufnr('%'), '&syntax', 'vim') |
| 1605 | assert_equal('vim', &syntax) |
| 1606 | setbufvar(bufnr('%'), '&ts', 16) |
| 1607 | assert_equal(16, &ts) |
| 1608 | settabwinvar(1, 1, '&syntax', 'vam') |
| 1609 | assert_equal('vam', &syntax) |
| 1610 | settabwinvar(1, 1, '&ts', 15) |
| 1611 | assert_equal(15, &ts) |
| 1612 | setlocal ts=8 |
| 1613 | |
| 1614 | setbufvar('%', 'myvar', 123) |
| 1615 | assert_equal(123, getbufvar('%', 'myvar')) |
| 1616 | enddef |
| 1617 | |
Bram Moolenaar | 401f0c0 | 2020-09-05 22:37:39 +0200 | [diff] [blame] | 1618 | def Test_setloclist() |
| 1619 | let items = [#{filename: '/tmp/file', lnum: 1, valid: true}] |
| 1620 | let what = #{items: items} |
| 1621 | setqflist([], ' ', what) |
| 1622 | setloclist(0, [], ' ', what) |
| 1623 | enddef |
| 1624 | |
Bram Moolenaar | 4b9bd69 | 2020-09-05 21:57:53 +0200 | [diff] [blame] | 1625 | def Test_setreg() |
| 1626 | setreg('a', ['aaa', 'bbb', 'ccc']) |
| 1627 | let reginfo = getreginfo('a') |
| 1628 | setreg('a', reginfo) |
| 1629 | assert_equal(reginfo, getreginfo('a')) |
| 1630 | enddef |
| 1631 | |
Bram Moolenaar | 7c27f33 | 2020-09-05 22:45:55 +0200 | [diff] [blame] | 1632 | def Test_spellsuggest() |
| 1633 | if !has('spell') |
| 1634 | MissingFeature 'spell' |
| 1635 | else |
| 1636 | spellsuggest('marrch', 1, true)->assert_equal(['March']) |
| 1637 | endif |
| 1638 | enddef |
| 1639 | |
Bram Moolenaar | 3986b94 | 2020-09-06 16:09:04 +0200 | [diff] [blame] | 1640 | def Test_split() |
| 1641 | split(' aa bb ', '\W\+', true)->assert_equal(['', 'aa', 'bb', '']) |
| 1642 | enddef |
| 1643 | |
| 1644 | def Test_str2nr() |
| 1645 | str2nr("1'000'000", 10, true)->assert_equal(1000000) |
| 1646 | enddef |
| 1647 | |
| 1648 | def Test_strchars() |
| 1649 | strchars("A\u20dd", true)->assert_equal(1) |
| 1650 | enddef |
| 1651 | |
Bram Moolenaar | ad30470 | 2020-09-06 18:22:53 +0200 | [diff] [blame] | 1652 | def Test_submatch() |
| 1653 | let pat = 'A\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)' |
| 1654 | let Rep = {-> range(10)->map({_, v -> submatch(v, true)})->string()} |
| 1655 | let actual = substitute('A123456789', pat, Rep, '') |
| 1656 | let expected = "[['A123456789'], ['1'], ['2'], ['3'], ['4'], ['5'], ['6'], ['7'], ['8'], ['9']]" |
| 1657 | assert_equal(expected, actual) |
| 1658 | enddef |
| 1659 | |
Bram Moolenaar | 4b9bd69 | 2020-09-05 21:57:53 +0200 | [diff] [blame] | 1660 | def Test_synID() |
| 1661 | new |
| 1662 | setline(1, "text") |
| 1663 | assert_equal(0, synID(1, 1, true)) |
| 1664 | bwipe! |
| 1665 | enddef |
| 1666 | |
Bram Moolenaar | ad30470 | 2020-09-06 18:22:53 +0200 | [diff] [blame] | 1667 | def Test_term_gettty() |
Bram Moolenaar | 63969ef | 2020-09-06 20:06:59 +0200 | [diff] [blame] | 1668 | if !has('terminal') |
| 1669 | MissingFeature 'terminal' |
| 1670 | else |
| 1671 | let buf = Run_shell_in_terminal({}) |
| 1672 | assert_notequal('', term_gettty(buf, true)) |
| 1673 | StopShellInTerminal(buf) |
| 1674 | endif |
Bram Moolenaar | ad30470 | 2020-09-06 18:22:53 +0200 | [diff] [blame] | 1675 | enddef |
| 1676 | |
| 1677 | def Test_term_start() |
Bram Moolenaar | 63969ef | 2020-09-06 20:06:59 +0200 | [diff] [blame] | 1678 | if !has('terminal') |
| 1679 | MissingFeature 'terminal' |
| 1680 | else |
| 1681 | botright new |
| 1682 | let winnr = winnr() |
| 1683 | term_start(&shell, #{curwin: true}) |
| 1684 | assert_equal(winnr, winnr()) |
| 1685 | bwipe! |
| 1686 | endif |
Bram Moolenaar | ad30470 | 2020-09-06 18:22:53 +0200 | [diff] [blame] | 1687 | enddef |
| 1688 | |
Bram Moolenaar | 418155d | 2020-09-06 18:39:38 +0200 | [diff] [blame] | 1689 | def Test_timer_paused() |
| 1690 | let id = timer_start(50, {-> 0}) |
| 1691 | timer_pause(id, true) |
| 1692 | let info = timer_info(id) |
| 1693 | assert_equal(1, info[0]['paused']) |
| 1694 | timer_stop(id) |
| 1695 | enddef |
| 1696 | |
Bram Moolenaar | 4b9bd69 | 2020-09-05 21:57:53 +0200 | [diff] [blame] | 1697 | def Test_win_splitmove() |
| 1698 | split |
| 1699 | win_splitmove(1, 2, #{vertical: true, rightbelow: true}) |
| 1700 | close |
| 1701 | enddef |
| 1702 | |
| 1703 | """"""" end of builtin functions |
| 1704 | |
| 1705 | def Fibonacci(n: number): number |
| 1706 | if n < 2 |
| 1707 | return n |
| 1708 | else |
| 1709 | return Fibonacci(n - 1) + Fibonacci(n - 2) |
| 1710 | endif |
| 1711 | enddef |
| 1712 | |
Bram Moolenaar | 985116a | 2020-07-12 17:31:09 +0200 | [diff] [blame] | 1713 | def Test_recursive_call() |
| 1714 | assert_equal(6765, Fibonacci(20)) |
| 1715 | enddef |
| 1716 | |
Bram Moolenaar | 08f7a41 | 2020-07-13 20:41:08 +0200 | [diff] [blame] | 1717 | def TreeWalk(dir: string): list<any> |
| 1718 | return readdir(dir)->map({_, val -> |
| 1719 | fnamemodify(dir .. '/' .. val, ':p')->isdirectory() |
Bram Moolenaar | bb1b5e2 | 2020-08-05 10:53:21 +0200 | [diff] [blame] | 1720 | ? {val: TreeWalk(dir .. '/' .. val)} |
Bram Moolenaar | 08f7a41 | 2020-07-13 20:41:08 +0200 | [diff] [blame] | 1721 | : val |
| 1722 | }) |
| 1723 | enddef |
| 1724 | |
| 1725 | def Test_closure_in_map() |
| 1726 | mkdir('XclosureDir/tdir', 'p') |
| 1727 | writefile(['111'], 'XclosureDir/file1') |
| 1728 | writefile(['222'], 'XclosureDir/file2') |
| 1729 | writefile(['333'], 'XclosureDir/tdir/file3') |
| 1730 | |
| 1731 | assert_equal(['file1', 'file2', {'tdir': ['file3']}], TreeWalk('XclosureDir')) |
| 1732 | |
| 1733 | delete('XclosureDir', 'rf') |
| 1734 | enddef |
| 1735 | |
Bram Moolenaar | a90afb9 | 2020-07-15 22:38:56 +0200 | [diff] [blame] | 1736 | def Test_partial_call() |
| 1737 | let Xsetlist = function('setloclist', [0]) |
| 1738 | Xsetlist([], ' ', {'title': 'test'}) |
| 1739 | assert_equal({'title': 'test'}, getloclist(0, {'title': 1})) |
| 1740 | |
| 1741 | Xsetlist = function('setloclist', [0, [], ' ']) |
| 1742 | Xsetlist({'title': 'test'}) |
| 1743 | assert_equal({'title': 'test'}, getloclist(0, {'title': 1})) |
| 1744 | |
| 1745 | Xsetlist = function('setqflist') |
| 1746 | Xsetlist([], ' ', {'title': 'test'}) |
| 1747 | assert_equal({'title': 'test'}, getqflist({'title': 1})) |
| 1748 | |
| 1749 | Xsetlist = function('setqflist', [[], ' ']) |
| 1750 | Xsetlist({'title': 'test'}) |
| 1751 | assert_equal({'title': 'test'}, getqflist({'title': 1})) |
| 1752 | enddef |
| 1753 | |
Bram Moolenaar | 2dd0a2c | 2020-08-08 15:10:27 +0200 | [diff] [blame] | 1754 | def Test_cmd_modifier() |
| 1755 | tab echo '0' |
Bram Moolenaar | d2c6170 | 2020-09-06 15:58:36 +0200 | [diff] [blame] | 1756 | CheckDefFailure(['5tab echo 3'], 'E16:') |
Bram Moolenaar | 2dd0a2c | 2020-08-08 15:10:27 +0200 | [diff] [blame] | 1757 | enddef |
| 1758 | |
| 1759 | def Test_restore_modifiers() |
| 1760 | # check that when compiling a :def function command modifiers are not messed |
| 1761 | # up. |
| 1762 | let lines =<< trim END |
| 1763 | vim9script |
| 1764 | set eventignore= |
| 1765 | autocmd QuickFixCmdPost * copen |
| 1766 | def AutocmdsDisabled() |
| 1767 | eval 0 |
| 1768 | enddef |
| 1769 | func Func() |
| 1770 | noautocmd call s:AutocmdsDisabled() |
| 1771 | let g:ei_after = &eventignore |
| 1772 | endfunc |
| 1773 | Func() |
| 1774 | END |
| 1775 | CheckScriptSuccess(lines) |
| 1776 | assert_equal('', g:ei_after) |
| 1777 | enddef |
| 1778 | |
Bram Moolenaar | dfa3d55 | 2020-09-10 22:05:08 +0200 | [diff] [blame^] | 1779 | def StackTop() |
| 1780 | eval 1 |
| 1781 | eval 2 |
| 1782 | # call not on fourth line |
| 1783 | StackBot() |
| 1784 | enddef |
| 1785 | |
| 1786 | def StackBot() |
| 1787 | # throw an error |
| 1788 | eval [][0] |
| 1789 | enddef |
| 1790 | |
| 1791 | def Test_callstack_def() |
| 1792 | try |
| 1793 | StackTop() |
| 1794 | catch |
| 1795 | assert_match('Test_callstack_def\[2\]..StackTop\[4\]..StackBot, line 2', v:throwpoint) |
| 1796 | endtry |
| 1797 | enddef |
| 1798 | |
Bram Moolenaar | f7779c6 | 2020-05-03 15:38:16 +0200 | [diff] [blame] | 1799 | |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 1800 | " vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker |