Fix PowerShell class to support deriving from an abstract class with abstract properties (#21331)

This commit is contained in:
Dongbo Wang
2024-03-13 14:44:57 -07:00
committed by GitHub
parent ad2bf787e4
commit 4e357f129e
2 changed files with 60 additions and 5 deletions
@@ -628,3 +628,47 @@ class Derived : Base
$sb.Invoke() | Should -Be 200
}
}
Describe 'Base type has abstract properties' -Tags "CI" {
It 'can derive from `FileSystemInfo`' {
## FileSystemInfo has 3 abstract members that a derived type needs to implement
## - public abstract bool Exists { get; }
## - public abstract string Name { get; }
## - public abstract void Delete ();
class myFileSystemInfo : System.IO.FileSystemInfo
{
[string] $Name
[bool] $Exists
myFileSystemInfo([string]$path)
{
# ctor
$this.Name = $path
$this.Exists = $true
}
[void] Delete()
{
}
}
$myFile = [myFileSystemInfo]::new('Hello')
$myFile.Name | Should -Be 'Hello'
$myFile.Exists | Should -BeTrue
}
It 'deriving from `FileSystemInfo` will fail when the abstract property `Exists` is not implemented' {
$script = [scriptblock]::Create('class WillFail : System.IO.FileSystemInfo { [string] $Name }')
$failure = $null
try {
& $script
} catch {
$failure = $_
}
$failure | Should -Not -BeNullOrEmpty
$failure.FullyQualifiedErrorId | Should -BeExactly "TypeCreationError"
$failure.Exception.Message | Should -BeLike "*'get_Exists'*"
}
}