diff options
17 files changed, 311 insertions, 95 deletions
diff --git a/core/src/main/kotlin/Formats/FormatDescriptor.kt b/core/src/main/kotlin/Formats/FormatDescriptor.kt index e384f223..7cc50acb 100644 --- a/core/src/main/kotlin/Formats/FormatDescriptor.kt +++ b/core/src/main/kotlin/Formats/FormatDescriptor.kt @@ -1,6 +1,7 @@ package org.jetbrains.dokka.Formats import org.jetbrains.dokka.* +import org.jetbrains.dokka.Samples.SampleProcessingService import kotlin.reflect.KClass interface FormatDescriptor { @@ -9,4 +10,5 @@ interface FormatDescriptor { val generatorServiceClass: KClass<out Generator> val packageDocumentationBuilderClass: KClass<out PackageDocumentationBuilder> val javaDocumentationBuilderClass: KClass<out JavaDocumentationBuilder> + val sampleProcessingService: KClass<out SampleProcessingService> } diff --git a/core/src/main/kotlin/Formats/KotlinWebsiteFormatService.kt b/core/src/main/kotlin/Formats/KotlinWebsiteFormatService.kt index bc9358ac..ac70bb02 100644 --- a/core/src/main/kotlin/Formats/KotlinWebsiteFormatService.kt +++ b/core/src/main/kotlin/Formats/KotlinWebsiteFormatService.kt @@ -30,11 +30,13 @@ class KotlinWebsiteOutputBuilder(to: StringBuilder, override fun appendStrikethrough(body: () -> Unit) = wrapInTag("s", body) - private fun div(to: StringBuilder, cssClass: String, block: () -> Unit) { - to.append("<div class=\"$cssClass\">") - insideDiv++ + private fun div(to: StringBuilder, cssClass: String, markdown: Boolean = false, block: () -> Unit) { + to.append("<div class=\"$cssClass\"") + if (markdown) to.append(" markdown=\"1\"") + to.append(">") + if (!markdown) insideDiv++ block() - insideDiv-- + if (!markdown) insideDiv-- to.append("</div>\n") } @@ -51,12 +53,23 @@ class KotlinWebsiteOutputBuilder(to: StringBuilder, } } + override fun appendSampleBlockCode(language: String, imports: () -> Unit, body: () -> Unit) { + div(to, "sample", true) { + appendBlockCode(language) { + imports() + wrap("\nfun main(args: Array<String>) {", "}") { + wrap("\n//sampleStart\n", "\n//sampleEnd\n", body) + } + } + } + } + override fun appendAsOverloadGroup(to: StringBuilder, block: () -> Unit) { - to.append("<div class=\"overload-group\" markdown=\"1\">") - ensureParagraph() - block() - ensureParagraph() - to.append("</div>") + div(to, "overload-group", true) { + ensureParagraph() + block() + ensureParagraph() + } } override fun appendLink(href: String, body: () -> Unit) = wrap("<a href=\"$href\">", "</a>", body) diff --git a/core/src/main/kotlin/Formats/StandardFormats.kt b/core/src/main/kotlin/Formats/StandardFormats.kt index fdc8eb9e..dabde881 100644 --- a/core/src/main/kotlin/Formats/StandardFormats.kt +++ b/core/src/main/kotlin/Formats/StandardFormats.kt @@ -1,6 +1,9 @@ package org.jetbrains.dokka.Formats import org.jetbrains.dokka.* +import org.jetbrains.dokka.Samples.DefaultSampleProcessingService +import org.jetbrains.dokka.Samples.KotlinWebsiteSampleProcessingService +import org.jetbrains.dokka.Samples.SampleProcessingService import kotlin.reflect.KClass abstract class KotlinFormatDescriptorBase : FormatDescriptor { @@ -9,6 +12,7 @@ abstract class KotlinFormatDescriptorBase : FormatDescriptor { override val generatorServiceClass = FileGenerator::class override val outlineServiceClass: KClass<out OutlineFormatService>? = null + override val sampleProcessingService: KClass<out SampleProcessingService> = DefaultSampleProcessingService::class } class HtmlFormatDescriptor : KotlinFormatDescriptorBase() { @@ -22,11 +26,13 @@ class HtmlAsJavaFormatDescriptor : FormatDescriptor { override val generatorServiceClass = FileGenerator::class override val packageDocumentationBuilderClass = KotlinAsJavaDocumentationBuilder::class override val javaDocumentationBuilderClass = JavaPsiDocumentationBuilder::class + override val sampleProcessingService: KClass<out SampleProcessingService> = DefaultSampleProcessingService::class } class KotlinWebsiteFormatDescriptor : KotlinFormatDescriptorBase() { override val formatServiceClass = KotlinWebsiteFormatService::class override val outlineServiceClass = YamlOutlineService::class + override val sampleProcessingService: KClass<out SampleProcessingService> = KotlinWebsiteSampleProcessingService::class } class JekyllFormatDescriptor : KotlinFormatDescriptorBase() { diff --git a/core/src/main/kotlin/Formats/StructuredFormatService.kt b/core/src/main/kotlin/Formats/StructuredFormatService.kt index a7a50f91..7896bcd8 100644 --- a/core/src/main/kotlin/Formats/StructuredFormatService.kt +++ b/core/src/main/kotlin/Formats/StructuredFormatService.kt @@ -45,6 +45,7 @@ abstract class StructuredOutputBuilder(val to: StringBuilder, protected abstract fun ensureParagraph() + open fun appendSampleBlockCode(language: String, imports: () -> Unit, body: () -> Unit) = appendBlockCode(language, body) abstract fun appendBlockCode(language: String, body: () -> Unit) abstract fun appendHeader(level: Int = 1, body: () -> Unit) abstract fun appendParagraph(body: () -> Unit) @@ -140,13 +141,22 @@ abstract class StructuredOutputBuilder(val to: StringBuilder, } } - is ContentBlockCode -> appendBlockCode(content.language) { - for ((index, contentNode) in content.children.withIndex()) { - appendContent(contentNode) - if (index < content.children.size - 1) { - to.append("\n") + is ContentBlockSampleCode, is ContentBlockCode -> { + content as ContentBlockCode + fun ContentBlockCode.appendBlockCodeContent() { + for ((index, contentNode) in this.children.withIndex()) { + appendContent(contentNode) + if (index < this.children.size - 1) { + to.append("\n") + } } } + when (content) { + is ContentBlockSampleCode -> + appendSampleBlockCode(content.language, content.importsBlock::appendBlockCodeContent, { content.appendBlockCodeContent() }) + is ContentBlockCode -> + appendBlockCode(content.language, { content.appendBlockCodeContent() }) + } } is ContentHeading -> appendHeader(content.level) { appendContent(content.children) } is ContentBlock -> appendContent(content.children) diff --git a/core/src/main/kotlin/Kotlin/DescriptorDocumentationParser.kt b/core/src/main/kotlin/Kotlin/DescriptorDocumentationParser.kt index 941d071d..d1f64eeb 100644 --- a/core/src/main/kotlin/Kotlin/DescriptorDocumentationParser.kt +++ b/core/src/main/kotlin/Kotlin/DescriptorDocumentationParser.kt @@ -5,24 +5,20 @@ import com.intellij.psi.PsiDocCommentOwner import com.intellij.psi.PsiNamedElement import com.intellij.psi.util.PsiTreeUtil import org.jetbrains.dokka.* +import org.jetbrains.dokka.Samples.SampleProcessingService import org.jetbrains.kotlin.descriptors.* import org.jetbrains.kotlin.idea.kdoc.findKDoc -import org.jetbrains.kotlin.idea.kdoc.getKDocLinkResolutionScope import org.jetbrains.kotlin.incremental.components.NoLookupLocation +import org.jetbrains.kotlin.kdoc.parser.KDocKnownTag import org.jetbrains.kotlin.kdoc.psi.impl.KDocSection import org.jetbrains.kotlin.kdoc.psi.impl.KDocTag import org.jetbrains.kotlin.load.java.descriptors.JavaCallableMemberDescriptor import org.jetbrains.kotlin.load.java.descriptors.JavaClassDescriptor import org.jetbrains.kotlin.name.FqName -import org.jetbrains.kotlin.name.Name -import org.jetbrains.kotlin.psi.KtBlockExpression -import org.jetbrains.kotlin.psi.KtDeclarationWithBody -import org.jetbrains.kotlin.resolve.DescriptorToSourceUtils import org.jetbrains.kotlin.resolve.DescriptorUtils import org.jetbrains.kotlin.resolve.annotations.argumentValue import org.jetbrains.kotlin.resolve.constants.StringValue import org.jetbrains.kotlin.resolve.scopes.DescriptorKindFilter -import org.jetbrains.kotlin.resolve.scopes.ResolutionScope import org.jetbrains.kotlin.resolve.scopes.getDescriptorsFiltered import org.jetbrains.kotlin.resolve.source.PsiSourceElement @@ -31,7 +27,8 @@ class DescriptorDocumentationParser val logger: DokkaLogger, val linkResolver: DeclarationLinkResolver, val resolutionFacade: DokkaResolutionFacade, - val refGraph: NodeReferenceGraph) + val refGraph: NodeReferenceGraph, + val sampleService: SampleProcessingService) { fun parseDocumentation(descriptor: DeclarationDescriptor, inline: Boolean = false): Content = parseDocumentationAndDetails(descriptor, inline).first @@ -60,10 +57,10 @@ class DescriptorDocumentationParser if (kdoc is KDocSection) { val tags = kdoc.getTags() tags.forEach { - when (it.name) { - "sample" -> - content.append(functionBody(descriptor, it.getSubjectName())) - "see" -> + when (it.knownTag) { + KDocKnownTag.SAMPLE -> + content.append(sampleService.resolveSample(descriptor, it.getSubjectName())) + KDocKnownTag.SEE -> content.addTagToSeeAlso(descriptor, it) else -> { val section = content.addSection(javadocSectionDisplayName(it.name), it.getSubjectName()) @@ -144,68 +141,4 @@ class DescriptorDocumentationParser } } - private fun functionBody(descriptor: DeclarationDescriptor, functionName: String?): ContentNode { - if (functionName == null) { - logger.warn("Missing function name in @sample in ${descriptor.signature()}") - return ContentBlockCode().let() { it.append(ContentText("Missing function name in @sample")); it } - } - val scope = getKDocLinkResolutionScope(resolutionFacade, descriptor) - val rootPackage = resolutionFacade.moduleDescriptor.getPackage(FqName.ROOT) - val rootScope = rootPackage.memberScope - val symbol = resolveInScope(functionName, scope) ?: resolveInScope(functionName, rootScope) - if (symbol == null) { - logger.warn("Unresolved function $functionName in @sample in ${descriptor.signature()}") - return ContentBlockCode().let() { it.append(ContentText("Unresolved: $functionName")); it } - } - val psiElement = DescriptorToSourceUtils.descriptorToDeclaration(symbol) - if (psiElement == null) { - logger.warn("Can't find source for function $functionName in @sample in ${descriptor.signature()}") - return ContentBlockCode().let() { it.append(ContentText("Source not found: $functionName")); it } - } - - val text = when (psiElement) { - is KtDeclarationWithBody -> ContentBlockCode().let() { - val bodyExpression = psiElement.bodyExpression - when (bodyExpression) { - is KtBlockExpression -> bodyExpression.text.removeSurrounding("{", "}") - else -> bodyExpression!!.text - } - } - else -> psiElement.text - } - - val lines = text.trimEnd().split("\n".toRegex()).toTypedArray().filterNot { it.length == 0 } - val indent = lines.map { it.takeWhile { it.isWhitespace() }.count() }.min() ?: 0 - val finalText = lines.map { it.drop(indent) }.joinToString("\n") - return ContentBlockCode("kotlin").let() { it.append(ContentText(finalText)); it } - } - - private fun resolveInScope(functionName: String, scope: ResolutionScope): DeclarationDescriptor? { - var currentScope = scope - val parts = functionName.split('.') - - var symbol: DeclarationDescriptor? = null - - for (part in parts) { - // short name - val symbolName = Name.identifier(part) - val partSymbol = currentScope.getContributedDescriptors(DescriptorKindFilter.ALL, { it == symbolName }) - .filter { it.name == symbolName } - .firstOrNull() - - if (partSymbol == null) { - symbol = null - break - } - currentScope = if (partSymbol is ClassDescriptor) - partSymbol.defaultType.memberScope - else if (partSymbol is PackageViewDescriptor) - partSymbol.memberScope - else - getKDocLinkResolutionScope(resolutionFacade, partSymbol) - symbol = partSymbol - } - - return symbol - } } diff --git a/core/src/main/kotlin/Model/Content.kt b/core/src/main/kotlin/Model/Content.kt index 1a8bc5d2..c8585418 100644 --- a/core/src/main/kotlin/Model/Content.kt +++ b/core/src/main/kotlin/Model/Content.kt @@ -82,7 +82,8 @@ class ContentEmphasis() : ContentBlock() class ContentStrong() : ContentBlock() class ContentStrikethrough() : ContentBlock() class ContentCode() : ContentBlock() -class ContentBlockCode(val language: String = "") : ContentBlock() +open class ContentBlockCode(val language: String = "") : ContentBlock() +class ContentBlockSampleCode(language: String = "kotlin", val importsBlock: ContentBlockCode = ContentBlockCode(language)) : ContentBlockCode(language) abstract class ContentNodeLink() : ContentBlock() { abstract val node: DocumentationNode? diff --git a/core/src/main/kotlin/Samples/DefaultSampleProcessingService.kt b/core/src/main/kotlin/Samples/DefaultSampleProcessingService.kt new file mode 100644 index 00000000..e6539135 --- /dev/null +++ b/core/src/main/kotlin/Samples/DefaultSampleProcessingService.kt @@ -0,0 +1,106 @@ +package org.jetbrains.dokka.Samples + +import com.google.inject.Inject +import com.intellij.psi.PsiElement +import org.jetbrains.dokka.* +import org.jetbrains.kotlin.descriptors.ClassDescriptor +import org.jetbrains.kotlin.descriptors.DeclarationDescriptor +import org.jetbrains.kotlin.descriptors.PackageViewDescriptor +import org.jetbrains.kotlin.idea.kdoc.getKDocLinkResolutionScope +import org.jetbrains.kotlin.name.FqName +import org.jetbrains.kotlin.name.Name +import org.jetbrains.kotlin.psi.KtBlockExpression +import org.jetbrains.kotlin.psi.KtDeclarationWithBody +import org.jetbrains.kotlin.psi.KtFile +import org.jetbrains.kotlin.resolve.DescriptorToSourceUtils +import org.jetbrains.kotlin.resolve.scopes.DescriptorKindFilter +import org.jetbrains.kotlin.resolve.scopes.ResolutionScope + + +open class DefaultSampleProcessingService +@Inject constructor(val options: DocumentationOptions, + val logger: DokkaLogger, + val resolutionFacade: DokkaResolutionFacade) + : SampleProcessingService { + + override fun resolveSample(descriptor: DeclarationDescriptor, functionName: String?): ContentNode { + if (functionName == null) { + logger.warn("Missing function name in @sample in ${descriptor.signature()}") + return ContentBlockSampleCode().apply { append(ContentText("//Missing function name in @sample")) } + } + val scope = getKDocLinkResolutionScope(resolutionFacade, descriptor) + val rootPackage = resolutionFacade.moduleDescriptor.getPackage(FqName.ROOT) + val rootScope = rootPackage.memberScope + val symbol = resolveInScope(functionName, scope) ?: resolveInScope(functionName, rootScope) + if (symbol == null) { + logger.warn("Unresolved function $functionName in @sample in ${descriptor.signature()}") + return ContentBlockSampleCode().apply { append(ContentText("//Unresolved: $functionName")) } + } + val psiElement = DescriptorToSourceUtils.descriptorToDeclaration(symbol) + if (psiElement == null) { + logger.warn("Can't find source for function $functionName in @sample in ${descriptor.signature()}") + return ContentBlockSampleCode().apply { append(ContentText("//Source not found: $functionName")) } + } + + val text = processSampleBody(psiElement) + + val lines = text.trimEnd().split("\n".toRegex()).toTypedArray().filterNot(String::isEmpty) + val indent = lines.map { it.takeWhile(Char::isWhitespace).count() }.min() ?: 0 + val finalText = lines.map { it.drop(indent) }.joinToString("\n") + + return ContentBlockSampleCode(importsBlock = processImports(psiElement)).apply { append(ContentText(finalText)) } + } + + protected open fun processSampleBody(psiElement: PsiElement): String = when (psiElement) { + is KtDeclarationWithBody -> { + val bodyExpression = psiElement.bodyExpression + when (bodyExpression) { + is KtBlockExpression -> bodyExpression.text.removeSurrounding("{", "}") + else -> bodyExpression!!.text + } + } + else -> psiElement.text + } + + protected open fun processImports(psiElement: PsiElement): ContentBlockCode { + val psiFile = psiElement.containingFile + if (psiFile is KtFile) { + return ContentBlockCode("kotlin").apply { + append(ContentText(psiFile.importList?.text ?: "")) + } + } else { + return ContentBlockCode("") + } + } + + private fun resolveInScope(functionName: String, scope: ResolutionScope): DeclarationDescriptor? { + var currentScope = scope + val parts = functionName.split('.') + + var symbol: DeclarationDescriptor? = null + + for (part in parts) { + // short name + val symbolName = Name.identifier(part) + val partSymbol = currentScope.getContributedDescriptors(DescriptorKindFilter.ALL, { it == symbolName }) + .filter { it.name == symbolName } + .firstOrNull() + + if (partSymbol == null) { + symbol = null + break + } + @Suppress("IfThenToElvis") + currentScope = if (partSymbol is ClassDescriptor) + partSymbol.defaultType.memberScope + else if (partSymbol is PackageViewDescriptor) + partSymbol.memberScope + else + getKDocLinkResolutionScope(resolutionFacade, partSymbol) + symbol = partSymbol + } + + return symbol + } +} + diff --git a/core/src/main/kotlin/Samples/KotlinWebsiteSampleProcessingService.kt b/core/src/main/kotlin/Samples/KotlinWebsiteSampleProcessingService.kt new file mode 100644 index 00000000..648a6b67 --- /dev/null +++ b/core/src/main/kotlin/Samples/KotlinWebsiteSampleProcessingService.kt @@ -0,0 +1,80 @@ +package org.jetbrains.dokka.Samples + +import com.google.inject.Inject +import com.intellij.psi.PsiElement +import org.jetbrains.dokka.DocumentationOptions +import org.jetbrains.dokka.DokkaLogger +import org.jetbrains.dokka.DokkaResolutionFacade +import org.jetbrains.kotlin.psi.* + +open class KotlinWebsiteSampleProcessingService +@Inject constructor(options: DocumentationOptions, + logger: DokkaLogger, + resolutionFacade: DokkaResolutionFacade) + : DefaultSampleProcessingService(options, logger, resolutionFacade) { + + private class SampleBuilder() : KtVisitorVoid() { + val builder = StringBuilder() + val text: String + get() = builder.toString() + + fun convertAssertPrints(expression: KtCallExpression) { + val (argument, commentArgument) = expression.valueArguments + val comment = commentArgument.getArgumentExpression() as KtStringTemplateExpression + val commentText = comment.entries.joinToString("") { it.text } + builder.apply { + append("println(") + append(argument.text) + append(") // ") + append(commentText) + } + } + + fun convertAssertTrue(expression: KtCallExpression) { + val (argument) = expression.valueArguments + builder.apply { + append("println(\"") + append(argument.text) + append(" is \${") + append(argument.text) + append("}\") // true") + } + } + + override fun visitCallExpression(expression: KtCallExpression) { + when (expression.calleeExpression?.text) { + "assertPrints" -> convertAssertPrints(expression) + "assertTrue" -> convertAssertTrue(expression) + else -> super.visitCallExpression(expression) + } + } + + override fun visitElement(element: PsiElement?) { + if (element != null) { + if (element.children.isEmpty()) + builder.append(element.text) + else + element.acceptChildren(this) + } + } + } + + private fun PsiElement.buildSampleText(): String { + val sampleBuilder = SampleBuilder() + this.accept(sampleBuilder) + return sampleBuilder.text + } + + override fun processSampleBody(psiElement: PsiElement) = when (psiElement) { + is KtDeclarationWithBody -> { + val bodyExpression = psiElement.bodyExpression + val bodyExpressionText = bodyExpression!!.buildSampleText() + when (bodyExpression) { + is KtBlockExpression -> bodyExpressionText.removeSurrounding("{", "}") + else -> bodyExpressionText + } + } + else -> psiElement.buildSampleText() + } +} + diff --git a/core/src/main/kotlin/Samples/SampleProcessingService.kt b/core/src/main/kotlin/Samples/SampleProcessingService.kt new file mode 100644 index 00000000..7f46299f --- /dev/null +++ b/core/src/main/kotlin/Samples/SampleProcessingService.kt @@ -0,0 +1,8 @@ +package org.jetbrains.dokka.Samples + +import org.jetbrains.dokka.ContentNode +import org.jetbrains.kotlin.descriptors.DeclarationDescriptor + +interface SampleProcessingService { + fun resolveSample(descriptor: DeclarationDescriptor, functionName: String?): ContentNode +}
\ No newline at end of file diff --git a/core/src/main/kotlin/Utilities/DokkaModule.kt b/core/src/main/kotlin/Utilities/DokkaModule.kt index 1eb82313..e1ae829a 100644 --- a/core/src/main/kotlin/Utilities/DokkaModule.kt +++ b/core/src/main/kotlin/Utilities/DokkaModule.kt @@ -6,6 +6,7 @@ import com.google.inject.Provider import com.google.inject.name.Names import org.jetbrains.dokka.* import org.jetbrains.dokka.Formats.FormatDescriptor +import org.jetbrains.dokka.Samples.SampleProcessingService import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment import java.io.File @@ -44,6 +45,7 @@ class DokkaModule(val environment: AnalysisEnvironment, } binder.bind<PackageDocumentationBuilder>().to(descriptor.packageDocumentationBuilderClass.java) binder.bind<JavaDocumentationBuilder>().to(descriptor.javaDocumentationBuilderClass.java) + binder.bind<SampleProcessingService>().to(descriptor.sampleProcessingService.java) binder.bind<Generator>().to(descriptor.generatorServiceClass.java) diff --git a/core/src/main/kotlin/javadoc/dokka-adapters.kt b/core/src/main/kotlin/javadoc/dokka-adapters.kt index 23ee1702..920eca2d 100644 --- a/core/src/main/kotlin/javadoc/dokka-adapters.kt +++ b/core/src/main/kotlin/javadoc/dokka-adapters.kt @@ -4,6 +4,7 @@ import com.google.inject.Inject import com.sun.tools.doclets.formats.html.HtmlDoclet import org.jetbrains.dokka.* import org.jetbrains.dokka.Formats.FormatDescriptor +import org.jetbrains.dokka.Samples.DefaultSampleProcessingService class JavadocGenerator @Inject constructor (val options: DocumentationOptions, val logger: DokkaLogger) : Generator { override fun buildPages(nodes: Iterable<DocumentationNode>) { @@ -27,4 +28,5 @@ class JavadocFormatDescriptor : FormatDescriptor { override val generatorServiceClass = JavadocGenerator::class override val packageDocumentationBuilderClass = KotlinAsJavaDocumentationBuilder::class override val javaDocumentationBuilderClass = JavaPsiDocumentationBuilder::class + override val sampleProcessingService = DefaultSampleProcessingService::class } diff --git a/core/src/test/kotlin/TestAPI.kt b/core/src/test/kotlin/TestAPI.kt index 508d28fc..7197d2c4 100644 --- a/core/src/test/kotlin/TestAPI.kt +++ b/core/src/test/kotlin/TestAPI.kt @@ -125,8 +125,9 @@ fun verifyOutput(roots: Array<ContentRoot>, outputExtension: String, withJdk: Boolean = false, withKotlinRuntime: Boolean = false, + format: String = "html", outputGenerator: (DocumentationModule, StringBuilder) -> Unit) { - verifyModel(*roots, withJdk = withJdk, withKotlinRuntime = withKotlinRuntime) { + verifyModel(*roots, withJdk = withJdk, withKotlinRuntime = withKotlinRuntime, format = format) { verifyModelOutput(it, outputExtension, outputGenerator, roots.first().path) } } @@ -147,8 +148,9 @@ fun verifyOutput(path: String, outputExtension: String, withJdk: Boolean = false, withKotlinRuntime: Boolean = false, + format: String = "html", outputGenerator: (DocumentationModule, StringBuilder) -> Unit) { - verifyOutput(arrayOf(contentRootFromPath(path)), outputExtension, withJdk, withKotlinRuntime, outputGenerator) + verifyOutput(arrayOf(contentRootFromPath(path)), outputExtension, withJdk, withKotlinRuntime, format, outputGenerator) } fun verifyJavaOutput(path: String, diff --git a/core/src/test/kotlin/format/KotlinWebSiteFormatTest.kt b/core/src/test/kotlin/format/KotlinWebSiteFormatTest.kt index e7419ec8..e7677862 100644 --- a/core/src/test/kotlin/format/KotlinWebSiteFormatTest.kt +++ b/core/src/test/kotlin/format/KotlinWebSiteFormatTest.kt @@ -19,8 +19,12 @@ class KotlinWebSiteFormatTest { verifyKWSNodeByName("overloadGroup", "magic") } + @Test fun sampleWithAsserts() { + verifyKWSNodeByName("sampleWithAsserts", "a") + } + private fun verifyKWSNodeByName(fileName: String, name: String) { - verifyOutput("testdata/format/website/$fileName.kt", ".md") { model, output -> + verifyOutput("testdata/format/website/$fileName.kt", ".md", format = "kotlin-website") { model, output -> kwsService.createOutputBuilder(output, tempLocation).appendNodes(model.members.single().members.filter { it.name == name }) } } diff --git a/core/testdata/format/website/overloadGroup.md b/core/testdata/format/website/overloadGroup.md index 81cb7822..d81f86bf 100644 --- a/core/testdata/format/website/overloadGroup.md +++ b/core/testdata/format/website/overloadGroup.md @@ -31,4 +31,4 @@ Spell ID for future casts **Return** Spell ID for future casts -</div>
\ No newline at end of file +</div> diff --git a/core/testdata/format/website/sample.md b/core/testdata/format/website/sample.md index 1a683da7..203f1b02 100644 --- a/core/testdata/format/website/sample.md +++ b/core/testdata/format/website/sample.md @@ -14,16 +14,25 @@ layout: api Groups elements of the original sequence by the key returned by the given <a href="#">keySelector</a> function applied to each element and returns a map where each group key is associated with a list of corresponding elements. +<div class="sample" markdown="1"> + ``` kotlin + +fun main(args: Array<String>) { +//sampleStart if (true) { println(property) } +//sampleEnd +} ``` </div> +</div> + <div class="overload-group" markdown="1"> <div class="signature"><code><span class="keyword">fun </span><span class="identifier">foo</span><span class="symbol">(</span><span class="parameterName" id="$foo(kotlin.Int)/i">i</span><span class="symbol">:</span> <span class="identifier">Int</span><span class="symbol">)</span><span class="symbol">: </span><span class="identifier">Int</span></code></div> -</div>
\ No newline at end of file +</div> diff --git a/core/testdata/format/website/sampleWithAsserts.kt b/core/testdata/format/website/sampleWithAsserts.kt new file mode 100644 index 00000000..bb9732d5 --- /dev/null +++ b/core/testdata/format/website/sampleWithAsserts.kt @@ -0,0 +1,15 @@ +/** + * @sample sample + */ +fun a(): String { + return "Hello, Work" +} + +fun b(): String { + return "Hello, Rest" +} + +fun sample() { + assertPrints(a(), "Hello, Work") + assertTrue(a() == b()) +}
\ No newline at end of file diff --git a/core/testdata/format/website/sampleWithAsserts.md b/core/testdata/format/website/sampleWithAsserts.md new file mode 100644 index 00000000..98d7df33 --- /dev/null +++ b/core/testdata/format/website/sampleWithAsserts.md @@ -0,0 +1,23 @@ +--- +title: a - test +layout: api +--- + +<div class='api-docs-breadcrumbs'><a href="test/index">test</a> / <a href="test/a">a</a></div> + +# a + +<div class="signature"><code><span class="keyword">fun </span><span class="identifier">a</span><span class="symbol">(</span><span class="symbol">)</span><span class="symbol">: </span><span class="identifier">String</span></code></div> +<div class="sample" markdown="1"> + +``` kotlin + +fun main(args: Array<String>) { +//sampleStart +println(a()) // Hello, Work +println("a() == b() is ${a() == b()}") // true +//sampleEnd +} +``` + +</div> |