Skip to content

Commit 1c89bdf

Browse files
committed
Restructuralize code generator
1 parent d554c3c commit 1c89bdf

File tree

98 files changed

+1585
-1505
lines changed

Some content is hidden

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

98 files changed

+1585
-1505
lines changed

docs/UtUtilsClass.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ As one can see, the comment mentions two characteristics of the `UtUtils` class:
2525
If the generated tests require additional _utility methods_, the
2626
existing `UtUtils` class is upgraded and gets a new version number, which should be defined here:
2727

28-
`org.utbot.framework.codegen.model.constructor.builtin.UtilClassFileMethodProvider.UTIL_CLASS_VERSION`
28+
`org.utbot.framework.codegen.domain.builtin.UtilClassFileMethodProvider.UTIL_CLASS_VERSION`
2929

3030
_2. Mockito support_
3131

utbot-cli-python/src/main/kotlin/org/utbot/cli/language/python/PythonGenerateTestsCommand.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import com.github.ajalt.clikt.parameters.options.*
66
import com.github.ajalt.clikt.parameters.types.choice
77
import com.github.ajalt.clikt.parameters.types.long
88
import mu.KotlinLogging
9-
import org.utbot.framework.codegen.TestFramework
9+
import org.utbot.framework.codegen.domain.TestFramework
1010
import org.utbot.python.PythonMethod
1111
import org.utbot.python.PythonTestGenerationProcessor
1212
import org.utbot.python.PythonTestGenerationProcessor.processTestGeneration

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ import org.utbot.common.PathUtil.toURL
1616
import org.utbot.common.toPath
1717
import org.utbot.engine.Mocker
1818
import org.utbot.framework.UtSettings
19-
import org.utbot.framework.codegen.ForceStaticMocking
20-
import org.utbot.framework.codegen.MockitoStaticMocking
21-
import org.utbot.framework.codegen.NoStaticMocking
22-
import org.utbot.framework.codegen.StaticsMocking
23-
import org.utbot.framework.codegen.model.CodeGenerator
24-
import org.utbot.framework.codegen.testFrameworkByName
19+
import org.utbot.framework.codegen.CodeGenerator
20+
import org.utbot.framework.codegen.domain.ForceStaticMocking
21+
import org.utbot.framework.codegen.domain.MockitoStaticMocking
22+
import org.utbot.framework.codegen.domain.NoStaticMocking
23+
import org.utbot.framework.codegen.domain.StaticsMocking
24+
import org.utbot.framework.codegen.domain.testFrameworkByName
2525
import org.utbot.framework.plugin.api.ClassId
2626
import org.utbot.framework.plugin.api.CodegenLanguage
2727
import org.utbot.framework.plugin.api.ExecutableId

utbot-framework-test/src/test/java/org/utbot/examples/manual/UtBotJavaApiTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@
2020
import org.utbot.external.api.TestMethodInfo;
2121
import org.utbot.external.api.UtBotJavaApi;
2222
import org.utbot.external.api.UtModelFactory;
23-
import org.utbot.framework.codegen.ForceStaticMocking;
24-
import org.utbot.framework.codegen.Junit4;
25-
import org.utbot.framework.codegen.MockitoStaticMocking;
23+
import org.utbot.framework.codegen.domain.ForceStaticMocking;
24+
import org.utbot.framework.codegen.domain.Junit4;
25+
import org.utbot.framework.codegen.domain.MockitoStaticMocking;
2626
import org.utbot.framework.plugin.api.*;
2727
import org.utbot.framework.plugin.api.util.UtContext;
2828
import org.utbot.framework.plugin.services.JdkInfoDefaultProvider;

