Skip to content

Commit

Permalink
fix(kotlin): fix kotlin test method issue #1
Browse files Browse the repository at this point in the history
  • Loading branch information
phodal committed Dec 28, 2023
1 parent 57a8a19 commit 0ecb7b8
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class ClassTestCodeBuilder(private val context: JobContext) : TestCodeBuilder {
BasicTestIns(
identifier = NodeIdentifier(
type = NodeType.CLASS,
name = dataStruct.NodeName,
name = underTestFile.NodeName,
),
language = context.project.language,
underTestCode = underTestFile.Content,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package cc.unitmesh.pick.builder.unittest.java

import cc.unitmesh.core.SupportedLang
import cc.unitmesh.core.unittest.TypedTestIns
import cc.unitmesh.pick.builder.unittest.base.UnitTestService
import cc.unitmesh.pick.worker.job.InstructionFileJob
Expand All @@ -10,7 +11,7 @@ import chapi.domain.core.CodeImport

open class JavaTestCodeService(open val context: JobContext) : UnitTestService {
override fun isApplicable(dataStruct: CodeDataStruct): Boolean {
return dataStruct.NodeName.endsWith("Test") || dataStruct.NodeName.endsWith("Tests")
return context.project.language == SupportedLang.JAVA && dataStruct.NodeName.endsWith("Test") || dataStruct.NodeName.endsWith("Tests")
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import cc.unitmesh.pick.ext.toSourceCode
import cc.unitmesh.pick.ext.toUml
import cc.unitmesh.pick.worker.job.JobContext
import chapi.domain.core.CodeDataStruct
import chapi.domain.core.DataStructType

/**
* 为给定的 CodeDataStruct 的每个 CodeFunction 生成测试指令。
Expand All @@ -36,7 +37,9 @@ class KotlinMethodTestCodeBuilder(private val context: JobContext) : TestCodeBui
// 分析测试代码,找到原始函数的代码内容,放到结果中
dataStruct.Functions.mapIndexed { _, function ->
underTestFile.Functions.map {
if (!function.Content.contains("." + it.Name + "(")) {
val isNotClassCall = underTestFile.Type == DataStructType.CLASS && !function.Content.contains("." + it.Name + "(")
val isNotObjectCall = underTestFile.Type == DataStructType.OBJECT && !function.Content.contains(it.Name + "(")
if (isNotClassCall || isNotObjectCall) {
return@map
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package cc.unitmesh.pick.builder.unittest.kotlin

import cc.unitmesh.core.SupportedLang
import cc.unitmesh.core.unittest.TypedTestIns
import cc.unitmesh.pick.builder.unittest.java.ClassTestCodeBuilder
import cc.unitmesh.pick.builder.unittest.java.JavaMethodTestCodeBuilder
import cc.unitmesh.pick.builder.unittest.java.JavaTestCodeService
import cc.unitmesh.pick.worker.job.JobContext
import chapi.domain.core.CodeDataStruct
Expand All @@ -11,6 +11,10 @@ import chapi.domain.core.CodeDataStruct
* 为给定的 CodeDataStruct 的每个 CodeFunction 生成测试指令。
*/
class KotlinTestCodeService(override val context: JobContext) : JavaTestCodeService(context) {
override fun isApplicable(dataStruct: CodeDataStruct): Boolean {
return context.project.language == SupportedLang.KOTLIN && dataStruct.NodeName.endsWith("Test") || dataStruct.NodeName.endsWith("Tests")
}

override fun build(dataStruct: CodeDataStruct): List<TypedTestIns> {
val underTestFile = this.findUnderTestFile(dataStruct).firstOrNull() ?: return emptyList()
val relevantClasses = this.lookupRelevantClass(dataStruct)
Expand Down

0 comments on commit 0ecb7b8

Please sign in to comment.