Skip to content

Commit

Permalink
made latest a special value in compare function
Browse files Browse the repository at this point in the history
  • Loading branch information
cmdjulian committed Aug 8, 2023
1 parent 0e708ff commit a81947d
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
38 changes: 38 additions & 0 deletions kirc-core/src/test/kotlin/de/cmdjulian/kirc/image/TagTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package de.cmdjulian.kirc.image

import io.kotest.matchers.comparables.shouldBeEqualComparingTo
import io.kotest.matchers.comparables.shouldBeGreaterThan
import io.kotest.matchers.comparables.shouldBeLessThan
import org.junit.jupiter.api.Test

class TagTest {
@Test
fun `should be equal for latest`() {
Tag.LATEST shouldBeEqualComparingTo Tag.LATEST
}

@Test
fun `should be equal`() {
Tag("1.2.0") shouldBeEqualComparingTo Tag("1.2.0")
}

@Test
fun `should be smaller`() {
Tag("1.2") shouldBeLessThan Tag("1.3")
}

@Test
fun `should be bigger`() {
Tag("1.3") shouldBeGreaterThan Tag("1.2")
}

@Test
fun `latest is always greater`() {
Tag.LATEST shouldBeGreaterThan Tag("99999999")
}

@Test
fun `nothing is greater than latest`() {
Tag("99999999") shouldBeLessThan Tag.LATEST
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@ class Tag(@JsonValue private val value: String) : Reference, Comparable<Tag> {
}

override val separator: Char get() = Companion.separator
override fun compareTo(other: Tag): Int = value.compareTo(value)

override fun compareTo(other: Tag): Int = when {
this == LATEST -> if (other == LATEST) 0 else 1
other == LATEST -> -1
else -> this.value.compareTo(other.value)
}

override fun equals(other: Any?): Boolean = other is Tag && other.value == value
override fun hashCode(): Int = value.hashCode()
Expand Down

0 comments on commit a81947d

Please sign in to comment.