Commit 4c0e7a8b authored by Peter van 't Hof's avatar Peter van 't Hof
Browse files

Simplify regex fetching

parent 5620069f
......@@ -68,82 +68,26 @@ package object utils {
val semanticVersionRegex = "(\\d+)\\.(\\d+)\\.(\\d+)(-.*)?".r
/**
* Check whether a version string is a semantic version.
*
* @param version version string
* @return boolean
*/
def isSemanticVersion(version: String): Boolean = {
version.matches(semanticVersionRegex.toString)
}
/**
* Get major number from a semantic version
*
* @param version version string
* @return
*/
def majorVersion(version: String): Option[Int] = {
if (!isSemanticVersion(version)) {
None
} else {
semanticVersionRegex.findFirstMatchIn(version) match {
case Some(matcher) => tryToParseNumber(matcher.group(1)) match {
case Success(s) => s match {
case i: Int => Some(i)
case _ => None
}
case _ => None
}
case _ => None
}
}
}
/**
* Get minor number from a semantic version
*
* @param version version string
* @return
*/
def minorVersion(version: String): Option[Int] = {
if (!isSemanticVersion(version)) {
None
} else {
semanticVersionRegex.findFirstMatchIn(version) match {
case Some(matcher) => tryToParseNumber(matcher.group(2)) match {
case Success(s) => s match {
case i: Int => Some(i)
case _ => None
}
case _ => None
}
case _ => None
}
}
}
* Check whether a version string is a semantic version.
*
* @param version version string
* @return boolean
*/
def isSemanticVersion(version: String): Boolean = getSemanticVersion(version).isDefined
case class SemanticVersion(major: Int, minor: Int, patch: Int, build: Option[String] = None)
/**
* Get patch number from a semantic version
*
* @param version version string
* @return
*/
def patchVersion(version: String): Option[Int] = {
if (!isSemanticVersion(version)) {
None
} else {
semanticVersionRegex.findFirstMatchIn(version) match {
case Some(matcher) => tryToParseNumber(matcher.group(3)) match {
case Success(s) => s match {
case i: Int => Some(i)
case _ => None
}
case _ => None
}
case _ => None
}
* Check whether a version string is a semantic version.
*
* @param version version string
* @return SemanticVersion case class
*/
def getSemanticVersion(version: String) = {
version match {
case semanticVersionRegex(major, minor, patch, build) =>
Some(SemanticVersion(major.toInt, minor.toInt, patch.toInt, Option(build).map(x => x.stripPrefix("-"))))
case _ => None
}
}
}
......@@ -5,8 +5,8 @@ import org.scalatest.testng.TestNGSuite
import org.testng.annotations.Test
/**
* Created by Sander Bollen on 12-10-16.
*/
* Created by Sander Bollen on 12-10-16.
*/
class UtilsTest extends TestNGSuite with Matchers {
val semanticVersion = "1.2.3"
......@@ -22,20 +22,25 @@ class UtilsTest extends TestNGSuite with Matchers {
@Test
def testMajorVersion() = {
majorVersion(semanticVersion) shouldBe Some(1)
majorVersion(semanticVersionWithBuild) shouldBe Some(1)
getSemanticVersion(semanticVersion).map(_.major) shouldBe Some(1)
getSemanticVersion(semanticVersionWithBuild).map(_.major) shouldBe Some(1)
}
@Test
def testMinorVersion() = {
minorVersion(semanticVersion) shouldBe Some(2)
minorVersion(semanticVersionWithBuild) shouldBe Some(2)
getSemanticVersion(semanticVersion).map(_.minor) shouldBe Some(2)
getSemanticVersion(semanticVersionWithBuild).map(_.minor) shouldBe Some(2)
}
@Test
def testPatchVersion() = {
patchVersion(semanticVersion) shouldBe Some(3)
patchVersion(semanticVersionWithBuild) shouldBe Some(3)
getSemanticVersion(semanticVersion).map(_.patch) shouldBe Some(3)
getSemanticVersion(semanticVersionWithBuild).map(_.patch) shouldBe Some(3)
}
@Test
def testBuildVersion() = {
getSemanticVersion(semanticVersion).flatMap(_.build) shouldBe None
getSemanticVersion(semanticVersionWithBuild).flatMap(_.build) shouldBe Some("alpha0.1")
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment