Skip to content

Commit 1cdc000

Browse files
Reduce summarization API to one method (#1732)
1 parent 3c8dc61 commit 1cdc000

File tree

7 files changed

+25
-24
lines changed

7 files changed

+25
-24
lines changed

utbot-cli/src/main/kotlin/org/utbot/cli/GenerateTestsAbstractCommand.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ abstract class GenerateTestsAbstractCommand(name: String, help: String) :
162162
chosenClassesToMockAlways,
163163
generationTimeout
164164
).map {
165-
if (sourceCodeFile != null) it.summarize(sourceCodeFile.toFile(), searchDirectory) else it
165+
if (sourceCodeFile != null) it.summarize(searchDirectory, sourceCodeFile.toFile()) else it
166166
}
167167

168168

utbot-framework/src/main/kotlin/org/utbot/framework/plugin/sarif/GenerateTestsAndSarifReportFacade.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ class GenerateTestsAndSarifReportFacade(
6060
sarifProperties.classesToMockAlways,
6161
sarifProperties.generationTimeout
6262
).map {
63-
it.summarize(targetClass.sourceCodeFile, workingDirectory)
63+
it.summarize(workingDirectory, targetClass.sourceCodeFile)
6464
}
6565

6666
private fun generateTestCode(targetClass: TargetClassWrapper, testSets: List<UtMethodTestSet>): String =

utbot-framework/src/main/kotlin/org/utbot/framework/process/EngineProcessMain.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ private fun EngineProcessModel.setup(kryoHelper: KryoHelper, watchdog: IdleWatch
113113
fuzzingValue = params.fuzzingValue
114114
})
115115
.apply { logger.info("generation ended, starting summarization, result size: ${this.size}") }
116-
.map { it.summarize(Paths.get(params.searchDirectory)) }
116+
.map { it.summarize(Paths.get(params.searchDirectory), sourceFile = null) }
117117
.apply { logger.info("summarization ended") }
118118
.filterNot { it.executions.isEmpty() && it.errors.isEmpty() }
119119

