Skip to content

Commit

Permalink
feat(test): add KotlinMethodContextBuilderTest
Browse files Browse the repository at this point in the history
This commit adds a new test class `KotlinMethodContextBuilderTest` to test the functionality of the `KotlinMethodContextBuilder` class. The test case `testShouldIgnoreMethodComments` verifies that method comments are ignored when generating the signature string.
  • Loading branch information
phodal committed Jan 12, 2024
1 parent 4dbe802 commit 11536f3
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 7 deletions.
2 changes: 1 addition & 1 deletion docs/_config.yml
@@ -1,6 +1,6 @@
remote_theme: pmarsceill/just-the-docs

title: 🧙‍AutoDev - 开源全流程 AI 辅助编程插件
title: AutoDev - 编码流 AI 辅助
description: The AI-powered coding wizard with multilingual support 🌐, auto code generation 🏗️, and a helpful bug-slaying assistant 🐞! Customizable prompts 🎨 and a magic Auto Testing feature 🧪 included! 🚀

heading_anchors: true
Expand Down
Expand Up @@ -8,6 +8,7 @@ import com.intellij.psi.PsiNameIdentifierOwner
import org.jetbrains.kotlin.idea.quickfix.createFromUsage.callableBuilder.getReturnTypeReference
import org.jetbrains.kotlin.psi.KtNamedFunction
import org.jetbrains.kotlin.psi.psiUtil.containingClass
import org.jetbrains.kotlin.psi.psiUtil.endOffset

class KotlinMethodContextBuilder : MethodContextBuilder {
override fun getMethodContext(
Expand Down Expand Up @@ -40,16 +41,19 @@ class KotlinMethodContextBuilder : MethodContextBuilder {
}

object Util {
fun getSignatureString(signatureString: KtNamedFunction): String {
val bodyBlockExpression = signatureString.bodyBlockExpression
fun getSignatureString(ktNamedFunction: KtNamedFunction): String {
val bodyBlockExpression = ktNamedFunction.bodyBlockExpression
val startOffsetInParent = if (bodyBlockExpression != null) {
bodyBlockExpression.startOffsetInParent
} else {
val bodyExpression = signatureString.bodyExpression
bodyExpression?.startOffsetInParent ?: signatureString.textLength
val bodyExpression = ktNamedFunction.bodyExpression
bodyExpression?.startOffsetInParent ?: ktNamedFunction.textLength
}
val text = signatureString.text
val substring = text.substring(0, startOffsetInParent)

val docEnd = ktNamedFunction.docComment?.endOffset ?: 0

val text = ktNamedFunction.text
val substring = text.substring(docEnd, startOffsetInParent)
return substring.replace('\n', ' ').trim()
}
}
Expand Down
@@ -0,0 +1,38 @@
package cc.unitmesh.kotlin.context;

import com.intellij.psi.util.PsiTreeUtil
import com.intellij.testFramework.LightPlatformTestCase
import junit.framework.TestCase
import org.jetbrains.kotlin.psi.KtClassOrObject
import org.jetbrains.kotlin.psi.KtNamedFunction
import org.jetbrains.kotlin.psi.KtPsiFactory
import org.junit.Test


class KotlinMethodContextBuilderTest : LightPlatformTestCase() {
fun testShouldIgnoreMethodComments() {
// given
val code = """
/**
* It's a hello, world.
*/
fun main() {
println("Hello, World!")
}
""".trimIndent()

val createFile = KtPsiFactory(project).createFile("UserController.kt", code)
val clz = PsiTreeUtil.findChildOfType(createFile, KtNamedFunction::class.java)!!

val signatureString = KotlinMethodContextBuilder.Util.getSignatureString(clz)
TestCase.assertEquals(signatureString, "fun main()")
}

/**
* It's a hello, world.
*/
fun main() {
println("Hello, World!")
}

}

0 comments on commit 11536f3

Please sign in to comment.