MegaBites-AI/Windows-powershell
0372
1# Copyright (c) Microsoft Corporation.2# Licensed under the MIT License.3Describe "Sort-Object" -Tags "CI" {4 5 It "should be able to sort object in ascending with using Property switch" {6 { Get-ChildItem -Path $PSScriptRoot -Include *.ps1 -Recurse | Sort-Object -Property Length } | Should -Not -Throw7 8 $firstLen = (Get-ChildItem -Path $PSScriptRoot -Include *.ps1 -Recurse | Sort-Object -Property Length | Select-Object -First 1).Length9 $lastLen = (Get-ChildItem -Path $PSScriptRoot -Include *.ps1 -Recurse | Sort-Object -Property Length | Select-Object -Last 1).Length10 11 $firstLen -lt $lastLen | Should -BeTrue12 13 }14 15 It "should be able to sort object in descending with using Descending switch" {16 { Get-ChildItem -Path $PSScriptRoot -Include *.ps1 -Recurse | Sort-Object -Property Length -Descending } | Should -Not -Throw17 18 $firstLen = (Get-ChildItem -Path $PSScriptRoot -Include *.ps1 -Recurse | Sort-Object -Property Length -Descending | Select-Object -First 1).Length19 $lastLen = (Get-ChildItem -Path $PSScriptRoot -Include *.ps1 -Recurse | Sort-Object -Property Length -Descending | Select-Object -Last 1).Length20 21 $firstLen -gt $lastLen | Should -BeTrue22 }23}24 25Describe "Sort-Object DRT Unit Tests" -Tags "CI" {26 It "Sort-Object with object array should work"{27 $employee1 = [pscustomobject]@{"FirstName"="Eight"; "LastName"="Eight"; "YearsInMS"=8}28 $employee2 = [pscustomobject]@{"FirstName"="Eight"; "YearsInMS"=$null}29 $employee3 = [pscustomobject]@{"FirstName"="Minus"; "LastName"="Two"; "YearsInMS"=-2}30 $employee4 = [pscustomobject]@{"FirstName"="One"; "LastName"="One"; "YearsInMS"=1}31 $employees = @($employee1,$employee2,$employee3,$employee4)32 $results = $employees | Sort-Object -Property YearsInMS33 34 $results[0].FirstName | Should -BeExactly "Minus"35 $results[0].LastName | Should -BeExactly "Two"36 $results[0].YearsInMS | Should -Be -237 38 $results[1].FirstName | Should -BeExactly "Eight"39 $results[1].YearsInMS | Should -BeNullOrEmpty40 41 $results[2].FirstName | Should -BeExactly "One"42 $results[2].LastName | Should -BeExactly "One"43 $results[2].YearsInMS | Should -Be 144 45 $results[3].FirstName | Should -BeExactly "Eight"46 $results[3].LastName | Should -BeExactly "Eight"47 $results[3].YearsInMS | Should -Be 848 }49 50 It "Sort-Object with Non Conflicting Order Entry Keys should work"{51 $employee1 = [pscustomobject]@{"FirstName"="john"; "LastName"="smith"; "YearsInMS"=5}52 $employee2 = [pscustomobject]@{"FirstName"="joesph"; "LastName"="smith"; "YearsInMS"=15}53 $employee3 = [pscustomobject]@{"FirstName"="john"; "LastName"="smyth"; "YearsInMS"=2}54 $employees = @($employee1,$employee2,$employee3)55 $ht = @{"e"="YearsInMS"; "descending"=$false; "ascending"=$true}56 $results = $employees | Sort-Object -Property $ht -Descending57 58 $results[0] | Should -Be $employees[2]59 $results[1] | Should -Be $employees[0]60 $results[2] | Should -Be $employees[1]61 }62 63 It "Sort-Object with Conflicting Order Entry Keys should work"{64 $employee1 = [pscustomobject]@{"FirstName"="john"; "LastName"="smith"; "YearsInMS"=5}65 $employee2 = [pscustomobject]@{"FirstName"="joesph"; "LastName"="smith"; "YearsInMS"=15}66 $employee3 = [pscustomobject]@{"FirstName"="john"; "LastName"="smyth"; "YearsInMS"=2}67 $employees = @($employee1,$employee2,$employee3)68 $ht = @{"e"="YearsInMS"; "descending"=$false; "ascending"=$false}69 $results = $employees | Sort-Object -Property $ht -Descending70 71 $results[0] | Should -Be $employees[1]72 $results[1] | Should -Be $employees[0]73 $results[2] | Should -Be $employees[2]74 }75 76 It "Sort-Object with One Order Entry Key should work"{77 $employee1 = [pscustomobject]@{"FirstName"="john"; "LastName"="smith"; "YearsInMS"=5}78 $employee2 = [pscustomobject]@{"FirstName"="joesph"; "LastName"="smith"; "YearsInMS"=15}79 $employee3 = [pscustomobject]@{"FirstName"="john"; "LastName"="smyth"; "YearsInMS"=2}80 $employees = @($employee1,$employee2,$employee3)81 $ht = @{"e"="YearsInMS"; "descending"=$false}82 $results = $employees | Sort-Object -Property $ht -Descending83 84 $results[0] | Should -Be $employees[2]85 $results[1] | Should -Be $employees[0]86 $results[2] | Should -Be $employees[1]87 }88 89 It "Sort-Object with HistoryInfo object should work"{90 Add-Type -TypeDefinition "public enum PipelineState{NotStarted,Running,Stopping,Stopped,Completed,Failed,Disconnected}"91 92 $historyInfo1 = [pscustomobject]@{"PipelineId"=1; "Cmdline"="cmd3"; "Status"=[PipelineState]::Completed; "StartTime" = [DateTime]::Now;"EndTime" = [DateTime]::Now.AddSeconds(5.0);}93 $historyInfo2 = [pscustomobject]@{"PipelineId"=2; "Cmdline"="cmd1"; "Status"=[PipelineState]::Completed; "StartTime" = [DateTime]::Now;"EndTime" = [DateTime]::Now.AddSeconds(5.0);}94 $historyInfo3 = [pscustomobject]@{"PipelineId"=3; "Cmdline"="cmd2"; "Status"=[PipelineState]::Completed; "StartTime" = [DateTime]::Now;"EndTime" = [DateTime]::Now.AddSeconds(5.0);}95 96 $historyInfos = @($historyInfo1,$historyInfo2,$historyInfo3)97 98 $results = $historyInfos | Sort-Object99 100 $results[0] | Should -Be $historyInfos[0]101 $results[1] | Should -Be $historyInfos[1]102 $results[2] | Should -Be $historyInfos[2]103 }104 105 It "Sort-Object with Non Existing And Null Script Property should work"{106 $n = New-Object microsoft.powershell.commands.newobjectcommand107 $d = New-Object microsoft.powershell.commands.newobjectcommand108 $d.TypeName = 'Deetype'109 $b = New-Object microsoft.powershell.commands.newobjectcommand110 $b.TypeName = 'btype'111 $a = New-Object microsoft.powershell.commands.newobjectcommand112 $a.TypeName = 'atype'113 $results = $n, $d, $b, 'b', $a | Sort-Object -proper {$_.TypeName}114 $results.Count | Should -Be 5115 $results[2] | Should -Be $a116 $results[3] | Should -Be $b117 $results[4] | Should -Be $d118 #results[0] and [1] can be any order119 }120 121 It "Sort-Object with Non Existing And Null Property should work"{122 $n = New-Object microsoft.powershell.commands.newobjectcommand123 $n.TypeName = $null124 $d = New-Object microsoft.powershell.commands.newobjectcommand125 $d.TypeName = 'Deetype'126 $b = New-Object microsoft.powershell.commands.newobjectcommand127 $b.TypeName = 'btype'128 $a = New-Object microsoft.powershell.commands.newobjectcommand129 $a.TypeName = 'atype'130 $results = $n, $d, $b, 'b', $a | Sort-Object -prop TypeName131 $results.Count | Should -Be 5132 $results[0] | Should -Be $n133 $results[1] | Should -Be $a134 $results[2] | Should -Be $b135 $results[3] | Should -Be $d136 $results[4] | Should -Be 'b'137 }138 139 It "Sort-Object with Non Case-Sensitive Unique should work"{140 $employee1 = [pscustomobject]@{"FirstName"="john"; "LastName"="smith"; "YearsInMS"=5}141 $employee2 = [pscustomobject]@{"FirstName"="joesph"; "LastName"="smith"; "YearsInMS"=15}142 $employee3 = [pscustomobject]@{"FirstName"="john"; "LastName"="smyth"; "YearsInMS"=12}143 $employees = @($employee1,$employee2,$employee3)144 $results = $employees | Sort-Object -Property "LastName" -Descending -Unique145 146 $results[0] | Should -Be $employees[2]147 $results[1] | Should -Be $employees[1]148 $results[2] | Should -BeNullOrEmpty149 }150 151 It "Sort-Object with Case-Sensitive Unique should work"{152 $employee1 = [pscustomobject]@{"FirstName"="john"; "LastName"="smith"; "YearsInMS"=5}153 $employee2 = [pscustomobject]@{"FirstName"="joesph"; "LastName"="smith"; "YearsInMS"=15}154 $employee3 = [pscustomobject]@{"FirstName"="john"; "LastName"="smyth"; "YearsInMS"=12}155 $employees = @($employee1,$employee2,$employee3)156 $results = $employees | Sort-Object -Property "LastName","FirstName" -Descending -Unique -CaseSensitive157 158 $results[0] | Should -Be $employees[2]159 $results[1] | Should -Be $employees[0]160 $results[2] | Should -Be $employees[1]161 }162 163 It "Sort-Object with Two Order Entry Keys should work"{164 $employee1 = [pscustomobject]@{"FirstName"="john"; "LastName"="smith"; "YearsInMS"=5}165 $employee2 = [pscustomobject]@{"FirstName"="joesph"; "LastName"="smith"; "YearsInMS"=15}166 $employee3 = [pscustomobject]@{"FirstName"="john"; "LastName"="smyth"; "YearsInMS"=2}167 $employees = @($employee1,$employee2,$employee3)168 $ht1 = @{"expression"="LastName"; "ascending"=$false}169 $ht2 = @{"expression"="FirstName"; "ascending"=$true}170 $results = $employees | Sort-Object -Property @($ht1,$ht2) -Descending171 172 $results[0] | Should -Be $employees[2]173 $results[1] | Should -Be $employees[1]174 $results[2] | Should -Be $employees[0]175 }176 177 It "Sort-Object with -Descending:$false and Two Order Entry Keys should work"{178 $employee1 = [pscustomobject]@{"FirstName"="john"; "LastName"="smith"; "YearsInMS"=5}179 $employee2 = [pscustomobject]@{"FirstName"="joesph"; "LastName"="smith"; "YearsInMS"=15}180 $employee3 = [pscustomobject]@{"FirstName"="john"; "LastName"="smyth"; "YearsInMS"=12}181 $employees = @($employee1,$employee2,$employee3)182 $results = $employees | Sort-Object -Property "LastName","FirstName" -Descending:$false183 184 $results[0] | Should -Be $employees[1]185 $results[1] | Should -Be $employees[0]186 $results[2] | Should -Be $employees[2]187 }188 189 It "Sort-Object with -Descending and Two Order Entry Keys should work"{190 $employee1 = [pscustomobject]@{"FirstName"="john"; "LastName"="smith"; "YearsInMS"=5}191 $employee2 = [pscustomobject]@{"FirstName"="joesph"; "LastName"="smith"; "YearsInMS"=15}192 $employee3 = [pscustomobject]@{"FirstName"="john"; "LastName"="smyth"; "YearsInMS"=12}193 $employees = @($employee1,$employee2,$employee3)194 $results = $employees | Sort-Object -Property "LastName","FirstName" -Descending195 196 $results[0] | Should -Be $employees[2]197 $results[1] | Should -Be $employees[0]198 $results[2] | Should -Be $employees[1]199 }200 201 It "Sort-Object with Two Order Entry Keys with asc=true should work"{202 $employee1 = [pscustomobject]@{"FirstName"="john"; "LastName"="smith"; "YearsInMS"=5}203 $employee2 = [pscustomobject]@{"FirstName"="joesph"; "LastName"="smith"; "YearsInMS"=15}204 $employee3 = [pscustomobject]@{"FirstName"="john"; "LastName"="smyth"; "YearsInMS"=12}205 $employees = @($employee1,$employee2,$employee3)206 $ht1 = @{"e"="FirstName"; "asc"=$true}207 $ht2 = @{"e"="LastName";}208 $results = $employees | Sort-Object -Property @($ht1,$ht2) -Descending209 210 $results[0] | Should -Be $employees[1]211 $results[1] | Should -Be $employees[2]212 $results[2] | Should -Be $employees[0]213 }214 215 It "Sort-Object with Descending No Property should work"{216 $employee1 = 1217 $employee2 = 2218 $employee3 = 3219 $employees = @($employee1,$employee2,$employee3)220 $results = $employees | Sort-Object -Descending221 222 $results[0] | Should -Be 3223 $results[1] | Should -Be 2224 $results[2] | Should -Be 1225 }226}227 228Describe 'Sort-Object Stable Unit Tests' -Tags 'CI' {229 230 Context 'Modulo stable sort' {231 232 $unsortedData = 1..20233 234 It 'Return each value in an ordered set, sorted by the value modulo 3, with items having the same result appearing in the same order' {235 $results = $unsortedData | Sort-Object {$_ % 3} -Stable236 237 $results[0] | Should -Be 3238 $results[1] | Should -Be 6239 $results[2] | Should -Be 9240 $results[3] | Should -Be 12241 $results[4] | Should -Be 15242 $results[5] | Should -Be 18243 $results[6] | Should -Be 1244 $results[7] | Should -Be 4245 $results[8] | Should -Be 7246 $results[9] | Should -Be 10247 $results[10] | Should -Be 13248 $results[11] | Should -Be 16249 $results[12] | Should -Be 19250 $results[13] | Should -Be 2251 $results[14] | Should -Be 5252 $results[15] | Should -Be 8253 $results[16] | Should -Be 11254 $results[17] | Should -Be 14255 $results[18] | Should -Be 17256 $results[19] | Should -Be 20257 }258 259 It 'Return each value in an ordered set, sorted by the value modulo 3 (descending), with items having the same result appearing in the same order' {260 $results = $unsortedData | Sort-Object {$_ % 3} -Stable -Descending261 262 $results[0] | Should -Be 2263 $results[1] | Should -Be 5264 $results[2] | Should -Be 8265 $results[3] | Should -Be 11266 $results[4] | Should -Be 14267 $results[5] | Should -Be 17268 $results[6] | Should -Be 20269 $results[7] | Should -Be 1270 $results[8] | Should -Be 4271 $results[9] | Should -Be 7272 $results[10] | Should -Be 10273 $results[11] | Should -Be 13274 $results[12] | Should -Be 16275 $results[13] | Should -Be 19276 $results[14] | Should -Be 3277 $results[15] | Should -Be 6278 $results[16] | Should -Be 9279 $results[17] | Should -Be 12280 $results[18] | Should -Be 15281 $results[19] | Should -Be 18282 }283 284 It 'Return each value in an ordered set, sorted by the value modulo 3, discarding duplicates' {285 $results = $unsortedData | Sort-Object {$_ % 3} -Stable -Unique286 287 $results[0] | Should -Be 3288 $results[1] | Should -Be 1289 $results[2] | Should -Be 2290 }291 292 It 'Return each value in an ordered set, sorted by the value modulo 3 (descending), discarding duplicates' {293 $results = $unsortedData | Sort-Object {$_ % 3} -Stable -Unique -Descending294 295 $results[0] | Should -Be 2296 $results[1] | Should -Be 1297 $results[2] | Should -Be 3298 }299 300 It "Sort-Object with Unique Stable should return proper records"{301 $Record1=[PSCustomObject]@{Key="A";Record="A1"}302 $Record2=[PSCustomObject]@{Key="A";Record="A2"}303 $Record3=[PSCustomObject]@{Key="B";Record="B1"}304 $Record4=[PSCustomObject]@{Key="B";Record="B2"}305 $Records = @($Record1,$Record2,$Record3,$Record4)306 $results = $Records | Sort-Object -Stable -Unique -Property Key307 308 $results[0] | Should -Be $Records[0]309 $results[1] | Should -Be $Records[2]310 }311 }312}313 314Describe 'Sort-Object Top and Bottom Unit Tests' -Tags 'CI' {315 316 # Helper function to compare two sort entries317 function Compare-SortEntry318 {319 param($nSortEntry, $fullSortEntry)320 if ($nSortEntry -is [System.Array]) {321 # Arrays are compared using reference equality to ensure that the original array was322 # moved to the correct position in both sorts; value equality doesn't verify this323 [object]::ReferenceEquals($nSortEntry, $fullSortEntry) | Should -BeTrue324 } else {325 $nSortEntry | Should -Be $fullSortEntry326 }327 }328 329 # Helper function that compares a full sort with an n-sort330 function Test-SortObject {331 param([array]$unsortedData, [hashtable]$baseSortParameters, [string]$nSortType, [int]$nValue)332 $nSortParameters = @{333 $nSortType = $nValue334 }335 # Sort the data336 $fullSortResults = $unsortedData | Sort-Object @baseSortParameters337 $nSortResults = $unsortedData | Sort-Object @baseSortParameters @nSortParameters338 # Verify the counts when not doing a -Unique sort339 if (-not $baseSortParameters.ContainsKey('Unique')) {340 $nSortResults.Count | Should -Be $(if ($nSortParameters[$nSortType] -gt $unsortedData.Length) {$unsortedData.Length} else {$nSortParameters[$nSortType]})341 $fullSortResults.Count | Should -Be $unsortedData.Length342 }343 # Compare the n-sort result entries with their corresponding full sort result entries344 if ($nSortType -eq 'Top') {345 $range = 0..$($nSortResults.Count - 1)346 } else {347 $range = -$nSortResults.Count..-1348 }349 foreach ($i in $range) {350 Compare-SortEntry $nSortResults[$i] $fullSortResults[$i]351 }352 }353 354 # Test cases when only the n-sort type needs to be changed355 $topBottom = @(356 @{nSortType='Top' }357 @{nSortType='Bottom'}358 )359 360 # Test cases when the n-sort type and the order type need to be changed361 $topBottomAscendingDescending = @(362 @{nSortType='Top'; orderType='ascending' }363 @{nSortType='Top'; orderType='descending'}364 @{nSortType='Bottom'; orderType='ascending' }365 @{nSortType='Bottom'; orderType='descending'}366 )367 368 Context 'Integer n-sort' {369 370 $unsortedData = 973474993,271612178,-1258909473,659770354,1829227828,-1709391247,-10835210,-1477737798,1125017828,813732193371 372 It 'Return the <nSortType> N sorted in <orderType> order' -TestCases $topBottomAscendingDescending {373 param([string]$nSortType, [string]$orderType)374 $baseSortParameters = @{Descending = $orderType -eq 'descending'}375 Test-SortObject $unsortedData $baseSortParameters $nSortType ($unsortedData.Count - 1)376 }377 378 It 'Return all sorted in <ordertype> order when -<nSortType> is too large' -TestCases $topBottomAscendingDescending {379 param([string]$nSortType, [string]$orderType)380 $baseSortParameters = @{Descending = $orderType -eq 'descending'}381 Test-SortObject $unsortedData $baseSortParameters $nSortType ($unsortedData.Count + 10)382 }383 384 }385 386 Context 'Heterogeneous n-sort' {387 388 $unsortedData = @(389 'x'390 Get-Alias where391 'c'392 ([pscustomobject]@{}) | Add-Member -Name ToString -Force -MemberType ScriptMethod -Value {$null} -PassThru # Use this syntax to get $null from a default sort (uses ToString)393 42394 Get-Alias foreach395 ,@('a','b','c') # Use this syntax to pass an array with a few values to sort-object (also useful when testing sorts with unexpected data)396 [pscustomobject]@{Name='NotAnAlias'}397 [pscustomobject]@{Name=$null;Definition='Custom'}398 'z'399 ,@($null) # Use this syntax to pass an array with a single $null value to sort-object (also useful when testing sorts with unexpected data)400 )401 402 It 'Return the <nSortType> N sorted in <orderType> order' -TestCases $topBottomAscendingDescending {403 param([string]$nSortType, [string]$orderType)404 $baseSortParameters = @{Descending = $orderType -eq 'descending'}405 Test-SortObject $unsortedData $baseSortParameters $nSortType ($unsortedData.Count - 1)406 }407 408 It 'Return the <nSortType> N sorted by property in <orderType> order' -TestCases $topBottomAscendingDescending {409 param([string]$nSortType, [string]$orderType)410 $baseSortParameters = @{Property = 'Name'}411 if ($orderType -eq 'Descending') {$baseSortParameters['Descending'] = $true}412 Test-SortObject $unsortedData $baseSortParameters $nSortType ($unsortedData.Count - 1)413 }414 415 }416 417 Context 'Homogeneous n-sort' {418 419 $unsortedData = @(420 [pscustomobject]@{PSTypeName='Employee';FirstName='Dwayne';LastName='Smith' ;YearsInMS=8 }421 [pscustomobject]@{PSTypeName='Employee';FirstName='Lucy' ; ;YearsInMS=$null}422 [pscustomobject]@{PSTypeName='Employee';FirstName='Jack' ;LastName='Jones' ;YearsInMS=-2 }423 [pscustomobject]@{PSTypeName='Employee';FirstName='Sylvie';LastName='Landry';YearsInMS=1 }424 [pscustomobject]@{PSTypeName='Employee';FirstName='Jack' ;LastName='Frank' ;YearsInMS=5 }425 [pscustomobject]@{PSTypeName='Employee';FirstName='John' ;LastName='smith' ;YearsInMS=6 }426 [pscustomobject]@{PSTypeName='Employee';FirstName='Joseph';LastName='Smith' ;YearsInMS=15 }427 [pscustomobject]@{PSTypeName='Employee';FirstName='John' ;LastName='Smyth' ;YearsInMS=12 }428 )429 430 It 'Return the <nSortType> N sorted by property in <orderType> order' -TestCases $topBottomAscendingDescending {431 param([string]$nSortType, [string]$orderType)432 $baseSortParameters = @{Property = 'YearsInMS'}433 if ($orderType -eq 'Descending') {$baseSortParameters['Descending'] = $true}434 Test-SortObject $unsortedData $baseSortParameters $nSortType ($unsortedData.Count - 1)435 }436 437 It 'Return the <nSortType> N sorted by property in <orderType> order (unique, case-insensitive)' -TestCases $topBottomAscendingDescending {438 param([string]$nSortType, [string]$orderType)439 $baseSortParameters = @{Property='LastName';Unique=$true}440 if ($orderType -eq 'Descending') {$baseSortParameters['Descending'] = $true}441 Test-SortObject $unsortedData $baseSortParameters $nSortType ($unsortedData.Count - 1)442 }443 444 It 'Return the <nSortType> N sorted by property in <orderType> order (unique, case-sensitive)' -TestCases $topBottomAscendingDescending {445 param([string]$nSortType, [string]$orderType)446 $baseSortParameters = @{Property='LastName';CaseSensitive=$true;Unique=$true}447 if ($orderType -eq 'Descending') {$baseSortParameters['Descending'] = $true}448 Test-SortObject $unsortedData $baseSortParameters $nSortType ($unsortedData.Count - 1)449 }450 451 It 'Return the <nSortType> N sorted by property with an order entry key' -TestCases $topBottom {452 param([string]$nSortType)453 $baseSortParameters = @{Descending=$true;Property=@{Expression='YearsInMS'; Descending=$false}}454 Test-SortObject $unsortedData $baseSortParameters $nSortType ($unsortedData.Count - 1)455 }456 457 It 'Return the <nSortType> N sorted by property with multiple non-conflicting order entry keys' -TestCases $topBottom {458 param([string]$nSortType)459 $baseSortParameters = @{Descending=$true;Property=@{Expression='YearsInMS'; Descending=$false; Ascending=$true}}460 Test-SortObject $unsortedData $baseSortParameters $nSortType ($unsortedData.Count - 1)461 }462 463 It 'Return the <nSortType> N sorted by property with multiple conflicting order entry keys' -TestCases $topBottom {464 param([string]$nSortType)465 $baseSortParameters = @{Descending=$true;Property=@{Expression='YearsInMS'; Descending=$false; Ascending=$false}}466 Test-SortObject $unsortedData $baseSortParameters $nSortType ($unsortedData.Count - 1)467 }468 469 It 'Return the <nSortType> N sorted by multiple properties with different order entry keys' -TestCases $topBottom {470 param([string]$nSortType)471 $baseSortParameters = @{Property=@{Expression='LastName';Ascending=$false},@{Expression='FirstName';Ascending=$true}}472 Test-SortObject $unsortedData $baseSortParameters $nSortType ($unsortedData.Count - 1)473 }474 475 It 'Return the <nSortType> N sorted by two properties in descending order' -TestCases $topBottom {476 param([string]$nSortType)477 $baseSortParameters = @{Property='LastName','FirstName';Descending=$true}478 Test-SortObject $unsortedData $baseSortParameters $nSortType ($unsortedData.Count - 1)479 }480 481 It 'Return the <nSortType> N sorted by two properties with -Descending:$false' -TestCases $topBottom {482 param([string]$nSortType)483 $baseSortParameters = @{Property='LastName','FirstName';Descending=$false}484 Test-SortObject $unsortedData $baseSortParameters $nSortType ($unsortedData.Count - 1)485 }486 487 It 'Return the <nSortType> N sorted by two properties with mixed sort order' -TestCases $topBottom {488 param([string]$nSortType)489 $baseSortParameters = @{Property=@{Expression='FirstName';Ascending=$true},@{Expression='LastName'};Descending=$true}490 Test-SortObject $unsortedData $baseSortParameters $nSortType ($unsortedData.Count - 1)491 }492 493 }494 495 Context 'N-sort of objects that do not define ToString' {496 497 Add-Type -TypeDefinition 'public enum PipelineState{NotStarted,Running,Stopping,Stopped,Completed,Failed,Disconnected}'498 $unsortedData = @(499 [pscustomobject]@{PipelineId=1;Cmdline='cmd3';Status=[PipelineState]::Completed;StartTime=[DateTime]::Now;EndTime=[DateTime]::Now.AddSeconds(5.0)}500 [pscustomobject]@{PipelineId=2;Cmdline='cmd1';Status=[PipelineState]::Completed;StartTime=[DateTime]::Now;EndTime=[DateTime]::Now.AddSeconds(5.0)}501 [pscustomobject]@{PipelineId=3;Cmdline='cmd2';Status=[PipelineState]::Completed;StartTime=[DateTime]::Now;EndTime=[DateTime]::Now.AddSeconds(5.0)}502 )503 504 It 'Return the <nSortType> N sorted in <orderType> order' -TestCases $topBottomAscendingDescending {505 param([string]$nSortType, [string]$orderType)506 $baseSortParameters = @{Descending = $orderType -eq 'descending'}507 Test-SortObject $unsortedData $baseSortParameters $nSortType ($unsortedData.Count - 1)508 }509 510 }511 512 Context 'N-sort of objects with some null property values' {513 514 $item0 = New-Object -TypeName Microsoft.PowerShell.Commands.NewObjectCommand515 $item1 = New-Object -TypeName Microsoft.PowerShell.Commands.NewObjectCommand516 $item1.TypeName = 'DeeType'517 $item2 = New-Object -TypeName Microsoft.PowerShell.Commands.NewObjectCommand518 $item2.TypeName = 'B-Type'519 $item3 = New-Object -TypeName Microsoft.PowerShell.Commands.NewObjectCommand520 $item3.TypeName = 'A-Type'521 $unsortedData = @($item0,$item1,$item2,'b',$item3)522 523 It 'Return the <nSortType> N objects by property in <orderType> order' -TestCases $topBottomAscendingDescending {524 param([string]$nSortType, [string]$orderType)525 $baseSortParameters = @{Property='TypeName'}526 if ($orderType -eq 'Descending') {$baseSortParameters['Descending'] = $true}527 Test-SortObject $unsortedData $baseSortParameters $nSortType ($unsortedData.Count - 1)528 }529 530 It 'Return the <nSortType> N by script property in <orderType> order' -TestCases $topBottomAscendingDescending {531 param([string]$nSortType, [string]$orderType)532 $baseSortParameters = @{Property={$_.TypeName}}533 if ($orderType -eq 'Descending') {$baseSortParameters['Descending'] = $true}534 Test-SortObject $unsortedData $baseSortParameters $nSortType ($unsortedData.Count - 1)535 }536 537 }538 539}540 