/
AboutGroupObject.Koans.ps1
37 lines (33 loc) · 1.18 KB
/
AboutGroupObject.Koans.ps1
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
using module PSKoans
[Koan(Position = 210)]
param()
<#
Group-Object
Group-Object can group objects based on essentially any criteria
you need. Its primary focus is complex objects with properties,
but it's perfectly possible to employ custom groupings on any
data set.
#>
Describe 'Group-Object' {
BeforeAll {
$Files = Get-ChildItem -Path $home -Recurse -Depth 2 -File
}
It 'groups items based on specified properties' {
$Extensions = $Files | Group-Object -Property Extension
<#
The structure of the grouped objects is as follows:
Count (Number of items in this group)
Name (Property that they are grouped by)
Group (All the objects in the group)
#>
'____' | Should -Be $Extensions[3].Name
__ | Should -Be $Extensions[2].Count
}
It 'can group on any custom expression' {
$NameLengths = $Files |
Group-Object -Property {$_.Name.Length} |
Sort-Object -Property {[int]$_.Name} # The 'name' here is the group name
$ShortestFileNames = $NameLengths[0].Group.Name
__ | Should -Be $ShortestFileNames.Length
}
}