-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Updated versioning to provide VersionCode instead of integer
That way comparisons through semver are possible to the point where all digits are actually in the constraints of java integers
- Loading branch information
Showing
5 changed files
with
131 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package dev.chainmail.taggy | ||
|
||
val characters = Regex("[a-zA-Z]+") | ||
val numbers = Regex("[0-9]+") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
src/main/kotlin/dev/chainmail/taggy/pattern/VersionCode.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package dev.chainmail.taggy.pattern | ||
|
||
import dev.chainmail.taggy.characters | ||
import kotlin.math.max | ||
|
||
class VersionCode( | ||
version: String, | ||
postfix: String? | ||
) : Comparable<VersionCode> { | ||
|
||
private val segments = version.splitToSequence(".") | ||
.map { it.replace(characters, "").toInt() } | ||
.toList() | ||
|
||
private val revision = postfix | ||
?.replace(characters, "") | ||
?.toIntOrNull() | ||
|
||
override operator fun compareTo(other: VersionCode): Int { | ||
if (this === other) { | ||
return 0 | ||
} | ||
|
||
val segmentCount = max( | ||
segments.size, | ||
other.segments.size | ||
) | ||
|
||
// loop through all the segments to establish which of these versions is higher | ||
for (i in 0 until segmentCount) { | ||
val my = segments.getOrElse(i) { 0 } | ||
val their = other.segments.getOrElse(i) { 0 } | ||
|
||
return when { | ||
my > their -> 1 | ||
my < their -> -1 | ||
else -> continue | ||
} | ||
} | ||
|
||
// if the versions are identical, then we check for revisions | ||
val my = revision ?: Int.MAX_VALUE | ||
val their = other.revision ?: Int.MAX_VALUE | ||
|
||
return my - their | ||
} | ||
|
||
override fun equals(other: Any?): Boolean { | ||
if (other !is VersionCode) return false | ||
return compareTo(other) == 0 | ||
} | ||
|
||
} |
71 changes: 71 additions & 0 deletions
71
src/test/kotlin/dev/chainmail/taggy/pattern/VersionCodeTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package dev.chainmail.taggy.pattern | ||
|
||
import org.junit.Test | ||
import kotlin.test.assertTrue | ||
|
||
class VersionCodeTest { | ||
|
||
private fun testComparison( | ||
old: String, | ||
new: String, | ||
postfix: String? = null, | ||
separator: String = "-" | ||
) { | ||
val contenderOld = Tag.from(old, postfix, separator) | ||
val contenderNew = Tag.from(new, postfix, separator) | ||
assertTrue("Codes do not properly handle semantic versioning. $new is newer than $old") { | ||
contenderNew.code > contenderOld.code | ||
} | ||
} | ||
|
||
@Test | ||
fun semanticComparison() { | ||
testComparison(old = "1.193.0", new = "2.0.0") | ||
} | ||
|
||
@Test | ||
fun semanticUnilateralPostfixComparison() { | ||
testComparison(old = "1.193.0-dev1", new = "1.193.0", postfix = "dev") | ||
} | ||
|
||
@Test | ||
fun semanticBilateralPostfixComparison() { | ||
testComparison(old = "1.193.0-dev1", new = "1.193.0-dev2", postfix = "dev") | ||
} | ||
|
||
@Test | ||
fun semanticUnilateralStagePostfixComparison() { | ||
testComparison(old = "1.193.0", new = "1.193.1-dev1", postfix = "dev") | ||
} | ||
|
||
// --- | ||
|
||
private fun testEquality( | ||
old: String, | ||
new: String, | ||
postfix: String? = null, | ||
separator: String = "-" | ||
) { | ||
val contenderOld = Tag.from(old, postfix, separator) | ||
val contenderNew = Tag.from(new, postfix, separator) | ||
assertTrue("Codes do not properly handle semantic versioning. $new is not equal to $old") { | ||
contenderNew.code == contenderOld.code | ||
} | ||
} | ||
|
||
@Test | ||
fun semanticEquality() { | ||
testEquality(old = "1.0.0", new = "1.0.0") | ||
} | ||
|
||
@Test(expected = AssertionError::class) | ||
fun semanticFailUnilateralPostfixEquality() { | ||
testEquality(old = "1.0.0-dev30", new = "1.0.0", postfix = "dev") | ||
} | ||
|
||
@Test(expected = AssertionError::class) | ||
fun semanticFailBilateralPostfixEquality() { | ||
testEquality(old = "1.0.0-dev1", new = "1.0.0-dev2", postfix = "dev") | ||
} | ||
|
||
} |
Empty file.