blob: f3c5f8c86bd461b2687d89ec4251e9d4a2c962d7 [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())
33 }
34
Dmitry Jemerovfac68722015-01-14 20:21:49 +010035 val options = DocumentationOptions(includeNonPublic = true, sourceLinks = listOf<SourceLinkDefinition>())
Ilya Ryzhenkov08e69002014-07-14 15:44:32 +040036
Ilya Ryzhenkov1cb3af92014-10-13 20:14:45 +040037 val documentation = environment.withContext { environment, session ->
38 val fragments = environment.getSourceFiles().map { session.getPackageFragment(it.getPackageFqName()) }.filterNotNull().distinct()
Ilya Ryzhenkov2ebfb982014-10-13 00:19:18 +040039
Ilya Ryzhenkov11355ce2014-10-12 22:35:47 +040040 val documentationModule = DocumentationModule("test")
Ilya Ryzhenkov1cb3af92014-10-13 20:14:45 +040041 val documentationBuilder = DocumentationBuilder(session, options)
Ilya Ryzhenkov11355ce2014-10-12 22:35:47 +040042 with(documentationBuilder) {
Ilya Ryzhenkov2ebfb982014-10-13 00:19:18 +040043 documentationModule.appendFragments(fragments)
Ilya Ryzhenkov11355ce2014-10-12 22:35:47 +040044 }
45 documentationBuilder.resolveReferences(documentationModule)
46 documentationModule
Ilya Ryzhenkov08e69002014-07-14 15:44:32 +040047 }
48 verifier(documentation)
Ilya Ryzhenkov044e1b82014-07-11 17:11:35 +040049 Disposer.dispose(environment)
50}
51
Dmitry Jemerov8ef68182014-12-30 12:36:14 +010052public fun verifyOutput(path: String, outputExtension: String, outputGenerator: (DocumentationModule, StringBuilder) -> Unit) {
Dmitry Jemerovc43a4372014-12-29 20:22:43 +010053 verifyModel(path) {
54 val output = StringBuilder()
55 outputGenerator(it, output)
Dmitry Jemerov1ce53732015-01-13 16:19:42 +010056 val expectedOutput = File(path.replace(".kt", outputExtension)).readText().replace("\r\n", "\n")
Dmitry Jemerov7fbff242015-01-09 18:54:06 +010057 Assert.assertEquals(expectedOutput, output.toString())
Dmitry Jemerovc43a4372014-12-29 20:22:43 +010058 }
59}
60
Ilya Ryzhenkov11355ce2014-10-12 22:35:47 +040061fun StringBuilder.appendChildren(node: ContentNode): StringBuilder {
Ilya Ryzhenkov778e2b32014-09-29 20:54:59 +040062 for (child in node.children) {
63 val childText = child.toTestString()
64 append(childText)
65 }
66 return this
67}
68
Ilya Ryzhenkov11355ce2014-10-12 22:35:47 +040069fun StringBuilder.appendNode(node: ContentNode): StringBuilder {
Ilya Ryzhenkov778e2b32014-09-29 20:54:59 +040070 when (node) {
71 is ContentText -> {
72 append(node.text)
73 }
74 is ContentEmphasis -> append("*").appendChildren(node).append("*")
Ilya Ryzhenkov1b5f12b2014-12-22 20:01:01 +030075 is ContentBlockCode -> {
76 appendln("[code]")
77 appendChildren(node)
78 appendln()
79 appendln("[/code]")
80 }
Ilya Ryzhenkovbd6cddd2014-12-16 21:41:32 +030081 is ContentNodeLink -> {
82 append("[")
83 appendChildren(node)
84 append(" -> ")
85 append(node.node.toString())
86 append("]")
87 }
Ilya Ryzhenkov778e2b32014-09-29 20:54:59 +040088 else -> {
89 appendChildren(node)
90 }
91 }
92 return this
93}
94
95fun ContentNode.toTestString(): String {
96 val node = this
97 return StringBuilder {
98 appendNode(node)
99 }.toString()
100}
Dmitry Jemerovc43a4372014-12-29 20:22:43 +0100101
102val tempLocation = Location(File("/tmp/out"))
103
104object InMemoryLocationService: LocationService {
105 override fun location(node: DocumentationNode) = tempLocation;
106}