utbot-framework-test/src/test/kotlin/org/utbot/framework/assemble/AssembleModelGeneratorTests.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import org.utbot.examples.assemble.DefaultFieldWithDirectAccessor
3737
import org.utbot.examples.assemble.DefaultFieldWithSetter
3838
import org.utbot.examples.assemble.DefaultPackagePrivateField
3939
import org.utbot.examples.assemble.StaticField
40+
import org.utbot.framework.codegen.tree.arrayTypeOf
4041
import org.utbot.framework.plugin.api.ClassId
4142
import org.utbot.framework.plugin.api.ExecutableId
4243
import org.utbot.framework.plugin.api.FieldId
@@ -58,7 +59,6 @@ import org.utbot.framework.util.SootUtils
5859
import org.utbot.framework.util.instanceCounter
5960
import org.utbot.framework.util.modelIdCounter
6061
import kotlin.reflect.full.functions
61-
import org.utbot.framework.codegen.model.constructor.util.arrayTypeOf
6262

6363
/**
6464
* Test classes must be located in the same folder as [AssembleTestUtils] class.

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ package org.utbot.external.api
33
import org.utbot.common.FileUtil
44
import org.utbot.common.nameOfPackage
55
import org.utbot.framework.UtSettings
6-
import org.utbot.framework.codegen.ForceStaticMocking
7-
import org.utbot.framework.codegen.Junit5
8-
import org.utbot.framework.codegen.NoStaticMocking
9-
import org.utbot.framework.codegen.StaticsMocking
10-
import org.utbot.framework.codegen.TestFramework
11-
import org.utbot.framework.codegen.model.CodeGenerator
6+
import org.utbot.framework.codegen.CodeGenerator
7+
import org.utbot.framework.codegen.domain.ForceStaticMocking
8+
import org.utbot.framework.codegen.domain.Junit5
9+
import org.utbot.framework.codegen.domain.NoStaticMocking
10+
import org.utbot.framework.codegen.domain.StaticsMocking
11+
import org.utbot.framework.codegen.domain.TestFramework
1212
import org.utbot.framework.concrete.UtConcreteExecutionData
1313
import org.utbot.framework.concrete.UtConcreteExecutionResult
1414
import org.utbot.framework.concrete.UtExecutionInstrumentation

utbot-framework/src/main/kotlin/org/utbot/framework/assemble/AssembleModelGenerator.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import org.utbot.common.isPublic
55
import org.utbot.engine.ResolvedExecution
66
import org.utbot.engine.ResolvedModels
77
import org.utbot.framework.UtSettings
8-
import org.utbot.framework.codegen.model.util.isAccessibleFrom
8+
import org.utbot.framework.codegen.util.isAccessibleFrom
99
import org.utbot.framework.modifications.AnalysisMode.SettersAndDirectAccessors
1010
import org.utbot.framework.modifications.ConstructorAnalyzer
1111
import org.utbot.framework.modifications.ConstructorAssembleInfo
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
package org.utbot.framework.codegen
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.RuntimeExceptionTestsBehaviour
7+
import org.utbot.framework.codegen.domain.StaticsMocking
8+
import org.utbot.framework.codegen.domain.TestFramework
9+
import org.utbot.framework.codegen.domain.models.CgMethodTestSet
10+
import org.utbot.framework.codegen.domain.models.TestClassModel
11+
import org.utbot.framework.codegen.domain.context.CgContext
12+
import org.utbot.framework.codegen.renderer.CgAbstractRenderer
13+
import org.utbot.framework.codegen.reports.TestsGenerationReport
14+
import org.utbot.framework.codegen.tree.CgTestClassConstructor
15+
import org.utbot.framework.codegen.tree.ututils.UtilClassKind
16+
import org.utbot.framework.codegen.services.language.CgLanguageAssistant
17+
import org.utbot.framework.plugin.api.ClassId
18+
import org.utbot.framework.plugin.api.CodegenLanguage
19+
import org.utbot.framework.plugin.api.ExecutableId
20+
import org.utbot.framework.plugin.api.MockFramework
21+
import org.utbot.framework.plugin.api.UtMethodTestSet
22+
23+
open class CodeGenerator(
24+
val classUnderTest: ClassId,
25+
paramNames: MutableMap<ExecutableId, List<String>> = mutableMapOf(),
26+
generateUtilClassFile: Boolean = false,
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,
38+
) {
39+
40+
open var context: CgContext = CgContext(
41+
classUnderTest = classUnderTest,
42+
generateUtilClassFile = generateUtilClassFile,
43+
paramNames = paramNames,
44+
testFramework = testFramework,
45+
mockFramework = mockFramework,
46+
codegenLanguage = codegenLanguage,
47+
cgLanguageAssistant = CgLanguageAssistant.getByCodegenLanguage(codegenLanguage),
48+
parametrizedTestSource = parameterizedTestSource,
49+
staticsMocking = staticsMocking,
50+
forceStaticMocking = forceStaticMocking,
51+
generateWarningsForStaticMocking = generateWarningsForStaticMocking,
52+
runtimeExceptionTestsBehaviour = runtimeExceptionTestsBehaviour,
53+
hangingTestsTimeout = hangingTestsTimeout,
54+
enableTestsTimeout = enableTestsTimeout,
55+
testClassPackageName = testClassPackageName
56+
)
57+
58+
//TODO: we support custom test class name only in utbot-online, probably support them in plugin as well
59+
fun generateAsString(testSets: Collection<UtMethodTestSet>, testClassCustomName: String? = null): String =
60+
generateAsStringWithTestReport(testSets, testClassCustomName).generatedCode
61+
62+
//TODO: we support custom test class name only in utbot-online, probably support them in plugin as well
63+
fun generateAsStringWithTestReport(
64+
testSets: Collection<UtMethodTestSet>,
65+
testClassCustomName: String? = null,
66+
): CodeGeneratorResult {
67+
val cgTestSets = testSets.map { CgMethodTestSet(it) }.toList()
68+
return withCustomContext(testClassCustomName) {
69+
context.withTestClassFileScope {
70+
val astConstructor = CgTestClassConstructor(context)
71+
val renderer = CgAbstractRenderer.makeRenderer(context)
72+
val testClassModel = TestClassModel.fromTestSets(classUnderTest, cgTestSets)
73+
74+
val testClassFile = astConstructor.construct(testClassModel)
75+
testClassFile.accept(renderer)
76+
77+
CodeGeneratorResult(
78+
generatedCode = renderer.toString(),
79+
utilClassKind = UtilClassKind.fromCgContextOrNull(context),
80+
testsGenerationReport = testClassFile.testsGenerationReport
81+
)
82+
}
83+
}
84+
}
85+
86+
/**
87+
* Wrapper function that configures context as needed for utbot-online:
88+
* - turns on imports optimization in code generator
89+
* - passes a custom test class name if there is one
90+
*/
91+
fun <R> withCustomContext(testClassCustomName: String? = null, block: () -> R): R {
92+
val prevContext = context
93+
return try {
94+
context = prevContext.copy(
95+
shouldOptimizeImports = true,
96+
testClassCustomName = testClassCustomName
97+
)
98+
block()
99+
} finally {
100+
context = prevContext
101+
}
102+
}
103+
}
104+
105+
/**
106+
* @property generatedCode the source code of the test class
107+
* @property testsGenerationReport some info about test generation process
108+
* @property utilClassKind the kind of util class if it is required, otherwise - null
109+
*/
110+
data class CodeGeneratorResult(
111+
val generatedCode: String,
112+
val testsGenerationReport: TestsGenerationReport,
113+
// null if no util class needed, e.g. when we are generating utils directly into test class
114+
val utilClassKind: UtilClassKind? = null,
115+
)
116+

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

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

