Skip to content

Commit e3d745b

Browse files
committed
Refactor everything related to codegen
1 parent f47e064 commit e3d745b

File tree

164 files changed

+787
-1082
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

164 files changed

+787
-1082
lines changed

utbot-analytics/src/test/kotlin/org/utbot/features/FeatureProcessorWithRepetitionTest.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ import org.junit.jupiter.api.Assertions
55
import org.junit.jupiter.api.BeforeAll
66
import org.junit.jupiter.api.Test
77
import org.utbot.analytics.EngineAnalyticsContext
8-
import org.utbot.examples.AbstractTestCaseGeneratorTest
8+
import org.utbot.examples.UtTestCaseChecker
99
import org.utbot.examples.eq
1010
import org.utbot.examples.withFeaturePath
1111
import java.io.File
1212
import java.io.FileInputStream
1313

14-
class FeatureProcessorWithRepetitionTest : AbstractTestCaseGeneratorTest(OnePath::class, false) {
14+
class FeatureProcessorWithRepetitionTest : UtTestCaseChecker(OnePath::class, false) {
1515
companion object {
1616
const val featureDir = "src/test/resources/features"
1717
fun reward(coverage: Double, time: Double) = RewardEstimator.reward(coverage, time)

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

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import com.github.ajalt.clikt.parameters.options.option
88
import com.github.ajalt.clikt.parameters.options.unique
99
import com.github.ajalt.clikt.parameters.types.choice
1010
import com.github.ajalt.clikt.parameters.types.long
11+
import mu.KotlinLogging
1112
import org.utbot.common.PathUtil.classFqnToPath
1213
import org.utbot.common.PathUtil.replaceSeparator
1314
import org.utbot.common.PathUtil.toPath
@@ -19,11 +20,10 @@ import org.utbot.framework.codegen.ForceStaticMocking
1920
import org.utbot.framework.codegen.MockitoStaticMocking
2021
import org.utbot.framework.codegen.NoStaticMocking
2122
import org.utbot.framework.codegen.StaticsMocking
22-
import org.utbot.framework.codegen.model.ModelBasedTestCodeGenerator
23+
import org.utbot.framework.codegen.model.CodeGenerator
2324
import org.utbot.framework.codegen.testFrameworkByName
2425
import org.utbot.framework.plugin.api.ClassId
2526
import org.utbot.framework.plugin.api.CodegenLanguage
26-
import org.utbot.framework.plugin.api.MockFramework
2727
import org.utbot.framework.plugin.api.MockStrategyApi
2828
import org.utbot.framework.plugin.api.TreatOverflowAsError
2929
import org.utbot.framework.plugin.api.UtBotTestCaseGenerator
@@ -41,7 +41,6 @@ import java.time.temporal.ChronoUnit
4141
import kotlin.reflect.KCallable
4242
import kotlin.reflect.KClass
4343
import kotlin.reflect.jvm.kotlinFunction
44-
import mu.KotlinLogging
4544

4645
private const val LONG_GENERATION_TIMEOUT = 1_200_000L
4746

@@ -160,7 +159,7 @@ abstract class GenerateTestsAbstractCommand(name: String, help: String) :
160159
searchDirectory: Path,
161160
chosenClassesToMockAlways: Set<ClassId>
162161
): List<UtTestCase> =
163-
UtBotTestCaseGenerator.generateForSeveralMethods(
162+
UtBotTestCaseGenerator.generateTestCases(
164163
targetMethods,
165164
mockStrategy,
166165
chosenClassesToMockAlways,
@@ -199,21 +198,20 @@ abstract class GenerateTestsAbstractCommand(name: String, help: String) :
199198
// Set UtSettings parameters.
200199
UtSettings.treatOverflowAsError = treatOverflowAsError == TreatOverflowAsError.AS_ERROR
201200

202-
UtBotTestCaseGenerator.init(workingDirectory, classPathNormalized, System.getProperty("java.class.path")) { false }
201+
UtBotTestCaseGenerator.init(workingDirectory, classPathNormalized, System.getProperty("java.class.path"))
203202
}
204203

205-
private fun initializeCodeGenerator(testFramework: String, classUnderTest: KClass<*>): ModelBasedTestCodeGenerator {
204+
private fun initializeCodeGenerator(testFramework: String, classUnderTest: KClass<*>): CodeGenerator {
206205
val generateWarningsForStaticMocking =
207206
forceStaticMocking == ForceStaticMocking.FORCE && staticsMocking is NoStaticMocking
208-
return ModelBasedTestCodeGenerator().apply {
207+
return CodeGenerator().apply {
209208
init(
210209
testFramework = testFrameworkByName(testFramework),
211210
classUnderTest = classUnderTest.java,
212-
mockFramework = MockFramework.MOCKITO, // TODO: rewrite default mock framework system
213211
codegenLanguage = codegenLanguage,
214212
staticsMocking = staticsMocking,
215213
forceStaticMocking = forceStaticMocking,
216-
generateWarningsForStaticMocking = generateWarningsForStaticMocking
214+
generateWarningsForStaticMocking = generateWarningsForStaticMocking,
217215
)
218216
}
219217
}

utbot-framework-api/src/main/kotlin/org/utbot/framework/plugin/api/Api.kt

Lines changed: 9 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,6 @@ import org.utbot.framework.plugin.api.util.primitiveTypeJvmNameOrNull
3333
import org.utbot.framework.plugin.api.util.shortClassId
3434
import org.utbot.framework.plugin.api.util.toReferenceTypeBytecodeSignature
3535
import org.utbot.framework.plugin.api.util.voidClassId
36-
import java.io.File
37-
import java.lang.reflect.Modifier
38-
import java.nio.file.Path
39-
import kotlin.jvm.internal.CallableReference
40-
import kotlin.reflect.KCallable
41-
import kotlin.reflect.KClass
42-
import kotlin.reflect.KFunction
43-
import kotlin.reflect.full.instanceParameter
44-
import kotlin.reflect.jvm.javaConstructor
45-
import kotlin.reflect.jvm.javaType
4636
import soot.ArrayType
4737
import soot.BooleanType
4838
import soot.ByteType
@@ -58,6 +48,15 @@ import soot.Type
5848
import soot.VoidType
5949
import soot.jimple.JimpleBody
6050
import soot.jimple.Stmt
51+
import java.io.File
52+
import java.lang.reflect.Modifier
53+
import kotlin.jvm.internal.CallableReference
54+
import kotlin.reflect.KCallable
55+
import kotlin.reflect.KClass
56+
import kotlin.reflect.KFunction
57+
import kotlin.reflect.full.instanceParameter
58+
import kotlin.reflect.jvm.javaConstructor
59+
import kotlin.reflect.jvm.javaType
6160

6261
data class UtMethod<R>(
6362
val callable: KCallable<R>,
@@ -1017,17 +1016,6 @@ open class TypeParameters(val parameters: List<ClassId> = emptyList())
10171016

10181017
class WildcardTypeParameter: TypeParameters(emptyList())
10191018

1020-
interface TestCaseGenerator {
1021-
fun init(
1022-
buildDir: Path,
1023-
classpath: String? = null,
1024-
dependencyPaths: String,
1025-
isCanceled: () -> Boolean = { false }
1026-
)
1027-
1028-
fun generate(method: UtMethod<*>, mockStrategy: MockStrategyApi): UtTestCase
1029-
}
1030-
10311019
interface CodeGenerationSettingItem {
10321020
val displayName: String
10331021
val description: String
@@ -1186,13 +1174,6 @@ fun isolateCommandLineArgumentsToArgumentFile(arguments: List<String>): String {
11861174
return argumentFile.absolutePath.let { "@$it" }
11871175
}
11881176

1189-
interface UtService<T> {
1190-
val displayName: String
1191-
val serviceProvider: T
1192-
}
1193-
1194-
interface TestGeneratorService : UtService<TestCaseGenerator>
1195-
11961177
private fun StringBuilder.appendOptional(name: String, value: Collection<*>) {
11971178
if (value.isNotEmpty()) {
11981179
append(", $name=$value")

utbot-framework/src/main/kotlin/org/utbot/engine/CollectionWrappers.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import org.utbot.engine.overrides.collections.UtHashSet
1010
import org.utbot.engine.overrides.collections.UtLinkedList
1111
import org.utbot.engine.pc.UtAddrExpression
1212
import org.utbot.engine.pc.UtExpression
13-
import org.utbot.engine.pc.UtFalse
1413
import org.utbot.engine.pc.select
1514
import org.utbot.engine.symbolic.asHardConstraint
1615
import org.utbot.engine.z3.intValue
@@ -26,7 +25,7 @@ import org.utbot.framework.plugin.api.UtNullModel
2625
import org.utbot.framework.plugin.api.UtReferenceModel
2726
import org.utbot.framework.plugin.api.UtStatementModel
2827
import org.utbot.framework.plugin.api.classId
29-
import org.utbot.framework.plugin.api.graph
28+
import org.utbot.framework.util.graph
3029
import org.utbot.framework.plugin.api.id
3130
import org.utbot.framework.plugin.api.util.booleanClassId
3231
import org.utbot.framework.plugin.api.util.constructorId

utbot-framework/src/main/kotlin/org/utbot/engine/Traverser.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,13 +89,13 @@ import org.utbot.framework.plugin.api.FieldId
8989
import org.utbot.framework.plugin.api.MethodId
9090
import org.utbot.framework.plugin.api.UtMethod
9191
import org.utbot.framework.plugin.api.classId
92-
import org.utbot.framework.plugin.api.graph
9392
import org.utbot.framework.plugin.api.id
9493
import org.utbot.framework.plugin.api.util.id
9594
import org.utbot.framework.plugin.api.util.jClass
9695
import org.utbot.framework.plugin.api.util.signature
9796
import org.utbot.framework.plugin.api.util.utContext
9897
import org.utbot.framework.util.executableId
98+
import org.utbot.framework.util.graph
9999
import java.lang.reflect.ParameterizedType
100100
import kotlin.collections.plus
101101
import kotlin.collections.plusAssign

utbot-framework/src/main/kotlin/org/utbot/engine/UtBotSymbolicEngine.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,13 +72,13 @@ import org.utbot.framework.plugin.api.UtMethod
7272
import org.utbot.framework.plugin.api.UtNullModel
7373
import org.utbot.framework.plugin.api.UtOverflowFailure
7474
import org.utbot.framework.plugin.api.UtResult
75-
import org.utbot.framework.plugin.api.graph
76-
import org.utbot.framework.plugin.api.jimpleBody
75+
import org.utbot.framework.util.graph
7776
import org.utbot.framework.plugin.api.onSuccess
7877
import org.utbot.framework.plugin.api.util.executableId
7978
import org.utbot.framework.plugin.api.util.id
8079
import org.utbot.framework.plugin.api.util.utContext
8180
import org.utbot.framework.plugin.api.util.description
81+
import org.utbot.framework.util.jimpleBody
8282
import org.utbot.fuzzer.FallbackModelProvider
8383
import org.utbot.fuzzer.FuzzedMethodDescription
8484
import org.utbot.fuzzer.ModelProvider

utbot-framework/src/main/kotlin/org/utbot/external/api/UtBotJavaApi.kt

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import org.utbot.framework.codegen.Junit5
88
import org.utbot.framework.codegen.NoStaticMocking
99
import org.utbot.framework.codegen.StaticsMocking
1010
import org.utbot.framework.codegen.TestFramework
11-
import org.utbot.framework.codegen.model.ModelBasedCodeGeneratorService
11+
import org.utbot.framework.codegen.model.CodeGenerator
1212
import org.utbot.framework.concrete.UtConcreteExecutionData
1313
import org.utbot.framework.concrete.UtConcreteExecutionResult
1414
import org.utbot.framework.concrete.UtExecutionInstrumentation
@@ -81,10 +81,9 @@ object UtBotJavaApi {
8181
}
8282

8383
return withUtContext(utContext) {
84-
val testGenerator = ModelBasedCodeGeneratorService().serviceProvider.apply {
84+
val testGenerator = CodeGenerator().apply {
8585
init(
8686
classUnderTest = classUnderTest,
87-
params = mutableMapOf(),
8887
testFramework = testFramework,
8988
mockFramework = mockFramework,
9089
codegenLanguage = codegenLanguage,
@@ -128,7 +127,7 @@ object UtBotJavaApi {
128127
FileUtil.isolateClassFiles(classUnderTest.kotlin).toPath(), classpath, dependencyClassPath
129128
)
130129
}
131-
.generateForSeveralMethods(
130+
.generateTestCases(
132131
methodsForAutomaticGeneration.map {
133132
toUtMethod(
134133
it.methodToBeTestedFromUserInput,
@@ -192,7 +191,7 @@ object UtBotJavaApi {
192191
init(
193192
FileUtil.isolateClassFiles(classUnderTest.kotlin).toPath(), classpath, dependencyClassPath
194193
)
195-
}.generateForSeveralMethods(
194+
}.generateTestCases(
196195
methodsForAutomaticGeneration.map {
197196
toUtMethod(
198197
it.methodToBeTestedFromUserInput,

utbot-framework/src/main/kotlin/org/utbot/framework/codegen/CodeGeneratorService.kt

Lines changed: 0 additions & 5 deletions
This file was deleted.

utbot-framework/src/main/kotlin/org/utbot/framework/codegen/TestCodeGenerator.kt

Lines changed: 0 additions & 35 deletions
This file was deleted.
Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
package org.utbot.framework.codegen.model
22

3+
import org.utbot.common.packageName
34
import org.utbot.framework.codegen.ForceStaticMocking
45
import org.utbot.framework.codegen.HangingTestsTimeout
56
import org.utbot.framework.codegen.ParametrizedTestSource
67
import org.utbot.framework.codegen.RuntimeExceptionTestsBehaviour
78
import org.utbot.framework.codegen.StaticsMocking
8-
import org.utbot.framework.codegen.TestCodeGenerator
99
import org.utbot.framework.codegen.TestFramework
10-
import org.utbot.framework.codegen.TestsCodeWithTestReport
1110
import org.utbot.framework.codegen.model.constructor.context.CgContext
1211
import org.utbot.framework.codegen.model.constructor.tree.CgTestClassConstructor
12+
import org.utbot.framework.codegen.model.constructor.tree.TestsGenerationReport
1313
import org.utbot.framework.codegen.model.tree.CgTestClassFile
1414
import org.utbot.framework.codegen.model.visitor.CgAbstractRenderer
1515
import org.utbot.framework.plugin.api.CodegenLanguage
@@ -18,23 +18,23 @@ import org.utbot.framework.plugin.api.UtMethod
1818
import org.utbot.framework.plugin.api.UtTestCase
1919
import org.utbot.framework.plugin.api.util.id
2020

21-
class ModelBasedTestCodeGenerator : TestCodeGenerator {
21+
class CodeGenerator {
2222
private lateinit var context: CgContext
2323

24-
override fun init(
24+
fun init(
2525
classUnderTest: Class<*>,
26-
params: MutableMap<UtMethod<*>, List<String>>,
27-
testFramework: TestFramework,
28-
mockFramework: MockFramework?,
29-
staticsMocking: StaticsMocking,
30-
forceStaticMocking: ForceStaticMocking,
31-
generateWarningsForStaticMocking: Boolean,
32-
codegenLanguage: CodegenLanguage,
33-
parameterizedTestSource: ParametrizedTestSource,
34-
runtimeExceptionTestsBehaviour: RuntimeExceptionTestsBehaviour,
35-
hangingTestsTimeout: HangingTestsTimeout,
36-
enableTestsTimeout: Boolean,
37-
testClassPackageName: String
26+
params: MutableMap<UtMethod<*>, List<String>> = mutableMapOf(),
27+
testFramework: TestFramework = TestFramework.defaultItem,
28+
mockFramework: MockFramework? = MockFramework.defaultItem,
29+
staticsMocking: StaticsMocking = StaticsMocking.defaultItem,
30+
forceStaticMocking: ForceStaticMocking = ForceStaticMocking.defaultItem,
31+
generateWarningsForStaticMocking: Boolean = true,
32+
codegenLanguage: CodegenLanguage = CodegenLanguage.defaultItem,
33+
parameterizedTestSource: ParametrizedTestSource = ParametrizedTestSource.defaultItem,
34+
runtimeExceptionTestsBehaviour: RuntimeExceptionTestsBehaviour = RuntimeExceptionTestsBehaviour.defaultItem,
35+
hangingTestsTimeout: HangingTestsTimeout = HangingTestsTimeout(),
36+
enableTestsTimeout: Boolean = true,
37+
testClassPackageName: String = classUnderTest.packageName,
3838
) {
3939
context = CgContext(
4040
classUnderTest = classUnderTest.id,
@@ -56,13 +56,13 @@ class ModelBasedTestCodeGenerator : TestCodeGenerator {
5656
}
5757

5858
//TODO: we support custom test class name only in utbot-online, probably support them in plugin as well
59-
override fun generateAsString(testCases: Collection<UtTestCase>, testClassCustomName: String?): String =
59+
fun generateAsString(testCases: Collection<UtTestCase>, testClassCustomName: String? = null): String =
6060
generateAsStringWithTestReport(testCases, testClassCustomName).generatedCode
6161

6262
//TODO: we support custom test class name only in utbot-online, probably support them in plugin as well
63-
override fun generateAsStringWithTestReport(
63+
fun generateAsStringWithTestReport(
6464
testCases: Collection<UtTestCase>,
65-
testClassCustomName: String?
65+
testClassCustomName: String? = null,
6666
): TestsCodeWithTestReport =
6767
withCustomContext(testClassCustomName) {
6868
context.withClassScope {
@@ -95,3 +95,6 @@ class ModelBasedTestCodeGenerator : TestCodeGenerator {
9595
return renderer.toString()
9696
}
9797
}
98+
99+
data class TestsCodeWithTestReport(val generatedCode: String, val testsGenerationReport: TestsGenerationReport)
100+

utbot-framework/src/main/kotlin/org/utbot/framework/codegen/model/ModelBasedCodeGeneratorService.kt

Lines changed: 0 additions & 9 deletions
This file was deleted.

utbot-framework/src/main/kotlin/org/utbot/framework/plugin/api/SymbolicEngineTestGeneratorService.kt

Lines changed: 0 additions & 6 deletions
This file was deleted.

0 commit comments

Comments
 (0)