From f423d558b70dfe8930259efe92fadf721ab55631 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=B5=9C=EB=AF=BC=EA=B7=9C?= Date: Tue, 5 Nov 2024 22:05:38 +0900 Subject: [PATCH] test: added common test cases for cache4k (#3501) --- .../core/arrow-cache4k/build.gradle.kts | 15 ++++++++++- .../kotlin/arrow/core/Cache4kTest.kt | 25 +++++++++++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 arrow-libs/core/arrow-cache4k/src/commonTest/kotlin/arrow/core/Cache4kTest.kt diff --git a/arrow-libs/core/arrow-cache4k/build.gradle.kts b/arrow-libs/core/arrow-cache4k/build.gradle.kts index f73ae851bd2..fdd073733a0 100644 --- a/arrow-libs/core/arrow-cache4k/build.gradle.kts +++ b/arrow-libs/core/arrow-cache4k/build.gradle.kts @@ -2,6 +2,7 @@ import org.jetbrains.kotlin.gradle.ExperimentalKotlinGradlePluginApi import org.jetbrains.kotlin.gradle.dsl.KotlinVersion +import org.jetbrains.kotlin.gradle.tasks.KotlinCompile plugins { @@ -15,7 +16,7 @@ apply(from = property("ANIMALSNIFFER_MPP")) java { toolchain { - languageVersion.set(JavaLanguageVersion.of(8)) + languageVersion.set(JavaLanguageVersion.of(17)) } } @@ -29,6 +30,13 @@ kotlin { api(libs.cache4k) } } + commonTest { + dependencies { + implementation(libs.kotlin.test) + implementation(libs.kotest.assertionsCore) + implementation(libs.coroutines.test) + } + } } jvm { @@ -75,3 +83,8 @@ tasks.named("jvmJar").configure { attributes["Automatic-Module-Name"] = "arrow.cache4k" } } + +// enables context receivers for Jvm Tests +tasks.named("compileTestKotlinJvm") { + compilerOptions.freeCompilerArgs.add("-Xcontext-receivers") +} diff --git a/arrow-libs/core/arrow-cache4k/src/commonTest/kotlin/arrow/core/Cache4kTest.kt b/arrow-libs/core/arrow-cache4k/src/commonTest/kotlin/arrow/core/Cache4kTest.kt new file mode 100644 index 00000000000..3e0277d4262 --- /dev/null +++ b/arrow-libs/core/arrow-cache4k/src/commonTest/kotlin/arrow/core/Cache4kTest.kt @@ -0,0 +1,25 @@ +package arrow.core + +import io.kotest.matchers.shouldBe +import kotlinx.coroutines.test.runTest +import kotlin.test.Test +import kotlin.time.Duration + +class Cache4kTest { + + @Test fun cacheShouldReturnSavedValueByKeyCorrectly() = runTest { + val cache = Cache4kMemoizationCache(buildCache4K { expireAfterAccess(Duration.INFINITE) }) + val expectedKey = "user:age:userId:5" + val expectedValue = 32 + + cache.set(expectedKey, expectedValue) + cache.get(expectedKey) shouldBe expectedValue + } + + @Test fun cacheShouldReturnNullCauseTryGetByUnsavedKey() = runTest { + val cache = Cache4kMemoizationCache(buildCache4K { expireAfterAccess(Duration.INFINITE) }) + val expectedKey = "user:name:userId:5" + + cache.get(expectedKey) shouldBe null + } +}