Skip to content

Commit

Permalink
feat(rust): extract unit service API for Rust
Browse files Browse the repository at this point in the history
  • Loading branch information
phodal committed Jan 2, 2024
1 parent b30b1dd commit 0dbe0bb
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ class SingleProjectCodePicker(private val config: InsPickerOption) {
)
)


workerManager.init(codeDir, language)

val walkdirChannel = Channel<FileJob>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import cc.unitmesh.core.completion.TypedInsBuilder
import cc.unitmesh.core.completion.TypedIns
import cc.unitmesh.pick.builder.unittest.base.UnitTestService
import cc.unitmesh.pick.worker.job.JobContext
import chapi.domain.core.CodeContainer
import chapi.domain.core.CodeDataStruct
import chapi.domain.core.CodeFunction

Expand All @@ -17,6 +18,14 @@ class TestCodeTypedInsBuilder(val context: JobContext) : TypedInsBuilder {
return testIns
}

override fun build(container: CodeContainer): List<TypedIns> {
val testIns = UnitTestService.lookup(container, context).map {
it.build(container)
}.flatten()

return testIns
}

override fun build(function: CodeFunction): List<CodeCompletionIns> {
return listOf()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import cc.unitmesh.pick.builder.unittest.kotlin.KotlinTestCodeService
import cc.unitmesh.pick.builder.unittest.rust.RustTestCodeService
import cc.unitmesh.pick.builder.unittest.typescript.TypeScriptTestCodeService
import cc.unitmesh.pick.worker.job.JobContext
import chapi.domain.core.CodeContainer
import chapi.domain.core.CodeDataStruct

/**
Expand All @@ -22,15 +23,38 @@ import chapi.domain.core.CodeDataStruct
* for these methods based on the specific requirements of the testing framework or tool being used.
*/
interface UnitTestService {
/**
* For [SupportedLang.RUST] can analysis by container
*/
fun isApplicable(container: CodeContainer): Boolean = false

/**
* For [SupportedLang.RUST], the file contains test code.
*/
fun build(container: CodeContainer): List<TypedTestIns> = listOf()

/**
* Checks if the given data structure is a test file.
*/
fun isApplicable(dataStruct: CodeDataStruct): Boolean

/**
* For [SupportedLang.JAVA], [SupportedLang.KOTLIN], can analysis by dataStruct
*/
fun build(dataStruct: CodeDataStruct): List<TypedTestIns>

companion object {
fun lookup(codeDataStruct: CodeDataStruct, job: JobContext): List<UnitTestService> {
val testCodeServices = unitTestServices(job)
return testCodeServices.filter { it.isApplicable(codeDataStruct) }
}

fun lookup(codeContainer: CodeContainer, job: JobContext): List<UnitTestService> {
val testCodeServices = unitTestServices(job)
return testCodeServices.filter { it.isApplicable(codeContainer) }
}

private fun unitTestServices(job: JobContext): List<UnitTestService> {
val testCodeServices = SupportedLang.all().map {
when (it) {
SupportedLang.JAVA -> JavaTestCodeService(job)
Expand All @@ -39,9 +63,7 @@ interface UnitTestService {
SupportedLang.RUST -> RustTestCodeService(job)
}
}

return testCodeServices.filter { it.isApplicable(codeDataStruct) }
return testCodeServices
}

}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
package cc.unitmesh.pick.builder.unittest.rust

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.JobContext
import chapi.domain.core.CodeContainer
import chapi.domain.core.CodeDataStruct

class RustTestCodeService(val job: JobContext): UnitTestService {
override fun isApplicable(dataStruct: CodeDataStruct): Boolean {
return false
class RustTestCodeService(val job: JobContext) : UnitTestService {
override fun isApplicable(dataStruct: CodeDataStruct): Boolean = false

override fun isApplicable(container: CodeContainer): Boolean = job.project.language == SupportedLang.RUST

override fun build(container: CodeContainer): List<TypedTestIns> {
return emptyList()
}

override fun build(dataStruct: CodeDataStruct): List<TypedTestIns> {
TODO()
return emptyList()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,27 @@ class SingleProjectCodePickerTest {
})
}
}

@Test
fun should_handle_for_rust_test_gen() {
val picker = SingleProjectCodePicker(
InsPickerOption(
language = "rust",
url = "https://github.com/unit-mesh/edge-infer",
maxTokenLength = 8192,
codeStrategyTypes = listOf(CodeStrategyType.RELATED_CODE),
completionTypes = listOf(
CompletionBuilderType.TEST_CODE_GEN
),
)
)

val outputFile = File("test.jsonl")
runBlocking {
val output: MutableList<Instruction> = picker.execute()
outputFile.writeText(output.joinToString("\n") {
Json.encodeToString(it)
})
}
}
}

0 comments on commit 0dbe0bb

Please sign in to comment.