-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Provide tests for a selection of method
The core algorithm is still not tested because I do not see how I could get the required objects in the necessary state. The unfortunate thing about the implemented selection algorithms is that they rely on an open editor for a normal execution, which seems to be hard to set up for a unit test. Any feedback and help on this is highly appreciated!
- Loading branch information
1 parent
833956d
commit 52fef0d
Showing
3 changed files
with
152 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 0 additions & 63 deletions
63
src/test/kotlin/org/jetbrains/research/testspark/actions/llm/LLMSampleSelectorBuilderTest.kt
This file was deleted.
Oops, something went wrong.
146 changes: 146 additions & 0 deletions
146
src/test/kotlin/org/jetbrains/research/testspark/actions/llm/LLMSampleSelectorTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
package org.jetbrains.research.testspark.actions.llm | ||
|
||
import com.google.gson.Gson | ||
import com.google.gson.reflect.TypeToken | ||
import com.intellij.openapi.application.runReadAction | ||
import com.intellij.psi.PsiClass | ||
import com.intellij.psi.PsiJavaFile | ||
import com.intellij.psi.PsiMethod | ||
import com.intellij.testFramework.fixtures.CodeInsightTestFixture | ||
import com.intellij.testFramework.fixtures.IdeaProjectTestFixture | ||
import com.intellij.testFramework.fixtures.IdeaTestFixtureFactory | ||
import com.intellij.testFramework.fixtures.JavaTestFixtureFactory | ||
import com.intellij.testFramework.fixtures.TestFixtureBuilder | ||
import com.intellij.util.containers.stream | ||
import net.jqwik.api.ForAll | ||
import net.jqwik.api.Property | ||
import net.jqwik.api.lifecycle.BeforeTry | ||
import org.junit.jupiter.api.Assertions.assertEquals | ||
import org.junit.jupiter.api.BeforeEach | ||
import org.junit.jupiter.api.Test | ||
import kotlin.test.assertContentEquals | ||
|
||
class LLMSampleSelectorTest { | ||
|
||
private lateinit var selector: LLMSampleSelector | ||
private lateinit var fixture: CodeInsightTestFixture | ||
private lateinit var openFile: PsiJavaFile | ||
|
||
@BeforeEach | ||
@BeforeTry | ||
fun setUpSelector() { | ||
selector = LLMSampleSelector() | ||
} | ||
|
||
@BeforeEach | ||
fun setUpProject() { | ||
val projectBuilder: TestFixtureBuilder<IdeaProjectTestFixture> = | ||
IdeaTestFixtureFactory.getFixtureFactory().createFixtureBuilder("project") | ||
|
||
fixture = JavaTestFixtureFactory.getFixtureFactory() | ||
.createCodeInsightFixture(projectBuilder.fixture) | ||
fixture.setUp() | ||
|
||
addFilesFromJSONToFixture() | ||
} | ||
|
||
private fun classFromFile(psiJavaFile: PsiJavaFile): PsiClass { | ||
return runReadAction { | ||
psiJavaFile.classes[ | ||
psiJavaFile.classes.stream().map { it.name }.toArray() | ||
.indexOf(psiJavaFile.name.removeSuffix(".java")), | ||
] | ||
} | ||
} | ||
|
||
private fun methodsFromClass(psiClass: PsiClass): Array<out PsiMethod> { | ||
return psiClass.methods | ||
} | ||
|
||
private fun addFilesFromJSONToFixture() { | ||
val jsonFileContent = getResourceAsText("/llm/sampleSelectorTestFiles.json") | ||
val type = object : TypeToken<Map<String, String>>() {}.type | ||
val fileMap = Gson().fromJson(jsonFileContent, type) as Map<String, String> | ||
fileMap.forEach { (fileName, fileContent) -> | ||
val psiFile = fixture.addFileToProject(fileName, fileContent) | ||
if (fileName == "test/dummy/CarTest.java") { | ||
openFile = psiFile as PsiJavaFile | ||
} | ||
} | ||
} | ||
|
||
private fun getResourceAsText(path: String): String? = | ||
object {}.javaClass.getResource(path)?.readText() | ||
|
||
@Test | ||
fun `test the initial test names`() { | ||
val expected = mutableListOf("<html>provide manually</html>") | ||
val actual = selector.getTestNames() | ||
assertContentEquals(expected, actual) | ||
} | ||
|
||
@Test | ||
fun `test the initial test code`() { | ||
val initialCode = """ | ||
public class TestSample { | ||
// provide test method code here | ||
} | ||
""".trimIndent() | ||
val expected = mutableListOf(initialCode) | ||
val actual = selector.getInitialTestCodes() | ||
assertContentEquals(expected, actual) | ||
} | ||
|
||
@Property | ||
fun `test the append of test sample code`(@ForAll index: Int, @ForAll code: String) { | ||
selector.appendTestSampleCode(index, code) | ||
val expected = "Test sample number ${index + 1}\n```\n$code\n```\n" | ||
val actual = selector.getTestSamplesCode() | ||
assertEquals(expected, actual) | ||
} | ||
|
||
@Test | ||
fun `test the class retrieval from a Java file`() { | ||
val expectedName = "CarTest" | ||
val actual = runReadAction { selector.retrievePsiClass(openFile as PsiJavaFile) } | ||
assertEquals(expectedName, actual.name) | ||
} | ||
|
||
@Test | ||
fun `test the import-statement retrieval`() { | ||
val expected = """ | ||
import org.junit.jupiter.api.Test; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import dummy.*; | ||
""".trimIndent() | ||
val actual = runReadAction { | ||
val file = openFile as PsiJavaFile | ||
selector.retrieveImportStatements(file, classFromFile(file)) | ||
} | ||
assertEquals(expected, actual) | ||
} | ||
|
||
@Test | ||
fun `test the create test-sample class`() { | ||
val expected = """ | ||
import org.junit.jupiter.api.Test; | ||
public class TestSample { | ||
// provide test method code here | ||
} | ||
""".trimIndent() | ||
val actual = selector.createTestSampleClass( | ||
"import org.junit.jupiter.api.Test;", | ||
"// provide test method code here", | ||
) | ||
assertEquals(expected, actual) | ||
} | ||
|
||
@Test | ||
fun `test the expected method name`() { | ||
val expected = "<html>dummy.CarTest#testCar</html>" | ||
val cls = classFromFile(openFile) | ||
val actual = selector.createMethodName(cls, methodsFromClass(cls)[0]) | ||
assertEquals(expected, actual) | ||
} | ||
} |