<lambda>null1 package org.jetbrains.dokka.javadoc
2 
3 import com.sun.javadoc.Tag
4 import com.sun.javadoc.Type
5 import org.jetbrains.dokka.DokkaConsoleLogger
6 import org.jetbrains.dokka.tests.assertEqualsIgnoringSeparators
7 import org.jetbrains.dokka.tests.verifyModel
8 import org.junit.Assert.*
9 import org.junit.Test
10 
11 class JavadocTest {
12     @Test fun testTypes() {
13         verifyJavadoc("testdata/javadoc/types.kt", withJdk = true) { doc ->
14             val classDoc = doc.classNamed("foo.TypesKt")!!
15             val method = classDoc.methods().find { it.name() == "foo" }!!
16 
17             val type = method.returnType()
18             assertFalse(type.asClassDoc().isIncluded)
19             assertEquals("String", type.qualifiedTypeName())
20             assertEquals("String", type.asClassDoc().qualifiedName())
21 
22             val params = method.parameters()
23             assertTrue(params[0].type().isPrimitive)
24             assertFalse(params[1].type().asClassDoc().isIncluded)
25         }
26     }
27 
28     @Test fun testObject() {
29         verifyJavadoc("testdata/javadoc/obj.kt") { doc ->
30             val classDoc = doc.classNamed("foo.O")
31             assertNotNull(classDoc)
32 
33             val companionDoc = doc.classNamed("foo.O.Companion")
34             assertNotNull(companionDoc)
35 
36             val pkgDoc = doc.packageNamed("foo")!!
37             assertEquals(2, pkgDoc.allClasses().size)
38         }
39     }
40 
41     @Test fun testException() {
42         verifyJavadoc("testdata/javadoc/exception.kt", withKotlinRuntime = true) { doc ->
43             val classDoc = doc.classNamed("foo.MyException")!!
44             val member = classDoc.methods().find { it.name() == "foo" }
45             assertEquals(classDoc, member!!.containingClass())
46         }
47     }
48 
49     @Test fun testByteArray() {
50         verifyJavadoc("testdata/javadoc/bytearr.kt", withKotlinRuntime = true) { doc ->
51             val classDoc = doc.classNamed("foo.ByteArray")!!
52             assertNotNull(classDoc.asClassDoc())
53 
54             val member = classDoc.methods().find { it.name() == "foo" }!!
55             assertEquals("[]", member.returnType().dimension())
56         }
57     }
58 
59     @Test fun testStringArray() {
60         verifyJavadoc("testdata/javadoc/stringarr.kt", withKotlinRuntime = true) { doc ->
61             val classDoc = doc.classNamed("foo.Foo")!!
62             assertNotNull(classDoc.asClassDoc())
63 
64             val member = classDoc.methods().find { it.name() == "main" }!!
65             val paramType = member.parameters()[0].type()
66             assertNull(paramType.asParameterizedType())
67             assertEquals("String", paramType.typeName())
68             assertEquals("String", paramType.asClassDoc().name())
69         }
70     }
71 
72     @Test fun testJvmName() {
73         verifyJavadoc("testdata/javadoc/jvmname.kt", withKotlinRuntime = true) { doc ->
74             val classDoc = doc.classNamed("foo.Apple")!!
75             assertNotNull(classDoc.asClassDoc())
76 
77             val member = classDoc.methods().find { it.name() == "_tree" }
78             assertNotNull(member)
79         }
80     }
81 
82     @Test fun testLinkWithParam() {
83         verifyJavadoc("testdata/javadoc/paramlink.kt", withKotlinRuntime = true) { doc ->
84             val classDoc = doc.classNamed("demo.Apple")!!
85             assertNotNull(classDoc.asClassDoc())
86             val tags = classDoc.inlineTags().filterIsInstance<SeeTagAdapter>()
87             assertEquals(2, tags.size)
88             val linkTag = tags[1] as SeeMethodTagAdapter
89             assertEquals("cutIntoPieces", linkTag.method.name())
90         }
91     }
92 
93     @Test fun testInternalVisibility() {
94         verifyJavadoc("testdata/javadoc/internal.kt", withKotlinRuntime = true, includeNonPublic = false) { doc ->
95             val classDoc = doc.classNamed("foo.Person")!!
96             val constructors = classDoc.constructors()
97             assertEquals(1, constructors.size)
98             assertEquals(1, constructors.single().parameters().size)
99         }
100     }
101 
102     @Test fun testSuppress() {
103         verifyJavadoc("testdata/javadoc/suppress.kt", withKotlinRuntime = true) { doc ->
104             assertNull(doc.classNamed("Some"))
105             assertNull(doc.classNamed("SomeAgain"))
106             assertNull(doc.classNamed("Interface"))
107             val classSame = doc.classNamed("Same")!!
108             assertTrue(classSame.fields().isEmpty())
109             assertTrue(classSame.methods().isEmpty())
110         }
111     }
112 
113     @Test fun testTypeAliases() {
114         verifyJavadoc("testdata/javadoc/typealiases.kt", withKotlinRuntime = true) { doc ->
115             assertNull(doc.classNamed("B"))
116             assertNull(doc.classNamed("D"))
117 
118             assertEquals("A", doc.classNamed("C")!!.superclass().name())
119             val methodParamType = doc.classNamed("TypealiasesKt")!!.methods()
120                     .find { it.name() == "some" }!!.parameters().first()
121                     .type()
122             assertEquals("Function1", methodParamType.qualifiedTypeName())
123             assertEquals("? super A, C", methodParamType.asParameterizedType().typeArguments()
124                     .map(Type::qualifiedTypeName).joinToString())
125         }
126     }
127 
128     @Test fun testKDocKeywordsOnMethod() {
129         verifyJavadoc("testdata/javadoc/kdocKeywordsOnMethod.kt", withKotlinRuntime = true) { doc ->
130             val method = doc.classNamed("KdocKeywordsOnMethodKt")!!.methods()[0]
131             assertEquals("@return [ContentText(text=value of a)]", method.tags("return").first().text())
132             assertEquals("@param a [ContentText(text=Some string)]", method.paramTags().first().text())
133             assertEquals("@throws FireException [ContentText(text=in case of fire)]", method.throwsTags().first().text())
134         }
135     }
136 
137     @Test
138     fun testBlankLineInsideCodeBlock() {
139         verifyJavadoc("testdata/javadoc/blankLineInsideCodeBlock.kt", withKotlinRuntime = true) { doc ->
140             val method = doc.classNamed("BlankLineInsideCodeBlockKt")!!.methods()[0]
141             val text = method.inlineTags().joinToString(separator = "", transform = Tag::text)
142             assertEqualsIgnoringSeparators("""
143                 <p><code><pre>
144                 This is a test
145                     of Dokka's code blocks.
146                 Here is a blank line.
147 
148                 The previous line was blank.
149                 </pre></code></p>
150             """.trimIndent(), text)
151         }
152     }
153 
154     @Test
155     fun testCompanionMethodReference() {
156         verifyJavadoc("testdata/javadoc/companionMethodReference.kt") { doc ->
157             val classDoc = doc.classNamed("foo.TestClass")!!
158             val tag = classDoc.inlineTags().filterIsInstance<SeeMethodTagAdapter>().first()
159             assertEquals("TestClass.Companion", tag.referencedClassName())
160             assertEquals("test", tag.referencedMemberName())
161         }
162     }
163 
164     @Test fun shouldHaveAllFunctionMarkedAsDeprecated() {
165         verifyJavadoc("testdata/javadoc/deprecated.java") { doc ->
166             val classDoc = doc.classNamed("bar.Banana")!!
167 
168             classDoc.methods().forEach { method ->
169                 assertTrue(method.tags().any { it.kind() == "deprecated" })
170             }
171         }
172     }
173 
174     private fun verifyJavadoc(name: String,
175                               withJdk: Boolean = false,
176                               withKotlinRuntime: Boolean = false,
177                               includeNonPublic: Boolean = true,
178                               callback: (ModuleNodeAdapter) -> Unit) {
179 
180         verifyModel(name, format = "javadoc", withJdk = withJdk, withKotlinRuntime = withKotlinRuntime, includeNonPublic = includeNonPublic) { model ->
181             val doc = ModuleNodeAdapter(model, StandardReporter(DokkaConsoleLogger), "")
182             callback(doc)
183         }
184     }
185 }
186