From 6581c140728659a6371133b4f36da2d10fa1716f Mon Sep 17 00:00:00 2001 From: Andrew Golovashevich Date: Wed, 26 Mar 2025 01:50:35 +0300 Subject: [PATCH] Tests to check the existence of kotlin-stdlib in runtime --- build.gradle.kts | 1 + gradle.properties | 6 +- .../tests/KotlinStdlibDependencyTest.kt | 130 ++++++++++++++++++ 3 files changed, 136 insertions(+), 1 deletion(-) create mode 100644 src/jvmTest/kotlin/ru/landrafhomyak/utility/reference_counter/tests/KotlinStdlibDependencyTest.kt diff --git a/build.gradle.kts b/build.gradle.kts index 5e18de0..d3df51e 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -68,6 +68,7 @@ xomrk { } } val jvmMain by getting { + dependsOn(commonMain) dependencies { compileOnly(kotlinStdlibDependency) } diff --git a/gradle.properties b/gradle.properties index 7fc6f1f..3ecb710 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1 +1,5 @@ -kotlin.code.style=official +kotlin.stdlib.default.dependency=false +kotlin.mpp.applyDefaultHierarchyTemplate=false +kotlin.native.enableKlibsCrossCompilation=true +# compileOnly dependencies from commonMain still throw warning +kotlin.suppressGradlePluginWarnings=IncorrectCompileOnlyDependencyWarning \ No newline at end of file diff --git a/src/jvmTest/kotlin/ru/landrafhomyak/utility/reference_counter/tests/KotlinStdlibDependencyTest.kt b/src/jvmTest/kotlin/ru/landrafhomyak/utility/reference_counter/tests/KotlinStdlibDependencyTest.kt new file mode 100644 index 0000000..be9cb9c --- /dev/null +++ b/src/jvmTest/kotlin/ru/landrafhomyak/utility/reference_counter/tests/KotlinStdlibDependencyTest.kt @@ -0,0 +1,130 @@ +package ru.landrafhomyak.utility.reference_counter.tests + +import java.lang.AssertionError +import org.testng.annotations.Test +import org.testng.asserts.Assertion +import ru.landgrafhomyak.utility.highlevel_try_finally.safeAutoClose1 +import ru.landrafhomyak.utility.reference_counter.CloseableReferenceCounter +import ru.landrafhomyak.utility.reference_counter.CloseableReferenceCounter_Debug + +@Test +class KotlinStdlibDependencyTest { + @Test + fun testNoKotlinStdlib() { + try { + if (KotlinVersion.CURRENT.major != -1) + throw AssertionError("Kotlin stdlib still in runtime classpath") + } catch (_: LinkageError) { + } + } + + private class CustomTestException : RuntimeException() + + private object CustomUnit + + private fun throw7throwFn() { + safeAutoClose1(finally = { throw CustomTestException() }, action = { throw CustomTestException() }) + } + + @Suppress("NOTHING_TO_INLINE") + private inline fun throw7throwIn() { + safeAutoClose1(finally = { throw CustomTestException() }, action = { throw CustomTestException() }) + } + + @Test(dependsOnMethods = ["testNoKotlinStdlib"]) + fun testIncrefDecrefClose() { + try { + val refcnt = CloseableReferenceCounter("") + refcnt.incref() + refcnt.decref() + refcnt.incref() + refcnt.incref() + refcnt.decref() + refcnt.decref() + refcnt.close("") + } catch (le: LinkageError) { + throw AssertionError("CloseableReferenceCounter still has dependency on kotlin stdlib", le) + } + } + + @OptIn(CloseableReferenceCounter_Debug.RequiresExplicitDebug::class) + @Test(dependsOnMethods = ["testNoKotlinStdlib"]) + fun testIncrefDecrefClose_Debug() { + try { + val refcnt = CloseableReferenceCounter_Debug("", "") + refcnt.incref() + refcnt.decref() + refcnt.incref() + refcnt.incref() + refcnt.decref() + refcnt.decref() + refcnt.close("") + } catch (le: LinkageError) { + throw AssertionError("CloseableReferenceCounter_Debug still has dependency on kotlin stdlib", le) + } + } + + @Test(dependsOnMethods = ["testNoKotlinStdlib", "testIncrefDecrefClose"]) + fun testWithRef() { + try { + val refcnt = CloseableReferenceCounter("") + refcnt.withRef { + return@withRef CustomUnit + } + refcnt.withRef w1@{ + return@w1 refcnt.withRef w2@{ + return@w2 CustomUnit + } + } + refcnt.close("") + } catch (le: LinkageError) { + throw AssertionError("CloseableReferenceCounter still has dependency on kotlin stdlib", le) + } + } + + @OptIn(CloseableReferenceCounter_Debug.RequiresExplicitDebug::class) + @Test(dependsOnMethods = ["testNoKotlinStdlib", "testIncrefDecrefClose_Debug"]) + fun testWithRef_Debug() { + try { + val refcnt = CloseableReferenceCounter_Debug("", "") + refcnt.withRef { + return@withRef CustomUnit + } + refcnt.withRef w1@{ + return@w1 refcnt.withRef w2@{ + return@w2 CustomUnit + } + } + refcnt.close("") + } catch (le: LinkageError) { + throw AssertionError("CloseableReferenceCounter_Debug still has dependency on kotlin stdlib", le) + } + } + + @Test(dependsOnMethods = ["testNoKotlinStdlib", "testIncrefDecrefClose", "testWithRef"]) + fun testWithRef_Err() { + try { + val refcnt = CloseableReferenceCounter("") + refcnt.withRef { + throw CustomTestException() + } + } catch (_: CustomTestException) { + } catch (le: LinkageError) { + throw AssertionError("CloseableReferenceCounter still has dependency on kotlin stdlib", le) + } + } + + @OptIn(CloseableReferenceCounter_Debug.RequiresExplicitDebug::class) + @Test(dependsOnMethods = ["testNoKotlinStdlib", "testIncrefDecrefClose_Debug", "testWithRef_Debug"]) + fun testWithRef_ErrDebug() { + try { + val refcnt = CloseableReferenceCounter_Debug("", "") + refcnt.withRef { + throw CustomTestException() + } + } catch (_: CustomTestException) { + } catch (le: LinkageError) { + throw AssertionError("CloseableReferenceCounter_Debug still has dependency on kotlin stdlib", le) + } + } +} \ No newline at end of file