forked from pester/Pester
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFormat.ps1
227 lines (181 loc) · 5.91 KB
/
Format.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
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
# PESTER_BUILD
if (-not (Get-Variable -Name "PESTER_BUILD" -ValueOnly -ErrorAction Ignore)) {
. "$PSScriptRoot/functions/Pester.SafeCommands.ps1"
. "$PSScriptRoot/TypeClass.ps1"
}
# end PESTER_BUILD
function Format-Collection ($Value, [switch]$Pretty) {
$Limit = 10
$separator = ', '
if ($Pretty) {
$separator = ",`n"
}
$count = $Value.Count
$trimmed = $count -gt $Limit
$formattedCollection = @()
# Using foreach to support ICollection
$i = 0
foreach ($v in $Value) {
if ($i -eq $Limit) { break }
$formattedValue = Format-Nicely -Value $v -Pretty:$Pretty
$formattedCollection += $formattedValue
$i++
}
'@(' + ($formattedCollection -join $separator) + $(if ($trimmed) { ", ...$($count - $limit) more" }) + ')'
}
function Format-Object ($Value, $Property, [switch]$Pretty) {
if ($null -eq $Property) {
$Property = $Value.PSObject.Properties | & $SafeCommands['Select-Object'] -ExpandProperty Name
}
$valueType = Get-ShortType $Value
$valueFormatted = ([string]([PSObject]$Value | & $SafeCommands['Select-Object'] -Property $Property))
if ($Pretty) {
$margin = " "
$valueFormatted = $valueFormatted `
-replace '^@{', "@{`n$margin" `
-replace '; ', ";`n$margin" `
-replace '}$', "`n}" `
}
$valueFormatted -replace "^@", $valueType
}
function Format-Null {
'$null'
}
function Format-String ($Value) {
if ('' -eq $Value) {
return '<empty>'
}
"'$Value'"
}
function Format-Date ($Value) {
$Value.ToString('o')
}
function Format-Boolean ($Value) {
'$' + $Value.ToString().ToLower()
}
function Format-ScriptBlock ($Value) {
'{' + $Value + '}'
}
function Format-Number ($Value) {
[string]$Value
}
function Format-Hashtable ($Value) {
$head = '@{'
$tail = '}'
$entries = $Value.Keys | & $SafeCommands['Sort-Object'] | & $SafeCommands['ForEach-Object'] {
$formattedValue = Format-Nicely $Value.$_
"$_=$formattedValue" }
$head + ( $entries -join '; ') + $tail
}
function Format-Dictionary ($Value) {
$head = 'Dictionary{'
$tail = '}'
$entries = $Value.Keys | & $SafeCommands['Sort-Object'] | & $SafeCommands['ForEach-Object'] {
$formattedValue = Format-Nicely $Value.$_
"$_=$formattedValue" }
$head + ( $entries -join '; ') + $tail
}
function Format-Nicely ($Value, [switch]$Pretty) {
if ($null -eq $Value) {
return Format-Null -Value $Value
}
if ($Value -is [bool]) {
return Format-Boolean -Value $Value
}
if ($Value -is [string]) {
return Format-String -Value $Value
}
if ($Value -is [DateTime]) {
return Format-Date -Value $Value
}
if ($value -is [Type]) {
return '[' + (Format-Type -Value $Value) + ']'
}
if (Is-DecimalNumber -Value $Value) {
return Format-Number -Value $Value
}
if (Is-ScriptBlock -Value $Value) {
return Format-ScriptBlock -Value $Value
}
if (Is-Value -Value $Value) {
return $Value
}
if (Is-Hashtable -Value $Value) {
# no advanced formatting of objects in the first version, till I balance it
return [string]$Value
#return Format-Hashtable -Value $Value
}
if (Is-Dictionary -Value $Value) {
# no advanced formatting of objects in the first version, till I balance it
return [string]$Value
#return Format-Dictionary -Value $Value
}
if (Is-Collection -Value $Value) {
return Format-Collection -Value $Value -Pretty:$Pretty
}
# no advanced formatting of objects in the first version, till I balance it
return [string]$Value
# Format-Object -Value $Value -Property (Get-DisplayProperty $Value) -Pretty:$Pretty
}
function Sort-Property ($InputObject, [string[]]$SignificantProperties, $Limit = 4) {
$properties = @($InputObject.PSObject.Properties |
& $SafeCommands['Where-Object'] { $_.Name -notlike "_*" } |
& $SafeCommands['Select-Object'] -expand Name |
& $SafeCommands['Sort-Object'])
$significant = @()
$rest = @()
foreach ($p in $properties) {
if ($significantProperties -contains $p) {
$significant += $p
}
else {
$rest += $p
}
}
#todo: I am assuming id, name properties, so I am just sorting the selected ones by name.
(@($significant | & $SafeCommands['Sort-Object']) + $rest) | & $SafeCommands['Select-Object'] -First $Limit
}
function Get-DisplayProperty ($Value) {
Sort-Property -InputObject $Value -SignificantProperties 'id', 'name'
}
function Get-ShortType ($Value) {
if ($null -ne $value) {
$type = Format-Type $Value.GetType()
# PSCustomObject serializes to the whole type name on normal PS but to
# just PSCustomObject on PS Core
$type `
-replace "^System\." `
-replace "^Management\.Automation\.PSCustomObject$", "PSObject" `
-replace "^PSCustomObject$", "PSObject" `
-replace "^Object\[\]$", "collection" `
}
else {
Format-Type $null
}
}
function Format-Type ([Type]$Value) {
if ($null -eq $Value) {
return '<none>'
}
[string]$Value
}
function Join-With ($Items, $Threshold = 2, $Separator = ', ', $LastSeparator = ' and ') {
if ($null -eq $items -or $items.count -lt $Threshold) {
$items -join $Separator
}
else {
$c = $items.count
($items[0..($c - 2)] -join $Separator) + $LastSeparator + $items[-1]
}
}
function Join-And ($Items, $Threshold = 2) {
Join-With -Items $Items -Threshold $Threshold -Separator ', ' -LastSeparator ' and '
}
function Join-Or ($Items, $Threshold = 2) {
Join-With -Items $Items -Threshold $Threshold -Separator ', ' -LastSeparator ' or '
}
function Add-SpaceToNonEmptyString ([string]$Value) {
if ($Value) {
" $Value"
}
}