Skip to content

Commit c067536

Browse files
committed
Review fixes: extracted method, polished code
1 parent 8e940a3 commit c067536

File tree

3 files changed

+30
-29
lines changed

3 files changed

+30
-29
lines changed

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ class CustomJavaDocCommentBuilder(
4949
val reason = findExceptionReason(currentMethod, thrownException)
5050
"{@link $exceptionName} $reason"
5151
}
52+
5253
if (exceptionThrow != null) {
5354
customJavaDocComment.throwsException = exceptionThrow
5455
}
@@ -64,9 +65,8 @@ class CustomJavaDocCommentBuilder(
6465
}
6566

6667
// builds Invoke, Execute, Return sections
67-
var currentBlock: SimpleSentenceBlock? = rootSentenceBlock
68-
while (currentBlock != null) {
69-
for (statement in currentBlock.stmtTexts) {
68+
generateSequence(rootSentenceBlock) { it.nextBlock }.forEach {
69+
for (statement in it.stmtTexts) {
7070
when (statement.stmtType) {
7171
StmtType.Invoke -> customJavaDocComment.invokes += "{@code ${statement.description}}"
7272
StmtType.Condition -> customJavaDocComment.executesCondition += "{@code ${statement.description}}"
@@ -76,7 +76,6 @@ class CustomJavaDocCommentBuilder(
7676
}
7777
}
7878
}
79-
currentBlock = currentBlock.nextBlock
8079
}
8180

8281
return customJavaDocComment

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

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,16 @@ sealed class CustomJavaDocTag(
4545
object ThrowsException :
4646
CustomJavaDocTag("utbot.throwsException", "Throws exception", CustomJavaDocComment::throwsException)
4747

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-
}
48+
fun generateDocStatement(comment: CustomJavaDocComment): DocRegularStmt? =
49+
when (val value = valueRetriever.invoke(comment)) {
50+
is String -> value.takeIf { it.isNotEmpty() }?.let {
51+
DocRegularStmt("@$name $value\n")
52+
}
53+
is List<*> -> value.takeIf { it.isNotEmpty() }?.let {
54+
val valueToString = value.joinToString(separator = ",\n", postfix = "\n")
55+
56+
DocRegularStmt("@$name $valueToString")
57+
}
58+
else -> null
59+
}
5660
}

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

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,21 @@ open class SimpleCommentBuilder(
4242
*/
4343
open fun buildString(currentMethod: SootMethod): String {
4444
val root = SimpleSentenceBlock(stringTemplates = stringTemplates)
45+
buildThrownExceptionInfo(root, currentMethod)
46+
skippedIterations()
47+
buildSentenceBlock(traceTag.rootStatementTag, root, currentMethod)
48+
var sentence = toSentence(root)
49+
if (sentence.isEmpty()) return genWarnNotification()
50+
sentence = splitLongSentence(sentence)
51+
sentence = lastCommaToDot(sentence)
4552

53+
return "<pre>\n$sentence</pre>".replace(CARRIAGE_RETURN, "")
54+
}
55+
56+
private fun buildThrownExceptionInfo(
57+
root: SimpleSentenceBlock,
58+
currentMethod: SootMethod
59+
) {
4660
val thrownException = traceTag.result.exceptionOrNull()
4761
if (thrownException == null) {
4862
root.exceptionThrow = traceTag.result.exceptionOrNull()?.let { it::class.qualifiedName }
@@ -51,14 +65,6 @@ open class SimpleCommentBuilder(
5165
val reason = findExceptionReason(currentMethod, thrownException)
5266
root.exceptionThrow = "$exceptionName $reason"
5367
}
54-
skippedIterations()
55-
buildSentenceBlock(traceTag.rootStatementTag, root, currentMethod)
56-
var sentence = toSentence(root)
57-
if (sentence.isEmpty()) return genWarnNotification()
58-
sentence = splitLongSentence(sentence)
59-
sentence = lastCommaToDot(sentence)
60-
61-
return "<pre>\n$sentence</pre>".replace(CARRIAGE_RETURN, "")
6268
}
6369

6470
/**
@@ -79,15 +85,7 @@ open class SimpleCommentBuilder(
7985

8086
private fun buildSentenceBlock(currentMethod: SootMethod): SimpleSentenceBlock {
8187
val rootSentenceBlock = SimpleSentenceBlock(stringTemplates = stringTemplates)
82-
83-
val thrownException = traceTag.result.exceptionOrNull()
84-
if (thrownException == null) {
85-
rootSentenceBlock.exceptionThrow = traceTag.result.exceptionOrNull()?.let { it::class.qualifiedName }
86-
} else {
87-
val exceptionName = thrownException.javaClass.simpleName
88-
val reason = findExceptionReason(currentMethod, thrownException)
89-
rootSentenceBlock.exceptionThrow = "$exceptionName $reason"
90-
}
88+
buildThrownExceptionInfo(rootSentenceBlock, currentMethod)
9189
skippedIterations()
9290
buildSentenceBlock(traceTag.rootStatementTag, rootSentenceBlock, currentMethod)
9391
return rootSentenceBlock

0 commit comments

Comments
 (0)