Team Ai
Datasetpublic

MegaBites-AI/Windows-powershell

sourceHugging Facemitupdated 6mo agoView on Hugging Face
0likes372downloads
Group-Object.Tests.ps1202 linesDownload Raw Back to Microsoft.PowerShell.Utility
1# Copyright (c) Microsoft Corporation.2# Licensed under the MIT License.3Describe "Group-Object DRT Unit Tests" -Tags "CI" {4    It "Test for CaseSensitive switch" {5        $testObject = 'aA', 'aA', 'AA', 'AA'6        $results = $testObject | Group-Object -CaseSensitive7        $results.Count | Should -Be 28        $results.Name.Count | Should -Be 29        $results.Group.Count | Should -Be 410        $results.Name | Should -Be aA,AA11        $results.Group | Should -Be aA,aA,AA,AA12        ,$results | Should -BeOfType System.Array13    }14}15 16Describe "Group-Object" -Tags "CI" {17 18    BeforeAll {19        $testObject = Get-ChildItem20    }21 22    It "Should be called using an object as piped without error with no switches" {23        {$testObject | Group-Object } | Should -Not -Throw24    }25 26    It "Should be called using the InputObject without error with no other switches" {27        { Group-Object -InputObject $testObject } | Should -Not -Throw28    }29 30    It "Should return three columns- count, name, and group" {31        $actual = Group-Object -InputObject $testObject32 33        $actual.Count       | Should -BeGreaterThan 034        $actual.Name.Count  | Should -BeGreaterThan 035        $actual.Group.Count | Should -BeGreaterThan 036    }37 38    It "Should use the group alias" {39        { Group-Object -InputObject $testObject } | Should -Not -Throw40    }41 42    It "Should create a collection when the inputObject parameter is used" {43        $actualParam = Group-Object -InputObject $testObject44        $actualParam.Group.Gettype().Name | Should -Be 'Collection`1'45    }46 47    It "Should return object of 'GroupInfo' type" {48        $actualParam = Group-Object -InputObject $testObject49        $actualParam | Should -BeOfType Microsoft.PowerShell.Commands.GroupInfo50    }51 52    It "Should output an array when piped input is used" {53        $actual = $testObject | Group-Object54 55        ,$actual | Should -BeOfType System.Array56    }57 58    It "Should have the same output between the group alias and the group-object cmdlet" {59        $actualAlias = Group-Object -InputObject $testObject60        $actualCmdlet = Group-Object -InputObject $testObject61 62        $actualAlias.Name[0] | Should -Be $actualCmdlet.Name[0]63        $actualAlias.Group[0] | Should -Be $actualCmdlet.Group[0]64    }65 66    It "Should be able to use the property switch without error" {67        { $testObject | Group-Object -Property Attributes } | Should -Not -Throw68 69        $actual = $testObject | Group-Object -Property Attributes70 71        $actual.Group.Count | Should -BeGreaterThan 072    }73 74    It "Should be able to use the property switch on multiple properties without error" {75        { $testObject | Group-Object -Property Attributes, Length }76 77        $actual = $testObject | Group-Object -Property Attributes, Length78 79        $actual.Group.Count | Should -BeGreaterThan 080    }81 82    It "Should be able to omit members of a group using the NoElement switch without error" {83        { $testObject | Group-Object -NoElement } | Should -Not -Throw84 85        ($testObject | Group-Object -NoElement).Group | Should -BeNullOrEmpty86    }87 88    It "Should be able to output a hashtable datatype" {89        $actual = $testObject | Group-Object -AsHashTable90 91        $actual | Should -Not -BeNullOrEmpty92        $actual | Should -BeOfType System.Collections.Hashtable93    }94 95    It "Should be able to access when output as hash table" {96        $actual = $testObject | Group-Object -AsHashTable97 98        $actual.Keys | Should -Not -BeNullOrEmpty99    }100 101    It "Should throw when attempting to use AsString without AsHashTable" {102        { $testObject | Group-Object -AsString } | Should -Throw103    }104 105    It "Should not throw error when using AsString when the AsHashTable was added" {106        { $testObject | Group-Object -AsHashTable -AsString } | Should -Not -Throw107    }108 109    It "Should be able to retrieve objects by key when using -AsHashTable without -AsString" {110        $testObject = [pscustomobject] @{a="one"; b=2}, [pscustomobject] @{a="two"; b=10}111        $result = $testObject | Group-Object -AsHashTable -Property a112        $result.one.b | Should -Be 2113        $result["two"].b | Should -Be 10114    }115 116    It "User's scenario should work (see issue #6933 for link to stackoverflow question)" {117        # Sort numbers into two groups even succeeded, odd failed.118        $result = 1..9 | ForEach-Object {[PSCustomObject]@{ErrorMessage = if ($_ % 2) {'SomeError'} else {''}}} |119            Group-Object -Property {if ($_.ErrorMessage) {'Failed'} else {'Successful'}} -AsHashTable120 121        $result['Failed'].ErrorMessage.Count | Should -Be 5122        $result['Failed'].ErrorMessage[0] | Should -Be 'SomeError'123        $result['Successful'].ErrorMessage.Count | Should -Be 4124        $result['Successful'].ErrorMessage[0] | Should -Be ''125    }126 127    It "Should understand empty NoteProperty" {128        $result = "dummy" | Select-Object -Property @{Name = 'X'; Expression = {}} | Group-Object X129        $result.Count | Should -Be 1130        $result[0].Name | Should -Be ""131        $result[0].Group | Should -Be '@{X=}'132    }133 134    It "Should handle format-like strings with curly braces like normal strings" {135        $result = '{', '}', '{0}' | Group-Object136        $result.Count | Should -Be 3137        $result[0].Name | Should -BeExactly '{'138        $result[1].Name | Should -BeExactly '{0}'139        $result[2].Name | Should -BeExactly '}'140    }141}142 143Describe "Check 'Culture' parameter in order object cmdlets (Group-Object, Sort-Object, Compare-Object)" -Tags "CI" {144 145    BeforeAll {146 147        $testObject = Get-ChildItem148 149    }150 151    It "Should accept a culture by name" {152 153        if ( (Get-Culture).Name -eq "ru-Ru" ) {154            $testCulture = "ru-UA"155        }156        else {157            $testCulture = "ru-RU"158        }159 160        {$testObject | Group-Object -Culture $testCulture } | Should -Not -Throw161    }162 163    It "Should accept a culture by hex string LCID" {164 165        if ( (Get-Culture).LCID -eq 1049 ) {166            $testCulture = "0x1000"167        }168        else {169            $testCulture = "0x419"170        }171 172        {$testObject | Group-Object -Culture $testCulture } | Should -Not -Throw173    }174 175    It "Should accept a culture by int string LCID" {176 177        if ( (Get-Culture).LCID -eq 1049 ) {178            $testCulture = "4096"179        }180        else {181            $testCulture = "1049"182        }183 184        { $testObject | Group-Object -Culture $testCulture } | Should -Not -Throw185    }186 187    It 'should not throw a key duplication error with -CaseSensitive -AsHashtable' {188        $capitonyms = @(189            [PSCustomObject]@{190                Capitonym = 'Bill'191            }192            [PSCustomObject]@{193                Capitonym = 'bill'194            }195        )196 197        $Result = $capitonyms | Group-Object -Property Capitonym -AsHashTable -CaseSensitive198        $Result | Should -BeOfType HashTable199        $Result.Keys | Should -BeIn @( 'Bill', 'bill' )200    }201}202