blob: 6f4e34c23734772a4b5972d9919c27233a22e693 [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 Jemerovc43a4372014-12-29 20:22:43 +010054public fun verifyOutput(path: String, outputGenerator: (DocumentationModule, StringBuilder) -> Unit) {
55 verifyModel(path) {
56 val output = StringBuilder()
57 outputGenerator(it, output)
Dmitry Jemerov2016ba92014-12-29 21:05:53 +010058 val trimmedOutput = output.toString().split('\n').map { it.trimTrailing() }.join("\n")
Dmitry Jemerovc43a4372014-12-29 20:22:43 +010059 val expectedOutput = File(path.replace(".kt", ".md")).readText()
Dmitry Jemerov2016ba92014-12-29 21:05:53 +010060 assertEquals(expectedOutput.trimTrailing(), trimmedOutput)
Dmitry Jemerovc43a4372014-12-29 20:22:43 +010061 }
62}
63
Ilya Ryzhenkov11355ce2014-10-12 22:35:47 +040064fun StringBuilder.appendChildren(node: ContentNode): StringBuilder {
Ilya Ryzhenkov778e2b32014-09-29 20:54:59 +040065 for (child in node.children) {
66 val childText = child.toTestString()
67 append(childText)
68 }
69 return this
70}
71
Ilya Ryzhenkov11355ce2014-10-12 22:35:47 +040072fun StringBuilder.appendNode(node: ContentNode): StringBuilder {
Ilya Ryzhenkov778e2b32014-09-29 20:54:59 +040073 when (node) {
74 is ContentText -> {
75 append(node.text)
76 }
77 is ContentEmphasis -> append("*").appendChildren(node).append("*")
Ilya Ryzhenkovbd6cddd2014-12-16 21:41:32 +030078 is ContentNodeLink -> {
79 append("[")
80 appendChildren(node)
81 append(" -> ")
82 append(node.node.toString())
83 append("]")
84 }
Ilya Ryzhenkov778e2b32014-09-29 20:54:59 +040085 else -> {
86 appendChildren(node)
87 }
88 }
89 return this
90}
91
92fun ContentNode.toTestString(): String {
93 val node = this
94 return StringBuilder {
95 appendNode(node)
96 }.toString()
97}
Dmitry Jemerovc43a4372014-12-29 20:22:43 +010098
99val tempLocation = Location(File("/tmp/out"))
100
101object InMemoryLocationService: LocationService {
102 override fun location(node: DocumentationNode) = tempLocation;
103}