Skip to content

Commit 6b3510f

Browse files
author
Arseniy Volynets
committed
Refactor UTBotRunWithCoverageLineMarkerInfo
1 parent 530752b commit 6b3510f

File tree

3 files changed

+53
-56
lines changed

3 files changed

+53
-56
lines changed

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

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,6 @@ class CoverageAndResultsHandler(
4949
project.messageBus.syncPublisher(UTBotTestResultsReceivedListener.TOPIC)
5050
.testResultsReceived(response.testRunResultsList)
5151

52-
logCoverageResponse(response)
53-
5452
val engine = CoverageEngine.EP_NAME.findExtension(UTBotCoverageEngine::class.java)
5553
?: error("UTBotEngine instance is not found!")
5654
val coverageRunner = CoverageRunner.getInstance(UTBotCoverageRunner::class.java)
@@ -67,14 +65,6 @@ class CoverageAndResultsHandler(
6765
notifyCoverageReceived()
6866
}
6967

70-
private fun logCoverageResponse(response: Testgen.CoverageAndResultsResponse) {
71-
if (response.errorMessage.isNotEmpty())
72-
project.logger.warn { response.errorMessage }
73-
if (response.coveragesList.isEmpty())
74-
project.logger.error { "No coverage received from server!" }
75-
project.logger.trace { "coverage list: \n${response.coveragesList}" }
76-
}
77-
7868
private fun notifyCoverageReceived() {
7969
if (sourceFilePath != null) {
8070
notifyInfo("Coverage received!", project, FocusAction(sourceFilePath))

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

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,14 @@
11
package org.utbot.cpp.clion.plugin.ui.services
22

33
import com.intellij.codeInsight.daemon.DaemonCodeAnalyzer
4-
import com.intellij.icons.AllIcons
54
import com.intellij.openapi.components.Service
65
import com.intellij.openapi.diagnostic.Logger
76
import com.intellij.openapi.fileEditor.FileEditorManager
87
import com.intellij.openapi.project.Project
98
import com.intellij.openapi.vfs.VirtualFileManager
109
import com.intellij.openapi.vfs.newvfs.BulkFileListener
1110
import com.intellij.openapi.vfs.newvfs.events.VFileEvent
12-
import com.intellij.psi.PsiElement
13-
import javax.swing.Icon
1411
import org.utbot.cpp.clion.plugin.listeners.UTBotTestResultsReceivedListener
15-
import org.utbot.cpp.clion.plugin.ui.testsResults.TestNameAndTestSuite
1612
import org.utbot.cpp.clion.plugin.utils.convertFromRemotePathIfNeeded
1713
import testsgen.Testgen
1814
import java.util.concurrent.ConcurrentHashMap
@@ -58,6 +54,8 @@ class TestsResultsStorage(val project: Project) {
5854

5955
}
6056

57+
fun getTestResultByTestName(testName: String): Testgen.TestResultObject? = storage[testName]
58+
6159
private fun shouldForceUpdate(): Boolean {
6260
val currentlyOpenedFilePaths = FileEditorManager.getInstance(project)
6361
.selectedEditors
@@ -75,21 +73,4 @@ class TestsResultsStorage(val project: Project) {
7573
if (shouldForceUpdate())
7674
DaemonCodeAnalyzer.getInstance(project).restart()
7775
}
78-
79-
fun getTestStatusIcon(element: PsiElement): Icon {
80-
if (element.text == "UTBot") {
81-
return AllIcons.RunConfigurations.TestState.Run_run
82-
}
83-
84-
val testName: String = TestNameAndTestSuite.create(element).name
85-
if (!storage.contains(testName) || testName.isEmpty()) {
86-
return AllIcons.RunConfigurations.TestState.Run
87-
}
88-
89-
return when (storage[testName]!!.status) {
90-
Testgen.TestStatus.TEST_FAILED -> AllIcons.RunConfigurations.TestState.Red2
91-
Testgen.TestStatus.TEST_PASSED -> AllIcons.RunConfigurations.TestState.Green2
92-
else -> AllIcons.RunConfigurations.TestError
93-
}
94-
}
95-
}
76+
}

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

Lines changed: 50 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package org.utbot.cpp.clion.plugin.ui.testsResults
22

33
import com.intellij.codeInsight.daemon.LineMarkerInfo
44
import com.intellij.codeInsight.daemon.LineMarkerProvider
5+
import com.intellij.icons.AllIcons
56
import com.intellij.openapi.actionSystem.AnAction
67
import com.intellij.openapi.components.service
78
import com.intellij.openapi.diagnostic.Logger
@@ -10,41 +11,66 @@ import com.intellij.psi.PsiElement
1011
import javax.swing.Icon
1112
import org.utbot.cpp.clion.plugin.actions.generate.RunWithCoverageAction
1213
import org.utbot.cpp.clion.plugin.ui.services.TestsResultsStorage
14+
import testsgen.Testgen
1315

1416
class UTBotTestRunLineMarkerProvider : LineMarkerProvider {
1517
val log = Logger.getInstance(this::class.java)
1618

1719
override fun getLineMarkerInfo(element: PsiElement): LineMarkerInfo<*>? {
18-
if (element.firstChild != null
19-
//TODO: introduce some alternative to the hardcode of this constants everywhere
20-
|| (element.text != "TEST" && element.text != "UTBot")
21-
|| element.containingFile.name.endsWith(".h")) {
22-
return null
23-
}
24-
val message = if (element.text == "TEST") "UTBot: Run with coverage" else "Run all tests with coverage"
25-
val icon = element.project.service<TestsResultsStorage>().getTestStatusIcon(element)
26-
27-
return UTBotRunWithCoverageLineMarkerInfo(element, message, icon)
20+
return UTBotRunWithCoverageLineMarkerInfo.createFromPsiElementOrNull(element)
2821
}
2922

30-
private class UTBotRunWithCoverageLineMarkerInfo(
31-
callElement: PsiElement,
32-
message: String,
33-
icon: Icon,
34-
): LineMarkerInfo<PsiElement>(
35-
callElement,
36-
callElement.textRange,
37-
icon,
38-
{ message },
39-
null,
40-
GutterIconRenderer.Alignment.LEFT,
41-
{ message }
42-
) {
23+
private class UTBotRunWithCoverageLineMarkerInfo private constructor(
24+
callElement: PsiElement,
25+
message: String,
26+
icon: Icon,
27+
) : LineMarkerInfo<PsiElement>(callElement, callElement.textRange, icon, { message }, null, GutterIconRenderer.Alignment.LEFT, { message }) {
4328
override fun createGutterRenderer(): GutterIconRenderer {
4429
return object : LineMarkerGutterIconRenderer<PsiElement>(this) {
4530
override fun isNavigateAction(): Boolean = true
4631
override fun getClickAction(): AnAction? = element?.let { RunWithCoverageAction(it) }
4732
}
4833
}
34+
35+
36+
companion object {
37+
fun createFromPsiElementOrNull(element: PsiElement): UTBotRunWithCoverageLineMarkerInfo? {
38+
if (element.firstChild != null || (!isSingleTest(element) && !isAllTests(element)) || element.containingFile.name.endsWith(".h")) {
39+
return null
40+
}
41+
val message = if (isSingleTest(element)) "UTBot: Run with coverage" else "Run all tests with coverage"
42+
43+
return UTBotRunWithCoverageLineMarkerInfo(element, message, getStatusIcon(element))
44+
}
45+
46+
private fun getStatusIcon(element: PsiElement): Icon {
47+
// return icon for Running All Tests
48+
if (!isAllTests(element)) {
49+
return AllIcons.RunConfigurations.TestState.Run_run
50+
}
51+
52+
val testName: String = TestNameAndTestSuite.create(element).name
53+
val testResult: Testgen.TestResultObject? = element.project.service<TestsResultsStorage>().getTestResultByTestName(testName)
54+
55+
// if there's no info about TestResult with the specified name, return icon for running single test
56+
if (testResult == null || testName.isEmpty()) {
57+
return AllIcons.RunConfigurations.TestState.Run
58+
}
59+
60+
// return icon corresponding to cached test status
61+
return when (testResult.status) {
62+
Testgen.TestStatus.TEST_FAILED -> AllIcons.RunConfigurations.TestState.Red2
63+
Testgen.TestStatus.TEST_PASSED -> AllIcons.RunConfigurations.TestState.Green2
64+
else -> AllIcons.RunConfigurations.TestError
65+
}
66+
}
67+
}
68+
}
69+
70+
companion object {
71+
private const val RUN_SINGLE_TEST_TEXT_MARK = "TEST"
72+
private const val RUN_ALL_TESTS_TEXT_MARK = "UTBot"
73+
private fun isSingleTest(element: PsiElement) = element.text == RUN_SINGLE_TEST_TEXT_MARK
74+
private fun isAllTests(element: PsiElement) = element.text == RUN_ALL_TESTS_TEXT_MARK
4975
}
50-
}
76+
}

0 commit comments

Comments
 (0)