-
Notifications
You must be signed in to change notification settings - Fork 23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Restrict TestSpark action to suitable code types and fix generation for a line #344
Restrict TestSpark action to suitable code types and fix generation for a line #344
Conversation
It does not solve #352 If I ask to generate test for package line in a kotlin project it throws the following exception:
|
java/src/main/kotlin/org/jetbrains/research/testspark/java/JavaPsiHelper.kt
Outdated
Show resolved
Hide resolved
As @pderakhshanfar metioned above, if you attempt generation on an
It is because we assert that a surrounding method exists. In general, we should quit using We should hide the action if there is no valid code construct under test. For a given line, we need to ensure it is within a method, class, or top-level function. The check should be performed in this order: 1) search for a surrounding method; if not found, 2) check for a surrounding class; if not found, 3) check for a top-level function. If none exist, the line is top-level, and we cannot generate tests, so the action should not appear in the right-click menu. |
The same error as here occurs if I start generation for a line public class Calc {
int a = 10; // (**)
public int sum(int a, int b) {
return a + b;
}
public int multiply(int a, int b) {
return a * b;
}
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are important comments above that identify unfixed bugs. Those should be addressed in this PR to close the issue #352.
java/src/main/kotlin/org/jetbrains/research/testspark/java/JavaPsiHelper.kt
Outdated
Show resolved
Hide resolved
langwrappers/src/main/kotlin/org/jetbrains/research/testspark/langwrappers/PsiComponents.kt
Outdated
Show resolved
Hide resolved
langwrappers/src/main/kotlin/org/jetbrains/research/testspark/langwrappers/PsiComponents.kt
Outdated
Show resolved
Hide resolved
src/main/kotlin/org/jetbrains/research/testspark/actions/TestSparkAction.kt
Outdated
Show resolved
Hide resolved
b40a077
to
3079173
Compare
Fixed. |
Fixed. |
Fixed. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Introduced modifications that solve the described problems.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I must approve the PR to allow its further merge. This approval does not count.
I've found several problems connected with line generation:
class Calc {
fun sum(a: Int, b: Int): Int {
return a + b
}
} LLM is struggling with generating This problem became even more crucial if attempted to generate a test for a line inside a companion object, or it's method. LLM just don't know anything about how to call such function. Solution: includes information about the class surrounding the line if such is present.
/// 123 -- not allowed for line generation
// 123 -- allowed for line generation
fun multwice(a: Int) = a * 2 Solution idea: For each line generation, check if it includes something besides comments, open/closed brackets and allow generation only in such case |
@Hello-zoka, I agree with the 2nd and 3rd points; I'll address them in the PR or explain why they should be addressed in a different PR. At first glance, the first point does not appear to be an easy task, and it does not represent a bug; it is a lack of context. It is a stand-alone issue that should be addressed in a different PR (possibly after the upcoming release). @Hello-zoka, could you create an issue for it? My main focus, for now, is to close as many issues as possible that yield runtime exceptions and fail the plugin. The aspects regarding line-based test generation mentioned appear minor to me as very few people use line-based test generation compared to method/class-based generation (a good thing to calculate it as a metric! @arksap2002). CC: @pderakhshanfar |
@Hello-zoka |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
...c/main/kotlin/org/jetbrains/research/testspark/core/generation/llm/prompt/PromptGenerator.kt
Outdated
Show resolved
Hide resolved
src/main/kotlin/org/jetbrains/research/testspark/actions/TestSparkAction.kt
Show resolved
Hide resolved
src/main/kotlin/org/jetbrains/research/testspark/tools/llm/generation/PromptManager.kt
Outdated
Show resolved
Hide resolved
src/main/kotlin/org/jetbrains/research/testspark/tools/llm/generation/PromptManager.kt
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit (and we dont need to resolve it in this issue) TestSpark option is still available when we try to generate test for a test file:
open a test file right click and ask for test generation using TestSpark
The bug is reflected in the issue #375.
…umbers Before, the `KotlinPsiHelper` returned a 0-based line number which caused an issue with line-based test generation. The generated prompt contained a line above the selected one.
When there is no surrounding method about the selected line, we use the CUT as a context for this line. The CUT must always be present. Otherwise, the generation action should have been disabled for this line.
The line-based test generation that has a method as a context of the line now also accepts constructors of the containing class.
eb19a43
to
7d99e58
Compare
The major version increased due to the change of the public API of `PromptGenerator.generatePromptForLine` method.
…or a line (JetBrains-Research#344) * fix update function * create availableForGeneration * ktlint * feat: add javadoc for `JavaPsiHelper.availableForGeneration` * feat: check for nullness of a PSI file in `TestSparkAction.update` * feat: update javadocs in `PsiComponents.kt` * feat: check for a class or method/func in `KotlinPsiHelper.availableForGeneration` * feat: add TODO to `ToolUtils` about a potential bug The bug is reflected in the issue JetBrains-Research#375. * feat: make `PsiHelper.getSurroundingLineNumber` return 1-based line numbers Before, the `KotlinPsiHelper` returned a 0-based line number which caused an issue with line-based test generation. The generated prompt contained a line above the selected one. * feat: implement line-based test generation with CUT as a context When there is no surrounding method about the selected line, we use the CUT as a context for this line. The CUT must always be present. Otherwise, the generation action should have been disabled for this line. * refactor: apply ktlint * feat: add `See` in TODO * feat: add TODO and surround $NAME in backticks in `linePrompt` template * feat: collect class constructor signatures in `PsiClassWrapper` * feat: remove backticks from `linePrompt` * feat: fill line-based test generation with additional context The line-based test generation that has a method as a context of the line now also accepts constructors of the containing class. * refactor: use `firstOrNull` for `cut` extraction * refactor: apply ktlint * fix: add required parameter to `ClassRepresentation` in tests * publish: core module version `4.0.0` The major version increased due to the change of the public API of `PromptGenerator.generatePromptForLine` method. --------- Co-authored-by: Vladislav Artiukhov <[email protected]>
…or a line (JetBrains-Research#344) * fix update function * create availableForGeneration * ktlint * feat: add javadoc for `JavaPsiHelper.availableForGeneration` * feat: check for nullness of a PSI file in `TestSparkAction.update` * feat: update javadocs in `PsiComponents.kt` * feat: check for a class or method/func in `KotlinPsiHelper.availableForGeneration` * feat: add TODO to `ToolUtils` about a potential bug The bug is reflected in the issue JetBrains-Research#375. * feat: make `PsiHelper.getSurroundingLineNumber` return 1-based line numbers Before, the `KotlinPsiHelper` returned a 0-based line number which caused an issue with line-based test generation. The generated prompt contained a line above the selected one. * feat: implement line-based test generation with CUT as a context When there is no surrounding method about the selected line, we use the CUT as a context for this line. The CUT must always be present. Otherwise, the generation action should have been disabled for this line. * refactor: apply ktlint * feat: add `See` in TODO * feat: add TODO and surround $NAME in backticks in `linePrompt` template * feat: collect class constructor signatures in `PsiClassWrapper` * feat: remove backticks from `linePrompt` * feat: fill line-based test generation with additional context The line-based test generation that has a method as a context of the line now also accepts constructors of the containing class. * refactor: use `firstOrNull` for `cut` extraction * refactor: apply ktlint * fix: add required parameter to `ClassRepresentation` in tests * publish: core module version `4.0.0` The major version increased due to the change of the public API of `PromptGenerator.generatePromptForLine` method. --------- Co-authored-by: Vladislav Artiukhov <[email protected]>
…or a line (JetBrains-Research#344) * fix update function * create availableForGeneration * ktlint * feat: add javadoc for `JavaPsiHelper.availableForGeneration` * feat: check for nullness of a PSI file in `TestSparkAction.update` * feat: update javadocs in `PsiComponents.kt` * feat: check for a class or method/func in `KotlinPsiHelper.availableForGeneration` * feat: add TODO to `ToolUtils` about a potential bug The bug is reflected in the issue JetBrains-Research#375. * feat: make `PsiHelper.getSurroundingLineNumber` return 1-based line numbers Before, the `KotlinPsiHelper` returned a 0-based line number which caused an issue with line-based test generation. The generated prompt contained a line above the selected one. * feat: implement line-based test generation with CUT as a context When there is no surrounding method about the selected line, we use the CUT as a context for this line. The CUT must always be present. Otherwise, the generation action should have been disabled for this line. * refactor: apply ktlint * feat: add `See` in TODO * feat: add TODO and surround $NAME in backticks in `linePrompt` template * feat: collect class constructor signatures in `PsiClassWrapper` * feat: remove backticks from `linePrompt` * feat: fill line-based test generation with additional context The line-based test generation that has a method as a context of the line now also accepts constructors of the containing class. * refactor: use `firstOrNull` for `cut` extraction * refactor: apply ktlint * fix: add required parameter to `ClassRepresentation` in tests * publish: core module version `4.0.0` The major version increased due to the change of the public API of `PromptGenerator.generatePromptForLine` method. --------- Co-authored-by: Vladislav Artiukhov <[email protected]>
* minimal attempt to run kex from testspark with harcoded settings Needs debugging and testing * load kex-runner jar from github (build.gradle.kts toplevel) * setup code for kex properties * KexErrorManager based on LLMErrorManager * KexProcessManager based on EvoSuiteProcessManager * Basic UI element (button for running kex) * kex works only for the class codeType (todo funciton and line if possible) * read resource files kex.policy and modules.info * generate Report and series of simplifications for MVP use a provided kex path for now instead of downloading jar and adding dependency use ProcessBuilder (jdk) instead of OSProcessHandler (IJ sdk) use kex.py instead of building java command directly generate Report objects by reading generated java classes * download kex as github release if it doesn't exist * delete commented code Deleted stuff: running Kex through through OSProcessHandler (IJ sdk) running Kex with kex.py downloading kex from github in build.gradle.kts * use javaparser to merge @before and @after generated methods * compiling tests successfully by adding helper method to TestGenData.otherInfo * fold anything but @test annotated code, but unfortunately only on UI updates * remove python dependency by running kex jar directly * use project's java and add version check * empty implementation of settings classes for kex * allow setting kex arguments from kex settings page * kex test generation for 'method' code type arguably overkill solution of modify PsiMethodWrapper class added parameter names, types and return tyepes explicitly updated implementation for java and kotlin * fold all but @test methods regardless of declaration order * remove settings for unsupported kex features * folding works even if junit isn't in classpath * use the the subprocess manager from IJ framework * empty kex path download to .cache and LOCALAPPDATA in windows * fix lint. remove wildcard imports * from IJ api, provide correct build directory to kex For multimodule projects using TestSpark the correct module's build directory path is passed based on the location of code for which tests are generated * set maxTests displayed and minimizeTestSuite options * fix code fold to only happen once at the start not every ui update * error handling for errors in kex process manager also fixed a bug with the way options were passed to kex subprocess made them a list of strings instead of a single big space separated string * fix lint * provide signatures with FQNs to kex for java This also includes a simple string based mapping to jvm types through type erasure * refactor. extract functons, add comments, remove redundancies * fix lint * every option in kex cmd is preceeded by --option * use kotlin.time.Duration instead of Int * refactor error handling code, check for non-zero exit code * make generated code manipulation more robust It no longer depends on the order of the methods generated * only check if the correct verison of kex exists * make download url a property and version a user setting * support jdk version 8. no --add-opens * bump up kex version to 0.0.10 * allow kex test generation for classes not in a package * null safety while folding helper code * Merge branch 'development' into edwin1729/improvement/kex-integration * disallow kex and line code type being chosen simultaneously * update description tab and README.md with kex * fix lint * fix run coverage by making method name same as class name This is expected by TestSpark. Relevant file TestProcess.kt:createXmlFromJacoco:109 * undo mistaken removal of addLanguageTextFieldListener before code folding * Add empty LLM response handling (#342) Show a warning if LLM returns a response that cannot be parsed into a test case (e.g., an explanation of this test case rather than a modification). * Restrict TestSpark action to suitable code types and fix generation for a line (#344) * fix update function * create availableForGeneration * ktlint * feat: add javadoc for `JavaPsiHelper.availableForGeneration` * feat: check for nullness of a PSI file in `TestSparkAction.update` * feat: update javadocs in `PsiComponents.kt` * feat: check for a class or method/func in `KotlinPsiHelper.availableForGeneration` * feat: add TODO to `ToolUtils` about a potential bug The bug is reflected in the issue #375. * feat: make `PsiHelper.getSurroundingLineNumber` return 1-based line numbers Before, the `KotlinPsiHelper` returned a 0-based line number which caused an issue with line-based test generation. The generated prompt contained a line above the selected one. * feat: implement line-based test generation with CUT as a context When there is no surrounding method about the selected line, we use the CUT as a context for this line. The CUT must always be present. Otherwise, the generation action should have been disabled for this line. * refactor: apply ktlint * feat: add `See` in TODO * feat: add TODO and surround $NAME in backticks in `linePrompt` template * feat: collect class constructor signatures in `PsiClassWrapper` * feat: remove backticks from `linePrompt` * feat: fill line-based test generation with additional context The line-based test generation that has a method as a context of the line now also accepts constructors of the containing class. * refactor: use `firstOrNull` for `cut` extraction * refactor: apply ktlint * fix: add required parameter to `ClassRepresentation` in tests * publish: core module version `4.0.0` The major version increased due to the change of the public API of `PromptGenerator.generatePromptForLine` method. --------- Co-authored-by: Vladislav Artiukhov <[email protected]> * Data class for execution results * Minor refactoring * apply ktlint * fix getJavaVersion * add KexSettingsService to plugin.xml * fix DefaultKexSettingsState * fix KexSettingsState * fix Bundles * Force junit4 for EvoSuite tests * JUnit version forcing * fix getExceptionData * fix tools * fix managers * fix TestCasePanelBuilder.kt * fix TestProcessor.kt * remove unused import * fix ktlint * fix ktlint * remove TestSparkStarter.kt * Add support for language applicability checks in Tool * Add support for language applicability checks in Llm * Add support for language applicability checks in Kex * Add support for language applicability checks in EvoSuite * Filter test generation buttons by language compatibility * Refactor imports for SupportedLanguage in EvoSuite and Llm * Set progress indicator text during Kex test generation --------- Co-authored-by: Edwin Fernando <[email protected]> Co-authored-by: Edwin Fernando <[email protected]> Co-authored-by: Edwin Fernando <[email protected]> Co-authored-by: Vladislav Artiukhov <[email protected]> Co-authored-by: Iurii Zaitsev <[email protected]> Co-authored-by: Hello-zoka <[email protected]>
* minimal attempt to run kex from testspark with harcoded settings Needs debugging and testing * load kex-runner jar from github (build.gradle.kts toplevel) * setup code for kex properties * KexErrorManager based on LLMErrorManager * KexProcessManager based on EvoSuiteProcessManager * Basic UI element (button for running kex) * kex works only for the class codeType (todo funciton and line if possible) * read resource files kex.policy and modules.info * generate Report and series of simplifications for MVP use a provided kex path for now instead of downloading jar and adding dependency use ProcessBuilder (jdk) instead of OSProcessHandler (IJ sdk) use kex.py instead of building java command directly generate Report objects by reading generated java classes * download kex as github release if it doesn't exist * delete commented code Deleted stuff: running Kex through through OSProcessHandler (IJ sdk) running Kex with kex.py downloading kex from github in build.gradle.kts * use javaparser to merge @before and @after generated methods * compiling tests successfully by adding helper method to TestGenData.otherInfo * fold anything but @test annotated code, but unfortunately only on UI updates * remove python dependency by running kex jar directly * use project's java and add version check * empty implementation of settings classes for kex * allow setting kex arguments from kex settings page * kex test generation for 'method' code type arguably overkill solution of modify PsiMethodWrapper class added parameter names, types and return tyepes explicitly updated implementation for java and kotlin * fold all but @test methods regardless of declaration order * remove settings for unsupported kex features * folding works even if junit isn't in classpath * use the the subprocess manager from IJ framework * empty kex path download to .cache and LOCALAPPDATA in windows * fix lint. remove wildcard imports * from IJ api, provide correct build directory to kex For multimodule projects using TestSpark the correct module's build directory path is passed based on the location of code for which tests are generated * set maxTests displayed and minimizeTestSuite options * fix code fold to only happen once at the start not every ui update * error handling for errors in kex process manager also fixed a bug with the way options were passed to kex subprocess made them a list of strings instead of a single big space separated string * fix lint * provide signatures with FQNs to kex for java This also includes a simple string based mapping to jvm types through type erasure * refactor. extract functons, add comments, remove redundancies * fix lint * every option in kex cmd is preceeded by --option * use kotlin.time.Duration instead of Int * refactor error handling code, check for non-zero exit code * make generated code manipulation more robust It no longer depends on the order of the methods generated * only check if the correct verison of kex exists * make download url a property and version a user setting * support jdk version 8. no --add-opens * bump up kex version to 0.0.10 * allow kex test generation for classes not in a package * null safety while folding helper code * Merge branch 'development' into edwin1729/improvement/kex-integration * disallow kex and line code type being chosen simultaneously * update description tab and README.md with kex * fix lint * fix run coverage by making method name same as class name This is expected by TestSpark. Relevant file TestProcess.kt:createXmlFromJacoco:109 * undo mistaken removal of addLanguageTextFieldListener before code folding * Add empty LLM response handling (#342) Show a warning if LLM returns a response that cannot be parsed into a test case (e.g., an explanation of this test case rather than a modification). * Restrict TestSpark action to suitable code types and fix generation for a line (#344) * fix update function * create availableForGeneration * ktlint * feat: add javadoc for `JavaPsiHelper.availableForGeneration` * feat: check for nullness of a PSI file in `TestSparkAction.update` * feat: update javadocs in `PsiComponents.kt` * feat: check for a class or method/func in `KotlinPsiHelper.availableForGeneration` * feat: add TODO to `ToolUtils` about a potential bug The bug is reflected in the issue #375. * feat: make `PsiHelper.getSurroundingLineNumber` return 1-based line numbers Before, the `KotlinPsiHelper` returned a 0-based line number which caused an issue with line-based test generation. The generated prompt contained a line above the selected one. * feat: implement line-based test generation with CUT as a context When there is no surrounding method about the selected line, we use the CUT as a context for this line. The CUT must always be present. Otherwise, the generation action should have been disabled for this line. * refactor: apply ktlint * feat: add `See` in TODO * feat: add TODO and surround $NAME in backticks in `linePrompt` template * feat: collect class constructor signatures in `PsiClassWrapper` * feat: remove backticks from `linePrompt` * feat: fill line-based test generation with additional context The line-based test generation that has a method as a context of the line now also accepts constructors of the containing class. * refactor: use `firstOrNull` for `cut` extraction * refactor: apply ktlint * fix: add required parameter to `ClassRepresentation` in tests * publish: core module version `4.0.0` The major version increased due to the change of the public API of `PromptGenerator.generatePromptForLine` method. --------- Co-authored-by: Vladislav Artiukhov <[email protected]> * Data class for execution results * Minor refactoring * apply ktlint * fix getJavaVersion * add KexSettingsService to plugin.xml * fix DefaultKexSettingsState * fix KexSettingsState * fix Bundles * Force junit4 for EvoSuite tests * JUnit version forcing * fix getExceptionData * fix tools * fix managers * fix TestCasePanelBuilder.kt * fix TestProcessor.kt * remove unused import * fix ktlint * fix ktlint * remove TestSparkStarter.kt * Add support for language applicability checks in Tool * Add support for language applicability checks in Llm * Add support for language applicability checks in Kex * Add support for language applicability checks in EvoSuite * Filter test generation buttons by language compatibility * Refactor imports for SupportedLanguage in EvoSuite and Llm * Set progress indicator text during Kex test generation --------- Co-authored-by: Edwin Fernando <[email protected]> Co-authored-by: Edwin Fernando <[email protected]> Co-authored-by: Edwin Fernando <[email protected]> Co-authored-by: Vladislav Artiukhov <[email protected]> Co-authored-by: Iurii Zaitsev <[email protected]> Co-authored-by: Hello-zoka <[email protected]>
Description of changes made
The PR fixes the following issues:
PsiHelper.getSurroundingLineNumber
; Kotlin used 0-based numbering which attempted the generated prompt, namely it contained a line before the selected one.See commit subjects and bodies for details.
What is left
The PR introduces a modification to
core
module (namely, extends the API ofPromptGenerator
with an overload ofgeneratePromptForLine
method that uses CUT as a line context). Therefore, I (@Vladislav0Art) need to publish a new version of thecore
module.TODOs:
4.0.0
is published.Other notes
Closes #336
Closes #352
Closes #247
Closes #249
Closes #377