diff options
Diffstat (limited to 'core')
3 files changed, 53 insertions, 4 deletions
diff --git a/core/src/main/kotlin/Samples/KotlinWebsiteSampleProcessingService.kt b/core/src/main/kotlin/Samples/KotlinWebsiteSampleProcessingService.kt index 7ac43184..13b9f2de 100644 --- a/core/src/main/kotlin/Samples/KotlinWebsiteSampleProcessingService.kt +++ b/core/src/main/kotlin/Samples/KotlinWebsiteSampleProcessingService.kt @@ -3,6 +3,7 @@ package org.jetbrains.dokka.Samples import com.google.inject.Inject import com.intellij.psi.PsiElement import com.intellij.psi.impl.source.tree.LeafPsiElement +import com.intellij.psi.util.PsiTreeUtil import org.jetbrains.dokka.* import org.jetbrains.kotlin.psi.* import org.jetbrains.kotlin.psi.psiUtil.allChildren @@ -19,15 +20,18 @@ open class KotlinWebsiteSampleProcessingService val text: String get() = builder.toString() + fun KtValueArgument.extractStringArgumentValue() = + (getArgumentExpression() as KtStringTemplateExpression) + .entries.joinToString("") { it.text } + + 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) + append(commentArgument.extractStringArgumentValue()) } } @@ -42,10 +46,40 @@ open class KotlinWebsiteSampleProcessingService } } + fun convertAssertFails(expression: KtCallExpression) { + val (message, funcArgument) = expression.valueArguments + builder.apply { + val argument = if (funcArgument.getArgumentExpression() is KtLambdaExpression) + PsiTreeUtil.findChildOfType(funcArgument, KtBlockExpression::class.java)?.text ?: "" + else + funcArgument.text + append(argument.lines().joinToString(separator = "\n") { "// $it" }) + append(" // ") + append(message.extractStringArgumentValue()) + append(" will fail") + } + } + + fun convertAssertFailsWith(expression: KtCallExpression) { + val (funcArgument) = expression.valueArguments + val (exceptionType) = expression.typeArguments + builder.apply { + val argument = if (funcArgument.firstChild is KtLambdaExpression) + PsiTreeUtil.findChildOfType(funcArgument, KtBlockExpression::class.java)?.text ?: "" + else + funcArgument.text + append(argument.lines().joinToString(separator = "\n") { "// $it" }) + append(" // will fail with ") + append(exceptionType.text) + } + } + override fun visitCallExpression(expression: KtCallExpression) { when (expression.calleeExpression?.text) { "assertPrints" -> convertAssertPrints(expression) "assertTrue" -> convertAssertTrue(expression) + "assertFails" -> convertAssertFails(expression) + "assertFailsWith" -> convertAssertFailsWith(expression) else -> super.visitCallExpression(expression) } } diff --git a/core/testdata/format/website-html/sampleWithAsserts.html b/core/testdata/format/website-html/sampleWithAsserts.html index 012f91ab..7d014dbb 100644 --- a/core/testdata/format/website-html/sampleWithAsserts.html +++ b/core/testdata/format/website-html/sampleWithAsserts.html @@ -2,11 +2,14 @@ <h1>a</h1> <a name="$a()"></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"><pre><code class="lang-kotlin"> +<div class="sample"><pre><code class="lang-kotlin">import java.io.FileNotFoundException +import java.io.File fun main(args: Array<String>) { //sampleStart println(a()) // Hello, Work println("a() == b() is ${a() == b()}") // true +// readSomeFile(File("some.txt")) // reading file now will fail +// readSomeFile(File("some.txt")) // will fail with FileNotFoundException //sampleEnd }</code></pre></div> diff --git a/core/testdata/format/website-html/sampleWithAsserts.kt b/core/testdata/format/website-html/sampleWithAsserts.kt index bb9732d5..f5b03eb8 100644 --- a/core/testdata/format/website-html/sampleWithAsserts.kt +++ b/core/testdata/format/website-html/sampleWithAsserts.kt @@ -1,3 +1,6 @@ +import java.io.FileNotFoundException +import java.io.File + /** * @sample sample */ @@ -9,7 +12,16 @@ fun b(): String { return "Hello, Rest" } +/** + * @throws FileNotFoundException every time + */ +fun readSomeFile(f: File) { + throw FileNotFoundException("BOOM") +} + fun sample() { assertPrints(a(), "Hello, Work") assertTrue(a() == b()) + assertFails("reading file now") { readSomeFile(File("some.txt")) } + assertFailsWith<FileNotFoundException> { readSomeFile(File("some.txt")) } }
\ No newline at end of file |