Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Mirrors
biopet.biopet
Commits
4db54050
Commit
4db54050
authored
Oct 13, 2016
by
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
Changes
2
Hide whitespace changes
Inline
Side-by-side
biopet-utils/src/main/scala/nl/lumc/sasc/biopet/utils/package.scala
View file @
4db54050
...
...
@@ -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
}
}
}
biopet-utils/src/test/scala/nl/lumc/sasc/biopet/utils/UtilsTest.scala
0 → 100644
View file @
4db54050
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"
)
}
}
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment