forked from PowerShell/PowerShell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSelect-Object.Tests.ps1
More file actions
298 lines (231 loc) · 10.1 KB
/
Copy pathSelect-Object.Tests.ps1
File metadata and controls
298 lines (231 loc) · 10.1 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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
. (Join-Path -Path $PSScriptRoot -ChildPath Test-Mocks.ps1)
Describe "Select-Object" -Tags "CI" {
BeforeEach {
$dirObject = GetFileMock
$TestLength = 3
}
It "Handle piped input without error" {
{ $dirObject | Select-Object } | Should Not Throw
}
It "Should treat input as a single object with the inputObject parameter" {
$result = $(Select-Object -inputObject $dirObject -last $TestLength).Length
$expected = $dirObject.Length
$result | Should Be $expected
}
It "Should be able to use the alias" {
{ $dirObject | select } | Should Not Throw
}
It "Should have same result when using alias" {
$result = $dirObject | select
$expected = $dirObject | Select-Object
$result | Should Be $expected
}
It "Should return correct object with First parameter" {
$result = $dirObject | Select-Object -First $TestLength
$result.Length | Should Be $TestLength
for ($i=0; $i -lt $TestLength; $i++)
{
$result[$i].Name | Should Be $dirObject[$i].Name
}
}
It "Should return correct object with Last parameter" {
$result = $dirObject | Select-Object -Last $TestLength
$result.Length | Should Be $TestLength
for ($i=0; $i -lt $TestLength; $i++)
{
$result[$i].Name | Should Be $dirObject[$dirObject.Length - $TestLength + $i].Name
}
}
It "Should work correctly with Unique parameter" {
$result = ("a","b","c","a","a","a" | Select-Object -Unique).Length
$expected = 3
$result | Should Be $expected
}
It "Should return correct object with Skip parameter" {
$result = $dirObject | Select-Object -Skip $TestLength
$result.Length | Should Be ($dirObject.Length - $TestLength)
for ($i=0; $i -lt $TestLength; $i++)
{
$result[$i].Name | Should Be $dirObject[$TestLength + $i].Name
}
}
It "Should return an object with selected columns" {
$result = $dirObject | Select-Object -Property Name, Size
$result.Length | Should Be $dirObject.Length
$result[0].Name | Should Be $dirObject[0].Name
$result[0].Size | Should Be $dirObject[0].Size
$result[0].Mode | Should BeNullOrEmpty
}
It "Should send output to pipe properly" {
{$dirObject | Select-Object -Unique | pipelineConsume} | Should Not Throw
}
It "Should select array indices with Index parameter" {
$firstIndex = 2
$secondIndex = 4
$result = $dirObject | Select-Object -Index $firstIndex, $secondIndex
$result[0].Name | Should Be $dirObject[$firstIndex].Name
$result[1].Name | Should Be $dirObject[$secondIndex].Name
}
# Note that these two tests will modify original values of $dirObject
It "Should not wait when used without -Wait option" {
$orig1 = $dirObject[0].Size
$orig2 = $dirObject[$TestLength].Size
$result = $dirObject | addOneToSizeProperty | Select-Object -First $TestLength
$result[0].Size | Should Be ($orig1 + 1)
$dirObject[0].Size | Should Be ($orig1 + 1)
$dirObject[$TestLength].Size | Should Be $orig2
}
It "Should wait when used with -Wait option" {
$orig1 = $dirObject[0].Size
$orig2 = $dirObject[$TestLength].Size
$result = $dirObject | addOneToSizeProperty | Select-Object -First $TestLength -Wait
$result[0].Size | Should Be ($orig1 + 1)
$dirObject[0].Size | Should Be ($orig1 + 1)
$dirObject[$TestLength].Size | Should Be ($orig2 + 1)
}
}
Describe "Select-Object DRT basic functionality" -Tags "CI" {
BeforeAll {
$employees = [pscustomobject]@{"FirstName"="joseph"; "LastName"="smith"; "YearsInMS"=15},
[pscustomobject]@{"FirstName"="paul"; "LastName"="smith"; "YearsInMS"=15},
[pscustomobject]@{"FirstName"="mary"; "LastName"="soe"; "YearsInMS"=5},
[pscustomobject]@{"FirstName"="edmund"; "LastName"="bush"; "YearsInMS"=9}
}
It "Select-Object with empty script block property should throw"{
try
{
"bar" | select-object -Prop {} -EA Stop
Throw "Execution OK"
}
catch
{
$_.CategoryInfo | Should Match "PSArgumentException"
$_.FullyQualifiedErrorId | Should be "EmptyScriptBlockAndNoName,Microsoft.PowerShell.Commands.SelectObjectCommand"
}
}
It "Select-Object with string property should work"{
$result = "bar" | select-object -Prop foo | Measure-Object
$result.Count | Should Be 1
}
It "Select-Object with Property First Last Overlap should work"{
$results = $employees | Select-Object -Property "YearsInMS", "L*" -First 2 -Last 3
$results.Count | Should Be 4
$results[0].LastName | Should Be $employees[0].LastName
$results[1].LastName | Should Be $employees[1].LastName
$results[2].LastName | Should Be $employees[2].LastName
$results[3].LastName | Should Be $employees[3].LastName
$results[0].YearsInMS | Should Be $employees[0].YearsInMS
$results[1].YearsInMS | Should Be $employees[1].YearsInMS
$results[2].YearsInMS | Should Be $employees[2].YearsInMS
$results[3].YearsInMS | Should Be $employees[3].YearsInMS
}
It "Select-Object with Property First Last should work"{
$results = $employees | Select-Object -Property "YearsInMS", "L*" -First 2 -Last 1
$results.Count | Should Be 3
$results[0].LastName | Should Be $employees[0].LastName
$results[1].LastName | Should Be $employees[1].LastName
$results[2].LastName | Should Be $employees[3].LastName
$results[0].YearsInMS | Should Be $employees[0].YearsInMS
$results[1].YearsInMS | Should Be $employees[1].YearsInMS
$results[2].YearsInMS | Should Be $employees[3].YearsInMS
}
It "Select-Object with Property First should work"{
$results = $employees | Select-Object -Property "YearsInMS", "L*" -First 2
$results.Count | Should Be 2
$results[0].LastName | Should Be $employees[0].LastName
$results[1].LastName | Should Be $employees[1].LastName
$results[0].YearsInMS | Should Be $employees[0].YearsInMS
$results[1].YearsInMS | Should Be $employees[1].YearsInMS
}
It "Select-Object with Property First Zero should work"{
$results = $employees | Select-Object -Property "YearsInMS", "L*" -First 0
$results.Count | Should Be 0
}
It "Select-Object with Property Last Zero should work"{
$results = $employees | Select-Object -Property "YearsInMS", "L*" -Last 0
$results.Count | Should Be 0
}
It "Select-Object with Unique should work"{
$results = $employees | Select-Object -Property "YearsInMS", "L*" -Unique:$true
$results.Count | Should Be 3
$results[0].LastName | Should Be $employees[1].LastName
$results[1].LastName | Should Be $employees[2].LastName
$results[2].LastName | Should Be $employees[3].LastName
$results[0].YearsInMS | Should Be $employees[1].YearsInMS
$results[1].YearsInMS | Should Be $employees[2].YearsInMS
$results[2].YearsInMS | Should Be $employees[3].YearsInMS
}
It "Select-Object with Simple should work"{
$employee1 = [pscustomobject]@{"FirstName"="joesph"; "LastName"="smith"; "YearsInMS"=15}
$employee2 = [pscustomobject]@{"FirstName"="paul"; "LastName"="smith"; "YearsInMS"=15}
$employee3 = [pscustomobject]@{"FirstName"="mary"; "LastName"="soe"; "YearsInMS"=15}
$employees3 = @($employee1,$employee2,$employee3,$employee4)
$results = $employees3 | Select-Object -Property "FirstName", "YearsInMS"
$results.Count | Should Be 3
$results[0].FirstName | Should Be $employees3[0].FirstName
$results[1].FirstName | Should Be $employees3[1].FirstName
$results[2].FirstName | Should Be $employees3[2].FirstName
$results[0].YearsInMS | Should Be $employees3[0].YearsInMS
$results[1].YearsInMS | Should Be $employees3[1].YearsInMS
$results[2].YearsInMS | Should Be $employees3[2].YearsInMS
}
It "Select-Object with no input should work"{
$results = $null | Select-Object -Property "FirstName", "YearsInMS", "FirstNa*"
$results.Count | Should Be 0
}
It "Select-Object with Start-Time In Idle Process should work"{
$results = Get-Process i* | Select-Object ProcessName
$results.Count | Should Not Be 0
}
It "Select-Object with Skip should work"{
$results = "1","2","3" | Select-Object -Skip 1
$results.Count | Should Be 2
$results[0] | Should Be 2
$results[1] | Should Be 3
}
It "Select-Object with Index should work"{
$results = "1","2","3" | Select-Object -Index 2
$results.Count | Should Be 1
$results[0] | Should Be "3"
}
}
Describe "Select-Object with Property = '*'" -Tags "CI" {
# Issue #2420
It "Select-Object with implicit Property = '*' don't return property named '*'"{
$results = [pscustomobject]@{Thing="thing1"} | Select-Object -ExcludeProperty thing
$results.psobject.Properties.Item("*") | Should Be $null
}
# Issue #2420
It "Select-Object with explicit Property = '*' don't return property named '*'"{
$results = [pscustomobject]@{Thing="thing1"} | Select-Object -Property * -ExcludeProperty thing
$results.psobject.Properties.Item("*") | Should Be $null
}
# Issue #2351
It "Select-Object with implicit Property = '*' exclude single property"{
$results = [pscustomobject]@{Thing="thing1"} | Select-Object -ExcludeProperty thing
$results.psobject.Properties.Item("Thing") | Should Be $null
$results.psobject.Properties.Item("*") | Should Be $null
}
# Issue #2351
It "Select-Object with explicit Property = '*' exclude single property"{
$results = [pscustomobject]@{Thing="thing1"} | Select-Object -Property * -ExcludeProperty thing
$results.psobject.Properties.Item("Thing") | Should Be $null
$results.psobject.Properties.Item("*") | Should Be $null
}
# Issue #2351
It "Select-Object with implicit Property = '*' exclude not single property"{
$results = [pscustomobject]@{Thing="thing1";Param2="param2"} | Select-Object -ExcludeProperty Param2
$results.Param2 | Should Be $null
$results.Thing | Should Be "thing1"
}
# Issue #2351
It "Select-Object with explicit Property = '*' exclude not single property"{
$results = [pscustomobject]@{Thing="thing1";Param2="param2"} | Select-Object -Property * -ExcludeProperty Param2
$results.Param2 | Should Be $null
$results.Thing | Should Be "thing1"
}
It "Select-Object with ExpandProperty and Property don't skip processing ExcludeProperty" {
$p = Get-Process -Id $pid | Select-Object -Property Process* -ExcludeProperty ProcessorAffinity -ExpandProperty Modules
$p[0].psobject.Properties.Item("ProcessorAffinity") | Should Be $null
}
}