1 # Copyright (c) Microsoft Corporation. 2 # Licensed under the MIT License. 3 4 Describe "Interface static members" -Tags "CI" { 5 BeforeAll { 6 $testType = "InterfaceStaticMemberTest.ITest" -as [Type] 7 if (-not $testType) { 8 Add-Type -TypeDefinition @' 9 using System; 10 11 namespace InterfaceStaticMemberTest { 12 public interface ITest { 13 public const int MyConst = 17; 14 15 public static string Type = "ITest-Type"; 16 public static string Name => "Joe"; 17 public static string GetPath() => "Path"; 18 public int GetAge(); 19 public int GetId(); 20 } 21 22 public class Foo : ITest { 23 int ITest.GetId() { return 100; } 24 int ITest.GetAge() { return 2; } 25 } 26 27 public class Zoo : Foo { 28 public static string Type = "Zoo-Type"; 29 } 30 } 31 '@ 32 } 33 } 34 35 It "Access static/const members on the interface type" { 36 [InterfaceStaticMemberTest.ITest]::MyConst | Should -Be 17 37 [InterfaceStaticMemberTest.ITest]::Type | Should -Be "ITest-Type" 38 [InterfaceStaticMemberTest.ITest]::Name | Should -Be "Joe" 39 [InterfaceStaticMemberTest.ITest]::GetPath() | Should -Be "Path" 40 41 $type = [InterfaceStaticMemberTest.ITest] 42 $type::MyConst | Should -Be 17 43 $type::Type | Should -Be "ITest-Type" 44 $type::Name | Should -Be "Joe" 45 $type::GetPath() | Should -Be "Path" 46 47 { [InterfaceStaticMemberTest.ITest]::Name = 'Jane' } | Should -Throw -ErrorId 'PropertyAssignmentException' 48 { $type::Name = 'Jane' } | Should -Throw -ErrorId 'PropertyAssignmentException' 49 } 50 51 It "Access interface static/const members on the implementation type" { 52 [InterfaceStaticMemberTest.Foo]::MyConst | Should -Be 17 53 [InterfaceStaticMemberTest.Foo]::Type | Should -Be "ITest-Type" 54 [InterfaceStaticMemberTest.Foo]::Name | Should -Be "Joe" 55 [InterfaceStaticMemberTest.Foo]::GetPath() | Should -Be "Path" 56 [InterfaceStaticMemberTest.Foo]::get_Name() | Should -Be "Joe" 57 58 $type = [InterfaceStaticMemberTest.Foo] 59 $type::MyConst | Should -Be 17 60 $type::Type | Should -Be "ITest-Type" 61 $type::Name | Should -Be "Joe" 62 $type::GetPath() | Should -Be "Path" 63 $type::get_Name() | Should -Be "Joe" 64 65 { [InterfaceStaticMemberTest.Foo]::Name = 'Jane' } | Should -Throw -ErrorId 'PropertyAssignmentException' 66 { $type::Name = 'Jane' } | Should -Throw -ErrorId 'PropertyAssignmentException' 67 } 68 69 It "Static field with the same name on the implementation type overrides the one from the interface" { 70 [InterfaceStaticMemberTest.Zoo]::Type | Should -Be "Zoo-Type" 71 72 $type = [InterfaceStaticMemberTest.Zoo] 73 $type::Type | Should -Be "Zoo-Type" 74 75 $nameMember = $type | Get-Member -Static -Name Name 76 $nameMember | Should -Not -BeNullOrEmpty 77 $nameMember.Name | Should -Be "Name" 78 $nameMember.MemberType | Should -Be "Property" 79 80 $getNameMember = $type | Get-Member -Static -Name 'get_Name' 81 $getNameMember | Should -BeNullOrEmpty 82 83 $getNameMember = $type | Get-Member -Static -Name 'get_Name' -Force 84 $getNameMember | Should -Not -BeNullOrEmpty 85 $getNameMember.Name | Should -Be "get_Name" 86 $getNameMember.MemberType | Should -Be "Method" 87 88 $type::Name | Should -Be "Joe" 89 $type::get_Name() | Should -Be "Joe" 90 } 91 92 It "Explicitly implemented interface members are visible/accessible by default" { 93 $obj = [InterfaceStaticMemberTest.Zoo]::new() 94 95 $ageMember = $obj | Get-Member -Name GetAge 96 $ageMember | Should -Not -BeNullOrEmpty 97 $ageMember.Name | Should -Be "GetAge" 98 $ageMember.MemberType | Should -Be "Method" 99 $ageMember.Definition | Should -Be "int ITest.GetAge()" 100 101 $idMember = $obj | Get-Member -Name GetId 102 $idMember | Should -Not -BeNullOrEmpty 103 $idMember.Name | Should -Be "GetId" 104 $idMember.MemberType | Should -Be "Method" 105 $idMember.Definition | Should -Be "int ITest.GetId()" 106 107 $obj.GetAge() | Should -Be 2 108 $obj.GetId() | Should -Be 100 109 } 110 }