Skip to content

Commit

Permalink
feat(kotlin): add baseRoute method to KotlinTestDataBuilder
Browse files Browse the repository at this point in the history
This commit adds a new method `baseRoute` to the `KotlinTestDataBuilder` class in order to extract the base route from a Kotlin function. The method uses the `KtNamedFunction` element to traverse the PSI tree and find the `RequestMapping` annotation. It then retrieves the value of the annotation and returns it as the base route. This method will be useful for generating test data for Kotlin controllers.
  • Loading branch information
phodal committed Jan 11, 2024
1 parent df4d12e commit 3da7e16
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 8 deletions.
Expand Up @@ -4,11 +4,41 @@ import cc.unitmesh.devti.provider.TestDataBuilder
import cc.unitmesh.kotlin.context.KotlinClassContextBuilder
import com.intellij.openapi.util.NlsSafe
import com.intellij.psi.PsiElement
import com.intellij.psi.PsiNameIdentifierOwner
import com.intellij.psi.util.PsiTreeUtil
import org.jetbrains.kotlin.idea.quickfix.createFromUsage.callableBuilder.getReturnTypeReference
import org.jetbrains.kotlin.idea.references.mainReference
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.psiUtil.getContentRange

class KotlinTestDataBuilder : TestDataBuilder {
override fun baseRoute(element: PsiElement): String {
if (element !is KtNamedFunction) return ""

val clazz = PsiTreeUtil.getParentOfType(element, PsiNameIdentifierOwner::class.java)
if (clazz !is KtClass) return ""

clazz.annotationEntries.forEach {
if (it.shortName?.asString() == "RequestMapping") {
return when (val value = it.valueArguments.firstOrNull()?.getArgumentExpression()) {
is KtStringTemplateExpression -> {
value.literalContents() ?: value.text
}

is KtSimpleNameExpression -> {
value.getReferencedName()
}

else -> {
"null"
}
}
}
}

return ""
}

override fun inboundData(element: PsiElement): Map<String, String> {
if (element !is KtNamedFunction) return emptyMap()

Expand Down Expand Up @@ -87,6 +117,15 @@ class KotlinTestDataBuilder : TestDataBuilder {
}
}

internal fun KtStringTemplateExpression.literalContents(): String? {
val escaper = createLiteralTextEscaper()
val ssb = StringBuilder()
return when(escaper.decode(getContentRange(), ssb)) {
true -> ssb.toString()
false -> null
}
}

fun KtReferenceExpression.resolveMainReference(): PsiElement? =
try {
mainReference.resolve()
Expand Down
Expand Up @@ -7,13 +7,7 @@ import org.jetbrains.kotlin.psi.KtPsiFactory


class KotlinTestDataBuilderTest : LightPlatformTestCase() {
fun testShouldPass() {
assertTrue(true)
}

// test will fail if 222
fun shouldReturnLangFileSuffix() {
val code = """
private val code = """
package cc.unitmesh.untitled.demo.controller
import org.springframework.web.bind.annotation.*
Expand All @@ -36,6 +30,21 @@ class KotlinTestDataBuilderTest : LightPlatformTestCase() {
)
""".trimIndent()

fun testShouldPass() {
assertTrue(true)
}

fun testShouldParseBaseRoute() {
val createFile = KtPsiFactory(project).createFile("UserController.kt", code)
val builder = KotlinTestDataBuilder()

val firstFunction = PsiTreeUtil.findChildOfType(createFile, KtNamedFunction::class.java)!!

assertEquals(builder.baseRoute(firstFunction), "/user")
}

// test will fail if 222
fun shouldReturnLangFileSuffix() {
val createFile = KtPsiFactory(project).createFile("UserController.kt", code)
val builder = KotlinTestDataBuilder()

Expand All @@ -44,7 +53,8 @@ class KotlinTestDataBuilderTest : LightPlatformTestCase() {

assertEquals(outboundData.size, 1)
assertEquals(
outboundData["cc.unitmesh.untitled.demo.controller.UserDTO"], "'package: cc.unitmesh.untitled.demo.controller.UserDTO\n" +
outboundData["cc.unitmesh.untitled.demo.controller.UserDTO"],
"'package: cc.unitmesh.untitled.demo.controller.UserDTO\n" +
"class UserDTO {\n" +
" \n" +
" \n" +
Expand Down

0 comments on commit 3da7e16

Please sign in to comment.