blob: 6fc83279a5990986d29d47992dcd575bdacc9ebe [file] [log] [blame]
Ilya Ryzhenkov8a4dad42014-07-11 20:41:53 +04001package org.jetbrains.dokka.tests
Ilya Ryzhenkov044e1b82014-07-11 17:11:35 +04002
Alexander Udalov5dbadfe2015-01-12 12:36:54 +03003import org.jetbrains.kotlin.cli.common.messages.*
Ilya Ryzhenkov044e1b82014-07-11 17:11:35 +04004import com.intellij.openapi.util.*
Ilya Ryzhenkov044e1b82014-07-11 17:11:35 +04005import kotlin.test.fail
Ilya Ryzhenkov044308b2014-07-11 20:49:04 +04006import org.jetbrains.dokka.*
Dmitry Jemerovc43a4372014-12-29 20:22:43 +01007import java.io.File
Dmitry Jemerov69dd2982014-12-30 18:47:03 +01008import com.intellij.openapi.application.PathManager
Dmitry Jemerov7fbff242015-01-09 18:54:06 +01009import org.junit.Assert
Ilya Ryzhenkov044e1b82014-07-11 17:11:35 +040010
Ilya Ryzhenkova6000802014-07-12 04:23:51 +040011public fun verifyModel(vararg files: String, verifier: (DocumentationModule) -> Unit) {
Ilya Ryzhenkov044e1b82014-07-11 17:11:35 +040012 val messageCollector = object : MessageCollector {
13 override fun report(severity: CompilerMessageSeverity, message: String, location: CompilerMessageLocation) {
14 when (severity) {
15 CompilerMessageSeverity.WARNING,
16 CompilerMessageSeverity.LOGGING,
17 CompilerMessageSeverity.OUTPUT,
18 CompilerMessageSeverity.INFO,
19 CompilerMessageSeverity.ERROR -> {
20 println("$severity: $message at $location")
21 }
22 CompilerMessageSeverity.EXCEPTION -> {
23 fail("$severity: $message at $location")
24 }
25 }
26 }
27 }
28
Ilya Ryzhenkov044308b2014-07-11 20:49:04 +040029 val environment = AnalysisEnvironment(messageCollector) {
Dmitry Jemerov69dd2982014-12-30 18:47:03 +010030 val stringRoot = PathManager.getResourceRoot(javaClass<String>(), "/java/lang/String.class")
31 addClasspath(File(stringRoot))
Ilya Ryzhenkov044e1b82014-07-11 17:11:35 +040032 addSources(files.toList())
Dmitry Jemerovfc701842015-02-25 20:06:16 +010033 addClasspath(files.map { File(it)}.filter { it.isDirectory()} )
Ilya Ryzhenkov044e1b82014-07-11 17:11:35 +040034 }
Dmitry Jemerovfac68722015-01-14 20:21:49 +010035 val options = DocumentationOptions(includeNonPublic = true, sourceLinks = listOf<SourceLinkDefinition>())
Dmitry Jemerovf2fd6172015-02-13 12:17:47 +010036 val documentation = buildDocumentationModule(environment, "test", options, logger = DokkaConsoleLogger)
Ilya Ryzhenkov08e69002014-07-14 15:44:32 +040037 verifier(documentation)
Ilya Ryzhenkov044e1b82014-07-11 17:11:35 +040038 Disposer.dispose(environment)
39}
40
Dmitry Jemerovaa3f0512015-02-13 17:04:58 +010041public fun verifyPackageMember(vararg files: String, verifier: (DocumentationNode) -> Unit) {
42 verifyModel(*files) { model ->
43 val pkg = model.members.single()
44 verifier(pkg.members.single())
45 }
46}
47
Dmitry Jemerov8ef68182014-12-30 12:36:14 +010048public fun verifyOutput(path: String, outputExtension: String, outputGenerator: (DocumentationModule, StringBuilder) -> Unit) {
Dmitry Jemerovc43a4372014-12-29 20:22:43 +010049 verifyModel(path) {
50 val output = StringBuilder()
51 outputGenerator(it, output)
Dmitry Jemerovfc701842015-02-25 20:06:16 +010052 val ext = outputExtension.trimLeading(".")
53 val expectedOutput = File(path.replaceAfterLast(".", ext, path + "." + ext)).readText()
Dmitry Jemerov599f32d2015-01-22 11:30:57 +010054 assertEqualsIgnoringSeparators(expectedOutput, output.toString())
Dmitry Jemerovc43a4372014-12-29 20:22:43 +010055 }
56}
57
Dmitry Jemerov599f32d2015-01-22 11:30:57 +010058public fun assertEqualsIgnoringSeparators(expectedOutput: String, output: String) {
59 Assert.assertEquals(expectedOutput.replace("\r\n", "\n"), output.replace("\r\n", "\n"))
60}
61
Dmitry Jemerov0d0fc1f2015-02-10 18:32:12 +010062fun StringBuilder.appendChildren(node: ContentBlock): StringBuilder {
Ilya Ryzhenkov778e2b32014-09-29 20:54:59 +040063 for (child in node.children) {
64 val childText = child.toTestString()
65 append(childText)
66 }
67 return this
68}
69
Ilya Ryzhenkov11355ce2014-10-12 22:35:47 +040070fun StringBuilder.appendNode(node: ContentNode): StringBuilder {
Ilya Ryzhenkov778e2b32014-09-29 20:54:59 +040071 when (node) {
72 is ContentText -> {
73 append(node.text)
74 }
75 is ContentEmphasis -> append("*").appendChildren(node).append("*")
Ilya Ryzhenkov1b5f12b2014-12-22 20:01:01 +030076 is ContentBlockCode -> {
77 appendln("[code]")
78 appendChildren(node)
79 appendln()
80 appendln("[/code]")
81 }
Ilya Ryzhenkovbd6cddd2014-12-16 21:41:32 +030082 is ContentNodeLink -> {
83 append("[")
84 appendChildren(node)
85 append(" -> ")
86 append(node.node.toString())
87 append("]")
88 }
Dmitry Jemerov0d0fc1f2015-02-10 18:32:12 +010089 is ContentBlock -> {
Ilya Ryzhenkov778e2b32014-09-29 20:54:59 +040090 appendChildren(node)
91 }
Dmitry Jemerov0d0fc1f2015-02-10 18:32:12 +010092 else -> throw IllegalStateException("Don't know how to format node $node")
Ilya Ryzhenkov778e2b32014-09-29 20:54:59 +040093 }
94 return this
95}
96
97fun ContentNode.toTestString(): String {
98 val node = this
99 return StringBuilder {
100 appendNode(node)
101 }.toString()
102}
Dmitry Jemerovc43a4372014-12-29 20:22:43 +0100103
Dmitry Jemerovd9bfa022015-02-19 18:59:00 +0100104class InMemoryLocation(override val path: String): Location {
Dmitry Jemerov85a3ae72015-02-20 14:08:30 +0100105 override fun relativePathTo(other: Location, anchor: String?): String =
106 if (anchor != null) other.path + "#" + anchor else other.path
Dmitry Jemerovd9bfa022015-02-19 18:59:00 +0100107}
Dmitry Jemerovc43a4372014-12-29 20:22:43 +0100108
109object InMemoryLocationService: LocationService {
Dmitry Jemerovecadf402015-02-20 14:44:30 +0100110 override fun location(qualifiedName: List<String>, hasMembers: Boolean) =
111 InMemoryLocation(relativePathToNode(qualifiedName, hasMembers))
Dmitry Jemerovc43a4372014-12-29 20:22:43 +0100112}
Dmitry Jemerovd9bfa022015-02-19 18:59:00 +0100113
114val tempLocation = InMemoryLocation("")