forked from pester/Pester
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Format.Tests.ps1
234 lines (206 loc) · 9.15 KB
/
Format.Tests.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
228
229
230
231
232
233
234
Set-StrictMode -Version Latest
BeforeAll {
Get-Module Axiom, Format | Remove-Module
Import-Module $PSScriptRoot\axiom\Axiom.psm1 -ErrorAction 'stop' -DisableNameChecking
. $PSScriptRoot\..\src\Format.ps1
}
# discovery time setup
# Add-Type -TypeDefinition 'namespace Assertions.TestType { public class Person { public string Name {get;set;} public int Age {get;set;}}}'
# function New-Dictionary ([hashtable]$Hashtable) {
# $d = new-object "Collections.Generic.Dictionary[string,object]"
# $Hashtable.GetEnumerator() | foreach { $d.Add($_.Key, $_.Value) }
# $d
# }
Describe "Format-Collection" {
It "Formats collection of values '<value>' to '<expected>' using comma separator" -TestCases @(
@{ Value = (1, 2, 3); Expected = "@(1, 2, 3)" }
) {
param ($Value, $Expected)
Format-Collection -Value $Value | Verify-Equal $Expected
}
It "Formats collection that is longer than 10 with elipsis and output remaining value count" -TestCases @(
@{ Value = (1..20); Expected = "@(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, ...10 more)" }
) {
param ($Value, $Expected)
Format-Collection -Value $Value | Verify-Equal $Expected
}
It "Formats collection '<value>' with `$null values to '<expected>'" -TestCases @(
@{ Value = @('a', 'b', 'c', $null); Expected = "@('a', 'b', 'c', `$null)" }
) {
param ($Value, $Expected)
Format-Collection -Value $Value | Verify-Equal $Expected
}
}
Describe "Format-Number" {
It "Formats number to use . separator (tests anything only on non-english systems --todo)" -TestCases @(
@{ Value = 1.1; },
@{ Value = [double] 1.1; },
@{ Value = [float] 1.1; },
@{ Value = [single] 1.1; },
@{ Value = [decimal] 1.1; }
) {
param ($Value)
Format-Number -Value $Value | Verify-Equal "1.1"
}
}
# skipping object formatting for now
# Describe "Format-Object" {
# It "Formats object '<value>' to '<expected>'" -TestCases @(
# @{ Value = ([PSCustomObject] @{Name = 'Jakub'; Age = 28}); Expected = "PSObject{Age=28; Name=Jakub}"},
# @{ Value = (New-Object -Type Assertions.TestType.Person -Property @{Name = 'Jakub'; Age = 28}); Expected = "Assertions.TestType.Person{Age=28; Name=Jakub}"}
# ) {
# param ($Value, $Expected)
# Format-Object -Value $Value | Verify-Equal $Expected
# }
# It "Formats object '<value>' with selected properties '<selectedProperties>' to '<expected>'" -TestCases @(
# @{ Value = ([PSCustomObject] @{Name = 'Jakub'; Age = 28}); SelectedProperties = "Age"; Expected = "PSObject{Age=28}"},
# @{ Value = (Get-Process -Name Idle); SelectedProperties = 'Name', 'Id'; Expected = "Diagnostics.Process{Id=0; Name=Idle}" },
# @{
# Value = (New-Object -Type Assertions.TestType.Person -Property @{Name = 'Jakub'; Age = 28})
# SelectedProperties = 'Name'
# Expected = "Assertions.TestType.Person{Name=Jakub}"
# }
# ) {
# param ($Value, $SelectedProperties, $Expected)
# Format-Object -Value $Value -Property $SelectedProperties | Verify-Equal $Expected
# }
# }
Describe "Format-Boolean" {
It "Formats boolean '<value>' to '<expected>'" -TestCases @(
@{ Value = $true; Expected = '$true' },
@{ Value = $false; Expected = '$false' }
) {
param($Value, $Expected)
Format-Boolean -Value $Value | Verify-Equal $Expected
}
}
Describe "Format-Null" {
It "Formats null to '`$null'" {
Format-Null | Verify-Equal '$null'
}
}
Describe "Format-String" {
It "Formats empty string to '``<empty``>'" {
Format-String -Value "" | Verify-Equal '<empty>'
}
It "Formats string to be sorrounded by quotes" {
Format-String -Value "abc" | Verify-Equal "'abc'"
}
}
Describe "Format-DateTime" {
It "Formats date to orderable format with ticks" {
Format-Date -Value ([dateTime]239842659899234234) | Verify-Equal '0761-01-12T16:06:29.9234234'
}
}
Describe "Format-ScriptBlock" {
It "Formats scriptblock as string with curly braces" {
Format-ScriptBlock -Value { abc } | Verify-Equal '{ abc }'
}
}
# skipping object formatting for now
# Describe "Format-Hashtable" {
# It "Formats empty hashtable as @{}" {
# Format-Hashtable @{} | Verify-Equal '@{}'
# }
# It "Formats hashtable as '<expected>'" -TestCases @(
# @{ Value = @{Age = 28; Name = 'Jakub'}; Expected = '@{Age=28; Name=Jakub}' }
# @{ Value = @{Z = 1; H = 1; A = 1}; Expected = '@{A=1; H=1; Z=1}' }
# @{ Value = @{Hash = @{Hash = 'Value'}}; Expected = '@{Hash=@{Hash=Value}}' }
# ) {
# param ($Value, $Expected)
# Format-Hashtable $Value | Verify-Equal $Expected
# }
# }
# skipping object formatting for now
# Describe "Format-Dictionary" {
# It "Formats empty dictionary as @{}" {
# Format-Dictionary (New-Dictionary @{}) | Verify-Equal 'Dictionary{}'
# }
# It "Formats dictionary as '<expected>'" -TestCases @(
# @{ Value = New-Dictionary @{Age = 28; Name = 'Jakub'}; Expected = 'Dictionary{Age=28; Name=Jakub}' }
# @{ Value = New-Dictionary @{Z = 1; H = 1; A = 1}; Expected = 'Dictionary{A=1; H=1; Z=1}' }
# @{ Value = New-Dictionary @{Dict = ( New-Dictionary @{Dict = 'Value'})}; Expected = 'Dictionary{Dict=Dictionary{Dict=Value}}' }
# ) {
# param ($Value, $Expected)
# Format-Dictionary $Value | Verify-Equal $Expected
# }
# }
Describe "Format-Nicely" {
It "Formats value '<value>' correctly to '<expected>'" -TestCases @(
@{ Value = $null; Expected = '$null' }
@{ Value = $true; Expected = '$true' }
@{ Value = $false; Expected = '$false' }
@{ Value = 'a' ; Expected = "'a'" },
@{ Value = '' ; Expected = '<empty>' },
@{ Value = ([datetime]636545721418385266) ; Expected = '2018-02-18T17:35:41.8385266' },
@{ Value = 1; Expected = '1' },
@{ Value = (1, 2, 3); Expected = '@(1, 2, 3)' },
@{ Value = 1.1; Expected = '1.1' },
@{ Value = [int]; Expected = '[int]' }
# @{ Value = [PSCustomObject] @{ Name = "Jakub" }; Expected = 'PSObject{Name=Jakub}' },
#@{ Value = (Get-Process Idle); Expected = 'Diagnostics.Process{Id=0; Name=Idle}'},
#@{ Value = (New-Object -Type Assertions.TestType.Person -Property @{Name = 'Jakub'; Age = 28}); Expected = "Assertions.TestType.Person{Age=28; Name=Jakub}"}
#@{ Value = @{Name = 'Jakub'; Age = 28}; Expected = '@{Age=28; Name=Jakub}' }
#@{ Value = New-Dictionary @{Age = 28; Name = 'Jakub'}; Expected = 'Dictionary{Age=28; Name=Jakub}' }
) {
param($Value, $Expected)
Format-Nicely -Value $Value | Verify-Equal $Expected
}
}
# skipping object formatting for now
# Describe "Get-DisplayProperty" {
# It "Returns '<expected>' for '<type>'" -TestCases @(
# @{ Type = "Diagnostics.Process"; Expected = ("Id", "Name") }
# ) {
# param ($Type, $Expected)
# $Actual = Get-DisplayProperty -Type $Type
# "$Actual" | Verify-Equal "$Expected"
# }
# }
Describe "Format-Type" {
It "Given '<value>' it returns the correct shortened type name '<expected>'" -TestCases @(
@{ Value = [int]; Expected = 'int' },
@{ Value = [double]; Expected = 'double' },
@{ Value = [string]; Expected = 'string' },
@{ Value = $null; Expected = '<none>' }
) {
param($Value, $Expected)
Format-Type -Value $Value | Verify-Equal $Expected
}
}
Describe "Get-ShortType" {
It "Given '<value>' it returns the correct shortened type name '<expected>'" -TestCases @(
@{ Value = 1; Expected = 'int' },
@{ Value = 1.1; Expected = 'double' },
@{ Value = 'a' ; Expected = 'string' },
@{ Value = $null ; Expected = '<none>' },
@{ Value = [PSCustomObject] @{Name = 'Jakub' } ; Expected = 'PSObject' },
@{ Value = [Object[]]1, 2, 3 ; Expected = 'collection' }
) {
param($Value, $Expected)
Get-ShortType -Value $Value | Verify-Equal $Expected
}
}
Describe "Join-And" {
It "Given '<items>' and default threshold of 2 it returns the correct joined string '<expected>'" -TestCases @(
@{ Items = $null; Expected = [string]::Empty },
@{ Items = @('one'); Expected = 'one' },
@{ Items = @('one', 'two'); Expected = 'one and two' },
@{ Items = @('one', 'two', 'three'); Expected = 'one, two and three' }
) {
param($Items, $Expected)
Join-And -Items $Items | Verify-Equal $Expected
}
It "Given '<items>' and custom threshold <threshold> it returns the correct joined string '<expected>'" -TestCases @(
@{ Items = @('one', 'two', 'three'); Threshold = 3; Expected = 'one, two and three' },
@{ Items = @('one', 'two', 'three'); Threshold = 4; Expected = 'one, two, three' }
) {
param($Items, $Threshold, $Expected)
Join-And -Items $Items -Threshold $Threshold | Verify-Equal $Expected
}
}
Describe "Add-SpaceToNonEmptyString" {
It "Formats non-empty string with leading whitespace" {
Add-SpaceToNonEmptyString -Value 'message' | Verify-Equal ' message'
}
}