Skip to content

Commit be95266

Browse files
CgContext is not a data class #2060 (#2091)
1 parent c57cc4b commit be95266

File tree

6 files changed

+72
-29
lines changed

6 files changed

+72
-29
lines changed

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -141,10 +141,7 @@ open class CodeGenerator(
141141
fun <R> withCustomContext(testClassCustomName: String? = null, block: () -> R): R {
142142
val prevContext = context
143143
return try {
144-
context = prevContext.copy(
145-
shouldOptimizeImports = true,
146-
testClassCustomName = testClassCustomName
147-
)
144+
context = prevContext.customCopy(shouldOptimizeImports = true, testClassCustomName = testClassCustomName)
148145
block()
149146
} finally {
150147
context = prevContext

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

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,7 @@ interface CgContextOwner {
445445
/**
446446
* Context with current code generation info
447447
*/
448-
data class CgContext(
448+
class CgContext(
449449
override val classUnderTest: ClassId,
450450
override val projectType: ProjectType,
451451
val generateUtilClassFile: Boolean = false,
@@ -594,7 +594,6 @@ data class CgContext(
594594
mockFrameworkUsed = false
595595
}
596596

597-
598597
override var valueByModel: IdentityHashMap<UtModel, CgValue> = IdentityHashMap()
599598

600599
override var valueByModelId: MutableMap<ModelId, CgValue> = mutableMapOf()
@@ -605,4 +604,47 @@ data class CgContext(
605604

606605
override val utilMethodsUsed: Boolean
607606
get() = requiredUtilMethods.isNotEmpty()
607+
608+
fun customCopy(shouldOptimizeImports: Boolean, testClassCustomName: String?) = CgContext(
609+
shouldOptimizeImports = shouldOptimizeImports,
610+
testClassCustomName = testClassCustomName,
611+
612+
classUnderTest = this.classUnderTest,
613+
projectType = this.projectType,
614+
generateUtilClassFile = this.generateUtilClassFile,
615+
currentExecutable = this.currentExecutable,
616+
collectedExceptions =this.collectedExceptions,
617+
collectedMethodAnnotations = this.collectedMethodAnnotations,
618+
collectedImports = this.collectedImports,
619+
importedStaticMethods = this.importedStaticMethods,
620+
importedClasses = this.importedClasses,
621+
requiredUtilMethods = this.requiredUtilMethods,
622+
testMethods = this.testMethods,
623+
existingMethodNames = this.existingMethodNames,
624+
prevStaticFieldValues = this.prevStaticFieldValues,
625+
paramNames = this.paramNames,
626+
currentExecution = this.currentExecution,
627+
testFramework = this.testFramework,
628+
mockFramework = this.mockFramework,
629+
staticsMocking = this.staticsMocking,
630+
forceStaticMocking = this.forceStaticMocking,
631+
generateWarningsForStaticMocking = this.generateWarningsForStaticMocking,
632+
codegenLanguage = this.codegenLanguage,
633+
cgLanguageAssistant = this.cgLanguageAssistant,
634+
parametrizedTestSource = this.parametrizedTestSource,
635+
mockFrameworkUsed = this.mockFrameworkUsed,
636+
currentBlock = this.currentBlock,
637+
existingVariableNames = this.existingVariableNames,
638+
declaredClassRefs = this.declaredClassRefs,
639+
declaredExecutableRefs = this.declaredExecutableRefs,
640+
declaredFieldRefs = this.declaredFieldRefs,
641+
thisInstance = this.thisInstance,
642+
methodArguments = this.methodArguments,
643+
codeGenerationErrors = this.codeGenerationErrors,
644+
testClassPackageName = this.testClassPackageName,
645+
runtimeExceptionTestsBehaviour = this.runtimeExceptionTestsBehaviour,
646+
hangingTestsTimeout = this.hangingTestsTimeout,
647+
enableTestsTimeout = this.enableTestsTimeout,
648+
containsReflectiveCall = this.containsReflectiveCall,
649+
)
608650
}

utbot-framework/src/main/kotlin/org/utbot/framework/codegen/tree/CgComponents.kt

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import org.utbot.framework.codegen.services.CgNameGenerator
55
import org.utbot.framework.codegen.services.access.CgCallableAccessManager
66
import org.utbot.framework.codegen.services.framework.MockFrameworkManager
77
import org.utbot.framework.codegen.services.framework.TestFrameworkManager
8+
import java.util.*
89

910
object CgComponents {
1011

@@ -23,33 +24,42 @@ object CgComponents {
2324
methodConstructors.clear()
2425
}
2526

26-
private val nameGenerators: MutableMap<CgContext, CgNameGenerator> = mutableMapOf()
27-
private val statementConstructors: MutableMap<CgContext, CgStatementConstructor> = mutableMapOf()
28-
private val callableAccessManagers: MutableMap<CgContext, CgCallableAccessManager> = mutableMapOf()
29-
private val testFrameworkManagers: MutableMap<CgContext, TestFrameworkManager> = mutableMapOf()
30-
private val mockFrameworkManagers: MutableMap<CgContext, MockFrameworkManager> = mutableMapOf()
27+
private val nameGenerators: IdentityHashMap<CgContext, CgNameGenerator> = IdentityHashMap()
3128

32-
private val variableConstructors: MutableMap<CgContext, CgVariableConstructor> = mutableMapOf()
33-
private val methodConstructors: MutableMap<CgContext, CgMethodConstructor> = mutableMapOf()
29+
private val callableAccessManagers: IdentityHashMap<CgContext, CgCallableAccessManager> = IdentityHashMap()
30+
private val testFrameworkManagers: IdentityHashMap<CgContext, TestFrameworkManager> = IdentityHashMap()
31+
private val mockFrameworkManagers: IdentityHashMap<CgContext, MockFrameworkManager> = IdentityHashMap()
3432

35-
fun getNameGeneratorBy(context: CgContext) = nameGenerators.getOrPut(context) {
33+
private val statementConstructors: IdentityHashMap<CgContext, CgStatementConstructor> = IdentityHashMap()
34+
private val variableConstructors: IdentityHashMap<CgContext, CgVariableConstructor> = IdentityHashMap()
35+
private val methodConstructors: IdentityHashMap<CgContext, CgMethodConstructor> = IdentityHashMap()
36+
37+
fun getNameGeneratorBy(context: CgContext): CgNameGenerator = nameGenerators.getOrPut(context) {
3638
context.cgLanguageAssistant.getNameGeneratorBy(context)
3739
}
38-
fun getCallableAccessManagerBy(context: CgContext) = callableAccessManagers.getOrPut(context) {
40+
41+
fun getCallableAccessManagerBy(context: CgContext): CgCallableAccessManager = callableAccessManagers.getOrPut(context) {
3942
context.cgLanguageAssistant.getCallableAccessManagerBy(context)
4043
}
41-
fun getStatementConstructorBy(context: CgContext) = statementConstructors.getOrPut(context) {
44+
45+
fun getStatementConstructorBy(context: CgContext): CgStatementConstructor = statementConstructors.getOrPut(context) {
4246
context.cgLanguageAssistant.getStatementConstructorBy(context)
4347
}
4448

45-
fun getTestFrameworkManagerBy(context: CgContext) =
46-
testFrameworkManagers.getOrDefault(context, context.cgLanguageAssistant.getLanguageTestFrameworkManager().managerByFramework(context))
49+
fun getTestFrameworkManagerBy(context: CgContext): TestFrameworkManager =
50+
testFrameworkManagers.getOrDefault(
51+
context,
52+
context.cgLanguageAssistant.getLanguageTestFrameworkManager().managerByFramework(context)
53+
)
4754

48-
fun getMockFrameworkManagerBy(context: CgContext) = mockFrameworkManagers.getOrPut(context) { MockFrameworkManager(context) }
49-
fun getVariableConstructorBy(context: CgContext) = variableConstructors.getOrPut(context) {
55+
fun getMockFrameworkManagerBy(context: CgContext): MockFrameworkManager =
56+
mockFrameworkManagers.getOrPut(context) { MockFrameworkManager(context) }
57+
58+
fun getVariableConstructorBy(context: CgContext): CgVariableConstructor = variableConstructors.getOrPut(context) {
5059
context.cgLanguageAssistant.getVariableConstructorBy(context)
5160
}
52-
fun getMethodConstructorBy(context: CgContext) = methodConstructors.getOrPut(context) {
61+
62+
fun getMethodConstructorBy(context: CgContext): CgMethodConstructor = methodConstructors.getOrPut(context) {
5363
context.cgLanguageAssistant.getMethodConstructorBy(context)
5464
}
5565
}

utbot-framework/src/main/kotlin/org/utbot/framework/codegen/tree/CgSpringVariableConstructor.kt

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

3-
import com.jetbrains.rd.util.firstOrNull
43
import org.utbot.framework.codegen.domain.context.CgContext
54
import org.utbot.framework.codegen.domain.models.CgValue
65
import org.utbot.framework.codegen.domain.models.CgVariable
@@ -13,8 +12,6 @@ class CgSpringVariableConstructor(context: CgContext) : CgVariableConstructor(co
1312
val injectedMocksModelsVariables: MutableMap<Set<UtModel>, CgValue> = mutableMapOf()
1413
val mockedModelsVariables: MutableMap<Set<UtModel>, CgValue> = mutableMapOf()
1514

16-
private val mockFrameworkManager = CgComponents.getMockFrameworkManagerBy(context)
17-
1815
override fun getOrCreateVariable(model: UtModel, name: String?): CgValue {
1916
val alreadyCreatedInjectMocks = findCgValueByModel(model, injectedMocksModelsVariables)
2017
if (alreadyCreatedInjectMocks != null) {

utbot-framework/src/main/kotlin/org/utbot/framework/codegen/tree/CgVariableConstructor.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ open class CgVariableConstructor(val context: CgContext) :
7575
CgStatementConstructor by getStatementConstructorBy(context) {
7676

7777
private val nameGenerator = getNameGeneratorBy(context)
78-
private val mockFrameworkManager = getMockFrameworkManagerBy(context)
78+
protected val mockFrameworkManager = getMockFrameworkManagerBy(context)
7979

8080
/**
8181
* Take already created CgValue or construct either a new [CgVariable] or new [CgLiteral] for the given model.

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,7 @@ class JsCodeGenerator(
6464
private fun <R> withCustomContext(testClassCustomName: String? = null, block: () -> R): R {
6565
val prevContext = context
6666
return try {
67-
context = prevContext.copy(
68-
shouldOptimizeImports = true,
69-
testClassCustomName = testClassCustomName
70-
)
67+
context = prevContext.customCopy(shouldOptimizeImports = true, testClassCustomName = testClassCustomName)
7168
block()
7269
} finally {
7370
context = prevContext

0 commit comments

Comments
 (0)