utbot-junit-contest/src/main/kotlin/org/utbot/contest/Contest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,7 @@ fun runGeneration(
403403

404404
val testSets = testsByMethod.map { (method, executions) ->
405405
UtMethodTestSet(method, minimizeExecutions(executions), jimpleBody(method))
406-
.summarize(cut.classfileDir.toPath())
406+
.summarize(cut.classfileDir.toPath(), sourceFile = null)
407407
}
408408

409409
logger.info().bracket("Flushing tests for [${cut.simpleName}] on disk") {

utbot-summary-tests/src/test/kotlin/examples/SummaryTestCaseGeneratorTest.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@ package examples
33
import org.junit.jupiter.api.*
44
import org.utbot.common.WorkaroundReason
55
import org.utbot.common.workaround
6+
import org.utbot.framework.SummariesGenerationType
67
import org.utbot.framework.UtSettings.checkNpeInNestedMethods
78
import org.utbot.framework.UtSettings.checkNpeInNestedNotPrivateMethods
89
import org.utbot.framework.UtSettings.checkSolverTimeoutMillis
10+
import org.utbot.framework.UtSettings.summaryGenerationType
911
import org.utbot.framework.plugin.api.*
1012
import org.utbot.framework.plugin.api.util.UtContext
1113
import org.utbot.framework.plugin.api.util.executableId
@@ -58,7 +60,7 @@ open class SummaryTestCaseGeneratorTest(
5860
checkNpeInNestedNotPrivateMethods = true
5961
}
6062
val testSet = executionsModel(method.executableId, mockStrategy)
61-
val testSetWithSummarization = testSet.summarize(searchDirectory)
63+
val testSetWithSummarization = testSet.summarize(searchDirectory, sourceFile = null)
6264

6365
testSetWithSummarization.executions.checkMatchersWithTextSummary(summaryKeys)
6466
testSetWithSummarization.executions.checkMatchersWithMethodNames(methodNames)

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

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,8 @@ import org.utbot.summary.comment.classic.symbolic.SimpleCommentBuilder
1515
import org.utbot.summary.name.SimpleNameBuilder
1616
import java.io.File
1717
import java.nio.file.Path
18-
import java.nio.file.Paths
1918
import mu.KotlinLogging
20-
import org.utbot.framework.SummariesGenerationType
19+
import org.utbot.framework.SummariesGenerationType.*
2120
import org.utbot.framework.UtSettings.enableClusterCommentsGeneration
2221
import org.utbot.framework.UtSettings.enableJavaDocGeneration
2322
import org.utbot.framework.UtSettings.useDisplayNameArrowStyle
@@ -37,21 +36,28 @@ import soot.SootMethod
3736

3837
private val logger = KotlinLogging.logger {}
3938

40-
fun UtMethodTestSet.summarize(sourceFile: File?, searchDirectory: Path = Paths.get("")): UtMethodTestSet {
41-
if (summaryGenerationType == SummariesGenerationType.NONE) return this
39+
fun UtMethodTestSet.summarize(searchDirectory: Path, sourceFile: File?): UtMethodTestSet {
40+
if (summaryGenerationType == NONE) return this
41+
42+
val sourceFileToAnalyze = sourceFile
43+
?: when (summaryGenerationType) {
44+
FULL -> Instrumenter.adapter.computeSourceFileByClass(this.method.classId.jClass, searchDirectory)
45+
LIGHT,
46+
NONE -> null
47+
}
4248

4349
return try {
4450
makeDiverseExecutions(this)
4551

4652
// HACK: we avoid calling [invokeDescriptions] method to save time, it is useless in Contest
4753
val invokeDescriptions = when (summaryGenerationType) {
48-
SummariesGenerationType.FULL -> invokeDescriptions(this, searchDirectory)
49-
SummariesGenerationType.LIGHT,
50-
SummariesGenerationType.NONE -> emptyList()
54+
FULL -> invokeDescriptions(this, searchDirectory)
55+
LIGHT,
56+
NONE -> emptyList()
5157
}
5258

5359
// every cluster has summary and list of executions
54-
val executionClusters = Summarization(sourceFile, invokeDescriptions).fillSummaries(this)
60+
val executionClusters = Summarization(sourceFileToAnalyze, invokeDescriptions).fillSummaries(this)
5561
val updatedExecutions = executionClusters.flatMap { it.executions }
5662
var pos = 0
5763
val clustersInfo = executionClusters.map {
@@ -70,13 +76,6 @@ fun UtMethodTestSet.summarize(sourceFile: File?, searchDirectory: Path = Paths.g
7076
}
7177
}
7278

73-
fun UtMethodTestSet.summarize(searchDirectory: Path): UtMethodTestSet =
74-
this.summarize(
75-
Instrumenter.adapter.computeSourceFileByClass(this.method.classId.jClass, searchDirectory),
76-
searchDirectory
77-
)
78-
79-
8079
class Summarization(val sourceFile: File?, val invokeDescriptions: List<InvokeDescription>) {
8180
private val tagGenerator = TagGenerator()
8281
private val jimpleBodyAnalysis = ExecutionStructureAnalysis()
@@ -93,16 +92,16 @@ class Summarization(val sourceFile: File?, val invokeDescriptions: List<InvokeDe
9392
val executionClusters = mutableListOf<UtExecutionCluster>()
9493

9594
when (summaryGenerationType) {
96-
SummariesGenerationType.FULL -> {
95+
FULL -> {
9796
executionClusters += generateSummariesForTestsWithNonEmptyPathsProducedBySymbolicExecutor(testSet)
9897
executionClusters += generateFuzzerBasedSummariesForTests(testSet)
9998
executionClusters += generateSummariesForTestsWithEmptyPathsProducedBySymbolicExecutor(testSet)
10099
}
101-
SummariesGenerationType.LIGHT -> {
100+
LIGHT -> {
102101
executionClusters += generateFuzzerBasedSummariesForTests(testSet, MethodDescriptionSource.SYMBOLIC)
103102
executionClusters += generateFuzzerBasedSummariesForTests(testSet)
104103
}
105-
SummariesGenerationType.NONE -> error("We must not fill summaries if SummariesGenerationType is NONE")
104+
NONE -> error("We must not fill summaries if SummariesGenerationType is NONE")
106105
}
107106

108107
return if (enableClusterCommentsGeneration && executionClusters.size > 0)

utbot-testing/src/main/kotlin/org/utbot/testing/UtValueTestCaseChecker.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2452,7 +2452,7 @@ abstract class UtValueTestCaseChecker(
24522452
} else {
24532453
walk(executableId, mockStrategy, additionalDependenciesClassPath)
24542454
}
2455-
testSet.summarize(searchDirectory)
2455+
testSet.summarize(searchDirectory, sourceFile = null)
24562456
val valueTestCase = testSet.toValueTestCase()
24572457

24582458
assertTrue(testSet.errors.isEmpty()) {

0 commit comments

Comments
 (0)