comparison src/testdir/test_vim9_class.vim @ 33385:b5ad84fdc702 v9.0.1951

patch 9.0.1951: Vim9: hard to debug vim9_class errors from CI Commit: https://github.com/vim/vim/commit/b90e3bc491c02bd6e4db1a1c1849ce3d21811c43 Author: Yegappan Lakshmanan <yegappan@yahoo.com> Date: Thu Sep 28 23:06:48 2023 +0200 patch 9.0.1951: Vim9: hard to debug vim9_class errors from CI Problem: Vim9: hard to debug vim9_class errors from CI Solution: Include the line number in assert_xxx() calls. Include the entire error message in the tests. Fix the indentation in the test file. Add tags for new error codes. closes: #13206 Signed-off-by: Christian Brabandt <cb@256bit.org> Co-authored-by: Yegappan Lakshmanan <yegappan@yahoo.com>
author Christian Brabandt <cb@256bit.org>
date Thu, 28 Sep 2023 23:30:03 +0200
parents 17301c641749
children 016d8f863230
comparison
equal deleted inserted replaced
33384:245624dcfea2 33385:b5ad84fdc702
4 import './vim9.vim' as v9 4 import './vim9.vim' as v9
5 5
6 def Test_class_basic() 6 def Test_class_basic()
7 # Class supported only in "vim9script" 7 # Class supported only in "vim9script"
8 var lines =<< trim END 8 var lines =<< trim END
9 class NotWorking 9 class NotWorking
10 endclass 10 endclass
11 END 11 END
12 v9.CheckSourceFailure(lines, 'E1316:') 12 v9.CheckSourceFailure(lines, 'E1316: Class can only be defined in Vim9 script', 1)
13 13
14 # First character in a class name should be capitalized. 14 # First character in a class name should be capitalized.
15 lines =<< trim END 15 lines =<< trim END
16 vim9script 16 vim9script
17 class notWorking 17 class notWorking
18 endclass 18 endclass
19 END 19 END
20 v9.CheckSourceFailure(lines, 'E1314:') 20 v9.CheckSourceFailure(lines, 'E1314: Class name must start with an uppercase letter: notWorking', 2)
21 21
22 # Only alphanumeric characters are supported in a class name 22 # Only alphanumeric characters are supported in a class name
23 lines =<< trim END 23 lines =<< trim END
24 vim9script 24 vim9script
25 class Not@working 25 class Not@working
26 endclass 26 endclass
27 END 27 END
28 v9.CheckSourceFailure(lines, 'E1315:') 28 v9.CheckSourceFailure(lines, 'E1315: White space required after name: Not@working', 2)
29 29
30 # Unsupported keyword (instead of class) 30 # Unsupported keyword (instead of class)
31 lines =<< trim END 31 lines =<< trim END
32 vim9script 32 vim9script
33 abstract noclass Something 33 abstract noclass Something
34 endclass 34 endclass
35 END 35 END
36 v9.CheckSourceFailure(lines, 'E475:') 36 v9.CheckSourceFailure(lines, 'E475: Invalid argument: noclass Something', 2)
37 37
38 # Only the completed word "class" should be recognized 38 # Only the completed word "class" should be recognized
39 lines =<< trim END 39 lines =<< trim END
40 vim9script 40 vim9script
41 abstract classy Something 41 abstract classy Something
42 endclass 42 endclass
43 END 43 END
44 v9.CheckSourceFailure(lines, 'E475:') 44 v9.CheckSourceFailure(lines, 'E475: Invalid argument: classy Something', 2)
45 45
46 # The complete "endclass" should be specified. 46 # The complete "endclass" should be specified.
47 lines =<< trim END 47 lines =<< trim END
48 vim9script 48 vim9script
49 class Something 49 class Something
50 endcl 50 endcl
51 END 51 END
52 v9.CheckSourceFailure(lines, 'E1065:') 52 v9.CheckSourceFailure(lines, 'E1065: Command cannot be shortened: endcl', 3)
53 53
54 # Additional words after "endclass" 54 # Additional words after "endclass"
55 lines =<< trim END 55 lines =<< trim END
56 vim9script 56 vim9script
57 class Something 57 class Something
58 endclass school's out 58 endclass school's out
59 END 59 END
60 v9.CheckSourceFailure(lines, 'E488:') 60 v9.CheckSourceFailure(lines, "E488: Trailing characters: school's out", 3)
61 61
62 # Additional commands after "endclass" 62 # Additional commands after "endclass"
63 lines =<< trim END 63 lines =<< trim END
64 vim9script 64 vim9script
65 class Something 65 class Something
66 endclass | echo 'done' 66 endclass | echo 'done'
67 END 67 END
68 v9.CheckSourceFailure(lines, 'E488:') 68 v9.CheckSourceFailure(lines, "E488: Trailing characters: | echo 'done'", 3)
69 69
70 # Use "this" without any member variable name 70 # Use "this" without any member variable name
71 lines =<< trim END 71 lines =<< trim END
72 vim9script 72 vim9script
73 class Something 73 class Something
74 this 74 this
75 endclass 75 endclass
76 END 76 END
77 v9.CheckSourceFailure(lines, 'E1317:') 77 v9.CheckSourceFailure(lines, 'E1317: Invalid object variable declaration: this', 3)
78 78
79 # Use "this." without any member variable name 79 # Use "this." without any member variable name
80 lines =<< trim END 80 lines =<< trim END
81 vim9script 81 vim9script
82 class Something 82 class Something
83 this. 83 this.
84 endclass 84 endclass
85 END 85 END
86 v9.CheckSourceFailure(lines, 'E1317:') 86 v9.CheckSourceFailure(lines, 'E1317: Invalid object variable declaration: this.', 3)
87 87
88 # Space between "this" and ".<variable>" 88 # Space between "this" and ".<variable>"
89 lines =<< trim END 89 lines =<< trim END
90 vim9script 90 vim9script
91 class Something 91 class Something
92 this .count 92 this .count
93 endclass 93 endclass
94 END 94 END
95 v9.CheckSourceFailure(lines, 'E1317:') 95 v9.CheckSourceFailure(lines, 'E1317: Invalid object variable declaration: this .count', 3)
96 96
97 # Space between "this." and the member variable name 97 # Space between "this." and the member variable name
98 lines =<< trim END 98 lines =<< trim END
99 vim9script 99 vim9script
100 class Something 100 class Something
101 this. count 101 this. count
102 endclass 102 endclass
103 END 103 END
104 v9.CheckSourceFailure(lines, 'E1317:') 104 v9.CheckSourceFailure(lines, 'E1317: Invalid object variable declaration: this. count', 3)
105 105
106 # Use "that" instead of "this" 106 # Use "that" instead of "this"
107 lines =<< trim END 107 lines =<< trim END
108 vim9script 108 vim9script
109 class Something 109 class Something
110 this.count: number 110 this.count: number
111 that.count 111 that.count
112 endclass 112 endclass
113 END 113 END
114 v9.CheckSourceFailure(lines, 'E1318: Not a valid command in a class: that.count') 114 v9.CheckSourceFailure(lines, 'E1318: Not a valid command in a class: that.count', 4)
115 115
116 # Member variable without a type or initialization 116 # Member variable without a type or initialization
117 lines =<< trim END 117 lines =<< trim END
118 vim9script 118 vim9script
119 class Something 119 class Something
120 this.count 120 this.count
121 endclass 121 endclass
122 END 122 END
123 v9.CheckSourceFailure(lines, 'E1022:') 123 v9.CheckSourceFailure(lines, 'E1022: Type or initialization required', 3)
124 124
125 # Use a non-existing member variable in new() 125 # Use a non-existing member variable in new()
126 lines =<< trim END 126 lines =<< trim END
127 vim9script 127 vim9script
128 class Something 128 class Something
129 def new() 129 def new()
130 this.state = 0 130 this.state = 0
131 enddef 131 enddef
132 endclass 132 endclass
133 var obj = Something.new() 133 var obj = Something.new()
134 END 134 END
135 v9.CheckSourceFailure(lines, 'E1326: Variable not found on object "Something": state') 135 v9.CheckSourceFailure(lines, 'E1326: Variable not found on object "Something": state', 1)
136 136
137 # Space before ":" in a member variable declaration 137 # Space before ":" in a member variable declaration
138 lines =<< trim END 138 lines =<< trim END
139 vim9script 139 vim9script
140 class Something 140 class Something
141 this.count : number 141 this.count : number
142 endclass 142 endclass
143 END 143 END
144 v9.CheckSourceFailure(lines, 'E1059:') 144 v9.CheckSourceFailure(lines, 'E1059: No white space allowed before colon: count : number', 3)
145 145
146 # No space after ":" in a member variable declaration 146 # No space after ":" in a member variable declaration
147 lines =<< trim END 147 lines =<< trim END
148 vim9script 148 vim9script
149 class Something 149 class Something
150 this.count:number 150 this.count:number
151 endclass 151 endclass
152 END 152 END
153 v9.CheckSourceFailure(lines, 'E1069:') 153 v9.CheckSourceFailure(lines, "E1069: White space required after ':'", 3)
154 154
155 # Test for unsupported comment specifier 155 # Test for unsupported comment specifier
156 lines =<< trim END 156 lines =<< trim END
157 vim9script 157 vim9script
158 class Something 158 class Something
159 # comment 159 # comment
160 #{ 160 #{
161 endclass 161 endclass
162 END 162 END
163 v9.CheckSourceFailure(lines, 'E1170:') 163 v9.CheckSourceFailure(lines, 'E1170: Cannot use #{ to start a comment', 3)
164 164
165 # Test for using class as a bool 165 # Test for using class as a bool
166 lines =<< trim END 166 lines =<< trim END
167 vim9script 167 vim9script
168 class A 168 class A
169 endclass 169 endclass
170 if A 170 if A
171 endif 171 endif
172 END 172 END
173 v9.CheckSourceFailure(lines, 'E1319: Using a class as a Number') 173 v9.CheckSourceFailure(lines, 'E1319: Using a class as a Number', 4)
174 174
175 # Test for using object as a bool 175 # Test for using object as a bool
176 lines =<< trim END 176 lines =<< trim END
177 vim9script 177 vim9script
178 class A 178 class A
179 endclass 179 endclass
180 var a = A.new() 180 var a = A.new()
181 if a 181 if a
182 endif 182 endif
183 END 183 END
184 v9.CheckSourceFailure(lines, 'E1320: Using an object as a Number') 184 v9.CheckSourceFailure(lines, 'E1320: Using an object as a Number', 5)
185 185
186 # Test for using class as a float 186 # Test for using class as a float
187 lines =<< trim END 187 lines =<< trim END
188 vim9script 188 vim9script
189 class A 189 class A
190 endclass 190 endclass
191 sort([1.1, A], 'f') 191 sort([1.1, A], 'f')
192 END 192 END
193 v9.CheckSourceFailure(lines, 'E1321: Using a class as a Float') 193 v9.CheckSourceFailure(lines, 'E1321: Using a class as a Float', 4)
194 194
195 # Test for using object as a float 195 # Test for using object as a float
196 lines =<< trim END 196 lines =<< trim END
197 vim9script 197 vim9script
198 class A 198 class A
199 endclass 199 endclass
200 var a = A.new() 200 var a = A.new()
201 sort([1.1, a], 'f') 201 sort([1.1, a], 'f')
202 END 202 END
203 v9.CheckSourceFailure(lines, 'E1322: Using an object as a Float') 203 v9.CheckSourceFailure(lines, 'E1322: Using an object as a Float', 5)
204 204
205 # Test for using class as a string 205 # Test for using class as a string
206 lines =<< trim END 206 lines =<< trim END
207 vim9script 207 vim9script
208 class A 208 class A
209 endclass 209 endclass
210 :exe 'call ' .. A 210 :exe 'call ' .. A
211 END 211 END
212 v9.CheckSourceFailure(lines, 'E1323: Using a class as a String') 212 v9.CheckSourceFailure(lines, 'E1323: Using a class as a String', 4)
213 213
214 # Test for using object as a string 214 # Test for using object as a string
215 lines =<< trim END 215 lines =<< trim END
216 vim9script 216 vim9script
217 class A 217 class A
218 endclass 218 endclass
219 var a = A.new() 219 var a = A.new()
220 :exe 'call ' .. a 220 :exe 'call ' .. a
221 END 221 END
222 v9.CheckSourceFailure(lines, 'E1324: Using an object as a String') 222 v9.CheckSourceFailure(lines, 'E1324: Using an object as a String', 5)
223 223
224 # Test creating a class with member variables and methods, calling a object 224 # Test creating a class with member variables and methods, calling a object
225 # method. Check for using type() and typename() with a class and an object. 225 # method. Check for using type() and typename() with a class and an object.
226 lines =<< trim END 226 lines =<< trim END
227 vim9script 227 vim9script
228 228
229 class TextPosition 229 class TextPosition
230 this.lnum: number 230 this.lnum: number
231 this.col: number 231 this.col: number
232 232
233 # make a nicely formatted string 233 # make a nicely formatted string
234 def ToString(): string 234 def ToString(): string
235 return $'({this.lnum}, {this.col})' 235 return $'({this.lnum}, {this.col})'
236 enddef 236 enddef
237 endclass 237 endclass
238 238
239 # use the automatically generated new() method 239 # use the automatically generated new() method
240 var pos = TextPosition.new(2, 12) 240 var pos = TextPosition.new(2, 12)
241 assert_equal(2, pos.lnum) 241 assert_equal(2, pos.lnum)
242 assert_equal(12, pos.col) 242 assert_equal(12, pos.col)
243 243
244 # call an object method 244 # call an object method
245 assert_equal('(2, 12)', pos.ToString()) 245 assert_equal('(2, 12)', pos.ToString())
246 246
247 assert_equal(v:t_class, type(TextPosition)) 247 assert_equal(v:t_class, type(TextPosition))
248 assert_equal(v:t_object, type(pos)) 248 assert_equal(v:t_object, type(pos))
249 assert_equal('class<TextPosition>', typename(TextPosition)) 249 assert_equal('class<TextPosition>', typename(TextPosition))
250 assert_equal('object<TextPosition>', typename(pos)) 250 assert_equal('object<TextPosition>', typename(pos))
251 END 251 END
252 v9.CheckSourceSuccess(lines) 252 v9.CheckSourceSuccess(lines)
253 253
254 # When referencing object methods, space cannot be used after a "." 254 # When referencing object methods, space cannot be used after a "."
255 lines =<< trim END 255 lines =<< trim END
260 enddef 260 enddef
261 endclass 261 endclass
262 var a = A.new() 262 var a = A.new()
263 var v = a. Foo() 263 var v = a. Foo()
264 END 264 END
265 v9.CheckSourceFailure(lines, 'E1202:') 265 v9.CheckSourceFailure(lines, "E1202: No white space allowed after '.'", 8)
266 266
267 # Using an object without specifying a method or a member variable 267 # Using an object without specifying a method or a member variable
268 lines =<< trim END 268 lines =<< trim END
269 vim9script 269 vim9script
270 class A 270 class A
273 enddef 273 enddef
274 endclass 274 endclass
275 var a = A.new() 275 var a = A.new()
276 var v = a. 276 var v = a.
277 END 277 END
278 v9.CheckSourceFailure(lines, 'E15:') 278 v9.CheckSourceFailure(lines, 'E15: Invalid expression: "a."', 8)
279 279
280 # Error when parsing the arguments of an object method. 280 # Error when parsing the arguments of an object method.
281 lines =<< trim END 281 lines =<< trim END
282 vim9script 282 vim9script
283 class A 283 class A
285 enddef 285 enddef
286 endclass 286 endclass
287 var a = A.new() 287 var a = A.new()
288 var v = a.Foo(,) 288 var v = a.Foo(,)
289 END 289 END
290 v9.CheckSourceFailure(lines, 'E15:') 290 v9.CheckSourceFailure(lines, 'E15: Invalid expression: "a.Foo(,)"', 7)
291 291
292 # Use a multi-line initialization for a member variable 292 # Use a multi-line initialization for a member variable
293 lines =<< trim END 293 lines =<< trim END
294 vim9script 294 vim9script
295 class A 295 class A
366 enddef 366 enddef
367 367
368 def Test_class_defined_twice() 368 def Test_class_defined_twice()
369 # class defined twice should fail 369 # class defined twice should fail
370 var lines =<< trim END 370 var lines =<< trim END
371 vim9script 371 vim9script
372 class There 372 class There
373 endclass 373 endclass
374 class There 374 class There
375 endclass 375 endclass
376 END 376 END
377 v9.CheckSourceFailure(lines, 'E1041: Redefining script item: "There"') 377 v9.CheckSourceFailure(lines, 'E1041: Redefining script item: "There"', 4)
378 378
379 # one class, reload same script twice is OK 379 # one class, reload same script twice is OK
380 lines =<< trim END 380 lines =<< trim END
381 vim9script 381 vim9script
382 class There 382 class There
383 endclass 383 endclass
384 END 384 END
385 writefile(lines, 'XclassTwice.vim', 'D') 385 writefile(lines, 'XclassTwice.vim', 'D')
386 source XclassTwice.vim 386 source XclassTwice.vim
387 source XclassTwice.vim 387 source XclassTwice.vim
388 enddef 388 enddef
389 389
390 def Test_returning_null_object() 390 def Test_returning_null_object()
391 # this was causing an internal error 391 # this was causing an internal error
392 var lines =<< trim END 392 var lines =<< trim END
393 vim9script 393 vim9script
394 394
395 class BufferList 395 class BufferList
396 def Current(): any 396 def Current(): any
397 return null_object 397 return null_object
398 enddef 398 enddef
399 endclass 399 endclass
400 400
401 var buffers = BufferList.new() 401 var buffers = BufferList.new()
402 echo buffers.Current() 402 echo buffers.Current()
403 END 403 END
404 v9.CheckSourceSuccess(lines) 404 v9.CheckSourceSuccess(lines)
405 enddef 405 enddef
406 406
407 def Test_using_null_class() 407 def Test_using_null_class()
408 var lines =<< trim END 408 var lines =<< trim END
409 @_ = null_class.member 409 @_ = null_class.member
410 END 410 END
411 v9.CheckDefExecAndScriptFailure(lines, ['E715:', 'E1363:']) 411 v9.CheckDefExecAndScriptFailure(lines, ['E715: Dictionary required', 'E1363: Incomplete type'])
412 enddef 412 enddef
413 413
414 def Test_class_interface_wrong_end() 414 def Test_class_interface_wrong_end()
415 var lines =<< trim END 415 var lines =<< trim END
416 vim9script 416 vim9script
417 abstract class SomeName 417 abstract class SomeName
418 this.member = 'text' 418 this.member = 'text'
419 endinterface 419 endinterface
420 END 420 END
421 v9.CheckSourceFailure(lines, 'E476: Invalid command: endinterface, expected endclass') 421 v9.CheckSourceFailure(lines, 'E476: Invalid command: endinterface, expected endclass', 4)
422 422
423 lines =<< trim END 423 lines =<< trim END
424 vim9script 424 vim9script
425 export interface AnotherName 425 export interface AnotherName
426 this.member: string 426 this.member: string
427 endclass 427 endclass
428 END 428 END
429 v9.CheckSourceFailure(lines, 'E476: Invalid command: endclass, expected endinterface') 429 v9.CheckSourceFailure(lines, 'E476: Invalid command: endclass, expected endinterface', 4)
430 enddef 430 enddef
431 431
432 def Test_object_not_set() 432 def Test_object_not_set()
433 # Use an uninitialized object in script context 433 # Use an uninitialized object in script context
434 var lines =<< trim END 434 var lines =<< trim END
435 vim9script 435 vim9script
436 436
437 class State 437 class State
438 this.value = 'xyz' 438 this.value = 'xyz'
439 endclass 439 endclass
440 440
441 var state: State 441 var state: State
442 var db = {'xyz': 789} 442 var db = {'xyz': 789}
443 echo db[state.value] 443 echo db[state.value]
444 END 444 END
445 v9.CheckSourceFailure(lines, 'E1360:') 445 v9.CheckSourceFailure(lines, 'E1360: Using a null object', 9)
446 446
447 # Use an uninitialized object from a def function 447 # Use an uninitialized object from a def function
448 lines =<< trim END 448 lines =<< trim END
449 vim9script 449 vim9script
450 450
451 class Class 451 class Class
452 this.id: string 452 this.id: string
453 def Method1() 453 def Method1()
454 echo 'Method1' .. this.id 454 echo 'Method1' .. this.id
455 enddef 455 enddef
456 endclass 456 endclass
457 457
458 var obj: Class 458 var obj: Class
459 def Func() 459 def Func()
460 obj.Method1() 460 obj.Method1()
461 enddef 461 enddef
462 Func() 462 Func()
463 END 463 END
464 v9.CheckSourceFailure(lines, 'E1360:') 464 v9.CheckSourceFailure(lines, 'E1360: Using a null object', 1)
465 465
466 # Pass an uninitialized object variable to a "new" function and try to call an 466 # Pass an uninitialized object variable to a "new" function and try to call an
467 # object method. 467 # object method.
468 lines =<< trim END 468 lines =<< trim END
469 vim9script 469 vim9script
470 470
471 class Background 471 class Background
472 this.background = 'dark' 472 this.background = 'dark'
473 endclass 473 endclass
474 474
475 class Colorscheme 475 class Colorscheme
476 this._bg: Background 476 this._bg: Background
477 477
478 def GetBackground(): string 478 def GetBackground(): string
479 return this._bg.background 479 return this._bg.background
480 enddef 480 enddef
481 endclass 481 endclass
482 482
483 var bg: Background # UNINITIALIZED 483 var bg: Background # UNINITIALIZED
484 echo Colorscheme.new(bg).GetBackground() 484 echo Colorscheme.new(bg).GetBackground()
485 END 485 END
486 v9.CheckSourceFailure(lines, 'E1360:') 486 v9.CheckSourceFailure(lines, 'E1360: Using a null object', 1)
487 487
488 # TODO: this should not give an error but be handled at runtime 488 # TODO: this should not give an error but be handled at runtime
489 lines =<< trim END 489 lines =<< trim END
490 vim9script 490 vim9script
491 491
492 class Class 492 class Class
493 this.id: string 493 this.id: string
494 def Method1() 494 def Method1()
495 echo 'Method1' .. this.id 495 echo 'Method1' .. this.id
496 enddef 496 enddef
497 endclass 497 endclass
498 498
499 var obj = null_object 499 var obj = null_object
500 def Func() 500 def Func()
501 obj.Method1() 501 obj.Method1()
502 enddef 502 enddef
503 Func() 503 Func()
504 END 504 END
505 v9.CheckSourceFailure(lines, 'E1363:') 505 v9.CheckSourceFailure(lines, 'E1363: Incomplete type', 1)
506 enddef 506 enddef
507 507
508 " Null object assignment and comparison 508 " Null object assignment and comparison
509 def Test_null_object_assign_compare() 509 def Test_null_object_assign_compare()
510 var lines =<< trim END 510 var lines =<< trim END
547 enddef 547 enddef
548 548
549 " Test for object member initialization and disassembly 549 " Test for object member initialization and disassembly
550 def Test_class_member_initializer() 550 def Test_class_member_initializer()
551 var lines =<< trim END 551 var lines =<< trim END
552 vim9script 552 vim9script
553 553
554 class TextPosition 554 class TextPosition
555 this.lnum: number = 1 555 this.lnum: number = 1
556 this.col: number = 1 556 this.col: number = 1
557 557
558 # constructor with only the line number 558 # constructor with only the line number
559 def new(lnum: number) 559 def new(lnum: number)
560 this.lnum = lnum 560 this.lnum = lnum
561 enddef 561 enddef
562 endclass 562 endclass
563 563
564 var pos = TextPosition.new(3) 564 var pos = TextPosition.new(3)
565 assert_equal(3, pos.lnum) 565 assert_equal(3, pos.lnum)
566 assert_equal(1, pos.col) 566 assert_equal(1, pos.col)
567 567
568 var instr = execute('disassemble TextPosition.new') 568 var instr = execute('disassemble TextPosition.new')
569 assert_match('new\_s*' .. 569 assert_match('new\_s*' ..
570 '0 NEW TextPosition size \d\+\_s*' .. 570 '0 NEW TextPosition size \d\+\_s*' ..
571 '\d PUSHNR 1\_s*' .. 571 '\d PUSHNR 1\_s*' ..
572 '\d STORE_THIS 0\_s*' .. 572 '\d STORE_THIS 0\_s*' ..
573 '\d PUSHNR 1\_s*' .. 573 '\d PUSHNR 1\_s*' ..
574 '\d STORE_THIS 1\_s*' .. 574 '\d STORE_THIS 1\_s*' ..
575 'this.lnum = lnum\_s*' .. 575 'this.lnum = lnum\_s*' ..
576 '\d LOAD arg\[-1]\_s*' .. 576 '\d LOAD arg\[-1]\_s*' ..
577 '\d PUSHNR 0\_s*' .. 577 '\d PUSHNR 0\_s*' ..
578 '\d LOAD $0\_s*' .. 578 '\d LOAD $0\_s*' ..
579 '\d\+ STOREINDEX object\_s*' .. 579 '\d\+ STOREINDEX object\_s*' ..
580 '\d\+ RETURN object.*', 580 '\d\+ RETURN object.*',
581 instr) 581 instr)
582 END 582 END
583 v9.CheckSourceSuccess(lines) 583 v9.CheckSourceSuccess(lines)
584 enddef 584 enddef
585 585
586 def Test_member_any_used_as_object() 586 def Test_member_any_used_as_object()
587 var lines =<< trim END 587 var lines =<< trim END
588 vim9script 588 vim9script
589 589
590 class Inner 590 class Inner
591 this.value: number = 0 591 this.value: number = 0
592 endclass 592 endclass
593 593
594 class Outer 594 class Outer
595 this.inner: any 595 this.inner: any
596 endclass 596 endclass
597 597
598 def F(outer: Outer) 598 def F(outer: Outer)
599 outer.inner.value = 1 599 outer.inner.value = 1
600 enddef 600 enddef
601 601
602 var inner_obj = Inner.new(0) 602 var inner_obj = Inner.new(0)
603 var outer_obj = Outer.new(inner_obj) 603 var outer_obj = Outer.new(inner_obj)
604 F(outer_obj) 604 F(outer_obj)
605 assert_equal(1, inner_obj.value) 605 assert_equal(1, inner_obj.value)
606 END 606 END
607 v9.CheckSourceSuccess(lines) 607 v9.CheckSourceSuccess(lines)
608 608
609 # Try modifying a private variable using an "any" object 609 # Try modifying a private variable using an "any" object
610 lines =<< trim END 610 lines =<< trim END
624 624
625 var inner_obj = Inner.new('a') 625 var inner_obj = Inner.new('a')
626 var outer_obj = Outer.new(inner_obj) 626 var outer_obj = Outer.new(inner_obj)
627 F(outer_obj) 627 F(outer_obj)
628 END 628 END
629 v9.CheckSourceFailure(lines, 'E1333: Cannot access private variable: _value') 629 v9.CheckSourceFailure(lines, 'E1333: Cannot access private variable: _value', 1)
630 630
631 # Try modifying a non-existing variable using an "any" object 631 # Try modifying a non-existing variable using an "any" object
632 lines =<< trim END 632 lines =<< trim END
633 vim9script 633 vim9script
634 634
646 646
647 var inner_obj = Inner.new('a') 647 var inner_obj = Inner.new('a')
648 var outer_obj = Outer.new(inner_obj) 648 var outer_obj = Outer.new(inner_obj)
649 F(outer_obj) 649 F(outer_obj)
650 END 650 END
651 v9.CheckSourceFailure(lines, 'E1326: Variable not found on object "Inner": someval') 651 v9.CheckSourceFailure(lines, 'E1326: Variable not found on object "Inner": someval', 1)
652 enddef 652 enddef
653 653
654 " Nested assignment to a object variable which is of another class type 654 " Nested assignment to a object variable which is of another class type
655 def Test_assignment_nested_type() 655 def Test_assignment_nested_type()
656 var lines =<< trim END 656 var lines =<< trim END
707 assert_equal(1, inner.value) 707 assert_equal(1, inner.value)
708 enddef 708 enddef
709 709
710 Test_assign_to_nested_typed_member() 710 Test_assign_to_nested_typed_member()
711 END 711 END
712 v9.CheckSourceFailure(lines, 'E1335: Variable "value" in class "Inner" is not writable') 712 v9.CheckSourceFailure(lines, 'E1335: Variable "value" in class "Inner" is not writable', 1)
713 713
714 # Assignment where target item is read only script level 714 # Assignment where target item is read only script level
715 lines =<< trim END 715 lines =<< trim END
716 vim9script 716 vim9script
717 717
730 var script_inner = Inner.new(0) 730 var script_inner = Inner.new(0)
731 var script_outer = Outer.new(script_inner) 731 var script_outer = Outer.new(script_inner)
732 script_outer.inner.value = 1 732 script_outer.inner.value = 1
733 assert_equal(1, script_inner.value) 733 assert_equal(1, script_inner.value)
734 END 734 END
735 v9.CheckSourceFailure(lines, 'E1335: Variable "value" in class "Inner" is not writable') 735 v9.CheckSourceFailure(lines, 'E1335: Variable "value" in class "Inner" is not writable', 17)
736 enddef 736 enddef
737 737
738 def Test_assignment_with_operator() 738 def Test_assignment_with_operator()
739 # Use "+=" to assign to a object variable 739 # Use "+=" to assign to a object variable
740 var lines =<< trim END 740 var lines =<< trim END
741 vim9script 741 vim9script
742 742
743 class Foo 743 class Foo
744 public this.x: number 744 public this.x: number
745 745
746 def Add(n: number) 746 def Add(n: number)
747 this.x += n 747 this.x += n
748 enddef 748 enddef
749 endclass 749 endclass
750 750
751 var f = Foo.new(3) 751 var f = Foo.new(3)
752 f.Add(17) 752 f.Add(17)
753 assert_equal(20, f.x) 753 assert_equal(20, f.x)
754 754
755 def AddToFoo(obj: Foo) 755 def AddToFoo(obj: Foo)
756 obj.x += 3 756 obj.x += 3
757 enddef 757 enddef
758 758
759 AddToFoo(f) 759 AddToFoo(f)
760 assert_equal(23, f.x) 760 assert_equal(23, f.x)
761 END 761 END
762 v9.CheckSourceSuccess(lines) 762 v9.CheckSourceSuccess(lines)
763 enddef 763 enddef
764 764
765 def Test_list_of_objects() 765 def Test_list_of_objects()
766 var lines =<< trim END 766 var lines =<< trim END
767 vim9script 767 vim9script
768 768
769 class Foo 769 class Foo
770 def Add() 770 def Add()
771 enddef 771 enddef
772 endclass 772 endclass
773 773
774 def ProcessList(fooList: list<Foo>) 774 def ProcessList(fooList: list<Foo>)
775 for foo in fooList 775 for foo in fooList
776 foo.Add() 776 foo.Add()
777 endfor 777 endfor
778 enddef 778 enddef
779 779
780 var l: list<Foo> = [Foo.new()] 780 var l: list<Foo> = [Foo.new()]
781 ProcessList(l) 781 ProcessList(l)
782 END 782 END
783 v9.CheckSourceSuccess(lines) 783 v9.CheckSourceSuccess(lines)
784 enddef 784 enddef
785 785
786 def Test_expr_after_using_object() 786 def Test_expr_after_using_object()
787 var lines =<< trim END 787 var lines =<< trim END
788 vim9script 788 vim9script
789 789
790 class Something 790 class Something
791 this.label: string = '' 791 this.label: string = ''
792 endclass 792 endclass
793 793
794 def Foo(): Something 794 def Foo(): Something
795 var v = Something.new() 795 var v = Something.new()
796 echo 'in Foo(): ' .. typename(v) 796 echo 'in Foo(): ' .. typename(v)
797 return v 797 return v
798 enddef 798 enddef
799 799
800 Foo() 800 Foo()
801 END 801 END
802 v9.CheckSourceSuccess(lines) 802 v9.CheckSourceSuccess(lines)
803 enddef 803 enddef
804 804
805 def Test_class_default_new() 805 def Test_class_default_new()
806 var lines =<< trim END 806 var lines =<< trim END
807 vim9script 807 vim9script
808 808
809 class TextPosition 809 class TextPosition
810 this.lnum: number = 1 810 this.lnum: number = 1
811 this.col: number = 1 811 this.col: number = 1
812 endclass 812 endclass
813 813
814 var pos = TextPosition.new() 814 var pos = TextPosition.new()
815 assert_equal(1, pos.lnum) 815 assert_equal(1, pos.lnum)
816 assert_equal(1, pos.col) 816 assert_equal(1, pos.col)
817 817
818 pos = TextPosition.new(v:none, v:none) 818 pos = TextPosition.new(v:none, v:none)
819 assert_equal(1, pos.lnum) 819 assert_equal(1, pos.lnum)
820 assert_equal(1, pos.col) 820 assert_equal(1, pos.col)
821 821
822 pos = TextPosition.new(3, 22) 822 pos = TextPosition.new(3, 22)
823 assert_equal(3, pos.lnum) 823 assert_equal(3, pos.lnum)
824 assert_equal(22, pos.col) 824 assert_equal(22, pos.col)
825 825
826 pos = TextPosition.new(v:none, 33) 826 pos = TextPosition.new(v:none, 33)
827 assert_equal(1, pos.lnum) 827 assert_equal(1, pos.lnum)
828 assert_equal(33, pos.col) 828 assert_equal(33, pos.col)
829 END 829 END
830 v9.CheckSourceSuccess(lines) 830 v9.CheckSourceSuccess(lines)
831 831
832 lines =<< trim END 832 lines =<< trim END
833 vim9script 833 vim9script
834 class Person 834 class Person
835 this.name: string 835 this.name: string
836 this.age: number = 42 836 this.age: number = 42
837 this.education: string = "unknown" 837 this.education: string = "unknown"
838 838
839 def new(this.name, this.age = v:none, this.education = v:none) 839 def new(this.name, this.age = v:none, this.education = v:none)
840 enddef 840 enddef
841 endclass 841 endclass
842 842
843 var piet = Person.new("Piet") 843 var piet = Person.new("Piet")
844 assert_equal("Piet", piet.name) 844 assert_equal("Piet", piet.name)
845 assert_equal(42, piet.age) 845 assert_equal(42, piet.age)
846 assert_equal("unknown", piet.education) 846 assert_equal("unknown", piet.education)
847 847
848 var chris = Person.new("Chris", 4, "none") 848 var chris = Person.new("Chris", 4, "none")
849 assert_equal("Chris", chris.name) 849 assert_equal("Chris", chris.name)
850 assert_equal(4, chris.age) 850 assert_equal(4, chris.age)
851 assert_equal("none", chris.education) 851 assert_equal("none", chris.education)
852 END 852 END
853 v9.CheckSourceSuccess(lines) 853 v9.CheckSourceSuccess(lines)
854 854
855 lines =<< trim END 855 lines =<< trim END
856 vim9script 856 vim9script
857 class Person 857 class Person
858 this.name: string 858 this.name: string
859 this.age: number = 42 859 this.age: number = 42
860 this.education: string = "unknown" 860 this.education: string = "unknown"
861 861
862 def new(this.name, this.age = v:none, this.education = v:none) 862 def new(this.name, this.age = v:none, this.education = v:none)
863 enddef 863 enddef
864 endclass 864 endclass
865 865
866 var missing = Person.new() 866 var missing = Person.new()
867 END 867 END
868 v9.CheckSourceFailure(lines, 'E119:') 868 v9.CheckSourceFailure(lines, 'E119: Not enough arguments for function: new', 11)
869 869
870 # Using a specific value to initialize an instance variable in the new() 870 # Using a specific value to initialize an instance variable in the new()
871 # method. 871 # method.
872 lines =<< trim END 872 lines =<< trim END
873 vim9script 873 vim9script
874 class A 874 class A
875 this.val: string 875 this.val: string
876 def new(this.val = 'a') 876 def new(this.val = 'a')
877 enddef 877 enddef
878 endclass 878 endclass
879 END 879 END
880 v9.CheckSourceFailure(lines, "E1328: Constructor default value must be v:none: = 'a'") 880 v9.CheckSourceFailure(lines, "E1328: Constructor default value must be v:none: = 'a'", 4)
881 enddef 881 enddef
882 882
883 def Test_class_new_with_object_member() 883 def Test_class_new_with_object_member()
884 var lines =<< trim END 884 var lines =<< trim END
885 vim9script
886
887 class C
888 this.str: string
889 this.num: number
890 def new(this.str, this.num)
891 enddef
892 def newVals(this.str, this.num)
893 enddef
894 endclass
895
896 def Check()
897 try
898 var c = C.new('cats', 2)
899 assert_equal('cats', c.str)
900 assert_equal(2, c.num)
901
902 c = C.newVals('dogs', 4)
903 assert_equal('dogs', c.str)
904 assert_equal(4, c.num)
905 catch
906 assert_report($'Unexpected exception was caught: {v:exception}')
907 endtry
908 enddef
909
910 Check()
911 END
912 v9.CheckSourceSuccess(lines)
913
914 lines =<< trim END
915 vim9script
916
917 class C
918 this.str: string
919 this.num: number
920 def new(this.str, this.num)
921 enddef
922 endclass
923
924 def Check()
925 try
926 var c = C.new(1, 2)
927 catch
928 assert_report($'Unexpected exception was caught: {v:exception}')
929 endtry
930 enddef
931
932 Check()
933 END
934 v9.CheckSourceFailure(lines, 'E1013:')
935
936 lines =<< trim END
937 vim9script
938
939 class C
940 this.str: string
941 this.num: number
942 def newVals(this.str, this.num)
943 enddef
944 endclass
945
946 def Check()
947 try
948 var c = C.newVals('dogs', 'apes')
949 catch
950 assert_report($'Unexpected exception was caught: {v:exception}')
951 endtry
952 enddef
953
954 Check()
955 END
956 v9.CheckSourceFailure(lines, 'E1013:')
957
958 lines =<< trim END
959 vim9script 885 vim9script
960 886
961 class C 887 class C
962 this.str: string 888 this.str: string
963 def MethodA(this.str) 889 this.num: number
964 enddef 890 def new(this.str, this.num)
965 endclass 891 enddef
966 END 892 def newVals(this.str, this.num)
967 v9.CheckSourceFailure(lines, 'E1390: Cannot use an object variable "this.str" except with the "new" method') 893 enddef
968 894 endclass
969 lines =<< trim END 895
970 vim9script 896 def Check()
971 897 try
972 class C 898 var c = C.new('cats', 2)
973 this.str: string 899 assert_equal('cats', c.str)
974 def new(str: any) 900 assert_equal(2, c.num)
975 enddef 901
976 endclass 902 c = C.newVals('dogs', 4)
977 903 assert_equal('dogs', c.str)
978 def Check() 904 assert_equal(4, c.num)
979 try 905 catch
980 var c = C.new(1) 906 assert_report($'Unexpected exception was caught: {v:exception}')
981 catch 907 endtry
982 assert_report($'Unexpected exception was caught: {v:exception}') 908 enddef
983 endtry 909
984 enddef 910 Check()
985 911 END
986 Check() 912 v9.CheckSourceSuccess(lines)
913
914 lines =<< trim END
915 vim9script
916
917 class C
918 this.str: string
919 this.num: number
920 def new(this.str, this.num)
921 enddef
922 endclass
923
924 def Check()
925 try
926 var c = C.new(1, 2)
927 catch
928 assert_report($'Unexpected exception was caught: {v:exception}')
929 endtry
930 enddef
931
932 Check()
933 END
934 v9.CheckSourceFailure(lines, 'E1013: Argument 1: type mismatch, expected string but got number', 2)
935
936 lines =<< trim END
937 vim9script
938
939 class C
940 this.str: string
941 this.num: number
942 def newVals(this.str, this.num)
943 enddef
944 endclass
945
946 def Check()
947 try
948 var c = C.newVals('dogs', 'apes')
949 catch
950 assert_report($'Unexpected exception was caught: {v:exception}')
951 endtry
952 enddef
953
954 Check()
955 END
956 v9.CheckSourceFailure(lines, 'E1013: Argument 2: type mismatch, expected number but got string', 2)
957
958 lines =<< trim END
959 vim9script
960
961 class C
962 this.str: string
963 def new(str: any)
964 enddef
965 endclass
966
967 def Check()
968 try
969 var c = C.new(1)
970 catch
971 assert_report($'Unexpected exception was caught: {v:exception}')
972 endtry
973 enddef
974
975 Check()
987 END 976 END
988 v9.CheckSourceSuccess(lines) 977 v9.CheckSourceSuccess(lines)
989 enddef 978 enddef
990 979
991 def Test_class_object_member_inits() 980 def Test_class_object_member_inits()
992 var lines =<< trim END 981 var lines =<< trim END
993 vim9script 982 vim9script
994 class TextPosition 983 class TextPosition
995 this.lnum: number 984 this.lnum: number
996 this.col = 1 985 this.col = 1
997 this.addcol: number = 2 986 this.addcol: number = 2
998 endclass 987 endclass
999 988
1000 var pos = TextPosition.new() 989 var pos = TextPosition.new()
1001 assert_equal(0, pos.lnum) 990 assert_equal(0, pos.lnum)
1002 assert_equal(1, pos.col) 991 assert_equal(1, pos.col)
1003 assert_equal(2, pos.addcol) 992 assert_equal(2, pos.addcol)
1004 END 993 END
1005 v9.CheckSourceSuccess(lines) 994 v9.CheckSourceSuccess(lines)
1006 995
1007 lines =<< trim END 996 lines =<< trim END
1008 vim9script 997 vim9script
1009 class TextPosition 998 class TextPosition
1010 this.lnum 999 this.lnum
1011 this.col = 1 1000 this.col = 1
1012 endclass 1001 endclass
1013 END 1002 END
1014 v9.CheckSourceFailure(lines, 'E1022:') 1003 v9.CheckSourceFailure(lines, 'E1022: Type or initialization required', 3)
1015 1004
1016 # If the type is not specified for a member, then it should be set during 1005 # If the type is not specified for a member, then it should be set during
1017 # object creation and not when defining the class. 1006 # object creation and not when defining the class.
1018 lines =<< trim END 1007 lines =<< trim END
1019 vim9script 1008 vim9script
1020 1009
1021 var init_count = 0 1010 var init_count = 0
1022 def Init(): string 1011 def Init(): string
1023 init_count += 1 1012 init_count += 1
1024 return 'foo' 1013 return 'foo'
1025 enddef 1014 enddef
1026 1015
1027 class A 1016 class A
1028 this.str1 = Init() 1017 this.str1 = Init()
1029 this.str2: string = Init() 1018 this.str2: string = Init()
1030 this.col = 1 1019 this.col = 1
1031 endclass 1020 endclass
1032 1021
1033 assert_equal(init_count, 0) 1022 assert_equal(init_count, 0)
1034 var a = A.new() 1023 var a = A.new()
1035 assert_equal(init_count, 2) 1024 assert_equal(init_count, 2)
1036 END 1025 END
1037 v9.CheckSourceSuccess(lines) 1026 v9.CheckSourceSuccess(lines)
1038 1027
1039 # Test for initializing an object member with an unknown variable/type 1028 # Test for initializing an object member with an unknown variable/type
1040 lines =<< trim END 1029 lines =<< trim END
1042 class A 1031 class A
1043 this.value = init_val 1032 this.value = init_val
1044 endclass 1033 endclass
1045 var a = A.new() 1034 var a = A.new()
1046 END 1035 END
1047 v9.CheckSourceFailure(lines, 'E1001:') 1036 v9.CheckSourceFailure(lines, 'E1001: Variable not found: init_val', 1)
1048 1037
1049 # Test for initializing an object member with an special type 1038 # Test for initializing an object member with an special type
1050 lines =<< trim END 1039 lines =<< trim END
1051 vim9script 1040 vim9script
1052 class A 1041 class A
1053 this.value: void 1042 this.value: void
1054 endclass 1043 endclass
1055 END 1044 END
1056 v9.CheckSourceFailure(lines, 'E1330: Invalid type for object variable: void') 1045 v9.CheckSourceFailure(lines, 'E1330: Invalid type for object variable: void', 3)
1057 enddef 1046 enddef
1058 1047
1059 " Test for instance variable access 1048 " Test for instance variable access
1060 def Test_instance_variable_access() 1049 def Test_instance_variable_access()
1061 var lines =<< trim END 1050 var lines =<< trim END
1062 vim9script 1051 vim9script
1063 class Triple 1052 class Triple
1064 this._one = 1 1053 this._one = 1
1065 this.two = 2 1054 this.two = 2
1066 public this.three = 3 1055 public this.three = 3
1067 1056
1068 def GetOne(): number 1057 def GetOne(): number
1069 return this._one 1058 return this._one
1070 enddef 1059 enddef
1071 endclass 1060 endclass
1072 1061
1073 var trip = Triple.new() 1062 var trip = Triple.new()
1074 assert_equal(1, trip.GetOne()) 1063 assert_equal(1, trip.GetOne())
1075 assert_equal(2, trip.two) 1064 assert_equal(2, trip.two)
1076 assert_equal(3, trip.three) 1065 assert_equal(3, trip.three)
1077 assert_fails('echo trip._one', 'E1333') 1066 assert_fails('echo trip._one', 'E1333: Cannot access private variable: _one')
1078 1067
1079 assert_fails('trip._one = 11', 'E1333') 1068 assert_fails('trip._one = 11', 'E1333: Cannot access private variable: _one')
1080 assert_fails('trip.two = 22', 'E1335') 1069 assert_fails('trip.two = 22', 'E1335: Variable "two" in class "Triple" is not writable')
1081 trip.three = 33 1070 trip.three = 33
1082 assert_equal(33, trip.three) 1071 assert_equal(33, trip.three)
1083 1072
1084 assert_fails('trip.four = 4', 'E1326') 1073 assert_fails('trip.four = 4', 'E1326: Variable not found on object "Triple": four')
1085 END 1074 END
1086 v9.CheckSourceSuccess(lines) 1075 v9.CheckSourceSuccess(lines)
1087 1076
1088 # Test for a public member variable name beginning with an underscore 1077 # Test for a public member variable name beginning with an underscore
1089 lines =<< trim END 1078 lines =<< trim END
1090 vim9script 1079 vim9script
1091 class A 1080 class A
1092 public this._val = 10 1081 public this._val = 10
1093 endclass 1082 endclass
1094 END 1083 END
1095 v9.CheckSourceFailure(lines, 'E1332:') 1084 v9.CheckSourceFailure(lines, 'E1332: Public variable name cannot start with underscore: public this._val = 10', 3)
1096 1085
1097 lines =<< trim END 1086 lines =<< trim END
1098 vim9script 1087 vim9script
1099 1088
1100 class MyCar 1089 class MyCar
1101 this.make: string 1090 this.make: string
1102 this.age = 5 1091 this.age = 5
1103 1092
1104 def new(make_arg: string) 1093 def new(make_arg: string)
1105 this.make = make_arg 1094 this.make = make_arg
1106 enddef 1095 enddef
1107 1096
1108 def GetMake(): string 1097 def GetMake(): string
1109 return $"make = {this.make}" 1098 return $"make = {this.make}"
1110 enddef 1099 enddef
1111 def GetAge(): number 1100 def GetAge(): number
1112 return this.age 1101 return this.age
1113 enddef 1102 enddef
1114 endclass 1103 endclass
1115 1104
1116 var c = MyCar.new("abc") 1105 var c = MyCar.new("abc")
1117 assert_equal('make = abc', c.GetMake()) 1106 assert_equal('make = abc', c.GetMake())
1118 1107
1119 c = MyCar.new("def") 1108 c = MyCar.new("def")
1120 assert_equal('make = def', c.GetMake()) 1109 assert_equal('make = def', c.GetMake())
1121 1110
1122 var c2 = MyCar.new("123") 1111 var c2 = MyCar.new("123")
1123 assert_equal('make = 123', c2.GetMake()) 1112 assert_equal('make = 123', c2.GetMake())
1124 1113
1125 def CheckCar() 1114 def CheckCar()
1126 assert_equal("make = def", c.GetMake()) 1115 assert_equal("make = def", c.GetMake())
1127 assert_equal(5, c.GetAge()) 1116 assert_equal(5, c.GetAge())
1128 enddef 1117 enddef
1129 CheckCar() 1118 CheckCar()
1130 END 1119 END
1131 v9.CheckSourceSuccess(lines) 1120 v9.CheckSourceSuccess(lines)
1132 1121
1133 lines =<< trim END 1122 lines =<< trim END
1134 vim9script 1123 vim9script
1135 1124
1136 class MyCar 1125 class MyCar
1137 this.make: string 1126 this.make: string
1138 1127
1139 def new(make_arg: string) 1128 def new(make_arg: string)
1140 this.make = make_arg 1129 this.make = make_arg
1141 enddef 1130 enddef
1142 endclass 1131 endclass
1143 1132
1144 var c = MyCar.new("abc") 1133 var c = MyCar.new("abc")
1145 var c = MyCar.new("def") 1134 var c = MyCar.new("def")
1146 END 1135 END
1147 v9.CheckSourceFailure(lines, 'E1041:') 1136 v9.CheckSourceFailure(lines, 'E1041: Redefining script item: "c"', 12)
1148 1137
1149 lines =<< trim END 1138 lines =<< trim END
1150 vim9script 1139 vim9script
1151 1140
1152 class Foo 1141 class Foo
1153 this.x: list<number> = [] 1142 this.x: list<number> = []
1154 1143
1155 def Add(n: number): any 1144 def Add(n: number): any
1156 this.x->add(n) 1145 this.x->add(n)
1157 return this 1146 return this
1158 enddef 1147 enddef
1159 endclass 1148 endclass
1160 1149
1161 echo Foo.new().Add(1).Add(2).x 1150 echo Foo.new().Add(1).Add(2).x
1162 echo Foo.new().Add(1).Add(2) 1151 echo Foo.new().Add(1).Add(2)
1163 .x 1152 .x
1164 echo Foo.new().Add(1) 1153 echo Foo.new().Add(1)
1165 .Add(2).x 1154 .Add(2).x
1166 echo Foo.new() 1155 echo Foo.new()
1167 .Add(1).Add(2).x 1156 .Add(1).Add(2).x
1168 echo Foo.new() 1157 echo Foo.new()
1169 .Add(1) 1158 .Add(1)
1170 .Add(2) 1159 .Add(2)
1171 .x 1160 .x
1172 END 1161 END
1173 v9.CheckSourceSuccess(lines) 1162 v9.CheckSourceSuccess(lines)
1174 1163
1175 # Test for "public" cannot be abbreviated 1164 # Test for "public" cannot be abbreviated
1176 lines =<< trim END 1165 lines =<< trim END
1177 vim9script 1166 vim9script
1178 class Something 1167 class Something
1179 pub this.val = 1 1168 pub this.val = 1
1180 endclass 1169 endclass
1181 END 1170 END
1182 v9.CheckSourceFailure(lines, 'E1065:') 1171 v9.CheckSourceFailure(lines, 'E1065: Command cannot be shortened: pub this.val = 1', 3)
1183 1172
1184 # Test for "public" keyword must be followed by "this" or "static". 1173 # Test for "public" keyword must be followed by "this" or "static".
1185 lines =<< trim END 1174 lines =<< trim END
1186 vim9script 1175 vim9script
1187 class Something 1176 class Something
1188 public val = 1 1177 public val = 1
1189 endclass 1178 endclass
1190 END 1179 END
1191 v9.CheckSourceFailure(lines, 'E1331:') 1180 v9.CheckSourceFailure(lines, 'E1331: Public must be followed by "this" or "static"', 3)
1192 1181
1193 # Modify a instance variable using the class name in the script context 1182 # Modify a instance variable using the class name in the script context
1194 lines =<< trim END 1183 lines =<< trim END
1195 vim9script 1184 vim9script
1196 class A 1185 class A
1197 public this.val = 1 1186 public this.val = 1
1198 endclass 1187 endclass
1199 A.val = 1 1188 A.val = 1
1200 END 1189 END
1201 v9.CheckSourceFailure(lines, 'E1376: Object variable "val" accessible only using class "A" object') 1190 v9.CheckSourceFailure(lines, 'E1376: Object variable "val" accessible only using class "A" object', 5)
1202 1191
1203 # Read a instance variable using the class name in the script context 1192 # Read a instance variable using the class name in the script context
1204 lines =<< trim END 1193 lines =<< trim END
1205 vim9script 1194 vim9script
1206 class A 1195 class A
1207 public this.val = 1 1196 public this.val = 1
1208 endclass 1197 endclass
1209 var i = A.val 1198 var i = A.val
1210 END 1199 END
1211 v9.CheckSourceFailure(lines, 'E1376: Object variable "val" accessible only using class "A" object') 1200 v9.CheckSourceFailure(lines, 'E1376: Object variable "val" accessible only using class "A" object', 5)
1212 1201
1213 # Modify a instance variable using the class name in a def function 1202 # Modify a instance variable using the class name in a def function
1214 lines =<< trim END 1203 lines =<< trim END
1215 vim9script 1204 vim9script
1216 class A 1205 class A
1219 def T() 1208 def T()
1220 A.val = 1 1209 A.val = 1
1221 enddef 1210 enddef
1222 T() 1211 T()
1223 END 1212 END
1224 v9.CheckSourceFailure(lines, 'E1376: Object variable "val" accessible only using class "A" object') 1213 v9.CheckSourceFailure(lines, 'E1376: Object variable "val" accessible only using class "A" object', 1)
1225 1214
1226 # Read a instance variable using the class name in a def function 1215 # Read a instance variable using the class name in a def function
1227 lines =<< trim END 1216 lines =<< trim END
1228 vim9script 1217 vim9script
1229 class A 1218 class A
1232 def T() 1221 def T()
1233 var i = A.val 1222 var i = A.val
1234 enddef 1223 enddef
1235 T() 1224 T()
1236 END 1225 END
1237 v9.CheckSourceFailure(lines, 'E1376: Object variable "val" accessible only using class "A" object') 1226 v9.CheckSourceFailure(lines, 'E1376: Object variable "val" accessible only using class "A" object', 1)
1238 1227
1239 # Access from child class extending a class: 1228 # Access from child class extending a class:
1240 lines =<< trim END 1229 lines =<< trim END
1241 vim9script 1230 vim9script
1242 class A 1231 class A
1243 this.ro_obj_var = 10 1232 this.ro_obj_var = 10
1244 public this.rw_obj_var = 20 1233 public this.rw_obj_var = 20
1245 this._priv_obj_var = 30 1234 this._priv_obj_var = 30
1246 endclass 1235 endclass
1247 1236
1248 class B extends A 1237 class B extends A
1249 def Foo() 1238 def Foo()
1250 var x: number 1239 var x: number
1251 x = this.ro_obj_var 1240 x = this.ro_obj_var
1252 this.ro_obj_var = 0 1241 this.ro_obj_var = 0
1253 x = this.rw_obj_var 1242 x = this.rw_obj_var
1254 this.rw_obj_var = 0 1243 this.rw_obj_var = 0
1255 x = this._priv_obj_var 1244 x = this._priv_obj_var
1256 this._priv_obj_var = 0 1245 this._priv_obj_var = 0
1257 enddef 1246 enddef
1258 endclass 1247 endclass
1259 1248
1260 var b = B.new() 1249 var b = B.new()
1261 b.Foo() 1250 b.Foo()
1262 END 1251 END
1263 v9.CheckSourceSuccess(lines) 1252 v9.CheckSourceSuccess(lines)
1264 enddef 1253 enddef
1265 1254
1266 " Test for class variable access 1255 " Test for class variable access
1270 vim9script 1259 vim9script
1271 class Something 1260 class Something
1272 stat this.val = 1 1261 stat this.val = 1
1273 endclass 1262 endclass
1274 END 1263 END
1275 v9.CheckSourceFailure(lines, 'E1065:') 1264 v9.CheckSourceFailure(lines, 'E1065: Command cannot be shortened: stat this.val = 1', 3)
1276 1265
1277 # Test for "static" cannot be followed by "this". 1266 # Test for "static" cannot be followed by "this".
1278 lines =<< trim END 1267 lines =<< trim END
1279 vim9script 1268 vim9script
1280 class Something 1269 class Something
1281 static this.val = 1 1270 static this.val = 1
1282 endclass 1271 endclass
1283 END 1272 END
1284 v9.CheckSourceFailure(lines, 'E1368: Static cannot be followed by "this" in a variable name') 1273 v9.CheckSourceFailure(lines, 'E1368: Static cannot be followed by "this" in a variable name', 3)
1285 1274
1286 # Test for "static" cannot be followed by "public". 1275 # Test for "static" cannot be followed by "public".
1287 lines =<< trim END 1276 lines =<< trim END
1288 vim9script 1277 vim9script
1289 class Something 1278 class Something
1290 static public val = 1 1279 static public val = 1
1291 endclass 1280 endclass
1292 END 1281 END
1293 v9.CheckSourceFailure(lines, 'E1022: Type or initialization required') 1282 v9.CheckSourceFailure(lines, 'E1022: Type or initialization required', 3)
1294 1283
1295 # A readonly class variable cannot be modified from a child class 1284 # A readonly class variable cannot be modified from a child class
1296 lines =<< trim END 1285 lines =<< trim END
1297 vim9script 1286 vim9script
1298 class A 1287 class A
1299 static ro_class_var = 40 1288 static ro_class_var = 40
1300 endclass 1289 endclass
1301 1290
1302 class B extends A 1291 class B extends A
1303 def Foo() 1292 def Foo()
1304 A.ro_class_var = 50 1293 A.ro_class_var = 50
1305 enddef 1294 enddef
1306 endclass 1295 endclass
1307 1296
1308 var b = B.new() 1297 var b = B.new()
1309 b.Foo() 1298 b.Foo()
1310 END 1299 END
1311 v9.CheckSourceFailure(lines, 'E1335: Variable "ro_class_var" in class "A" is not writable') 1300 v9.CheckSourceFailure(lines, 'E1335: Variable "ro_class_var" in class "A" is not writable', 1)
1312 1301
1313 # A private class variable cannot be accessed from a child class 1302 # A private class variable cannot be accessed from a child class
1314 lines =<< trim END 1303 lines =<< trim END
1315 vim9script 1304 vim9script
1316 class A 1305 class A
1317 static _priv_class_var = 60 1306 static _priv_class_var = 60
1318 endclass 1307 endclass
1319 1308
1320 class B extends A 1309 class B extends A
1321 def Foo() 1310 def Foo()
1322 var i = A._priv_class_var 1311 var i = A._priv_class_var
1323 enddef 1312 enddef
1324 endclass 1313 endclass
1325 1314
1326 var b = B.new() 1315 var b = B.new()
1327 b.Foo() 1316 b.Foo()
1328 END 1317 END
1329 v9.CheckSourceFailure(lines, 'E1333: Cannot access private variable: _priv_class_var') 1318 v9.CheckSourceFailure(lines, 'E1333: Cannot access private variable: _priv_class_var', 1)
1330 1319
1331 # A private class variable cannot be modified from a child class 1320 # A private class variable cannot be modified from a child class
1332 lines =<< trim END 1321 lines =<< trim END
1333 vim9script 1322 vim9script
1334 class A 1323 class A
1335 static _priv_class_var = 60 1324 static _priv_class_var = 60
1336 endclass 1325 endclass
1337 1326
1338 class B extends A 1327 class B extends A
1339 def Foo() 1328 def Foo()
1340 A._priv_class_var = 0 1329 A._priv_class_var = 0
1341 enddef 1330 enddef
1342 endclass 1331 endclass
1343 1332
1344 var b = B.new() 1333 var b = B.new()
1345 b.Foo() 1334 b.Foo()
1346 END 1335 END
1347 v9.CheckSourceFailure(lines, 'E1333: Cannot access private variable: _priv_class_var') 1336 v9.CheckSourceFailure(lines, 'E1333: Cannot access private variable: _priv_class_var', 1)
1348 1337
1349 # Access from child class extending a class and from script context 1338 # Access from child class extending a class and from script context
1350 lines =<< trim END 1339 lines =<< trim END
1351 vim9script 1340 vim9script
1352 class A 1341 class A
1353 static ro_class_var = 10 1342 static ro_class_var = 10
1354 public static rw_class_var = 20 1343 public static rw_class_var = 20
1355 static _priv_class_var = 30 1344 static _priv_class_var = 30
1356 endclass 1345 endclass
1357 1346
1358 class B extends A 1347 class B extends A
1359 def Foo() 1348 def Foo()
1360 var x: number 1349 var x: number
1361 x = A.ro_class_var 1350 x = A.ro_class_var
1362 assert_equal(10, x) 1351 assert_equal(10, x)
1363 x = A.rw_class_var 1352 x = A.rw_class_var
1364 assert_equal(25, x) 1353 assert_equal(25, x)
1365 A.rw_class_var = 20 1354 A.rw_class_var = 20
1366 assert_equal(20, A.rw_class_var) 1355 assert_equal(20, A.rw_class_var)
1367 enddef 1356 enddef
1368 endclass 1357 endclass
1369 1358
1370 assert_equal(10, A.ro_class_var) 1359 assert_equal(10, A.ro_class_var)
1371 assert_equal(20, A.rw_class_var) 1360 assert_equal(20, A.rw_class_var)
1372 A.rw_class_var = 25 1361 A.rw_class_var = 25
1373 assert_equal(25, A.rw_class_var) 1362 assert_equal(25, A.rw_class_var)
1374 var b = B.new() 1363 var b = B.new()
1375 b.Foo() 1364 b.Foo()
1376 END 1365 END
1377 v9.CheckSourceSuccess(lines) 1366 v9.CheckSourceSuccess(lines)
1378 enddef 1367 enddef
1379 1368
1380 def Test_class_object_compare() 1369 def Test_class_object_compare()
1381 var class_lines =<< trim END 1370 var class_lines =<< trim END
1382 vim9script 1371 vim9script
1383 class Item 1372 class Item
1384 this.nr = 0 1373 this.nr = 0
1385 this.name = 'xx' 1374 this.name = 'xx'
1386 endclass 1375 endclass
1387 END 1376 END
1388 1377
1389 # used at the script level and in a compiled function 1378 # used at the script level and in a compiled function
1390 var test_lines =<< trim END 1379 var test_lines =<< trim END
1391 var i1 = Item.new() 1380 var i1 = Item.new()
1392 assert_equal(i1, i1) 1381 assert_equal(i1, i1)
1393 assert_true(i1 is i1) 1382 assert_true(i1 is i1)
1394 var i2 = Item.new() 1383 var i2 = Item.new()
1395 assert_equal(i1, i2) 1384 assert_equal(i1, i2)
1396 assert_false(i1 is i2) 1385 assert_false(i1 is i2)
1397 var i3 = Item.new(0, 'xx') 1386 var i3 = Item.new(0, 'xx')
1398 assert_equal(i1, i3) 1387 assert_equal(i1, i3)
1399 1388
1400 var io1 = Item.new(1, 'xx') 1389 var io1 = Item.new(1, 'xx')
1401 assert_notequal(i1, io1) 1390 assert_notequal(i1, io1)
1402 var io2 = Item.new(0, 'yy') 1391 var io2 = Item.new(0, 'yy')
1403 assert_notequal(i1, io2) 1392 assert_notequal(i1, io2)
1404 END 1393 END
1405 1394
1406 v9.CheckSourceSuccess(class_lines + test_lines) 1395 v9.CheckSourceSuccess(class_lines + test_lines)
1407 v9.CheckSourceSuccess( 1396 v9.CheckSourceSuccess(
1408 class_lines + ['def Test()'] + test_lines + ['enddef', 'Test()']) 1397 class_lines + ['def Test()'] + test_lines + ['enddef', 'Test()'])
1409 1398
1410 for op in ['>', '>=', '<', '<=', '=~', '!~'] 1399 for op in ['>', '>=', '<', '<=', '=~', '!~']
1411 var op_lines = [ 1400 var op_lines = [
1412 'var i1 = Item.new()', 1401 'var i1 = Item.new()',
1413 'var i2 = Item.new()', 1402 'var i2 = Item.new()',
1414 'echo i1 ' .. op .. ' i2', 1403 'echo i1 ' .. op .. ' i2',
1415 ] 1404 ]
1416 v9.CheckSourceFailure(class_lines + op_lines, 'E1153: Invalid operation for object') 1405 v9.CheckSourceFailure(class_lines + op_lines, 'E1153: Invalid operation for object', 8)
1417 v9.CheckSourceFailure(class_lines 1406 v9.CheckSourceFailure(class_lines
1418 + ['def Test()'] + op_lines + ['enddef', 'Test()'], 'E1153: Invalid operation for object') 1407 + ['def Test()'] + op_lines + ['enddef', 'Test()'], 'E1153: Invalid operation for object')
1419 endfor 1408 endfor
1420 enddef 1409 enddef
1421 1410
1422 def Test_object_type() 1411 def Test_object_type()
1423 var lines =<< trim END 1412 var lines =<< trim END
1424 vim9script 1413 vim9script
1425 1414
1426 class One 1415 class One
1427 this.one = 1 1416 this.one = 1
1428 endclass 1417 endclass
1429 class Two 1418 class Two
1430 this.two = 2 1419 this.two = 2
1431 endclass 1420 endclass
1432 class TwoMore extends Two 1421 class TwoMore extends Two
1433 this.more = 9 1422 this.more = 9
1434 endclass 1423 endclass
1435 1424
1436 var o: One = One.new() 1425 var o: One = One.new()
1437 var t: Two = Two.new() 1426 var t: Two = Two.new()
1438 var m: TwoMore = TwoMore.new() 1427 var m: TwoMore = TwoMore.new()
1439 var tm: Two = TwoMore.new() 1428 var tm: Two = TwoMore.new()
1440 1429
1441 t = m 1430 t = m
1442 END 1431 END
1443 v9.CheckSourceSuccess(lines) 1432 v9.CheckSourceSuccess(lines)
1444 1433
1445 lines =<< trim END 1434 lines =<< trim END
1446 vim9script 1435 vim9script
1447 1436
1448 class One 1437 class One
1449 this.one = 1 1438 this.one = 1
1450 endclass 1439 endclass
1451 class Two 1440 class Two
1452 this.two = 2 1441 this.two = 2
1453 endclass 1442 endclass
1454 1443
1455 var o: One = Two.new() 1444 var o: One = Two.new()
1456 END 1445 END
1457 v9.CheckSourceFailure(lines, 'E1012: Type mismatch; expected object<One> but got object<Two>') 1446 v9.CheckSourceFailure(lines, 'E1012: Type mismatch; expected object<One> but got object<Two>', 10)
1458 1447
1459 lines =<< trim END 1448 lines =<< trim END
1460 vim9script 1449 vim9script
1461 1450
1462 interface One 1451 interface One
1463 def GetMember(): number 1452 def GetMember(): number
1464 endinterface 1453 endinterface
1465 class Two implements One 1454 class Two implements One
1466 this.one = 1 1455 this.one = 1
1467 def GetMember(): number 1456 def GetMember(): number
1468 return this.one 1457 return this.one
1469 enddef 1458 enddef
1470 endclass 1459 endclass
1471 1460
1472 var o: One = Two.new(5) 1461 var o: One = Two.new(5)
1473 assert_equal(5, o.GetMember()) 1462 assert_equal(5, o.GetMember())
1474 END 1463 END
1475 v9.CheckSourceSuccess(lines) 1464 v9.CheckSourceSuccess(lines)
1476 1465
1477 lines =<< trim END 1466 lines =<< trim END
1478 vim9script 1467 vim9script
1479 1468
1480 class Num 1469 class Num
1481 this.n: number = 0 1470 this.n: number = 0
1482 endclass 1471 endclass
1483 1472
1484 def Ref(name: string): func(Num): Num 1473 def Ref(name: string): func(Num): Num
1485 return (arg: Num): Num => { 1474 return (arg: Num): Num => {
1486 return eval(name)(arg) 1475 return eval(name)(arg)
1487 } 1476 }
1488 enddef 1477 enddef
1489 1478
1490 const Fn = Ref('Double') 1479 const Fn = Ref('Double')
1491 var Double = (m: Num): Num => Num.new(m.n * 2) 1480 var Double = (m: Num): Num => Num.new(m.n * 2)
1492 1481
1493 echo Fn(Num.new(4)) 1482 echo Fn(Num.new(4))
1494 END 1483 END
1495 v9.CheckSourceSuccess(lines) 1484 v9.CheckSourceSuccess(lines)
1496 enddef 1485 enddef
1497 1486
1498 def Test_class_member() 1487 def Test_class_member()
1499 # check access rules 1488 # check access rules
1500 var lines =<< trim END 1489 var lines =<< trim END
1501 vim9script 1490 vim9script
1502 class TextPos 1491 class TextPos
1503 this.lnum = 1 1492 this.lnum = 1
1504 this.col = 1 1493 this.col = 1
1505 static counter = 0 1494 static counter = 0
1506 static _secret = 7 1495 static _secret = 7
1507 public static anybody = 42 1496 public static anybody = 42
1508 1497
1509 static def AddToCounter(nr: number) 1498 static def AddToCounter(nr: number)
1510 counter += nr 1499 counter += nr
1511 enddef 1500 enddef
1512 endclass 1501 endclass
1513 1502
1514 assert_equal(0, TextPos.counter) 1503 assert_equal(0, TextPos.counter)
1515 TextPos.AddToCounter(3) 1504 TextPos.AddToCounter(3)
1516 assert_equal(3, TextPos.counter) 1505 assert_equal(3, TextPos.counter)
1517 assert_fails('echo TextPos.noSuchMember', 'E1337:') 1506 assert_fails('echo TextPos.noSuchMember', 'E1337: Class variable "noSuchMember" not found in class "TextPos"')
1518 1507
1519 def GetCounter(): number 1508 def GetCounter(): number
1520 return TextPos.counter 1509 return TextPos.counter
1521 enddef 1510 enddef
1522 assert_equal(3, GetCounter()) 1511 assert_equal(3, GetCounter())
1523 1512
1524 assert_fails('TextPos.noSuchMember = 2', 'E1337:') 1513 assert_fails('TextPos.noSuchMember = 2', 'E1337: Class variable "noSuchMember" not found in class "TextPos"')
1525 assert_fails('TextPos.counter = 5', 'E1335:') 1514 assert_fails('TextPos.counter = 5', 'E1335: Variable "counter" in class "TextPos" is not writable')
1526 assert_fails('TextPos.counter += 5', 'E1335:') 1515 assert_fails('TextPos.counter += 5', 'E1335: Variable "counter" in class "TextPos" is not writable')
1527 1516
1528 assert_fails('echo TextPos._secret', 'E1333:') 1517 assert_fails('echo TextPos._secret', 'E1333: Cannot access private variable: _secret')
1529 assert_fails('TextPos._secret = 8', 'E1333:') 1518 assert_fails('TextPos._secret = 8', 'E1333: Cannot access private variable: _secret')
1530 1519
1531 assert_equal(42, TextPos.anybody) 1520 assert_equal(42, TextPos.anybody)
1532 TextPos.anybody = 12 1521 TextPos.anybody = 12
1533 assert_equal(12, TextPos.anybody) 1522 assert_equal(12, TextPos.anybody)
1534 TextPos.anybody += 5 1523 TextPos.anybody += 5
1535 assert_equal(17, TextPos.anybody) 1524 assert_equal(17, TextPos.anybody)
1536 END 1525 END
1537 v9.CheckSourceSuccess(lines) 1526 v9.CheckSourceSuccess(lines)
1538 1527
1539 # example in the help 1528 # example in the help
1540 lines =<< trim END 1529 lines =<< trim END
1541 vim9script 1530 vim9script
1542 class OtherThing 1531 class OtherThing
1543 this.size: number 1532 this.size: number
1544 static totalSize: number 1533 static totalSize: number
1545 1534
1546 def new(this.size) 1535 def new(this.size)
1547 totalSize += this.size 1536 totalSize += this.size
1548 enddef 1537 enddef
1549 endclass 1538 endclass
1550 assert_equal(0, OtherThing.totalSize) 1539 assert_equal(0, OtherThing.totalSize)
1551 var to3 = OtherThing.new(3) 1540 var to3 = OtherThing.new(3)
1552 assert_equal(3, OtherThing.totalSize) 1541 assert_equal(3, OtherThing.totalSize)
1553 var to7 = OtherThing.new(7) 1542 var to7 = OtherThing.new(7)
1554 assert_equal(10, OtherThing.totalSize) 1543 assert_equal(10, OtherThing.totalSize)
1555 END 1544 END
1556 v9.CheckSourceSuccess(lines) 1545 v9.CheckSourceSuccess(lines)
1557 1546
1558 # using static class member twice 1547 # using static class member twice
1559 lines =<< trim END 1548 lines =<< trim END
1560 vim9script 1549 vim9script
1561 1550
1562 class HTML 1551 class HTML
1563 static author: string = 'John Doe' 1552 static author: string = 'John Doe'
1564 1553
1565 static def MacroSubstitute(s: string): string 1554 static def MacroSubstitute(s: string): string
1566 return substitute(s, '{{author}}', author, 'gi') 1555 return substitute(s, '{{author}}', author, 'gi')
1567 enddef 1556 enddef
1568 endclass 1557 endclass
1569 1558
1570 assert_equal('some text', HTML.MacroSubstitute('some text')) 1559 assert_equal('some text', HTML.MacroSubstitute('some text'))
1571 assert_equal('some text', HTML.MacroSubstitute('some text')) 1560 assert_equal('some text', HTML.MacroSubstitute('some text'))
1572 END 1561 END
1573 v9.CheckSourceSuccess(lines) 1562 v9.CheckSourceSuccess(lines)
1574 1563
1575 # access private member in lambda 1564 # access private member in lambda
1576 lines =<< trim END 1565 lines =<< trim END
1577 vim9script 1566 vim9script
1578 1567
1579 class Foo 1568 class Foo
1580 this._x: number = 0 1569 this._x: number = 0
1581 1570
1582 def Add(n: number): number 1571 def Add(n: number): number
1583 const F = (): number => this._x + n 1572 const F = (): number => this._x + n
1584 return F() 1573 return F()
1585 enddef 1574 enddef
1586 endclass 1575 endclass
1587 1576
1588 var foo = Foo.new() 1577 var foo = Foo.new()
1589 assert_equal(5, foo.Add(5)) 1578 assert_equal(5, foo.Add(5))
1590 END 1579 END
1591 v9.CheckSourceSuccess(lines) 1580 v9.CheckSourceSuccess(lines)
1592 1581
1593 # access private member in lambda body 1582 # access private member in lambda body
1594 lines =<< trim END 1583 lines =<< trim END
1595 vim9script 1584 vim9script
1596 1585
1597 class Foo 1586 class Foo
1598 this._x: number = 6 1587 this._x: number = 6
1599 1588
1600 def Add(n: number): number 1589 def Add(n: number): number
1601 var Lam = () => { 1590 var Lam = () => {
1602 this._x = this._x + n 1591 this._x = this._x + n
1603 } 1592 }
1604 Lam() 1593 Lam()
1605 return this._x 1594 return this._x
1606 enddef 1595 enddef
1607 endclass 1596 endclass
1608 1597
1609 var foo = Foo.new() 1598 var foo = Foo.new()
1610 assert_equal(13, foo.Add(7)) 1599 assert_equal(13, foo.Add(7))
1611 END 1600 END
1612 v9.CheckSourceSuccess(lines) 1601 v9.CheckSourceSuccess(lines)
1613 1602
1614 # check shadowing 1603 # check shadowing
1615 lines =<< trim END 1604 lines =<< trim END
1616 vim9script 1605 vim9script
1617 1606
1618 class Some 1607 class Some
1619 static count = 0 1608 static count = 0
1620 def Method(count: number) 1609 def Method(count: number)
1621 echo count 1610 echo count
1622 enddef 1611 enddef
1623 endclass 1612 endclass
1624 1613
1625 var s = Some.new() 1614 var s = Some.new()
1626 s.Method(7) 1615 s.Method(7)
1627 END 1616 END
1628 v9.CheckSourceFailure(lines, 'E1340: Argument already declared in the class: count') 1617 v9.CheckSourceFailure(lines, 'E1340: Argument already declared in the class: count', 5)
1629 1618
1630 # Use a local variable in a method with the same name as a class variable 1619 # Use a local variable in a method with the same name as a class variable
1631 lines =<< trim END 1620 lines =<< trim END
1632 vim9script 1621 vim9script
1633 1622
1634 class Some 1623 class Some
1635 static count = 0 1624 static count = 0
1636 def Method(arg: number) 1625 def Method(arg: number)
1637 var count = 3 1626 var count = 3
1638 echo arg count 1627 echo arg count
1639 enddef 1628 enddef
1640 endclass 1629 endclass
1641 1630
1642 var s = Some.new() 1631 var s = Some.new()
1643 s.Method(7) 1632 s.Method(7)
1644 END 1633 END
1645 v9.CheckSourceFailure(lines, 'E1341: Variable already declared in the class: count') 1634 v9.CheckSourceFailure(lines, 'E1341: Variable already declared in the class: count', 1)
1646 1635
1647 # Test for using an invalid type for a member variable 1636 # Test for using an invalid type for a member variable
1648 lines =<< trim END 1637 lines =<< trim END
1649 vim9script 1638 vim9script
1650 class A 1639 class A
1651 this.val: xxx 1640 this.val: xxx
1652 endclass 1641 endclass
1653 END 1642 END
1654 v9.CheckSourceFailure(lines, 'E1010:') 1643 v9.CheckSourceFailure(lines, 'E1010: Type not recognized: xxx', 3)
1655 1644
1656 # Test for setting a member on a null object 1645 # Test for setting a member on a null object
1657 lines =<< trim END 1646 lines =<< trim END
1658 vim9script 1647 vim9script
1659 class A 1648 class A
1660 public this.val: string 1649 public this.val: string
1661 endclass 1650 endclass
1662 1651
1663 def F() 1652 def F()
1664 var obj: A 1653 var obj: A
1665 obj.val = "" 1654 obj.val = ""
1666 enddef 1655 enddef
1667 F() 1656 F()
1668 END 1657 END
1669 v9.CheckSourceFailure(lines, 'E1360: Using a null object') 1658 v9.CheckSourceFailure(lines, 'E1360: Using a null object', 2)
1670 1659
1671 # Test for accessing a member on a null object 1660 # Test for accessing a member on a null object
1672 lines =<< trim END 1661 lines =<< trim END
1673 vim9script 1662 vim9script
1674 class A 1663 class A
1675 this.val: string 1664 this.val: string
1676 endclass 1665 endclass
1677 1666
1678 def F() 1667 def F()
1679 var obj: A 1668 var obj: A
1680 echo obj.val 1669 echo obj.val
1681 enddef 1670 enddef
1682 F() 1671 F()
1683 END 1672 END
1684 v9.CheckSourceFailure(lines, 'E1360: Using a null object') 1673 v9.CheckSourceFailure(lines, 'E1360: Using a null object', 2)
1685 1674
1686 # Test for setting a member on a null object, at script level 1675 # Test for setting a member on a null object, at script level
1687 lines =<< trim END 1676 lines =<< trim END
1688 vim9script 1677 vim9script
1689 class A 1678 class A
1690 public this.val: string 1679 public this.val: string
1691 endclass 1680 endclass
1692 1681
1693 var obj: A 1682 var obj: A
1694 obj.val = "" 1683 obj.val = ""
1695 END 1684 END
1696 # FIXME(in source): this should give E1360 as well! 1685 # FIXME(in source): this should give E1360 as well!
1697 v9.CheckSourceFailure(lines, 'E1012: Type mismatch; expected object<A> but got string') 1686 v9.CheckSourceFailure(lines, 'E1012: Type mismatch; expected object<A> but got string', 7)
1698 1687
1699 # Test for accessing a member on a null object, at script level 1688 # Test for accessing a member on a null object, at script level
1700 lines =<< trim END 1689 lines =<< trim END
1701 vim9script 1690 vim9script
1702 class A 1691 class A
1703 this.val: string 1692 this.val: string
1704 endclass 1693 endclass
1705 1694
1706 var obj: A 1695 var obj: A
1707 echo obj.val 1696 echo obj.val
1708 END 1697 END
1709 v9.CheckSourceFailure(lines, 'E1360: Using a null object') 1698 v9.CheckSourceFailure(lines, 'E1360: Using a null object', 7)
1710 1699
1711 # Test for no space before or after the '=' when initializing a member 1700 # Test for no space before or after the '=' when initializing a member
1712 # variable 1701 # variable
1713 lines =<< trim END 1702 lines =<< trim END
1714 vim9script 1703 vim9script
1715 class A 1704 class A
1716 this.val: number= 10 1705 this.val: number= 10
1717 endclass 1706 endclass
1718 END 1707 END
1719 v9.CheckSourceFailure(lines, 'E1004:') 1708 v9.CheckSourceFailure(lines, "E1004: White space required before and after '='", 3)
1720 lines =<< trim END 1709 lines =<< trim END
1721 vim9script 1710 vim9script
1722 class A 1711 class A
1723 this.val: number =10 1712 this.val: number =10
1724 endclass 1713 endclass
1725 END 1714 END
1726 v9.CheckSourceFailure(lines, 'E1004:') 1715 v9.CheckSourceFailure(lines, "E1004: White space required before and after '='", 3)
1727 1716
1728 # Access a non-existing member 1717 # Access a non-existing member
1729 lines =<< trim END 1718 lines =<< trim END
1730 vim9script 1719 vim9script
1731 class A 1720 class A
1732 endclass 1721 endclass
1733 var a = A.new() 1722 var a = A.new()
1734 var v = a.bar 1723 var v = a.bar
1735 END 1724 END
1736 v9.CheckSourceFailure(lines, 'E1326: Variable not found on object "A": bar') 1725 v9.CheckSourceFailure(lines, 'E1326: Variable not found on object "A": bar', 5)
1737 enddef 1726 enddef
1738 1727
1739 func Test_class_garbagecollect() 1728 func Test_class_garbagecollect()
1740 let lines =<< trim END 1729 let lines =<< trim END
1741 vim9script 1730 vim9script
1742 1731
1743 class Point 1732 class Point
1744 this.p = [2, 3] 1733 this.p = [2, 3]
1745 static pl = ['a', 'b'] 1734 static pl = ['a', 'b']
1746 static pd = {a: 'a', b: 'b'} 1735 static pd = {a: 'a', b: 'b'}
1747 endclass 1736 endclass
1748 1737
1749 echo Point.pl Point.pd 1738 echo Point.pl Point.pd
1750 call test_garbagecollect_now() 1739 call test_garbagecollect_now()
1751 echo Point.pl Point.pd 1740 echo Point.pl Point.pd
1752 END 1741 END
1753 call v9.CheckSourceSuccess(lines) 1742 call v9.CheckSourceSuccess(lines)
1754 1743
1755 let lines =<< trim END 1744 let lines =<< trim END
1756 vim9script 1745 vim9script
1757 1746
1758 interface View 1747 interface View
1759 endinterface 1748 endinterface
1760 1749
1761 class Widget 1750 class Widget
1762 this.view: View 1751 this.view: View
1763 endclass 1752 endclass
1764 1753
1765 class MyView implements View 1754 class MyView implements View
1766 this.widget: Widget 1755 this.widget: Widget
1767 1756
1768 def new() 1757 def new()
1769 # this will result in a circular reference to this object 1758 # this will result in a circular reference to this object
1770 this.widget = Widget.new(this) 1759 this.widget = Widget.new(this)
1771 enddef 1760 enddef
1772 endclass 1761 endclass
1773 1762
1774 var view = MyView.new() 1763 var view = MyView.new()
1775 1764
1776 # overwrite "view", will be garbage-collected next 1765 # overwrite "view", will be garbage-collected next
1777 view = MyView.new() 1766 view = MyView.new()
1778 test_garbagecollect_now() 1767 test_garbagecollect_now()
1779 END 1768 END
1780 call v9.CheckSourceSuccess(lines) 1769 call v9.CheckSourceSuccess(lines)
1781 endfunc 1770 endfunc
1782 1771
1783 " Test interface garbage collection 1772 " Test interface garbage collection
1825 call v9.CheckSourceSuccess(lines) 1814 call v9.CheckSourceSuccess(lines)
1826 endfunc 1815 endfunc
1827 1816
1828 def Test_class_method() 1817 def Test_class_method()
1829 var lines =<< trim END 1818 var lines =<< trim END
1830 vim9script 1819 vim9script
1831 class Value 1820 class Value
1832 this.value = 0 1821 this.value = 0
1833 static objects = 0 1822 static objects = 0
1834 1823
1835 def new(v: number) 1824 def new(v: number)
1836 this.value = v 1825 this.value = v
1837 ++objects 1826 ++objects
1838 enddef 1827 enddef
1839 1828
1840 static def GetCount(): number 1829 static def GetCount(): number
1841 return objects 1830 return objects
1842 enddef 1831 enddef
1843 endclass 1832 endclass
1844 1833
1845 assert_equal(0, Value.GetCount()) 1834 assert_equal(0, Value.GetCount())
1846 var v1 = Value.new(2) 1835 var v1 = Value.new(2)
1847 assert_equal(1, Value.GetCount()) 1836 assert_equal(1, Value.GetCount())
1848 var v2 = Value.new(7) 1837 var v2 = Value.new(7)
1849 assert_equal(2, Value.GetCount()) 1838 assert_equal(2, Value.GetCount())
1850 END 1839 END
1851 v9.CheckSourceSuccess(lines) 1840 v9.CheckSourceSuccess(lines)
1852 1841
1853 # Test for cleaning up after a class definition failure when using class 1842 # Test for cleaning up after a class definition failure when using class
1854 # functions. 1843 # functions.
1858 static def Foo() 1847 static def Foo()
1859 enddef 1848 enddef
1860 aaa 1849 aaa
1861 endclass 1850 endclass
1862 END 1851 END
1863 v9.CheckSourceFailure(lines, 'E1318:') 1852 v9.CheckSourceFailure(lines, 'E1318: Not a valid command in a class: aaa', 5)
1864 1853
1865 # Test for calling a class method from another class method without the class 1854 # Test for calling a class method from another class method without the class
1866 # name prefix. 1855 # name prefix.
1867 lines =<< trim END 1856 lines =<< trim END
1868 vim9script 1857 vim9script
1886 v9.CheckSourceSuccess(lines) 1875 v9.CheckSourceSuccess(lines)
1887 enddef 1876 enddef
1888 1877
1889 def Test_class_defcompile() 1878 def Test_class_defcompile()
1890 var lines =<< trim END 1879 var lines =<< trim END
1891 vim9script 1880 vim9script
1892 1881
1893 class C 1882 class C
1894 def Fo(i: number): string 1883 def Fo(i: number): string
1895 return i 1884 return i
1896 enddef 1885 enddef
1897 endclass 1886 endclass
1898 1887
1899 defcompile C.Fo 1888 defcompile C.Fo
1900 END 1889 END
1901 v9.CheckSourceFailure(lines, 'E1012: Type mismatch; expected string but got number') 1890 v9.CheckSourceFailure(lines, 'E1012: Type mismatch; expected string but got number', 1)
1902 1891
1903 lines =<< trim END 1892 lines =<< trim END
1904 vim9script 1893 vim9script
1905 1894
1906 class C 1895 class C
1907 static def Fc(): number 1896 static def Fc(): number
1908 return 'x' 1897 return 'x'
1909 enddef 1898 enddef
1910 endclass 1899 endclass
1911 1900
1912 defcompile C.Fc 1901 defcompile C.Fc
1913 END 1902 END
1914 v9.CheckSourceFailure(lines, 'E1012: Type mismatch; expected number but got string') 1903 v9.CheckSourceFailure(lines, 'E1012: Type mismatch; expected number but got string', 1)
1915 1904
1916 lines =<< trim END 1905 lines =<< trim END
1917 vim9script 1906 vim9script
1918 1907
1919 class C 1908 class C
1920 static def new() 1909 static def new()
1921 enddef 1910 enddef
1922 endclass 1911 endclass
1923 1912
1924 defcompile C.new 1913 defcompile C.new
1925 END 1914 END
1926 v9.CheckSourceFailure(lines, 'E1370: Cannot define a "new" method as static') 1915 v9.CheckSourceFailure(lines, 'E1370: Cannot define a "new" method as static', 5)
1927 1916
1928 # Trying to compile a function using a non-existing class variable 1917 # Trying to compile a function using a non-existing class variable
1929 lines =<< trim END 1918 lines =<< trim END
1930 vim9script 1919 vim9script
1931 defcompile x.Foo() 1920 defcompile x.Foo()
1932 END 1921 END
1933 v9.CheckSourceFailure(lines, 'E475:') 1922 v9.CheckSourceFailure(lines, 'E475: Invalid argument: x.Foo()', 2)
1934 1923
1935 # Trying to compile a function using a variable which is not a class 1924 # Trying to compile a function using a variable which is not a class
1936 lines =<< trim END 1925 lines =<< trim END
1937 vim9script 1926 vim9script
1938 var x: number 1927 var x: number
1939 defcompile x.Foo() 1928 defcompile x.Foo()
1940 END 1929 END
1941 v9.CheckSourceFailure(lines, 'E475:') 1930 v9.CheckSourceFailure(lines, 'E475: Invalid argument: x.Foo()', 3)
1942 1931
1943 # Trying to compile a function without specifying the name 1932 # Trying to compile a function without specifying the name
1944 lines =<< trim END 1933 lines =<< trim END
1945 vim9script 1934 vim9script
1946 class A 1935 class A
1947 endclass 1936 endclass
1948 defcompile A. 1937 defcompile A.
1949 END 1938 END
1950 v9.CheckSourceFailure(lines, 'E475:') 1939 v9.CheckSourceFailure(lines, 'E475: Invalid argument: A.', 4)
1951 1940
1952 # Trying to compile a non-existing class object member function 1941 # Trying to compile a non-existing class object member function
1953 lines =<< trim END 1942 lines =<< trim END
1954 vim9script 1943 vim9script
1955 class A 1944 class A
1956 endclass 1945 endclass
1957 var a = A.new() 1946 var a = A.new()
1958 defcompile a.Foo() 1947 defcompile a.Foo()
1959 END 1948 END
1960 v9.CheckSourceFailureList(lines, ['E1326:', 'E475:']) 1949 v9.CheckSourceFailureList(lines, ['E1326: Variable not found on object "A": Foo', 'E475: Invalid argument: a.Foo()'])
1961 enddef 1950 enddef
1962 1951
1963 def Test_class_object_to_string() 1952 def Test_class_object_to_string()
1964 var lines =<< trim END 1953 var lines =<< trim END
1965 vim9script 1954 vim9script
1966 class TextPosition 1955 class TextPosition
1967 this.lnum = 1 1956 this.lnum = 1
1968 this.col = 22 1957 this.col = 22
1969 endclass 1958 endclass
1970 1959
1971 assert_equal("class TextPosition", string(TextPosition)) 1960 assert_equal("class TextPosition", string(TextPosition))
1972 1961
1973 var pos = TextPosition.new() 1962 var pos = TextPosition.new()
1974 assert_equal("object of TextPosition {lnum: 1, col: 22}", string(pos)) 1963 assert_equal("object of TextPosition {lnum: 1, col: 22}", string(pos))
1975 END 1964 END
1976 v9.CheckSourceSuccess(lines) 1965 v9.CheckSourceSuccess(lines)
1977 enddef 1966 enddef
1978 1967
1979 def Test_interface_basics() 1968 def Test_interface_basics()
1980 var lines =<< trim END 1969 var lines =<< trim END
1981 vim9script 1970 vim9script
1982 interface Something 1971 interface Something
1983 this.ro_var: list<number> 1972 this.ro_var: list<number>
1984 def GetCount(): number 1973 def GetCount(): number
1985 endinterface 1974 endinterface
1986 END 1975 END
1987 v9.CheckSourceSuccess(lines) 1976 v9.CheckSourceSuccess(lines)
1988 1977
1989 lines =<< trim END 1978 lines =<< trim END
1990 interface SomethingWrong 1979 interface SomethingWrong
1991 static count = 7 1980 static count = 7
1992 endinterface 1981 endinterface
1993 END 1982 END
1994 v9.CheckSourceFailure(lines, 'E1342:') 1983 v9.CheckSourceFailure(lines, 'E1342: Interface can only be defined in Vim9 script', 1)
1995 1984
1996 lines =<< trim END 1985 lines =<< trim END
1997 vim9script 1986 vim9script
1998 1987
1999 interface Some 1988 interface Some
2000 this.value: number 1989 this.value: number
2001 def Method(value: number) 1990 def Method(value: number)
2002 endinterface 1991 endinterface
2003 END 1992 END
2004 # The argument name and the object member name are the same, but this is not a 1993 # The argument name and the object member name are the same, but this is not a
2005 # problem because object members are always accessed with the "this." prefix. 1994 # problem because object members are always accessed with the "this." prefix.
2006 v9.CheckSourceSuccess(lines) 1995 v9.CheckSourceSuccess(lines)
2007 1996
2008 lines =<< trim END 1997 lines =<< trim END
2009 vim9script 1998 vim9script
2010 interface somethingWrong 1999 interface somethingWrong
2011 static count = 7 2000 static count = 7
2012 endinterface 2001 endinterface
2013 END 2002 END
2014 v9.CheckSourceFailure(lines, 'E1343: Interface name must start with an uppercase letter: somethingWrong') 2003 v9.CheckSourceFailure(lines, 'E1343: Interface name must start with an uppercase letter: somethingWrong', 2)
2015 2004
2016 lines =<< trim END 2005 lines =<< trim END
2017 vim9script 2006 vim9script
2018 interface SomethingWrong 2007 interface SomethingWrong
2019 this.value: string 2008 this.value: string
2020 this.count = 7 2009 this.count = 7
2021 def GetCount(): number 2010 def GetCount(): number
2022 endinterface 2011 endinterface
2023 END 2012 END
2024 v9.CheckSourceFailure(lines, 'E1344:') 2013 v9.CheckSourceFailure(lines, 'E1344: Cannot initialize a variable in an interface', 4)
2025 2014
2026 lines =<< trim END 2015 lines =<< trim END
2027 vim9script 2016 vim9script
2028 interface SomethingWrong 2017 interface SomethingWrong
2029 this.value: string 2018 this.value: string
2030 this.count: number 2019 this.count: number
2031 def GetCount(): number 2020 def GetCount(): number
2032 return 5 2021 return 5
2033 enddef 2022 enddef
2034 endinterface 2023 endinterface
2035 END 2024 END
2036 v9.CheckSourceFailure(lines, 'E1345: Not a valid command in an interface: return 5') 2025 v9.CheckSourceFailure(lines, 'E1345: Not a valid command in an interface: return 5', 6)
2037 2026
2038 lines =<< trim END 2027 lines =<< trim END
2039 vim9script 2028 vim9script
2040 export interface EnterExit 2029 export interface EnterExit
2041 def Enter(): void 2030 def Enter(): void
2042 def Exit(): void 2031 def Exit(): void
2043 endinterface 2032 endinterface
2044 END 2033 END
2045 writefile(lines, 'XdefIntf.vim', 'D') 2034 writefile(lines, 'XdefIntf.vim', 'D')
2046 2035
2047 lines =<< trim END 2036 lines =<< trim END
2048 vim9script 2037 vim9script
2049 import './XdefIntf.vim' as defIntf 2038 import './XdefIntf.vim' as defIntf
2050 export def With(ee: defIntf.EnterExit, F: func) 2039 export def With(ee: defIntf.EnterExit, F: func)
2051 ee.Enter() 2040 ee.Enter()
2052 try 2041 try
2053 F() 2042 F()
2054 finally 2043 finally
2055 ee.Exit() 2044 ee.Exit()
2056 endtry 2045 endtry
2057 enddef 2046 enddef
2058 END 2047 END
2059 v9.CheckScriptSuccess(lines) 2048 v9.CheckScriptSuccess(lines)
2060 2049
2061 var imported =<< trim END 2050 var imported =<< trim END
2062 vim9script 2051 vim9script
2063 export abstract class EnterExit 2052 export abstract class EnterExit
2064 def Enter(): void 2053 def Enter(): void
2065 enddef 2054 enddef
2066 def Exit(): void 2055 def Exit(): void
2067 enddef 2056 enddef
2068 endclass 2057 endclass
2069 END 2058 END
2070 writefile(imported, 'XdefIntf2.vim', 'D') 2059 writefile(imported, 'XdefIntf2.vim', 'D')
2071 2060
2072 lines[1] = " import './XdefIntf2.vim' as defIntf" 2061 lines[1] = " import './XdefIntf2.vim' as defIntf"
2073 v9.CheckScriptSuccess(lines) 2062 v9.CheckScriptSuccess(lines)
2074 enddef 2063 enddef
2075 2064
2076 def Test_class_implements_interface() 2065 def Test_class_implements_interface()
2077 var lines =<< trim END 2066 var lines =<< trim END
2078 vim9script 2067 vim9script
2079 2068
2080 interface Some 2069 interface Some
2081 this.count: number 2070 this.count: number
2082 def Method(nr: number) 2071 def Method(nr: number)
2083 endinterface 2072 endinterface
2084 2073
2085 class SomeImpl implements Some 2074 class SomeImpl implements Some
2086 this.count: number 2075 this.count: number
2087 def Method(nr: number) 2076 def Method(nr: number)
2088 echo nr 2077 echo nr
2089 enddef 2078 enddef
2090 endclass 2079 endclass
2091 2080
2092 interface Another 2081 interface Another
2093 this.member: string 2082 this.member: string
2094 endinterface 2083 endinterface
2095 2084
2096 class AnotherImpl implements Some, Another 2085 class AnotherImpl implements Some, Another
2097 this.member = 'abc' 2086 this.member = 'abc'
2098 this.count = 20 2087 this.count = 20
2099 def Method(nr: number) 2088 def Method(nr: number)
2100 echo nr 2089 echo nr
2101 enddef 2090 enddef
2102 endclass 2091 endclass
2103 END 2092 END
2104 v9.CheckSourceSuccess(lines) 2093 v9.CheckSourceSuccess(lines)
2105 2094
2106 lines =<< trim END 2095 lines =<< trim END
2107 vim9script 2096 vim9script
2108 2097
2109 interface Some 2098 interface Some
2110 this.count: number 2099 this.count: number
2111 endinterface 2100 endinterface
2112 2101
2113 class SomeImpl implements Some implements Some 2102 class SomeImpl implements Some implements Some
2114 this.count: number 2103 this.count: number
2115 endclass 2104 endclass
2116 END 2105 END
2117 v9.CheckSourceFailure(lines, 'E1350:') 2106 v9.CheckSourceFailure(lines, 'E1350: Duplicate "implements"', 7)
2118 2107
2119 lines =<< trim END 2108 lines =<< trim END
2120 vim9script 2109 vim9script
2121 2110
2122 interface Some 2111 interface Some
2123 this.count: number 2112 this.count: number
2124 endinterface 2113 endinterface
2125 2114
2126 class SomeImpl implements Some, Some 2115 class SomeImpl implements Some, Some
2127 this.count: number 2116 this.count: number
2128 endclass 2117 endclass
2129 END 2118 END
2130 v9.CheckSourceFailure(lines, 'E1351: Duplicate interface after "implements": Some') 2119 v9.CheckSourceFailure(lines, 'E1351: Duplicate interface after "implements": Some', 7)
2131 2120
2132 lines =<< trim END 2121 lines =<< trim END
2133 vim9script 2122 vim9script
2134 2123
2135 interface Some 2124 interface Some
2136 this.counter: number 2125 this.counter: number
2137 def Method(nr: number) 2126 def Method(nr: number)
2138 endinterface 2127 endinterface
2139 2128
2140 class SomeImpl implements Some 2129 class SomeImpl implements Some
2141 this.count: number 2130 this.count: number
2142 def Method(nr: number) 2131 def Method(nr: number)
2143 echo nr 2132 echo nr
2144 enddef 2133 enddef
2145 endclass 2134 endclass
2146 END 2135 END
2147 v9.CheckSourceFailure(lines, 'E1348: Variable "counter" of interface "Some" is not implemented') 2136 v9.CheckSourceFailure(lines, 'E1348: Variable "counter" of interface "Some" is not implemented', 13)
2148 2137
2149 lines =<< trim END 2138 lines =<< trim END
2150 vim9script 2139 vim9script
2151 2140
2152 interface Some 2141 interface Some
2153 this.count: number 2142 this.count: number
2154 def Methods(nr: number) 2143 def Methods(nr: number)
2155 endinterface 2144 endinterface
2156 2145
2157 class SomeImpl implements Some 2146 class SomeImpl implements Some
2158 this.count: number 2147 this.count: number
2159 def Method(nr: number) 2148 def Method(nr: number)
2160 echo nr 2149 echo nr
2161 enddef 2150 enddef
2162 endclass 2151 endclass
2163 END 2152 END
2164 v9.CheckSourceFailure(lines, 'E1349: Method "Methods" of interface "Some" is not implemented') 2153 v9.CheckSourceFailure(lines, 'E1349: Method "Methods" of interface "Some" is not implemented', 13)
2165 2154
2166 # Check different order of members in class and interface works. 2155 # Check different order of members in class and interface works.
2167 lines =<< trim END 2156 lines =<< trim END
2168 vim9script 2157 vim9script
2169 2158
2170 interface Result 2159 interface Result
2171 this.label: string 2160 this.label: string
2172 this.errpos: number 2161 this.errpos: number
2173 endinterface 2162 endinterface
2177 public this.lnum: number = 5 2166 public this.lnum: number = 5
2178 this.errpos: number = 42 2167 this.errpos: number = 42
2179 this.label: string = 'label' 2168 this.label: string = 'label'
2180 endclass 2169 endclass
2181 2170
2182 def Test() 2171 def Test()
2183 var result: Result = Failure.new() 2172 var result: Result = Failure.new()
2184 2173
2185 assert_equal('label', result.label) 2174 assert_equal('label', result.label)
2186 assert_equal(42, result.errpos) 2175 assert_equal(42, result.errpos)
2187 enddef 2176 enddef
2188 2177
2189 Test() 2178 Test()
2190 END 2179 END
2191 v9.CheckSourceSuccess(lines) 2180 v9.CheckSourceSuccess(lines)
2192 2181
2193 # Interface name after "extends" doesn't end in a space or NUL character 2182 # Interface name after "extends" doesn't end in a space or NUL character
2194 lines =<< trim END 2183 lines =<< trim END
2196 interface A 2185 interface A
2197 endinterface 2186 endinterface
2198 class B extends A" 2187 class B extends A"
2199 endclass 2188 endclass
2200 END 2189 END
2201 v9.CheckSourceFailure(lines, 'E1315:') 2190 v9.CheckSourceFailure(lines, 'E1315: White space required after name: A"', 4)
2202 2191
2203 # Trailing characters after a class name 2192 # Trailing characters after a class name
2204 lines =<< trim END 2193 lines =<< trim END
2205 vim9script 2194 vim9script
2206 class A bbb 2195 class A bbb
2207 endclass 2196 endclass
2208 END 2197 END
2209 v9.CheckSourceFailure(lines, 'E488:') 2198 v9.CheckSourceFailure(lines, 'E488: Trailing characters: bbb', 2)
2210 2199
2211 # using "implements" with a non-existing class 2200 # using "implements" with a non-existing class
2212 lines =<< trim END 2201 lines =<< trim END
2213 vim9script 2202 vim9script
2214 class A implements B 2203 class A implements B
2215 endclass 2204 endclass
2216 END 2205 END
2217 v9.CheckSourceFailure(lines, 'E1346:') 2206 v9.CheckSourceFailure(lines, 'E1346: Interface name not found: B', 3)
2218 2207
2219 # using "implements" with a regular class 2208 # using "implements" with a regular class
2220 lines =<< trim END 2209 lines =<< trim END
2221 vim9script 2210 vim9script
2222 class A 2211 class A
2223 endclass 2212 endclass
2224 class B implements A 2213 class B implements A
2225 endclass 2214 endclass
2226 END 2215 END
2227 v9.CheckSourceFailure(lines, 'E1347:') 2216 v9.CheckSourceFailure(lines, 'E1347: Not a valid interface: A', 5)
2228 2217
2229 # using "implements" with a variable 2218 # using "implements" with a variable
2230 lines =<< trim END 2219 lines =<< trim END
2231 vim9script 2220 vim9script
2232 var T: number = 10 2221 var T: number = 10
2233 class A implements T 2222 class A implements T
2234 endclass 2223 endclass
2235 END 2224 END
2236 v9.CheckSourceFailure(lines, 'E1347:') 2225 v9.CheckSourceFailure(lines, 'E1347: Not a valid interface: T', 4)
2237 2226
2238 # implements should be followed by a white space 2227 # implements should be followed by a white space
2239 lines =<< trim END 2228 lines =<< trim END
2240 vim9script 2229 vim9script
2241 interface A 2230 interface A
2242 endinterface 2231 endinterface
2243 class B implements A; 2232 class B implements A;
2244 endclass 2233 endclass
2245 END 2234 END
2246 v9.CheckSourceFailure(lines, 'E1315:') 2235 v9.CheckSourceFailure(lines, 'E1315: White space required after name: A;', 4)
2247 2236
2248 lines =<< trim END 2237 lines =<< trim END
2249 vim9script 2238 vim9script
2250 2239
2251 interface One 2240 interface One
2252 def IsEven(nr: number): bool 2241 def IsEven(nr: number): bool
2253 endinterface 2242 endinterface
2254 class Two implements One 2243 class Two implements One
2255 def IsEven(nr: number): string 2244 def IsEven(nr: number): string
2256 enddef 2245 enddef
2257 endclass 2246 endclass
2258 END 2247 END
2259 v9.CheckSourceFailure(lines, 'E1383: Method "IsEven": type mismatch, expected func(number): bool but got func(number): string') 2248 v9.CheckSourceFailure(lines, 'E1383: Method "IsEven": type mismatch, expected func(number): bool but got func(number): string', 9)
2260 2249
2261 lines =<< trim END 2250 lines =<< trim END
2262 vim9script 2251 vim9script
2263 2252
2264 interface One 2253 interface One
2265 def IsEven(nr: number): bool 2254 def IsEven(nr: number): bool
2266 endinterface 2255 endinterface
2267 class Two implements One 2256 class Two implements One
2268 def IsEven(nr: bool): bool 2257 def IsEven(nr: bool): bool
2269 enddef 2258 enddef
2270 endclass 2259 endclass
2271 END 2260 END
2272 v9.CheckSourceFailure(lines, 'E1383: Method "IsEven": type mismatch, expected func(number): bool but got func(bool): bool') 2261 v9.CheckSourceFailure(lines, 'E1383: Method "IsEven": type mismatch, expected func(number): bool but got func(bool): bool', 9)
2273 2262
2274 lines =<< trim END 2263 lines =<< trim END
2275 vim9script 2264 vim9script
2276 2265
2277 interface One 2266 interface One
2278 def IsEven(nr: number): bool 2267 def IsEven(nr: number): bool
2279 endinterface 2268 endinterface
2280 class Two implements One 2269 class Two implements One
2281 def IsEven(nr: number, ...extra: list<number>): bool 2270 def IsEven(nr: number, ...extra: list<number>): bool
2282 enddef 2271 enddef
2283 endclass 2272 endclass
2284 END 2273 END
2285 v9.CheckSourceFailure(lines, 'E1383: Method "IsEven": type mismatch, expected func(number): bool but got func(number, ...list<number>): bool') 2274 v9.CheckSourceFailure(lines, 'E1383: Method "IsEven": type mismatch, expected func(number): bool but got func(number, ...list<number>): bool', 9)
2286 2275
2287 # access superclass interface members from subclass, mix variable order 2276 # access superclass interface members from subclass, mix variable order
2288 lines =<< trim END 2277 lines =<< trim END
2289 vim9script 2278 vim9script
2290 2279
2291 interface I1 2280 interface I1
2292 this.mvar1: number 2281 this.mvar1: number
2293 this.mvar2: number 2282 this.mvar2: number
2294 endinterface 2283 endinterface
2295 2284
2296 # NOTE: the order is swapped 2285 # NOTE: the order is swapped
2297 class A implements I1 2286 class A implements I1
2298 this.mvar2: number 2287 this.mvar2: number
2299 this.mvar1: number 2288 this.mvar1: number
2300 public static svar2: number 2289 public static svar2: number
2301 public static svar1: number 2290 public static svar1: number
2302 def new() 2291 def new()
2303 svar1 = 11 2292 svar1 = 11
2304 svar2 = 12 2293 svar2 = 12
2305 this.mvar1 = 111 2294 this.mvar1 = 111
2306 this.mvar2 = 112 2295 this.mvar2 = 112
2307 enddef 2296 enddef
2308 endclass 2297 endclass
2309 2298
2310 class B extends A 2299 class B extends A
2311 def new() 2300 def new()
2312 this.mvar1 = 121 2301 this.mvar1 = 121
2313 this.mvar2 = 122 2302 this.mvar2 = 122
2314 enddef 2303 enddef
2315 endclass 2304 endclass
2316 2305
2317 class C extends B 2306 class C extends B
2318 def new() 2307 def new()
2319 this.mvar1 = 131 2308 this.mvar1 = 131
2320 this.mvar2 = 132 2309 this.mvar2 = 132
2321 enddef 2310 enddef
2322 endclass 2311 endclass
2323 2312
2324 def F2(i: I1): list<number> 2313 def F2(i: I1): list<number>
2325 return [ i.mvar1, i.mvar2 ] 2314 return [ i.mvar1, i.mvar2 ]
2326 enddef 2315 enddef
2327 2316
2328 var oa = A.new() 2317 var oa = A.new()
2329 var ob = B.new() 2318 var ob = B.new()
2330 var oc = C.new() 2319 var oc = C.new()
2339 # Two interfaces, one on A, one on B; each has both kinds of variables 2328 # Two interfaces, one on A, one on B; each has both kinds of variables
2340 lines =<< trim END 2329 lines =<< trim END
2341 vim9script 2330 vim9script
2342 2331
2343 interface I1 2332 interface I1
2344 this.mvar1: number 2333 this.mvar1: number
2345 this.mvar2: number 2334 this.mvar2: number
2346 endinterface 2335 endinterface
2347 2336
2348 interface I2 2337 interface I2
2349 this.mvar3: number 2338 this.mvar3: number
2350 this.mvar4: number 2339 this.mvar4: number
2351 endinterface 2340 endinterface
2352 2341
2353 class A implements I1 2342 class A implements I1
2354 public static svar1: number 2343 public static svar1: number
2355 public static svar2: number 2344 public static svar2: number
2356 this.mvar1: number 2345 this.mvar1: number
2357 this.mvar2: number 2346 this.mvar2: number
2358 def new() 2347 def new()
2359 svar1 = 11 2348 svar1 = 11
2360 svar2 = 12 2349 svar2 = 12
2361 this.mvar1 = 111 2350 this.mvar1 = 111
2362 this.mvar2 = 112 2351 this.mvar2 = 112
2363 enddef 2352 enddef
2364 endclass 2353 endclass
2365 2354
2366 class B extends A implements I2 2355 class B extends A implements I2
2367 static svar3: number 2356 static svar3: number
2368 static svar4: number 2357 static svar4: number
2369 this.mvar3: number 2358 this.mvar3: number
2370 this.mvar4: number 2359 this.mvar4: number
2371 def new() 2360 def new()
2372 svar3 = 23 2361 svar3 = 23
2373 svar4 = 24 2362 svar4 = 24
2374 this.mvar1 = 121 2363 this.mvar1 = 121
2375 this.mvar2 = 122 2364 this.mvar2 = 122
2376 this.mvar3 = 123 2365 this.mvar3 = 123
2377 this.mvar4 = 124 2366 this.mvar4 = 124
2378 enddef 2367 enddef
2379 endclass 2368 endclass
2380 2369
2381 class C extends B 2370 class C extends B
2382 public static svar5: number 2371 public static svar5: number
2383 def new() 2372 def new()
2384 svar5 = 1001 2373 svar5 = 1001
2385 this.mvar1 = 131 2374 this.mvar1 = 131
2386 this.mvar2 = 132 2375 this.mvar2 = 132
2387 this.mvar3 = 133 2376 this.mvar3 = 133
2388 this.mvar4 = 134 2377 this.mvar4 = 134
2389 enddef 2378 enddef
2390 endclass 2379 endclass
2391 2380
2392 def F2(i: I1): list<number> 2381 def F2(i: I1): list<number>
2393 return [ i.mvar1, i.mvar2 ] 2382 return [ i.mvar1, i.mvar2 ]
2394 enddef 2383 enddef
2395 2384
2396 def F4(i: I2): list<number> 2385 def F4(i: I2): list<number>
2397 return [ i.mvar3, i.mvar4 ] 2386 return [ i.mvar3, i.mvar4 ]
2398 enddef 2387 enddef
2399 2388
2400 var oa = A.new() 2389 var oa = A.new()
2401 var ob = B.new() 2390 var ob = B.new()
2402 var oc = C.new() 2391 var oc = C.new()
2540 END 2529 END
2541 v9.CheckSourceSuccess(lines) 2530 v9.CheckSourceSuccess(lines)
2542 2531
2543 # No class that implements the interface. 2532 # No class that implements the interface.
2544 lines =<< trim END 2533 lines =<< trim END
2545 vim9script 2534 vim9script
2546 2535
2547 interface IWithEE 2536 interface IWithEE
2548 def Enter(): any 2537 def Enter(): any
2549 def Exit(): void 2538 def Exit(): void
2550 endinterface 2539 endinterface
2551 2540
2552 def With1(ee: IWithEE, F: func) 2541 def With1(ee: IWithEE, F: func)
2553 var r = ee.Enter() 2542 var r = ee.Enter()
2554 enddef 2543 enddef
2555 2544
2556 defcompile 2545 defcompile
2557 END 2546 END
2558 v9.CheckSourceSuccess(lines) 2547 v9.CheckSourceSuccess(lines)
2559 enddef 2548 enddef
2560 2549
2561 def Test_class_used_as_type() 2550 def Test_class_used_as_type()
2562 var lines =<< trim END 2551 var lines =<< trim END
2563 vim9script 2552 vim9script
2564 2553
2565 class Point 2554 class Point
2566 this.x = 0 2555 this.x = 0
2567 this.y = 0 2556 this.y = 0
2568 endclass 2557 endclass
2569 2558
2570 var p: Point 2559 var p: Point
2571 p = Point.new(2, 33) 2560 p = Point.new(2, 33)
2572 assert_equal(2, p.x) 2561 assert_equal(2, p.x)
2573 assert_equal(33, p.y) 2562 assert_equal(33, p.y)
2574 END 2563 END
2575 v9.CheckSourceSuccess(lines) 2564 v9.CheckSourceSuccess(lines)
2576 2565
2577 lines =<< trim END 2566 lines =<< trim END
2578 vim9script 2567 vim9script
2579 2568
2580 interface HasX 2569 interface HasX
2581 this.x: number 2570 this.x: number
2582 endinterface 2571 endinterface
2583 2572
2584 class Point implements HasX 2573 class Point implements HasX
2585 this.x = 0 2574 this.x = 0
2586 this.y = 0 2575 this.y = 0
2587 endclass 2576 endclass
2588 2577
2589 var p: Point 2578 var p: Point
2590 p = Point.new(2, 33) 2579 p = Point.new(2, 33)
2591 var hx = p 2580 var hx = p
2592 assert_equal(2, hx.x) 2581 assert_equal(2, hx.x)
2593 END 2582 END
2594 v9.CheckSourceSuccess(lines) 2583 v9.CheckSourceSuccess(lines)
2595 2584
2596 lines =<< trim END 2585 lines =<< trim END
2597 vim9script 2586 vim9script
2598 2587
2599 class Point 2588 class Point
2600 this.x = 0 2589 this.x = 0
2601 this.y = 0 2590 this.y = 0
2602 endclass 2591 endclass
2603 2592
2604 var p: Point 2593 var p: Point
2605 p = 'text' 2594 p = 'text'
2606 END 2595 END
2607 v9.CheckSourceFailure(lines, 'E1012: Type mismatch; expected object<Point> but got string') 2596 v9.CheckSourceFailure(lines, 'E1012: Type mismatch; expected object<Point> but got string', 9)
2608 enddef 2597 enddef
2609 2598
2610 def Test_class_extends() 2599 def Test_class_extends()
2611 var lines =<< trim END 2600 var lines =<< trim END
2612 vim9script 2601 vim9script
2613 class Base 2602 class Base
2614 this.one = 1 2603 this.one = 1
2615 def GetOne(): number 2604 def GetOne(): number
2616 return this.one 2605 return this.one
2617 enddef 2606 enddef
2618 endclass 2607 endclass
2619 class Child extends Base 2608 class Child extends Base
2620 this.two = 2 2609 this.two = 2
2621 def GetTotal(): number 2610 def GetTotal(): number
2622 return this.one + this.two 2611 return this.one + this.two
2623 enddef 2612 enddef
2624 endclass 2613 endclass
2625 var o = Child.new() 2614 var o = Child.new()
2626 assert_equal(1, o.one) 2615 assert_equal(1, o.one)
2627 assert_equal(2, o.two) 2616 assert_equal(2, o.two)
2628 assert_equal(1, o.GetOne()) 2617 assert_equal(1, o.GetOne())
2629 assert_equal(3, o.GetTotal()) 2618 assert_equal(3, o.GetTotal())
2630 END 2619 END
2631 v9.CheckSourceSuccess(lines) 2620 v9.CheckSourceSuccess(lines)
2632 2621
2633 lines =<< trim END 2622 lines =<< trim END
2634 vim9script 2623 vim9script
2635 class Base 2624 class Base
2636 this.one = 1 2625 this.one = 1
2637 endclass 2626 endclass
2638 class Child extends Base 2627 class Child extends Base
2639 this.two = 2 2628 this.two = 2
2640 endclass 2629 endclass
2641 var o = Child.new(3, 44) 2630 var o = Child.new(3, 44)
2642 assert_equal(3, o.one) 2631 assert_equal(3, o.one)
2643 assert_equal(44, o.two) 2632 assert_equal(44, o.two)
2644 END 2633 END
2645 v9.CheckSourceSuccess(lines) 2634 v9.CheckSourceSuccess(lines)
2646 2635
2647 lines =<< trim END 2636 lines =<< trim END
2648 vim9script 2637 vim9script
2649 class Base 2638 class Base
2650 this.one = 1 2639 this.one = 1
2651 endclass 2640 endclass
2652 class Child extends Base extends Base 2641 class Child extends Base extends Base
2653 this.two = 2 2642 this.two = 2
2654 endclass 2643 endclass
2655 END 2644 END
2656 v9.CheckSourceFailure(lines, 'E1352: Duplicate "extends"') 2645 v9.CheckSourceFailure(lines, 'E1352: Duplicate "extends"', 5)
2657 2646
2658 lines =<< trim END 2647 lines =<< trim END
2659 vim9script 2648 vim9script
2660 class Child extends BaseClass 2649 class Child extends BaseClass
2661 this.two = 2 2650 this.two = 2
2662 endclass 2651 endclass
2663 END 2652 END
2664 v9.CheckSourceFailure(lines, 'E1353: Class name not found: BaseClass') 2653 v9.CheckSourceFailure(lines, 'E1353: Class name not found: BaseClass', 4)
2665 2654
2666 lines =<< trim END 2655 lines =<< trim END
2667 vim9script 2656 vim9script
2668 var SomeVar = 99 2657 var SomeVar = 99
2669 class Child extends SomeVar 2658 class Child extends SomeVar
2670 this.two = 2 2659 this.two = 2
2671 endclass 2660 endclass
2672 END 2661 END
2673 v9.CheckSourceFailure(lines, 'E1354: Cannot extend SomeVar') 2662 v9.CheckSourceFailure(lines, 'E1354: Cannot extend SomeVar', 5)
2674 2663
2675 lines =<< trim END 2664 lines =<< trim END
2676 vim9script 2665 vim9script
2677 class Base 2666 class Base
2678 this.name: string 2667 this.name: string
2679 def ToString(): string
2680 return this.name
2681 enddef
2682 endclass
2683
2684 class Child extends Base
2685 this.age: number
2686 def ToString(): string
2687 return super.ToString() .. ': ' .. this.age
2688 enddef
2689 endclass
2690
2691 var o = Child.new('John', 42)
2692 assert_equal('John: 42', o.ToString())
2693 END
2694 v9.CheckSourceSuccess(lines)
2695
2696 lines =<< trim END
2697 vim9script
2698 class Child
2699 this.age: number
2700 def ToString(): number
2701 return this.age
2702 enddef
2703 def ToString(): string
2704 return this.age
2705 enddef
2706 endclass
2707 END
2708 v9.CheckSourceFailure(lines, 'E1355: Duplicate function: ToString')
2709
2710 lines =<< trim END
2711 vim9script
2712 class Child
2713 this.age: number
2714 def ToString(): string
2715 return super .ToString() .. ': ' .. this.age
2716 enddef
2717 endclass
2718 var o = Child.new(42)
2719 echo o.ToString()
2720 END
2721 v9.CheckSourceFailure(lines, 'E1356:')
2722
2723 lines =<< trim END
2724 vim9script
2725 class Base
2726 this.name: string
2727 def ToString(): string
2728 return this.name
2729 enddef
2730 endclass
2731
2732 var age = 42
2733 def ToString(): string 2668 def ToString(): string
2734 return super.ToString() .. ': ' .. age 2669 return this.name
2735 enddef 2670 enddef
2736 echo ToString() 2671 endclass
2737 END 2672
2738 v9.CheckSourceFailure(lines, 'E1357:') 2673 class Child extends Base
2739 2674 this.age: number
2740 lines =<< trim END 2675 def ToString(): string
2741 vim9script 2676 return super.ToString() .. ': ' .. this.age
2742 class Child 2677 enddef
2743 this.age: number 2678 endclass
2744 def ToString(): string 2679
2745 return super.ToString() .. ': ' .. this.age 2680 var o = Child.new('John', 42)
2746 enddef 2681 assert_equal('John: 42', o.ToString())
2747 endclass 2682 END
2748 var o = Child.new(42) 2683 v9.CheckSourceSuccess(lines)
2749 echo o.ToString() 2684
2750 END 2685 lines =<< trim END
2751 v9.CheckSourceFailure(lines, 'E1358:') 2686 vim9script
2752 2687 class Child
2753 lines =<< trim END 2688 this.age: number
2754 vim9script 2689 def ToString(): number
2755 class Base 2690 return this.age
2756 this.name: string 2691 enddef
2757 static def ToString(): string 2692 def ToString(): string
2758 return 'Base class' 2693 return this.age
2759 enddef 2694 enddef
2760 endclass 2695 endclass
2761 2696 END
2762 class Child extends Base 2697 v9.CheckSourceFailure(lines, 'E1355: Duplicate function: ToString', 9)
2763 this.age: number 2698
2764 def ToString(): string 2699 lines =<< trim END
2765 return Base.ToString() .. ': ' .. this.age 2700 vim9script
2766 enddef 2701 class Child
2767 endclass 2702 this.age: number
2768 2703 def ToString(): string
2769 var o = Child.new('John', 42) 2704 return super .ToString() .. ': ' .. this.age
2770 assert_equal('Base class: 42', o.ToString()) 2705 enddef
2771 END 2706 endclass
2772 v9.CheckSourceSuccess(lines) 2707 var o = Child.new(42)
2773 2708 echo o.ToString()
2774 lines =<< trim END 2709 END
2775 vim9script 2710 v9.CheckSourceFailure(lines, 'E1356: "super" must be followed by a dot', 1)
2776 class Base 2711
2777 this.value = 1 2712 lines =<< trim END
2778 def new(init: number) 2713 vim9script
2779 this.value = number + 1 2714 class Base
2780 enddef 2715 this.name: string
2781 endclass 2716 def ToString(): string
2782 class Child extends Base 2717 return this.name
2783 def new() 2718 enddef
2784 this.new(3) 2719 endclass
2785 enddef 2720
2786 endclass 2721 var age = 42
2787 var c = Child.new() 2722 def ToString(): string
2788 END 2723 return super.ToString() .. ': ' .. age
2789 v9.CheckSourceFailure(lines, 'E1385: Class method "new" accessible only using class "Child"') 2724 enddef
2725 echo ToString()
2726 END
2727 v9.CheckSourceFailure(lines, 'E1357: Using "super" not in a class method', 1)
2728
2729 lines =<< trim END
2730 vim9script
2731 class Child
2732 this.age: number
2733 def ToString(): string
2734 return super.ToString() .. ': ' .. this.age
2735 enddef
2736 endclass
2737 var o = Child.new(42)
2738 echo o.ToString()
2739 END
2740 v9.CheckSourceFailure(lines, 'E1358: Using "super" not in a child class', 1)
2741
2742 lines =<< trim END
2743 vim9script
2744 class Base
2745 this.name: string
2746 static def ToString(): string
2747 return 'Base class'
2748 enddef
2749 endclass
2750
2751 class Child extends Base
2752 this.age: number
2753 def ToString(): string
2754 return Base.ToString() .. ': ' .. this.age
2755 enddef
2756 endclass
2757
2758 var o = Child.new('John', 42)
2759 assert_equal('Base class: 42', o.ToString())
2760 END
2761 v9.CheckSourceSuccess(lines)
2762
2763 lines =<< trim END
2764 vim9script
2765 class Base
2766 this.value = 1
2767 def new(init: number)
2768 this.value = number + 1
2769 enddef
2770 endclass
2771 class Child extends Base
2772 def new()
2773 this.new(3)
2774 enddef
2775 endclass
2776 var c = Child.new()
2777 END
2778 v9.CheckSourceFailure(lines, 'E1385: Class method "new" accessible only using class "Child"', 1)
2790 2779
2791 # base class with more than one object member 2780 # base class with more than one object member
2792 lines =<< trim END 2781 lines =<< trim END
2793 vim9script 2782 vim9script
2794 2783
2795 class Result 2784 class Result
2796 this.success: bool 2785 this.success: bool
2797 this.value: any = null 2786 this.value: any = null
2798 endclass 2787 endclass
2799 2788
2800 class Success extends Result 2789 class Success extends Result
2801 def new(this.value = v:none) 2790 def new(this.value = v:none)
2802 this.success = true 2791 this.success = true
2803 enddef 2792 enddef
2804 endclass 2793 endclass
2805 2794
2806 var v = Success.new('asdf') 2795 var v = Success.new('asdf')
2807 assert_equal("object of Success {success: true, value: 'asdf'}", string(v)) 2796 assert_equal("object of Success {success: true, value: 'asdf'}", string(v))
2808 END 2797 END
2809 v9.CheckSourceSuccess(lines) 2798 v9.CheckSourceSuccess(lines)
2810 2799
2811 # class name after "extends" doesn't end in a space or NUL character 2800 # class name after "extends" doesn't end in a space or NUL character
2812 lines =<< trim END 2801 lines =<< trim END
2814 class A 2803 class A
2815 endclass 2804 endclass
2816 class B extends A" 2805 class B extends A"
2817 endclass 2806 endclass
2818 END 2807 END
2819 v9.CheckSourceFailure(lines, 'E1315:') 2808 v9.CheckSourceFailure(lines, 'E1315: White space required after name: A"', 4)
2820 enddef 2809 enddef
2821 2810
2822 def Test_using_base_class() 2811 def Test_using_base_class()
2823 var lines =<< trim END 2812 var lines =<< trim END
2824 vim9script 2813 vim9script
2825 2814
2826 class BaseEE 2815 class BaseEE
2827 def Enter(): any 2816 def Enter(): any
2828 return null 2817 return null
2829 enddef 2818 enddef
2830 def Exit(resource: any): void 2819 def Exit(resource: any): void
2831 enddef 2820 enddef
2832 endclass 2821 endclass
2833 2822
2834 class ChildEE extends BaseEE 2823 class ChildEE extends BaseEE
2835 def Enter(): any 2824 def Enter(): any
2836 return 42 2825 return 42
2837 enddef 2826 enddef
2838 2827
2839 def Exit(resource: number): void 2828 def Exit(resource: number): void
2840 g:result ..= '/exit' 2829 g:result ..= '/exit'
2841 enddef 2830 enddef
2842 endclass 2831 endclass
2843 2832
2844 def With(ee: BaseEE) 2833 def With(ee: BaseEE)
2845 var r = ee.Enter() 2834 var r = ee.Enter()
2846 try 2835 try
2847 g:result ..= r 2836 g:result ..= r
2848 finally 2837 finally
2849 g:result ..= '/finally' 2838 g:result ..= '/finally'
2850 ee.Exit(r) 2839 ee.Exit(r)
2851 endtry 2840 endtry
2852 enddef 2841 enddef
2853 2842
2854 g:result = '' 2843 g:result = ''
2855 With(ChildEE.new()) 2844 With(ChildEE.new())
2856 assert_equal('42/finally/exit', g:result) 2845 assert_equal('42/finally/exit', g:result)
2861 # Using super, Child invokes Base method which has optional arg. #12471 2850 # Using super, Child invokes Base method which has optional arg. #12471
2862 lines =<< trim END 2851 lines =<< trim END
2863 vim9script 2852 vim9script
2864 2853
2865 class Base 2854 class Base
2866 this.success: bool = false 2855 this.success: bool = false
2867 def Method(arg = 0) 2856 def Method(arg = 0)
2868 this.success = true 2857 this.success = true
2869 enddef 2858 enddef
2870 endclass 2859 endclass
2871 2860
2872 class Child extends Base 2861 class Child extends Base
2873 def new() 2862 def new()
2874 super.Method() 2863 super.Method()
2875 enddef 2864 enddef
2876 endclass 2865 endclass
2877 2866
2878 var obj = Child.new() 2867 var obj = Child.new()
2879 assert_equal(true, obj.success) 2868 assert_equal(true, obj.success)
2880 END 2869 END
2881 v9.CheckSourceSuccess(lines) 2870 v9.CheckSourceSuccess(lines)
2882 enddef 2871 enddef
2883 2872
2884 def Test_class_import() 2873 def Test_class_import()
2885 var lines =<< trim END 2874 var lines =<< trim END
2886 vim9script 2875 vim9script
2887 export class Animal 2876 export class Animal
2888 this.kind: string 2877 this.kind: string
2889 this.name: string 2878 this.name: string
2890 endclass 2879 endclass
2891 END 2880 END
2892 writefile(lines, 'Xanimal.vim', 'D') 2881 writefile(lines, 'Xanimal.vim', 'D')
2893 2882
2894 lines =<< trim END 2883 lines =<< trim END
2895 vim9script 2884 vim9script
2896 import './Xanimal.vim' as animal 2885 import './Xanimal.vim' as animal
2897 2886
2898 var a: animal.Animal 2887 var a: animal.Animal
2899 a = animal.Animal.new('fish', 'Eric') 2888 a = animal.Animal.new('fish', 'Eric')
2900 assert_equal('fish', a.kind) 2889 assert_equal('fish', a.kind)
2901 assert_equal('Eric', a.name) 2890 assert_equal('Eric', a.name)
2902 2891
2903 var b: animal.Animal = animal.Animal.new('cat', 'Garfield') 2892 var b: animal.Animal = animal.Animal.new('cat', 'Garfield')
2904 assert_equal('cat', b.kind) 2893 assert_equal('cat', b.kind)
2905 assert_equal('Garfield', b.name) 2894 assert_equal('Garfield', b.name)
2906 END 2895 END
2907 v9.CheckScriptSuccess(lines) 2896 v9.CheckScriptSuccess(lines)
2908 enddef 2897 enddef
2909 2898
2910 def Test_abstract_class() 2899 def Test_abstract_class()
2911 var lines =<< trim END 2900 var lines =<< trim END
2912 vim9script 2901 vim9script
2913 abstract class Base 2902 abstract class Base
2914 this.name: string 2903 this.name: string
2915 endclass 2904 endclass
2916 class Person extends Base 2905 class Person extends Base
2917 this.age: number 2906 this.age: number
2918 endclass 2907 endclass
2919 var p: Base = Person.new('Peter', 42) 2908 var p: Base = Person.new('Peter', 42)
2920 assert_equal('Peter', p.name) 2909 assert_equal('Peter', p.name)
2921 assert_equal(42, p.age) 2910 assert_equal(42, p.age)
2922 END 2911 END
2923 v9.CheckSourceSuccess(lines) 2912 v9.CheckSourceSuccess(lines)
2924 2913
2925 lines =<< trim END 2914 lines =<< trim END
2926 vim9script 2915 vim9script
2927 abstract class Base 2916 abstract class Base
2928 this.name: string 2917 this.name: string
2929 endclass 2918 endclass
2930 class Person extends Base 2919 class Person extends Base
2931 this.age: number 2920 this.age: number
2932 endclass 2921 endclass
2933 var p = Base.new('Peter') 2922 var p = Base.new('Peter')
2934 END 2923 END
2935 v9.CheckSourceFailure(lines, 'E1325: Method not found on class "Base": new') 2924 v9.CheckSourceFailure(lines, 'E1325: Method not found on class "Base": new', 8)
2936 2925
2937 lines =<< trim END 2926 lines =<< trim END
2938 abstract class Base 2927 abstract class Base
2939 this.name: string 2928 this.name: string
2940 endclass 2929 endclass
2941 END 2930 END
2942 v9.CheckSourceFailure(lines, 'E1316:') 2931 v9.CheckSourceFailure(lines, 'E1316: Class can only be defined in Vim9 script', 1)
2943 2932
2944 # Abstract class cannot have a "new" function 2933 # Abstract class cannot have a "new" function
2945 lines =<< trim END 2934 lines =<< trim END
2946 vim9script 2935 vim9script
2947 abstract class Base 2936 abstract class Base
2948 def new() 2937 def new()
2949 enddef 2938 enddef
2950 endclass 2939 endclass
2951 END 2940 END
2952 v9.CheckSourceFailure(lines, 'E1359:') 2941 v9.CheckSourceFailure(lines, 'E1359: Cannot define a "new" method in an abstract class', 4)
2953 enddef 2942 enddef
2954 2943
2955 def Test_closure_in_class() 2944 def Test_closure_in_class()
2956 var lines =<< trim END 2945 var lines =<< trim END
2957 vim9script 2946 vim9script
2958 2947
2959 class Foo 2948 class Foo
2960 this.y: list<string> = ['B'] 2949 this.y: list<string> = ['B']
2961 2950
2962 def new() 2951 def new()
2963 g:result = filter(['A', 'B'], (_, v) => index(this.y, v) == -1) 2952 g:result = filter(['A', 'B'], (_, v) => index(this.y, v) == -1)
2964 enddef 2953 enddef
2965 endclass 2954 endclass
2966 2955
2967 Foo.new() 2956 Foo.new()
2968 assert_equal(['A'], g:result) 2957 assert_equal(['A'], g:result)
2969 END 2958 END
2970 v9.CheckSourceSuccess(lines) 2959 v9.CheckSourceSuccess(lines)
2971 enddef 2960 enddef
2972 2961
2973 def Test_call_constructor_from_legacy() 2962 def Test_call_constructor_from_legacy()
2974 var lines =<< trim END 2963 var lines =<< trim END
2975 vim9script 2964 vim9script
2976 2965
2977 var newCalled = 'false' 2966 var newCalled = 'false'
2978 2967
2979 class A 2968 class A
2980 def new() 2969 def new()
2981 newCalled = 'true' 2970 newCalled = 'true'
2982 enddef 2971 enddef
2983 endclass 2972 endclass
2984 2973
2985 export def F(options = {}): any 2974 export def F(options = {}): any
2986 return A 2975 return A
2987 enddef 2976 enddef
2988 2977
2989 g:p = F() 2978 g:p = F()
2990 legacy call p.new() 2979 legacy call p.new()
2991 assert_equal('true', newCalled) 2980 assert_equal('true', newCalled)
2992 END 2981 END
2993 v9.CheckSourceSuccess(lines) 2982 v9.CheckSourceSuccess(lines)
2994 enddef 2983 enddef
2995 2984
2996 def Test_defer_with_object() 2985 def Test_defer_with_object()
2997 var lines =<< trim END 2986 var lines =<< trim END
2998 vim9script 2987 vim9script
2999 2988
3000 class CWithEE 2989 class CWithEE
3001 def Enter() 2990 def Enter()
3002 g:result ..= "entered/" 2991 g:result ..= "entered/"
3003 enddef 2992 enddef
3004 def Exit() 2993 def Exit()
3005 g:result ..= "exited" 2994 g:result ..= "exited"
3006 enddef 2995 enddef
3007 endclass 2996 endclass
3008 2997
3009 def With(ee: CWithEE, F: func) 2998 def With(ee: CWithEE, F: func)
3010 ee.Enter() 2999 ee.Enter()
3011 defer ee.Exit() 3000 defer ee.Exit()
3012 F() 3001 F()
3013 enddef 3002 enddef
3014 3003
3015 g:result = '' 3004 g:result = ''
3016 var obj = CWithEE.new() 3005 var obj = CWithEE.new()
3017 obj->With(() => { 3006 obj->With(() => {
3018 g:result ..= "called/" 3007 g:result ..= "called/"
3019 }) 3008 })
3020 assert_equal('entered/called/exited', g:result) 3009 assert_equal('entered/called/exited', g:result)
3021 END 3010 END
3022 v9.CheckSourceSuccess(lines) 3011 v9.CheckSourceSuccess(lines)
3023 unlet g:result 3012 unlet g:result
3024 3013
3025 lines =<< trim END 3014 lines =<< trim END
3026 vim9script 3015 vim9script
3027 3016
3028 class BaseWithEE 3017 class BaseWithEE
3029 def Enter() 3018 def Enter()
3030 g:result ..= "entered-base/" 3019 g:result ..= "entered-base/"
3031 enddef 3020 enddef
3032 def Exit() 3021 def Exit()
3033 g:result ..= "exited-base" 3022 g:result ..= "exited-base"
3034 enddef 3023 enddef
3035 endclass 3024 endclass
3036 3025
3037 class CWithEE extends BaseWithEE 3026 class CWithEE extends BaseWithEE
3038 def Enter() 3027 def Enter()
3039 g:result ..= "entered-child/" 3028 g:result ..= "entered-child/"
3040 enddef 3029 enddef
3041 def Exit() 3030 def Exit()
3042 g:result ..= "exited-child" 3031 g:result ..= "exited-child"
3043 enddef 3032 enddef
3044 endclass 3033 endclass
3045 3034
3046 def With(ee: BaseWithEE, F: func) 3035 def With(ee: BaseWithEE, F: func)
3047 ee.Enter() 3036 ee.Enter()
3048 defer ee.Exit() 3037 defer ee.Exit()
3049 F() 3038 F()
3050 enddef 3039 enddef
3051 3040
3052 g:result = '' 3041 g:result = ''
3053 var obj = CWithEE.new() 3042 var obj = CWithEE.new()
3054 obj->With(() => { 3043 obj->With(() => {
3055 g:result ..= "called/" 3044 g:result ..= "called/"
3056 }) 3045 })
3057 assert_equal('entered-child/called/exited-child', g:result) 3046 assert_equal('entered-child/called/exited-child', g:result)
3058 END 3047 END
3059 v9.CheckSourceSuccess(lines) 3048 v9.CheckSourceSuccess(lines)
3060 unlet g:result 3049 unlet g:result
3061 enddef 3050 enddef
3062 3051
3344 def Test_stack_expansion_with_methods() 3333 def Test_stack_expansion_with_methods()
3345 var lines =<< trim END 3334 var lines =<< trim END
3346 vim9script 3335 vim9script
3347 3336
3348 class C 3337 class C
3349 def M1() 3338 def M1()
3350 F0() 3339 F0()
3351 enddef 3340 enddef
3352 endclass 3341 endclass
3353 3342
3354 def F0() 3343 def F0()
3355 assert_match('<SNR>\d\+_F\[1\]\.\.C\.M1\[1\]\.\.<SNR>\d\+_F0\[1\]$', expand('<stack>')) 3344 assert_match('<SNR>\d\+_F\[1\]\.\.C\.M1\[1\]\.\.<SNR>\d\+_F0\[1\]$', expand('<stack>'))
3356 enddef 3345 enddef
3357 3346
3358 def F() 3347 def F()
3359 C.new().M1() 3348 C.new().M1()
3360 enddef 3349 enddef
3361 3350
3362 F() 3351 F()
3363 END 3352 END
3364 v9.CheckSourceSuccess(lines) 3353 v9.CheckSourceSuccess(lines)
3440 return this 3429 return this
3441 endif 3430 endif
3442 enddef 3431 enddef
3443 endclass 3432 endclass
3444 END 3433 END
3445 v9.CheckSourceFailure(lines, 'E1365:') 3434 v9.CheckSourceFailure(lines, 'E1365: Cannot use a return type with the "new" method', 11)
3446 3435
3447 # new() uses 'Dict' return type and returns a Dict 3436 # new() uses 'Dict' return type and returns a Dict
3448 lines =<< trim END 3437 lines =<< trim END
3449 vim9script 3438 vim9script
3450 3439
3458 endclass 3447 endclass
3459 3448
3460 var c = C.new() 3449 var c = C.new()
3461 assert_equal('object<C>', typename(c)) 3450 assert_equal('object<C>', typename(c))
3462 END 3451 END
3463 v9.CheckSourceFailure(lines, 'E1365:') 3452 v9.CheckSourceFailure(lines, 'E1365: Cannot use a return type with the "new" method', 9)
3464 enddef 3453 enddef
3465 3454
3466 " Test for checking a member initialization type at run time. 3455 " Test for checking a member initialization type at run time.
3467 def Test_runtime_type_check_for_member_init() 3456 def Test_runtime_type_check_for_member_init()
3468 var lines =<< trim END 3457 var lines =<< trim END
3469 vim9script 3458 vim9script
3470 3459
3471 var retnum: bool = false 3460 var retnum: bool = false
3472 3461
3473 def F(): any 3462 def F(): any
3474 retnum = !retnum 3463 retnum = !retnum
3475 if retnum 3464 if retnum
3476 return 1 3465 return 1
3477 else 3466 else
3478 return "hello" 3467 return "hello"
3479 endif 3468 endif
3480 enddef 3469 enddef
3481 3470
3482 class C 3471 class C
3483 this._foo: bool = F() 3472 this._foo: bool = F()
3484 endclass 3473 endclass
3485 3474
3486 var c1 = C.new() 3475 var c1 = C.new()
3487 var c2 = C.new() 3476 var c2 = C.new()
3488 END 3477 END
3489 v9.CheckSourceFailure(lines, 'E1012:') 3478 v9.CheckSourceFailure(lines, 'E1012: Type mismatch; expected bool but got string', 0)
3490 enddef 3479 enddef
3491 3480
3492 " Test for locking a variable referring to an object and reassigning to another 3481 " Test for locking a variable referring to an object and reassigning to another
3493 " object. 3482 " object.
3494 def Test_object_lockvar() 3483 def Test_object_lockvar()
3538 enddef 3527 enddef
3539 endclass 3528 endclass
3540 var a = A.new() 3529 var a = A.new()
3541 a._Foo() 3530 a._Foo()
3542 END 3531 END
3543 v9.CheckSourceFailure(lines, 'E1366: Cannot access private method: _Foo()') 3532 v9.CheckSourceFailure(lines, 'E1366: Cannot access private method: _Foo()', 9)
3544 3533
3545 # Try calling a private method using an object (from a def function) 3534 # Try calling a private method using an object (from a def function)
3546 lines =<< trim END 3535 lines =<< trim END
3547 vim9script 3536 vim9script
3548 3537
3555 var a = A.new() 3544 var a = A.new()
3556 a._Foo() 3545 a._Foo()
3557 enddef 3546 enddef
3558 T() 3547 T()
3559 END 3548 END
3560 v9.CheckSourceFailure(lines, 'E1366: Cannot access private method: _Foo()') 3549 v9.CheckSourceFailure(lines, 'E1366: Cannot access private method: _Foo()', 2)
3561 3550
3562 # Use a private method from another object method (in script context) 3551 # Use a private method from another object method (in script context)
3563 lines =<< trim END 3552 lines =<< trim END
3564 vim9script 3553 vim9script
3565 3554
3609 enddef 3598 enddef
3610 endclass 3599 endclass
3611 var a = A.new() 3600 var a = A.new()
3612 a.Bar() 3601 a.Bar()
3613 END 3602 END
3614 v9.CheckSourceFailure(lines, 'E117: Unknown function: _Foo') 3603 v9.CheckSourceFailure(lines, 'E117: Unknown function: _Foo', 1)
3615 3604
3616 # Try calling a private method using the class name 3605 # Try calling a private method using the class name
3617 lines =<< trim END 3606 lines =<< trim END
3618 vim9script 3607 vim9script
3619 3608
3622 return 1234 3611 return 1234
3623 enddef 3612 enddef
3624 endclass 3613 endclass
3625 A._Foo() 3614 A._Foo()
3626 END 3615 END
3627 v9.CheckSourceFailure(lines, 'E1366: Cannot access private method: _Foo') 3616 v9.CheckSourceFailure(lines, 'E1366: Cannot access private method: _Foo', 8)
3628 3617
3629 # Define two private methods with the same name 3618 # Define two private methods with the same name
3630 lines =<< trim END 3619 lines =<< trim END
3631 vim9script 3620 vim9script
3632 3621
3636 def _Foo() 3625 def _Foo()
3637 enddef 3626 enddef
3638 endclass 3627 endclass
3639 var a = A.new() 3628 var a = A.new()
3640 END 3629 END
3641 v9.CheckSourceFailure(lines, 'E1355: Duplicate function: _Foo') 3630 v9.CheckSourceFailure(lines, 'E1355: Duplicate function: _Foo', 7)
3642 3631
3643 # Define a private method and a object method with the same name 3632 # Define a private method and a object method with the same name
3644 lines =<< trim END 3633 lines =<< trim END
3645 vim9script 3634 vim9script
3646 3635
3650 def Foo() 3639 def Foo()
3651 enddef 3640 enddef
3652 endclass 3641 endclass
3653 var a = A.new() 3642 var a = A.new()
3654 END 3643 END
3655 v9.CheckSourceFailure(lines, 'E1355: Duplicate function: Foo') 3644 v9.CheckSourceFailure(lines, 'E1355: Duplicate function: Foo', 7)
3656 3645
3657 # Define an object method and a private method with the same name 3646 # Define an object method and a private method with the same name
3658 lines =<< trim END 3647 lines =<< trim END
3659 vim9script 3648 vim9script
3660 3649
3664 def _Foo() 3653 def _Foo()
3665 enddef 3654 enddef
3666 endclass 3655 endclass
3667 var a = A.new() 3656 var a = A.new()
3668 END 3657 END
3669 v9.CheckSourceFailure(lines, 'E1355: Duplicate function: _Foo') 3658 v9.CheckSourceFailure(lines, 'E1355: Duplicate function: _Foo', 7)
3670 3659
3671 # Call a public method and a private method from a private method 3660 # Call a public method and a private method from a private method
3672 lines =<< trim END 3661 lines =<< trim END
3673 vim9script 3662 vim9script
3674 3663
3708 enddef 3697 enddef
3709 endclass 3698 endclass
3710 var b = B.new() 3699 var b = B.new()
3711 b.Foo() 3700 b.Foo()
3712 END 3701 END
3713 v9.CheckSourceFailure(lines, 'E1366: Cannot access private method: _Foo()') 3702 v9.CheckSourceFailure(lines, 'E1366: Cannot access private method: _Foo()', 2)
3714 3703
3715 # Call a private object method from a child class object method 3704 # Call a private object method from a child class object method
3716 lines =<< trim END 3705 lines =<< trim END
3717 vim9script 3706 vim9script
3718 class A 3707 class A
3751 enddef 3740 enddef
3752 endclass 3741 endclass
3753 var c = C.new() 3742 var c = C.new()
3754 assert_equal(1234, c._Foo()) 3743 assert_equal(1234, c._Foo())
3755 END 3744 END
3756 v9.CheckSourceFailure(lines, 'E1366: Cannot access private method: _Foo()') 3745 v9.CheckSourceFailure(lines, 'E1366: Cannot access private method: _Foo()', 16)
3757 3746
3758 # Using "_" prefix in a method name should fail outside of a class 3747 # Using "_" prefix in a method name should fail outside of a class
3759 lines =<< trim END 3748 lines =<< trim END
3760 vim9script 3749 vim9script
3761 def _Foo(): number 3750 def _Foo(): number
3762 return 1234 3751 return 1234
3763 enddef 3752 enddef
3764 var a = _Foo() 3753 var a = _Foo()
3765 END 3754 END
3766 v9.CheckSourceFailure(lines, 'E1267: Function name must start with a capital: _Foo(): number') 3755 v9.CheckSourceFailure(lines, 'E1267: Function name must start with a capital: _Foo(): number', 2)
3767 enddef 3756 enddef
3768 3757
3769 " Test for an private class method 3758 " Test for an private class method
3770 def Test_private_class_method() 3759 def Test_private_class_method()
3771 # Try calling a class private method (at the script level) 3760 # Try calling a class private method (at the script level)
3777 return 1234 3766 return 1234
3778 enddef 3767 enddef
3779 endclass 3768 endclass
3780 A._Foo() 3769 A._Foo()
3781 END 3770 END
3782 v9.CheckSourceFailure(lines, 'E1366: Cannot access private method: _Foo()') 3771 v9.CheckSourceFailure(lines, 'E1366: Cannot access private method: _Foo()', 8)
3783 3772
3784 # Try calling a class private method (from a def function) 3773 # Try calling a class private method (from a def function)
3785 lines =<< trim END 3774 lines =<< trim END
3786 vim9script 3775 vim9script
3787 3776
3793 def T() 3782 def T()
3794 A._Foo() 3783 A._Foo()
3795 enddef 3784 enddef
3796 T() 3785 T()
3797 END 3786 END
3798 v9.CheckSourceFailure(lines, 'E1366: Cannot access private method: _Foo()') 3787 v9.CheckSourceFailure(lines, 'E1366: Cannot access private method: _Foo()', 1)
3799 3788
3800 # Try calling a class private method using an object (at the script level) 3789 # Try calling a class private method using an object (at the script level)
3801 lines =<< trim END 3790 lines =<< trim END
3802 vim9script 3791 vim9script
3803 3792
3807 enddef 3796 enddef
3808 endclass 3797 endclass
3809 var a = A.new() 3798 var a = A.new()
3810 a._Foo() 3799 a._Foo()
3811 END 3800 END
3812 v9.CheckSourceFailure(lines, 'E1366: Cannot access private method: _Foo') 3801 v9.CheckSourceFailure(lines, 'E1366: Cannot access private method: _Foo', 9)
3813 3802
3814 # Try calling a class private method using an object (from a def function) 3803 # Try calling a class private method using an object (from a def function)
3815 lines =<< trim END 3804 lines =<< trim END
3816 vim9script 3805 vim9script
3817 3806
3824 var a = A.new() 3813 var a = A.new()
3825 a._Foo() 3814 a._Foo()
3826 enddef 3815 enddef
3827 T() 3816 T()
3828 END 3817 END
3829 v9.CheckSourceFailure(lines, 'E1366: Cannot access private method: _Foo') 3818 v9.CheckSourceFailure(lines, 'E1366: Cannot access private method: _Foo', 2)
3830 3819
3831 # Use a class private method from an object method 3820 # Use a class private method from an object method
3832 lines =<< trim END 3821 lines =<< trim END
3833 vim9script 3822 vim9script
3834 3823
3876 static def Foo() 3865 static def Foo()
3877 enddef 3866 enddef
3878 endclass 3867 endclass
3879 var a = A.new() 3868 var a = A.new()
3880 END 3869 END
3881 v9.CheckSourceFailure(lines, 'E1355: Duplicate function: Foo') 3870 v9.CheckSourceFailure(lines, 'E1355: Duplicate function: Foo', 7)
3882 3871
3883 # Try calling a class private method from another class 3872 # Try calling a class private method from another class
3884 lines =<< trim END 3873 lines =<< trim END
3885 vim9script 3874 vim9script
3886 3875
3895 enddef 3884 enddef
3896 endclass 3885 endclass
3897 var b = B.new() 3886 var b = B.new()
3898 assert_equal(1234, b.Foo()) 3887 assert_equal(1234, b.Foo())
3899 END 3888 END
3900 v9.CheckSourceFailure(lines, 'E1366: Cannot access private method: _Foo()') 3889 v9.CheckSourceFailure(lines, 'E1366: Cannot access private method: _Foo()', 1)
3901 3890
3902 # Call a private class method from a child class object method 3891 # Call a private class method from a child class object method
3903 lines =<< trim END 3892 lines =<< trim END
3904 vim9script 3893 vim9script
3905 class A 3894 class A
3917 enddef 3906 enddef
3918 endclass 3907 endclass
3919 var c = C.new() 3908 var c = C.new()
3920 assert_equal(1234, c.Baz()) 3909 assert_equal(1234, c.Baz())
3921 END 3910 END
3922 v9.CheckSourceFailure(lines, 'E1366: Cannot access private method: _Foo()') 3911 v9.CheckSourceFailure(lines, 'E1366: Cannot access private method: _Foo()', 1)
3923 3912
3924 # Call a private class method from a child class private class method 3913 # Call a private class method from a child class private class method
3925 lines =<< trim END 3914 lines =<< trim END
3926 vim9script 3915 vim9script
3927 class A 3916 class A
3938 return A._Foo() 3927 return A._Foo()
3939 enddef 3928 enddef
3940 endclass 3929 endclass
3941 assert_equal(1234, C.Baz()) 3930 assert_equal(1234, C.Baz())
3942 END 3931 END
3943 v9.CheckSourceFailure(lines, 'E1366: Cannot access private method: _Foo()') 3932 v9.CheckSourceFailure(lines, 'E1366: Cannot access private method: _Foo()', 1)
3944 3933
3945 # Call a private class method from a child class object 3934 # Call a private class method from a child class object
3946 lines =<< trim END 3935 lines =<< trim END
3947 vim9script 3936 vim9script
3948 class A 3937 class A
3959 enddef 3948 enddef
3960 endclass 3949 endclass
3961 var c = C.new() 3950 var c = C.new()
3962 assert_equal(1234, C._Foo()) 3951 assert_equal(1234, C._Foo())
3963 END 3952 END
3964 v9.CheckSourceFailure(lines, 'E1325: Method not found on class "C": _Foo') 3953 v9.CheckSourceFailure(lines, 'E1325: Method not found on class "C": _Foo', 16)
3965 enddef 3954 enddef
3966 3955
3967 " Test for using the return value of a class/object method as a function 3956 " Test for using the return value of a class/object method as a function
3968 " argument. 3957 " argument.
3969 def Test_objmethod_funcarg() 3958 def Test_objmethod_funcarg()
4015 # subclasses get their own static copy 4004 # subclasses get their own static copy
4016 var lines =<< trim END 4005 var lines =<< trim END
4017 vim9script 4006 vim9script
4018 4007
4019 class A 4008 class A
4020 static _svar: number 4009 static _svar: number
4021 this._mvar: number 4010 this._mvar: number
4022 def new() 4011 def new()
4023 _svar = 1 4012 _svar = 1
4024 this._mvar = 101 4013 this._mvar = 101
4025 enddef 4014 enddef
4026 def AccessObject(): number 4015 def AccessObject(): number
4027 return this._mvar 4016 return this._mvar
4028 enddef 4017 enddef
4029 def AccessStaticThroughObject(): number 4018 def AccessStaticThroughObject(): number
4030 return _svar 4019 return _svar
4031 enddef 4020 enddef
4032 endclass 4021 endclass
4033 4022
4034 class B extends A 4023 class B extends A
4035 def new() 4024 def new()
4036 this._mvar = 102 4025 this._mvar = 102
4037 enddef 4026 enddef
4038 endclass 4027 endclass
4039 4028
4040 class C extends B 4029 class C extends B
4041 def new() 4030 def new()
4042 this._mvar = 103 4031 this._mvar = 103
4043 enddef 4032 enddef
4044 4033
4045 def AccessPrivateStaticThroughClassName(): number 4034 def AccessPrivateStaticThroughClassName(): number
4046 assert_equal(1, A._svar) 4035 assert_equal(1, A._svar)
4047 return 444 4036 return 444
4048 enddef 4037 enddef
4049 endclass 4038 endclass
4050 4039
4051 var oa = A.new() 4040 var oa = A.new()
4052 var ob = B.new() 4041 var ob = B.new()
4053 var oc = C.new() 4042 var oc = C.new()
4073 class C 4062 class C
4074 this.val = 10 4063 this.val = 10
4075 this.val = 20 4064 this.val = 20
4076 endclass 4065 endclass
4077 END 4066 END
4078 v9.CheckSourceFailure(lines, 'E1369: Duplicate variable: val') 4067 v9.CheckSourceFailure(lines, 'E1369: Duplicate variable: val', 4)
4079 4068
4080 # Duplicate private member variable 4069 # Duplicate private member variable
4081 lines =<< trim END 4070 lines =<< trim END
4082 vim9script 4071 vim9script
4083 class C 4072 class C
4084 this._val = 10 4073 this._val = 10
4085 this._val = 20 4074 this._val = 20
4086 endclass 4075 endclass
4087 END 4076 END
4088 v9.CheckSourceFailure(lines, 'E1369: Duplicate variable: _val') 4077 v9.CheckSourceFailure(lines, 'E1369: Duplicate variable: _val', 4)
4089 4078
4090 # Duplicate public member variable 4079 # Duplicate public member variable
4091 lines =<< trim END 4080 lines =<< trim END
4092 vim9script 4081 vim9script
4093 class C 4082 class C
4094 public this.val = 10 4083 public this.val = 10
4095 public this.val = 20 4084 public this.val = 20
4096 endclass 4085 endclass
4097 END 4086 END
4098 v9.CheckSourceFailure(lines, 'E1369: Duplicate variable: val') 4087 v9.CheckSourceFailure(lines, 'E1369: Duplicate variable: val', 4)
4099 4088
4100 # Duplicate private member variable 4089 # Duplicate private member variable
4101 lines =<< trim END 4090 lines =<< trim END
4102 vim9script 4091 vim9script
4103 class C 4092 class C
4104 this.val = 10 4093 this.val = 10
4105 this._val = 20 4094 this._val = 20
4106 endclass 4095 endclass
4107 END 4096 END
4108 v9.CheckSourceFailure(lines, 'E1369: Duplicate variable: _val') 4097 v9.CheckSourceFailure(lines, 'E1369: Duplicate variable: _val', 4)
4109 4098
4110 # Duplicate public and private member variable 4099 # Duplicate public and private member variable
4111 lines =<< trim END 4100 lines =<< trim END
4112 vim9script 4101 vim9script
4113 class C 4102 class C
4114 this._val = 20 4103 this._val = 20
4115 public this.val = 10 4104 public this.val = 10
4116 endclass 4105 endclass
4117 END 4106 END
4118 v9.CheckSourceFailure(lines, 'E1369: Duplicate variable: val') 4107 v9.CheckSourceFailure(lines, 'E1369: Duplicate variable: val', 4)
4119 4108
4120 # Duplicate class member variable 4109 # Duplicate class member variable
4121 lines =<< trim END 4110 lines =<< trim END
4122 vim9script 4111 vim9script
4123 class C 4112 class C
4124 static s: string = "abc" 4113 static s: string = "abc"
4125 static _s: string = "def" 4114 static _s: string = "def"
4126 endclass 4115 endclass
4127 END 4116 END
4128 v9.CheckSourceFailure(lines, 'E1369: Duplicate variable: _s') 4117 v9.CheckSourceFailure(lines, 'E1369: Duplicate variable: _s', 4)
4129 4118
4130 # Duplicate public and private class member variable 4119 # Duplicate public and private class member variable
4131 lines =<< trim END 4120 lines =<< trim END
4132 vim9script 4121 vim9script
4133 class C 4122 class C
4134 public static s: string = "abc" 4123 public static s: string = "abc"
4135 static _s: string = "def" 4124 static _s: string = "def"
4136 endclass 4125 endclass
4137 END 4126 END
4138 v9.CheckSourceFailure(lines, 'E1369: Duplicate variable: _s') 4127 v9.CheckSourceFailure(lines, 'E1369: Duplicate variable: _s', 4)
4139 4128
4140 # Duplicate class and object member variable 4129 # Duplicate class and object member variable
4141 lines =<< trim END 4130 lines =<< trim END
4142 vim9script 4131 vim9script
4143 class C 4132 class C
4162 endclass 4151 endclass
4163 class C extends B 4152 class C extends B
4164 this.val = 20 4153 this.val = 20
4165 endclass 4154 endclass
4166 END 4155 END
4167 v9.CheckSourceFailure(lines, 'E1369: Duplicate variable: val') 4156 v9.CheckSourceFailure(lines, 'E1369: Duplicate variable: val', 9)
4168 4157
4169 # Duplicate object private member variable in a derived class 4158 # Duplicate object private member variable in a derived class
4170 lines =<< trim END 4159 lines =<< trim END
4171 vim9script 4160 vim9script
4172 class A 4161 class A
4176 endclass 4165 endclass
4177 class C extends B 4166 class C extends B
4178 this._val = 20 4167 this._val = 20
4179 endclass 4168 endclass
4180 END 4169 END
4181 v9.CheckSourceFailure(lines, 'E1369: Duplicate variable: _val') 4170 v9.CheckSourceFailure(lines, 'E1369: Duplicate variable: _val', 9)
4182 4171
4183 # Duplicate object private member variable in a derived class 4172 # Duplicate object private member variable in a derived class
4184 lines =<< trim END 4173 lines =<< trim END
4185 vim9script 4174 vim9script
4186 class A 4175 class A
4190 endclass 4179 endclass
4191 class C extends B 4180 class C extends B
4192 this._val = 20 4181 this._val = 20
4193 endclass 4182 endclass
4194 END 4183 END
4195 v9.CheckSourceFailure(lines, 'E1369: Duplicate variable: _val') 4184 v9.CheckSourceFailure(lines, 'E1369: Duplicate variable: _val', 9)
4196 4185
4197 # Duplicate object member variable in a derived class 4186 # Duplicate object member variable in a derived class
4198 lines =<< trim END 4187 lines =<< trim END
4199 vim9script 4188 vim9script
4200 class A 4189 class A
4204 endclass 4193 endclass
4205 class C extends B 4194 class C extends B
4206 this.val = 20 4195 this.val = 20
4207 endclass 4196 endclass
4208 END 4197 END
4209 v9.CheckSourceFailure(lines, 'E1369: Duplicate variable: val') 4198 v9.CheckSourceFailure(lines, 'E1369: Duplicate variable: val', 9)
4210 4199
4211 # Two member variables with a common prefix 4200 # Two member variables with a common prefix
4212 lines =<< trim END 4201 lines =<< trim END
4213 vim9script 4202 vim9script
4214 class A 4203 class A
4234 var a = A.new() 4223 var a = A.new()
4235 a._val = 20 4224 a._val = 20
4236 enddef 4225 enddef
4237 T() 4226 T()
4238 END 4227 END
4239 v9.CheckSourceFailure(lines, 'E1333: Cannot access private variable: _val') 4228 v9.CheckSourceFailure(lines, 'E1333: Cannot access private variable: _val', 2)
4240 4229
4241 # access a non-existing private object member variable 4230 # access a non-existing private object member variable
4242 lines =<< trim END 4231 lines =<< trim END
4243 vim9script 4232 vim9script
4244 class A 4233 class A
4248 var a = A.new() 4237 var a = A.new()
4249 a._a = 1 4238 a._a = 1
4250 enddef 4239 enddef
4251 T() 4240 T()
4252 END 4241 END
4253 v9.CheckSourceFailure(lines, 'E1326: Variable not found on object "A": _a') 4242 v9.CheckSourceFailure(lines, 'E1326: Variable not found on object "A": _a', 2)
4254 4243
4255 # private static member variable 4244 # private static member variable
4256 lines =<< trim END 4245 lines =<< trim END
4257 vim9script 4246 vim9script
4258 class A 4247 class A
4262 var a = A.new() 4251 var a = A.new()
4263 var x = a._val 4252 var x = a._val
4264 enddef 4253 enddef
4265 T() 4254 T()
4266 END 4255 END
4267 v9.CheckSourceFailure(lines, 'E1375: Class variable "_val" accessible only using class "A"') 4256 v9.CheckSourceFailure(lines, 'E1375: Class variable "_val" accessible only using class "A"', 2)
4268 4257
4269 # private static member variable 4258 # private static member variable
4270 lines =<< trim END 4259 lines =<< trim END
4271 vim9script 4260 vim9script
4272 class A 4261 class A
4276 var a = A.new() 4265 var a = A.new()
4277 a._val = 3 4266 a._val = 3
4278 enddef 4267 enddef
4279 T() 4268 T()
4280 END 4269 END
4281 v9.CheckSourceFailure(lines, 'E1375: Class variable "_val" accessible only using class "A"') 4270 v9.CheckSourceFailure(lines, 'E1375: Class variable "_val" accessible only using class "A"', 2)
4282 4271
4283 # private static class variable 4272 # private static class variable
4284 lines =<< trim END 4273 lines =<< trim END
4285 vim9script 4274 vim9script
4286 class A 4275 class A
4289 def T() 4278 def T()
4290 var x = A._val 4279 var x = A._val
4291 enddef 4280 enddef
4292 T() 4281 T()
4293 END 4282 END
4294 v9.CheckSourceFailure(lines, 'E1333: Cannot access private variable: _val') 4283 v9.CheckSourceFailure(lines, 'E1333: Cannot access private variable: _val', 1)
4295 4284
4296 # private static class variable 4285 # private static class variable
4297 lines =<< trim END 4286 lines =<< trim END
4298 vim9script 4287 vim9script
4299 class A 4288 class A
4302 def T() 4291 def T()
4303 A._val = 3 4292 A._val = 3
4304 enddef 4293 enddef
4305 T() 4294 T()
4306 END 4295 END
4307 v9.CheckSourceFailure(lines, 'E1333: Cannot access private variable: _val') 4296 v9.CheckSourceFailure(lines, 'E1333: Cannot access private variable: _val', 1)
4308 enddef 4297 enddef
4309 4298
4310 " Test for changing the member access of an interface in a implementation class 4299 " Test for changing the member access of an interface in a implementation class
4311 def Test_change_interface_member_access() 4300 def Test_change_interface_member_access()
4312 var lines =<< trim END 4301 var lines =<< trim END
4316 endinterface 4305 endinterface
4317 class B implements A 4306 class B implements A
4318 public this.val = 10 4307 public this.val = 10
4319 endclass 4308 endclass
4320 END 4309 END
4321 v9.CheckSourceFailure(lines, 'E1367: Access level of variable "val" of interface "A" is different') 4310 v9.CheckSourceFailure(lines, 'E1367: Access level of variable "val" of interface "A" is different', 7)
4322 4311
4323 lines =<< trim END 4312 lines =<< trim END
4324 vim9script 4313 vim9script
4325 interface A 4314 interface A
4326 this.val: number 4315 this.val: number
4327 endinterface 4316 endinterface
4328 class B implements A 4317 class B implements A
4329 public this.val = 10 4318 public this.val = 10
4330 endclass 4319 endclass
4331 END 4320 END
4332 v9.CheckSourceFailure(lines, 'E1367: Access level of variable "val" of interface "A" is different') 4321 v9.CheckSourceFailure(lines, 'E1367: Access level of variable "val" of interface "A" is different', 7)
4333 enddef 4322 enddef
4334 4323
4335 " Test for trying to change a readonly member from a def function 4324 " Test for trying to change a readonly member from a def function
4336 def Test_readonly_member_change_in_def_func() 4325 def Test_readonly_member_change_in_def_func()
4337 var lines =<< trim END 4326 var lines =<< trim END
4343 var a = A.new() 4332 var a = A.new()
4344 a.val = 20 4333 a.val = 20
4345 enddef 4334 enddef
4346 T() 4335 T()
4347 END 4336 END
4348 v9.CheckSourceFailure(lines, 'E1335: Variable "val" in class "A" is not writable') 4337 v9.CheckSourceFailure(lines, 'E1335: Variable "val" in class "A" is not writable', 2)
4349 enddef 4338 enddef
4350 4339
4351 " Test for reading and writing a class member from a def function 4340 " Test for reading and writing a class member from a def function
4352 def Test_modify_class_member_from_def_function() 4341 def Test_modify_class_member_from_def_function()
4353 var lines =<< trim END 4342 var lines =<< trim END
4405 endclass 4394 endclass
4406 4395
4407 var a = A.new() 4396 var a = A.new()
4408 echo a.svar2 4397 echo a.svar2
4409 END 4398 END
4410 v9.CheckSourceFailure(lines, 'E1375: Class variable "svar2" accessible only using class "A"') 4399 v9.CheckSourceFailure(lines, 'E1375: Class variable "svar2" accessible only using class "A"', 8)
4411 4400
4412 # Cannot write to a class variable using an object in script context 4401 # Cannot write to a class variable using an object in script context
4413 lines =<< trim END 4402 lines =<< trim END
4414 vim9script 4403 vim9script
4415 class A 4404 class A
4418 endclass 4407 endclass
4419 4408
4420 var a = A.new() 4409 var a = A.new()
4421 a.svar2 = [2] 4410 a.svar2 = [2]
4422 END 4411 END
4423 v9.CheckSourceFailure(lines, 'E1375: Class variable "svar2" accessible only using class "A"') 4412 v9.CheckSourceFailure(lines, 'E1375: Class variable "svar2" accessible only using class "A"', 8)
4424 4413
4425 # Cannot read from a class variable using an object in def method context 4414 # Cannot read from a class variable using an object in def method context
4426 lines =<< trim END 4415 lines =<< trim END
4427 vim9script 4416 vim9script
4428 class A 4417 class A
4434 var a = A.new() 4423 var a = A.new()
4435 echo a.svar2 4424 echo a.svar2
4436 enddef 4425 enddef
4437 T() 4426 T()
4438 END 4427 END
4439 v9.CheckSourceFailure(lines, 'E1375: Class variable "svar2" accessible only using class "A"') 4428 v9.CheckSourceFailure(lines, 'E1375: Class variable "svar2" accessible only using class "A"', 2)
4440 4429
4441 # Cannot write to a class variable using an object in def method context 4430 # Cannot write to a class variable using an object in def method context
4442 lines =<< trim END 4431 lines =<< trim END
4443 vim9script 4432 vim9script
4444 class A 4433 class A
4450 var a = A.new() 4439 var a = A.new()
4451 a.svar2 = [2] 4440 a.svar2 = [2]
4452 enddef 4441 enddef
4453 T() 4442 T()
4454 END 4443 END
4455 v9.CheckSourceFailure(lines, 'E1375: Class variable "svar2" accessible only using class "A"') 4444 v9.CheckSourceFailure(lines, 'E1375: Class variable "svar2" accessible only using class "A"', 2)
4456 enddef 4445 enddef
4457 4446
4458 " Test for using a interface method using a child object 4447 " Test for using a interface method using a child object
4459 def Test_interface_method_from_child() 4448 def Test_interface_method_from_child()
4460 var lines =<< trim END 4449 var lines =<< trim END
4564 abstract def Foo() 4553 abstract def Foo()
4565 endclass 4554 endclass
4566 class B extends A 4555 class B extends A
4567 endclass 4556 endclass
4568 END 4557 END
4569 v9.CheckSourceFailure(lines, 'E1373: Abstract method "Foo" is not implemented') 4558 v9.CheckSourceFailure(lines, 'E1373: Abstract method "Foo" is not implemented', 6)
4570 4559
4571 # Use abstract method in a concrete class 4560 # Use abstract method in a concrete class
4572 lines =<< trim END 4561 lines =<< trim END
4573 vim9script 4562 vim9script
4574 class A 4563 class A
4575 abstract def Foo() 4564 abstract def Foo()
4576 endclass 4565 endclass
4577 class B extends A 4566 class B extends A
4578 endclass 4567 endclass
4579 END 4568 END
4580 v9.CheckSourceFailure(lines, 'E1372: Abstract method "abstract def Foo()" cannot be defined in a concrete class') 4569 v9.CheckSourceFailure(lines, 'E1372: Abstract method "abstract def Foo()" cannot be defined in a concrete class', 3)
4581 4570
4582 # Use abstract method in an interface 4571 # Use abstract method in an interface
4583 lines =<< trim END 4572 lines =<< trim END
4584 vim9script 4573 vim9script
4585 interface A 4574 interface A
4597 vim9script 4586 vim9script
4598 class A 4587 class A
4599 abs def Foo() 4588 abs def Foo()
4600 endclass 4589 endclass
4601 END 4590 END
4602 v9.CheckSourceFailure(lines, 'E1065: Command cannot be shortened: abs def Foo()') 4591 v9.CheckSourceFailure(lines, 'E1065: Command cannot be shortened: abs def Foo()', 3)
4603 4592
4604 # Use "abstract" with a member variable 4593 # Use "abstract" with a member variable
4605 lines =<< trim END 4594 lines =<< trim END
4606 vim9script 4595 vim9script
4607 abstract class A 4596 abstract class A
4608 abstract this.val = 10 4597 abstract this.val = 10
4609 endclass 4598 endclass
4610 END 4599 END
4611 v9.CheckSourceFailure(lines, 'E1371: Abstract must be followed by "def" or "static"') 4600 v9.CheckSourceFailure(lines, 'E1371: Abstract must be followed by "def" or "static"', 3)
4612 4601
4613 # Use a static abstract method 4602 # Use a static abstract method
4614 lines =<< trim END 4603 lines =<< trim END
4615 vim9script 4604 vim9script
4616 abstract class A 4605 abstract class A
4635 def Foo(a: number, b: string): list<string> 4624 def Foo(a: number, b: string): list<string>
4636 return [] 4625 return []
4637 enddef 4626 enddef
4638 endclass 4627 endclass
4639 END 4628 END
4640 v9.CheckSourceFailure(lines, 'E1383: Method "Foo": type mismatch, expected func(string, number): list<number> but got func(number, string): list<string>') 4629 v9.CheckSourceFailure(lines, 'E1383: Method "Foo": type mismatch, expected func(string, number): list<number> but got func(number, string): list<string>', 9)
4641 4630
4642 # Use an abstract class to invoke an abstract method 4631 # Use an abstract class to invoke an abstract method
4643 # FIXME: This should fail 4632 # FIXME: This should fail
4644 lines =<< trim END 4633 lines =<< trim END
4645 vim9script 4634 vim9script
4689 endclass 4678 endclass
4690 4679
4691 var b = B.new() 4680 var b = B.new()
4692 b.Bar() 4681 b.Bar()
4693 END 4682 END
4694 v9.CheckSourceFailure(lines, 'E1384: Class method "Foo" accessible only inside class "A"') 4683 v9.CheckSourceFailure(lines, 'E1384: Class method "Foo" accessible only inside class "A"', 1)
4695 enddef 4684 enddef
4696 4685
4697 " Test for calling a class method using an object in a def function context and 4686 " Test for calling a class method using an object in a def function context and
4698 " script context. 4687 " script context.
4699 def Test_class_method_call_using_object() 4688 def Test_class_method_call_using_object()
4733 endclass 4722 endclass
4734 4723
4735 var a = A.new() 4724 var a = A.new()
4736 assert_equal('foo', a.Foo()) 4725 assert_equal('foo', a.Foo())
4737 END 4726 END
4738 v9.CheckSourceFailure(lines, 'E1385: Class method "Foo" accessible only using class "A"') 4727 v9.CheckSourceFailure(lines, 'E1385: Class method "Foo" accessible only using class "A"', 9)
4739 4728
4740 # def function context 4729 # def function context
4741 lines =<< trim END 4730 lines =<< trim END
4742 vim9script 4731 vim9script
4743 class A 4732 class A
4750 var a = A.new() 4739 var a = A.new()
4751 assert_equal('foo', a.Foo()) 4740 assert_equal('foo', a.Foo())
4752 enddef 4741 enddef
4753 T() 4742 T()
4754 END 4743 END
4755 v9.CheckSourceFailure(lines, 'E1385: Class method "Foo" accessible only using class "A"') 4744 v9.CheckSourceFailure(lines, 'E1385: Class method "Foo" accessible only using class "A"', 2)
4756 enddef 4745 enddef
4757 4746
4758 def Test_class_variable() 4747 def Test_class_variable()
4759 var lines =<< trim END 4748 var lines =<< trim END
4760 vim9script 4749 vim9script
4803 val = 20 4792 val = 20
4804 enddef 4793 enddef
4805 endclass 4794 endclass
4806 B.ClassFunc() 4795 B.ClassFunc()
4807 END 4796 END
4808 v9.CheckSourceFailure(lines, 'E1374: Class variable "val" accessible only inside class "A"') 4797 v9.CheckSourceFailure(lines, 'E1374: Class variable "val" accessible only inside class "A"', 1)
4809 4798
4810 # Reading a parent class variable from a child class method 4799 # Reading a parent class variable from a child class method
4811 lines =<< trim END 4800 lines =<< trim END
4812 vim9script 4801 vim9script
4813 4802
4820 var i = val 4809 var i = val
4821 enddef 4810 enddef
4822 endclass 4811 endclass
4823 B.ClassFunc() 4812 B.ClassFunc()
4824 END 4813 END
4825 v9.CheckSourceFailure(lines, 'E1374: Class variable "val" accessible only inside class "A"') 4814 v9.CheckSourceFailure(lines, 'E1374: Class variable "val" accessible only inside class "A"', 1)
4826 4815
4827 # Modifying a parent class variable from a child object method 4816 # Modifying a parent class variable from a child object method
4828 lines =<< trim END 4817 lines =<< trim END
4829 vim9script 4818 vim9script
4830 4819
4838 enddef 4827 enddef
4839 endclass 4828 endclass
4840 var b = B.new() 4829 var b = B.new()
4841 b.ObjFunc() 4830 b.ObjFunc()
4842 END 4831 END
4843 v9.CheckSourceFailure(lines, 'E1374: Class variable "val" accessible only inside class "A"') 4832 v9.CheckSourceFailure(lines, 'E1374: Class variable "val" accessible only inside class "A"', 1)
4844 4833
4845 # Reading a parent class variable from a child object method 4834 # Reading a parent class variable from a child object method
4846 lines =<< trim END 4835 lines =<< trim END
4847 vim9script 4836 vim9script
4848 4837
4856 enddef 4845 enddef
4857 endclass 4846 endclass
4858 var b = B.new() 4847 var b = B.new()
4859 b.ObjFunc() 4848 b.ObjFunc()
4860 END 4849 END
4861 v9.CheckSourceFailure(lines, 'E1374: Class variable "val" accessible only inside class "A"') 4850 v9.CheckSourceFailure(lines, 'E1374: Class variable "val" accessible only inside class "A"', 1)
4862 4851
4863 # Modifying a class variable using an object at script level 4852 # Modifying a class variable using an object at script level
4864 lines =<< trim END 4853 lines =<< trim END
4865 vim9script 4854 vim9script
4866 4855
4868 static val: number = 10 4857 static val: number = 10
4869 endclass 4858 endclass
4870 var a = A.new() 4859 var a = A.new()
4871 a.val = 20 4860 a.val = 20
4872 END 4861 END
4873 v9.CheckSourceFailure(lines, 'E1375: Class variable "val" accessible only using class "A"') 4862 v9.CheckSourceFailure(lines, 'E1375: Class variable "val" accessible only using class "A"', 7)
4874 4863
4875 # Reading a class variable using an object at script level 4864 # Reading a class variable using an object at script level
4876 lines =<< trim END 4865 lines =<< trim END
4877 vim9script 4866 vim9script
4878 4867
4880 static val: number = 10 4869 static val: number = 10
4881 endclass 4870 endclass
4882 var a = A.new() 4871 var a = A.new()
4883 var i = a.val 4872 var i = a.val
4884 END 4873 END
4885 v9.CheckSourceFailure(lines, 'E1375: Class variable "val" accessible only using class "A"') 4874 v9.CheckSourceFailure(lines, 'E1375: Class variable "val" accessible only using class "A"', 7)
4886 4875
4887 # Modifying a class variable using an object at function level 4876 # Modifying a class variable using an object at function level
4888 lines =<< trim END 4877 lines =<< trim END
4889 vim9script 4878 vim9script
4890 4879
4896 var a = A.new() 4885 var a = A.new()
4897 a.val = 20 4886 a.val = 20
4898 enddef 4887 enddef
4899 T() 4888 T()
4900 END 4889 END
4901 v9.CheckSourceFailure(lines, 'E1375: Class variable "val" accessible only using class "A"') 4890 v9.CheckSourceFailure(lines, 'E1375: Class variable "val" accessible only using class "A"', 2)
4902 4891
4903 # Reading a class variable using an object at function level 4892 # Reading a class variable using an object at function level
4904 lines =<< trim END 4893 lines =<< trim END
4905 vim9script 4894 vim9script
4906 4895
4911 var a = A.new() 4900 var a = A.new()
4912 var i = a.val 4901 var i = a.val
4913 enddef 4902 enddef
4914 T() 4903 T()
4915 END 4904 END
4916 v9.CheckSourceFailure(lines, 'E1375: Class variable "val" accessible only using class "A"') 4905 v9.CheckSourceFailure(lines, 'E1375: Class variable "val" accessible only using class "A"', 2)
4917 enddef 4906 enddef
4918 4907
4919 " Test for using a duplicate class method and class variable in a child class 4908 " Test for using a duplicate class method and class variable in a child class
4920 def Test_dup_class_member() 4909 def Test_dup_class_member()
4921 # duplicate class variable, class method and overridden object method 4910 # duplicate class variable, class method and overridden object method
5010 echo "foo" 4999 echo "foo"
5011 enddef 5000 enddef
5012 endclass 5001 endclass
5013 A.Foo() 5002 A.Foo()
5014 END 5003 END
5015 v9.CheckSourceFailure(lines, 'E1386: Object method "Foo" accessible only using class "A" object') 5004 v9.CheckSourceFailure(lines, 'E1386: Object method "Foo" accessible only using class "A" object', 7)
5016 5005
5017 # Invoke an object method using a class in def function context 5006 # Invoke an object method using a class in def function context
5018 lines =<< trim END 5007 lines =<< trim END
5019 vim9script 5008 vim9script
5020 class A 5009 class A
5025 def T() 5014 def T()
5026 A.Foo() 5015 A.Foo()
5027 enddef 5016 enddef
5028 T() 5017 T()
5029 END 5018 END
5030 v9.CheckSourceFailure(lines, 'E1386: Object method "Foo" accessible only using class "A" object') 5019 v9.CheckSourceFailure(lines, 'E1386: Object method "Foo" accessible only using class "A" object', 1)
5031 enddef 5020 enddef
5032 5021
5033 " Test for duplicate class method and instance method 5022 " Test for duplicate class method and instance method
5034 def Test_dup_classmethod_objmethod() 5023 def Test_dup_classmethod_objmethod()
5035 # Duplicate instance method 5024 # Duplicate instance method
5040 enddef 5029 enddef
5041 def Foo() 5030 def Foo()
5042 enddef 5031 enddef
5043 endclass 5032 endclass
5044 END 5033 END
5045 v9.CheckSourceFailure(lines, 'E1355: Duplicate function: Foo') 5034 v9.CheckSourceFailure(lines, 'E1355: Duplicate function: Foo', 6)
5046 5035
5047 # Duplicate private instance method 5036 # Duplicate private instance method
5048 lines =<< trim END 5037 lines =<< trim END
5049 vim9script 5038 vim9script
5050 class A 5039 class A
5052 enddef 5041 enddef
5053 def _Foo() 5042 def _Foo()
5054 enddef 5043 enddef
5055 endclass 5044 endclass
5056 END 5045 END
5057 v9.CheckSourceFailure(lines, 'E1355: Duplicate function: _Foo') 5046 v9.CheckSourceFailure(lines, 'E1355: Duplicate function: _Foo', 6)
5058 5047
5059 # Duplicate class method 5048 # Duplicate class method
5060 lines =<< trim END 5049 lines =<< trim END
5061 vim9script 5050 vim9script
5062 class A 5051 class A
5064 enddef 5053 enddef
5065 static def Foo() 5054 static def Foo()
5066 enddef 5055 enddef
5067 endclass 5056 endclass
5068 END 5057 END
5069 v9.CheckSourceFailure(lines, 'E1355: Duplicate function: Foo') 5058 v9.CheckSourceFailure(lines, 'E1355: Duplicate function: Foo', 6)
5070 5059
5071 # Duplicate private class method 5060 # Duplicate private class method
5072 lines =<< trim END 5061 lines =<< trim END
5073 vim9script 5062 vim9script
5074 class A 5063 class A
5076 enddef 5065 enddef
5077 static def _Foo() 5066 static def _Foo()
5078 enddef 5067 enddef
5079 endclass 5068 endclass
5080 END 5069 END
5081 v9.CheckSourceFailure(lines, 'E1355: Duplicate function: _Foo') 5070 v9.CheckSourceFailure(lines, 'E1355: Duplicate function: _Foo', 6)
5082 5071
5083 # Duplicate private class and object method 5072 # Duplicate private class and object method
5084 lines =<< trim END 5073 lines =<< trim END
5085 vim9script 5074 vim9script
5086 class A 5075 class A
5088 enddef 5077 enddef
5089 static def _Foo() 5078 static def _Foo()
5090 enddef 5079 enddef
5091 endclass 5080 endclass
5092 END 5081 END
5093 v9.CheckSourceFailure(lines, 'E1355: Duplicate function: _Foo') 5082 v9.CheckSourceFailure(lines, 'E1355: Duplicate function: _Foo', 6)
5094 enddef 5083 enddef
5095 5084
5096 " Test for an instance method access level comparison with parent instance 5085 " Test for an instance method access level comparison with parent instance
5097 " methods. 5086 " methods.
5098 def Test_instance_method_access_level() 5087 def Test_instance_method_access_level()
5108 class C extends B 5097 class C extends B
5109 def _Foo() 5098 def _Foo()
5110 enddef 5099 enddef
5111 endclass 5100 endclass
5112 END 5101 END
5113 v9.CheckSourceFailure(lines, 'E1377: Access level of method "_Foo" is different in class "A"') 5102 v9.CheckSourceFailure(lines, 'E1377: Access level of method "_Foo" is different in class "A"', 11)
5114 5103
5115 # Public method in subclass 5104 # Public method in subclass
5116 lines =<< trim END 5105 lines =<< trim END
5117 vim9script 5106 vim9script
5118 class A 5107 class A
5124 class C extends B 5113 class C extends B
5125 def Foo() 5114 def Foo()
5126 enddef 5115 enddef
5127 endclass 5116 endclass
5128 END 5117 END
5129 v9.CheckSourceFailure(lines, 'E1377: Access level of method "Foo" is different in class "A"') 5118 v9.CheckSourceFailure(lines, 'E1377: Access level of method "Foo" is different in class "A"', 11)
5130 enddef 5119 enddef
5131 5120
5132 def Test_extend_empty_class() 5121 def Test_extend_empty_class()
5133 var lines =<< trim END 5122 var lines =<< trim END
5134 vim9script 5123 vim9script
5162 vim9script 5151 vim9script
5163 interface A 5152 interface A
5164 static num: number 5153 static num: number
5165 endinterface 5154 endinterface
5166 END 5155 END
5167 v9.CheckSourceFailure(lines, 'E1378: Static member not supported in an interface') 5156 v9.CheckSourceFailure(lines, 'E1378: Static member not supported in an interface', 3)
5168 5157
5169 lines =<< trim END 5158 lines =<< trim END
5170 vim9script 5159 vim9script
5171 interface A 5160 interface A
5172 static _num: number 5161 static _num: number
5173 endinterface 5162 endinterface
5174 END 5163 END
5175 v9.CheckSourceFailure(lines, 'E1378: Static member not supported in an interface') 5164 v9.CheckSourceFailure(lines, 'E1378: Static member not supported in an interface', 3)
5176 5165
5177 lines =<< trim END 5166 lines =<< trim END
5178 vim9script 5167 vim9script
5179 interface A 5168 interface A
5180 public static num: number 5169 public static num: number
5181 endinterface 5170 endinterface
5182 END 5171 END
5183 v9.CheckSourceFailure(lines, 'E1387: Public variable not supported in an interface') 5172 v9.CheckSourceFailure(lines, 'E1387: Public variable not supported in an interface', 3)
5184 5173
5185 lines =<< trim END 5174 lines =<< trim END
5186 vim9script 5175 vim9script
5187 interface A 5176 interface A
5188 public static num: number 5177 public static num: number
5189 endinterface 5178 endinterface
5190 END 5179 END
5191 v9.CheckSourceFailure(lines, 'E1387: Public variable not supported in an interface') 5180 v9.CheckSourceFailure(lines, 'E1387: Public variable not supported in an interface', 3)
5192 5181
5193 lines =<< trim END 5182 lines =<< trim END
5194 vim9script 5183 vim9script
5195 interface A 5184 interface A
5196 static _num: number 5185 static _num: number
5197 endinterface 5186 endinterface
5198 END 5187 END
5199 v9.CheckSourceFailure(lines, 'E1378: Static member not supported in an interface') 5188 v9.CheckSourceFailure(lines, 'E1378: Static member not supported in an interface', 3)
5200 5189
5201 lines =<< trim END 5190 lines =<< trim END
5202 vim9script 5191 vim9script
5203 interface A 5192 interface A
5204 static def Foo(d: dict<any>): list<string> 5193 static def Foo(d: dict<any>): list<string>
5205 endinterface 5194 endinterface
5206 END 5195 END
5207 v9.CheckSourceFailure(lines, 'E1378: Static member not supported in an interface') 5196 v9.CheckSourceFailure(lines, 'E1378: Static member not supported in an interface', 3)
5208 5197
5209 lines =<< trim END 5198 lines =<< trim END
5210 vim9script 5199 vim9script
5211 interface A 5200 interface A
5212 static def _Foo(d: dict<any>): list<string> 5201 static def _Foo(d: dict<any>): list<string>
5213 endinterface 5202 endinterface
5214 END 5203 END
5215 v9.CheckSourceFailure(lines, 'E1378: Static member not supported in an interface') 5204 v9.CheckSourceFailure(lines, 'E1378: Static member not supported in an interface', 3)
5216 5205
5217 lines =<< trim END 5206 lines =<< trim END
5218 vim9script 5207 vim9script
5219 interface A 5208 interface A
5220 this._Foo: list<string> 5209 this._Foo: list<string>
5221 endinterface 5210 endinterface
5222 END 5211 END
5223 v9.CheckSourceFailure(lines, 'E1379: Private variable not supported in an interface') 5212 v9.CheckSourceFailure(lines, 'E1379: Private variable not supported in an interface', 3)
5224 5213
5225 lines =<< trim END 5214 lines =<< trim END
5226 vim9script 5215 vim9script
5227 interface A 5216 interface A
5228 def _Foo(d: dict<any>): list<string> 5217 def _Foo(d: dict<any>): list<string>
5229 endinterface 5218 endinterface
5230 END 5219 END
5231 v9.CheckSourceFailure(lines, 'E1380: Private method not supported in an interface') 5220 v9.CheckSourceFailure(lines, 'E1380: Private method not supported in an interface', 3)
5232 enddef 5221 enddef
5233 5222
5234 " Test for extending an interface 5223 " Test for extending an interface
5235 def Test_extend_interface() 5224 def Test_extend_interface()
5236 var lines =<< trim END 5225 var lines =<< trim END
5264 endinterface 5253 endinterface
5265 class C implements A, B 5254 class C implements A, B
5266 this.var2 = {a: '1'} 5255 this.var2 = {a: '1'}
5267 endclass 5256 endclass
5268 END 5257 END
5269 v9.CheckSourceFailure(lines, 'E1349: Method "Foo" of interface "A" is not implemented') 5258 v9.CheckSourceFailure(lines, 'E1349: Method "Foo" of interface "A" is not implemented', 10)
5270 5259
5271 lines =<< trim END 5260 lines =<< trim END
5272 vim9script 5261 vim9script
5273 interface A 5262 interface A
5274 def Foo() 5263 def Foo()
5279 class C implements A, B 5268 class C implements A, B
5280 def Foo() 5269 def Foo()
5281 enddef 5270 enddef
5282 endclass 5271 endclass
5283 END 5272 END
5284 v9.CheckSourceFailure(lines, 'E1348: Variable "var2" of interface "B" is not implemented') 5273 v9.CheckSourceFailure(lines, 'E1348: Variable "var2" of interface "B" is not implemented', 11)
5285 5274
5286 # interface cannot extend a class 5275 # interface cannot extend a class
5287 lines =<< trim END 5276 lines =<< trim END
5288 vim9script 5277 vim9script
5289 class A 5278 class A
5290 endclass 5279 endclass
5291 interface B extends A 5280 interface B extends A
5292 endinterface 5281 endinterface
5293 END 5282 END
5294 v9.CheckSourceFailure(lines, 'E1354: Cannot extend A') 5283 v9.CheckSourceFailure(lines, 'E1354: Cannot extend A', 5)
5295 5284
5296 # class cannot extend an interface 5285 # class cannot extend an interface
5297 lines =<< trim END 5286 lines =<< trim END
5298 vim9script 5287 vim9script
5299 interface A 5288 interface A
5300 endinterface 5289 endinterface
5301 class B extends A 5290 class B extends A
5302 endclass 5291 endclass
5303 END 5292 END
5304 v9.CheckSourceFailure(lines, 'E1354: Cannot extend A') 5293 v9.CheckSourceFailure(lines, 'E1354: Cannot extend A', 5)
5305 5294
5306 # interface cannot implement another interface 5295 # interface cannot implement another interface
5307 lines =<< trim END 5296 lines =<< trim END
5308 vim9script 5297 vim9script
5309 interface A 5298 interface A
5310 endinterface 5299 endinterface
5311 interface B implements A 5300 interface B implements A
5312 endinterface 5301 endinterface
5313 END 5302 END
5314 v9.CheckSourceFailure(lines, 'E1381: Interface cannot use "implements"') 5303 v9.CheckSourceFailure(lines, 'E1381: Interface cannot use "implements"', 4)
5315 5304
5316 # interface cannot extend multiple interfaces 5305 # interface cannot extend multiple interfaces
5317 lines =<< trim END 5306 lines =<< trim END
5318 vim9script 5307 vim9script
5319 interface A 5308 interface A
5321 interface B 5310 interface B
5322 endinterface 5311 endinterface
5323 interface C extends A, B 5312 interface C extends A, B
5324 endinterface 5313 endinterface
5325 END 5314 END
5326 v9.CheckSourceFailure(lines, 'E1315: White space required after name: A, B') 5315 v9.CheckSourceFailure(lines, 'E1315: White space required after name: A, B', 6)
5327 5316
5328 # Variable type in an extended interface is of different type 5317 # Variable type in an extended interface is of different type
5329 lines =<< trim END 5318 lines =<< trim END
5330 vim9script 5319 vim9script
5331 interface A 5320 interface A
5337 interface C extends B 5326 interface C extends B
5338 this.val1: string 5327 this.val1: string
5339 this.val2: number 5328 this.val2: number
5340 endinterface 5329 endinterface
5341 END 5330 END
5342 v9.CheckSourceFailure(lines, 'E1382: Variable "val1": type mismatch, expected number but got string') 5331 v9.CheckSourceFailure(lines, 'E1382: Variable "val1": type mismatch, expected number but got string', 11)
5343 enddef 5332 enddef
5344 5333
5345 " Test for a child class implementing an interface when some of the methods are 5334 " Test for a child class implementing an interface when some of the methods are
5346 " defined in the parent class. 5335 " defined in the parent class.
5347 def Test_child_class_implements_interface() 5336 def Test_child_class_implements_interface()
5434 enddef 5423 enddef
5435 def F1() 5424 def F1()
5436 enddef 5425 enddef
5437 endclass 5426 endclass
5438 END 5427 END
5439 v9.CheckSourceFailure(lines, 'E1349: Method "F3" of interface "Intf" is not implemented') 5428 v9.CheckSourceFailure(lines, 'E1349: Method "F3" of interface "Intf" is not implemented', 26)
5440 5429
5441 # One of the interface methods is of different type 5430 # One of the interface methods is of different type
5442 lines =<< trim END 5431 lines =<< trim END
5443 vim9script 5432 vim9script
5444 5433
5468 enddef 5457 enddef
5469 def F1() 5458 def F1()
5470 enddef 5459 enddef
5471 endclass 5460 endclass
5472 END 5461 END
5473 v9.CheckSourceFailure(lines, 'E1383: Method "F3": type mismatch, expected func() but got func(): number') 5462 v9.CheckSourceFailure(lines, 'E1383: Method "F3": type mismatch, expected func() but got func(): number', 29)
5474 5463
5475 # One of the interface variables is not present 5464 # One of the interface variables is not present
5476 lines =<< trim END 5465 lines =<< trim END
5477 vim9script 5466 vim9script
5478 5467
5494 class C extends B implements Intf 5483 class C extends B implements Intf
5495 this.v3: list<list<number>> = [[0]] 5484 this.v3: list<list<number>> = [[0]]
5496 this.var1 = [{a: 10}] 5485 this.var1 = [{a: 10}]
5497 endclass 5486 endclass
5498 END 5487 END
5499 v9.CheckSourceFailure(lines, 'E1348: Variable "var3" of interface "Intf" is not implemented') 5488 v9.CheckSourceFailure(lines, 'E1348: Variable "var3" of interface "Intf" is not implemented', 21)
5500 5489
5501 # One of the interface variables is of different type 5490 # One of the interface variables is of different type
5502 lines =<< trim END 5491 lines =<< trim END
5503 vim9script 5492 vim9script
5504 5493
5521 class C extends B implements Intf 5510 class C extends B implements Intf
5522 this.v3: list<list<number>> = [[0]] 5511 this.v3: list<list<number>> = [[0]]
5523 this.var1 = [{a: 10}] 5512 this.var1 = [{a: 10}]
5524 endclass 5513 endclass
5525 END 5514 END
5526 v9.CheckSourceFailure(lines, 'E1382: Variable "var3": type mismatch, expected list<dict<number>> but got list<dict<string>>') 5515 v9.CheckSourceFailure(lines, 'E1382: Variable "var3": type mismatch, expected list<dict<number>> but got list<dict<string>>', 22)
5527 enddef 5516 enddef
5528 5517
5529 " Test for extending an interface with duplicate variables and methods 5518 " Test for extending an interface with duplicate variables and methods
5530 def Test_interface_extends_with_dup_members() 5519 def Test_interface_extends_with_dup_members()
5531 var lines =<< trim END 5520 var lines =<< trim END
5592 class B implements A 5581 class B implements A
5593 this.val = {a: 1, b: 2} 5582 this.val = {a: 1, b: 2}
5594 endclass 5583 endclass
5595 var b = B.new() 5584 var b = B.new()
5596 END 5585 END
5597 v9.CheckSourceFailure(lines, 'E1382: Variable "val": type mismatch, expected list<dict<string>> but got dict<number>') 5586 v9.CheckSourceFailure(lines, 'E1382: Variable "val": type mismatch, expected list<dict<string>> but got dict<number>', 1)
5598 enddef 5587 enddef
5599 5588
5600 " Test for assigning to a member variable in a nested class 5589 " Test for assigning to a member variable in a nested class
5601 def Test_nested_object_assignment() 5590 def Test_nested_object_assignment()
5602 var lines =<< trim END 5591 var lines =<< trim END
5603 vim9script 5592 vim9script
5604 5593
5605 class A 5594 class A
5606 this.value: number 5595 this.value: number
5607 endclass 5596 endclass
5608 5597
5609 class B 5598 class B
5610 this.a: A = A.new() 5599 this.a: A = A.new()
5611 endclass 5600 endclass
5612 5601
5613 class C 5602 class C
5614 this.b: B = B.new() 5603 this.b: B = B.new()
5615 endclass 5604 endclass
5616 5605
5617 class D 5606 class D
5618 this.c: C = C.new() 5607 this.c: C = C.new()
5619 endclass 5608 endclass
5620 5609
5621 def T(da: D) 5610 def T(da: D)
5622 da.c.b.a.value = 10 5611 da.c.b.a.value = 10
5623 enddef 5612 enddef
5624 5613
5625 var d = D.new() 5614 var d = D.new()
5626 T(d) 5615 T(d)
5627 END 5616 END
5628 v9.CheckSourceFailure(lines, 'E1335: Variable "value" in class "A" is not writable') 5617 v9.CheckSourceFailure(lines, 'E1335: Variable "value" in class "A" is not writable', 1)
5629 enddef 5618 enddef
5630 5619
5631 " Test for calling methods using a null object 5620 " Test for calling methods using a null object
5632 def Test_null_object_method_call() 5621 def Test_null_object_method_call()
5633 # Calling a object method using a null object in script context 5622 # Calling a object method using a null object in script context