Skip to content

Commit 3e9f385

Browse files
committed
Introduce CodeGeneratorParams data class, to avoid repeating same params in functions and constructors
1 parent 94af547 commit 3e9f385

File tree

11 files changed

+163
-256
lines changed

11 files changed

+163
-256
lines changed

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

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import org.utbot.framework.codegen.domain.ProjectType
2323
import org.utbot.framework.codegen.domain.StaticsMocking
2424
import org.utbot.framework.codegen.domain.testFrameworkByName
2525
import org.utbot.framework.codegen.generator.CodeGenerator
26+
import org.utbot.framework.codegen.generator.CodeGeneratorParams
2627
import org.utbot.framework.codegen.services.language.CgLanguageAssistant
2728
import org.utbot.framework.plugin.api.ClassId
2829
import org.utbot.framework.plugin.api.CodegenLanguage
@@ -210,15 +211,17 @@ abstract class GenerateTestsAbstractCommand(name: String, help: String) :
210211
val generateWarningsForStaticMocking =
211212
forceStaticMocking == ForceStaticMocking.FORCE && staticsMocking is NoStaticMocking
212213
return CodeGenerator(
213-
testFramework = testFrameworkByName(testFramework),
214-
classUnderTest = classUnderTest,
215-
//TODO: Support Spring projects in utbot-cli if requested
216-
projectType = ProjectType.PureJvm,
217-
codegenLanguage = codegenLanguage,
218-
cgLanguageAssistant = CgLanguageAssistant.getByCodegenLanguage(codegenLanguage),
219-
staticsMocking = staticsMocking,
220-
forceStaticMocking = forceStaticMocking,
221-
generateWarningsForStaticMocking = generateWarningsForStaticMocking,
214+
CodeGeneratorParams(
215+
testFramework = testFrameworkByName(testFramework),
216+
classUnderTest = classUnderTest,
217+
//TODO: Support Spring projects in utbot-cli if requested
218+
projectType = ProjectType.PureJvm,
219+
codegenLanguage = codegenLanguage,
220+
cgLanguageAssistant = CgLanguageAssistant.getByCodegenLanguage(codegenLanguage),
221+
staticsMocking = staticsMocking,
222+
forceStaticMocking = forceStaticMocking,
223+
generateWarningsForStaticMocking = generateWarningsForStaticMocking,
224+
)
222225
)
223226
}
224227

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import org.utbot.framework.codegen.domain.ProjectType
1010
import org.utbot.framework.codegen.domain.StaticsMocking
1111
import org.utbot.framework.codegen.domain.TestFramework
1212
import org.utbot.framework.codegen.generator.CodeGenerator
13+
import org.utbot.framework.codegen.generator.CodeGeneratorParams
1314
import org.utbot.framework.codegen.services.language.CgLanguageAssistant
1415
import org.utbot.instrumentation.instrumentation.execution.UtConcreteExecutionData
1516
import org.utbot.instrumentation.instrumentation.execution.UtConcreteExecutionResult
@@ -85,6 +86,7 @@ object UtBotJavaApi {
8586

8687
return withUtContext(utContext) {
8788
val codeGenerator = CodeGenerator(
89+
CodeGeneratorParams(
8890
classUnderTest = classUnderTest.id,
8991
projectType = projectType,
9092
testFramework = testFramework,
@@ -96,6 +98,7 @@ object UtBotJavaApi {
9698
generateWarningsForStaticMocking = generateWarningsForStaticMocking,
9799
testClassPackageName = testClassPackageName
98100
)
101+
)
99102

100103
codeGenerator.generateAsString(testSets, destinationClassName)
101104
}

utbot-framework/src/main/kotlin/org/utbot/framework/codegen/generator/AbstractCodeGenerator.kt

Lines changed: 21 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,37 @@
11
package org.utbot.framework.codegen.generator
22

33
import mu.KotlinLogging
4-
import org.utbot.framework.codegen.domain.ForceStaticMocking
5-
import org.utbot.framework.codegen.domain.HangingTestsTimeout
6-
import org.utbot.framework.codegen.domain.ParametrizedTestSource
7-
import org.utbot.framework.codegen.domain.ProjectType
8-
import org.utbot.framework.codegen.domain.RuntimeExceptionTestsBehaviour
9-
import org.utbot.framework.codegen.domain.StaticsMocking
10-
import org.utbot.framework.codegen.domain.TestFramework
114
import org.utbot.framework.codegen.domain.context.CgContext
125
import org.utbot.framework.codegen.domain.models.CgClassFile
136
import org.utbot.framework.codegen.domain.models.CgMethodTestSet
147
import org.utbot.framework.codegen.renderer.CgAbstractRenderer
15-
import org.utbot.framework.codegen.services.language.CgLanguageAssistant
16-
import org.utbot.framework.plugin.api.ClassId
17-
import org.utbot.framework.plugin.api.CodegenLanguage
18-
import org.utbot.framework.plugin.api.ExecutableId
19-
import org.utbot.framework.plugin.api.MockFramework
208
import org.utbot.framework.plugin.api.UtMethodTestSet
219
import java.time.LocalDateTime
2210
import java.time.format.DateTimeFormatter
2311

24-
abstract class AbstractCodeGenerator(
25-
classUnderTest: ClassId,
26-
projectType: ProjectType,
27-
paramNames: MutableMap<ExecutableId, List<String>> = mutableMapOf(),
28-
generateUtilClassFile: Boolean = false,
29-
testFramework: TestFramework = TestFramework.defaultItem,
30-
mockFramework: MockFramework = MockFramework.defaultItem,
31-
staticsMocking: StaticsMocking = StaticsMocking.defaultItem,
32-
forceStaticMocking: ForceStaticMocking = ForceStaticMocking.defaultItem,
33-
generateWarningsForStaticMocking: Boolean = true,
34-
codegenLanguage: CodegenLanguage = CodegenLanguage.defaultItem,
35-
cgLanguageAssistant: CgLanguageAssistant = CgLanguageAssistant.getByCodegenLanguage(codegenLanguage),
36-
parameterizedTestSource: ParametrizedTestSource = ParametrizedTestSource.defaultItem,
37-
runtimeExceptionTestsBehaviour: RuntimeExceptionTestsBehaviour = RuntimeExceptionTestsBehaviour.defaultItem,
38-
hangingTestsTimeout: HangingTestsTimeout = HangingTestsTimeout(),
39-
enableTestsTimeout: Boolean = true,
40-
testClassPackageName: String = classUnderTest.packageName,
41-
) {
12+
abstract class AbstractCodeGenerator(params: CodeGeneratorParams) {
4213
protected val logger = KotlinLogging.logger {}
4314

44-
open var context: CgContext = CgContext(
45-
classUnderTest = classUnderTest,
46-
projectType = projectType,
47-
generateUtilClassFile = generateUtilClassFile,
48-
paramNames = paramNames,
49-
testFramework = testFramework,
50-
mockFramework = mockFramework,
51-
codegenLanguage = codegenLanguage,
52-
cgLanguageAssistant = cgLanguageAssistant,
53-
parametrizedTestSource = parameterizedTestSource,
54-
staticsMocking = staticsMocking,
55-
forceStaticMocking = forceStaticMocking,
56-
generateWarningsForStaticMocking = generateWarningsForStaticMocking,
57-
runtimeExceptionTestsBehaviour = runtimeExceptionTestsBehaviour,
58-
hangingTestsTimeout = hangingTestsTimeout,
59-
enableTestsTimeout = enableTestsTimeout,
60-
testClassPackageName = testClassPackageName
61-
)
15+
open var context: CgContext = with(params) {
16+
CgContext(
17+
classUnderTest = classUnderTest,
18+
projectType = projectType,
19+
generateUtilClassFile = generateUtilClassFile,
20+
paramNames = paramNames,
21+
testFramework = testFramework,
22+
mockFramework = mockFramework,
23+
codegenLanguage = codegenLanguage,
24+
cgLanguageAssistant = cgLanguageAssistant,
25+
parametrizedTestSource = parameterizedTestSource,
26+
staticsMocking = staticsMocking,
27+
forceStaticMocking = forceStaticMocking,
28+
generateWarningsForStaticMocking = generateWarningsForStaticMocking,
29+
runtimeExceptionTestsBehaviour = runtimeExceptionTestsBehaviour,
30+
hangingTestsTimeout = hangingTestsTimeout,
31+
enableTestsTimeout = enableTestsTimeout,
32+
testClassPackageName = testClassPackageName
33+
)
34+
}
6235

6336
//TODO: we support custom test class name only in utbot-online, probably support them in plugin as well
6437
fun generateAsString(testSets: Collection<UtMethodTestSet>, testClassCustomName: String? = null): String =

utbot-framework/src/main/kotlin/org/utbot/framework/codegen/generator/CodeGenerator.kt

Lines changed: 2 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,13 @@
11
package org.utbot.framework.codegen.generator
22

3-
import org.utbot.framework.codegen.domain.ForceStaticMocking
4-
import org.utbot.framework.codegen.domain.HangingTestsTimeout
5-
import org.utbot.framework.codegen.domain.ParametrizedTestSource
6-
import org.utbot.framework.codegen.domain.ProjectType
7-
import org.utbot.framework.codegen.domain.RuntimeExceptionTestsBehaviour
8-
import org.utbot.framework.codegen.domain.StaticsMocking
9-
import org.utbot.framework.codegen.domain.TestFramework
103
import org.utbot.framework.codegen.domain.models.CgMethodTestSet
114
import org.utbot.framework.codegen.domain.models.builders.SimpleTestClassModelBuilder
12-
import org.utbot.framework.codegen.services.language.CgLanguageAssistant
135
import org.utbot.framework.codegen.tree.CgSimpleTestClassConstructor
146
import org.utbot.framework.codegen.tree.ututils.UtilClassKind
157
import org.utbot.framework.plugin.api.ClassId
16-
import org.utbot.framework.plugin.api.CodegenLanguage
17-
import org.utbot.framework.plugin.api.ExecutableId
18-
import org.utbot.framework.plugin.api.MockFramework
198

20-
open class CodeGenerator(
21-
val classUnderTest: ClassId,
22-
val projectType: ProjectType,
23-
paramNames: MutableMap<ExecutableId, List<String>> = mutableMapOf(),
24-
generateUtilClassFile: Boolean = false,
25-
testFramework: TestFramework = TestFramework.defaultItem,
26-
mockFramework: MockFramework = MockFramework.defaultItem,
27-
staticsMocking: StaticsMocking = StaticsMocking.defaultItem,
28-
forceStaticMocking: ForceStaticMocking = ForceStaticMocking.defaultItem,
29-
generateWarningsForStaticMocking: Boolean = true,
30-
codegenLanguage: CodegenLanguage = CodegenLanguage.defaultItem,
31-
cgLanguageAssistant: CgLanguageAssistant = CgLanguageAssistant.getByCodegenLanguage(codegenLanguage),
32-
parameterizedTestSource: ParametrizedTestSource = ParametrizedTestSource.defaultItem,
33-
runtimeExceptionTestsBehaviour: RuntimeExceptionTestsBehaviour = RuntimeExceptionTestsBehaviour.defaultItem,
34-
hangingTestsTimeout: HangingTestsTimeout = HangingTestsTimeout(),
35-
enableTestsTimeout: Boolean = true,
36-
testClassPackageName: String = classUnderTest.packageName,
37-
): AbstractCodeGenerator(
38-
classUnderTest,
39-
projectType,
40-
paramNames,
41-
generateUtilClassFile,
42-
testFramework,
43-
mockFramework,
44-
staticsMocking,
45-
forceStaticMocking,
46-
generateWarningsForStaticMocking,
47-
codegenLanguage,
48-
cgLanguageAssistant,
49-
parameterizedTestSource,
50-
runtimeExceptionTestsBehaviour,
51-
hangingTestsTimeout,
52-
enableTestsTimeout,
53-
testClassPackageName,
54-
) {
9+
open class CodeGenerator(params: CodeGeneratorParams): AbstractCodeGenerator(params) {
10+
val classUnderTest: ClassId = params.classUnderTest
5511

5612
override fun generate(testSets: List<CgMethodTestSet>): CodeGeneratorResult {
5713
val testClassModel = SimpleTestClassModelBuilder(context).createTestClassModel(classUnderTest, testSets)
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package org.utbot.framework.codegen.generator
2+
3+
import org.utbot.framework.codegen.domain.ForceStaticMocking
4+
import org.utbot.framework.codegen.domain.HangingTestsTimeout
5+
import org.utbot.framework.codegen.domain.ParametrizedTestSource
6+
import org.utbot.framework.codegen.domain.ProjectType
7+
import org.utbot.framework.codegen.domain.RuntimeExceptionTestsBehaviour
8+
import org.utbot.framework.codegen.domain.StaticsMocking
9+
import org.utbot.framework.codegen.domain.TestFramework
10+
import org.utbot.framework.codegen.services.language.CgLanguageAssistant
11+
import org.utbot.framework.plugin.api.ClassId
12+
import org.utbot.framework.plugin.api.CodegenLanguage
13+
import org.utbot.framework.plugin.api.ExecutableId
14+
import org.utbot.framework.plugin.api.MockFramework
15+
16+
data class CodeGeneratorParams(
17+
val classUnderTest: ClassId,
18+
val projectType: ProjectType,
19+
val paramNames: MutableMap<ExecutableId, List<String>> = mutableMapOf(),
20+
val generateUtilClassFile: Boolean = false,
21+
val testFramework: TestFramework = TestFramework.defaultItem,
22+
val mockFramework: MockFramework = MockFramework.defaultItem,
23+
val staticsMocking: StaticsMocking = StaticsMocking.defaultItem,
24+
val forceStaticMocking: ForceStaticMocking = ForceStaticMocking.defaultItem,
25+
val generateWarningsForStaticMocking: Boolean = true,
26+
val codegenLanguage: CodegenLanguage = CodegenLanguage.defaultItem,
27+
val cgLanguageAssistant: CgLanguageAssistant = CgLanguageAssistant.getByCodegenLanguage(codegenLanguage),
28+
val parameterizedTestSource: ParametrizedTestSource = ParametrizedTestSource.defaultItem,
29+
val runtimeExceptionTestsBehaviour: RuntimeExceptionTestsBehaviour = RuntimeExceptionTestsBehaviour.defaultItem,
30+
val hangingTestsTimeout: HangingTestsTimeout = HangingTestsTimeout(),
31+
val enableTestsTimeout: Boolean = true,
32+
val testClassPackageName: String = classUnderTest.packageName,
33+
)

utbot-framework/src/main/kotlin/org/utbot/framework/codegen/generator/SpringCodeGenerator.kt

Lines changed: 13 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,5 @@
11
package org.utbot.framework.codegen.generator
22

3-
import org.utbot.framework.codegen.domain.ForceStaticMocking
4-
import org.utbot.framework.codegen.domain.HangingTestsTimeout
5-
import org.utbot.framework.codegen.domain.ParametrizedTestSource
6-
import org.utbot.framework.codegen.domain.ProjectType
7-
import org.utbot.framework.codegen.domain.RuntimeExceptionTestsBehaviour
8-
import org.utbot.framework.codegen.domain.StaticsMocking
9-
import org.utbot.framework.codegen.domain.TestFramework
103
import org.utbot.framework.codegen.domain.context.CgContext
114
import org.utbot.framework.codegen.domain.models.CgMethodTestSet
125
import org.utbot.framework.codegen.domain.models.builders.SpringTestClassModelBuilder
@@ -17,53 +10,25 @@ import org.utbot.framework.codegen.tree.CgSpringVariableConstructor
1710
import org.utbot.framework.codegen.tree.CgVariableConstructor
1811
import org.utbot.framework.codegen.tree.ututils.UtilClassKind
1912
import org.utbot.framework.plugin.api.ClassId
20-
import org.utbot.framework.plugin.api.CodegenLanguage
21-
import org.utbot.framework.plugin.api.ExecutableId
22-
import org.utbot.framework.plugin.api.MockFramework
23-
import org.utbot.framework.plugin.api.SpringTestType
2413
import org.utbot.framework.plugin.api.SpringCodeGenerationContext
25-
import org.utbot.framework.plugin.api.SpringSettings.*
14+
import org.utbot.framework.plugin.api.SpringSettings.AbsentSpringSettings
15+
import org.utbot.framework.plugin.api.SpringSettings.PresentSpringSettings
16+
import org.utbot.framework.plugin.api.SpringTestType
2617

2718
class SpringCodeGenerator(
28-
val classUnderTest: ClassId,
29-
val projectType: ProjectType,
3019
val springCodeGenerationContext: SpringCodeGenerationContext,
31-
paramNames: MutableMap<ExecutableId, List<String>> = mutableMapOf(),
32-
generateUtilClassFile: Boolean = false,
33-
testFramework: TestFramework = TestFramework.defaultItem,
34-
mockFramework: MockFramework = MockFramework.defaultItem,
35-
staticsMocking: StaticsMocking = StaticsMocking.defaultItem,
36-
forceStaticMocking: ForceStaticMocking = ForceStaticMocking.defaultItem,
37-
generateWarningsForStaticMocking: Boolean = true,
38-
codegenLanguage: CodegenLanguage = CodegenLanguage.defaultItem,
39-
cgLanguageAssistant: CgLanguageAssistant = CgLanguageAssistant.getByCodegenLanguage(codegenLanguage),
40-
parameterizedTestSource: ParametrizedTestSource = ParametrizedTestSource.defaultItem,
41-
runtimeExceptionTestsBehaviour: RuntimeExceptionTestsBehaviour = RuntimeExceptionTestsBehaviour.defaultItem,
42-
hangingTestsTimeout: HangingTestsTimeout = HangingTestsTimeout(),
43-
enableTestsTimeout: Boolean = true,
44-
testClassPackageName: String = classUnderTest.packageName,
20+
params: CodeGeneratorParams
4521
) : AbstractCodeGenerator(
46-
classUnderTest,
47-
projectType,
48-
paramNames,
49-
generateUtilClassFile,
50-
testFramework,
51-
mockFramework,
52-
staticsMocking,
53-
forceStaticMocking,
54-
generateWarningsForStaticMocking,
55-
codegenLanguage,
56-
cgLanguageAssistant = object : CgLanguageAssistant by cgLanguageAssistant {
57-
override fun getVariableConstructorBy(context: CgContext): CgVariableConstructor =
58-
// TODO decorate original `cgLanguageAssistant.getVariableConstructorBy(context)`
59-
CgSpringVariableConstructor(context)
60-
},
61-
parameterizedTestSource,
62-
runtimeExceptionTestsBehaviour,
63-
hangingTestsTimeout,
64-
enableTestsTimeout,
65-
testClassPackageName,
22+
params.copy(
23+
cgLanguageAssistant = object : CgLanguageAssistant by params.cgLanguageAssistant {
24+
override fun getVariableConstructorBy(context: CgContext): CgVariableConstructor =
25+
// TODO decorate original `params.cgLanguageAssistant.getVariableConstructorBy(context)`
26+
CgSpringVariableConstructor(context)
27+
}
28+
)
6629
) {
30+
val classUnderTest: ClassId = params.classUnderTest
31+
6732
override fun generate(testSets: List<CgMethodTestSet>): CodeGeneratorResult {
6833
val testClassModel = SpringTestClassModelBuilder(context).createTestClassModel(classUnderTest, testSets)
6934

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

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package org.utbot.framework.plugin.sarif
33
import org.utbot.framework.codegen.domain.ForceStaticMocking
44
import org.utbot.framework.codegen.domain.NoStaticMocking
55
import org.utbot.framework.codegen.generator.CodeGenerator
6+
import org.utbot.framework.codegen.generator.CodeGeneratorParams
67
import org.utbot.framework.codegen.services.language.CgLanguageAssistant
78
import org.utbot.framework.plugin.api.TestCaseGenerator
89
import org.utbot.framework.plugin.api.UtMethodTestSet
@@ -70,15 +71,17 @@ class GenerateTestsAndSarifReportFacade(
7071
val isForceStaticMocking = sarifProperties.forceStaticMocking == ForceStaticMocking.FORCE
7172

7273
return CodeGenerator(
73-
classUnderTest = targetClass.classUnderTest.id,
74-
projectType = sarifProperties.projectType,
75-
testFramework = sarifProperties.testFramework,
76-
mockFramework = sarifProperties.mockFramework,
77-
staticsMocking = sarifProperties.staticsMocking,
78-
forceStaticMocking = sarifProperties.forceStaticMocking,
79-
generateWarningsForStaticMocking = isNoStaticMocking && isForceStaticMocking,
80-
codegenLanguage = sarifProperties.codegenLanguage,
81-
cgLanguageAssistant = CgLanguageAssistant.getByCodegenLanguage(sarifProperties.codegenLanguage),
74+
CodeGeneratorParams(
75+
classUnderTest = targetClass.classUnderTest.id,
76+
projectType = sarifProperties.projectType,
77+
testFramework = sarifProperties.testFramework,
78+
mockFramework = sarifProperties.mockFramework,
79+
staticsMocking = sarifProperties.staticsMocking,
80+
forceStaticMocking = sarifProperties.forceStaticMocking,
81+
generateWarningsForStaticMocking = isNoStaticMocking && isForceStaticMocking,
82+
codegenLanguage = sarifProperties.codegenLanguage,
83+
cgLanguageAssistant = CgLanguageAssistant.getByCodegenLanguage(sarifProperties.codegenLanguage),
84+
)
8285
)
8386
}
8487

0 commit comments

Comments
 (0)