forked from PowerShell/PowerShell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSemanticVersion.Tests.ps1
More file actions
205 lines (180 loc) · 8.25 KB
/
SemanticVersion.Tests.ps1
File metadata and controls
205 lines (180 loc) · 8.25 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
using namespace System.Management.Automation
using namespace System.Management.Automation.Language
Describe "SemanticVersion api tests" -Tags 'CI' {
Context "constructing valid versions" {
It "string argument constructor" {
$v = [SemanticVersion]::new("1.2.3-alpha")
$v.Major | Should Be 1
$v.Minor | Should Be 2
$v.Patch | Should Be 3
$v.Label | Should Be "alpha"
$v.ToString() | Should Be "1.2.3-alpha"
$v = [SemanticVersion]::new("1.0.0")
$v.Major | Should Be 1
$v.Minor | Should Be 0
$v.Patch | Should Be 0
$v.Label | Should BeNullOrEmpty
$v.ToString() | Should Be "1.0.0"
}
# After the above test, we trust the properties and rely on ToString for validation
It "int args constructor" {
$v = [SemanticVersion]::new(1, 0, 0)
$v.ToString() | Should Be "1.0.0"
$v = [SemanticVersion]::new(3, 2, 0, "beta.1")
$v.ToString() | Should Be "3.2.0-beta.1"
}
It "version arg constructor" {
$v = [SemanticVersion]::new([Version]::new(1, 2, 3))
$v.ToString() | Should Be '1.2.3'
}
It "semantic version can round trip through version" {
$v1 = [SemanticVersion]::new(3, 2, 1, "prerelease")
$v2 = [SemanticVersion]::new([Version]$v1)
$v2.ToString() | Should Be "3.2.1-prerelease"
}
}
Context "Comparisons" {
$v1_0_0 = [SemanticVersion]::new(1, 0, 0)
$v1_1_0 = [SemanticVersion]::new(1, 1, 0)
$v1_1_1 = [SemanticVersion]::new(1, 1, 1)
$v2_1_0 = [SemanticVersion]::new(2, 1, 0)
$v1_0_0_alpha = [SemanticVersion]::new(1, 0, 0, "alpha")
$v1_0_0_beta = [SemanticVersion]::new(1, 0, 0, "beta")
$testCases = @(
@{ lhs = $v1_0_0; rhs = $v1_1_0 }
@{ lhs = $v1_0_0; rhs = $v1_1_1 }
@{ lhs = $v1_1_0; rhs = $v1_1_1 }
@{ lhs = $v1_0_0; rhs = $v2_1_0 }
@{ lhs = $v1_0_0_alpha; rhs = $v1_0_0_beta }
@{ lhs = $v1_0_0_alpha; rhs = $v1_0_0 }
@{ lhs = $v1_0_0_beta; rhs = $v1_0_0 }
)
It "less than" -TestCases $testCases {
param($lhs, $rhs)
$lhs -lt $rhs | Should Be $true
$rhs -lt $lhs | Should Be $false
}
It "less than or equal" -TestCases $testCases {
param($lhs, $rhs)
$lhs -le $rhs | Should Be $true
$rhs -le $lhs | Should Be $false
$lhs -le $lhs | Should Be $true
$rhs -le $rhs | Should Be $true
}
It "greater than" -TestCases $testCases {
param($lhs, $rhs)
$lhs -gt $rhs | Should Be $false
$rhs -gt $lhs | Should Be $true
}
It "greater than or equal" -TestCases $testCases {
param($lhs, $rhs)
$lhs -ge $rhs | Should Be $false
$rhs -ge $lhs | Should Be $true
$lhs -ge $lhs | Should Be $true
$rhs -ge $rhs | Should Be $true
}
$testCases = @(
@{ operand = $v1_0_0 }
@{ operand = $v1_0_0_alpha }
)
It "Equality" -TestCases $testCases {
param($operand)
$operand -eq $operand | Should Be $true
$operand -ne $operand | Should Be $false
$null -eq $operand | Should Be $false
$operand -eq $null | Should Be $false
$null -ne $operand | Should Be $true
$operand -ne $null | Should Be $true
}
It "comparisons with null" {
$v1_0_0 -lt $null | Should Be $false
$null -lt $v1_0_0 | Should Be $true
$v1_0_0 -le $null | Should Be $false
$null -le $v1_0_0 | Should Be $true
$v1_0_0 -gt $null | Should Be $true
$null -gt $v1_0_0 | Should Be $false
$v1_0_0 -ge $null | Should Be $true
$null -ge $v1_0_0 | Should Be $false
}
}
Context "error handling" {
# The specific errors aren't too useful here, but noted in comments
# so when we pick up a version of Pester that will let us check FullyQualifiedErrorId,
# it's easier to tweak the tests
$testCases = @(
@{ expectedResult = $false; version = $null }
@{ expectedResult = $false; version = [NullString]::Value }
@{ expectedResult = $false; version = "" }
@{ expectedResult = $false; version = "1.0.0-" }
@{ expectedResult = $false; version = "-" }
@{ expectedResult = $false; version = "-alpha" }
@{ expectedResult = $false; version = "1.0" } # REVIEW - should this be allowed
@{ expectedResult = $false; version = "1..0" }
@{ expectedResult = $false; version = "1.0.-alpha" }
@{ expectedResult = $false; version = "1.0." }
@{ expectedResult = $false; version = ".0.0" }
)
It "parts of version missing" -TestCases $testCases {
param($version, $expectedResult)
{ [SemanticVersion]::new($version) } | Should Throw # PSArgumentException
{ [SemanticVersion]::Parse($version) } | Should Throw # PSArgumentException
$semVer = $null
[SemanticVersion]::TryParse($_, [ref]$semVer) | Should Be $expectedResult
$semVer | Should Be $null
}
$testCases = @(
@{ expectedResult = $false; version = "-1.0.0" }
@{ expectedResult = $false; version = "1.-1.0" }
@{ expectedResult = $false; version = "1.0.-1" }
)
It "range check of versions" -TestCases $testCases {
param($version, $expectedResult)
{ [SemanticVersion]::new($version) } | Should Throw # PSArgumentException
{ [SemanticVersion]::Parse($version) } | Should Throw # PSArgumentException
$semVer = $null
[SemanticVersion]::TryParse($_, [ref]$semVer) | Should Be $expectedResult
$semVer | Should Be $null
}
$testCases = @(
@{ expectedResult = $false; version = "aa.0.0" }
@{ expectedResult = $false; version = "1.bb.0" }
@{ expectedResult = $false; version = "1.0.cc" }
)
It "format errors" -TestCases $testCases {
param($version, $expectedResult)
{ [SemanticVersion]::new($version) } | Should Throw # PSArgumentException
{ [SemanticVersion]::Parse($version) } | Should Throw # PSArgumentException
$semVer = $null
[SemanticVersion]::TryParse($_, [ref]$semVer) | Should Be $expectedResult
$semVer | Should Be $null
}
It "Negative version arguments" {
{ [SemanticVersion]::new(-1, 0) } | Should Throw # PSArgumentException
{ [SemanticVersion]::new(1, -1) } | Should Throw # PSArgumentException
{ [SemanticVersion]::new(1, 1, -1) } | Should Throw # PSArgumentException
}
It "Incompatible version throws" {
# Revision isn't supported
{ [SemanticVersion]::new([Version]::new(0, 0, 0, 4)) } | Should Throw # PSArgumentException
{ [SemanticVersion]::new([Version]::new("1.2.3.4")) } | Should Throw # PSArgumentException
# Build is required
{ [SemanticVersion]::new([Version]::new(1, 2)) } | Should Throw # PSArgumentException
{ [SemanticVersion]::new([Version]::new("1.2")) } | Should Throw # PSArgumentException
}
}
Context "Serialization" {
$testCases = @(
@{ expectedResult = "1.0.0"; semver = [SemanticVersion]::new(1, 0, 0) }
@{ expectedResult = "1.0.1"; semver = [SemanticVersion]::new(1, 0, 1) }
@{ expectedResult = "1.0.0-alpha"; semver = [SemanticVersion]::new(1, 0, 0, "alpha") }
@{ expectedResult = "1.0.0-beta"; semver = [SemanticVersion]::new(1, 0, 0, "beta") }
)
It "Can round trip" -TestCases $testCases {
param($semver, $expectedResult)
$ser = [PSSerializer]::Serialize($semver)
$des = [PSSerializer]::Deserialize($ser)
$des | Should BeOfType System.Management.Automation.SemanticVersion
$des.ToString() | Should Be $expectedResult
}
}
}