blob: cc09f00106f0d3830918ea7e89ca0ce7baac1ada [file] [log] [blame]
Ilya Ryzhenkov8a4dad42014-07-11 20:41:53 +04001package org.jetbrains.dokka.tests
Ilya Ryzhenkov044e1b82014-07-11 17:11:35 +04002
3import org.jetbrains.jet.cli.common.messages.*
4import 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.*
Ilya Ryzhenkov2ebfb982014-10-13 00:19:18 +04007import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor
Dmitry Jemerovc43a4372014-12-29 20:22:43 +01008import java.io.File
9import kotlin.test.assertEquals
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) {
Ilya Ryzhenkov044e1b82014-07-11 17:11:35 +040030 addSources(files.toList())
31 }
32
Ilya Ryzhenkov11355ce2014-10-12 22:35:47 +040033 val options = DocumentationOptions(includeNonPublic = true)
Ilya Ryzhenkov08e69002014-07-14 15:44:32 +040034
Ilya Ryzhenkov1cb3af92014-10-13 20:14:45 +040035 val documentation = environment.withContext { environment, session ->
36 val fragments = environment.getSourceFiles().map { session.getPackageFragment(it.getPackageFqName()) }.filterNotNull().distinct()
Ilya Ryzhenkov2ebfb982014-10-13 00:19:18 +040037 val descriptors = hashMapOf<String, List<DeclarationDescriptor>>()
38 for ((name, parts) in fragments.groupBy { it.fqName }) {
39 descriptors.put(name.asString(), parts.flatMap { it.getMemberScope().getAllDescriptors() })
40 }
41
Ilya Ryzhenkov11355ce2014-10-12 22:35:47 +040042 val documentationModule = DocumentationModule("test")
Ilya Ryzhenkov1cb3af92014-10-13 20:14:45 +040043 val documentationBuilder = DocumentationBuilder(session, options)
Ilya Ryzhenkov11355ce2014-10-12 22:35:47 +040044 with(documentationBuilder) {
Ilya Ryzhenkov2ebfb982014-10-13 00:19:18 +040045 documentationModule.appendFragments(fragments)
Ilya Ryzhenkov11355ce2014-10-12 22:35:47 +040046 }
47 documentationBuilder.resolveReferences(documentationModule)
48 documentationModule
Ilya Ryzhenkov08e69002014-07-14 15:44:32 +040049 }
50 verifier(documentation)
Ilya Ryzhenkov044e1b82014-07-11 17:11:35 +040051 Disposer.dispose(environment)
52}
53
Dmitry Jemerov8ef68182014-12-30 12:36:14 +010054public fun verifyOutput(path: String, outputExtension: String, outputGenerator: (DocumentationModule, StringBuilder) -> Unit) {
Dmitry Jemerovc43a4372014-12-29 20:22:43 +010055 verifyModel(path) {
56 val output = StringBuilder()
57 outputGenerator(it, output)
Dmitry Jemerov8ef68182014-12-30 12:36:14 +010058 val expectedOutput = File(path.replace(".kt", outputExtension)).readText()
59 assertEquals(expectedOutput, output.toString())
Dmitry Jemerovc43a4372014-12-29 20:22:43 +010060 }
61}
62
Ilya Ryzhenkov11355ce2014-10-12 22:35:47 +040063fun StringBuilder.appendChildren(node: ContentNode): StringBuilder {
Ilya Ryzhenkov778e2b32014-09-29 20:54:59 +040064 for (child in node.children) {
65 val childText = child.toTestString()
66 append(childText)
67 }
68 return this
69}
70
Ilya Ryzhenkov11355ce2014-10-12 22:35:47 +040071fun StringBuilder.appendNode(node: ContentNode): StringBuilder {
Ilya Ryzhenkov778e2b32014-09-29 20:54:59 +040072 when (node) {
73 is ContentText -> {
74 append(node.text)
75 }
76 is ContentEmphasis -> append("*").appendChildren(node).append("*")
Ilya Ryzhenkovbd6cddd2014-12-16 21:41:32 +030077 is ContentNodeLink -> {
78 append("[")
79 appendChildren(node)
80 append(" -> ")
81 append(node.node.toString())
82 append("]")
83 }
Ilya Ryzhenkov778e2b32014-09-29 20:54:59 +040084 else -> {
85 appendChildren(node)
86 }
87 }
88 return this
89}
90
91fun ContentNode.toTestString(): String {
92 val node = this
93 return StringBuilder {
94 appendNode(node)
95 }.toString()
96}
Dmitry Jemerovc43a4372014-12-29 20:22:43 +010097
98val tempLocation = Location(File("/tmp/out"))
99
100object InMemoryLocationService: LocationService {
101 override fun location(node: DocumentationNode) = tempLocation;
102}