jvmTest.com.bkahlert.kommons.test.junit.ExtensionContextsKtTest.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of kommons-test Show documentation
Show all versions of kommons-test Show documentation
Kommons Test is a Kotlin Multiplatform Library to ease testing.
package com.bkahlert.kommons.test.junit
import com.bkahlert.kommons.test.test
import io.kotest.assertions.throwables.shouldThrow
import io.kotest.matchers.collections.shouldContainExactly
import io.kotest.matchers.should
import io.kotest.matchers.shouldBe
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.extension.ExtensionContext
import org.junit.jupiter.api.extension.ExtensionContextException
class ExtensionContextsKtTest {
@Test fun ancestors(extensionContext: ExtensionContext) = test {
extensionContext.ancestors.map { it.uniqueId }.shouldContainExactly(
listOf(
"[engine:junit-jupiter]",
"[class:com.bkahlert.kommons.test.junit.ExtensionContextsKtTest]",
).joinToString("/"),
listOf(
"[engine:junit-jupiter]",
).joinToString("/"),
)
}
@Test fun get_store(context: ExtensionContext) = test {
context.getStore() should {
it.get("key") shouldBe null
it.put("key", "value")
it.get("key") shouldBe "value"
}
context.getStore("additional-part") should {
it.get("key") shouldBe null
it.put("key", "value")
it.get("key") shouldBe "value"
}
context.getStore() should {
it.get("key") shouldBe null
}
}
@Test fun get_test_store(context: ExtensionContext) = test {
context.getStore().put("key", "value")
context.getTestStore() should {
it.get("key") shouldBe null
it.put("key", "value")
it.get("key") shouldBe "value"
}
context.getTestStore("additional-part") should {
it.get("key") shouldBe null
it.put("key", "value")
it.get("key") shouldBe "value"
}
context.getTestStore() should {
it.get("key") shouldBe null
}
}
@Test fun store_get_typed(context: ExtensionContext) = test {
context.getStore() should {
it.getTyped("key") shouldBe null
it.put("key", "value")
val value: String? = it.getTyped("key")
value shouldBe "value"
it.put("key", true)
shouldThrow { it.getTyped("key") }
}
}
@Test fun store_get_typed_or_default(context: ExtensionContext) = test {
context.getStore() should {
it.getTypedOrDefault("key", "default") shouldBe "default"
it.put("key", "value")
it.getTypedOrDefault("key", "default") shouldBe "value"
it.put("key", true)
shouldThrow { it.getTypedOrDefault("key", "default") }
}
}
@Test fun store_get_typed_or_compute_if_absent(context: ExtensionContext) = test {
context.getStore() should {
it.getTypedOrComputeIfAbsent("key") { "computed" } shouldBe "computed"
it.get("key") shouldBe "computed"
it.put("key", "value")
it.getTypedOrComputeIfAbsent("key") { "computed" } shouldBe "value"
it.put("key", true)
shouldThrow { it.getTypedOrComputeIfAbsent("key") { "computed" } }
}
}
@Test fun store_remove_typed(context: ExtensionContext) = test {
context.getStore() should {
it.put("key", "value")
val removed: String = it.removeTyped("key")
removed shouldBe "value"
it.get("key") shouldBe null
it.put("key", true)
shouldThrow { it.removeTyped("key") }
}
}
}
internal class Foo
internal class Bar