blob: 901e78fcfac5ade0d8995f320da4f1068aaf0113 [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.*
Alexander Udalov5dbadfe2015-01-12 12:36:54 +03007import org.jetbrains.kotlin.descriptors.DeclarationDescriptor
Dmitry Jemerovc43a4372014-12-29 20:22:43 +01008import java.io.File
Dmitry Jemerov69dd2982014-12-30 18:47:03 +01009import com.intellij.openapi.application.PathManager
Dmitry Jemerov7fbff242015-01-09 18:54:06 +010010import org.junit.Assert
Ilya Ryzhenkov044e1b82014-07-11 17:11:35 +040011
Ilya Ryzhenkova6000802014-07-12 04:23:51 +040012public fun verifyModel(vararg files: String, verifier: (DocumentationModule) -> Unit) {
Ilya Ryzhenkov044e1b82014-07-11 17:11:35 +040013 val messageCollector = object : MessageCollector {
14 override fun report(severity: CompilerMessageSeverity, message: String, location: CompilerMessageLocation) {
15 when (severity) {
16 CompilerMessageSeverity.WARNING,
17 CompilerMessageSeverity.LOGGING,
18 CompilerMessageSeverity.OUTPUT,
19 CompilerMessageSeverity.INFO,
20 CompilerMessageSeverity.ERROR -> {
21 println("$severity: $message at $location")
22 }
23 CompilerMessageSeverity.EXCEPTION -> {
24 fail("$severity: $message at $location")
25 }
26 }
27 }
28 }
29
Ilya Ryzhenkov044308b2014-07-11 20:49:04 +040030 val environment = AnalysisEnvironment(messageCollector) {
Dmitry Jemerov69dd2982014-12-30 18:47:03 +010031 val stringRoot = PathManager.getResourceRoot(javaClass<String>(), "/java/lang/String.class")
32 addClasspath(File(stringRoot))
Ilya Ryzhenkov044e1b82014-07-11 17:11:35 +040033 addSources(files.toList())
34 }
35
Ilya Ryzhenkov11355ce2014-10-12 22:35:47 +040036 val options = DocumentationOptions(includeNonPublic = true)
Ilya Ryzhenkov08e69002014-07-14 15:44:32 +040037
Ilya Ryzhenkov1cb3af92014-10-13 20:14:45 +040038 val documentation = environment.withContext { environment, session ->
39 val fragments = environment.getSourceFiles().map { session.getPackageFragment(it.getPackageFqName()) }.filterNotNull().distinct()
Ilya Ryzhenkov2ebfb982014-10-13 00:19:18 +040040
Ilya Ryzhenkov11355ce2014-10-12 22:35:47 +040041 val documentationModule = DocumentationModule("test")
Ilya Ryzhenkov1cb3af92014-10-13 20:14:45 +040042 val documentationBuilder = DocumentationBuilder(session, options)
Ilya Ryzhenkov11355ce2014-10-12 22:35:47 +040043 with(documentationBuilder) {
Ilya Ryzhenkov2ebfb982014-10-13 00:19:18 +040044 documentationModule.appendFragments(fragments)
Ilya Ryzhenkov11355ce2014-10-12 22:35:47 +040045 }
46 documentationBuilder.resolveReferences(documentationModule)
47 documentationModule
Ilya Ryzhenkov08e69002014-07-14 15:44:32 +040048 }
49 verifier(documentation)
Ilya Ryzhenkov044e1b82014-07-11 17:11:35 +040050 Disposer.dispose(environment)
51}
52
Dmitry Jemerov8ef68182014-12-30 12:36:14 +010053public fun verifyOutput(path: String, outputExtension: String, outputGenerator: (DocumentationModule, StringBuilder) -> Unit) {
Dmitry Jemerovc43a4372014-12-29 20:22:43 +010054 verifyModel(path) {
55 val output = StringBuilder()
56 outputGenerator(it, output)
Dmitry Jemerov8ef68182014-12-30 12:36:14 +010057 val expectedOutput = File(path.replace(".kt", outputExtension)).readText()
Dmitry Jemerov7fbff242015-01-09 18:54:06 +010058 Assert.assertEquals(expectedOutput, output.toString())
Dmitry Jemerovc43a4372014-12-29 20:22:43 +010059 }
60}
61
Ilya Ryzhenkov11355ce2014-10-12 22:35:47 +040062fun StringBuilder.appendChildren(node: ContentNode): 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 Ryzhenkovbd6cddd2014-12-16 21:41:32 +030076 is ContentNodeLink -> {
77 append("[")
78 appendChildren(node)
79 append(" -> ")
80 append(node.node.toString())
81 append("]")
82 }
Ilya Ryzhenkov778e2b32014-09-29 20:54:59 +040083 else -> {
84 appendChildren(node)
85 }
86 }
87 return this
88}
89
90fun ContentNode.toTestString(): String {
91 val node = this
92 return StringBuilder {
93 appendNode(node)
94 }.toString()
95}
Dmitry Jemerovc43a4372014-12-29 20:22:43 +010096
97val tempLocation = Location(File("/tmp/out"))
98
99object InMemoryLocationService: LocationService {
100 override fun location(node: DocumentationNode) = tempLocation;
101}