blob: 165278f9f28c7bcf3516cd07bf51313142d2ede7 [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
12import org.jetbrains.kotlin.utils.addToStdlib.firstIsInstance
Dmitry Jemerov7fbff242015-01-09 18:54:06 +010013import org.junit.Assert
Dmitry Jemerovbc0654e2015-05-27 19:26:13 +020014import java.io.File
15import kotlin.test.fail
Ilya Ryzhenkov044e1b82014-07-11 17:11:35 +040016
Dmitry Jemerovbc0654e2015-05-27 19:26:13 +020017public fun verifyModel(vararg roots: ContentRoot, verifier: (DocumentationModule) -> Unit) {
Ilya Ryzhenkov044e1b82014-07-11 17:11:35 +040018 val messageCollector = object : MessageCollector {
19 override fun report(severity: CompilerMessageSeverity, message: String, location: CompilerMessageLocation) {
20 when (severity) {
21 CompilerMessageSeverity.WARNING,
22 CompilerMessageSeverity.LOGGING,
23 CompilerMessageSeverity.OUTPUT,
24 CompilerMessageSeverity.INFO,
25 CompilerMessageSeverity.ERROR -> {
26 println("$severity: $message at $location")
27 }
28 CompilerMessageSeverity.EXCEPTION -> {
29 fail("$severity: $message at $location")
30 }
31 }
32 }
33 }
34
Ilya Ryzhenkov044308b2014-07-11 20:49:04 +040035 val environment = AnalysisEnvironment(messageCollector) {
Dmitry Jemerov69dd2982014-12-30 18:47:03 +010036 val stringRoot = PathManager.getResourceRoot(javaClass<String>(), "/java/lang/String.class")
37 addClasspath(File(stringRoot))
Dmitry Jemerovbc0654e2015-05-27 19:26:13 +020038 addRoots(roots.toList())
Ilya Ryzhenkov044e1b82014-07-11 17:11:35 +040039 }
Dmitry Jemerov94d8cc02015-03-02 17:57:05 +010040 val options = DocumentationOptions(includeNonPublic = true, skipEmptyPackages = false, sourceLinks = listOf<SourceLinkDefinition>())
Dmitry Jemerovf2fd6172015-02-13 12:17:47 +010041 val documentation = buildDocumentationModule(environment, "test", options, logger = DokkaConsoleLogger)
Ilya Ryzhenkov08e69002014-07-14 15:44:32 +040042 verifier(documentation)
Ilya Ryzhenkov044e1b82014-07-11 17:11:35 +040043 Disposer.dispose(environment)
44}
45
Dmitry Jemerovbc0654e2015-05-27 19:26:13 +020046public fun verifyModel(source: String, verifier: (DocumentationModule) -> Unit) {
47 verifyModel(contentRootFromPath(source), verifier = verifier)
48}
49
50public fun verifyPackageMember(kotlinSource: String, verifier: (DocumentationNode) -> Unit) {
51 verifyModel(kotlinSource) { model ->
Dmitry Jemerovaa3f0512015-02-13 17:04:58 +010052 val pkg = model.members.single()
53 verifier(pkg.members.single())
54 }
55}
56
Dmitry Jemerovbc0654e2015-05-27 19:26:13 +020057public fun verifyOutput(roots: Array<ContentRoot>, outputExtension: String, outputGenerator: (DocumentationModule, StringBuilder) -> Unit) {
58 verifyModel(*roots) {
Dmitry Jemerovc43a4372014-12-29 20:22:43 +010059 val output = StringBuilder()
60 outputGenerator(it, output)
Dmitry Jemerov64414ce2015-05-29 13:52:43 +020061 val ext = outputExtension.removePrefix(".")
Dmitry Jemerovbc0654e2015-05-27 19:26:13 +020062 val path = roots.first().path
Dmitry Jemerovfc701842015-02-25 20:06:16 +010063 val expectedOutput = File(path.replaceAfterLast(".", ext, path + "." + ext)).readText()
Dmitry Jemerov599f32d2015-01-22 11:30:57 +010064 assertEqualsIgnoringSeparators(expectedOutput, output.toString())
Dmitry Jemerovc43a4372014-12-29 20:22:43 +010065 }
66}
67
Dmitry Jemerovbc0654e2015-05-27 19:26:13 +020068public fun verifyOutput(path: String, outputExtension: String, outputGenerator: (DocumentationModule, StringBuilder) -> Unit) {
69 verifyOutput(arrayOf(contentRootFromPath(path)), outputExtension, outputGenerator)
70}
71
Dmitry Jemerov599f32d2015-01-22 11:30:57 +010072public fun assertEqualsIgnoringSeparators(expectedOutput: String, output: String) {
73 Assert.assertEquals(expectedOutput.replace("\r\n", "\n"), output.replace("\r\n", "\n"))
74}
75
Dmitry Jemerov0d0fc1f2015-02-10 18:32:12 +010076fun StringBuilder.appendChildren(node: ContentBlock): StringBuilder {
Ilya Ryzhenkov778e2b32014-09-29 20:54:59 +040077 for (child in node.children) {
78 val childText = child.toTestString()
79 append(childText)
80 }
81 return this
82}
83
Ilya Ryzhenkov11355ce2014-10-12 22:35:47 +040084fun StringBuilder.appendNode(node: ContentNode): StringBuilder {
Ilya Ryzhenkov778e2b32014-09-29 20:54:59 +040085 when (node) {
86 is ContentText -> {
87 append(node.text)
88 }
89 is ContentEmphasis -> append("*").appendChildren(node).append("*")
Ilya Ryzhenkov1b5f12b2014-12-22 20:01:01 +030090 is ContentBlockCode -> {
91 appendln("[code]")
92 appendChildren(node)
93 appendln()
94 appendln("[/code]")
95 }
Ilya Ryzhenkovbd6cddd2014-12-16 21:41:32 +030096 is ContentNodeLink -> {
97 append("[")
98 appendChildren(node)
99 append(" -> ")
100 append(node.node.toString())
101 append("]")
102 }
Dmitry Jemerov0d0fc1f2015-02-10 18:32:12 +0100103 is ContentBlock -> {
Ilya Ryzhenkov778e2b32014-09-29 20:54:59 +0400104 appendChildren(node)
105 }
Dmitry Jemerov0d0fc1f2015-02-10 18:32:12 +0100106 else -> throw IllegalStateException("Don't know how to format node $node")
Ilya Ryzhenkov778e2b32014-09-29 20:54:59 +0400107 }
108 return this
109}
110
111fun ContentNode.toTestString(): String {
112 val node = this
113 return StringBuilder {
114 appendNode(node)
115 }.toString()
116}
Dmitry Jemerovc43a4372014-12-29 20:22:43 +0100117
Dmitry Jemerovd9bfa022015-02-19 18:59:00 +0100118class InMemoryLocation(override val path: String): Location {
Dmitry Jemerov85a3ae72015-02-20 14:08:30 +0100119 override fun relativePathTo(other: Location, anchor: String?): String =
120 if (anchor != null) other.path + "#" + anchor else other.path
Dmitry Jemerovd9bfa022015-02-19 18:59:00 +0100121}
Dmitry Jemerovc43a4372014-12-29 20:22:43 +0100122
123object InMemoryLocationService: LocationService {
Dmitry Jemerovecadf402015-02-20 14:44:30 +0100124 override fun location(qualifiedName: List<String>, hasMembers: Boolean) =
125 InMemoryLocation(relativePathToNode(qualifiedName, hasMembers))
Dmitry Jemerovc43a4372014-12-29 20:22:43 +0100126}
Dmitry Jemerovd9bfa022015-02-19 18:59:00 +0100127
128val tempLocation = InMemoryLocation("")
Dmitry Jemerovbc0654e2015-05-27 19:26:13 +0200129
130val ContentRoot.path: String
131 get() = when(this) {
132 is KotlinSourceRoot -> path
133 is JavaSourceRoot -> file.path
134 else -> throw UnsupportedOperationException()
135 }