2016-07-19 22:52:03 +02:00
|
|
|
#
|
|
|
|
# Copyright (c) Microsoft Corporation, 2015
|
|
|
|
#
|
|
|
|
|
2016-07-25 23:01:41 +02:00
|
|
|
Describe 'Classes inheritance syntax' -Tags "CI" {
|
2016-07-19 22:52:03 +02:00
|
|
|
|
|
|
|
It 'Base types' {
|
|
|
|
class C1 {}
|
|
|
|
class C2a : C1 {}
|
|
|
|
class C2b:C1 {}
|
2017-01-16 22:31:14 +01:00
|
|
|
|
2016-07-19 22:52:03 +02:00
|
|
|
[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() {} }
|
|
|
|
}
|
|
|
|
|
2016-07-20 02:21:32 +02:00
|
|
|
It 'inheritance syntax allows newlines in various places' {
|
2016-07-19 22:52:03 +02:00
|
|
|
class C {}
|
2016-07-20 02:21:32 +02:00
|
|
|
class C2a:C,system.IDisposable{ [void] Dispose() { }}
|
2017-01-16 22:31:14 +01:00
|
|
|
class C2b
|
|
|
|
:
|
2016-07-19 22:52:03 +02:00
|
|
|
C
|
2017-01-16 22:31:14 +01:00
|
|
|
,
|
|
|
|
system.IDisposable
|
|
|
|
{
|
2016-07-20 02:21:32 +02:00
|
|
|
[void] Dispose() {}
|
2016-07-19 22:52:03 +02:00
|
|
|
C2b()
|
|
|
|
: # there are extra spaces here
|
|
|
|
base
|
|
|
|
(
|
|
|
|
)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-20 02:21:32 +02:00
|
|
|
[C2a].GetInterface("System.IDisposable") | Should Not Be $null
|
|
|
|
[C2b].GetInterface("System.IDisposable") | Should Not Be $null
|
2016-07-19 22:52:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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' {
|
2017-01-16 22:31:14 +01:00
|
|
|
class MyComparable : system.IComparable
|
2016-07-19 22:52:03 +02:00
|
|
|
{
|
|
|
|
[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"
|
2017-02-16 01:40:51 +01:00
|
|
|
throw "No Exception!"
|
2016-07-19 22:52:03 +02:00
|
|
|
} catch {
|
2017-02-16 01:40:51 +01:00
|
|
|
$_.Exception | Should BeOfType 'System.Management.Automation.SetValueInvocationException'
|
2016-07-19 22:52:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-26 23:52:03 +02:00
|
|
|
Describe 'Classes inheritance syntax errors' -Tags "CI" {
|
2016-07-19 22:52:03 +02:00
|
|
|
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
|
2016-07-20 02:21:32 +02:00
|
|
|
ShouldBeParseError "class A{} ; class B : A, System.IDisposable[] {}" SubtypeArray 25 -SkipAndCheckRuntimeError
|
2016-07-19 22:52:03 +02:00
|
|
|
ShouldBeParseError "class A {}; class B : A, NonExistingInterface {}" TypeNotFound 25
|
|
|
|
|
|
|
|
# base should be accepted only on instance ctors
|
2017-01-16 22:31:14 +01:00
|
|
|
ShouldBeParseError 'class A { A($a){} } ; class B : A { foo() : base(1) {} }' MissingFunctionBody 41
|
2016-07-19 22:52:03 +02:00
|
|
|
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
|
|
|
|
|
2017-01-16 22:31:14 +01:00
|
|
|
# .NET abstract method not implemented
|
2016-07-19 22:52:03 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2016-07-26 23:52:03 +02:00
|
|
|
Describe 'Classes methods with inheritance' -Tags "CI" {
|
2016-07-19 22:52:03 +02:00
|
|
|
|
|
|
|
Context 'Method calls' {
|
|
|
|
|
|
|
|
It 'can call instance method on base class' {
|
2017-01-16 22:31:14 +01:00
|
|
|
class bar
|
2016-07-19 22:52:03 +02:00
|
|
|
{
|
|
|
|
[int]foo() {return 100500}
|
|
|
|
}
|
|
|
|
class baz : bar {}
|
|
|
|
[baz]::new().foo() | Should Be 100500
|
|
|
|
}
|
|
|
|
|
|
|
|
It 'can call static method on base class' {
|
2017-01-16 22:31:14 +01:00
|
|
|
class bar
|
2016-07-19 22:52:03 +02:00
|
|
|
{
|
|
|
|
static [int]foo() {return 100500}
|
|
|
|
}
|
|
|
|
class baz : bar {}
|
|
|
|
[baz]::foo() | Should Be 100500
|
|
|
|
}
|
|
|
|
|
|
|
|
It 'can access static and instance base class property' {
|
2017-01-16 22:31:14 +01:00
|
|
|
class A
|
|
|
|
{
|
|
|
|
static [int]$si
|
|
|
|
[int]$i
|
2016-07-19 22:52:03 +02:00
|
|
|
}
|
2017-01-16 22:31:14 +01:00
|
|
|
class B : A
|
2016-07-19 22:52:03 +02:00
|
|
|
{
|
|
|
|
[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' {
|
2017-01-16 22:31:14 +01:00
|
|
|
class bar
|
2016-07-19 22:52:03 +02:00
|
|
|
{
|
|
|
|
[int]foo() {return 100500}
|
|
|
|
}
|
2017-01-16 22:31:14 +01:00
|
|
|
class baz : bar
|
2016-07-19 22:52:03 +02:00
|
|
|
{
|
|
|
|
[int]foo() {return 200600}
|
|
|
|
}
|
|
|
|
[baz]::new().foo() | Should Be 200600
|
|
|
|
}
|
|
|
|
|
|
|
|
It 'allows base class method call and doesn''t fall into recursion' {
|
2017-01-16 22:31:14 +01:00
|
|
|
class bar
|
2016-07-19 22:52:03 +02:00
|
|
|
{
|
|
|
|
[int]foo() {return 1001}
|
|
|
|
}
|
2017-01-16 22:31:14 +01:00
|
|
|
class baz : bar
|
2016-07-19 22:52:03 +02:00
|
|
|
{
|
|
|
|
[int] $fooCallCounter
|
2017-01-16 22:31:14 +01:00
|
|
|
[int]foo()
|
2016-07-19 22:52:03 +02:00
|
|
|
{
|
2017-01-16 22:31:14 +01:00
|
|
|
if ($this.fooCallCounter++ -gt 0)
|
2016-07-19 22:52:03 +02:00
|
|
|
{
|
|
|
|
throw "Recursion happens"
|
|
|
|
}
|
2017-01-16 22:31:14 +01:00
|
|
|
return 3 * ([bar]$this).foo()
|
2016-07-19 22:52:03 +02:00
|
|
|
}
|
|
|
|
}
|
2017-01-16 22:31:14 +01:00
|
|
|
|
2016-07-19 22:52:03 +02:00
|
|
|
$res = [baz]::new().foo()
|
|
|
|
$res | Should Be 3003
|
|
|
|
}
|
|
|
|
|
|
|
|
It 'case insensitive for base class method calls' {
|
2017-01-16 22:31:14 +01:00
|
|
|
class bar
|
2016-07-19 22:52:03 +02:00
|
|
|
{
|
|
|
|
[int]foo() {return 1001}
|
|
|
|
}
|
2017-01-16 22:31:14 +01:00
|
|
|
class baz : bar
|
2016-07-19 22:52:03 +02:00
|
|
|
{
|
|
|
|
[int] $fooCallCounter
|
2017-01-16 22:31:14 +01:00
|
|
|
[int]fOo()
|
2016-07-19 22:52:03 +02:00
|
|
|
{
|
2017-01-16 22:31:14 +01:00
|
|
|
if ($this.fooCallCounter++ -gt 0)
|
2016-07-19 22:52:03 +02:00
|
|
|
{
|
|
|
|
throw "Recursion happens"
|
|
|
|
}
|
|
|
|
return ([bAr]$this).fOo() + ([bAr]$this).FOO()
|
|
|
|
}
|
|
|
|
}
|
2017-01-16 22:31:14 +01:00
|
|
|
|
2016-07-19 22:52:03 +02:00
|
|
|
$res = [baz]::new().foo()
|
|
|
|
$res | Should Be 2002
|
|
|
|
}
|
|
|
|
|
|
|
|
It 'allows any call from the inheritance hierarchy' {
|
2017-01-16 22:31:14 +01:00
|
|
|
class A
|
2016-07-19 22:52:03 +02:00
|
|
|
{
|
|
|
|
[string]GetName() {return "A"}
|
|
|
|
}
|
|
|
|
class B : A
|
|
|
|
{
|
|
|
|
[string]GetName() {return "B"}
|
|
|
|
}
|
|
|
|
class C : B
|
|
|
|
{
|
|
|
|
[string]GetName() {return "C"}
|
|
|
|
}
|
2017-01-16 22:31:14 +01:00
|
|
|
class D : C
|
2016-07-19 22:52:03 +02:00
|
|
|
{
|
|
|
|
[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' {
|
2017-01-16 22:31:14 +01:00
|
|
|
class A
|
2016-07-19 22:52:03 +02:00
|
|
|
{
|
|
|
|
[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' {
|
2017-01-16 22:31:14 +01:00
|
|
|
class A
|
2016-07-19 22:52:03 +02:00
|
|
|
{
|
|
|
|
[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)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
}
|
2017-01-16 22:31:14 +01:00
|
|
|
$b = [B]::new()
|
2016-07-19 22:52:03 +02:00
|
|
|
|
|
|
|
# we don't really care about methods results, we only checks that calls doesn't throw
|
2017-01-16 22:31:14 +01:00
|
|
|
|
2016-07-19 22:52:03 +02:00
|
|
|
# 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'
|
2017-01-16 22:31:14 +01:00
|
|
|
|
2016-07-19 22:52:03 +02:00
|
|
|
# 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
|
2017-01-16 22:31:14 +01:00
|
|
|
class A
|
2016-07-19 22:52:03 +02:00
|
|
|
{
|
|
|
|
[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' {
|
2017-01-16 22:31:14 +01:00
|
|
|
class MyIntList : system.collections.generic.list[int]
|
2016-07-19 22:52:03 +02:00
|
|
|
{
|
|
|
|
# Add is final, can we hide it?
|
2017-01-16 22:31:14 +01:00
|
|
|
[void] Add([int]$arg)
|
2016-07-19 22:52:03 +02:00
|
|
|
{
|
|
|
|
([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' {
|
2017-01-16 22:31:14 +01:00
|
|
|
class A
|
2016-07-19 22:52:03 +02:00
|
|
|
{
|
|
|
|
static [string]ToStr([int]$a) {return "A" + $a}
|
|
|
|
}
|
|
|
|
class B : A
|
|
|
|
{
|
|
|
|
static [string]ToStr([int]$a) {return "B" + $a}
|
|
|
|
}
|
|
|
|
|
|
|
|
$b = [B]::new()
|
|
|
|
|
2017-01-16 22:31:14 +01:00
|
|
|
# MSFT:1911652
|
2016-07-19 22:52:03 +02:00
|
|
|
# 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"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-07-26 23:52:03 +02:00
|
|
|
Describe 'Classes inheritance ctors syntax errors' -Tags "CI" {
|
2017-01-16 22:31:14 +01:00
|
|
|
|
2016-07-19 22:52:03 +02:00
|
|
|
#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
|
|
|
|
}
|
|
|
|
|
2016-07-26 23:52:03 +02:00
|
|
|
Describe 'Classes inheritance ctors' -Tags "CI" {
|
2017-01-16 22:31:14 +01:00
|
|
|
|
2016-07-19 22:52:03 +02:00
|
|
|
It 'can call base ctor' {
|
2017-01-16 22:31:14 +01:00
|
|
|
class A {
|
|
|
|
[int]$a
|
|
|
|
A([int]$a)
|
2016-07-19 22:52:03 +02:00
|
|
|
{
|
|
|
|
$this.a = $a
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-16 22:31:14 +01:00
|
|
|
class B : A
|
|
|
|
{
|
2016-07-19 22:52:03 +02:00
|
|
|
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' {
|
2017-01-16 22:31:14 +01:00
|
|
|
class A {
|
|
|
|
[int]$a
|
|
|
|
A([int]$a)
|
2016-07-19 22:52:03 +02:00
|
|
|
{
|
|
|
|
$this.a = $a
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-16 22:31:14 +01:00
|
|
|
class B : A
|
|
|
|
{
|
2016-07-19 22:52:03 +02:00
|
|
|
B([int]$a) : base($a * 2, 100) {}
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
[B]::new(101)
|
2017-02-16 01:40:51 +01:00
|
|
|
throw "No Exception!"
|
2016-07-19 22:52:03 +02:00
|
|
|
} catch {
|
2017-02-16 01:40:51 +01:00
|
|
|
$_.Exception | Should BeOfType "System.Management.Automation.MethodException"
|
2016-07-19 22:52:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
It 'call default base ctor implicitly' {
|
2017-01-16 22:31:14 +01:00
|
|
|
class A {
|
|
|
|
[int]$a
|
|
|
|
A()
|
2016-07-19 22:52:03 +02:00
|
|
|
{
|
|
|
|
$this.a = 1007
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-16 22:31:14 +01:00
|
|
|
class B : A
|
|
|
|
{
|
2016-07-19 22:52:03 +02:00
|
|
|
B() {}
|
|
|
|
}
|
|
|
|
|
2017-01-16 22:31:14 +01:00
|
|
|
class C : A
|
|
|
|
{
|
2016-07-19 22:52:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$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
|
2017-01-16 22:31:14 +01:00
|
|
|
return
|
2016-07-19 22:52:03 +02:00
|
|
|
}
|
|
|
|
# Fail
|
|
|
|
'' | Should Be "Exception expected"
|
|
|
|
}
|
2017-01-16 22:31:14 +01:00
|
|
|
|
2016-08-25 19:49:32 +02:00
|
|
|
It 'allow use conversion [string -> int] in base ctor call' {
|
2017-01-16 22:31:14 +01:00
|
|
|
class A {
|
|
|
|
[int]$a
|
|
|
|
A([int]$a)
|
2016-07-19 22:52:03 +02:00
|
|
|
{
|
|
|
|
$this.a = $a
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-16 22:31:14 +01:00
|
|
|
class B : A
|
|
|
|
{
|
2016-07-19 22:52:03 +02:00
|
|
|
B() : base("103") {}
|
|
|
|
}
|
|
|
|
|
|
|
|
$b = [B]::new()
|
|
|
|
$b.a | Should Be 103
|
|
|
|
}
|
|
|
|
|
|
|
|
It 'resolves ctor call based on argument type' {
|
2017-01-16 22:31:14 +01:00
|
|
|
class A {
|
2016-07-19 22:52:03 +02:00
|
|
|
[int]$i
|
|
|
|
[string]$s
|
2017-01-16 22:31:14 +01:00
|
|
|
A([int]$a)
|
2016-07-19 22:52:03 +02:00
|
|
|
{
|
|
|
|
$this.i = $a
|
|
|
|
}
|
2017-01-16 22:31:14 +01:00
|
|
|
A([string]$a)
|
2016-07-19 22:52:03 +02:00
|
|
|
{
|
|
|
|
$this.s = $a
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-16 22:31:14 +01:00
|
|
|
class B : A
|
|
|
|
{
|
2016-07-19 22:52:03 +02:00
|
|
|
B($a) : base($a) {}
|
|
|
|
}
|
|
|
|
|
|
|
|
$b1 = [B]::new("foo")
|
|
|
|
$b2 = [B]::new(1001)
|
|
|
|
$b1.s | Should Be "foo"
|
|
|
|
$b2.i | Should Be 1001
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-26 23:52:03 +02:00
|
|
|
Describe 'Type creation' -Tags "CI" {
|
2016-07-19 22:52:03 +02:00
|
|
|
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() }
|
|
|
|
}
|
|
|
|
|
2017-01-16 22:31:14 +01:00
|
|
|
[Derived]::new().foo()
|
2016-07-19 22:52:03 +02:00
|
|
|
'@)
|
|
|
|
$sb.Invoke() | Should Be 200
|
|
|
|
$sb.Invoke() | Should Be 200
|
|
|
|
}
|
2016-07-20 02:21:32 +02:00
|
|
|
}
|