Skip to content

Commit

Permalink
Merge pull request #811 from imazen/master
Browse files Browse the repository at this point in the history
Add SemVerHelper.isValidSemVer(string)
  • Loading branch information
forki committed May 26, 2015
2 parents 36df89a + 8271642 commit ecc04a0
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/app/FakeLib/SemVerHelper.fs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,15 @@ type SemVerInfo =
0
| _ -> invalidArg "yobj" "cannot compare values of different types"


let private SemVerPattern = "^(?:0|[1-9][0-9]*)\.(?:0|[1-9][0-9]*)\.(?:0|[1-9][0-9]*)(?:-[\da-zA-Z\-]+(?:\.[\da-zA-Z\-]+)*)?(?:\+[\da-zA-Z\-]+(?:\.[\da-zA-Z\-]+)*)?$"

/// Returns true if input appears to be a parsable semver string
let isValidSemVer input =
let m = Regex.Match(input, SemVerPattern)
if m.Success then true
else false

/// Parses the given version string into a SemVerInfo which can be printed using ToString() or compared
/// according to the rules described in the [SemVer docs](http://semver.org/).
/// ## Sample
Expand All @@ -100,3 +109,6 @@ let parse version =
PreRelease = PreRelease.TryParse preRelease
Build = if l > 3 then splitted.[3] else ""
}



24 changes: 24 additions & 0 deletions src/test/Test.FAKECore/SemVerHelperSpecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,30 @@ public class when_parsing_semver_strings_and_printing_the_result
() => SemVerHelper.parse("1.2.3-foo").ToString().ShouldEqual("1.2.3-foo");
}

public class when_validating_semver_strings
{
It should_validate_0_1_2 =
() => SemVerHelper.isValidSemVer("0.1.2").ShouldEqual(true);

It should_validate_alpha_beta_versions =
() => SemVerHelper.isValidSemVer("1.0.0-alpha.beta").ShouldEqual(true);

It should_validate_prerelease_versions_without_build =
() => SemVerHelper.isValidSemVer("1.2.3-foo").ShouldEqual(true);

It should_reject_leading_zeros =
() => SemVerHelper.isValidSemVer("01.02.03").ShouldEqual(false);

It should_require_3_numbers =
() => SemVerHelper.isValidSemVer("1.2").ShouldEqual(false);

It should_reject_leading_v =
() => SemVerHelper.isValidSemVer("v1.2.0").ShouldEqual(false);


}


public class when_parsing_semver_strings
{
static SemVerHelper.SemVerInfo semVer;
Expand Down

0 comments on commit ecc04a0

Please sign in to comment.