blob: b885a15f56cb6b8dfde6402db4512fcb73784cdf [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
Ilya Ryzhenkov044e1b82014-07-11 17:11:35 +04008
Ilya Ryzhenkova6000802014-07-12 04:23:51 +04009public fun verifyModel(vararg files: String, verifier: (DocumentationModule) -> Unit) {
Ilya Ryzhenkov044e1b82014-07-11 17:11:35 +040010 val messageCollector = object : MessageCollector {
11 override fun report(severity: CompilerMessageSeverity, message: String, location: CompilerMessageLocation) {
12 when (severity) {
13 CompilerMessageSeverity.WARNING,
14 CompilerMessageSeverity.LOGGING,
15 CompilerMessageSeverity.OUTPUT,
16 CompilerMessageSeverity.INFO,
17 CompilerMessageSeverity.ERROR -> {
18 println("$severity: $message at $location")
19 }
20 CompilerMessageSeverity.EXCEPTION -> {
21 fail("$severity: $message at $location")
22 }
23 }
24 }
25 }
26
Ilya Ryzhenkov044308b2014-07-11 20:49:04 +040027 val environment = AnalysisEnvironment(messageCollector) {
Ilya Ryzhenkov044e1b82014-07-11 17:11:35 +040028 addSources(files.toList())
29 }
30
Ilya Ryzhenkov11355ce2014-10-12 22:35:47 +040031 val options = DocumentationOptions(includeNonPublic = true)
Ilya Ryzhenkov08e69002014-07-14 15:44:32 +040032
Ilya Ryzhenkov11355ce2014-10-12 22:35:47 +040033 val documentation = environment.withContext { environment, module, context ->
Ilya Ryzhenkov2ebfb982014-10-13 00:19:18 +040034 val fragments = environment.getSourceFiles().map { context.getPackageFragment(it) }.filterNotNull().distinct()
35 val descriptors = hashMapOf<String, List<DeclarationDescriptor>>()
36 for ((name, parts) in fragments.groupBy { it.fqName }) {
37 descriptors.put(name.asString(), parts.flatMap { it.getMemberScope().getAllDescriptors() })
38 }
39
Ilya Ryzhenkov11355ce2014-10-12 22:35:47 +040040 val documentationModule = DocumentationModule("test")
41 val documentationBuilder = DocumentationBuilder(context, options)
42 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
Ilya Ryzhenkov11355ce2014-10-12 22:35:47 +040052fun StringBuilder.appendChildren(node: ContentNode): StringBuilder {
Ilya Ryzhenkov778e2b32014-09-29 20:54:59 +040053 for (child in node.children) {
54 val childText = child.toTestString()
55 append(childText)
56 }
57 return this
58}
59
Ilya Ryzhenkov11355ce2014-10-12 22:35:47 +040060fun StringBuilder.appendNode(node: ContentNode): StringBuilder {
Ilya Ryzhenkov778e2b32014-09-29 20:54:59 +040061 when (node) {
62 is ContentText -> {
63 append(node.text)
64 }
65 is ContentEmphasis -> append("*").appendChildren(node).append("*")
66 else -> {
67 appendChildren(node)
68 }
69 }
70 return this
71}
72
73fun ContentNode.toTestString(): String {
74 val node = this
75 return StringBuilder {
76 appendNode(node)
77 }.toString()
78}