@@ -27,4 +27,129 @@ Describe "Type name transformation" {
27
27
It " Null type gives null type name" {
28
28
[Microsoft.PowerShell.CrossCompatibility.Utility.TypeDataConversion ]::GetFullTypeName($null ) | Should - Be $null
29
29
}
30
+ }
31
+
32
+ Describe " PowerShell version object" {
33
+ Context " Version parsing" {
34
+ BeforeAll {
35
+ $genericVerCases = @ (
36
+ @ { VerStr = ' 6' ; Major = 6 ; Minor = 0 ; Patch = 0 }
37
+ @ { VerStr = ' 6.1' ; Major = 6 ; Minor = 1 ; Patch = 0 }
38
+ @ { VerStr = ' 5.2.7' ; Major = 5 ; Minor = 2 ; Patch = 7 }
39
+ @ { VerStr = ' 512.2124.71' ; Major = 512 ; Minor = 2124 ; Patch = 71 }
40
+ )
41
+
42
+ $semVerCases = @ (
43
+ @ { VerStr = ' 6.1.0-rc.1' ; Major = 6 ; Minor = 1 ; Patch = 0 ; Label = ' rc.1' }
44
+ @ { VerStr = ' 6-preview.2' ; Major = 6 ; Minor = 0 ; Patch = 0 ; Label = ' preview.2' }
45
+ @ { VerStr = ' 6.2-preview.2' ; Major = 6 ; Minor = 2 ; Patch = 0 ; Label = ' preview.2' }
46
+ )
47
+
48
+ $systemVerCases = @ (
49
+ @ { VerStr = ' 5.2.1.12312' ; Major = 5 ; Minor = 2 ; Patch = 1 ; Revision = 12312 }
50
+ )
51
+
52
+ $versionFailCases = @ (
53
+ @ { VerStr = ' banana' }
54
+ @ { VerStr = ' ' }
55
+ @ { VerStr = ' 1.' }
56
+ @ { VerStr = ' .6' }
57
+ @ { VerStr = ' 5.1.' }
58
+ @ { VerStr = ' 5.1.2.' }
59
+ @ { VerStr = ' 4.1.5.7.' }
60
+ @ { VerStr = ' 4.1.5.7.4' }
61
+ @ { VerStr = ' 4.1.5.7-rc.2' }
62
+ @ { VerStr = ' 4.1.5.-rc.2' }
63
+ )
64
+ }
65
+
66
+ It " Parses version string '<VerStr>' as <Major>.<Minor>.<Patch>" - TestCases $semVerCases {
67
+ param ([string ]$VerStr , [int ]$Major , [int ]$Minor , [int ]$Patch )
68
+
69
+ $v = [Microsoft.PowerShell.CrossCompatibility.Utility.PowerShellVersion ]::Parse($VerStr )
70
+
71
+ $v.Major | Should - Be $Major
72
+ $v.Minor | Should - Be $Minor
73
+ $v.Patch | Should - Be $Patch
74
+ }
75
+
76
+ It " Parses version string '<VerStr>' as <Major>.<Minor>.<Patch>-<Label>" - TestCases $semVerCases {
77
+ param ([string ]$VerStr , [int ]$Major , [int ]$Minor , [int ]$Patch , [string ]$Label )
78
+
79
+ $v = [Microsoft.PowerShell.CrossCompatibility.Utility.PowerShellVersion ]::Parse($VerStr )
80
+
81
+ $v.Major | Should - Be $Major
82
+ $v.Minor | Should - Be $Minor
83
+ $v.Patch | Should - Be $Patch
84
+ $v.PreReleaseLabel | Should - BeExactly $Label
85
+ }
86
+
87
+ It " Parses version string '<VerStr>' as <Major>.<Minor>.<Patch>.<Revision>" - TestCases $systemVerCases {
88
+ param ([string ]$VerStr , [int ]$Major , [int ]$Minor , [int ]$Patch , [int ]$Revision )
89
+
90
+ $v = [Microsoft.PowerShell.CrossCompatibility.Utility.PowerShellVersion ]::Parse($VerStr )
91
+
92
+ $v.Major | Should - Be $Major
93
+ $v.Minor | Should - Be $Minor
94
+ $v.Patch | Should - Be $Patch
95
+ $v.Revision | Should - Be $Revision
96
+ }
97
+
98
+ It " Does not parse '<VerStr>' as a version" - TestCases $versionFailCases {
99
+ param ([string ]$VerStr )
100
+
101
+ { [Microsoft.PowerShell.CrossCompatibility.Utility.PowerShellVersion ]::Parse($VerStr ) } | Should - Throw
102
+ }
103
+ }
104
+
105
+ Context " Version creation from other versions" {
106
+ BeforeAll {
107
+ $versionCreationTests = @ (
108
+ @ { Version = ' 6.1' ; Major = 6 ; Minor = 1 ; Patch = 0 }
109
+ @ { Version = ' 6.1.4' ; Major = 6 ; Minor = 1 ; Patch = 4 ; }
110
+ @ { Version = ' 5.1.8-preview.2' ; Major = 5 ; Minor = 1 ; Patch = 8 ; Label = ' preview.2' }
111
+ @ { Version = [version ]' 4.2' ; Major = 4 ; Minor = 2 ; Patch = -1 ; Revision = -1 }
112
+ @ { Version = [version ]' 4.2.1' ; Major = 4 ; Minor = 2 ; Patch = 1 ; Revision = -1 }
113
+ @ { Version = [version ]' 4.2.1.7' ; Major = 4 ; Minor = 2 ; Patch = 1 ; Revision = 7 }
114
+ )
115
+
116
+ if ($PSVersionTable.PSVersion.Major -ge 6 )
117
+ {
118
+ $versionCreationTests += @ (
119
+ @ { Version = [semver ]' 6.1.2' ; Major = 6 ; Minor = 1 ; Patch = 2 ; Label = $null }
120
+ @ { Version = [semver ]' 6.1.2-rc.1' ; Major = 6 ; Minor = 1 ; Patch = 2 ; Label = ' rc.1' }
121
+ @ { Version = [semver ]' 6.1-rc.1' ; Major = 6 ; Minor = 1 ; Patch = 0 ; Label = ' rc.1' }
122
+ @ { Version = [semver ]' 6-rc.1' ; Major = 6 ; Minor = 0 ; Patch = 0 ; Label = ' rc.1' }
123
+ )
124
+ }
125
+
126
+ $versionCreationFailTests = @ (
127
+ @ { Version = $null }
128
+ @ { Version = New-Object ' object' }
129
+ @ { Version = ' Hello' }
130
+ )
131
+ }
132
+
133
+ It " Creates a PowerShellVersion from '<Version>'" - TestCases $versionCreationTests {
134
+ param ($Version , [int ]$Major , [int ]$Minor , [int ]$Patch , [int ]$Revision , $Label )
135
+
136
+ $v = [Microsoft.PowerShell.CrossCompatibility.Utility.PowerShellVersion ]::Create($Version )
137
+
138
+ $v.Major | Should - Be $Major
139
+ $v.Minor | Should - Be $Minor
140
+ $v.Patch | Should - Be $Patch
141
+ $v.PreReleaseLabel | Should - Be $Label
142
+
143
+ if ($Revision )
144
+ {
145
+ $v.Revision | Should - Be $Revision
146
+ }
147
+ }
148
+
149
+ It " Does not create a PowerShellVersion from <Version>" - TestCases $versionCreationFailTests {
150
+ param ($Version )
151
+
152
+ { [Microsoft.PowerShell.CrossCompatibility.Utility.PowerShellVersion ]::Create($Version ) } | Should - Throw
153
+ }
154
+ }
30
155
}
0 commit comments