From 044e1b86602662c78bb2ed12b9d645ab10b23429 Mon Sep 17 00:00:00 2001 From: Ilya Ryzhenkov Date: Fri, 11 Jul 2014 17:11:35 +0400 Subject: Setup test framework and empty DocumentationModel to kickstart test development. --- src/AnalysesEnvironment.kt | 71 ++++++++++++++++++++++++++++++++++++++++++++++ src/AnalysisEnvironment.kt | 65 ------------------------------------------ src/CommentsAPI.kt | 2 +- src/DocumentationModel.kt | 14 +++++++++ src/main.kt | 9 +++--- test/data/discovery.kt | 0 test/data/function.kt | 0 test/playground.kt | 16 +++++++++-- test/src/DiscoveryTest.kt | 12 +++++++- test/src/TestAPI.kt | 36 +++++++++++++++++++++++ 10 files changed, 152 insertions(+), 73 deletions(-) create mode 100644 src/AnalysesEnvironment.kt delete mode 100644 src/AnalysisEnvironment.kt create mode 100644 src/DocumentationModel.kt delete mode 100644 test/data/discovery.kt create mode 100644 test/data/function.kt create mode 100644 test/src/TestAPI.kt diff --git a/src/AnalysesEnvironment.kt b/src/AnalysesEnvironment.kt new file mode 100644 index 00000000..3eae33ca --- /dev/null +++ b/src/AnalysesEnvironment.kt @@ -0,0 +1,71 @@ +package com.jetbrains.dokka + +import com.intellij.openapi.* +import org.jetbrains.jet.cli.common.messages.* +import org.jetbrains.jet.cli.common.* +import org.jetbrains.jet.cli.jvm.compiler.* +import java.io.* +import org.jetbrains.jet.cli.jvm.* +import org.jetbrains.jet.config.* +import com.intellij.openapi.util.* +import org.jetbrains.jet.lang.resolve.* +import org.jetbrains.jet.lang.psi.* +import kotlin.platform.* + +public class AnalysesEnvironment(val messageCollector: MessageCollector, body: AnalysesEnvironment.() -> Unit = {}) : Disposable { + val configuration = CompilerConfiguration(); + + { + configuration.put(CLIConfigurationKeys.MESSAGE_COLLECTOR_KEY, messageCollector) + body() + } + + private fun withContext(processor: (JetCoreEnvironment, BindingContext) -> T): T { + val environment = JetCoreEnvironment.createForProduction(this, configuration) + val result = environment.analyze(messageCollector) + return processor(environment, result) + } + + public fun withContext(processor: (BindingContext) -> T): T { + return withContext { environment, context -> processor(context) } + } + + public fun streamFiles(processor: (BindingContext, JetFile) -> T): Stream { + return withContext { environment, context -> + environment.getSourceFiles().stream().map { file -> processor(context, file) } + } + } + + public fun processFiles(processor: (BindingContext, JetFile) -> T): List { + return withContext { environment, context -> + environment.getSourceFiles().map { file -> processor(context, file) } + } + } + + public fun processFilesFlat(processor: (BindingContext, JetFile) -> List): List { + return withContext { environment, context -> + environment.getSourceFiles().flatMap { file -> processor(context, file) } + } + } + + public val classpath: List + get() = configuration.get(JVMConfigurationKeys.CLASSPATH_KEY) ?: listOf() + + public fun addClasspath(list: List) { + configuration.addAll(JVMConfigurationKeys.CLASSPATH_KEY, list) + } + + public fun addClasspath(file: File) { + configuration.add(JVMConfigurationKeys.CLASSPATH_KEY, file) + } + + public val sources: List + get() = configuration.get(CommonConfigurationKeys.SOURCE_ROOTS_KEY) ?: listOf() + public fun addSources(list: List) { + configuration.addAll(CommonConfigurationKeys.SOURCE_ROOTS_KEY, list) + } + + public override fun dispose() { + Disposer.dispose(this) + } +} \ No newline at end of file diff --git a/src/AnalysisEnvironment.kt b/src/AnalysisEnvironment.kt deleted file mode 100644 index 13316419..00000000 --- a/src/AnalysisEnvironment.kt +++ /dev/null @@ -1,65 +0,0 @@ -package com.jetbrains.dokka - -import com.intellij.openapi.Disposable -import org.jetbrains.jet.config.CompilerConfiguration -import org.jetbrains.jet.cli.common.messages.MessageCollector -import org.jetbrains.jet.cli.common.CLIConfigurationKeys -import org.jetbrains.jet.cli.jvm.compiler.JetCoreEnvironment -import java.io.File -import org.jetbrains.jet.cli.jvm.JVMConfigurationKeys -import org.jetbrains.jet.config.CommonConfigurationKeys -import com.intellij.openapi.util.Disposer -import org.jetbrains.jet.lang.resolve.BindingContext -import org.jetbrains.jet.lang.psi.JetFile - -public class AnalysisEnvironment(val messageCollector: MessageCollector, body: AnalysisEnvironment.()->Unit = {}) : Disposable { - val configuration = CompilerConfiguration(); - - { - configuration.put(CLIConfigurationKeys.MESSAGE_COLLECTOR_KEY, messageCollector) - body() - } - - private fun processContext(processor: (JetCoreEnvironment, BindingContext) -> T): T { - val environment = JetCoreEnvironment.createForProduction(this, configuration) - val result = environment.analyze(messageCollector) - return processor(environment, result) - } - - public fun processContext(processor: (BindingContext) -> T): T { - return processContext { environment, context -> processor(context) } - } - - public fun streamFiles(processor: (BindingContext, JetFile) -> T): Stream { - return processContext { environment, context -> - environment.getSourceFiles().stream().map { file -> processor(context, file) } - } - } - - public fun processFiles(processor: (BindingContext, JetFile) -> T): List { - return processContext { environment, context -> - environment.getSourceFiles().map { file -> processor(context, file) } - } - } - - public val classpath: List - get() = configuration.get(JVMConfigurationKeys.CLASSPATH_KEY) ?: listOf() - - public fun addClasspath(list: List) { - configuration.addAll(JVMConfigurationKeys.CLASSPATH_KEY, list) - } - - public fun addClasspath(file: File) { - configuration.add(JVMConfigurationKeys.CLASSPATH_KEY, file) - } - - public val sources: List - get() = configuration.get(CommonConfigurationKeys.SOURCE_ROOTS_KEY) ?: listOf() - public fun addSources(list: List) { - configuration.addAll(CommonConfigurationKeys.SOURCE_ROOTS_KEY, list) - } - - public override fun dispose() { - Disposer.dispose(this) - } -} \ No newline at end of file diff --git a/src/CommentsAPI.kt b/src/CommentsAPI.kt index 4bfb19e6..21ba1067 100644 --- a/src/CommentsAPI.kt +++ b/src/CommentsAPI.kt @@ -6,7 +6,7 @@ import org.jetbrains.jet.lang.resolve.* import org.jetbrains.jet.kdoc.psi.api.* import org.jetbrains.jet.lang.psi.JetDeclaration -fun BindingContext.getComments(descriptor: DeclarationDescriptor): KDoc? { +fun BindingContext.getDocumentation(descriptor: DeclarationDescriptor): KDoc? { val psiElement = DescriptorToSourceUtils.descriptorToDeclaration(descriptor) if (psiElement == null) throw IllegalArgumentException("$descriptor doesn't have connection to source code, is it synthetic?") diff --git a/src/DocumentationModel.kt b/src/DocumentationModel.kt new file mode 100644 index 00000000..6da18d42 --- /dev/null +++ b/src/DocumentationModel.kt @@ -0,0 +1,14 @@ +package com.jetbrains.dokka + +import com.intellij.psi.PsiFile +import org.jetbrains.jet.lang.resolve.BindingContext + +public class DocumentationModel { + fun merge(other: DocumentationModel): DocumentationModel { + return DocumentationModel() + } +} + +fun BindingContext.createDocumentation(file: PsiFile): DocumentationModel { + return DocumentationModel() +} \ No newline at end of file diff --git a/src/main.kt b/src/main.kt index 3083ba90..04415070 100644 --- a/src/main.kt +++ b/src/main.kt @@ -13,10 +13,11 @@ public fun main(args: Array) { val compilerArguments = K2JVMCompilerArguments() val sources: List = Args.parse(compilerArguments, args) ?: listOf() - val environment = AnalysisEnvironment(MessageCollectorPlainTextToStream.PLAIN_TEXT_TO_SYSTEM_ERR) { + val environment = AnalysesEnvironment(MessageCollectorPlainTextToStream.PLAIN_TEXT_TO_SYSTEM_ERR) { +/* addClasspath(PathUtil.getJdkClassesRoots()) addClasspath(PathUtil.getKotlinPathsForCompiler().getRuntimePath()) - +*/ addSources(sources) } @@ -52,9 +53,9 @@ fun BindingContext.analyseFile(file: JetFile) { println("Package: ${packageFragment}") for (descriptor in packageFragment.getMemberScope().getAllDescriptors()) { println("Member: ${descriptor}") - val doc = getComments(descriptor) + val doc = getDocumentation(descriptor) if (doc != null) { - println("Comment: ${doc.getText()}") + println("Comment:\n${doc.getText()}") } println() } diff --git a/test/data/discovery.kt b/test/data/discovery.kt deleted file mode 100644 index e69de29b..00000000 diff --git a/test/data/function.kt b/test/data/function.kt new file mode 100644 index 00000000..e69de29b diff --git a/test/playground.kt b/test/playground.kt index 80992fbd..5bc5207a 100644 --- a/test/playground.kt +++ b/test/playground.kt @@ -6,10 +6,10 @@ fun topLevelFunction() { val topLevelConstantValue = "Hello" -val topLevelValue : String +val topLevelValue: String get() = "Bye bye" -var topLevelVariable : String +var topLevelVariable: String get() = "Modify me!" set(value) { } @@ -23,6 +23,18 @@ class Class { } +/** + * This is a class with constructor and space after doc + */ + +class ClassWithConstructor(val name: String) + +/** + * This is data class with constructor and comment after doc + */ +// irrelevant comment +data class DataClass(val name: String, val age: Int) {} + object Object { fun objectFunction() { } diff --git a/test/src/DiscoveryTest.kt b/test/src/DiscoveryTest.kt index 71a8aa4f..690f0404 100644 --- a/test/src/DiscoveryTest.kt +++ b/test/src/DiscoveryTest.kt @@ -1,2 +1,12 @@ -public class DiscoveryTest { +package com.jetbrains.dokka.tests + +import org.junit.Test + + +public class FunctionTest { + Test fun function() { + verifyFiles("test/data/function.kt") { + + } + } } \ No newline at end of file diff --git a/test/src/TestAPI.kt b/test/src/TestAPI.kt new file mode 100644 index 00000000..80dc47d5 --- /dev/null +++ b/test/src/TestAPI.kt @@ -0,0 +1,36 @@ +package com.jetbrains.dokka.tests + +import org.jetbrains.jet.cli.common.messages.* +import com.intellij.openapi.util.* +import com.jetbrains.dokka.* +import kotlin.test.fail + +public fun verifyFiles(vararg files: String, verifier: (DocumentationModel) -> Unit) { + val messageCollector = object : MessageCollector { + override fun report(severity: CompilerMessageSeverity, message: String, location: CompilerMessageLocation) { + when (severity) { + CompilerMessageSeverity.WARNING, + CompilerMessageSeverity.LOGGING, + CompilerMessageSeverity.OUTPUT, + CompilerMessageSeverity.INFO, + CompilerMessageSeverity.ERROR -> { + println("$severity: $message at $location") + } + CompilerMessageSeverity.EXCEPTION -> { + fail("$severity: $message at $location") + } + } + } + } + + val environment = AnalysesEnvironment(messageCollector) { + addSources(files.toList()) + } + + val result = environment.processFiles { context, file -> + context.createDocumentation(file) + }.fold(DocumentationModel()) {(aggregate, item) -> aggregate.merge(item) } + verifier(result) + Disposer.dispose(environment) +} + -- cgit