blob: 88d1903680e8ee5335f8242bfa2b3134013b9914 [file] [log] [blame]
Ilya Ryzhenkov8a4dad42014-07-11 20:41:53 +04001package org.jetbrains.dokka.tests
Ilya Ryzhenkov044e1b82014-07-11 17:11:35 +04002
Dmitry Jemerov69dd2982014-12-30 18:47:03 +01003import com.intellij.openapi.application.PathManager
Dmitry Jemerovbc0654e2015-05-27 19:26:13 +02004import com.intellij.openapi.util.Disposer
5import org.jetbrains.dokka.*
6import org.jetbrains.kotlin.cli.common.messages.CompilerMessageLocation
7import org.jetbrains.kotlin.cli.common.messages.CompilerMessageSeverity
8import org.jetbrains.kotlin.cli.common.messages.MessageCollector
9import org.jetbrains.kotlin.cli.jvm.config.JavaSourceRoot
10import org.jetbrains.kotlin.config.ContentRoot
11import org.jetbrains.kotlin.config.KotlinSourceRoot
Dmitry Jemerov7fbff242015-01-09 18:54:06 +010012import org.junit.Assert
Dmitry Jemerovbc0654e2015-05-27 19:26:13 +020013import java.io.File
14import kotlin.test.fail
Ilya Ryzhenkov044e1b82014-07-11 17:11:35 +040015
Dmitry Jemerovbc0654e2015-05-27 19:26:13 +020016public fun verifyModel(vararg roots: ContentRoot, verifier: (DocumentationModule) -> Unit) {
Ilya Ryzhenkov044e1b82014-07-11 17:11:35 +040017 val messageCollector = object : MessageCollector {
18 override fun report(severity: CompilerMessageSeverity, message: String, location: CompilerMessageLocation) {
19 when (severity) {
20 CompilerMessageSeverity.WARNING,
21 CompilerMessageSeverity.LOGGING,
22 CompilerMessageSeverity.OUTPUT,
23 CompilerMessageSeverity.INFO,
24 CompilerMessageSeverity.ERROR -> {
25 println("$severity: $message at $location")
26 }
27 CompilerMessageSeverity.EXCEPTION -> {
28 fail("$severity: $message at $location")
29 }
30 }
31 }
32 }
33
Ilya Ryzhenkov044308b2014-07-11 20:49:04 +040034 val environment = AnalysisEnvironment(messageCollector) {
Dmitry Jemerov1e862f22015-09-09 14:54:19 +020035 val stringRoot = PathManager.getResourceRoot(String::class.java, "/java/lang/String.class")
Dmitry Jemerov69dd2982014-12-30 18:47:03 +010036 addClasspath(File(stringRoot))
Dmitry Jemerov1e862f22015-09-09 14:54:19 +020037 val kotlinPairRoot = PathManager.getResourceRoot(Pair::class.java, "/kotlin/Pair.class")
38 addClasspath(File(kotlinPairRoot))
Dmitry Jemerovbc0654e2015-05-27 19:26:13 +020039 addRoots(roots.toList())
Ilya Ryzhenkov044e1b82014-07-11 17:11:35 +040040 }
Dmitry Jemerov94d8cc02015-03-02 17:57:05 +010041 val options = DocumentationOptions(includeNonPublic = true, skipEmptyPackages = false, sourceLinks = listOf<SourceLinkDefinition>())
Dmitry Jemerovf2fd6172015-02-13 12:17:47 +010042 val documentation = buildDocumentationModule(environment, "test", options, logger = DokkaConsoleLogger)
Ilya Ryzhenkov08e69002014-07-14 15:44:32 +040043 verifier(documentation)
Ilya Ryzhenkov044e1b82014-07-11 17:11:35 +040044 Disposer.dispose(environment)
45}
46
Dmitry Jemerovbc0654e2015-05-27 19:26:13 +020047public fun verifyModel(source: String, verifier: (DocumentationModule) -> Unit) {
48 verifyModel(contentRootFromPath(source), verifier = verifier)
49}
50
51public fun verifyPackageMember(kotlinSource: String, verifier: (DocumentationNode) -> Unit) {
52 verifyModel(kotlinSource) { model ->
Dmitry Jemerovaa3f0512015-02-13 17:04:58 +010053 val pkg = model.members.single()
54 verifier(pkg.members.single())
55 }
56}
57
Dmitry Jemerovbc0654e2015-05-27 19:26:13 +020058public fun verifyOutput(roots: Array<ContentRoot>, outputExtension: String, outputGenerator: (DocumentationModule, StringBuilder) -> Unit) {
59 verifyModel(*roots) {
Dmitry Jemerovc43a4372014-12-29 20:22:43 +010060 val output = StringBuilder()
61 outputGenerator(it, output)
Dmitry Jemerov64414ce2015-05-29 13:52:43 +020062 val ext = outputExtension.removePrefix(".")
Dmitry Jemerovbc0654e2015-05-27 19:26:13 +020063 val path = roots.first().path
Dmitry Jemerovfc701842015-02-25 20:06:16 +010064 val expectedOutput = File(path.replaceAfterLast(".", ext, path + "." + ext)).readText()
Dmitry Jemerov599f32d2015-01-22 11:30:57 +010065 assertEqualsIgnoringSeparators(expectedOutput, output.toString())
Dmitry Jemerovc43a4372014-12-29 20:22:43 +010066 }
67}
68
Dmitry Jemerovbc0654e2015-05-27 19:26:13 +020069public fun verifyOutput(path: String, outputExtension: String, outputGenerator: (DocumentationModule, StringBuilder) -> Unit) {
70 verifyOutput(arrayOf(contentRootFromPath(path)), outputExtension, outputGenerator)
71}
72
Dmitry Jemerov599f32d2015-01-22 11:30:57 +010073public fun assertEqualsIgnoringSeparators(expectedOutput: String, output: String) {
74 Assert.assertEquals(expectedOutput.replace("\r\n", "\n"), output.replace("\r\n", "\n"))
75}
76
Dmitry Jemerov0d0fc1f2015-02-10 18:32:12 +010077fun StringBuilder.appendChildren(node: ContentBlock): StringBuilder {
Ilya Ryzhenkov778e2b32014-09-29 20:54:59 +040078 for (child in node.children) {
79 val childText = child.toTestString()
80 append(childText)
81 }
82 return this
83}
84
Ilya Ryzhenkov11355ce2014-10-12 22:35:47 +040085fun StringBuilder.appendNode(node: ContentNode): StringBuilder {
Ilya Ryzhenkov778e2b32014-09-29 20:54:59 +040086 when (node) {
87 is ContentText -> {
88 append(node.text)
89 }
90 is ContentEmphasis -> append("*").appendChildren(node).append("*")
Ilya Ryzhenkov1b5f12b2014-12-22 20:01:01 +030091 is ContentBlockCode -> {
92 appendln("[code]")
93 appendChildren(node)
94 appendln()
95 appendln("[/code]")
96 }
Ilya Ryzhenkovbd6cddd2014-12-16 21:41:32 +030097 is ContentNodeLink -> {
98 append("[")
99 appendChildren(node)
100 append(" -> ")
101 append(node.node.toString())
102 append("]")
103 }
Dmitry Jemerov0d0fc1f2015-02-10 18:32:12 +0100104 is ContentBlock -> {
Ilya Ryzhenkov778e2b32014-09-29 20:54:59 +0400105 appendChildren(node)
106 }
Dmitry Jemerov0d0fc1f2015-02-10 18:32:12 +0100107 else -> throw IllegalStateException("Don't know how to format node $node")
Ilya Ryzhenkov778e2b32014-09-29 20:54:59 +0400108 }
109 return this
110}
111
112fun ContentNode.toTestString(): String {
113 val node = this
114 return StringBuilder {
115 appendNode(node)
116 }.toString()
117}
Dmitry Jemerovc43a4372014-12-29 20:22:43 +0100118
Dmitry Jemerovd9bfa022015-02-19 18:59:00 +0100119class InMemoryLocation(override val path: String): Location {
Dmitry Jemerov85a3ae72015-02-20 14:08:30 +0100120 override fun relativePathTo(other: Location, anchor: String?): String =
121 if (anchor != null) other.path + "#" + anchor else other.path
Dmitry Jemerovd9bfa022015-02-19 18:59:00 +0100122}
Dmitry Jemerovc43a4372014-12-29 20:22:43 +0100123
124object InMemoryLocationService: LocationService {
Dmitry Jemerovecadf402015-02-20 14:44:30 +0100125 override fun location(qualifiedName: List<String>, hasMembers: Boolean) =
126 InMemoryLocation(relativePathToNode(qualifiedName, hasMembers))
Dmitry Jemerovc43a4372014-12-29 20:22:43 +0100127}
Dmitry Jemerovd9bfa022015-02-19 18:59:00 +0100128
129val tempLocation = InMemoryLocation("")
Dmitry Jemerovbc0654e2015-05-27 19:26:13 +0200130
131val ContentRoot.path: String
132 get() = when(this) {
133 is KotlinSourceRoot -> path
134 is JavaSourceRoot -> file.path
135 else -> throw UnsupportedOperationException()
136 }