utbot-framework/src/main/kotlin/org/utbot/framework/codegen/Domain.kt renamed to utbot-framework/src/main/kotlin/org/utbot/framework/codegen/domain/Domain.kt

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
package org.utbot.framework.codegen
1+
package org.utbot.framework.codegen.domain
22

33
import org.utbot.framework.DEFAULT_CONCRETE_EXECUTION_TIMEOUT_IN_CHILD_PROCESS_MS
4-
import org.utbot.framework.codegen.model.constructor.builtin.mockitoClassId
5-
import org.utbot.framework.codegen.model.constructor.builtin.ongoingStubbingClassId
6-
import org.utbot.framework.codegen.model.constructor.util.argumentsClassId
7-
import org.utbot.framework.codegen.model.tree.CgClassId
4+
import org.utbot.framework.codegen.domain.builtin.mockitoClassId
5+
import org.utbot.framework.codegen.domain.builtin.ongoingStubbingClassId
6+
import org.utbot.framework.codegen.domain.models.CgClassId
7+
import org.utbot.framework.codegen.tree.argumentsClassId
88
import org.utbot.framework.plugin.api.BuiltinClassId
99
import org.utbot.framework.plugin.api.ClassId
1010
import org.utbot.framework.plugin.api.CodeGenerationSettingBox
@@ -30,7 +30,6 @@ import org.utbot.framework.plugin.api.util.shortArrayClassId
3030
import org.utbot.framework.plugin.api.util.voidClassId
3131
import java.io.File
3232
import org.utbot.framework.plugin.api.util.longClassId
33-
import org.utbot.framework.plugin.api.util.objectArrayClassId
3433
import org.utbot.framework.plugin.api.util.voidWrapperClassId
3534

