From 2b224e220091ef3c8ed0446c4493f0d4c3a9541e Mon Sep 17 00:00:00 2001 From: Kamil Doległo Date: Thu, 21 Jan 2021 14:22:10 +0100 Subject: Add tests for nested classes in Kotlin as Java --- .../src/test/kotlin/DRITranslationTest.kt | 125 +++++++++++++++++++++ 1 file changed, 125 insertions(+) create mode 100644 plugins/kotlin-as-java/src/test/kotlin/DRITranslationTest.kt (limited to 'plugins/kotlin-as-java/src') diff --git a/plugins/kotlin-as-java/src/test/kotlin/DRITranslationTest.kt b/plugins/kotlin-as-java/src/test/kotlin/DRITranslationTest.kt new file mode 100644 index 00000000..257b17ff --- /dev/null +++ b/plugins/kotlin-as-java/src/test/kotlin/DRITranslationTest.kt @@ -0,0 +1,125 @@ +package kotlinAsJavaPlugin + + +import org.jetbrains.dokka.base.testApi.testRunner.BaseAbstractTest +import org.jetbrains.dokka.model.DClass +import org.jetbrains.dokka.model.DEnum +import org.junit.jupiter.api.Assertions.assertTrue +import org.junit.jupiter.api.Test + +class DRITranslationTest : BaseAbstractTest() { + val configuration = dokkaConfiguration { + sourceSets { + sourceSet { + sourceRoots = listOf("src/") + classpath += jvmStdlibPath!! + } + } + } + + @Test + fun `should correctly handle nested classes`() { + testInline( + """ + |/src/main/kotlin/kotlinAsJavaPlugin/sample.kt + |package kotlinAsJavaPlugin + |class A { + | class B(val x: String) + |} + |class C { + | class B(val x: String) + |} + """.trimMargin(), + configuration, + ) { + documentablesTransformationStage = { module -> + val nestedClasslikesDRIs = module.packages.flatMap { it.classlikes }.flatMap { it.classlikes }.map { it.dri } + val driRegex = "[AC]\\.B".toRegex() + + nestedClasslikesDRIs.forEach { dri -> + assertTrue(driRegex.matches(dri.classNames.toString())) + } + } + } + } + + @Test + fun `should correctly handle interface methods`() { + testInline( + """ + |/src/main/kotlin/kotlinAsJavaPlugin/sample.kt + |package kotlinAsJavaPlugin + |interface A { + | fun b() + |} + """.trimMargin(), + configuration, + ) { + documentablesTransformationStage = { module -> + val nestedFunctionDRI = module.packages.flatMap { it.classlikes }.flatMap { it.functions }.filter { it.name == "b" }.map { it.dri }.single() + + assertTrue(nestedFunctionDRI.classNames == "A") + } + } + } + + @Test + fun `should correctly handle object methods`() { + testInline( + """ + |/src/main/kotlin/kotlinAsJavaPlugin/sample.kt + |package kotlinAsJavaPlugin + |object A { + | fun b() {} + |} + """.trimMargin(), + configuration, + ) { + documentablesTransformationStage = { module -> + val nestedFunctionDRI = module.packages.flatMap { it.classlikes }.flatMap { it.functions }.filter { it.name == "b" }.map { it.dri }.single() + + assertTrue(nestedFunctionDRI.classNames == "A") + } + } + } + + @Test + fun `should correctly handle enum functions`() { + testInline( + """ + |/src/main/kotlin/kotlinAsJavaPlugin/sample.kt + |package kotlinAsJavaPlugin + |enum class A(private val x: Int) { + | X(0); + | fun b() = x + |} + """.trimMargin(), + configuration, + ) { + documentablesTransformationStage = { module -> + val nestedFunctionDRI = (module.packages.single().classlikes.single() as DEnum).functions.filter { it.name == "b" }.map { it.dri }.single() + + assertTrue(nestedFunctionDRI.classNames == "A") + } + } + } + + @Test + fun `should correctly handle nested classes' constructors`() { + testInline( + """ + |/src/main/kotlin/kotlinAsJavaPlugin/sample.kt + |package kotlinAsJavaPlugin + |class A { + | class B(val x: String) + |} + """.trimMargin(), + configuration, + ) { + documentablesTransformationStage = { module -> + val constructorDRI = (module.packages.flatMap { it.classlikes }.flatMap { it.classlikes }.single() as DClass).constructors.single().dri + assertTrue(constructorDRI.classNames == "A.B") + } + } + } +} -- cgit