forked from PowerShell/PowerShell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscripting.Classes.inheritance.tests.ps1
More file actions
545 lines (469 loc) · 16.8 KB
/
Copy pathscripting.Classes.inheritance.tests.ps1
File metadata and controls
545 lines (469 loc) · 16.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
#
# Copyright (c) Microsoft Corporation, 2015
#
try {
#
# CrossGen'ed assemblies cause a hang to happen intermittently when running this test suite in Linux and OSX.
# The issue has been reported to CoreCLR team. We need to work around it for now with the following approach:
# 1. For pull request and push commit, build without '-CrossGen' and run the parsing tests
# 2. For daily build, build with '-CrossGen' but don't run the parsing tests
# In this way, we will continue to exercise these parsing tests for each CI build, and skip them for daily
# build to avoid a hang.
# Note: this change should be reverted once the 'CrossGen' issue is fixed by CoreCLR. The issue is tracked by
# https://github.com/dotnet/coreclr/issues/9745
#
$isDailyBuild = $env:TRAVIS_EVENT_TYPE -eq 'cron' -or $env:TRAVIS_EVENT_TYPE -eq 'api'
$defaultParamValues = $PSdefaultParameterValues.Clone()
$IsSkipped = (!$IsWindows -and $isDailyBuild)
$PSDefaultParameterValues["it:skip"] = $IsSkipped
$PSDefaultParameterValues["ShouldBeParseError:SkipInTravisFullBuild"] = $IsSkipped
Describe 'Classes inheritance syntax' -Tags "CI" {
It 'Base types' {
class C1 {}
class C2a : C1 {}
class C2b:C1 {}
[C2a]::new().GetType().BaseType.Name | Should Be "C1"
[C2b].BaseType.Name | Should Be "C1"
}
It 'inheritance from abstract base class with no abstract methods and protected ctor' {
class C3 : system.collections.collectionbase {}
class C4 { C4([int]$a) {} }
class C5 : C4 { C5() : base(1) {} }
}
It 'inheritance from base class with implicit ctor' {
class C6 {}
class C7 : C6 { C7() : base() {} }
}
It 'inheritance syntax allows newlines in various places' {
class C {}
class C2a:C,system.IDisposable{ [void] Dispose() { }}
class C2b
:
C
,
system.IDisposable
{
[void] Dispose() {}
C2b()
: # there are extra spaces here
base
(
)
{
}
}
[C2a].GetInterface("System.IDisposable") | Should Not Be $null
[C2b].GetInterface("System.IDisposable") | Should Not Be $null
}
It 'can subclass .NET type' {
class MyIntList : system.collections.generic.list[int] {}
[MyIntList]::new().GetType().BaseType.FullName.StartsWith('System.Collections.Generic.List') | Should Be $true
}
It 'can implement .NET interface' {
class MyComparable : system.IComparable
{
[int] CompareTo([object] $obj)
{
return 0;
}
}
[MyComparable].GetInterface("System.IComparable") | Should Not Be $null
}
It 'allows use of defined later type as a property type' {
class A { static [B]$b }
class B : A {}
[A]::b = [B]::new()
try {
[A]::b = "bla"
throw "No Exception!"
} catch {
$_.Exception | Should BeOfType 'System.Management.Automation.SetValueInvocationException'
}
}
}
Describe 'Classes inheritance syntax errors' -Tags "CI" {
ShouldBeParseError "class A : NonExistingClass {}" TypeNotFound 10
ShouldBeParseError "class A : {}" TypeNameExpected 9
ShouldBeParseError "class A {}; class B : A, {}" TypeNameExpected 24
ShouldBeParseError "class A{} ; class B : A[] {}" SubtypeArray 22 -SkipAndCheckRuntimeError
ShouldBeParseError "class A : System.Collections.Generic.List``1 {}" SubtypeUnclosedGeneric 10 -SkipAndCheckRuntimeError
ShouldBeParseError "class A {}; class B : A, NonExistingInterface {}" TypeNotFound 25
ShouldBeParseError "class A {} ; class B {}; class C : A, B {}" InterfaceNameExpected 38 -SkipAndCheckRuntimeError
ShouldBeParseError "class A{} ; class B : A, System.IDisposable[] {}" SubtypeArray 25 -SkipAndCheckRuntimeError
ShouldBeParseError "class A {}; class B : A, NonExistingInterface {}" TypeNotFound 25
# base should be accepted only on instance ctors
ShouldBeParseError 'class A { A($a){} } ; class B : A { foo() : base(1) {} }' MissingFunctionBody 41
ShouldBeParseError 'class A { static A() {} }; class B { static B() : base() {} }' MissingFunctionBody 47
# Incomplete input
ShouldBeParseError 'class A { A($a){} } ; class B : A { B() : bas {} }' MissingBaseCtorCall 41
ShouldBeParseError 'class A { A($a){} } ; class B : A { B() : base( {} }' @('MissingEndParenthesisInMethodCall', 'MissingFunctionBody') @(50, 39)
ShouldBeParseError 'class A { A($a){} } ; class B : A { B() : base {} }' @('MissingMethodParameterList', 'UnexpectedToken') @(46, 50)
# Sealed base
ShouldBeParseError "class baz : string {}" SealedBaseClass 12 -SkipAndCheckRuntimeError
# Non-existing Interface
ShouldBeParseError "class bar {}; class baz : bar, Non.Existing.Interface {}" TypeNotFound 31 -SkipAndCheckRuntimeError
# .NET abstract method not implemented
ShouldBeParseError "class MyType : Type {}" TypeCreationError 0 -SkipAndCheckRuntimeError
# inheritance doesn't allow non linear order
ShouldBeParseError "class A : B {}; class B {}" TypeNotFound 10 -SkipAndCheckRuntimeError
# inheritance doesn't allow circular order
ShouldBeParseError "class A : B {}; class B : A {}" TypeNotFound 10 -SkipAndCheckRuntimeError
ShouldBeParseError "class A : C {}; class B : A {}; class C : B {}" TypeNotFound 10 -SkipAndCheckRuntimeError
}
Describe 'Classes methods with inheritance' -Tags "CI" {
Context 'Method calls' {
It 'can call instance method on base class' {
class bar
{
[int]foo() {return 100500}
}
class baz : bar {}
[baz]::new().foo() | Should Be 100500
}
It 'can call static method on base class' {
class bar
{
static [int]foo() {return 100500}
}
class baz : bar {}
[baz]::foo() | Should Be 100500
}
It 'can access static and instance base class property' {
class A
{
static [int]$si
[int]$i
}
class B : A
{
[void]foo()
{
$this::si = 1001
$this.i = 1003
}
}
$b = [B]::new()
$b.foo()
[A]::si | Should Be 1001
($b.i) | Should Be 1003
}
It 'works with .NET types' {
class MyIntList : system.collections.generic.list[int] {}
$intList = [MyIntList]::new()
$intList.Add(100501)
$intList.Add(100502)
$intList.Count | Should Be 2
$intList[0] | Should Be 100501
$intList[1] | Should Be 100502
}
It 'overrides instance method' {
class bar
{
[int]foo() {return 100500}
}
class baz : bar
{
[int]foo() {return 200600}
}
[baz]::new().foo() | Should Be 200600
}
It 'allows base class method call and doesn''t fall into recursion' {
class bar
{
[int]foo() {return 1001}
}
class baz : bar
{
[int] $fooCallCounter
[int]foo()
{
if ($this.fooCallCounter++ -gt 0)
{
throw "Recursion happens"
}
return 3 * ([bar]$this).foo()
}
}
$res = [baz]::new().foo()
$res | Should Be 3003
}
It 'case insensitive for base class method calls' {
class bar
{
[int]foo() {return 1001}
}
class baz : bar
{
[int] $fooCallCounter
[int]fOo()
{
if ($this.fooCallCounter++ -gt 0)
{
throw "Recursion happens"
}
return ([bAr]$this).fOo() + ([bAr]$this).FOO()
}
}
$res = [baz]::new().foo()
$res | Should Be 2002
}
It 'allows any call from the inheritance hierarchy' {
class A
{
[string]GetName() {return "A"}
}
class B : A
{
[string]GetName() {return "B"}
}
class C : B
{
[string]GetName() {return "C"}
}
class D : C
{
[string]GetName() {return "D"}
}
$d = [D]::new()
([A]$d).GetName() | Should Be "A"
([B]$d).GetName() | Should Be "B"
([C]$d).GetName() | Should Be "C"
([D]$d).GetName() | Should Be "D"
$d.GetName() | Should Be "D"
}
It 'can call base method with params' {
class A
{
[string]ToStr([int]$a) {return "A" + $a}
}
class B : A
{
[string]ToStr([int]$a) {return "B" + $a}
}
$b = [B]::new()
([A]$b).ToStr(101) | Should Be "A101"
$b.ToStr(100) | Should Be "B100"
}
It 'can call base method with many params' {
class A
{
[string]ToStr([int]$a1, [int]$a2, [int]$a3, [int]$a4, [int]$a5, [int]$a6, [int]$a7, [int]$a8, [int]$a9, [int]$a10, [int]$a11, [int]$a12, [int]$a13, [int]$a14)
{
return "A"
}
[void]Noop([int]$a1, [int]$a2, [int]$a3, [int]$a4, [int]$a5, [int]$a6, [int]$a7, [int]$a8, [int]$a9, [int]$a10, [int]$a11, [int]$a12, [int]$a13, [int]$a14)
{
}
}
class B : A
{
[string]ToStr([int]$a1, [int]$a2, [int]$a3, [int]$a4, [int]$a5, [int]$a6, [int]$a7, [int]$a8, [int]$a9, [int]$a10, [int]$a11, [int]$a12, [int]$a13, [int]$a14)
{
return "B"
}
[void]Noop([int]$a1, [int]$a2, [int]$a3, [int]$a4, [int]$a5, [int]$a6, [int]$a7, [int]$a8, [int]$a9, [int]$a10, [int]$a11, [int]$a12, [int]$a13, [int]$a14)
{
}
}
$b = [B]::new()
# we don't really care about methods results, we only checks that calls doesn't throw
# 14 args is a limit
$b.ToStr(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14) | Should Be 'B'
([A]$b).ToStr(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14) | Should Be 'A'
# 14 args is a limit
$b.Noop(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14)
([A]$b).Noop(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14)
}
It 'overrides void method call' {
$script:voidOverrideVar = $null
class A
{
[void]SetStr([int]$a) {$script:voidOverrideVar = "A" + $a}
[void]SetStr() {$script:voidOverrideVar = "A"}
}
class B : A
{
[void]SetStr([int]$a) {$script:voidOverrideVar = "B" + $a}
[void]SetStr() {$script:voidOverrideVar = "B"}
}
$b = [B]::new()
([A]$b).SetStr(101)
$script:voidOverrideVar | Should Be "A101"
$b.SetStr(100)
$script:voidOverrideVar | Should Be "B100"
([A]$b).SetStr()
$script:voidOverrideVar | Should Be "A"
$b.SetStr()
$script:voidOverrideVar | Should Be "B"
}
It 'hides final .NET method' {
class MyIntList : system.collections.generic.list[int]
{
# Add is final, can we hide it?
[void] Add([int]$arg)
{
([system.collections.generic.list[int]]$this).Add($arg * 2)
}
}
$intList = [MyIntList]::new()
$intList.Add(100201)
$intList.Count | Should Be 1
$intList[0] | Should Be 200402
}
}
Context 'base static method call' {
class A
{
static [string]ToStr([int]$a) {return "A" + $a}
}
class B : A
{
static [string]ToStr([int]$a) {return "B" + $a}
}
$b = [B]::new()
# MSFT:1911652
# MSFT:2973835
It 'doesn''t affect static method call on type' -Skip {
([A]$b)::ToStr(101) | Should Be "A101"
}
It 'overrides static method call on instance' {
$b::ToStr(100) | Should Be "B100"
}
}
}
Describe 'Classes inheritance ctors syntax errors' -Tags "CI" {
#DotNet.Interface.NotImplemented
ShouldBeParseError "class MyComparable : system.IComparable {}" TypeCreationError 0 -SkipAndCheckRuntimeError
#DotNet.Interface.WrongSignature
ShouldBeParseError 'class MyComparable : system.IComparable { [void] CompareTo([object]$obj) {} }' TypeCreationError 0 -SkipAndCheckRuntimeError
#DotNet.NoDefaultCtor
ShouldBeParseError "class MyCollection : System.Collections.ObjectModel.ReadOnlyCollection[int] {}" BaseClassNoDefaultCtor 0 -SkipAndCheckRuntimeError
#NoDefaultCtor
ShouldBeParseError 'class A { A([int]$a) {} }; class B : A {}' BaseClassNoDefaultCtor 27 -SkipAndCheckRuntimeError
}
Describe 'Classes inheritance ctors' -Tags "CI" {
It 'can call base ctor' {
class A {
[int]$a
A([int]$a)
{
$this.a = $a
}
}
class B : A
{
B([int]$a) : base($a * 2) {}
}
$b = [B]::new(101)
$b.a | Should Be 202
}
# TODO: can we detect it in the parse time?
It 'cannot call base ctor with the wrong number of parameters' {
class A {
[int]$a
A([int]$a)
{
$this.a = $a
}
}
class B : A
{
B([int]$a) : base($a * 2, 100) {}
}
try {
[B]::new(101)
throw "No Exception!"
} catch {
$_.Exception | Should BeOfType "System.Management.Automation.MethodException"
}
}
It 'call default base ctor implicitly' {
class A {
[int]$a
A()
{
$this.a = 1007
}
}
class B : A
{
B() {}
}
class C : A
{
}
$b = [B]::new()
$c = [C]::new()
$b.a | Should Be 1007
$c.a | Should Be 1007
}
It 'doesn''t allow base ctor as an explicit method call' {
$o = [object]::new()
try {
# we should not allow direct .ctor call.
$o.{.ctor}()
} catch {
$_.FullyQualifiedErrorId | Should Be MethodNotFound
return
}
# Fail
'' | Should Be "Exception expected"
}
It 'allow use conversion [string -> int] in base ctor call' {
class A {
[int]$a
A([int]$a)
{
$this.a = $a
}
}
class B : A
{
B() : base("103") {}
}
$b = [B]::new()
$b.a | Should Be 103
}
It 'resolves ctor call based on argument type' {
class A {
[int]$i
[string]$s
A([int]$a)
{
$this.i = $a
}
A([string]$a)
{
$this.s = $a
}
}
class B : A
{
B($a) : base($a) {}
}
$b1 = [B]::new("foo")
$b2 = [B]::new(1001)
$b1.s | Should Be "foo"
$b2.i | Should Be 1001
}
}
Describe 'Type creation' -Tags "CI" {
It 'can call super-class methods sequentially' {
$sb = [scriptblock]::Create(@'
class Base
{
[int] foo() { return 100 }
}
class Derived : Base
{
[int] foo() { return 2 * ([Base]$this).foo() }
}
[Derived]::new().foo()
'@)
$sb.Invoke() | Should Be 200
$sb.Invoke() | Should Be 200
}
}
} finally {
$global:PSdefaultParameterValues = $defaultParamValues
}