-
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.
chore(common-enums): add
ValueEnum
tests
- Loading branch information
1 parent
0abf688
commit a5ba8ed
Showing
1 changed file
with
101 additions
and
0 deletions.
There are no files selected for viewing
101 changes: 101 additions & 0 deletions
101
...c/commonTest/kotlin/io/github/edricchan03/androidx/common/enums/ValueEnumFromValueTest.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,101 @@ | ||
package io.github.edricchan03.androidx.common.enums | ||
|
||
import io.github.edricchan03.androidx.common.enums.impl.ValueEnumFromValue | ||
import io.kotest.assertions.throwables.shouldThrowExactlyUnit | ||
import io.kotest.core.spec.style.DescribeSpec | ||
import io.kotest.matchers.equals.shouldBeEqual | ||
import io.kotest.matchers.nulls.shouldBeNull | ||
import io.kotest.matchers.nulls.shouldNotBeNull | ||
import io.kotest.property.Arb | ||
import io.kotest.property.Exhaustive | ||
import io.kotest.property.arbitrary.filterNot | ||
import io.kotest.property.arbitrary.string | ||
import io.kotest.property.checkAll | ||
import io.kotest.property.exhaustive.collection | ||
|
||
private enum class ExampleValue(override val value: String) : ValueEnum<String> { | ||
One("custom"), | ||
Two("custom-2"), | ||
Three("custom-3"), | ||
Custom("abc"), | ||
Custom2("def"); | ||
|
||
@OptIn(ExperimentalEnumsApi::class) | ||
companion object : ValueEnumFromValue<String, ExampleValue>(Custom2, entries) | ||
} | ||
|
||
private val default = ExampleValue.Custom2 | ||
|
||
private val exampleEntries = ExampleValue.entries.associateBy { it.value } | ||
|
||
private val Arb.Companion.nonEntries get() = string().filterNot { it in exampleEntries } | ||
|
||
class ValueEnumFromValueTest : DescribeSpec({ | ||
describe("fromValueOrNull") { | ||
it("should return null respectively") { | ||
checkAll(Exhaustive.collection(exampleEntries.entries)) { (key, value) -> | ||
val result = ExampleValue.fromValueOrNull(key).shouldNotBeNull() | ||
result shouldBeEqual value | ||
} | ||
|
||
checkAll(Arb.nonEntries) { | ||
ExampleValue.fromValueOrNull(it).shouldBeNull() | ||
} | ||
} | ||
} | ||
|
||
describe("fromValue") { | ||
it("should return the default value") { | ||
checkAll(Exhaustive.collection(exampleEntries.keys)) { | ||
val value = ExampleValue.fromValue(it) | ||
value shouldBeEqual ExampleValue.fromValueOrNull(it).shouldNotBeNull() | ||
} | ||
|
||
checkAll(Arb.nonEntries) { | ||
val value = ExampleValue.fromValue(it) | ||
value shouldBeEqual default | ||
} | ||
} | ||
} | ||
|
||
describe("fromValueOrElse") { | ||
it("should return the default value") { | ||
checkAll(Exhaustive.collection(exampleEntries.keys)) { | ||
val value = ExampleValue.fromValueOrElse(it, ExampleValue.Custom) | ||
value shouldBeEqual ExampleValue.fromValueOrNull(it).shouldNotBeNull() | ||
} | ||
|
||
checkAll(Arb.nonEntries) { | ||
val value = ExampleValue.fromValueOrElse(it, ExampleValue.Custom) | ||
value shouldBeEqual ExampleValue.Custom | ||
} | ||
} | ||
} | ||
|
||
describe("requireValue") { | ||
it("should throw when the value does not exist") { | ||
checkAll(Exhaustive.collection(exampleEntries.keys)) { | ||
val value = ExampleValue.requireValue(it) | ||
value shouldBeEqual requireNotNull(ExampleValue.fromValueOrNull(it)) | ||
} | ||
|
||
checkAll(Arb.nonEntries) { | ||
shouldThrowExactlyUnit<IllegalArgumentException> { | ||
ExampleValue.requireValue(it) | ||
} | ||
} | ||
} | ||
} | ||
|
||
describe("hasValue") { | ||
it("should return whether the value exists") { | ||
checkAll(Exhaustive.collection(exampleEntries.keys)) { | ||
ExampleValue.hasValue(it) shouldBeEqual true | ||
} | ||
|
||
checkAll(Arb.nonEntries) { | ||
ExampleValue.hasValue(it) shouldBeEqual false | ||
} | ||
} | ||
} | ||
}) |