Skip to content

Commit 58609af

Browse files
authored
Added an initial solution with comments for Fuzzing in old good plain mode (#1202)
* Added an initial solution * Added an initial solution * Renaming and refactoring * Fix imports and constants
1 parent 6ce8761 commit 58609af

15 files changed

+99
-57
lines changed

utbot-summary/src/main/kotlin/org/utbot/summary/AbstractTextBuilder.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ import com.github.javaparser.ast.stmt.SwitchStmt
1111
import com.github.javaparser.ast.stmt.WhileStmt
1212
import org.utbot.framework.plugin.api.Step
1313
import org.utbot.summary.ast.JimpleToASTMap
14-
import org.utbot.summary.comment.IterationDescription
15-
import org.utbot.summary.comment.SimpleSentenceBlock
16-
import org.utbot.summary.comment.StmtDescription
17-
import org.utbot.summary.comment.StmtType
14+
import org.utbot.summary.comment.classic.symbolic.IterationDescription
15+
import org.utbot.summary.comment.classic.symbolic.SimpleSentenceBlock
16+
import org.utbot.summary.comment.classic.symbolic.StmtDescription
17+
import org.utbot.summary.comment.classic.symbolic.StmtType
1818
import org.utbot.summary.comment.getTextIterationDescription
1919
import org.utbot.summary.comment.getTextTypeIterationDescription
2020
import org.utbot.summary.comment.numberWithSuffix

utbot-summary/src/main/kotlin/org/utbot/summary/Summarization.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import org.utbot.summary.analysis.ExecutionStructureAnalysis
1717
import org.utbot.summary.ast.JimpleToASTMap
1818
import org.utbot.summary.ast.SourceCodeParser
1919
import org.utbot.summary.comment.cluster.SymbolicExecutionClusterCommentBuilder
20-
import org.utbot.summary.comment.SimpleCommentBuilder
20+
import org.utbot.summary.comment.classic.symbolic.SimpleCommentBuilder
2121
import org.utbot.summary.name.SimpleNameBuilder
2222
import java.io.File
2323
import java.nio.file.Path

utbot-summary/src/main/kotlin/org/utbot/summary/UtSummarySettings.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,7 @@ object SummarySentenceConstants {
6464
const val AT_CODE = "@code"
6565
const val OPEN_BRACKET = "{"
6666
const val CLOSE_BRACKET = "}"
67+
68+
const val JAVA_CLASS_DELIMITER = "$"
69+
const val JAVA_DOC_CLASS_DELIMITER = "."
6770
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ import org.utbot.summary.SummarySentenceConstants.COMMA_SYMBOL
1414
import org.utbot.summary.SummarySentenceConstants.DOT_SYMBOL
1515
import org.utbot.summary.SummarySentenceConstants.NEW_LINE
1616
import org.utbot.summary.SummarySentenceConstants.TAB
17+
import org.utbot.summary.comment.classic.symbolic.SquashedStmtTexts
18+
import org.utbot.summary.comment.classic.symbolic.StmtType
1719

1820
fun numberWithSuffix(number: Int) = when (number % 10) {
1921
1 -> "${number}st"
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,45 @@
11
package org.utbot.summary.comment.classic.fuzzer
22

33
import org.utbot.framework.plugin.api.DocPreTagStatement
4+
import org.utbot.framework.plugin.api.DocRegularStmt
45
import org.utbot.framework.plugin.api.DocStatement
56
import org.utbot.framework.plugin.api.UtExecutionResult
67
import org.utbot.fuzzer.FuzzedMethodDescription
78
import org.utbot.fuzzer.FuzzedValue
9+
import org.utbot.summary.SummarySentenceConstants.NEW_LINE
10+
import org.utbot.summary.comment.customtags.getClassReference
11+
import org.utbot.summary.comment.customtags.getMethodReferenceForFuzzingTest
812

9-
// TODO: https://github.com/UnitTestBot/UTBotJava/issues/1127
1013
class SimpleCommentForTestProducedByFuzzerBuilder(
11-
description: FuzzedMethodDescription,
12-
values: List<FuzzedValue>,
13-
result: UtExecutionResult?
14+
val methodDescription: FuzzedMethodDescription,
15+
val values: List<FuzzedValue>,
16+
val result: UtExecutionResult?
1417
) {
1518
fun buildDocStatements(): List<DocStatement> {
16-
return listOf<DocStatement>(DocPreTagStatement(emptyList()))
19+
val packageName = methodDescription.packageName
20+
val className = methodDescription.className
21+
val methodName = methodDescription.compilableName
22+
23+
val result = if (packageName != null && className != null && methodName != null) {
24+
val fullClassName = "$packageName.$className"
25+
26+
val methodReference = getMethodReferenceForFuzzingTest(
27+
fullClassName,
28+
methodName,
29+
methodDescription.parameters,
30+
false
31+
)
32+
33+
val classReference = getClassReference(fullClassName)
34+
35+
val docStatements = mutableListOf<DocStatement>()
36+
docStatements.add(DocRegularStmt("Class under test: $classReference$NEW_LINE"))
37+
docStatements.add(DocRegularStmt("Method under test: $methodReference$NEW_LINE"))
38+
docStatements
39+
} else {
40+
emptyList()
41+
}
42+
43+
return listOf<DocStatement>(DocPreTagStatement(result))
1744
}
1845
}

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package org.utbot.summary.comment
1+
package org.utbot.summary.comment.classic.symbolic
22

33
import com.github.javaparser.ast.body.MethodDeclaration
44
import com.github.javaparser.ast.stmt.CatchClause
@@ -16,7 +16,8 @@ import org.utbot.framework.plugin.api.exceptionOrNull
1616
import org.utbot.summary.AbstractTextBuilder
1717
import org.utbot.summary.SummarySentenceConstants.CARRIAGE_RETURN
1818
import org.utbot.summary.ast.JimpleToASTMap
19-
import org.utbot.summary.comment.customtags.getMethodReference
19+
import org.utbot.summary.comment.*
20+
import org.utbot.summary.comment.customtags.getMethodReferenceForSymbolicTest
2021
import org.utbot.summary.tag.BasicTypeTag
2122
import org.utbot.summary.tag.CallOrderTag
2223
import org.utbot.summary.tag.StatementTag
@@ -196,7 +197,7 @@ open class SimpleCommentBuilder(
196197
if (!sentenceInvoke.isEmpty()) {
197198
sentenceBlock.invokeSentenceBlock =
198199
Pair(
199-
getMethodReference(className, methodName, methodParameterTypes, invokeSootMethod.isPrivate),
200+
getMethodReferenceForSymbolicTest(className, methodName, methodParameterTypes, invokeSootMethod.isPrivate),
200201
sentenceInvoke
201202
)
202203
createNextBlock = true
@@ -333,7 +334,7 @@ open class SimpleCommentBuilder(
333334
sentenceBlock.stmtTexts.add(
334335
StmtDescription(
335336
StmtType.Invoke,
336-
getMethodReference(className, methodName, methodParameterTypes, isPrivate),
337+
getMethodReferenceForSymbolicTest(className, methodName, methodParameterTypes, isPrivate),
337338
frequency
338339
)
339340
)

utbot-summary/src/main/kotlin/org/utbot/summary/comment/classic/symbolic/SimpleSentenceBlock.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package org.utbot.summary.comment
1+
package org.utbot.summary.comment.classic.symbolic
22

33
import org.utbot.framework.plugin.api.DocCodeStmt
44
import org.utbot.framework.plugin.api.DocRegularStmt
@@ -12,6 +12,7 @@ import org.utbot.summary.SummarySentenceConstants.NEW_LINE
1212
import org.utbot.summary.SummarySentenceConstants.OPEN_BRACKET
1313
import org.utbot.summary.SummarySentenceConstants.SENTENCE_SEPARATION
1414
import org.utbot.summary.SummarySentenceConstants.TAB
15+
import org.utbot.summary.comment.*
1516

1617
class SimpleSentenceBlock(val stringTemplates: StringsTemplatesInterface) {
1718
val iterationSentenceBlocks = mutableListOf<Pair<String, List<SimpleSentenceBlock>>>()

utbot-summary/src/main/kotlin/org/utbot/summary/comment/cluster/SymbolicExecutionClusterCommentBuilder.kt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ import org.utbot.framework.plugin.api.DocStatement
77
import org.utbot.summary.SummarySentenceConstants.CARRIAGE_RETURN
88
import org.utbot.summary.ast.JimpleToASTMap
99
import org.utbot.summary.comment.*
10-
import org.utbot.summary.comment.customtags.getMethodReference
10+
import org.utbot.summary.comment.classic.symbolic.*
11+
import org.utbot.summary.comment.customtags.getMethodReferenceForSymbolicTest
1112
import org.utbot.summary.tag.BasicTypeTag
1213
import org.utbot.summary.tag.CallOrderTag
1314
import org.utbot.summary.tag.StatementTag
@@ -42,7 +43,7 @@ class SymbolicExecutionClusterCommentBuilder(
4243
sentence = splitLongSentence(sentence)
4344
sentence = lastCommaToDot(sentence)
4445

45-
return "<pre>\n$sentence</pre>".replace(CARRIAGE_RETURN, "")
46+
return "<pre>\n$sentence</pre>".replace(CARRIAGE_RETURN, EMPTY_STRING)
4647
}
4748

4849
override fun buildDocStmts(currentMethod: SootMethod): List<DocStatement> {
@@ -98,7 +99,7 @@ class SymbolicExecutionClusterCommentBuilder(
9899
if (!sentenceInvoke.isEmpty()) {
99100
sentenceBlock.invokeSentenceBlock =
100101
Pair(
101-
getMethodReference(className, methodName, methodParameterTypes, isPrivate),
102+
getMethodReferenceForSymbolicTest(className, methodName, methodParameterTypes, isPrivate),
102103
sentenceInvoke
103104
)
104105
createNextBlock = true
Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
package org.utbot.summary.comment.customtags
22

33
import org.utbot.framework.plugin.api.ClassId
4+
import org.utbot.summary.SummarySentenceConstants.CARRIAGE_RETURN
5+
import org.utbot.summary.SummarySentenceConstants.JAVA_CLASS_DELIMITER
6+
import org.utbot.summary.SummarySentenceConstants.JAVA_DOC_CLASS_DELIMITER
7+
import org.utbot.summary.comment.classic.symbolic.EMPTY_STRING
48
import soot.Type
59

610
/**
@@ -12,34 +16,15 @@ import soot.Type
1216
* In case when an enclosing class in nested, we need to replace '$' with '.'
1317
* to render the reference.
1418
*/
15-
fun getMethodReference(
19+
fun getMethodReferenceForSymbolicTest(
1620
className: String,
1721
methodName: String,
1822
methodParameterTypes: List<Type>,
1923
isPrivate: Boolean
2024
): String {
21-
val prettyClassName: String = className.replace("$", ".")
25+
val methodParametersAsString = if (methodParameterTypes.isNotEmpty()) methodParameterTypes.joinToString(",") else EMPTY_STRING
2226

23-
val text = if (methodParameterTypes.isEmpty()) {
24-
"$prettyClassName#$methodName()"
25-
} else {
26-
val methodParametersAsString = methodParameterTypes.joinToString(",")
27-
"$prettyClassName#$methodName($methodParametersAsString)"
28-
}
29-
30-
return if (isPrivate) {
31-
text
32-
} else {
33-
"{@link $text}"
34-
}
35-
}
36-
37-
/**
38-
* Returns a reference to the class.
39-
* Replaces '$' with '.' in case a class is nested.
40-
*/
41-
fun getClassReference(fullClassName: String): String {
42-
return "{@link ${fullClassName.replace("$", ".")}}"
27+
return formMethodReferenceForJavaDoc(className, methodName, methodParametersAsString, isPrivate)
4328
}
4429

4530
/**
@@ -51,12 +36,24 @@ fun getClassReference(fullClassName: String): String {
5136
* to render the reference.
5237
*/
5338
fun getMethodReferenceForFuzzingTest(className: String, methodName: String, methodParameterTypes: List<ClassId>, isPrivate: Boolean): String {
54-
val prettyClassName: String = className.replace("$", ".")
39+
val methodParametersAsString = if (methodParameterTypes.isNotEmpty()) methodParameterTypes.joinToString(",") { it.canonicalName } else EMPTY_STRING
40+
41+
return formMethodReferenceForJavaDoc(className, methodName, methodParametersAsString, isPrivate).replace(
42+
CARRIAGE_RETURN, EMPTY_STRING
43+
)
44+
}
45+
46+
private fun formMethodReferenceForJavaDoc(
47+
className: String,
48+
methodName: String,
49+
methodParametersAsString: String,
50+
isPrivate: Boolean
51+
): String {
52+
val prettyClassName: String = className.replace(JAVA_CLASS_DELIMITER, JAVA_DOC_CLASS_DELIMITER)
5553

56-
val text = if (methodParameterTypes.isEmpty()) {
54+
val text = if (methodParametersAsString == EMPTY_STRING) {
5755
"$prettyClassName#$methodName()"
5856
} else {
59-
val methodParametersAsString = methodParameterTypes.joinToString(",") { it.canonicalName }
6057
"$prettyClassName#$methodName($methodParametersAsString)"
6158
}
6259

@@ -65,4 +62,12 @@ fun getMethodReferenceForFuzzingTest(className: String, methodName: String, meth
6562
} else {
6663
"{@link $text}"
6764
}
65+
}
66+
67+
/**
68+
* Returns a reference to the class.
69+
* Replaces '$' with '.' in case a class is nested.
70+
*/
71+
fun getClassReference(fullClassName: String): String {
72+
return "{@link ${fullClassName.replace(JAVA_CLASS_DELIMITER, JAVA_DOC_CLASS_DELIMITER)}}".replace(CARRIAGE_RETURN, EMPTY_STRING)
6873
}

utbot-summary/src/main/kotlin/org/utbot/summary/comment/customtags/fuzzer/CommentWithCustomTagForTestProducedByFuzzer.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package org.utbot.summary.comment.customtags.fuzzer
22

3-
import org.utbot.summary.comment.EMPTY_STRING
3+
import org.utbot.summary.comment.classic.symbolic.EMPTY_STRING
44

55
/**
66
* Represents a set of plugin's custom JavaDoc tags.

utbot-summary/src/main/kotlin/org/utbot/summary/comment/customtags/fuzzer/CommentWithCustomTagForTestProducedByFuzzerBuilder.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@ class CommentWithCustomTagForTestProducedByFuzzerBuilder(
4242
methodName,
4343
methodDescription.parameters,
4444
false
45-
).replace(CARRIAGE_RETURN, "")
45+
)
4646

47-
val classReference = getClassReference(fullClassName).replace(CARRIAGE_RETURN, "")
47+
val classReference = getClassReference(fullClassName)
4848

4949
CommentWithCustomTagForTestProducedByFuzzer(
5050
classUnderTest = classReference,

utbot-summary/src/main/kotlin/org/utbot/summary/comment/customtags/symbolic/CustomJavaDocComment.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package org.utbot.summary.comment.customtags.symbolic
22

3-
import org.utbot.summary.comment.EMPTY_STRING
3+
import org.utbot.summary.comment.classic.symbolic.EMPTY_STRING
44

55
/**
66
* Represents a set of plugin's custom JavaDoc tags.

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@ import org.utbot.framework.plugin.api.exceptionOrNull
66
import org.utbot.summary.SummarySentenceConstants.CARRIAGE_RETURN
77
import org.utbot.summary.ast.JimpleToASTMap
88
import org.utbot.summary.comment.*
9+
import org.utbot.summary.comment.classic.symbolic.*
910
import org.utbot.summary.comment.customtags.getClassReference
10-
import org.utbot.summary.comment.customtags.getMethodReference
11+
import org.utbot.summary.comment.customtags.getMethodReferenceForSymbolicTest
1112
import org.utbot.summary.tag.TraceTagWithoutExecution
1213
import soot.SootMethod
1314

@@ -30,7 +31,7 @@ class CustomJavaDocCommentBuilder(
3031
}
3132

3233
private fun buildCustomJavaDocComment(currentMethod: SootMethod): CustomJavaDocComment {
33-
val methodReference = getMethodReference(
34+
val methodReference = getMethodReferenceForSymbolicTest(
3435
currentMethod.declaringClass.name,
3536
currentMethod.name,
3637
currentMethod.parameterTypes,
@@ -39,8 +40,8 @@ class CustomJavaDocCommentBuilder(
3940
val classReference = getClassReference(currentMethod.declaringClass.javaStyleName)
4041

4142
val comment = CustomJavaDocComment(
42-
classUnderTest = classReference.replace(CARRIAGE_RETURN, ""),
43-
methodUnderTest = methodReference.replace(CARRIAGE_RETURN, ""),
43+
classUnderTest = classReference,
44+
methodUnderTest = methodReference,
4445
)
4546

4647
val rootSentenceBlock = SimpleSentenceBlock(stringTemplates = stringTemplates)

utbot-summary/src/main/kotlin/org/utbot/summary/fuzzer/names/ModelBasedNameSuggester.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import org.utbot.framework.plugin.api.*
55
import org.utbot.framework.plugin.api.util.voidClassId
66
import org.utbot.fuzzer.FuzzedMethodDescription
77
import org.utbot.fuzzer.FuzzedValue
8+
import org.utbot.summary.UtSummarySettings
89
import org.utbot.summary.comment.classic.fuzzer.SimpleCommentForTestProducedByFuzzerBuilder
910
import org.utbot.summary.comment.customtags.fuzzer.CommentWithCustomTagForTestProducedByFuzzerBuilder
1011
import java.util.*
@@ -32,9 +33,9 @@ class ModelBasedNameSuggester(
3233

3334
return sequenceOf(
3435
TestSuggestedInfo(
35-
testName = createTestName(description, values, result),
36+
testName = createTestName(description, values, result),
3637
displayName = createDisplayName(description, values, result),
37-
javaDoc = createJavaDoc(description, values, result)
38+
javaDoc = if (UtSummarySettings.GENERATE_COMMENTS) createJavaDoc(description, values, result) else null
3839
)
3940
)
4041
}

utbot-summary/src/test/kotlin/org/utbot/summary/comment/SimpleCommentBuilderTest.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ import org.mockito.Mockito.`when`
99
import org.utbot.framework.plugin.api.Step
1010
import org.utbot.framework.plugin.api.UtOverflowFailure
1111
import org.utbot.summary.ast.JimpleToASTMap
12-
import org.utbot.summary.comment.customtags.getMethodReference
12+
import org.utbot.summary.comment.classic.symbolic.SimpleCommentBuilder
13+
import org.utbot.summary.comment.customtags.getMethodReferenceForSymbolicTest
1314
import org.utbot.summary.tag.StatementTag
1415
import org.utbot.summary.tag.TraceTag
1516
import soot.SootMethod
@@ -67,17 +68,16 @@ class SimpleCommentBuilderTest {
6768

6869
@Test
6970
fun `builds inline link for method`() {
70-
val methodReference = getMethodReference("org.utbot.ClassName", "methodName", listOf(), false)
71+
val methodReference = getMethodReferenceForSymbolicTest("org.utbot.ClassName", "methodName", listOf(), false)
7172
val expectedMethodReference = "{@link org.utbot.ClassName#methodName()}"
7273
assertEquals(methodReference, expectedMethodReference)
7374
}
7475

7576
@Test
7677
fun `builds inline link for method in nested class`() {
7778
val methodReference =
78-
getMethodReference("org.utbot.ClassName\$NestedClassName", "methodName", listOf(), false)
79+
getMethodReferenceForSymbolicTest("org.utbot.ClassName\$NestedClassName", "methodName", listOf(), false)
7980
val expectedMethodReference = "{@link org.utbot.ClassName.NestedClassName#methodName()}"
8081
assertEquals(methodReference, expectedMethodReference)
8182
}
82-
8383
}

0 commit comments

Comments
 (0)