Skip to content

Instantly share code, notes, and snippets.

@android10
Last active November 27, 2020 12:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save android10/dbd837141e25390c52cbf3b3f9cbb5a6 to your computer and use it in GitHub Desktop.
Save android10/dbd837141e25390c52cbf3b3f9cbb5a6 to your computer and use it in GitHub Desktop.
Android Version Code Generator
//Code
package com.fernandocejas.android
import java.time.*
class Versionizer(private val localDateTime: LocalDateTime = LocalDateTime.now()) {
val versionCode = generateVersionCode()
private fun generateVersionCode(): Int {
val timeInSeconds = localDateTime.atZone(ZoneOffset.UTC).toEpochSecond()
return (timeInSeconds.toInt()..MAX_VERSION_CODE_ALLOWED).first()
}
companion object {
//This is Google Play Max Version Code allowed
//https://developer.android.com/studio/publish/versioning
const val MAX_VERSION_CODE_ALLOWED = 2100000000
}
}
//Tests
package com.fernandocejas.android
import org.amshove.kluent.shouldBeGreaterThan
import org.amshove.kluent.shouldBeLessThan
import org.junit.Test
import java.time.LocalDateTime
class VersionizerTest: UnitTest() {
@Test
fun `given version generator when I generate a new version NOW then I should get an incremented version number`() {
val versionCodeOld = Versionizer(LocalDateTime.now().minusSeconds(10)).versionCode
val versionCodeNew = Versionizer().versionCode
assertVersionCodes(versionCodeOld, versionCodeNew)
}
@Test
fun `given version generator when I generate a new version TOMORROW then I should get an incremented version number`() {
val oldVersionCode = Versionizer().versionCode
val newVersionCode = Versionizer(LocalDateTime.now().plusDays(1)).versionCode
assertVersionCodes(oldVersionCode, newVersionCode)
}
@Test
fun `given version generator when I generate a new version IN ONE YEAR then I should get an incremented version number`() {
val oldVersionCode = Versionizer().versionCode
val newVersionCode = Versionizer(LocalDateTime.now().plusYears(1)).versionCode
assertVersionCodes(oldVersionCode, newVersionCode)
}
@Test
fun `given version generator when I generate a new version IN ONE and TWO YEARS then I should get an incremented version number`() {
val now = LocalDateTime.now()
val oldVersionCode = Versionizer(now.plusYears(1)).versionCode
val newVersionCode = Versionizer(now.plusYears(2)).versionCode
assertVersionCodes(oldVersionCode, newVersionCode)
}
@Test
fun `given version generator when I generate a new version IN TEN YEARS then I should get an incremented version number`() {
val oldVersionCode = Versionizer().versionCode
val newVersionCode = Versionizer(LocalDateTime.now().plusYears(10)).versionCode
assertVersionCodes(oldVersionCode, newVersionCode)
}
private fun assertVersionCodes(oldVersionCode: Int, newVersionCode: Int) {
oldVersionCode shouldBeGreaterThan 0
newVersionCode shouldBeGreaterThan 0
oldVersionCode shouldBeLessThan newVersionCode
oldVersionCode shouldBeLessThan Versionizer.MAX_VERSION_CODE_ALLOWED
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment