diff options
author | Andrey Tyrin <andrei.tyrin@jetbrains.com> | 2023-01-12 20:03:06 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-01-12 20:03:06 +0100 |
commit | 303c937a7c33fa9df5c28079c423ee071e87e410 (patch) | |
tree | 6849bd6114d2711fe49be4d9f06a7fc089399c38 /plugins/base/src/test | |
parent | 8d1536bdbadbab7e1b89902b0cc920c36a6103ad (diff) | |
download | dokka-303c937a7c33fa9df5c28079c423ee071e87e410.tar.gz dokka-303c937a7c33fa9df5c28079c423ee071e87e410.tar.bz2 dokka-303c937a7c33fa9df5c28079c423ee071e87e410.zip |
Default Java constructor (#2795)
Diffstat (limited to 'plugins/base/src/test')
4 files changed, 135 insertions, 24 deletions
diff --git a/plugins/base/src/test/kotlin/model/JavaTest.kt b/plugins/base/src/test/kotlin/model/JavaTest.kt index 47a25943..b3ee2726 100644 --- a/plugins/base/src/test/kotlin/model/JavaTest.kt +++ b/plugins/base/src/test/kotlin/model/JavaTest.kt @@ -13,7 +13,6 @@ import utils.AbstractModelTest import utils.assertNotNull import utils.name import kotlin.test.assertEquals -import org.jetbrains.dokka.links.Callable as DRICallable class JavaTest : AbstractModelTest("/src/main/kotlin/java/Test.java", "java") { val configuration = dokkaConfiguration { @@ -48,7 +47,7 @@ class JavaTest : AbstractModelTest("/src/main/kotlin/java/Test.java", "java") { ) { with((this / "java" / "Test").cast<DClass>()) { name equals "Test" - children counts 1 + children counts 2 // default constructor and function with((this / "fn").cast<DFunction>()) { name equals "fn" val params = parameters.map { it.documentation.values.first().children.first() as Param } @@ -118,7 +117,7 @@ class JavaTest : AbstractModelTest("/src/main/kotlin/java/Test.java", "java") { ) { with((this / "java" / "Test").cast<DClass>()) { name equals "Test" - children counts 1 + children counts 2 // default constructor and function with((this / "arrayToString").cast<DFunction>()) { name equals "arrayToString" @@ -219,10 +218,10 @@ class JavaTest : AbstractModelTest("/src/main/kotlin/java/Test.java", "java") { """, configuration = configuration ) { with((this / "java" / "InnerClass").cast<DClass>()) { - children counts 1 + children counts 2 // default constructor and inner class with((this / "D").cast<DClass>()) { name equals "D" - children counts 0 + children counts 1 // default constructor } } } @@ -239,7 +238,7 @@ class JavaTest : AbstractModelTest("/src/main/kotlin/java/Test.java", "java") { ) { with((this / "java" / "Foo").cast<DClass>()) { name equals "Foo" - children counts 1 + children counts 2 // default constructor and function with((this / "bar").cast<DFunction>()) { name equals "bar" @@ -263,7 +262,7 @@ class JavaTest : AbstractModelTest("/src/main/kotlin/java/Test.java", "java") { """, configuration = configuration ) { with((this / "java" / "Test").cast<DClass>()) { - children counts 2 + children counts 3 // default constructor + 2 props with((this / "i").cast<DProperty>()) { getter equals null diff --git a/plugins/base/src/test/kotlin/signatures/InheritedAccessorsSignatureTest.kt b/plugins/base/src/test/kotlin/signatures/InheritedAccessorsSignatureTest.kt index a75c00fc..49a70f1c 100644 --- a/plugins/base/src/test/kotlin/signatures/InheritedAccessorsSignatureTest.kt +++ b/plugins/base/src/test/kotlin/signatures/InheritedAccessorsSignatureTest.kt @@ -62,11 +62,11 @@ class InheritedAccessorsSignatureTest : BaseAbstractTest() { writerPlugin.writer.renderedContent("root/test/-a/index.html").let { javaClassContent -> val signatures = javaClassContent.signature().toList() assertEquals( - 2, signatures.size, - "Expected 2 signatures: class signature and property" + 3, signatures.size, + "Expected 3 signatures: class signature, default constructor and property" ) - val property = signatures[1] + val property = signatures[2] property.match( "open var ", A("a"), ":", A("Int"), ignoreSpanWithTokenStyle = true @@ -109,9 +109,13 @@ class InheritedAccessorsSignatureTest : BaseAbstractTest() { writerPlugin.writer.renderedContent("root/test/-a/index.html").let { javaClassContent -> val signatures = javaClassContent.signature().toList() - assertEquals(2, signatures.size, "Expected 2 signatures: class signature and property") + assertEquals( + 3, + signatures.size, + "Expected 3 signatures: class signature, default constructor and property" + ) - val property = signatures[1] + val property = signatures[2] property.match( "open val ", A("a"), ":", A("Int"), ignoreSpanWithTokenStyle = true @@ -156,9 +160,13 @@ class InheritedAccessorsSignatureTest : BaseAbstractTest() { writerPlugin.writer.renderedContent("root/test/-a/index.html").let { javaClassContent -> val signatures = javaClassContent.signature().toList() - assertEquals(2, signatures.size, "Expected 2 signatures: class signature and setter") + assertEquals( + 3, + signatures.size, + "Expected 3 signatures: class signature, default constructor and setter" + ) - val setterFunction = signatures[1] + val setterFunction = signatures[2] setterFunction.match( "open fun ", A("setA"), "(", Parameters( Parameter("a: ", A("Int")) @@ -241,9 +249,13 @@ class InheritedAccessorsSignatureTest : BaseAbstractTest() { renderingStage = { _, _ -> writerPlugin.writer.renderedContent("root/test/-java-class/index.html").let { kotlinClassContent -> val signatures = kotlinClassContent.signature().toList() - assertEquals(2, signatures.size, "Expected to find two signatures: class and property") + assertEquals( + 3, + signatures.size, + "Expected to find 3 signatures: class, default constructor and property" + ) - val property = signatures[1] + val property = signatures[2] property.match( "open var ", A("variable"), ": ", Span("String"), ignoreSpanWithTokenStyle = true @@ -290,15 +302,19 @@ class InheritedAccessorsSignatureTest : BaseAbstractTest() { // test added to control changes writerPlugin.writer.renderedContent("root/test/-java-class/index.html").let { javaClassContent -> val signatures = javaClassContent.signature().toList() - assertEquals(3, signatures.size, "Expected to find 3 signatures: class and two accessors") + assertEquals( + 4, + signatures.size, + "Expected to find 4 signatures: class, default constructor and two accessors" + ) - val getter = signatures[1] + val getter = signatures[2] getter.match( "fun ", A("getVariable"), "(): ", Span("String"), ignoreSpanWithTokenStyle = true ) - val setter = signatures[2] + val setter = signatures[3] setter.match( "fun ", A("setVariable"), "(", Parameters( Parameter("value: ", Span("String")) @@ -367,9 +383,13 @@ class InheritedAccessorsSignatureTest : BaseAbstractTest() { writerPlugin.writer.renderedContent("root/test/-java-class/index.html").let { javaClassContent -> val signatures = javaClassContent.signature().toList() - assertEquals(2, signatures.size, "Expected 2 signatures: class signature and property") + assertEquals( + 3, + signatures.size, + "Expected 3 signatures: class signature, default constructor and property" + ) - val property = signatures[1] + val property = signatures[2] property.match( "protected open var ", A("protectedGetterAndProtectedSetter"), ":", A("Int"), ignoreSpanWithTokenStyle = true diff --git a/plugins/base/src/test/kotlin/signatures/SignatureTest.kt b/plugins/base/src/test/kotlin/signatures/SignatureTest.kt index 77c92d9c..f017c815 100644 --- a/plugins/base/src/test/kotlin/signatures/SignatureTest.kt +++ b/plugins/base/src/test/kotlin/signatures/SignatureTest.kt @@ -976,9 +976,13 @@ class SignatureTest : BaseAbstractTest() { writerPlugin.writer.renderedContent("root/test/-java-class/index.html").let { kotlinClassContent -> val signatures = kotlinClassContent.signature().toList() - assertEquals(2, signatures.size, "Expected 2 signatures: class signature and property") + assertEquals( + 3, + signatures.size, + "Expected 3 signatures: class signature, default constructor and property" + ) - val property = signatures[1] + val property = signatures[2] property.match( "open var ", A("property"), ":", A("Int"), ignoreSpanWithTokenStyle = true diff --git a/plugins/base/src/test/kotlin/translators/DefaultPsiToDocumentableTranslatorTest.kt b/plugins/base/src/test/kotlin/translators/DefaultPsiToDocumentableTranslatorTest.kt index 711b9c02..537a4bfc 100644 --- a/plugins/base/src/test/kotlin/translators/DefaultPsiToDocumentableTranslatorTest.kt +++ b/plugins/base/src/test/kotlin/translators/DefaultPsiToDocumentableTranslatorTest.kt @@ -8,7 +8,6 @@ import org.jetbrains.dokka.links.PointingToDeclaration import org.jetbrains.dokka.model.* import org.jetbrains.dokka.model.doc.* import org.jetbrains.dokka.plugability.DokkaPlugin -import org.junit.jupiter.api.Assertions import org.junit.jupiter.api.Assertions.* import org.junit.jupiter.api.Test import utils.assertNotNull @@ -741,4 +740,93 @@ class DefaultPsiToDocumentableTranslatorTest : BaseAbstractTest() { } } } + + @Test + fun `should have public default constructor in public class`() { + testInline( + """ + |/src/main/java/test/A.java + |package test; + |public class A { + |} + """.trimIndent(), + configuration + ) { + documentablesMergingStage = { module -> + val testedClass = module.findClasslike(packageName = "test", "A") as DClass + + assertEquals(1, testedClass.constructors.size, "Expect 1 default constructor") + assertTrue( + testedClass.constructors.first().parameters.isEmpty(), + "Expect default constructor doesn't have params" + ) + assertEquals(JavaVisibility.Public, testedClass.constructors.first().visibility()) + } + } + } + + @Test + fun `should have package-private default constructor in package-private class`() { + testInline( + """ + |/src/main/java/test/A.java + |package test; + |class A { + |} + """.trimIndent(), + configuration + ) { + documentablesMergingStage = { module -> + val testedClass = module.findClasslike(packageName = "test", "A") as DClass + + assertEquals(1, testedClass.constructors.size, "Expect 1 default constructor") + assertEquals(JavaVisibility.Default, testedClass.constructors.first().visibility()) + } + } + } + + @Test + fun `should have private default constructor in private nested class`() { + testInline( + """ + |/src/main/java/test/A.java + |package test; + |class A { + | private static class PrivateNested{} + |} + """.trimIndent(), + configuration + ) { + documentablesMergingStage = { module -> + val parentClass = module.findClasslike(packageName = "test", "A") as DClass + val testedClass = parentClass.classlikes.single { it.name == "PrivateNested" } as DClass + + assertEquals(1, testedClass.constructors.size, "Expect 1 default constructor") + assertEquals(JavaVisibility.Private, testedClass.constructors.first().visibility()) + } + } + } + + @Test + fun `should not have a default constructor because have explicit private`() { + testInline( + """ + |/src/main/java/test/A.java + |package test; + |public class A { + | private A(){} + |} + """.trimIndent(), + configuration + ) { + documentablesMergingStage = { module -> + val testedClass = module.findClasslike(packageName = "test", "A") as DClass + + assertEquals(1, testedClass.constructors.size, "Expect 1 declared constructor") + assertEquals(JavaVisibility.Private, testedClass.constructors.first().visibility()) + } + } + } } + +private fun DFunction.visibility() = visibility.values.first() |