Skip to content

Commit dffca68

Browse files
authored
Little improvements related to TODOs in code of CLion plugin (#351)
1 parent 4ae7f4e commit dffca68

File tree

10 files changed

+111
-66
lines changed

10 files changed

+111
-66
lines changed

clion-plugin/src/main/kotlin/org/utbot/cpp/clion/plugin/actions/generate/GenerateForPredicateAction.kt

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,34 @@ import org.utbot.cpp.clion.plugin.client.requests.FunctionReturnTypeRequest
1515
import org.utbot.cpp.clion.plugin.client.requests.PredicateRequest
1616
import org.utbot.cpp.clion.plugin.utils.activeProject
1717
import org.utbot.cpp.clion.plugin.utils.client
18+
import org.utbot.cpp.clion.plugin.utils.notifyError
1819
import testsgen.Util.ValidationType
1920
import java.awt.Dimension
2021
import java.awt.event.KeyAdapter
2122
import java.awt.event.KeyEvent
2223
import java.math.BigInteger
2324
import java.util.function.Supplier
2425

26+
/**
27+
* Action to take needed information from user and trigger generation for predicate.
28+
*
29+
* Predicate request generates tests that satisfy some condition (predicate) for
30+
* function return type. E.g. tests where return value (function return type is int)
31+
* is smaller than 10.
32+
*
33+
* To assemble predicate request we need to know function return type.
34+
* For that we send request to server @see [FunctionReturnTypeRequest].
35+
* Depending on return type we ask user what comparison operator to use.
36+
* For example, if return type is bool, we suggest == or !=, if it is
37+
* not bool, then available operators are: ==, <=, >=, <, >. @see [chooseComparisonOperator]
38+
* Then we ask user for a value to compare with, @see [chooseReturnValue].
39+
* Then we assemble the predicate request and launch its execution @see [sendPredicateToServer].
40+
*
41+
* For asking comparison operator and return value we use popups.
42+
*/
2543
class GenerateForPredicateAction : BaseGenerateTestsAction() {
26-
2744
override fun actionPerformed(e: AnActionEvent) {
45+
// when we gathered all needed information for predicate request, assemble it and execute it.
2846
fun sendPredicateToServer(validationType: ValidationType, valueToCompare: String, comparisonOperator: String) =
2947
PredicateRequest(
3048
getPredicateGrpcRequest(e, comparisonOperator, validationType, valueToCompare),
@@ -33,7 +51,7 @@ class GenerateForPredicateAction : BaseGenerateTestsAction() {
3351
e.client.executeRequest(this)
3452
}
3553

36-
//TODO: this code requires some comments
54+
// ask for comparison operator to use in predicate
3755
fun chooseComparisonOperator(
3856
validationType: ValidationType,
3957
proceedWithComparisonOperator: (comparisonOperator: String) -> Unit,
@@ -44,6 +62,20 @@ class GenerateForPredicateAction : BaseGenerateTestsAction() {
4462
proceedWithComparisonOperator("==")
4563
return
4664
}
65+
ValidationType.UNSUPPORTED -> {
66+
notifyError(
67+
"Unsupported return type for \'Generate Tests With Prompted Result\' feature: \n" +
68+
"supported types are integers, booleans, characters, floats and strings"
69+
)
70+
return
71+
}
72+
ValidationType.UNRECOGNIZED -> {
73+
notifyError(
74+
"Could not recognise return type for 'Generate Tests With Prompted Result' feature: \n" +
75+
"supported types are integers, booleans, characters, floats and strings"
76+
)
77+
return
78+
}
4779
else -> {
4880
val operators = listOf("==", "<=", "=>", "<", ">")
4981
createListPopup("Select predicate", operators) { comparisonOperator ->
@@ -53,6 +85,7 @@ class GenerateForPredicateAction : BaseGenerateTestsAction() {
5385
}
5486
}
5587

88+
// ask for return value of the function to compare with
5689
fun chooseReturnValue(
5790
validationType: ValidationType,
5891
proceedWithValueToCompare: (valueToCompare: String) -> Unit,
@@ -63,14 +96,17 @@ class GenerateForPredicateAction : BaseGenerateTestsAction() {
6396
}
6497
popup.showInBestPositionFor(e.dataContext)
6598
}
66-
99+
//ask server for return type
67100
FunctionReturnTypeRequest(
68101
getFunctionGrpcRequest(e),
69102
e.activeProject(),
70103
) { functionReturnType ->
71104
val validationType = functionReturnType.validationType
105+
// then ask for comparison operator to use from user
72106
chooseComparisonOperator(validationType) { comparisonOperator ->
107+
// then ask for return value to compare with from user
73108
chooseReturnValue(validationType) { valueToCompare ->
109+
// when we have all needed information, assemble and execute
74110
sendPredicateToServer(validationType, valueToCompare, comparisonOperator)
75111
}
76112
}
@@ -95,6 +131,7 @@ class GenerateForPredicateAction : BaseGenerateTestsAction() {
95131
private fun createTrueFalsePopup(onChoose: (String) -> Unit) =
96132
createListPopup("Select bool value", listOf("true", "false")) { onChoose(it) }
97133

134+
//creates popup with input textfield, used for asking return value
98135
private fun createTextFieldPopup(type: ValidationType, onChoose: (String) -> Unit): JBPopup {
99136
val textField = ExtendableTextField()
100137
textField.minimumSize = Dimension(100, textField.width)
@@ -142,7 +179,7 @@ class GenerateForPredicateAction : BaseGenerateTestsAction() {
142179
}
143180

144181
companion object {
145-
//TODO: why don't we have DOUBLE and BYTE here?
182+
//Note: server does not support some types like byte or boolean
146183
val defaultReturnValues = mapOf(
147184
ValidationType.INT8_T to "0",
148185
ValidationType.INT16_T to "0",

clion-plugin/src/main/kotlin/org/utbot/cpp/clion/plugin/client/Client.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,8 @@ class Client(
8383

8484
fun configureProject() {
8585
CheckProjectConfigurationRequest(
86+
getProjectConfigGrpcRequest(project, Testgen.ConfigMode.CHECK),
8687
project,
87-
getProjectConfigGrpcRequest(project, Testgen.ConfigMode.CHECK)
8888
).also {
8989
executeRequest(it)
9090
}

clion-plugin/src/main/kotlin/org/utbot/cpp/clion/plugin/client/handlers/TestsStreamHandler.kt

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import org.utbot.cpp.clion.plugin.utils.refreshAndFindIOFile
1010
import testsgen.Testgen
1111
import testsgen.Util
1212
import java.nio.file.Path
13-
import java.nio.file.Paths
1413

1514
class TestsStreamHandler(
1615
project: Project,
@@ -36,10 +35,10 @@ class TestsStreamHandler(
3635

3736
private fun handleSourceCode(sources: List<Util.SourceCode>, isStubs: Boolean = false) {
3837
sources.forEach { sourceCode ->
39-
val filePath: String = sourceCode.filePath.convertFromRemotePathIfNeeded(project)
38+
val filePath: Path = sourceCode.filePath.convertFromRemotePathIfNeeded(project)
4039

4140
if (!isStubs)
42-
myGeneratedTestFilesLocalFS.add(Paths.get(filePath))
41+
myGeneratedTestFilesLocalFS.add(filePath)
4342

4443
if (sourceCode.code.isNotEmpty()) {
4544
project.logger.trace { "Creating generated test file: $filePath." }
@@ -50,7 +49,7 @@ class TestsStreamHandler(
5049
}
5150

5251
var infoMessage = "Generated " + if (isStubs) "stub" else "test" + " file"
53-
if (isGeneratedFileTestSourceFile(filePath))
52+
if (isGeneratedFileTestSourceFile(filePath.toString()))
5453
infoMessage += " with ${sourceCode.regressionMethodsNumber} tests in regression suite" +
5554
" and ${sourceCode.errorMethodsNumber} tests in error suite"
5655
project.logger.info { "$infoMessage: $filePath" }

clion-plugin/src/main/kotlin/org/utbot/cpp/clion/plugin/coverage/UTBotCoverageRunner.kt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,16 @@ import testsgen.Testgen
1313
import java.io.File
1414
import java.nio.charset.StandardCharsets
1515
import java.nio.file.Files
16-
import java.nio.file.Paths
16+
import java.nio.file.Path
1717

1818
/**
1919
* This class is used to convert from our representation of coverage to IntelliJ's [ProjectData]
2020
*/
2121
class UTBotCoverageRunner : CoverageRunner() {
2222
private val log = Logger.getInstance(this::class.java)
23-
private fun getLineCount(filePath: String): Int {
23+
private fun getLineCount(filePath: Path): Int {
2424
var lineCount: Int
25-
Files.lines(Paths.get(filePath), StandardCharsets.UTF_8).use { stream -> lineCount = stream.count().toInt() }
25+
Files.lines(filePath, StandardCharsets.UTF_8).use { stream -> lineCount = stream.count().toInt() }
2626
return lineCount
2727
}
2828

@@ -40,12 +40,12 @@ class UTBotCoverageRunner : CoverageRunner() {
4040
if (filePathFromServer.isNotEmpty()) {
4141
isAnyCoverage = true
4242
val localFilePath = filePathFromServer.convertFromRemotePathIfNeeded(baseCoverageSuite.project)
43-
if (!Paths.get(localFilePath).exists()) {
43+
if (!localFilePath.exists()) {
4444
log.warn("Skipping $localFilePath in coverage processing as it does not exist!")
4545
continue
4646
}
4747
val lines = arrayOfNulls<LineData>(getLineCount(localFilePath))
48-
val classData = projectData.getOrCreateClassData(provideQualifiedNameForFile(localFilePath))
48+
val classData = projectData.getOrCreateClassData(provideQualifiedNameForFile(localFilePath.toAbsolutePath().toString()))
4949
fun processRanges(rangesList: List<Testgen.SourceLine?>, status: Byte) {
5050
rangesList.filterNotNull().forEach {
5151
val lineData = LineData(it.line + 1, null)

clion-plugin/src/main/kotlin/org/utbot/cpp/clion/plugin/grpc/VersionGrpcRequests.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@ package org.utbot.cpp.clion.plugin.grpc
22

33
import testsgen.Testgen
44

5-
//TODO: hardcoding the version is a bad practice, determine it somehow
6-
fun getVersionGrpcRequest(): Testgen.VersionInfo = Testgen.VersionInfo.newBuilder().setVersion("2022.7").build()
5+
//TODO: when plugin is ready for release, take version from publish github action.
6+
fun getVersionGrpcRequest(): Testgen.VersionInfo = Testgen.VersionInfo.newBuilder().setVersion("0.0.1").build()

clion-plugin/src/main/kotlin/org/utbot/cpp/clion/plugin/settings/UTBotAllSettings.kt

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -212,11 +212,15 @@ class UTBotAllSettings(val project: Project) {
212212

213213
val convertedProjectPath: String get() = projectPath.convertToRemotePathIfNeeded(project)
214214

215-
//TODO: it seems to be a kind of boolshit to me
216215
private val isLocalHost: Boolean
217-
get() = serverName == "localhost" || serverName == "127.0.0.01"
218-
219-
//TODO: it is unclear, requires a comment
216+
get() = serverName == "localhost" || serverName == "127.0.0.1"
217+
218+
/**
219+
* If this property returns true, plugin must convert path sent and returned from server.
220+
* @see [String.convertToRemotePathIfNeeded], [String.convertFromRemotePathIfNeeded]
221+
*
222+
* If we are on Windows, this is not a server, so it is always a remote scenario.
223+
*/
220224
val isRemoteScenario: Boolean
221225
get() = !(remotePath == projectPath && isLocalHost) || isWindows
222226

clion-plugin/src/main/kotlin/org/utbot/cpp/clion/plugin/ui/sourceFoldersView/Updater.kt

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
package org.utbot.cpp.clion.plugin.ui.sourceFoldersView
22

33
import com.intellij.psi.PsiDirectory
4-
import org.utbot.cpp.clion.plugin.utils.addDirectoriesRecursive
5-
import org.utbot.cpp.clion.plugin.utils.removeDirectoriesRecursive
4+
import org.utbot.cpp.clion.plugin.utils.visitAllDirectories
65

76
interface DirectoriesStatusUpdater {
87
fun toggle()
@@ -11,6 +10,30 @@ interface DirectoriesStatusUpdater {
1110
}
1211

1312
abstract class BaseUpdater(val selectedDirectories: List<PsiDirectory>) : DirectoriesStatusUpdater {
13+
// merge directories' paths from this set, with all the directories from passed list including nested directories
14+
private fun Set<String>.addDirectoriesRecursive(dirsToAdd: List<PsiDirectory>): Set<String> {
15+
val newSourceFolders = this.toMutableSet()
16+
dirsToAdd.forEach { dir ->
17+
newSourceFolders.add(dir.virtualFile.path)
18+
dir.virtualFile.toNioPath().visitAllDirectories {
19+
newSourceFolders.add(it.toString())
20+
}
21+
}
22+
return newSourceFolders
23+
}
24+
25+
// subtract from this set of directories the passed directories including nested directories
26+
private fun Set<String>.removeDirectoriesRecursive(dirsToRemove: List<PsiDirectory>): Set<String> {
27+
val newSourceFolders = this.toMutableSet()
28+
dirsToRemove.forEach { dir ->
29+
newSourceFolders.add(dir.virtualFile.path)
30+
dir.virtualFile.toNioPath().visitAllDirectories {
31+
newSourceFolders.remove(it.toString())
32+
}
33+
}
34+
return newSourceFolders
35+
}
36+
1437
abstract fun getCurrentMarkedDirs(): Set<String>
1538
abstract fun setCurrentMarkedDirs(value: Set<String>)
1639
override fun markAsSource() {

clion-plugin/src/main/kotlin/org/utbot/cpp/clion/plugin/ui/targetsToolWindow/UTBotTarget.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ import testsgen.Testgen
66

77
data class UTBotTarget(val path: String, val name: String, val description: String) {
88
constructor(target: Testgen.ProjectTarget, project: Project) : this(
9-
if (target.name == autoTarget.name) target.path else target.path.convertFromRemotePathIfNeeded(project),
9+
if (target.name == autoTarget.name) target.path else target.path.convertFromRemotePathIfNeeded(project)
10+
.toAbsolutePath().toString(),
1011
target.name,
1112
target.description
1213
)

clion-plugin/src/main/kotlin/org/utbot/cpp/clion/plugin/ui/testsResults/TestsResultsStorage.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ class TestsResultsStorage(val project: Project) {
6363
it.file?.toNioPath()
6464
}
6565
for (testResult in storage.values) {
66-
if (Paths.get(testResult.testFilePath.convertFromRemotePathIfNeeded(project)) in currentlyOpenedFilePaths) {
66+
if (testResult.testFilePath.convertFromRemotePathIfNeeded(project) in currentlyOpenedFilePaths) {
6767
return true
6868
}
6969
}

0 commit comments

Comments
 (0)