Skip to content

Commit 443db20

Browse files
committed
Refactored code to avoid code duplication
1 parent 67b0874 commit 443db20

File tree

4 files changed

+72
-72
lines changed

4 files changed

+72
-72
lines changed

utbot-intellij/src/main/kotlin/org/utbot/intellij/plugin/javadoc/UtCustomJavaDocTagProvider.kt

Lines changed: 9 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -6,30 +6,20 @@ import com.intellij.psi.PsiReference
66
import com.intellij.psi.javadoc.CustomJavadocTagProvider
77
import com.intellij.psi.javadoc.JavadocTagInfo
88
import com.intellij.psi.javadoc.PsiDocTagValue
9+
import org.utbot.summary.comment.CustomJavaDocTag
10+
import org.utbot.summary.comment.CustomJavaDocTagProvider
911

1012
/**
1113
* Provides plugin's custom JavaDoc tags to make test summaries structured.
1214
*/
1315
class UtCustomJavaDocTagProvider : CustomJavadocTagProvider {
14-
// The tags' order is important.
15-
override fun getSupportedTags(): List<UtCustomTag> =
16-
listOf(
17-
UtCustomTag.ClassUnderTest,
18-
UtCustomTag.MethodUnderTest,
19-
UtCustomTag.ExpectedResult,
20-
UtCustomTag.ActualResult,
21-
UtCustomTag.Executes,
22-
UtCustomTag.Invokes,
23-
UtCustomTag.Iterates,
24-
UtCustomTag.ReturnsFrom,
25-
UtCustomTag.ThrowsException,
26-
)
27-
28-
//TODO: move it to another module to avoid duplication
29-
sealed class UtCustomTag(private val name: String, private val message: String) : JavadocTagInfo {
30-
override fun getName(): String = name
31-
32-
fun getMessage(): String = message
16+
override fun getSupportedTags(): List<UtCustomTagInfo> =
17+
CustomJavaDocTagProvider().getPluginCustomTags().map { t -> UtCustomTagInfo(t) }
18+
19+
class UtCustomTagInfo(private val tag: CustomJavaDocTag) : JavadocTagInfo {
20+
override fun getName(): String = tag.name
21+
22+
fun getMessage(): String = tag.message
3323

3424
override fun isInline() = false
3525

@@ -40,15 +30,5 @@ class UtCustomJavaDocTagProvider : CustomJavadocTagProvider {
4030
override fun isValidInContext(element: PsiElement?): Boolean {
4131
return element is PsiMethod
4232
}
43-
44-
object ClassUnderTest : UtCustomTag("utbot.classUnderTest", "Class under test")
45-
object MethodUnderTest : UtCustomTag("utbot.methodUnderTest", "Method under test")
46-
object ExpectedResult : UtCustomTag("utbot.expectedResult", "Expected result")
47-
object ActualResult : UtCustomTag("utbot.actualResult", "Actual result")
48-
object Executes : UtCustomTag("utbot.executesCondition", "Executes condition")
49-
object Invokes : UtCustomTag("utbot.invokes", "Invokes")
50-
object Iterates : UtCustomTag("utbot.iterates", "Iterates")
51-
object ReturnsFrom : UtCustomTag("utbot.returnsFrom", "Returns from")
52-
object ThrowsException : UtCustomTag("utbot.throwsException", "Throws exception")
5333
}
5434
}

utbot-intellij/src/main/kotlin/org/utbot/intellij/plugin/javadoc/UtJavaDocInfoGenerator.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class UtJavaDocInfoGenerator {
4141
private fun generateUtTagSection(
4242
builder: StringBuilder,
4343
comment: PsiDocComment?,
44-
utTag: UtCustomJavaDocTagProvider.UtCustomTag
44+
utTag: UtCustomJavaDocTagProvider.UtCustomTagInfo
4545
) {
4646
if (comment != null) {
4747
val tag = comment.findTagByName(utTag.name) ?: return

utbot-summary/src/main/kotlin/org/utbot/summary/comment/CustomJavaDocCommentBuilder.kt

Lines changed: 6 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
package org.utbot.summary.comment
22

33
import org.utbot.framework.plugin.api.DocCustomTagStatement
4-
import org.utbot.framework.plugin.api.DocRegularStmt
54
import org.utbot.framework.plugin.api.DocStatement
65
import org.utbot.framework.plugin.api.exceptionOrNull
76
import org.utbot.summary.ast.JimpleToASTMap
87
import org.utbot.summary.tag.TraceTagWithoutExecution
98
import soot.SootMethod
109

11-
//TODO: polish code
1210
class CustomJavaDocCommentBuilder(
1311
traceTag: TraceTagWithoutExecution,
1412
sootToAST: MutableMap<SootMethod, JimpleToASTMap>
@@ -19,33 +17,8 @@ class CustomJavaDocCommentBuilder(
1917
*/
2018
fun buildDocStatements(method: SootMethod): List<DocStatement> {
2119
val comment: CustomJavaDocComment = buildCustomJavaDocComment(method)
22-
val docStatementList = mutableListOf<DocStatement>()
23-
24-
docStatementList += DocRegularStmt("@utbot.classUnderTest ${comment.classUnderTest}\n")
25-
docStatementList += DocRegularStmt("@utbot.methodUnderTest ${comment.methodUnderTest}\n")
26-
27-
if (comment.expectedResult.isNotEmpty())
28-
docStatementList += DocRegularStmt("@utbot.expectedResult ${comment.expectedResult}\n")
29-
if (comment.actualResult.isNotEmpty())
30-
docStatementList += DocRegularStmt("@utbot.actualResult ${comment.actualResult}\n")
31-
if (comment.executesCondition.isNotEmpty()) {
32-
val statement =
33-
"@utbot.executesCondition ${comment.executesCondition.joinToString(separator = ",\n")}\n"
34-
docStatementList += DocRegularStmt(statement)
35-
}
36-
if (comment.invokes.isNotEmpty()) {
37-
val statement = "@utbot.invokes ${comment.invokes.joinToString(separator = ",\n")}\n"
38-
docStatementList += DocRegularStmt(statement)
39-
}
40-
if (comment.iterates.isNotEmpty()) {
41-
val statement = "@utbot.iterates ${comment.iterates.joinToString(separator = ",\n")}\n"
42-
docStatementList += DocRegularStmt(statement)
43-
}
44-
if (comment.returnsFrom.isNotEmpty())
45-
docStatementList += DocRegularStmt("@utbot.returnsFrom ${comment.returnsFrom}\n")
46-
if (comment.throwsException.isNotEmpty())
47-
docStatementList += DocRegularStmt("@utbot.throwsException ${comment.throwsException}")
48-
20+
val docStatementList =
21+
CustomJavaDocTagProvider().getPluginCustomTags().mapNotNull { it.generateDocStatement(comment) }
4922
return listOf<DocStatement>(DocCustomTagStatement(docStatementList))
5023
}
5124

@@ -95,20 +68,11 @@ class CustomJavaDocCommentBuilder(
9568
while (currentBlock != null) {
9669
for (statement in currentBlock.stmtTexts) {
9770
when (statement.stmtType) {
98-
StmtType.Invoke -> {
99-
val info = statement.description
100-
customJavaDocComment.invokes += "{@code $info}"
101-
}
102-
StmtType.Condition -> {
103-
val info = statement.description
104-
customJavaDocComment.executesCondition += "{@code $info}"
105-
}
106-
StmtType.Return -> {
107-
val info = statement.description
108-
customJavaDocComment.returnsFrom = "{@code $info}"
109-
}
71+
StmtType.Invoke -> customJavaDocComment.invokes += "{@code ${statement.description}}"
72+
StmtType.Condition -> customJavaDocComment.executesCondition += "{@code ${statement.description}}"
73+
StmtType.Return -> customJavaDocComment.returnsFrom = "{@code ${statement.description}}"
11074
else -> {
111-
//TODO
75+
//TODO: do we need to handle others?
11276
}
11377
}
11478
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package org.utbot.summary.comment
2+
3+
import org.utbot.framework.plugin.api.DocRegularStmt
4+
5+
/**
6+
* Provides a list of supported custom JavaDoc tags.
7+
*/
8+
class CustomJavaDocTagProvider {
9+
// The tags' order is important because plugin builds final JavaDoc comment according to it.
10+
fun getPluginCustomTags(): List<CustomJavaDocTag> =
11+
listOf(
12+
CustomJavaDocTag.ClassUnderTest,
13+
CustomJavaDocTag.MethodUnderTest,
14+
CustomJavaDocTag.ExpectedResult,
15+
CustomJavaDocTag.ActualResult,
16+
CustomJavaDocTag.Executes,
17+
CustomJavaDocTag.Invokes,
18+
CustomJavaDocTag.Iterates,
19+
CustomJavaDocTag.ReturnsFrom,
20+
CustomJavaDocTag.ThrowsException,
21+
)
22+
}
23+
24+
sealed class CustomJavaDocTag(
25+
val name: String,
26+
val message: String,
27+
private val valueRetriever: (CustomJavaDocComment) -> Any
28+
) {
29+
object ClassUnderTest :
30+
CustomJavaDocTag("utbot.classUnderTest", "Class under test", CustomJavaDocComment::classUnderTest)
31+
32+
object MethodUnderTest :
33+
CustomJavaDocTag("utbot.methodUnderTest", "Method under test", CustomJavaDocComment::methodUnderTest)
34+
35+
object ExpectedResult :
36+
CustomJavaDocTag("utbot.expectedResult", "Expected result", CustomJavaDocComment::expectedResult)
37+
38+
object ActualResult : CustomJavaDocTag("utbot.actualResult", "Actual result", CustomJavaDocComment::actualResult)
39+
object Executes :
40+
CustomJavaDocTag("utbot.executesCondition", "Executes condition", CustomJavaDocComment::executesCondition)
41+
42+
object Invokes : CustomJavaDocTag("utbot.invokes", "Invokes", CustomJavaDocComment::invokes)
43+
object Iterates : CustomJavaDocTag("utbot.iterates", "Iterates", CustomJavaDocComment::iterates)
44+
object ReturnsFrom : CustomJavaDocTag("utbot.returnsFrom", "Returns from", CustomJavaDocComment::returnsFrom)
45+
object ThrowsException :
46+
CustomJavaDocTag("utbot.throwsException", "Throws exception", CustomJavaDocComment::throwsException)
47+
48+
fun generateDocStatement(comment: CustomJavaDocComment): DocRegularStmt? {
49+
val value = valueRetriever.invoke(comment)
50+
return if (value is String) {
51+
if (value.isNotEmpty()) DocRegularStmt("@$name $value\n") else null
52+
} else if (value is List<*>) {
53+
if (value.isNotEmpty()) DocRegularStmt("@$name ${value.joinToString(separator = ",\n")}\n") else null
54+
} else null
55+
}
56+
}

0 commit comments

Comments
 (0)