Skip to content

Commit 4223184

Browse files
committed
Introduce projectType field in CodeGenerator
1 parent d3a39e3 commit 4223184

File tree

15 files changed

+66
-33
lines changed

15 files changed

+66
-33
lines changed

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

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import mu.KotlinLogging
44
import org.utbot.framework.codegen.domain.ForceStaticMocking
55
import org.utbot.framework.codegen.domain.HangingTestsTimeout
66
import org.utbot.framework.codegen.domain.ParametrizedTestSource
7+
import org.utbot.framework.codegen.domain.ProjectType
8+
import org.utbot.framework.codegen.domain.ProjectType.*
79
import org.utbot.framework.codegen.domain.RuntimeExceptionTestsBehaviour
810
import org.utbot.framework.codegen.domain.StaticsMocking
911
import org.utbot.framework.codegen.domain.TestFramework
@@ -28,6 +30,8 @@ import java.time.format.DateTimeFormatter
2830

2931
open class CodeGenerator(
3032
val classUnderTest: ClassId,
33+
//TODO: support setting `projectType` in Sarif plugins, UtBotJava api, etc.
34+
val projectType: ProjectType = PureJvm,
3135
paramNames: MutableMap<ExecutableId, List<String>> = mutableMapOf(),
3236
generateUtilClassFile: Boolean = false,
3337
testFramework: TestFramework = TestFramework.defaultItem,
@@ -48,6 +52,7 @@ open class CodeGenerator(
4852

4953
open var context: CgContext = CgContext(
5054
classUnderTest = classUnderTest,
55+
projectType = projectType,
5156
generateUtilClassFile = generateUtilClassFile,
5257
paramNames = paramNames,
5358
testFramework = testFramework,
@@ -76,10 +81,11 @@ open class CodeGenerator(
7681
val cgTestSets = testSets.map { CgMethodTestSet(it) }.toList()
7782
return withCustomContext(testClassCustomName) {
7883
context.withTestClassFileScope {
79-
if (context.isSpringClass) {
80-
generateForSpringClass(cgTestSets)
81-
} else {
82-
generateForSimpleClass(cgTestSets)
84+
when (context.projectType) {
85+
PureJvm,
86+
Python,
87+
JavaScript -> generateForSimpleClass(cgTestSets)
88+
Spring -> generateForSpringClass(cgTestSets)
8389
}
8490
}
8591
}

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

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -714,14 +714,24 @@ enum class ParametrizedTestSource(
714714

715715
enum class ProjectType {
716716
/**
717-
* Standard JVM application without DI frameworks.
717+
* Standard JVM project without DI frameworks
718718
*/
719-
PURE_JVM,
719+
PureJvm,
720720

721721
/**
722-
* Spring or Spring Boot application.
722+
* Spring or Spring Boot project
723723
*/
724-
SPRING_APPLICATION,
724+
Spring,
725+
726+
/**
727+
* Python project
728+
*/
729+
Python,
730+
731+
/**
732+
* JavaScript project
733+
*/
734+
JavaScript,
725735
}
726736

727737
enum class TypeReplacementApproach {

utbot-framework/src/main/kotlin/org/utbot/framework/codegen/domain/context/CgContext.kt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import kotlinx.collections.immutable.PersistentSet
2222
import kotlinx.collections.immutable.persistentListOf
2323
import kotlinx.collections.immutable.persistentMapOf
2424
import kotlinx.collections.immutable.persistentSetOf
25+
import org.utbot.framework.codegen.domain.ProjectType
2526
import org.utbot.framework.codegen.domain.models.CgMethodTestSet
2627
import org.utbot.framework.codegen.domain.builtin.TestClassUtilMethodProvider
2728
import org.utbot.framework.codegen.domain.builtin.UtilClassFileMethodProvider
@@ -66,8 +67,8 @@ interface CgContextOwner {
6667
// current class under test
6768
val classUnderTest: ClassId
6869

69-
// If test class is configured with Spring, we should do some extra analysis
70-
val isSpringClass: Boolean
70+
// If project under test is configured with Spring, we should do some extra analysis
71+
val projectType: ProjectType
7172

7273
// test class currently being generated (if series of nested classes is generated, it is the outermost one)
7374
val outerMostTestClass: ClassId
@@ -434,7 +435,7 @@ interface CgContextOwner {
434435
*/
435436
data class CgContext(
436437
override val classUnderTest: ClassId,
437-
override val isSpringClass: Boolean = false,
438+
override val projectType: ProjectType,
438439
val generateUtilClassFile: Boolean = false,
439440
override var currentExecutable: ExecutableId? = null,
440441
override val collectedExceptions: MutableSet<ClassId> = mutableSetOf(),

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import org.utbot.framework.codegen.domain.HangingTestsTimeout
1212
import org.utbot.framework.codegen.domain.MockitoStaticMocking
1313
import org.utbot.framework.codegen.domain.NoStaticMocking
1414
import org.utbot.framework.codegen.domain.ParametrizedTestSource
15+
import org.utbot.framework.codegen.domain.ProjectType
1516
import org.utbot.framework.codegen.domain.RuntimeExceptionTestsBehaviour
1617
import org.utbot.framework.codegen.domain.testFrameworkByName
1718
import org.utbot.framework.codegen.reports.TestsGenerationReport
@@ -135,6 +136,7 @@ private fun EngineProcessModel.setup(kryoHelper: KryoHelper, watchdog: IdleWatch
135136
val testSetsId: Long = params.testSetsId
136137
val codeGenerator = CodeGenerator(
137138
classUnderTest = classId,
139+
projectType = ProjectType.valueOf(params.projectType),
138140
generateUtilClassFile = params.generateUtilClassFile,
139141
paramNames = kryoHelper.readObject(params.paramNames),
140142
testFramework = testFramework,

utbot-framework/src/main/kotlin/org/utbot/framework/process/generated/EngineProcessModel.Generated.kt

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ class EngineProcessModel private constructor(
7272
}
7373

7474

75-
const val serializationHash = -120710112541549600L
75+
const val serializationHash = -2087034443345538396L
7676

7777
}
7878
override val serializersOwner: ISerializersOwner get() = EngineProcessModel
@@ -173,7 +173,7 @@ val IProtocol.engineProcessModel get() = getOrCreateExtension(EngineProcessModel
173173

174174

175175
/**
176-
* #### Generated from [EngineProcessModel.kt:98]
176+
* #### Generated from [EngineProcessModel.kt:99]
177177
*/
178178
data class FindMethodParamNamesArguments (
179179
val classId: ByteArray,
@@ -236,7 +236,7 @@ data class FindMethodParamNamesArguments (
236236

237237

238238
/**
239-
* #### Generated from [EngineProcessModel.kt:102]
239+
* #### Generated from [EngineProcessModel.kt:103]
240240
*/
241241
data class FindMethodParamNamesResult (
242242
val paramNames: ByteArray
@@ -293,7 +293,7 @@ data class FindMethodParamNamesResult (
293293

294294

295295
/**
296-
* #### Generated from [EngineProcessModel.kt:91]
296+
* #### Generated from [EngineProcessModel.kt:92]
297297
*/
298298
data class FindMethodsInClassMatchingSelectedArguments (
299299
val classId: ByteArray,
@@ -356,7 +356,7 @@ data class FindMethodsInClassMatchingSelectedArguments (
356356

357357

358358
/**
359-
* #### Generated from [EngineProcessModel.kt:95]
359+
* #### Generated from [EngineProcessModel.kt:96]
360360
*/
361361
data class FindMethodsInClassMatchingSelectedResult (
362362
val executableIds: ByteArray
@@ -581,7 +581,7 @@ data class GenerateResult (
581581

582582

583583
/**
584-
* #### Generated from [EngineProcessModel.kt:110]
584+
* #### Generated from [EngineProcessModel.kt:111]
585585
*/
586586
data class GenerateTestReportArgs (
587587
val eventLogMessage: String?,
@@ -674,7 +674,7 @@ data class GenerateTestReportArgs (
674674

675675

676676
/**
677-
* #### Generated from [EngineProcessModel.kt:119]
677+
* #### Generated from [EngineProcessModel.kt:120]
678678
*/
679679
data class GenerateTestReportResult (
680680
val notifyMessage: String,
@@ -806,7 +806,7 @@ data class JdkInfo (
806806

807807

808808
/**
809-
* #### Generated from [EngineProcessModel.kt:86]
809+
* #### Generated from [EngineProcessModel.kt:87]
810810
*/
811811
data class MethodDescription (
812812
val name: String,
@@ -880,6 +880,7 @@ data class MethodDescription (
880880
data class RenderParams (
881881
val testSetsId: Long,
882882
val classUnderTest: ByteArray,
883+
val projectType: String,
883884
val paramNames: ByteArray,
884885
val generateUtilClassFile: Boolean,
885886
val testFramework: String,
@@ -903,6 +904,7 @@ data class RenderParams (
903904
override fun read(ctx: SerializationCtx, buffer: AbstractBuffer): RenderParams {
904905
val testSetsId = buffer.readLong()
905906
val classUnderTest = buffer.readByteArray()
907+
val projectType = buffer.readString()
906908
val paramNames = buffer.readByteArray()
907909
val generateUtilClassFile = buffer.readBool()
908910
val testFramework = buffer.readString()
@@ -916,12 +918,13 @@ data class RenderParams (
916918
val hangingTestsTimeout = buffer.readLong()
917919
val enableTestsTimeout = buffer.readBool()
918920
val testClassPackageName = buffer.readString()
919-
return RenderParams(testSetsId, classUnderTest, paramNames, generateUtilClassFile, testFramework, mockFramework, codegenLanguage, parameterizedTestSource, staticsMocking, forceStaticMocking, generateWarningsForStaticMocking, runtimeExceptionTestsBehaviour, hangingTestsTimeout, enableTestsTimeout, testClassPackageName)
921+
return RenderParams(testSetsId, classUnderTest, projectType, paramNames, generateUtilClassFile, testFramework, mockFramework, codegenLanguage, parameterizedTestSource, staticsMocking, forceStaticMocking, generateWarningsForStaticMocking, runtimeExceptionTestsBehaviour, hangingTestsTimeout, enableTestsTimeout, testClassPackageName)
920922
}
921923

922924
override fun write(ctx: SerializationCtx, buffer: AbstractBuffer, value: RenderParams) {
923925
buffer.writeLong(value.testSetsId)
924926
buffer.writeByteArray(value.classUnderTest)
927+
buffer.writeString(value.projectType)
925928
buffer.writeByteArray(value.paramNames)
926929
buffer.writeBool(value.generateUtilClassFile)
927930
buffer.writeString(value.testFramework)
@@ -952,6 +955,7 @@ data class RenderParams (
952955

953956
if (testSetsId != other.testSetsId) return false
954957
if (!(classUnderTest contentEquals other.classUnderTest)) return false
958+
if (projectType != other.projectType) return false
955959
if (!(paramNames contentEquals other.paramNames)) return false
956960
if (generateUtilClassFile != other.generateUtilClassFile) return false
957961
if (testFramework != other.testFramework) return false
@@ -973,6 +977,7 @@ data class RenderParams (
973977
var __r = 0
974978
__r = __r*31 + testSetsId.hashCode()
975979
__r = __r*31 + classUnderTest.contentHashCode()
980+
__r = __r*31 + projectType.hashCode()
976981
__r = __r*31 + paramNames.contentHashCode()
977982
__r = __r*31 + generateUtilClassFile.hashCode()
978983
__r = __r*31 + testFramework.hashCode()
@@ -994,6 +999,7 @@ data class RenderParams (
994999
printer.indent {
9951000
print("testSetsId = "); testSetsId.print(printer); println()
9961001
print("classUnderTest = "); classUnderTest.print(printer); println()
1002+
print("projectType = "); projectType.print(printer); println()
9971003
print("paramNames = "); paramNames.print(printer); println()
9981004
print("generateUtilClassFile = "); generateUtilClassFile.print(printer); println()
9991005
print("testFramework = "); testFramework.print(printer); println()
@@ -1016,7 +1022,7 @@ data class RenderParams (
10161022

10171023

10181024
/**
1019-
* #### Generated from [EngineProcessModel.kt:79]
1025+
* #### Generated from [EngineProcessModel.kt:80]
10201026
*/
10211027
data class RenderResult (
10221028
val generatedCode: String,
@@ -1079,7 +1085,7 @@ data class RenderResult (
10791085

10801086

10811087
/**
1082-
* #### Generated from [EngineProcessModel.kt:83]
1088+
* #### Generated from [EngineProcessModel.kt:84]
10831089
*/
10841090
data class SetupContextParams (
10851091
val classpathForUrlsClassloader: List<String>
@@ -1217,7 +1223,7 @@ data class TestGeneratorParams (
12171223

12181224

12191225
/**
1220-
* #### Generated from [EngineProcessModel.kt:105]
1226+
* #### Generated from [EngineProcessModel.kt:106]
12211227
*/
12221228
data class WriteSarifReportArguments (
12231229
val testSetsId: Long,

utbot-intellij-python/src/main/kotlin/org/utbot/intellij/plugin/language/python/PythonDialogWindow.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import com.jetbrains.python.refactoring.classes.PyMemberInfoStorage
1515
import com.jetbrains.python.refactoring.classes.membersManager.PyMemberInfo
1616
import com.jetbrains.python.refactoring.classes.ui.PyMemberSelectionTable
1717
import org.utbot.framework.UtSettings
18+
import org.utbot.framework.codegen.domain.ProjectType
1819
import org.utbot.intellij.plugin.settings.Settings
1920
import java.awt.BorderLayout
2021
import java.util.concurrent.TimeUnit
@@ -171,6 +172,7 @@ class PythonDialogWindow(val model: PythonTestsModel) : DialogWrapper(model.proj
171172
model.testFramework = testFrameworks.item
172173
model.timeout = TimeUnit.SECONDS.toMillis(timeoutSpinnerForTotalTimeout.number.toLong())
173174
model.testSourceRootPath = testSourceFolderField.text
175+
model.projectType = ProjectType.Python
174176

175177
val settings = model.project.service<Settings>()
176178
with(settings) {

utbot-intellij/src/main/kotlin/org/utbot/intellij/plugin/generator/CodeGenerationController.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import com.intellij.ide.fileTemplates.FileTemplateUtil
88
import com.intellij.ide.fileTemplates.JavaTemplateUtil
99
import com.intellij.ide.highlighter.JavaFileType
1010
import com.intellij.openapi.application.ApplicationManager
11-
import com.intellij.openapi.application.readAction
1211
import com.intellij.openapi.application.runReadAction
1312
import com.intellij.openapi.application.runWriteAction
1413
import com.intellij.openapi.command.WriteCommandAction.runWriteCommandAction
@@ -686,6 +685,7 @@ object CodeGenerationController {
686685
proc.render(
687686
testSetsId,
688687
classUnderTest,
688+
model.projectType,
689689
paramNames.toMutableMap(),
690690
generateUtilClassFile = true,
691691
model.testFramework,

utbot-intellij/src/main/kotlin/org/utbot/intellij/plugin/generator/UtTestsDialogProcessor.kt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import org.utbot.framework.CancellationStrategyType.CANCEL_EVERYTHING
2525
import org.utbot.framework.CancellationStrategyType.NONE
2626
import org.utbot.framework.CancellationStrategyType.SAVE_PROCESSED_RESULTS
2727
import org.utbot.framework.UtSettings
28+
import org.utbot.framework.codegen.domain.ProjectType.*
2829
import org.utbot.framework.codegen.domain.ProjectType
2930
import org.utbot.framework.codegen.domain.TypeReplacementApproach
3031
import org.utbot.framework.plugin.api.ClassId
@@ -177,8 +178,10 @@ object UtTestsDialogProcessor {
177178
val staticMockingConfigured = model.staticsMocking.isConfigured
178179

179180
val applicationContext = when (model.projectType) {
180-
ProjectType.PURE_JVM -> ApplicationContext(mockFrameworkInstalled, staticMockingConfigured)
181-
ProjectType.SPRING_APPLICATION -> {
181+
PureJvm,
182+
Python,
183+
JavaScript -> ApplicationContext(mockFrameworkInstalled, staticMockingConfigured)
184+
Spring -> {
182185
val shouldUseImplementors = when (model.typeReplacementApproach) {
183186
TypeReplacementApproach.DO_NOT_REPLACE -> false
184187
TypeReplacementApproach.REPLACE_IF_POSSIBLE -> true

utbot-intellij/src/main/kotlin/org/utbot/intellij/plugin/models/GenerateTestsModel.kt

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import com.intellij.refactoring.util.classMembers.MemberInfo
1818
import org.jetbrains.kotlin.psi.KtFile
1919
import org.utbot.framework.SummariesGenerationType
2020
import org.utbot.framework.UtSettings
21-
import org.utbot.framework.codegen.domain.ProjectType
2221
import org.utbot.framework.codegen.domain.TypeReplacementApproach
2322
import org.utbot.framework.plugin.api.JavaDocCommentStyle
2423
import org.utbot.framework.util.ConflictTriggers
@@ -43,8 +42,6 @@ class GenerateTestsModel(
4342
override var sourceRootHistory = project.service<Settings>().sourceRootHistory
4443
override var codegenLanguage = project.service<Settings>().codegenLanguage
4544

46-
lateinit var projectType: ProjectType
47-
4845
lateinit var testFramework: TestFramework
4946
lateinit var mockStrategy: MockStrategyApi
5047
lateinit var mockFramework: MockFramework

utbot-intellij/src/main/kotlin/org/utbot/intellij/plugin/process/EngineProcess.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,7 @@ class EngineProcess private constructor(val project: Project, private val classN
278278
fun render(
279279
testSetsId: Long,
280280
classUnderTest: ClassId,
281+
projectType: ProjectType,
281282
paramNames: MutableMap<ExecutableId, List<String>>,
282283
generateUtilClassFile: Boolean,
283284
testFramework: TestFramework,
@@ -296,6 +297,7 @@ class EngineProcess private constructor(val project: Project, private val classN
296297
val params = RenderParams(
297298
testSetsId,
298299
kryoHelper.writeObject(classUnderTest),
300+
projectType.toString(),
299301
kryoHelper.writeObject(paramNames),
300302
generateUtilClassFile,
301303
testFramework.id.lowercase(),

utbot-intellij/src/main/kotlin/org/utbot/intellij/plugin/ui/GenerateTestsDialogWindow.kt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -247,9 +247,8 @@ class GenerateTestsDialogWindow(val model: GenerateTestsModel) : DialogWrapper(m
247247
DependencyInjectionFramework.allItems.forEach {
248248
it.isInstalled = findDependencyInjectionLibrary(model.project, model.testModule, it) != null
249249
}
250-
model.projectType =
251-
if (SpringBeans.isInstalled) ProjectType.SPRING_APPLICATION
252-
else ProjectType.PURE_JVM
250+
model.projectType = if (SpringBeans.isInstalled) ProjectType.Spring else ProjectType.PureJvm
251+
253252
StaticsMocking.allItems.forEach {
254253
it.isConfigured = staticsMockingConfigured()
255254
}

utbot-js/src/main/kotlin/codegen/JsCodeGenerator.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ class JsCodeGenerator(
3333
) {
3434
private var context: CgContext = CgContext(
3535
classUnderTest = classUnderTest,
36+
projectType = ProjectType.JavaScript,
3637
paramNames = paramNames,
3738
testFramework = testFramework,
3839
mockFramework = MockFramework.MOCKITO,

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import org.utbot.framework.codegen.CodeGeneratorResult
55
import org.utbot.framework.codegen.domain.ForceStaticMocking
66
import org.utbot.framework.codegen.domain.HangingTestsTimeout
77
import org.utbot.framework.codegen.domain.ParametrizedTestSource
8+
import org.utbot.framework.codegen.domain.ProjectType
89
import org.utbot.framework.codegen.domain.RuntimeExceptionTestsBehaviour
910
import org.utbot.framework.codegen.domain.StaticsMocking
1011
import org.utbot.framework.codegen.domain.TestFramework
@@ -42,7 +43,6 @@ import org.utbot.python.newtyping.pythonAnyType
4243
import org.utbot.python.newtyping.pythonModules
4344
import org.utbot.python.newtyping.pythonTypeRepresentation
4445
import org.utbot.python.framework.codegen.toPythonRawString
45-
import org.utbot.python.newtyping.pythonDescription
4646
import org.utbot.python.newtyping.pythonName
4747

4848
class PythonCodeGenerator(
@@ -60,6 +60,7 @@ class PythonCodeGenerator(
6060
testClassPackageName: String = classUnderTest.packageName
6161
) : CodeGenerator(
6262
classUnderTest = classUnderTest,
63+
projectType = ProjectType.Python,
6364
paramNames = paramNames,
6465
generateUtilClassFile = true,
6566
testFramework = testFramework,

0 commit comments

Comments
 (0)