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

Merge branch 'feature-convenience-version' into 'develop'

semantic version convenience methods

See #387

See merge request !459
parents 2727f142 950fe55b
......@@ -64,4 +64,30 @@ package object utils {
case _ if fallBack => Try(raw)
case _ => Try(throw new Exception(s"Can not extract number from string $raw"))
}
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 = getSemanticVersion(version).isDefined
case class SemanticVersion(major: Int, minor: Int, patch: Int, build: Option[String] = None)
/**
* Check whether a version string is a semantic version.
* Note: the toInt calls here are only safe because the regex only matches numbers
*
* @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
}
}
}
package nl.lumc.sasc.biopet.utils
import org.scalatest.Matchers
import org.scalatest.testng.TestNGSuite
import org.testng.annotations.Test
/**
* Created by Sander Bollen on 12-10-16.
*/
class UtilsTest extends TestNGSuite with Matchers {
val semanticVersion = "1.2.3"
val semanticVersionWithBuild = "1.2.3-alpha0.1"
val nonSemanticVersion = "v1222.1"
@Test
def testIsSemantic() = {
isSemanticVersion(semanticVersion) shouldBe true
isSemanticVersion(semanticVersionWithBuild) shouldBe true
isSemanticVersion(nonSemanticVersion) shouldBe false
}
@Test
def testMajorVersion() = {
getSemanticVersion(semanticVersion).map(_.major) shouldBe Some(1)
getSemanticVersion(semanticVersionWithBuild).map(_.major) shouldBe Some(1)
}
@Test
def testMinorVersion() = {
getSemanticVersion(semanticVersion).map(_.minor) shouldBe Some(2)
getSemanticVersion(semanticVersionWithBuild).map(_.minor) shouldBe Some(2)
}
@Test
def testPatchVersion() = {
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