Team Ai
Datasetpublic

MegaBites-AI/Windows-powershell

sourceHugging Facemitupdated 6mo agoView on Hugging Face
0likes308downloads
Measure-Object.Tests.ps1459 linesDownload Raw Back to Microsoft.PowerShell.Utility
1# Copyright (c) Microsoft Corporation.2# Licensed under the MIT License.3Describe "Measure-Object" -Tags "CI" {4    BeforeAll {5        $testObject = 1,3,46        $testObject2 = 1..1007    }8 9    It "Should be able to be called without error" {10        { Measure-Object | Out-Null } | Should -Not -Throw11    }12 13    It "Should be able to call on piped input" {14        { $testObject | Measure-Object } | Should -Not -Throw15    }16 17    It "Should be able to count the number of objects input to it" {18        $($testObject | Measure-Object).Count | Should -Be $testObject.Length19    }20 21    It "Should calculate Standard Deviation" {22        $actual = ($testObject | Measure-Object -StandardDeviation)23        # We check this way since .StandardDeviation returns a double value24        # 1.52752523165195 was calculated outside powershell using formula from25        # http://mathworld.wolfram.com/StandardDeviation.html26        [Math]::abs($actual.StandardDeviation - 1.52752523165195) | Should -BeLessThan .0000000000000127    }28 29 30    It "Should calculate Standard Deviation" {31        $actual = ($testObject2 | Measure-Object -StandardDeviation)32        # We check this way since .StandardDeviation returns a double value33        # 29.011491975882 was calculated outside powershell using formula from34        # http://mathworld.wolfram.com/StandardDeviation.html35        [Math]::abs($actual.StandardDeviation - 29.011491975882) | Should -BeLessThan .000000000000136    }37 38    It "Should calculate Standard Deviation with -Sum" {39        $actual = ($testObject | Measure-Object -Sum -StandardDeviation)40        # We check this way since .StandardDeviation returns a double value41        $actual.Sum | Should -Be 842        # 1.52752523165195 was calculated outside powershell using formula from43        # http://mathworld.wolfram.com/StandardDeviation.html44        [Math]::abs($actual.StandardDeviation - 1.52752523165195) | Should -BeLessThan .0000000000000145    }46 47    It "Should calculate Standard Deviation with -Average" {48        $actual = ($testObject | Measure-Object -Average -StandardDeviation)49        # We check this way since .StandardDeviation returns a double value50        [Math]::abs($actual.Average - 2.66666666666667) | Should -BeLessThan .0000000000000151        # 1.52752523165195 was calculated outside powershell using formula from52        # http://mathworld.wolfram.com/StandardDeviation.html53        [Math]::abs($actual.StandardDeviation - 1.52752523165195) | Should -BeLessThan .0000000000000154    }55 56    It "Should calculate Standard Deviation with -Sum -Average" {57        $actual = ($testObject2 | Measure-Object -Sum -Average -StandardDeviation)58        # We check this way since .StandardDeviation returns a double value59        $actual.Sum | Should -Be 505060        $actual.Average | Should -Be 50.561        # 29.011491975882 was calculated outside powershell using formula from62        # http://mathworld.wolfram.com/StandardDeviation.html63        [Math]::abs($actual.StandardDeviation - 29.011491975882) | Should -BeLessThan .000000000000164    }65 66    It "Should be able to count using the Property switch" {67        $items = @(68            [pscustomobject]@{ X = 'a'; Y = 'b' }69            [pscustomobject]@{ X = 'c' }70            [pscustomobject]@{ X = 'e'; Y = 'f' }71        )72 73        ($items | Measure-Object -Property Y).Count | Should -Be 274    }75 76    It "Should be able to use wildcards for the Property argument" {77        $data = [pscustomobject]@{ A1 = 1; A2 = 2; C3 = 3 },78                [pscustomobject]@{ A1 = 1; A2 = 2; A3 = 3 }79        $actual = $data | Measure-Object -Property A* -Sum80        $actual.Count       | Should -Be 381        $actual[0].Property | Should -Be A182        $actual[0].Sum      | Should -Be 283        $actual[0].Count    | Should -Be 284        $actual[1].Property | Should -Be A285        $actual[1].Sum      | Should -Be 486        $actual[1].Count    | Should -Be 287        $actual[2].Property | Should -Be A388        $actual[2].Sum      | Should -Be 389        $actual[2].Count    | Should -Be 190    }91 92    Context "Numeric tests" {93        It "Should be able to sum" {94            $actual   = $testObject | Measure-Object -Sum95            $expected = 096 97            foreach ( $obj in $testObject )98            {99                $expected += $obj100            }101 102            $actual.Sum | Should -Be $expected103        }104 105        It "Should be able to average" {106            $actual   = $testObject | Measure-Object -Average107            $expected = 0108 109            foreach ( $obj in $testObject )110            {111                $expected += $obj112            }113 114            $expected /= $testObject.length115 116            $actual.Average | Should -Be $expected117        }118 119        It "Should be able to return a minimum" {120            $actual   = $testObject | Measure-Object -Minimum121            $expected = $testObject[0]122 123            for ($i=0; $i -lt $testObject.length; $i++)124            {125                if ( $testObject[$i] -lt $expected )126                {127                    $expected = $testObject[$i]128                }129            }130 131            $actual.Minimum | Should -Be $expected132        }133 134        It "Should be able to return a minimum when multiple objects are the minimum" {135            $testMinimum = 1,1,2,4136            $actual      = $testMinimum | Measure-Object -Minimum137            $expected    = $testMinimum[0]138 139            for ($i=1; $i -lt $testMinimum.length; $i++)140            {141                if ( $testMinimum[$i] -lt $expected )142                {143                    $expected = $testMinimum[$i]144                }145            }146 147            $actual.Minimum | Should -Be $expected148        }149 150        It "Should be able to return a maximum" {151            $actual   = $testObject | Measure-Object -Maximum152            $expected = $testObject[0]153 154            for ($i=1; $i -lt $testObject.length; $i++)155            {156                if ( $testObject[$i] -gt $expected )157                {158                    $expected = $testObject[$i]159                }160            }161 162            $actual.Maximum | Should -Be $expected163        }164 165        It "Should be able to return a maximum when multiple objects are the maximum" {166            $testMaximum = 1,3,5,5167            $actual      = $testMaximum | Measure-Object -Maximum168            $expected    = $testMaximum[0]169 170            for ($i=1; $i -lt $testMaximum.length; $i++)171            {172                if ( $testMaximum[$i] -gt $expected )173                {174                    $expected = $testMaximum[$i]175                }176            }177 178            $actual.Maximum | Should -Be $expected179        }180 181        It "Should be able to return all the statitics for given values" {182            $result = 1..10  | Measure-Object -AllStats183            $result.Count    | Should -Be 10184            $result.Average  | Should -Be 5.5185            $result.Sum      | Should -Be 55186            $result.Minimum  | Should -Be 1187            $result.Maximum  | Should -Be 10188            ($result.StandardDeviation).ToString()  | Should -Be '3.0276503540974917'189        }190    }191 192    Context "Empty folder tests" {193        BeforeAll {194            $repoPath = Join-Path -Path $TESTDRIVE -ChildPath "my_folder"195            $testEmptyFolder = New-Item $repoPath -ItemType "directory"196            $propertyName = "Length"197        }198 199        AfterAll {200            Set-StrictMode -off201        }202 203        It "Should not throw an invalid property error when not in StrictMode" {204            { Set-StrictMode -off; Get-Item $testEmptyFolder | Measure-Object $propertyName -ErrorAction Stop -sum } | Should -Not -Throw205        }206 207        It "Should throw an invalid property error when in StrictMode" {208            { Set-StrictMode -version 3.0; Get-Item $testEmptyFolder | Measure-Object $propertyName -ErrorAction Stop -sum } | Should -Throw -ErrorId 'GenericMeasurePropertyNotFound,Microsoft.PowerShell.Commands.MeasureObjectCommand'209        }210    }211 212    Context "String tests" {213        BeforeAll {214            $nl = [Environment]::NewLine215            $testString = "HAD I the heavens' embroidered cloths,$nl Enwrought with golden and silver light,$nl The blue and the dim and the dark cloths$nl Of night and light and the half light,$nl I would spread the cloths under your feet:$nl But I, being poor, have only my dreams;$nl I have spread my dreams under your feet;$nl Tread softly because you tread on my dreams."216        }217 218        It "Should be able to count the number of words in a string" {219            $expectedLength = $testString.Replace($nl,"").Split().length220            $actualLength   = $testString | Measure-Object -Word221 222            $actualLength.Words | Should -Be $expectedLength223        }224 225        It "Should be able to count the number of characters in a string" {226            $expectedLength = $testString.length227            $actualLength   = $testString | Measure-Object -Character228 229            $actualLength.Characters | Should -Be $expectedLength230        }231 232        It "Should be able to count the number of lines in a string" {233            $expectedLength = $testString.Split($nl, [System.StringSplitOptions]::RemoveEmptyEntries).length234            $actualLength   = $testString | Measure-Object -Line235 236            $actualLength.Lines | Should -Be $expectedLength237        }238    }239}240 241Describe "Measure-Object DRT basic functionality" -Tags "CI" {242    BeforeAll {243        if(-not ([System.Management.Automation.PSTypeName]'TestMeasureGeneric').Type)244        {245            Add-Type -TypeDefinition @"246    [System.Flags]247    public enum TestMeasureGeneric : uint248    {249        TestSum = 1,250        TestAverage = 2,251        TestMax = 4,252        TestMin = 8253    }254"@255        }256 257        if(-not ([System.Management.Automation.PSTypeName]'TestMeasureText').Type)258        {259            Add-Type -TypeDefinition @"260    [System.Flags]261    public enum TestMeasureText : uint262    {263        TestIgnoreWS = 1,264        TestCharacter = 2,265        TestWord = 4,266        TestLine = 8267    }268"@269        }270 271        $employees = [pscustomobject]@{"FirstName"="joseph"; "LastName"="smith"; "YearsInMS"=15},272                            [pscustomobject]@{"FirstName"="paul"; "LastName"="smith"; "YearsInMS"=15},273                            [pscustomobject]@{"FirstName"="mary jo"; "LastName"="soe"; "YearsInMS"=5},274                            [pscustomobject]@{"FirstName"="edmund`todd `n"; "LastName"="bush"; "YearsInMS"=9}275    }276 277    It "Measure-Object with Generic enum value options combination should work"{278        $flags = [TestMeasureGeneric]0279        $property = "FirstName"280        $testSum = ($flags -band [TestMeasureGeneric]::TestSum) -gt 0281        $testAverage = ($flags -band [TestMeasureGeneric]::TestAverage) -gt 0282        $testMax = ($flags -band [TestMeasureGeneric]::TestMax) -gt 0283        $testMin = ($flags -band [TestMeasureGeneric]::TestMin) -gt 0284        $result = $employees | Measure-Object -Sum:$testSum -Average:$testAverage -Max:$testMax -Min:$testMin -Prop $property285        $result.Count   | Should -Be 4286        $result.Sum     | Should -BeNullOrEmpty287        $result.Average | Should -BeNullOrEmpty288        $result.Max     | Should -BeNullOrEmpty289        $result.Min     | Should -BeNullOrEmpty290        for ($i = 1; $i -lt 8 * 2; $i++)291        {292            $flags = [TestMeasureGeneric]$i293            $property = "YearsInMS"294            $testSum = ($flags -band [TestMeasureGeneric]::TestSum) -gt 0295            $testAverage = ($flags -band [TestMeasureGeneric]::TestAverage) -gt 0296            $testMax = ($flags -band [TestMeasureGeneric]::TestMax) -gt 0297            $testMin = ($flags -band [TestMeasureGeneric]::TestMin) -gt 0298            $result = $employees | Measure-Object -Sum:$testSum -Average:$testAverage -Max:$testMax -Min:$testMin -Prop $property299            $result.Count | Should -Be 4300            if($testSum)301            {302                $result.Sum | Should -Be 44303            }304            else305            {306                $result.Sum | Should -BeNullOrEmpty307            }308 309            if($testAverage)310            {311                $result.Average | Should -Be 11312            }313            else314            {315                $result.Average | Should -BeNullOrEmpty316            }317 318            if($testMax)319            {320                $result.Maximum | Should -Be 15321            }322            else323            {324                $result.Maximum | Should -BeNullOrEmpty325            }326 327            if($testMin)328            {329                $result.Minimum | Should -Be 5330            }331            else332            {333                $result.Minimum | Should -BeNullOrEmpty334            }335        }336    }337 338    It "Measure-Object with Text combination should work"{339        for ($i = 1; $i -lt 8 * 2; $i++)340        {341            $flags = [TestMeasureText]$i342            $property = "FirstName"343            $testIgnoreWS = ($flags -band [TestMeasureText]::TestIgnoreWS) -gt 0344            $testCharacter = ($flags -band [TestMeasureText]::TestCharacter) -gt 0345            $testWord = ($flags -band [TestMeasureText]::TestWord) -gt 0346            $testLine = ($flags -band [TestMeasureText]::TestLine) -gt 0347            $result = $employees | Measure-Object -IgnoreWhiteSpace:$testIgnoreWS -Character:$testCharacter -Word:$testWord -Line:$testLine -Prop $property348 349            if($testCharacter)350            {351                if($testIgnoreWS)352                {353                    $result.Characters | Should -Be 25354                }355                else356                {357                    $result.Characters | Should -Be 29358                }359            }360            else361            {362                $result.Characters | Should -BeNullOrEmpty363            }364 365            if($testWord)366            {367                $result.Words | Should -Be 6368            }369            else370            {371                $result.Words | Should -BeNullOrEmpty372            }373 374            if($testLine)375            {376                $result.Lines | Should -Be 4377            }378            else379            {380                $result.Lines | Should -BeNullOrEmpty381            }382        }383    }384}385 386# Since PSPropertyExpression is now a public type, it can be tested387# directly, independent of the Measure-Object cmdlet388Describe "Directly test the PSPropertyExpression type" -Tags "CI" {389    # this function is used to test the use of PSPropertyExpression390    # as a parameter in script391    function Test-PSPropertyExpression {392        [CmdletBinding()]393        param (394            [Parameter(Mandatory,Position=0)]395            [PSPropertyExpression]396                $pe,397            [Parameter(ValueFromPipeline)]398                $InputObject399        )400        begin { $sum = 0}401        process { $sum += $pe.GetValues($InputObject).result }402        end { $sum }403    }404 405    It "Test-PropertyExpression function with a wildcard property expression should sum numbers" {406        $result = (1..10).ForEach{@{value = $_}} | Test-PSPropertyExpression val*407        $result | Should -Be 55408    }409 410    It "Test-PropertyExpression function with a scriptblock property expression should sum numbers" {411        $result = 1..10 | Test-PSPropertyExpression {$_}412        $result | Should -Be 55413    }414 415    It "Test-PropertyExpression function with a scriptblock property expression should be able to transform input" {416        # Count the number of 'e's in the words.417        $result = "one", "two", "three", "four", "five" | Test-PSPropertyExpression {($_.ToCharArray() -match 'e').Count}418        $result | Should -Be 4419    }420 421    It "Measure-Object with multiple lines should work"{422        $result = "123`n4" | Measure-Object -Line423        $result.Lines | Should -Be 2424    }425 426    It "Measure-Object with ScriptBlock properties should work" {427        $result = 1..10 | Measure-Object -Sum -Average -Minimum -Maximum -Property {$_ * 10}428        $result.Count    | Should -Be 10429        $result.Average  | Should -Be 55430        $result.Sum      | Should -Be 550431        $result.Minimum  | Should -Be 10432        $result.Maximum  | Should -Be 100433        $result.Property | Should -Be '$_ * 10'434    }435 436    It "Measure-Object with ScriptBlock properties should work with -word" {437        $result = "a,b,c", "d,e" | Measure-Object -Word  {$_ -split ','}438        $result.Words | Should -Be 5439    }440 441    It "Measure-Object ScriptBlock properties should be able to transform input" {442        $map = @{ one = 1; two = 2; three = 3 }443        $result = "one", "two", "three" | Measure-Object -Sum {$map[$_]}444        $result.Sum | Should -Be 6445    }446 447    It "Measure-Object should handle hashtables as objects" {448        $htables = @{foo = 1}, @{foo = 3}, @{foo = 10}449        $result = $htables | Measure-Object -Sum fo*450        $result.Sum | Should -Be 14451    }452 453    It "Measure-Object should handle hashtables as objects with ScriptBlock properties" {454        $htables = @{foo = 1}, @{foo = 3}, @{foo = 10}455        $result = $htables | Measure-Object -Sum {$_.foo * 10 }456        $result.Sum | Should -Be 140457    }458}459