File tree Expand file tree Collapse file tree 3 files changed +36
-2
lines changed
Expand file tree Collapse file tree 3 files changed +36
-2
lines changed Original file line number Diff line number Diff line change 22 # region Static properties
33 hidden static [string ] $PSSemVerPattern = ' ^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)' +
44 ' (?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$'
5- hidden static [string ] $LoosePSSemVerPattern = ' ^(?:([a-zA-Z]*)-?)?(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)' +
5+ hidden static [string ] $LoosePSSemVerPattern = ' ^(?:([a-zA-Z]*)-?)?(0|[1-9]\d*)(?: \.(0|[1-9]\d*))?(?: \.(0|[1-9]\d*))? ' +
66 ' (?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$'
77 # endregion Static properties
88
109109 $this.Minor = if ($version.Minor -lt 0 ) { 0 } else { $version.Minor }
110110 $this.Patch = if ($version.Build -lt 0 ) { 0 } else { $version.Build }
111111 }
112+
113+ PSSemVer([string ]$version , [string ]$PreReleaseLabel , [string ]$BuildLabel ) {
114+ if ($version -match [PSSemVer ]::PSSemVerPattern) {
115+ $this.Major = [int ]$Matches [1 ]
116+ $this.Minor = [int ]$Matches [2 ]
117+ $this.Patch = [int ]$Matches [3 ]
118+ $this.Prerelease = $PreReleaseLabel
119+ $this.BuildMetadata = $BuildLabel
120+ } else {
121+ throw [ArgumentException ]::new(' The version string is not a valid SemVer string' )
122+ }
123+ }
112124 # endregion Constructors
113125
114126 # region Methods
Original file line number Diff line number Diff line change 4848
4949 # The prerelease version.
5050 [Parameter (ParameterSetName = ' Values' )]
51+ [Parameter (ParameterSetName = ' String' )]
5152 [Alias (' PreReleaseLabel' )]
5253 [string ] $Prerelease = ' ' ,
5354
5455 # The build metadata.
5556 [Parameter (ParameterSetName = ' Values' )]
57+ [Parameter (ParameterSetName = ' String' )]
5658 [Alias (' Build' , ' BuildLabel' )]
5759 [string ] $BuildMetadata = ' ' ,
5860
7375 if ([string ]::IsNullOrEmpty($Version )) {
7476 $Version = ' 0.0.0'
7577 }
76- return [PSSemVer ]::New($Version )
78+ return [PSSemVer ]::New($Version , $Prerelease , $BuildMetadata )
7779 }
7880 }
7981}
Original file line number Diff line number Diff line change 4343 $PSSemVer.Prerelease | Should - BeNullOrEmpty
4444 $PSSemVer.BuildMetadata | Should - BeNullOrEmpty
4545 }
46+ It " New-PSSemVer -Version '1.0.0' -PreRelease 'alpha' -Build '2020-01-01'" {
47+ $PSSemVer = New-PSSemVer - Version ' 1.0.0' - Prerelease ' alpha' - Build ' 2020-01-01'
48+ $PSSemVer.Major | Should - Be 1
49+ $PSSemVer.Minor | Should - Be 0
50+ $PSSemVer.Patch | Should - Be 0
51+ $PSSemVer.Prerelease | Should - Be ' alpha'
52+ $PSSemVer.BuildMetadata | Should - Be ' 2020-01-01'
53+ }
4654 }
4755
4856 Describe ' Function: ConvertTo-PSSemVer' {
138146 It " Throws on [PSSemVer]'1.0.0-beta!1'" {
139147 { [PSSemVer ]' 1.0.0-beta!1' } | Should - Throw
140148 }
149+ It " [PSSemVer]'1.2' => '1.2.0'" {
150+ $PSSemVer = [PSSemVer ]' 1.2'
151+ $PSSemVer.Major | Should - Be 1
152+ $PSSemVer.Minor | Should - Be 2
153+ $PSSemVer.Patch | Should - Be 0
154+ }
155+ It " [PSSemVer]'2' => '2.0.0'" {
156+ $PSSemVer = [PSSemVer ]' 2'
157+ $PSSemVer.Major | Should - Be 2
158+ $PSSemVer.Minor | Should - Be 0
159+ $PSSemVer.Patch | Should - Be 0
160+ }
141161 }
142162
143163 Describe ' Class: ToString()' {
You can’t perform that action at this time.
0 commit comments