Skip to content

Commit

Permalink
feat(kotlin): add test case for KotlinTestDataBuilder
Browse files Browse the repository at this point in the history
This commit adds a new test case `testShouldHandleForList` to the `KotlinTestDataBuilderTest` class in the `kotlin` module. The test verifies the behavior of the `KotlinTestDataBuilder` class when handling a list type in a Kotlin class. It checks if the outbound data generated by the builder for a given function contains the expected information. The test is skipped if the IntelliJ IDEA version is older than 2022.2 (build 232). Additionally, the commit includes a minor refactoring in the `KotlinTestDataBuilder` class to improve code readability and maintainability.
  • Loading branch information
phodal committed Jan 11, 2024
1 parent 3da7e16 commit bca537e
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 14 deletions.
@@ -1,6 +1,7 @@
package cc.unitmesh.kotlin.provider

import cc.unitmesh.devti.provider.TestDataBuilder
import cc.unitmesh.idea.service.isProjectContent
import cc.unitmesh.kotlin.context.KotlinClassContextBuilder
import com.intellij.openapi.util.NlsSafe
import com.intellij.psi.PsiElement
Expand Down Expand Up @@ -45,21 +46,23 @@ class KotlinTestDataBuilder : TestDataBuilder {
val result = mutableMapOf<String, String>()
val parameters = element.valueParameters
for (parameter in parameters) {
result += handleFromType(parameter)
result += handleFromType(parameter, element)
}
return result
}

private fun handleFromType(parameter: KtParameter): Map<@NlsSafe String, String> {
private fun handleFromType(parameter: KtParameter, element: PsiElement): Map<@NlsSafe String, String> {
when (val type = parameter.typeReference?.typeElement) {
is KtClass -> processingClassType(type)
is KtClass -> processingClassType(type, element)
}

return emptyMap()
}


private fun processingClassType(type: KtClass): Map<@NlsSafe String, String> {
private fun processingClassType(type: KtClass, element: PsiElement): Map<@NlsSafe String, String> {
if (!isProjectContent(type)) return emptyMap()

val result = mutableMapOf<String, String>()
val fqn = type.fqName?.asString() ?: return result

Expand All @@ -78,11 +81,7 @@ class KotlinTestDataBuilder : TestDataBuilder {

val returnType = element.getReturnTypeReference() ?: return emptyMap()

val result = mutableMapOf<String, String>()

result += processing(returnType)

return result
return processing(returnType, element)
}

/**
Expand All @@ -96,19 +95,23 @@ class KotlinTestDataBuilder : TestDataBuilder {
val methods = element.declarations.filterIsInstance<KtNamedFunction>()
for (method in methods) {
val returnType = method.getReturnTypeReference() ?: continue
result += processing(returnType)
result += processing(returnType, element)
}

return result
}

private fun processing(returnType: KtTypeReference): Map<String, String> {
private fun processing(returnType: KtTypeReference, element: PsiElement): Map<String, String> {
val result = mutableMapOf<String, String>()
when (val typeElement = returnType.typeElement) {
is KtUserType -> {
val referenceExpression = typeElement.referenceExpression?.resolveMainReference()
if (referenceExpression is KtClass) {
result += processingClassType(referenceExpression)
result += processingClassType(referenceExpression, element)
}

typeElement.typeArgumentsAsTypes.forEach {
result += processing(it, element)
}
}
}
Expand All @@ -120,7 +123,7 @@ class KotlinTestDataBuilder : TestDataBuilder {
internal fun KtStringTemplateExpression.literalContents(): String? {
val escaper = createLiteralTextEscaper()
val ssb = StringBuilder()
return when(escaper.decode(getContentRange(), ssb)) {
return when (escaper.decode(getContentRange(), ssb)) {
true -> ssb.toString()
false -> null
}
Expand Down
@@ -1,5 +1,6 @@
package cc.unitmesh.kotlin.provider;

import com.intellij.openapi.application.ApplicationInfo
import com.intellij.psi.util.PsiTreeUtil
import com.intellij.testFramework.LightPlatformTestCase
import org.jetbrains.kotlin.psi.KtNamedFunction
Expand Down Expand Up @@ -43,8 +44,61 @@ class KotlinTestDataBuilderTest : LightPlatformTestCase() {
assertEquals(builder.baseRoute(firstFunction), "/user")
}

fun testShouldHandleForList() {
if(!isLatest()) {
println("skip testShouldReturnLangFileSuffix")
return
}

val code = """
package cc.unitmesh.untitled.demo.controller
import org.springframework.web.bind.annotation.*
@RestController
@RequestMapping("/user")
class UserController() {
@GetMapping
fun getUsers(): List<UserDTO> {
return listOf(UserDTO(1L, "username", "email", "name", "surname"))
}
}
data class UserDTO(
val id: Long? = null,
val username: String,
val email: String,
val name: String,
val surname: String? = null,
)
""".trimIndent()

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

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

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

private fun isLatest() = ApplicationInfo.getInstance().build.baselineVersion >= 232

// test will fail if 222
fun shouldReturnLangFileSuffix() {
fun testShouldReturnLangFileSuffix() {
if(!isLatest()) {
println("skip testShouldReturnLangFileSuffix")
return
}

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

Expand Down

0 comments on commit bca537e

Please sign in to comment.