Ilya Ryzhenkov | 8a4dad4 | 2014-07-11 20:41:53 +0400 | [diff] [blame] | 1 | package org.jetbrains.dokka.tests |
Ilya Ryzhenkov | 044e1b8 | 2014-07-11 17:11:35 +0400 | [diff] [blame] | 2 | |
Dmitry Jemerov | 69dd298 | 2014-12-30 18:47:03 +0100 | [diff] [blame] | 3 | import com.intellij.openapi.application.PathManager |
Dmitry Jemerov | bc0654e | 2015-05-27 19:26:13 +0200 | [diff] [blame] | 4 | import com.intellij.openapi.util.Disposer |
| 5 | import org.jetbrains.dokka.* |
| 6 | import org.jetbrains.kotlin.cli.common.messages.CompilerMessageLocation |
| 7 | import org.jetbrains.kotlin.cli.common.messages.CompilerMessageSeverity |
| 8 | import org.jetbrains.kotlin.cli.common.messages.MessageCollector |
| 9 | import org.jetbrains.kotlin.cli.jvm.config.JavaSourceRoot |
| 10 | import org.jetbrains.kotlin.config.ContentRoot |
| 11 | import org.jetbrains.kotlin.config.KotlinSourceRoot |
Dmitry Jemerov | 7fbff24 | 2015-01-09 18:54:06 +0100 | [diff] [blame] | 12 | import org.junit.Assert |
Dmitry Jemerov | bc0654e | 2015-05-27 19:26:13 +0200 | [diff] [blame] | 13 | import java.io.File |
| 14 | import kotlin.test.fail |
Ilya Ryzhenkov | 044e1b8 | 2014-07-11 17:11:35 +0400 | [diff] [blame] | 15 | |
Dmitry Jemerov | bc0654e | 2015-05-27 19:26:13 +0200 | [diff] [blame] | 16 | public fun verifyModel(vararg roots: ContentRoot, verifier: (DocumentationModule) -> Unit) { |
Ilya Ryzhenkov | 044e1b8 | 2014-07-11 17:11:35 +0400 | [diff] [blame] | 17 | val messageCollector = object : MessageCollector { |
| 18 | override fun report(severity: CompilerMessageSeverity, message: String, location: CompilerMessageLocation) { |
| 19 | when (severity) { |
| 20 | CompilerMessageSeverity.WARNING, |
| 21 | CompilerMessageSeverity.LOGGING, |
| 22 | CompilerMessageSeverity.OUTPUT, |
| 23 | CompilerMessageSeverity.INFO, |
| 24 | CompilerMessageSeverity.ERROR -> { |
| 25 | println("$severity: $message at $location") |
| 26 | } |
| 27 | CompilerMessageSeverity.EXCEPTION -> { |
| 28 | fail("$severity: $message at $location") |
| 29 | } |
| 30 | } |
| 31 | } |
| 32 | } |
| 33 | |
Ilya Ryzhenkov | 044308b | 2014-07-11 20:49:04 +0400 | [diff] [blame] | 34 | val environment = AnalysisEnvironment(messageCollector) { |
Dmitry Jemerov | 1e862f2 | 2015-09-09 14:54:19 +0200 | [diff] [blame^] | 35 | val stringRoot = PathManager.getResourceRoot(String::class.java, "/java/lang/String.class") |
Dmitry Jemerov | 69dd298 | 2014-12-30 18:47:03 +0100 | [diff] [blame] | 36 | addClasspath(File(stringRoot)) |
Dmitry Jemerov | 1e862f2 | 2015-09-09 14:54:19 +0200 | [diff] [blame^] | 37 | val kotlinPairRoot = PathManager.getResourceRoot(Pair::class.java, "/kotlin/Pair.class") |
| 38 | addClasspath(File(kotlinPairRoot)) |
Dmitry Jemerov | bc0654e | 2015-05-27 19:26:13 +0200 | [diff] [blame] | 39 | addRoots(roots.toList()) |
Ilya Ryzhenkov | 044e1b8 | 2014-07-11 17:11:35 +0400 | [diff] [blame] | 40 | } |
Dmitry Jemerov | 94d8cc0 | 2015-03-02 17:57:05 +0100 | [diff] [blame] | 41 | val options = DocumentationOptions(includeNonPublic = true, skipEmptyPackages = false, sourceLinks = listOf<SourceLinkDefinition>()) |
Dmitry Jemerov | f2fd617 | 2015-02-13 12:17:47 +0100 | [diff] [blame] | 42 | val documentation = buildDocumentationModule(environment, "test", options, logger = DokkaConsoleLogger) |
Ilya Ryzhenkov | 08e6900 | 2014-07-14 15:44:32 +0400 | [diff] [blame] | 43 | verifier(documentation) |
Ilya Ryzhenkov | 044e1b8 | 2014-07-11 17:11:35 +0400 | [diff] [blame] | 44 | Disposer.dispose(environment) |
| 45 | } |
| 46 | |
Dmitry Jemerov | bc0654e | 2015-05-27 19:26:13 +0200 | [diff] [blame] | 47 | public fun verifyModel(source: String, verifier: (DocumentationModule) -> Unit) { |
| 48 | verifyModel(contentRootFromPath(source), verifier = verifier) |
| 49 | } |
| 50 | |
| 51 | public fun verifyPackageMember(kotlinSource: String, verifier: (DocumentationNode) -> Unit) { |
| 52 | verifyModel(kotlinSource) { model -> |
Dmitry Jemerov | aa3f051 | 2015-02-13 17:04:58 +0100 | [diff] [blame] | 53 | val pkg = model.members.single() |
| 54 | verifier(pkg.members.single()) |
| 55 | } |
| 56 | } |
| 57 | |
Dmitry Jemerov | bc0654e | 2015-05-27 19:26:13 +0200 | [diff] [blame] | 58 | public fun verifyOutput(roots: Array<ContentRoot>, outputExtension: String, outputGenerator: (DocumentationModule, StringBuilder) -> Unit) { |
| 59 | verifyModel(*roots) { |
Dmitry Jemerov | c43a437 | 2014-12-29 20:22:43 +0100 | [diff] [blame] | 60 | val output = StringBuilder() |
| 61 | outputGenerator(it, output) |
Dmitry Jemerov | 64414ce | 2015-05-29 13:52:43 +0200 | [diff] [blame] | 62 | val ext = outputExtension.removePrefix(".") |
Dmitry Jemerov | bc0654e | 2015-05-27 19:26:13 +0200 | [diff] [blame] | 63 | val path = roots.first().path |
Dmitry Jemerov | fc70184 | 2015-02-25 20:06:16 +0100 | [diff] [blame] | 64 | val expectedOutput = File(path.replaceAfterLast(".", ext, path + "." + ext)).readText() |
Dmitry Jemerov | 599f32d | 2015-01-22 11:30:57 +0100 | [diff] [blame] | 65 | assertEqualsIgnoringSeparators(expectedOutput, output.toString()) |
Dmitry Jemerov | c43a437 | 2014-12-29 20:22:43 +0100 | [diff] [blame] | 66 | } |
| 67 | } |
| 68 | |
Dmitry Jemerov | bc0654e | 2015-05-27 19:26:13 +0200 | [diff] [blame] | 69 | public fun verifyOutput(path: String, outputExtension: String, outputGenerator: (DocumentationModule, StringBuilder) -> Unit) { |
| 70 | verifyOutput(arrayOf(contentRootFromPath(path)), outputExtension, outputGenerator) |
| 71 | } |
| 72 | |
Dmitry Jemerov | 599f32d | 2015-01-22 11:30:57 +0100 | [diff] [blame] | 73 | public fun assertEqualsIgnoringSeparators(expectedOutput: String, output: String) { |
| 74 | Assert.assertEquals(expectedOutput.replace("\r\n", "\n"), output.replace("\r\n", "\n")) |
| 75 | } |
| 76 | |
Dmitry Jemerov | 0d0fc1f | 2015-02-10 18:32:12 +0100 | [diff] [blame] | 77 | fun StringBuilder.appendChildren(node: ContentBlock): StringBuilder { |
Ilya Ryzhenkov | 778e2b3 | 2014-09-29 20:54:59 +0400 | [diff] [blame] | 78 | for (child in node.children) { |
| 79 | val childText = child.toTestString() |
| 80 | append(childText) |
| 81 | } |
| 82 | return this |
| 83 | } |
| 84 | |
Ilya Ryzhenkov | 11355ce | 2014-10-12 22:35:47 +0400 | [diff] [blame] | 85 | fun StringBuilder.appendNode(node: ContentNode): StringBuilder { |
Ilya Ryzhenkov | 778e2b3 | 2014-09-29 20:54:59 +0400 | [diff] [blame] | 86 | when (node) { |
| 87 | is ContentText -> { |
| 88 | append(node.text) |
| 89 | } |
| 90 | is ContentEmphasis -> append("*").appendChildren(node).append("*") |
Ilya Ryzhenkov | 1b5f12b | 2014-12-22 20:01:01 +0300 | [diff] [blame] | 91 | is ContentBlockCode -> { |
| 92 | appendln("[code]") |
| 93 | appendChildren(node) |
| 94 | appendln() |
| 95 | appendln("[/code]") |
| 96 | } |
Ilya Ryzhenkov | bd6cddd | 2014-12-16 21:41:32 +0300 | [diff] [blame] | 97 | is ContentNodeLink -> { |
| 98 | append("[") |
| 99 | appendChildren(node) |
| 100 | append(" -> ") |
| 101 | append(node.node.toString()) |
| 102 | append("]") |
| 103 | } |
Dmitry Jemerov | 0d0fc1f | 2015-02-10 18:32:12 +0100 | [diff] [blame] | 104 | is ContentBlock -> { |
Ilya Ryzhenkov | 778e2b3 | 2014-09-29 20:54:59 +0400 | [diff] [blame] | 105 | appendChildren(node) |
| 106 | } |
Dmitry Jemerov | 0d0fc1f | 2015-02-10 18:32:12 +0100 | [diff] [blame] | 107 | else -> throw IllegalStateException("Don't know how to format node $node") |
Ilya Ryzhenkov | 778e2b3 | 2014-09-29 20:54:59 +0400 | [diff] [blame] | 108 | } |
| 109 | return this |
| 110 | } |
| 111 | |
| 112 | fun ContentNode.toTestString(): String { |
| 113 | val node = this |
| 114 | return StringBuilder { |
| 115 | appendNode(node) |
| 116 | }.toString() |
| 117 | } |
Dmitry Jemerov | c43a437 | 2014-12-29 20:22:43 +0100 | [diff] [blame] | 118 | |
Dmitry Jemerov | d9bfa02 | 2015-02-19 18:59:00 +0100 | [diff] [blame] | 119 | class InMemoryLocation(override val path: String): Location { |
Dmitry Jemerov | 85a3ae7 | 2015-02-20 14:08:30 +0100 | [diff] [blame] | 120 | override fun relativePathTo(other: Location, anchor: String?): String = |
| 121 | if (anchor != null) other.path + "#" + anchor else other.path |
Dmitry Jemerov | d9bfa02 | 2015-02-19 18:59:00 +0100 | [diff] [blame] | 122 | } |
Dmitry Jemerov | c43a437 | 2014-12-29 20:22:43 +0100 | [diff] [blame] | 123 | |
| 124 | object InMemoryLocationService: LocationService { |
Dmitry Jemerov | ecadf40 | 2015-02-20 14:44:30 +0100 | [diff] [blame] | 125 | override fun location(qualifiedName: List<String>, hasMembers: Boolean) = |
| 126 | InMemoryLocation(relativePathToNode(qualifiedName, hasMembers)) |
Dmitry Jemerov | c43a437 | 2014-12-29 20:22:43 +0100 | [diff] [blame] | 127 | } |
Dmitry Jemerov | d9bfa02 | 2015-02-19 18:59:00 +0100 | [diff] [blame] | 128 | |
| 129 | val tempLocation = InMemoryLocation("") |
Dmitry Jemerov | bc0654e | 2015-05-27 19:26:13 +0200 | [diff] [blame] | 130 | |
| 131 | val ContentRoot.path: String |
| 132 | get() = when(this) { |
| 133 | is KotlinSourceRoot -> path |
| 134 | is JavaSourceRoot -> file.path |
| 135 | else -> throw UnsupportedOperationException() |
| 136 | } |