Skip to content

Commit

Permalink
feat(unitest): add test support
Browse files Browse the repository at this point in the history
  • Loading branch information
phodal committed Dec 24, 2023
1 parent 8591675 commit 1ebea0e
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class ClassTestCodeBuilder(private val context: JobContext) : TestCodeBuilder {
dataStruct: CodeDataStruct,
underTestFile: CodeDataStruct,
relevantClasses: List<CodeDataStruct>,
): List<ClassTestIns> {
): List<BasicTestIns> {
val generatedCode = dataStruct.toSourceCode()
if (generatedCode.lines().size > context.insQualityThreshold.maxLineInCode) {
return emptyList()
Expand All @@ -29,7 +29,7 @@ class ClassTestCodeBuilder(private val context: JobContext) : TestCodeBuilder {
val namingStyle = dataStruct.checkNamingStyle()

return listOf(
ClassTestIns(
BasicTestIns(
lang = context.project.language,
underTestCode = underTestFile.Content,
generatedCode = generatedCode,
Expand All @@ -44,7 +44,7 @@ class ClassTestCodeBuilder(private val context: JobContext) : TestCodeBuilder {
}
}

class ClassTestIns(
class BasicTestIns(
val lang: SupportedLang,
val underTestCode: String,
val generatedCode: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,32 +12,47 @@ class MethodTestCodeBuilder(private val context: JobContext) : TestCodeBuilder {
dataStruct: CodeDataStruct,
underTestFile: CodeDataStruct,
relevantClasses: List<CodeDataStruct>,
): List<ClassTestIns> {
): List<BasicTestIns> {
val generatedCode = dataStruct.toSourceCode()
if (generatedCode.lines().size > context.insQualityThreshold.maxLineInCode) {
return emptyList()
}

val namingStyle = dataStruct.checkNamingStyle()
// test canonicalName map BasicTestIns
val results: HashMap<String, List<BasicTestIns>> = hashMapOf()

dataStruct.Functions.map {
it.FunctionCalls.map {
// todo
val underTestFunctionMap = underTestFile.Functions.map {
val canonicalName = underTestFile.Package + "." + underTestFile.NodeName + ":" + it.Name
canonicalName to it.Content
}.toMap()

// analysis test code, and find original function content, put to results
dataStruct.Functions.map { function ->
function.FunctionCalls.map {
if (it.NodeName == underTestFile.NodeName) {
val canonicalName = it.Package + "." + it.NodeName + ":" + it.FunctionName
val originalContent = underTestFunctionMap[canonicalName]

if (originalContent != null) {
val testIns = BasicTestIns(
lang = context.project.language,
underTestCode = originalContent,
generatedCode = function.Content,
coreFrameworks = context.project.coreFrameworks,
testFrameworks = context.project.testFrameworks,
testType = TestCodeBuilderType.METHOD_UNIT,
specs = listOf(
"Test class should be named `${namingStyle}`."
)
)

results[canonicalName] = results[canonicalName]?.plus(testIns) ?: listOf(testIns)
}
}
}
}

return listOf(
ClassTestIns(
lang = context.project.language,
underTestCode = underTestFile.Content,
generatedCode = generatedCode,
coreFrameworks = context.project.coreFrameworks,
testFrameworks = context.project.testFrameworks,
testType = TestCodeBuilderType.CLASS_UNIT,
specs = listOf(
"Test class should be named `${namingStyle}`."
)
)
)
return results.values.flatten()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,10 @@ class JavaTestCodeService(val context: JobContext) : UnitTestService {
val classTestIns = ClassTestCodeBuilder(context)
.build(dataStruct, underTestFile, relevantClasses)

MethodTestCodeBuilder(context)
val methodTests = MethodTestCodeBuilder(context)
.build(dataStruct, underTestFile, relevantClasses)

// todo: add method level support
return classTestIns
return classTestIns + methodTests
}
}

0 comments on commit 1ebea0e

Please sign in to comment.