blob: fcff75e9c7bcde513676b100f0ed41ecc27968c4 [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 Ryzhenkov044e1b82014-07-11 17:11:35 +04007
Ilya Ryzhenkova6000802014-07-12 04:23:51 +04008public fun verifyModel(vararg files: String, verifier: (DocumentationModule) -> Unit) {
Ilya Ryzhenkov044e1b82014-07-11 17:11:35 +04009 val messageCollector = object : MessageCollector {
10 override fun report(severity: CompilerMessageSeverity, message: String, location: CompilerMessageLocation) {
11 when (severity) {
12 CompilerMessageSeverity.WARNING,
13 CompilerMessageSeverity.LOGGING,
14 CompilerMessageSeverity.OUTPUT,
15 CompilerMessageSeverity.INFO,
16 CompilerMessageSeverity.ERROR -> {
17 println("$severity: $message at $location")
18 }
19 CompilerMessageSeverity.EXCEPTION -> {
20 fail("$severity: $message at $location")
21 }
22 }
23 }
24 }
25
Ilya Ryzhenkov044308b2014-07-11 20:49:04 +040026 val environment = AnalysisEnvironment(messageCollector) {
Ilya Ryzhenkov044e1b82014-07-11 17:11:35 +040027 addSources(files.toList())
28 }
29
Ilya Ryzhenkov11355ce2014-10-12 22:35:47 +040030 val options = DocumentationOptions(includeNonPublic = true)
Ilya Ryzhenkov08e69002014-07-14 15:44:32 +040031
Ilya Ryzhenkov11355ce2014-10-12 22:35:47 +040032 val documentation = environment.withContext { environment, module, context ->
33 val documentationModule = DocumentationModule("test")
34 val documentationBuilder = DocumentationBuilder(context, options)
35 with(documentationBuilder) {
36 documentationModule.appendFiles(environment.getSourceFiles())
37 }
38 documentationBuilder.resolveReferences(documentationModule)
39 documentationModule
Ilya Ryzhenkov08e69002014-07-14 15:44:32 +040040 }
41 verifier(documentation)
Ilya Ryzhenkov044e1b82014-07-11 17:11:35 +040042 Disposer.dispose(environment)
43}
44
Ilya Ryzhenkov11355ce2014-10-12 22:35:47 +040045fun StringBuilder.appendChildren(node: ContentNode): StringBuilder {
Ilya Ryzhenkov778e2b32014-09-29 20:54:59 +040046 for (child in node.children) {
47 val childText = child.toTestString()
48 append(childText)
49 }
50 return this
51}
52
Ilya Ryzhenkov11355ce2014-10-12 22:35:47 +040053fun StringBuilder.appendNode(node: ContentNode): StringBuilder {
Ilya Ryzhenkov778e2b32014-09-29 20:54:59 +040054 when (node) {
55 is ContentText -> {
56 append(node.text)
57 }
58 is ContentEmphasis -> append("*").appendChildren(node).append("*")
59 else -> {
60 appendChildren(node)
61 }
62 }
63 return this
64}
65
66fun ContentNode.toTestString(): String {
67 val node = this
68 return StringBuilder {
69 appendNode(node)
70 }.toString()
71}