Skip to content

Commit

Permalink
feat(test): add basic rules for generate data
Browse files Browse the repository at this point in the history
  • Loading branch information
phodal committed Dec 23, 2023
1 parent 076fa59 commit 2e9549a
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 24 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package cc.unitmesh.core.unittest

import chapi.domain.core.CodeDataStruct

interface TestCodeBuilder {
fun build(): List<TypedTestIns>
fun build(dataStruct: CodeDataStruct, underTestFile: CodeDataStruct, relevantClasses: List<CodeDataStruct>): List<TypedTestIns>
}


Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ import cc.unitmesh.core.unittest.TestCodeBuilderType
import cc.unitmesh.core.unittest.TypedTestIns
import cc.unitmesh.core.unittest.TestCodeBuilder
import cc.unitmesh.pick.worker.job.JobContext
import chapi.domain.core.CodeDataStruct

class ApiTestCodeBuilder(private val context: JobContext) : TestCodeBuilder {
override fun build(): List<ApiTestIns> {
override fun build(dataStruct: CodeDataStruct, underTestFile: CodeDataStruct, relevantClasses: List<CodeDataStruct>): List<ApiTestIns> {
TODO("Not yet implemented")
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,54 @@ import cc.unitmesh.core.unittest.TestCodeBuilderType
import cc.unitmesh.core.unittest.TypedTestIns
import cc.unitmesh.core.unittest.TestCodeBuilder
import cc.unitmesh.pick.worker.job.JobContext
import chapi.domain.core.CodeDataStruct

class ClassTestCodeBuilder(private val context: JobContext) : TestCodeBuilder {
override fun build(): List<ClassTestIns> {
val dataStructures = context.job.container?.DataStructures
val isTestFile = dataStructures?.any {
it.NodeName.endsWith("Test") || it.NodeName.endsWith("Tests")
}

if (!isTestFile!!) {
return emptyList()
}



return emptyList()
/**
* @param dataStruct is the test file
* @param underTestFile is the file under test
* @param relevantClasses are the classes relevant to the test file
*/
override fun build(
dataStruct: CodeDataStruct,
underTestFile: CodeDataStruct,
relevantClasses: List<CodeDataStruct>,
): List<ClassTestIns> {
return listOf(
ClassTestIns(
coreFrameworks = listOf(),
testType = TestCodeBuilderType.CLASS_UNIT,
specs = listOf(
"You MUST use should_xx_xx style for test method name.",
"You MUST use given-when-then style.",
"Test file should be complete and compilable, without need for further actions.",
"Instead of using `@BeforeEach` methods for setup, include all necessary code initialization within each individual test method, do not write parameterized tests."
),
additionalRules = listOf()
)
)
}

}

class ClassTestIns(override val testType: TestCodeBuilderType) : TypedTestIns() {
class ClassTestIns(
val coreFrameworks: List<String> = listOf(),
val testFrameworks: List<String> = listOf(),
/**
* the Specification of the test
*/
val specs: List<String> = listOf(),
val relatedCode: List<String> = listOf(),
additionalRules: List<String> = listOf(),
override val testType: TestCodeBuilderType,
) : TypedTestIns() {
override fun unique(): Instruction {
TODO("Not yet implemented")
val input = StringBuilder()

return Instruction(
instruction = "Write unit test for following code.",
input = "",
output = "",
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ import cc.unitmesh.core.unittest.TestCodeBuilderType
import cc.unitmesh.core.unittest.TypedTestIns
import cc.unitmesh.core.unittest.TestCodeBuilder
import cc.unitmesh.pick.worker.job.JobContext
import chapi.domain.core.CodeDataStruct

class MethodTestCodeBuilder(private val context: JobContext) : TestCodeBuilder {
override fun build(): List<MethodTestIns> {
override fun build(dataStruct: CodeDataStruct, underTestFile: CodeDataStruct, relevantClasses: List<CodeDataStruct>): List<MethodTestIns> {
TODO("Not yet implemented")
}

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

import cc.unitmesh.core.unittest.TypedTestIns
import cc.unitmesh.pick.builder.unittest.ClassTestCodeBuilder
import cc.unitmesh.pick.worker.job.InstructionFileJob
import cc.unitmesh.pick.worker.job.JobContext
import chapi.domain.core.CodeDataStruct
Expand Down Expand Up @@ -62,9 +63,12 @@ class JavaTestCodeService(val context: JobContext) : UnitTestService {
}
}.flatten()

override fun build(dataStructs: List<CodeDataStruct>): List<TypedTestIns> {
// TODO("Not yet implemented")
return listOf()
override fun build(dataStruct: CodeDataStruct): List<TypedTestIns> {
val underTestFile = this.findUnderTestFile(dataStruct).firstOrNull() ?: return emptyList()
val relevantClasses = this.lookupRelevantClass(dataStruct)
val classTestIns = ClassTestCodeBuilder(context).build(dataStruct, underTestFile, relevantClasses)
// todo: add method level support
return classTestIns
}
}

Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package cc.unitmesh.pick.builder.unittest.lang

import cc.unitmesh.core.completion.TypedIns
import cc.unitmesh.core.SupportedLang
import cc.unitmesh.core.unittest.TypedTestIns
import cc.unitmesh.pick.worker.job.JobContext
Expand All @@ -24,7 +23,7 @@ interface UnitTestService {
*/
fun isApplicable(dataStruct: CodeDataStruct): Boolean

fun build(dataStructs: List<CodeDataStruct>): List<TypedTestIns>
fun build(dataStruct: CodeDataStruct): List<TypedTestIns>

companion object {
fun lookup(codeDataStruct: CodeDataStruct, job: JobContext): List<JavaTestCodeService> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class RelatedCodeStrategyBuilder(private val context: JobContext) : CodeStrategy

val codeCompletionIns = dataStructs.map { ds ->
UnitTestService.lookup(ds, context).map {
it.build(dataStructs)
it.build(ds)
}
ds.Functions.map { function ->
builders.asSequence().map {
Expand Down

0 comments on commit 2e9549a

Please sign in to comment.