Team Ai
Datasetpublic

MegaBites-AI/Windows-powershell

sourceHugging Facemitupdated 6mo agoView on Hugging Face
0likes372downloads
Get-Member.Tests.ps1283 linesDownload Raw Back to Microsoft.PowerShell.Utility
1# Copyright (c) Microsoft Corporation.2# Licensed under the MIT License.3Describe "Get-Member" -Tags "CI" {4    It "Should be able to be called on string objects, ints, arrays, etc" {5        $a = 1 #test numbers6        $b = 1.37        $c = $false #test bools8        $d = @(1, 3) # test arrays9        $e = "anoeduntodeu" #test strings10        $f = 'asntoheusth' #test strings11 12        Get-Member -InputObject $a | Should -Not -BeNullOrEmpty13        Get-Member -InputObject $b | Should -Not -BeNullOrEmpty14        Get-Member -InputObject $c | Should -Not -BeNullOrEmpty15        Get-Member -InputObject $d | Should -Not -BeNullOrEmpty16        Get-Member -InputObject $e | Should -Not -BeNullOrEmpty17        Get-Member -InputObject $f | Should -Not -BeNullOrEmpty18    }19 20    It "Should be able to extract a field from string objects, ints, arrays, etc" {21        $a = 1 #test numbers22        $b = 1.323        $c = $false #test bools24        $d = @(1, 3) # test arrays25        $e = "anoeduntodeu" #test strings26        $f = 'asntoheusth' #test strings27 28        $a | Should -BeOfType Int3229        $b | Should -BeOfType Double30        $c | Should -BeOfType Boolean31        , $d | Should -BeOfType Object[]32        $e | Should -BeOfType String33        $f | Should -BeOfType String34    }35 36    It "Should be able to be called on a newly created PSObject" {37        $o = New-Object psobject38        # this creates a dependency on the Add-Member cmdlet.39        Add-Member -InputObject $o -MemberType NoteProperty -Name proppy -Value "superVal"40 41        Get-Member -InputObject $o | Should -Not -BeNullOrEmpty42    }43 44    It "Should be able to be called on IntPtr" {45        $results = [System.IntPtr] | Get-Member -Type Property -Static | Sort-Object -Property Name46        $results.Count | Should -BeExactly 447        $results[0].Name | Should -BeExactly 'MaxValue'48        $results[1].Name | Should -BeExactly 'MinValue'49        $results[2].Name | Should -BeExactly 'Size'50        $results[3].Name | Should -BeExactly 'Zero'51    }52 53    It "Should work with incomplete parameter '-i'" {54        $a = 155        { Get-Member -i $a } | Should -Not -Throw56    }57}58 59Describe "Get-Member DRT Unit Tests" -Tags "CI" {60    Context "Verify Get-Member with Class" {61        if (-not ([System.Management.Automation.PSTypeName]'Employee').Type) {62            Add-Type -TypeDefinition @"63        public class Employee64        {65            private string firstName;66            private string lastName;67            private int    yearsInMS;68 69            public string FirstName70            {71                get72                {73                    return firstName;74                }75                set76                {77                    firstName = value;78                }79            }80 81            public string LastName82            {83                get84                {85                    return lastName;86                }87                set88                {89                    lastName = value;90                }91            }92 93            public int YearsInMS94            {95                get96                {97                    return yearsInMS;98                }99                set100                {101                    yearsInMS = value;102                }103            }104 105            public Employee(string firstName, string lastName, int yearsInMS)106            {107                this.firstName = firstName;108                this.lastName  = lastName;109                this.yearsInMS = yearsInMS;110            }111        }112"@113        }114 115        $fileToDeleteName = Join-Path $TestDrive -ChildPath "getMemberTest.ps1xml"116        $XMLFile = @"117<Types>118    <Type>119    <Name>Employee</Name>120    <Members>121        <PropertySet>122            <Name>PropertySetName</Name>123            <ReferencedProperties>124                <Name>FirstName</Name>125                <Name>LastName</Name>126            </ReferencedProperties>127        </PropertySet>128        <PropertySet>129            <Name>FullSet</Name>130            <ReferencedProperties>131                <Name>FirstName</Name>132                <Name>LastName</Name>133                <Name>YearsInMS</Name>134            </ReferencedProperties>135        </PropertySet>136    </Members>137    </Type>138    <Type>139    <Name>EmployeePartTime</Name>140    <Members>141        <PropertySet>142            <Name>PropertySetName</Name>143            <ReferencedProperties>144                <Name>FirstName</Name>145                <Name>HoursPerWeek</Name>146            </ReferencedProperties>147        </PropertySet>148        <PropertySet>149            <Name>FullSet</Name>150            <ReferencedProperties>151                <Name>FirstName</Name>152                <Name>LastName</Name>153                <Name>HoursPerWeek</Name>154            </ReferencedProperties>155        </PropertySet>156    </Members>157    </Type>158</Types>159"@160 161        $XMLFile > $fileToDeleteName162        Update-TypeData -AppendPath $fileToDeleteName163 164        It "Fail to get member without any input" {165            { Get-Member -MemberType All -ErrorAction Stop } | Should -Throw -ErrorId 'NoObjectInGetMember,Microsoft.PowerShell.Commands.GetMemberCommand'166        }167 168        It 'Get the expected Properties of "Employee" object' {169            $emps = [Employee]::New("john", "smith", 5), [Employee]::New("joesph", "smith", 15), [Employee]::New("john", "smyth", 2)170            $results = $emps | Get-Member -MemberType Property171            $results.Length | Should -Be 3172            $results[0].Name | Should -BeExactly "FirstName"173            $results[1].Name | Should -BeExactly "LastName"174            $results[2].Name | Should -BeExactly "YearsInMS"175        }176 177        It 'Get the Public Methods of "Employee" object' {178            $emps = [Employee]::New("john", "smith", 5), [Employee]::New("joesph", "smith", 15), [Employee]::New("john", "smyth", 2)179            $methodList = "GetHashCode", "Equals", "ToString", "GetType"180            $results = $emps | Get-Member -MemberType Method181            $results.Length | Should -Be $methodList.Length182            $methodFound = @()183 184            for ($i = 0; $i -lt $methodList.Length; $i++) {185                for ($j = 0; $j -lt $results.Length; $j++) {186                    if ($results[$j].Name.Equals($methodList[$i])) {187                        $methodFound += $true188                    }189                }190            }191 192            for ($i = 0; $i -lt $methodList.Length; $i++) {193                $methodFound[$i] | Should -BeTrue194            }195        }196 197        It 'Get property sets defined in private members' {198            $emps = [Employee]::New("john", "smith", 5), [Employee]::New("joesph", "smith", 15), [Employee]::New("john", "smyth", 2)199            $results = $emps | Get-Member -MemberType PropertySet200            $results.Length | Should -Be 2201            $results[0].Name | Should -BeExactly "FullSet"202            $results[1].Name | Should -BeExactly "PropertySetName"203        }204    }205 206    Context "Verify Get-Member with Static Parameter" {207        It 'Get the static properties and methods of the object' {208            $obj = New-Object -TypeName System.Int32209            $results = $obj | Get-Member -Static210            $members = "MaxValue", "MinValue", "Parse", "TryParse"211            foreach ($member in $members) {212                foreach ($result in $results) {213                    if ($result.Name.Equals($member)) {214                        return $true215                    }216                }217                return $false218            }219        }220 221        It "Get the static properties and methods of int instance" {222            $results = 1 | Get-Member -Static223            $members = "MaxValue", "MinValue", "Parse", "TryParse"224            foreach ($member in $members) {225                foreach ($result in $results) {226                    if ($result.Name.Equals($member)) {227                        return $true228                    }229                }230                return $false231            }232        }233 234        It "Get the static properties and methods of int instance Wrapped" {235            $results = [pscustomobject]1 | Get-Member -Static236            $members = "MaxValue", "MinValue", "Parse", "TryParse"237            foreach ($member in $members) {238                foreach ($result in $results) {239                    if ($result.Name.Equals($member)) {240                        return $true241                    }242                }243                return $false244            }245        }246    }247 248    Context "Verify Get-Member with other parameters" {249        It 'works with View Parameter' {250            $results = [xml]'<a>some text</a>' | Get-Member -View adapted251            $results | Where-Object Name -EQ a | Should -Not -BeNullOrEmpty252            $results | Where-Object Name -EQ CreateElement | Should -Not -BeNullOrEmpty253            $results | Where-Object Name -EQ CreateNode | Should -Not -BeNullOrEmpty254        }255 256        It 'Get hidden members' {257            $results = 'abc' | Get-Member -Force258            $hiddenMembers = "psbase", "psextended", "psadapted", "pstypenames", "psobject"259            foreach ($member in $hiddenMembers) {260                foreach ($result in $results) {261                    if ($result.Name.Equals($member)) {262                        return $true263                    }264                }265                return $false266            }267        }268 269        It 'Get Set Property Accessors On PsBase' {270            $results = ('abc').psbase | Get-Member -Force get_*271            $expectedMembers = "get_Chars", "get_Length"272            foreach ($member in $expectedMembers) {273                foreach ($result in $results) {274                    if ($result.Name.Equals($member)) {275                        return $true276                    }277                }278                return $false279            }280        }281    }282}283