Skip to content

Commit f11ed76

Browse files
committed
Refactor everything related to codegen
1 parent e7e448a commit f11ed76

File tree

166 files changed

+843
-1098
lines changed

Some content is hidden

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

166 files changed

+843
-1098
lines changed

HowToUseLoggers.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ The file is usually in the resource folder.
1818

1919
The easiest way is:
2020

21-
- Go in the code that you are going to debug. Let’s assume it is a method in org.utbot.framework.plugin.api.UtBotTestCaseGenerator.
21+
- Go in the code that you are going to debug. Let’s assume it is a method in org.utbot.framework.plugin.api.TestCaseGenerator.
2222
- Find out if there is a KotlinLogging object that is used to create a **logger**
2323
- If such a logger exists, use the fully qualified class name as the logger name in the next steps
2424
<br/>
@@ -28,7 +28,7 @@ The easiest way is:
2828
Open log4j2.xml and add the logger in the loggers section like this
2929

3030
```
31-
<Logger name=" org.utbot.framework.plugin.api.UtBotTestCaseGenerator " level="info">
31+
<Logger name=" org.utbot.framework.plugin.api.TestCaseGenerator " level="info">
3232
<AppenderRef ref="Console"/>
3333
</Logger>
3434
```

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: 8 additions & 10 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,14 +20,13 @@ 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
29-
import org.utbot.framework.plugin.api.UtBotTestCaseGenerator
29+
import org.utbot.framework.plugin.api.TestCaseGenerator
3030
import org.utbot.framework.plugin.api.UtMethod
3131
import org.utbot.framework.plugin.api.UtTestCase
3232
import org.utbot.summary.summarize
@@ -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+
TestCaseGenerator.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+
TestCaseGenerator.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>,
@@ -1024,17 +1023,6 @@ open class TypeParameters(val parameters: List<ClassId> = emptyList())
10241023

10251024
class WildcardTypeParameter: TypeParameters(emptyList())
10261025

1027-
interface TestCaseGenerator {
1028-
fun init(
1029-
buildDir: Path,
1030-
classpath: String? = null,
1031-
dependencyPaths: String,
1032-
isCanceled: () -> Boolean = { false }
1033-
)
1034-
1035-
fun generate(method: UtMethod<*>, mockStrategy: MockStrategyApi): UtTestCase
1036-
}
1037-
10381026
interface CodeGenerationSettingItem {
10391027
val displayName: String
10401028
val description: String
@@ -1193,13 +1181,6 @@ fun isolateCommandLineArgumentsToArgumentFile(arguments: List<String>): String {
11931181
return argumentFile.absolutePath.let { "@$it" }
11941182
}
11951183

1196-
interface UtService<T> {
1197-
val displayName: String
1198-
val serviceProvider: T
1199-
}
1200-
1201-
interface TestGeneratorService : UtService<TestCaseGenerator>
1202-
12031184
private fun StringBuilder.appendOptional(name: String, value: Collection<*>) {
12041185
if (value.isNotEmpty()) {
12051186
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.FuzzedValue

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

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@ 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
1515
import org.utbot.framework.plugin.api.ClassId
1616
import org.utbot.framework.plugin.api.CodegenLanguage
1717
import org.utbot.framework.plugin.api.MockFramework
1818
import org.utbot.framework.plugin.api.MockStrategyApi
19-
import org.utbot.framework.plugin.api.UtBotTestCaseGenerator
19+
import org.utbot.framework.plugin.api.TestCaseGenerator
2020
import org.utbot.framework.plugin.api.UtExecution
2121
import org.utbot.framework.plugin.api.UtMethod
2222
import org.utbot.framework.plugin.api.UtPrimitiveModel
@@ -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,
@@ -122,13 +121,13 @@ object UtBotJavaApi {
122121
val testCases: MutableList<UtTestCase> = mutableListOf()
123122

124123
testCases.addAll(withUtContext(utContext) {
125-
UtBotTestCaseGenerator
124+
TestCaseGenerator
126125
.apply {
127126
init(
128127
FileUtil.isolateClassFiles(classUnderTest.kotlin).toPath(), classpath, dependencyClassPath
129128
)
130129
}
131-
.generateForSeveralMethods(
130+
.generateTestCases(
132131
methodsForAutomaticGeneration.map {
133132
toUtMethod(
134133
it.methodToBeTestedFromUserInput,
@@ -187,12 +186,12 @@ object UtBotJavaApi {
187186
}
188187

189188
return withUtContext(UtContext(classUnderTest.classLoader)) {
190-
UtBotTestCaseGenerator
189+
TestCaseGenerator
191190
.apply {
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+

0 commit comments

Comments
 (0)