JetBrains-Research / TestSpark

TestSpark - a plugin for generating unit tests. TestSpark natively integrates different AI-based test generation tools and techniques in the IDE. Started by SERG TU Delft. Currently under implementation by JetBrains Research (Software Testing Research) for research purposes.
MIT License
47 stars 13 forks source link

Incorrect construct under test selection after code type radio button click #251

Closed Vladislav0Art closed 2 months ago

Vladislav0Art commented 3 months ago

Describe the bug

Execution error due to specific construct under test selection.

In the following class, place a cursor on the first line (marked with *()**), press right click and select TestSpark for test generation; then select a line as a construct under test and proceed to generation. You will see an error stating that the no method was selected. This is because TestSparkAction.startUnitTestGenerationTool checks the 0th, 1st, and 2nd buttons, assuming that the 1st always corresponds to a method, although it is not true in this scenario (here, the 1st button corresponds to the line).

public class Calc { (*)
    public int sum(int a, int b) {
        return a + b; // line 3
    }

    public int multiply(int a, int b) {
        return a * b;
    }
}

To Reproduce Steps to reproduce the behavior:

  1. Use the example above (or any other class you have) and place the cursor on the very first line of the class definition.
  2. Select test generation via TestSpark and select line 1 as a construct under test.
  3. Start the generation
  4. See error

Expected behavior TestSpark should generate tests for a line in this scenario; the fix requires re-implementing the PsiHelper.getCurrentListOfCodeTypes method.

Additional context Suggestion for the fix:

PsiHelper.getCurrentListOfCodeTypes returns Array<*>?, although the returned object is never null and always contains only String.

It is better to return List<CodeTypeOption> where CodeTypeOption = (org.jetbrains.research.testspark.data.CodeType, String). Next, store pairs of (org.jetbrains.research.testspark.data.CodeType, JRadioButton) within TestSparkAction.codeTypeButtons, so that we know what button corresponds to what code type without relying on indices of TestSparkAction.codeTypeButtons.

Frosendroska commented 2 months ago

Fixed in the PR