3635
data class TestClassFile(val packageName: String, val imports: List<Import>, val testClass: String)
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
package org.utbot.framework.codegen.model.constructor.builtin
1+
package org.utbot.framework.codegen.domain.builtin
22

33
import org.utbot.framework.plugin.api.BuiltinClassId
4-
import org.utbot.framework.plugin.api.MethodId
54
import org.utbot.framework.plugin.api.util.booleanClassId
65
import org.utbot.framework.plugin.api.util.builtinMethodId
76
import org.utbot.framework.plugin.api.util.builtinStaticMethodId
Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package org.utbot.framework.codegen.model.constructor.builtin
1+
package org.utbot.framework.codegen.domain.builtin
22

33
import org.utbot.framework.plugin.api.ClassId
44
import org.utbot.framework.plugin.api.MethodId
@@ -19,15 +19,6 @@ import java.lang.reflect.InvocationTargetException
1919
//TODO: these methods are called builtins, but actually are just [MethodId]
2020
//may be fixed in https://github.com/UnitTestBot/UTBotJava/issues/138
2121

22-
internal val reflectionBuiltins: Set<MethodId>
23-
get() = setOf(
24-
setAccessible, invoke, newInstance, getMethodId, forName,
25-
getDeclaredMethod, getDeclaredConstructor, allocateInstance,
26-
getClass, getDeclaredField, isEnumConstant, getFieldName,
27-
equals, getSuperclass, setMethodId, newArrayInstance,
28-
setArrayElement, getArrayElement, getTargetException,
29-
)
30-
3122
internal val setAccessible = methodId(
3223
classId = AccessibleObject::class.id,
3324
name = "setAccessible",
Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
package org.utbot.framework.codegen.model.constructor.builtin
2-
3-
import org.utbot.framework.codegen.MockitoStaticMocking
4-
import org.utbot.framework.codegen.model.UtilClassKind.Companion.PACKAGE_DELIMITER
5-
import org.utbot.framework.codegen.model.UtilClassKind.Companion.UT_UTILS_BASE_PACKAGE_NAME
6-
import org.utbot.framework.codegen.model.constructor.util.arrayTypeOf
7-
import org.utbot.framework.codegen.model.constructor.util.utilMethodId
8-
import org.utbot.framework.codegen.model.tree.CgClassId
9-
import org.utbot.framework.codegen.model.visitor.utilMethodTextById
1+
package org.utbot.framework.codegen.domain.builtin
2+
3+
import org.utbot.framework.codegen.domain.MockitoStaticMocking
4+
import org.utbot.framework.codegen.domain.models.CgClassId
5+
import org.utbot.framework.codegen.renderer.utilMethodTextById
6+
import org.utbot.framework.codegen.tree.arrayTypeOf
7+
import org.utbot.framework.codegen.tree.utilMethodId
8+
import org.utbot.framework.codegen.tree.ututils.UtilClassKind.Companion.PACKAGE_DELIMITER
9+
import org.utbot.framework.codegen.tree.ututils.UtilClassKind.Companion.UT_UTILS_BASE_PACKAGE_NAME
1010
import org.utbot.framework.plugin.api.BuiltinClassId
1111
import org.utbot.framework.plugin.api.BuiltinConstructorId
1212
import org.utbot.framework.plugin.api.ClassId

0 commit comments

Comments
 (0)