From a0c2f8f83657af2ee665327811f6eaa387f1bcad Mon Sep 17 00:00:00 2001 From: Egor Kulikov Date: Mon, 3 Apr 2023 18:45:48 +0300 Subject: [PATCH 1/4] CgContext is not a data class!!! --- .../codegen/domain/context/CgContext.kt | 3 +- .../framework/codegen/tree/CgComponents.kt | 40 ++++++++++++------- .../tree/CgSpringVariableConstructor.kt | 3 -- .../codegen/tree/CgVariableConstructor.kt | 2 +- 4 files changed, 27 insertions(+), 21 deletions(-) diff --git a/utbot-framework/src/main/kotlin/org/utbot/framework/codegen/domain/context/CgContext.kt b/utbot-framework/src/main/kotlin/org/utbot/framework/codegen/domain/context/CgContext.kt index 09cd397c80..a966ca3f2e 100644 --- a/utbot-framework/src/main/kotlin/org/utbot/framework/codegen/domain/context/CgContext.kt +++ b/utbot-framework/src/main/kotlin/org/utbot/framework/codegen/domain/context/CgContext.kt @@ -445,7 +445,7 @@ interface CgContextOwner { /** * Context with current code generation info */ -data class CgContext( +class CgContext( override val classUnderTest: ClassId, override val projectType: ProjectType, val generateUtilClassFile: Boolean = false, @@ -594,7 +594,6 @@ data class CgContext( mockFrameworkUsed = false } - override var valueByModel: IdentityHashMap = IdentityHashMap() override var valueByModelId: MutableMap = mutableMapOf() diff --git a/utbot-framework/src/main/kotlin/org/utbot/framework/codegen/tree/CgComponents.kt b/utbot-framework/src/main/kotlin/org/utbot/framework/codegen/tree/CgComponents.kt index 99357aed5a..8940a73bdd 100644 --- a/utbot-framework/src/main/kotlin/org/utbot/framework/codegen/tree/CgComponents.kt +++ b/utbot-framework/src/main/kotlin/org/utbot/framework/codegen/tree/CgComponents.kt @@ -5,6 +5,7 @@ import org.utbot.framework.codegen.services.CgNameGenerator import org.utbot.framework.codegen.services.access.CgCallableAccessManager import org.utbot.framework.codegen.services.framework.MockFrameworkManager import org.utbot.framework.codegen.services.framework.TestFrameworkManager +import java.util.* object CgComponents { @@ -23,33 +24,42 @@ object CgComponents { methodConstructors.clear() } - private val nameGenerators: MutableMap = mutableMapOf() - private val statementConstructors: MutableMap = mutableMapOf() - private val callableAccessManagers: MutableMap = mutableMapOf() - private val testFrameworkManagers: MutableMap = mutableMapOf() - private val mockFrameworkManagers: MutableMap = mutableMapOf() + private val nameGenerators: IdentityHashMap = IdentityHashMap() - private val variableConstructors: MutableMap = mutableMapOf() - private val methodConstructors: MutableMap = mutableMapOf() + private val callableAccessManagers: IdentityHashMap = IdentityHashMap() + private val testFrameworkManagers: IdentityHashMap = IdentityHashMap() + private val mockFrameworkManagers: IdentityHashMap = IdentityHashMap() - fun getNameGeneratorBy(context: CgContext) = nameGenerators.getOrPut(context) { + private val statementConstructors: IdentityHashMap = IdentityHashMap() + private val variableConstructors: IdentityHashMap = IdentityHashMap() + private val methodConstructors: IdentityHashMap = IdentityHashMap() + + fun getNameGeneratorBy(context: CgContext): CgNameGenerator = nameGenerators.getOrPut(context) { context.cgLanguageAssistant.getNameGeneratorBy(context) } - fun getCallableAccessManagerBy(context: CgContext) = callableAccessManagers.getOrPut(context) { + + fun getCallableAccessManagerBy(context: CgContext): CgCallableAccessManager = callableAccessManagers.getOrPut(context) { context.cgLanguageAssistant.getCallableAccessManagerBy(context) } - fun getStatementConstructorBy(context: CgContext) = statementConstructors.getOrPut(context) { + + fun getStatementConstructorBy(context: CgContext): CgStatementConstructor = statementConstructors.getOrPut(context) { context.cgLanguageAssistant.getStatementConstructorBy(context) } - fun getTestFrameworkManagerBy(context: CgContext) = - testFrameworkManagers.getOrDefault(context, context.cgLanguageAssistant.getLanguageTestFrameworkManager().managerByFramework(context)) + fun getTestFrameworkManagerBy(context: CgContext): TestFrameworkManager = + testFrameworkManagers.getOrDefault( + context, + context.cgLanguageAssistant.getLanguageTestFrameworkManager().managerByFramework(context) + ) - fun getMockFrameworkManagerBy(context: CgContext) = mockFrameworkManagers.getOrPut(context) { MockFrameworkManager(context) } - fun getVariableConstructorBy(context: CgContext) = variableConstructors.getOrPut(context) { + fun getMockFrameworkManagerBy(context: CgContext): MockFrameworkManager = + mockFrameworkManagers.getOrPut(context) { MockFrameworkManager(context) } + + fun getVariableConstructorBy(context: CgContext): CgVariableConstructor = variableConstructors.getOrPut(context) { context.cgLanguageAssistant.getVariableConstructorBy(context) } - fun getMethodConstructorBy(context: CgContext) = methodConstructors.getOrPut(context) { + + fun getMethodConstructorBy(context: CgContext): CgMethodConstructor = methodConstructors.getOrPut(context) { context.cgLanguageAssistant.getMethodConstructorBy(context) } } \ No newline at end of file diff --git a/utbot-framework/src/main/kotlin/org/utbot/framework/codegen/tree/CgSpringVariableConstructor.kt b/utbot-framework/src/main/kotlin/org/utbot/framework/codegen/tree/CgSpringVariableConstructor.kt index 347f009e8a..7189f1b4ab 100644 --- a/utbot-framework/src/main/kotlin/org/utbot/framework/codegen/tree/CgSpringVariableConstructor.kt +++ b/utbot-framework/src/main/kotlin/org/utbot/framework/codegen/tree/CgSpringVariableConstructor.kt @@ -1,6 +1,5 @@ package org.utbot.framework.codegen.tree -import com.jetbrains.rd.util.firstOrNull import org.utbot.framework.codegen.domain.context.CgContext import org.utbot.framework.codegen.domain.models.CgValue import org.utbot.framework.codegen.domain.models.CgVariable @@ -13,8 +12,6 @@ class CgSpringVariableConstructor(context: CgContext) : CgVariableConstructor(co val injectedMocksModelsVariables: MutableMap, CgValue> = mutableMapOf() val mockedModelsVariables: MutableMap, CgValue> = mutableMapOf() - private val mockFrameworkManager = CgComponents.getMockFrameworkManagerBy(context) - override fun getOrCreateVariable(model: UtModel, name: String?): CgValue { val alreadyCreatedInjectMocks = findCgValueByModel(model, injectedMocksModelsVariables) if (alreadyCreatedInjectMocks != null) { diff --git a/utbot-framework/src/main/kotlin/org/utbot/framework/codegen/tree/CgVariableConstructor.kt b/utbot-framework/src/main/kotlin/org/utbot/framework/codegen/tree/CgVariableConstructor.kt index 465d90471f..970c951cfb 100644 --- a/utbot-framework/src/main/kotlin/org/utbot/framework/codegen/tree/CgVariableConstructor.kt +++ b/utbot-framework/src/main/kotlin/org/utbot/framework/codegen/tree/CgVariableConstructor.kt @@ -75,7 +75,7 @@ open class CgVariableConstructor(val context: CgContext) : CgStatementConstructor by getStatementConstructorBy(context) { private val nameGenerator = getNameGeneratorBy(context) - private val mockFrameworkManager = getMockFrameworkManagerBy(context) + protected val mockFrameworkManager = getMockFrameworkManagerBy(context) /** * Take already created CgValue or construct either a new [CgVariable] or new [CgLiteral] for the given model. From 24f002df72eb12517e9d610853bcb7a0bf6d69f9 Mon Sep 17 00:00:00 2001 From: Egor Kulikov Date: Mon, 3 Apr 2023 19:06:16 +0300 Subject: [PATCH 2/4] Avoid copy method in CgContext --- .../org/utbot/framework/codegen/CodeGenerator.kt | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/utbot-framework/src/main/kotlin/org/utbot/framework/codegen/CodeGenerator.kt b/utbot-framework/src/main/kotlin/org/utbot/framework/codegen/CodeGenerator.kt index e0769f6cc8..597a0f6cd7 100644 --- a/utbot-framework/src/main/kotlin/org/utbot/framework/codegen/CodeGenerator.kt +++ b/utbot-framework/src/main/kotlin/org/utbot/framework/codegen/CodeGenerator.kt @@ -141,13 +141,13 @@ open class CodeGenerator( fun withCustomContext(testClassCustomName: String? = null, block: () -> R): R { val prevContext = context return try { - context = prevContext.copy( - shouldOptimizeImports = true, - testClassCustomName = testClassCustomName - ) + context.shouldOptimizeImports = true + context.testClassCustomName = testClassCustomName + block() } finally { - context = prevContext + context.shouldOptimizeImports = prevContext.shouldOptimizeImports + context.testClassCustomName = prevContext.testClassCustomName } } } From be9c3ca32df6546f77a6c6302fa372436a124a31 Mon Sep 17 00:00:00 2001 From: Egor Kulikov Date: Tue, 4 Apr 2023 09:49:42 +0300 Subject: [PATCH 3/4] Fix bug in withCustomContext --- .../kotlin/org/utbot/framework/codegen/CodeGenerator.kt | 3 +-- utbot-js/src/main/kotlin/codegen/JsCodeGenerator.kt | 7 +++---- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/utbot-framework/src/main/kotlin/org/utbot/framework/codegen/CodeGenerator.kt b/utbot-framework/src/main/kotlin/org/utbot/framework/codegen/CodeGenerator.kt index 597a0f6cd7..1c23212df9 100644 --- a/utbot-framework/src/main/kotlin/org/utbot/framework/codegen/CodeGenerator.kt +++ b/utbot-framework/src/main/kotlin/org/utbot/framework/codegen/CodeGenerator.kt @@ -146,8 +146,7 @@ open class CodeGenerator( block() } finally { - context.shouldOptimizeImports = prevContext.shouldOptimizeImports - context.testClassCustomName = prevContext.testClassCustomName + context = prevContext } } } diff --git a/utbot-js/src/main/kotlin/codegen/JsCodeGenerator.kt b/utbot-js/src/main/kotlin/codegen/JsCodeGenerator.kt index cecd57dbac..6fd3136516 100644 --- a/utbot-js/src/main/kotlin/codegen/JsCodeGenerator.kt +++ b/utbot-js/src/main/kotlin/codegen/JsCodeGenerator.kt @@ -64,10 +64,9 @@ class JsCodeGenerator( private fun withCustomContext(testClassCustomName: String? = null, block: () -> R): R { val prevContext = context return try { - context = prevContext.copy( - shouldOptimizeImports = true, - testClassCustomName = testClassCustomName - ) + context.shouldOptimizeImports = true + context.testClassCustomName = testClassCustomName + block() } finally { context = prevContext From 544e6baa649a4534b457c0aeb64f2331858101af Mon Sep 17 00:00:00 2001 From: Egor Kulikov Date: Tue, 4 Apr 2023 11:56:18 +0300 Subject: [PATCH 4/4] Introduce customCopy --- .../utbot/framework/codegen/CodeGenerator.kt | 4 +- .../codegen/domain/context/CgContext.kt | 43 +++++++++++++++++++ .../main/kotlin/codegen/JsCodeGenerator.kt | 4 +- 3 files changed, 45 insertions(+), 6 deletions(-) diff --git a/utbot-framework/src/main/kotlin/org/utbot/framework/codegen/CodeGenerator.kt b/utbot-framework/src/main/kotlin/org/utbot/framework/codegen/CodeGenerator.kt index 1c23212df9..72cbd385a5 100644 --- a/utbot-framework/src/main/kotlin/org/utbot/framework/codegen/CodeGenerator.kt +++ b/utbot-framework/src/main/kotlin/org/utbot/framework/codegen/CodeGenerator.kt @@ -141,9 +141,7 @@ open class CodeGenerator( fun withCustomContext(testClassCustomName: String? = null, block: () -> R): R { val prevContext = context return try { - context.shouldOptimizeImports = true - context.testClassCustomName = testClassCustomName - + context = prevContext.customCopy(shouldOptimizeImports = true, testClassCustomName = testClassCustomName) block() } finally { context = prevContext diff --git a/utbot-framework/src/main/kotlin/org/utbot/framework/codegen/domain/context/CgContext.kt b/utbot-framework/src/main/kotlin/org/utbot/framework/codegen/domain/context/CgContext.kt index a966ca3f2e..18c54e64ec 100644 --- a/utbot-framework/src/main/kotlin/org/utbot/framework/codegen/domain/context/CgContext.kt +++ b/utbot-framework/src/main/kotlin/org/utbot/framework/codegen/domain/context/CgContext.kt @@ -604,4 +604,47 @@ class CgContext( override val utilMethodsUsed: Boolean get() = requiredUtilMethods.isNotEmpty() + + fun customCopy(shouldOptimizeImports: Boolean, testClassCustomName: String?) = CgContext( + shouldOptimizeImports = shouldOptimizeImports, + testClassCustomName = testClassCustomName, + + classUnderTest = this.classUnderTest, + projectType = this.projectType, + generateUtilClassFile = this.generateUtilClassFile, + currentExecutable = this.currentExecutable, + collectedExceptions =this.collectedExceptions, + collectedMethodAnnotations = this.collectedMethodAnnotations, + collectedImports = this.collectedImports, + importedStaticMethods = this.importedStaticMethods, + importedClasses = this.importedClasses, + requiredUtilMethods = this.requiredUtilMethods, + testMethods = this.testMethods, + existingMethodNames = this.existingMethodNames, + prevStaticFieldValues = this.prevStaticFieldValues, + paramNames = this.paramNames, + currentExecution = this.currentExecution, + testFramework = this.testFramework, + mockFramework = this.mockFramework, + staticsMocking = this.staticsMocking, + forceStaticMocking = this.forceStaticMocking, + generateWarningsForStaticMocking = this.generateWarningsForStaticMocking, + codegenLanguage = this.codegenLanguage, + cgLanguageAssistant = this.cgLanguageAssistant, + parametrizedTestSource = this.parametrizedTestSource, + mockFrameworkUsed = this.mockFrameworkUsed, + currentBlock = this.currentBlock, + existingVariableNames = this.existingVariableNames, + declaredClassRefs = this.declaredClassRefs, + declaredExecutableRefs = this.declaredExecutableRefs, + declaredFieldRefs = this.declaredFieldRefs, + thisInstance = this.thisInstance, + methodArguments = this.methodArguments, + codeGenerationErrors = this.codeGenerationErrors, + testClassPackageName = this.testClassPackageName, + runtimeExceptionTestsBehaviour = this.runtimeExceptionTestsBehaviour, + hangingTestsTimeout = this.hangingTestsTimeout, + enableTestsTimeout = this.enableTestsTimeout, + containsReflectiveCall = this.containsReflectiveCall, + ) } \ No newline at end of file diff --git a/utbot-js/src/main/kotlin/codegen/JsCodeGenerator.kt b/utbot-js/src/main/kotlin/codegen/JsCodeGenerator.kt index 6fd3136516..099ca3ccb6 100644 --- a/utbot-js/src/main/kotlin/codegen/JsCodeGenerator.kt +++ b/utbot-js/src/main/kotlin/codegen/JsCodeGenerator.kt @@ -64,9 +64,7 @@ class JsCodeGenerator( private fun withCustomContext(testClassCustomName: String? = null, block: () -> R): R { val prevContext = context return try { - context.shouldOptimizeImports = true - context.testClassCustomName = testClassCustomName - + context = prevContext.customCopy(shouldOptimizeImports = true, testClassCustomName = testClassCustomName) block() } finally { context = prevContext