Skip to content
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

Kex Integration #431

Open
wants to merge 63 commits into
base: development
Choose a base branch
from

Conversation

arksap2002
Copy link
Collaborator

Integrating Kex (Symbolic Execution) into TestSpark

Generates test cases using Kex and displays them in the TestSpark tool window

Summary of changes

The integration of kex follows the example of EvoSuite and LLM for teh most part, so I highlight just the kex specific parts.

  • A third party java parser is used for preprocessing of generated test files. For each generated test class, the fields @before @test annotated methods are rewritten into a single test method
  • TestGenerationData.otherInfo contains all the helper methods and a static initializer. This shows up in each test case in tool window for compilation and exactly once in the generated test file if "apply to test suite" is chosen
  • To make the test cases in the tool window readable even with the large amount of helper code
    Automatic folding (hiding behind ...) of all code other than test annotated methods in the tool window is implemented. This is using the PsiFile for each test case
  • There are additional fields added to the implementations of PsiMethodWrapper (and implementations) because kex requires specification of the method for which tests are generated in a specific format. Namely it involves FQNs and an example is given here: org.example.Matrix::product(org.example.Matrix,java.util.function.BinaryOperator,java.util.function.BinaryOperator):org.example.Matrix
    Note also that types have been erased. Kex expects JVM types which is easy to get from Java types but not from kotlin. For this reason kotlin is not yet supported by kex.
  • Kex is released as zip file, so it is downloaded and unpacked into the OS-specific cache directory (~/.cache on unix, LOCALAPPDATA in windows). Also note it doesn't currently support ARM linux (but does support ARM mac)
  • Kex uses .class files to generate tests, so the build directory for the correct module is obtained from IJ API

edwin1729 and others added 30 commits January 14, 2025 12:54
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
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
Deleted stuff:
running Kex through through OSProcessHandler (IJ sdk)
running Kex with kex.py
downloading kex from github in build.gradle.kts
arguably overkill solution of modify PsiMethodWrapper class
added parameter names, types and return tyepes explicitly
updated implementation for java and kotlin
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
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
This also includes a simple string based mapping to jvm types through type erasure
It no longer depends on the order of the methods generated
edwin1729 and others added 19 commits January 14, 2025 13:09
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).
…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]>
@arksap2002 arksap2002 force-pushed the edwin1729/improvement/kex-integration branch from 3435206 to a919e65 Compare January 14, 2025 17:05
@pderakhshanfar
Copy link
Collaborator

I have a weird issue in Kex test generation. The indicator starts with Building project state (which is normal. However, it gets in the same state till it shows the generated tests. It is probably because the indicator has never been updated during the test generation

@pderakhshanfar
Copy link
Collaborator

Also another issue, When trying to generate tests by Kex on Kotlin, it generates some random Java code

@arksap2002 arksap2002 added new feature Implementation of a new feature and removed Ready for review labels Jan 17, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
new feature Implementation of a new feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants