forked from PowerShell/PowerShell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSort-Object.Tests.ps1
More file actions
224 lines (185 loc) · 10.2 KB
/
Copy pathSort-Object.Tests.ps1
File metadata and controls
224 lines (185 loc) · 10.2 KB
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
Describe "Sort-Object" -Tags "CI" {
It "should be able to sort object in ascending with using Property switch" {
{ Get-ChildItem -Path $PSScriptRoot -Include *.ps1 -Recurse | Sort-Object -Property Length } | Should Not Throw
$firstLen = (Get-ChildItem -Path $PSScriptRoot -Include *.ps1 -Recurse | Sort-Object -Property Length | Select-Object -First 1).Length
$lastLen = (Get-ChildItem -Path $PSScriptRoot -Include *.ps1 -Recurse | Sort-Object -Property Length | Select-Object -Last 1).Length
$firstLen -lt $lastLen | Should be $true
}
It "should be able to sort object in descending with using Descending switch" {
{ Get-ChildItem -Path $PSScriptRoot -Include *.ps1 -Recurse | Sort-Object -Property Length -Descending } | Should Not Throw
$firstLen = (Get-ChildItem -Path $PSScriptRoot -Include *.ps1 -Recurse | Sort-Object -Property Length -Descending | Select-Object -First 1).Length
$lastLen = (Get-ChildItem -Path $PSScriptRoot -Include *.ps1 -Recurse | Sort-Object -Property Length -Descending | Select-Object -Last 1).Length
$firstLen -gt $lastLen | Should be $true
}
}
Describe "Sort-Object DRT Unit Tests" -Tags "CI" {
It "Sort-Object with object array should work"{
$employee1 = [pscustomobject]@{"FirstName"="Eight"; "LastName"="Eight"; "YearsInMS"=8}
$employee2 = [pscustomobject]@{"FirstName"="Eight"; "YearsInMS"=$null}
$employee3 = [pscustomobject]@{"FirstName"="Minus"; "LastName"="Two"; "YearsInMS"=-2}
$employee4 = [pscustomobject]@{"FirstName"="One"; "LastName"="One"; "YearsInMS"=1}
$employees = @($employee1,$employee2,$employee3,$employee4)
$results = $employees | Sort-Object -Property YearsInMS
$results[0].FirstName | Should Be "Minus"
$results[0].LastName | Should Be "Two"
$results[0].YearsInMS | Should Be -2
$results[1].FirstName | Should Be "Eight"
$results[1].YearsInMS | Should BeNullOrEmpty
$results[2].FirstName | Should Be "One"
$results[2].LastName | Should Be "One"
$results[2].YearsInMS | Should Be 1
$results[3].FirstName | Should Be "Eight"
$results[3].LastName | Should Be "Eight"
$results[3].YearsInMS | Should Be 8
}
It "Sort-Object with Non Conflicting Order Entry Keys should work"{
$employee1 = [pscustomobject]@{"FirstName"="john"; "LastName"="smith"; "YearsInMS"=5}
$employee2 = [pscustomobject]@{"FirstName"="joesph"; "LastName"="smith"; "YearsInMS"=15}
$employee3 = [pscustomobject]@{"FirstName"="john"; "LastName"="smyth"; "YearsInMS"=2}
$employees = @($employee1,$employee2,$employee3)
$ht = @{"e"="YearsInMS"; "descending"=$false; "ascending"=$true}
$results = $employees | Sort-Object -Property $ht -Descending
$results[0] | Should Be $employees[2]
$results[1] | Should Be $employees[0]
$results[2] | Should Be $employees[1]
}
It "Sort-Object with Conflicting Order Entry Keys should work"{
$employee1 = [pscustomobject]@{"FirstName"="john"; "LastName"="smith"; "YearsInMS"=5}
$employee2 = [pscustomobject]@{"FirstName"="joesph"; "LastName"="smith"; "YearsInMS"=15}
$employee3 = [pscustomobject]@{"FirstName"="john"; "LastName"="smyth"; "YearsInMS"=2}
$employees = @($employee1,$employee2,$employee3)
$ht = @{"e"="YearsInMS"; "descending"=$false; "ascending"=$false}
$results = $employees | Sort-Object -Property $ht -Descending
$results[0] | Should Be $employees[1]
$results[1] | Should Be $employees[0]
$results[2] | Should Be $employees[2]
}
It "Sort-Object with One Order Entry Key should work"{
$employee1 = [pscustomobject]@{"FirstName"="john"; "LastName"="smith"; "YearsInMS"=5}
$employee2 = [pscustomobject]@{"FirstName"="joesph"; "LastName"="smith"; "YearsInMS"=15}
$employee3 = [pscustomobject]@{"FirstName"="john"; "LastName"="smyth"; "YearsInMS"=2}
$employees = @($employee1,$employee2,$employee3)
$ht = @{"e"="YearsInMS"; "descending"=$false}
$results = $employees | Sort-Object -Property $ht -Descending
$results[0] | Should Be $employees[2]
$results[1] | Should Be $employees[0]
$results[2] | Should Be $employees[1]
}
It "Sort-Object with HistoryInfo object should work"{
Add-Type -TypeDefinition "public enum PipelineState{NotStarted,Running,Stopping,Stopped,Completed,Failed,Disconnected}"
$historyInfo1 = [pscustomobject]@{"PipelineId"=1; "Cmdline"="cmd3"; "Status"=[PipelineState]::Completed; "StartTime" = [DateTime]::Now;"EndTime" = [DateTime]::Now.AddSeconds(5.0);}
$historyInfo2 = [pscustomobject]@{"PipelineId"=2; "Cmdline"="cmd1"; "Status"=[PipelineState]::Completed; "StartTime" = [DateTime]::Now;"EndTime" = [DateTime]::Now.AddSeconds(5.0);}
$historyInfo3 = [pscustomobject]@{"PipelineId"=3; "Cmdline"="cmd2"; "Status"=[PipelineState]::Completed; "StartTime" = [DateTime]::Now;"EndTime" = [DateTime]::Now.AddSeconds(5.0);}
$historyInfos = @($historyInfo1,$historyInfo2,$historyInfo3)
$results = $historyInfos | Sort-Object
$results[0] | Should Be $historyInfos[0]
$results[1] | Should Be $historyInfos[1]
$results[2] | Should Be $historyInfos[2]
}
It "Sort-Object with Non Existing And Null Script Property should work"{
$n = new-object microsoft.powershell.commands.newobjectcommand
$d = new-object microsoft.powershell.commands.newobjectcommand
$d.TypeName = 'Deetype'
$b = new-object microsoft.powershell.commands.newobjectcommand
$b.TypeName = 'btype'
$a = new-object microsoft.powershell.commands.newobjectcommand
$a.TypeName = 'atype'
$results = $n, $d, $b, 'b', $a | Sort-Object -proper {$_.TypeName}
$results.Count | Should Be 5
$results[2] | Should Be $a
$results[3] | Should Be $b
$results[4] | Should Be $d
#results[0] and [1] can be any order
}
It "Sort-Object with Non Existing And Null Property should work"{
$n = new-object microsoft.powershell.commands.newobjectcommand
$n.TypeName = $null
$d = new-object microsoft.powershell.commands.newobjectcommand
$d.TypeName = 'Deetype'
$b = new-object microsoft.powershell.commands.newobjectcommand
$b.TypeName = 'btype'
$a = new-object microsoft.powershell.commands.newobjectcommand
$a.TypeName = 'atype'
$results = $n, $d, $b, 'b', $a | Sort-Object -prop TypeName
$results.Count | Should Be 5
$results[0] | Should Be $n
$results[1] | Should Be $a
$results[2] | Should Be $b
$results[3] | Should Be $d
$results[4] | Should Be 'b'
}
It "Sort-Object with Non Case-Sensitive Unique should work"{
$employee1 = [pscustomobject]@{"FirstName"="john"; "LastName"="smith"; "YearsInMS"=5}
$employee2 = [pscustomobject]@{"FirstName"="joesph"; "LastName"="smith"; "YearsInMS"=15}
$employee3 = [pscustomobject]@{"FirstName"="john"; "LastName"="smyth"; "YearsInMS"=12}
$employees = @($employee1,$employee2,$employee3)
$results = $employees | Sort-Object -Property "LastName" -Descending -Unique
$results[0] | Should Be $employees[2]
$results[1] | Should Be $employees[1]
$results[2] | Should BeNullOrEmpty
}
It "Sort-Object with Case-Sensitive Unique should work"{
$employee1 = [pscustomobject]@{"FirstName"="john"; "LastName"="smith"; "YearsInMS"=5}
$employee2 = [pscustomobject]@{"FirstName"="joesph"; "LastName"="smith"; "YearsInMS"=15}
$employee3 = [pscustomobject]@{"FirstName"="john"; "LastName"="smyth"; "YearsInMS"=12}
$employees = @($employee1,$employee2,$employee3)
$results = $employees | Sort-Object -Property "LastName","FirstName" -Descending -Unique -CaseSensitive
$results[0] | Should Be $employees[2]
$results[1] | Should Be $employees[0]
$results[2] | Should Be $employees[1]
}
It "Sort-Object with Two Order Entry Keys should work"{
$employee1 = [pscustomobject]@{"FirstName"="john"; "LastName"="smith"; "YearsInMS"=5}
$employee2 = [pscustomobject]@{"FirstName"="joesph"; "LastName"="smith"; "YearsInMS"=15}
$employee3 = [pscustomobject]@{"FirstName"="john"; "LastName"="smyth"; "YearsInMS"=2}
$employees = @($employee1,$employee2,$employee3)
$ht1 = @{"expression"="LastName"; "ascending"=$false}
$ht2 = @{"expression"="FirstName"; "ascending"=$true}
$results = $employees | Sort-Object -Property @($ht1,$ht2) -Descending
$results[0] | Should Be $employees[2]
$results[1] | Should Be $employees[1]
$results[2] | Should Be $employees[0]
}
It "Sort-Object with -Descending:$false and Two Order Entry Keys should work"{
$employee1 = [pscustomobject]@{"FirstName"="john"; "LastName"="smith"; "YearsInMS"=5}
$employee2 = [pscustomobject]@{"FirstName"="joesph"; "LastName"="smith"; "YearsInMS"=15}
$employee3 = [pscustomobject]@{"FirstName"="john"; "LastName"="smyth"; "YearsInMS"=12}
$employees = @($employee1,$employee2,$employee3)
$results = $employees | Sort-Object -Property "LastName","FirstName" -Descending:$false
$results[0] | Should Be $employees[1]
$results[1] | Should Be $employees[0]
$results[2] | Should Be $employees[2]
}
It "Sort-Object with -Descending and Two Order Entry Keys should work"{
$employee1 = [pscustomobject]@{"FirstName"="john"; "LastName"="smith"; "YearsInMS"=5}
$employee2 = [pscustomobject]@{"FirstName"="joesph"; "LastName"="smith"; "YearsInMS"=15}
$employee3 = [pscustomobject]@{"FirstName"="john"; "LastName"="smyth"; "YearsInMS"=12}
$employees = @($employee1,$employee2,$employee3)
$results = $employees | Sort-Object -Property "LastName","FirstName" -Descending
$results[0] | Should Be $employees[2]
$results[1] | Should Be $employees[0]
$results[2] | Should Be $employees[1]
}
It "Sort-Object with Two Order Entry Keys with asc=true should work"{
$employee1 = [pscustomobject]@{"FirstName"="john"; "LastName"="smith"; "YearsInMS"=5}
$employee2 = [pscustomobject]@{"FirstName"="joesph"; "LastName"="smith"; "YearsInMS"=15}
$employee3 = [pscustomobject]@{"FirstName"="john"; "LastName"="smyth"; "YearsInMS"=12}
$employees = @($employee1,$employee2,$employee3)
$ht1 = @{"e"="FirstName"; "asc"=$true}
$ht2 = @{"e"="LastName";}
$results = $employees | Sort-Object -Property @($ht1,$ht2) -Descending
$results[0] | Should Be $employees[1]
$results[1] | Should Be $employees[2]
$results[2] | Should Be $employees[0]
}
It "Sort-Object with Descending No Property should work"{
$employee1 = 1
$employee2 = 2
$employee3 = 3
$employees = @($employee1,$employee2,$employee3)
$results = $employees | Sort-Object -Descending
$results[0] | Should Be 3
$results[1] | Should Be 2
$results[2] | Should Be 1
}
}