Skip to content

Commit

Permalink
feat(testing): add source code to be tested
Browse files Browse the repository at this point in the history
Add the source code to be tested in the test file. This allows for better organization and readability of the test code. The source code is inserted using the appropriate language syntax.
  • Loading branch information
phodal committed Jan 16, 2024
1 parent 5e4a1d3 commit fc1763f
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 13 deletions.
@@ -1,12 +1,13 @@
package cc.unitmesh.ide.javascript.util

import com.intellij.javascript.testing.JSTestRunnerManager
import com.intellij.openapi.application.runReadAction
import com.intellij.psi.PsiFile

object JsUtil {
fun guessTestFrameworkName(file: PsiFile): String? {
val findPackageDependentProducers =
JSTestRunnerManager.getInstance().findPackageDependentProducers(file)
runReadAction { JSTestRunnerManager.getInstance().findPackageDependentProducers(file) }

val testRunConfigurationProducer = findPackageDependentProducers.firstOrNull()
return testRunConfigurationProducer?.configurationType?.displayName
Expand Down
Expand Up @@ -13,6 +13,7 @@ import com.intellij.lang.javascript.psi.ecmal4.JSClass
import com.intellij.openapi.application.ReadAction
import com.intellij.openapi.application.runReadAction
import com.intellij.openapi.command.WriteCommandAction
import com.intellij.openapi.fileEditor.FileDocumentManager
import com.intellij.openapi.project.Project
import com.intellij.openapi.project.guessProjectDir
import com.intellij.openapi.vfs.LocalFileSystem
Expand Down Expand Up @@ -51,6 +52,13 @@ class JSWriteTestService : WriteTestService() {
PsiFileFactory.getInstance(project).createFileFromText(testFileName, language, testFileText)
}

// write test file
WriteCommandAction.runWriteCommandAction(project) {
val document = testFilePsi.viewProvider.document!!
document.insertString(document.textLength, testFileText)
FileDocumentManager.getInstance().saveDocument(document)
}

val currentClz = JavaScriptClassContextBuilder().getClassContext(elementToTest, false)

return TestFileContext(true, testFilePsi.virtualFile, emptyList(), elementName, language, currentClz)
Expand Down Expand Up @@ -83,7 +91,6 @@ class JSWriteTestService : WriteTestService() {
}

val resolveClass = jsFunction.returnTypeElement

return mapOf()
}

Expand Down Expand Up @@ -142,19 +149,26 @@ class JSWriteTestService : WriteTestService() {
return testFile
}

/**
* Todo: since in JavaScript has different test framework, we need to find the test directory by the framework.
*/
private fun suggestTestDirectory(element: PsiElement): PsiDirectory? {
val project: Project = element.project
val elementDirectory = runReadAction { element.containingFile }

val parentDir = elementDirectory?.virtualFile?.parent ?: return null
val testDir = runReadAction { parentDir.findChild("test") }
testDir?.let {
return runReadAction { PsiManager.getInstance(project).findDirectory(it) }
}

return WriteCommandAction.writeCommandAction(project, elementDirectory)
val outputFile = WriteCommandAction.writeCommandAction(project, elementDirectory)
.withName("Creating Directory For Tests")
.compute<VirtualFile?, IOException> {
return@compute parentDir.createChildDirectory(this, "test")
}?.let {
return runReadAction { PsiManager.getInstance(project).findDirectory(it) }
}
} ?: return null

return runReadAction { PsiManager.getInstance(project).findDirectory(outputFile) }
}

private fun generateUniqueTestFile(
Expand Down
Expand Up @@ -18,6 +18,12 @@ open class JavaScriptTestCodeModifier : CodeModifier {
}

override fun insertTestCode(sourceFile: VirtualFile, project: Project, code: String): Boolean {
val isExit = sourceFile as? JSFile
if (isExit == null) {
insertClass(sourceFile, project, code)
return true
}

insertMethod(sourceFile, project, code)
return true
}
Expand Down
Expand Up @@ -27,11 +27,12 @@ import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.runBlocking

data class TestGenPromptContext(
var language: String = "",
var lang: String = "",
var imports: String = "",
var frameworkedContext: String = "",
var currentClass: String = "",
var relatedClasses: String = "",
var sourceCode: String = "",
var testClassName: String = "",
var isNewFile: Boolean = true,
)
Expand Down Expand Up @@ -97,6 +98,8 @@ class TestCodeGenTask(val request: TestCodeGenRequest) :
testPromptContext.imports = testContext.imports.joinToString("\n") {
"$comment $it"
}
// prompter += "\nCode:\n$importString\n```${lang.lowercase()}\n${request.selectText}\n```\n"
testPromptContext.sourceCode = request.selectText
testPromptContext.isNewFile = testContext.isNewFile

templateRender.context = testPromptContext
Expand All @@ -117,8 +120,8 @@ class TestCodeGenTask(val request: TestCodeGenRequest) :

runBlocking {
writeTestToFile(request.project, flow, testContext)
navigateTestFile(testContext.file, request.project)
writeTestService?.runTest(request.project, testContext.file)
navigateTestFile(testContext.outputFile, request.project)
writeTestService?.runTest(request.project, testContext.outputFile)

AutoDevStatusService.notifyApplication(AutoDevStatus.Ready)
indicator.fraction = 1.0
Expand All @@ -142,7 +145,7 @@ class TestCodeGenTask(val request: TestCodeGenRequest) :
?: throw IllegalStateException("Unsupported language: ${context.language}")

parseCodeFromString(suggestion.toString()).forEach {
modifier.insertTestCode(context.file, project, it)
modifier.insertTestCode(context.outputFile, project, it)
}
}

Expand Down
Expand Up @@ -6,7 +6,7 @@ import com.intellij.openapi.vfs.VirtualFile

data class TestFileContext(
val isNewFile: Boolean,
val file: VirtualFile,
val outputFile: VirtualFile,
val relatedClasses: List<ClassContext> = emptyList(),
val testClassName: String?,
val language: Language,
Expand Down
8 changes: 6 additions & 2 deletions src/main/resources/genius/code/test-gen.vm
Expand Up @@ -9,13 +9,17 @@ Here is current class information:
${context.currentClass}
#end

Here is the source code to be tested:

```$context.lang
${context.imports}
${context.sourceCode}
```

## if newFile
#if( $context.isNewFile )
Start method test code here:
#else
Start ${context.testClassName} with `import` syntax here:
Start ${context.testClassName} test code here:
#end


0 comments on commit fc1763f

Please sign in to comment.