File size: 9,817 Bytes
8c763fb | 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 | # Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
Describe "Get-Member" -Tags "CI" {
It "Should be able to be called on string objects, ints, arrays, etc" {
$a = 1 #test numbers
$b = 1.3
$c = $false #test bools
$d = @(1, 3) # test arrays
$e = "anoeduntodeu" #test strings
$f = 'asntoheusth' #test strings
Get-Member -InputObject $a | Should -Not -BeNullOrEmpty
Get-Member -InputObject $b | Should -Not -BeNullOrEmpty
Get-Member -InputObject $c | Should -Not -BeNullOrEmpty
Get-Member -InputObject $d | Should -Not -BeNullOrEmpty
Get-Member -InputObject $e | Should -Not -BeNullOrEmpty
Get-Member -InputObject $f | Should -Not -BeNullOrEmpty
}
It "Should be able to extract a field from string objects, ints, arrays, etc" {
$a = 1 #test numbers
$b = 1.3
$c = $false #test bools
$d = @(1, 3) # test arrays
$e = "anoeduntodeu" #test strings
$f = 'asntoheusth' #test strings
$a | Should -BeOfType Int32
$b | Should -BeOfType Double
$c | Should -BeOfType Boolean
, $d | Should -BeOfType Object[]
$e | Should -BeOfType String
$f | Should -BeOfType String
}
It "Should be able to be called on a newly created PSObject" {
$o = New-Object psobject
# this creates a dependency on the Add-Member cmdlet.
Add-Member -InputObject $o -MemberType NoteProperty -Name proppy -Value "superVal"
Get-Member -InputObject $o | Should -Not -BeNullOrEmpty
}
It "Should be able to be called on IntPtr" {
$results = [System.IntPtr] | Get-Member -Type Property -Static | Sort-Object -Property Name
$results.Count | Should -BeExactly 4
$results[0].Name | Should -BeExactly 'MaxValue'
$results[1].Name | Should -BeExactly 'MinValue'
$results[2].Name | Should -BeExactly 'Size'
$results[3].Name | Should -BeExactly 'Zero'
}
It "Should work with incomplete parameter '-i'" {
$a = 1
{ Get-Member -i $a } | Should -Not -Throw
}
}
Describe "Get-Member DRT Unit Tests" -Tags "CI" {
Context "Verify Get-Member with Class" {
if (-not ([System.Management.Automation.PSTypeName]'Employee').Type) {
Add-Type -TypeDefinition @"
public class Employee
{
private string firstName;
private string lastName;
private int yearsInMS;
public string FirstName
{
get
{
return firstName;
}
set
{
firstName = value;
}
}
public string LastName
{
get
{
return lastName;
}
set
{
lastName = value;
}
}
public int YearsInMS
{
get
{
return yearsInMS;
}
set
{
yearsInMS = value;
}
}
public Employee(string firstName, string lastName, int yearsInMS)
{
this.firstName = firstName;
this.lastName = lastName;
this.yearsInMS = yearsInMS;
}
}
"@
}
$fileToDeleteName = Join-Path $TestDrive -ChildPath "getMemberTest.ps1xml"
$XMLFile = @"
<Types>
<Type>
<Name>Employee</Name>
<Members>
<PropertySet>
<Name>PropertySetName</Name>
<ReferencedProperties>
<Name>FirstName</Name>
<Name>LastName</Name>
</ReferencedProperties>
</PropertySet>
<PropertySet>
<Name>FullSet</Name>
<ReferencedProperties>
<Name>FirstName</Name>
<Name>LastName</Name>
<Name>YearsInMS</Name>
</ReferencedProperties>
</PropertySet>
</Members>
</Type>
<Type>
<Name>EmployeePartTime</Name>
<Members>
<PropertySet>
<Name>PropertySetName</Name>
<ReferencedProperties>
<Name>FirstName</Name>
<Name>HoursPerWeek</Name>
</ReferencedProperties>
</PropertySet>
<PropertySet>
<Name>FullSet</Name>
<ReferencedProperties>
<Name>FirstName</Name>
<Name>LastName</Name>
<Name>HoursPerWeek</Name>
</ReferencedProperties>
</PropertySet>
</Members>
</Type>
</Types>
"@
$XMLFile > $fileToDeleteName
Update-TypeData -AppendPath $fileToDeleteName
It "Fail to get member without any input" {
{ Get-Member -MemberType All -ErrorAction Stop } | Should -Throw -ErrorId 'NoObjectInGetMember,Microsoft.PowerShell.Commands.GetMemberCommand'
}
It 'Get the expected Properties of "Employee" object' {
$emps = [Employee]::New("john", "smith", 5), [Employee]::New("joesph", "smith", 15), [Employee]::New("john", "smyth", 2)
$results = $emps | Get-Member -MemberType Property
$results.Length | Should -Be 3
$results[0].Name | Should -BeExactly "FirstName"
$results[1].Name | Should -BeExactly "LastName"
$results[2].Name | Should -BeExactly "YearsInMS"
}
It 'Get the Public Methods of "Employee" object' {
$emps = [Employee]::New("john", "smith", 5), [Employee]::New("joesph", "smith", 15), [Employee]::New("john", "smyth", 2)
$methodList = "GetHashCode", "Equals", "ToString", "GetType"
$results = $emps | Get-Member -MemberType Method
$results.Length | Should -Be $methodList.Length
$methodFound = @()
for ($i = 0; $i -lt $methodList.Length; $i++) {
for ($j = 0; $j -lt $results.Length; $j++) {
if ($results[$j].Name.Equals($methodList[$i])) {
$methodFound += $true
}
}
}
for ($i = 0; $i -lt $methodList.Length; $i++) {
$methodFound[$i] | Should -BeTrue
}
}
It 'Get property sets defined in private members' {
$emps = [Employee]::New("john", "smith", 5), [Employee]::New("joesph", "smith", 15), [Employee]::New("john", "smyth", 2)
$results = $emps | Get-Member -MemberType PropertySet
$results.Length | Should -Be 2
$results[0].Name | Should -BeExactly "FullSet"
$results[1].Name | Should -BeExactly "PropertySetName"
}
}
Context "Verify Get-Member with Static Parameter" {
It 'Get the static properties and methods of the object' {
$obj = New-Object -TypeName System.Int32
$results = $obj | Get-Member -Static
$members = "MaxValue", "MinValue", "Parse", "TryParse"
foreach ($member in $members) {
foreach ($result in $results) {
if ($result.Name.Equals($member)) {
return $true
}
}
return $false
}
}
It "Get the static properties and methods of int instance" {
$results = 1 | Get-Member -Static
$members = "MaxValue", "MinValue", "Parse", "TryParse"
foreach ($member in $members) {
foreach ($result in $results) {
if ($result.Name.Equals($member)) {
return $true
}
}
return $false
}
}
It "Get the static properties and methods of int instance Wrapped" {
$results = [pscustomobject]1 | Get-Member -Static
$members = "MaxValue", "MinValue", "Parse", "TryParse"
foreach ($member in $members) {
foreach ($result in $results) {
if ($result.Name.Equals($member)) {
return $true
}
}
return $false
}
}
}
Context "Verify Get-Member with other parameters" {
It 'works with View Parameter' {
$results = [xml]'<a>some text</a>' | Get-Member -View adapted
$results | Where-Object Name -EQ a | Should -Not -BeNullOrEmpty
$results | Where-Object Name -EQ CreateElement | Should -Not -BeNullOrEmpty
$results | Where-Object Name -EQ CreateNode | Should -Not -BeNullOrEmpty
}
It 'Get hidden members' {
$results = 'abc' | Get-Member -Force
$hiddenMembers = "psbase", "psextended", "psadapted", "pstypenames", "psobject"
foreach ($member in $hiddenMembers) {
foreach ($result in $results) {
if ($result.Name.Equals($member)) {
return $true
}
}
return $false
}
}
It 'Get Set Property Accessors On PsBase' {
$results = ('abc').psbase | Get-Member -Force get_*
$expectedMembers = "get_Chars", "get_Length"
foreach ($member in $expectedMembers) {
foreach ($result in $results) {
if ($result.Name.Equals($member)) {
return $true
}
}
return $false
}
}
}
}
|