From adb53c8c3ee99b4e8a2f99b05682de3a49b7e161 Mon Sep 17 00:00:00 2001 From: Egor Kulikov Date: Thu, 10 Nov 2022 10:46:35 +0300 Subject: [PATCH] Refactor CgClassFile, remove report from it --- .../utbot/framework/codegen/model/CodeGenerator.kt | 6 ++++-- .../model/constructor/tree/CgTestClassConstructor.kt | 11 +++++------ .../utbot/framework/codegen/model/tree/Builders.kt | 10 ---------- .../utbot/framework/codegen/model/tree/CgElement.kt | 7 ------- utbot-js/src/main/kotlin/codegen/JsCodeGenerator.kt | 3 +-- .../framework/codegen/model/PythonCodeGenerator.kt | 6 ++++-- .../constructor/tree/PythonCgTestClassConstructor.kt | 9 ++++----- 7 files changed, 18 insertions(+), 34 deletions(-) diff --git a/utbot-framework/src/main/kotlin/org/utbot/framework/codegen/model/CodeGenerator.kt b/utbot-framework/src/main/kotlin/org/utbot/framework/codegen/model/CodeGenerator.kt index 0fe271fead..7b4c14e0c8 100644 --- a/utbot-framework/src/main/kotlin/org/utbot/framework/codegen/model/CodeGenerator.kt +++ b/utbot-framework/src/main/kotlin/org/utbot/framework/codegen/model/CodeGenerator.kt @@ -81,11 +81,13 @@ open class CodeGenerator( ): CodeGeneratorResult = withCustomContext(testClassCustomName) { context.withTestClassFileScope { val testClassModel = TestClassModel.fromTestSets(classUnderTest, cgTestSets) - val testClassFile = CgTestClassConstructor(context).construct(testClassModel) + val cgClassConstructor = CgTestClassConstructor(context) + val testClassFile = cgClassConstructor.construct(testClassModel) + CodeGeneratorResult( generatedCode = renderClassFile(testClassFile), utilClassKind = UtilClassKind.fromCgContextOrNull(context), - testsGenerationReport = testClassFile.testsGenerationReport + testsGenerationReport = cgClassConstructor.testsGenerationReport ) } } diff --git a/utbot-framework/src/main/kotlin/org/utbot/framework/codegen/model/constructor/tree/CgTestClassConstructor.kt b/utbot-framework/src/main/kotlin/org/utbot/framework/codegen/model/constructor/tree/CgTestClassConstructor.kt index 85d1b16168..7da4a464fc 100644 --- a/utbot-framework/src/main/kotlin/org/utbot/framework/codegen/model/constructor/tree/CgTestClassConstructor.kt +++ b/utbot-framework/src/main/kotlin/org/utbot/framework/codegen/model/constructor/tree/CgTestClassConstructor.kt @@ -21,8 +21,8 @@ import org.utbot.framework.codegen.model.tree.CgRegion import org.utbot.framework.codegen.model.tree.CgSimpleRegion import org.utbot.framework.codegen.model.tree.CgStaticsRegion import org.utbot.framework.codegen.model.tree.CgClass +import org.utbot.framework.codegen.model.tree.CgClassFile import org.utbot.framework.codegen.model.tree.CgRealNestedClassesRegion -import org.utbot.framework.codegen.model.tree.CgTestClassFile import org.utbot.framework.codegen.model.tree.CgTestMethod import org.utbot.framework.codegen.model.tree.CgTestMethodCluster import org.utbot.framework.codegen.model.tree.CgTripleSlashMultilineComment @@ -30,7 +30,7 @@ import org.utbot.framework.codegen.model.tree.CgUtilEntity import org.utbot.framework.codegen.model.tree.CgUtilMethod import org.utbot.framework.codegen.model.tree.buildClass import org.utbot.framework.codegen.model.tree.buildClassBody -import org.utbot.framework.codegen.model.tree.buildTestClassFile +import org.utbot.framework.codegen.model.tree.buildClassFile import org.utbot.framework.codegen.model.visitor.importUtilMethodDependencies import org.utbot.framework.plugin.api.ClassId import org.utbot.framework.plugin.api.MethodId @@ -53,16 +53,15 @@ open class CgTestClassConstructor(val context: CgContext) : private val nameGenerator = getNameGeneratorBy(context) private val testFrameworkManager = getTestFrameworkManagerBy(context) - protected val testsGenerationReport: TestsGenerationReport = TestsGenerationReport() + val testsGenerationReport = TestsGenerationReport() /** * Given a testClass model constructs CgTestClass */ - open fun construct(testClassModel: TestClassModel): CgTestClassFile { - return buildTestClassFile { + open fun construct(testClassModel: TestClassModel): CgClassFile { + return buildClassFile { this.declaredClass = withTestClassScope { constructTestClass(testClassModel) } imports += context.collectedImports - testsGenerationReport = this@CgTestClassConstructor.testsGenerationReport } } diff --git a/utbot-framework/src/main/kotlin/org/utbot/framework/codegen/model/tree/Builders.kt b/utbot-framework/src/main/kotlin/org/utbot/framework/codegen/model/tree/Builders.kt index f95ca886ad..d62a5dce05 100644 --- a/utbot-framework/src/main/kotlin/org/utbot/framework/codegen/model/tree/Builders.kt +++ b/utbot-framework/src/main/kotlin/org/utbot/framework/codegen/model/tree/Builders.kt @@ -21,16 +21,6 @@ class CgClassFileBuilder : CgBuilder { fun buildClassFile(init: CgClassFileBuilder.() -> Unit) = CgClassFileBuilder().apply(init).build() -class CgTestClassFileBuilder : CgBuilder { - val imports: MutableList = mutableListOf() - lateinit var declaredClass: CgClass - lateinit var testsGenerationReport: TestsGenerationReport - - override fun build() = CgTestClassFile(imports, declaredClass, testsGenerationReport) -} - -fun buildTestClassFile(init: CgTestClassFileBuilder.() -> Unit) = CgTestClassFileBuilder().apply(init).build() - class CgClassBuilder : CgBuilder { lateinit var id: ClassId val annotations: MutableList = mutableListOf() diff --git a/utbot-framework/src/main/kotlin/org/utbot/framework/codegen/model/tree/CgElement.kt b/utbot-framework/src/main/kotlin/org/utbot/framework/codegen/model/tree/CgElement.kt index b52cd625b7..6062bf4cda 100644 --- a/utbot-framework/src/main/kotlin/org/utbot/framework/codegen/model/tree/CgElement.kt +++ b/utbot-framework/src/main/kotlin/org/utbot/framework/codegen/model/tree/CgElement.kt @@ -36,7 +36,6 @@ interface CgElement { // TODO: order of cases is important here due to inheritance between some of the element types fun accept(visitor: CgVisitor): R = visitor.run { when (val element = this@CgElement) { - is CgTestClassFile -> visit(element) is CgClassFile -> visit(element) is CgClass -> visit(element) is CgClassBody -> visit(element) @@ -125,12 +124,6 @@ open class CgClassFile( open val declaredClass: CgClass, ): CgElement -data class CgTestClassFile( - override val imports: List, - override val declaredClass: CgClass, - val testsGenerationReport: TestsGenerationReport -) : CgClassFile(imports, declaredClass) - class CgClass( val id: ClassId, val annotations: List, diff --git a/utbot-js/src/main/kotlin/codegen/JsCodeGenerator.kt b/utbot-js/src/main/kotlin/codegen/JsCodeGenerator.kt index a5c09204dc..1c4b3623e7 100644 --- a/utbot-js/src/main/kotlin/codegen/JsCodeGenerator.kt +++ b/utbot-js/src/main/kotlin/codegen/JsCodeGenerator.kt @@ -14,7 +14,6 @@ import org.utbot.framework.codegen.model.constructor.CgMethodTestSet import org.utbot.framework.codegen.model.constructor.TestClassModel import org.utbot.framework.codegen.model.constructor.context.CgContext import org.utbot.framework.codegen.model.constructor.tree.CgTestClassConstructor -import org.utbot.framework.codegen.model.tree.CgTestClassFile import org.utbot.framework.codegen.model.visitor.CgAbstractRenderer import org.utbot.framework.plugin.api.CodegenLanguage import org.utbot.framework.plugin.api.ExecutableId @@ -78,7 +77,7 @@ class JsCodeGenerator( } } - private fun renderClassFile(file: CgTestClassFile): String { + private fun renderClassFile(file: CgClassFile): String { val renderer = CgAbstractRenderer.makeRenderer(context) file.accept(renderer) return renderer.toString() diff --git a/utbot-python/src/main/kotlin/org/utbot/python/framework/codegen/model/PythonCodeGenerator.kt b/utbot-python/src/main/kotlin/org/utbot/python/framework/codegen/model/PythonCodeGenerator.kt index 33aec599b1..21963b35eb 100644 --- a/utbot-python/src/main/kotlin/org/utbot/python/framework/codegen/model/PythonCodeGenerator.kt +++ b/utbot-python/src/main/kotlin/org/utbot/python/framework/codegen/model/PythonCodeGenerator.kt @@ -66,8 +66,10 @@ class PythonCodeGenerator( context.withTestClassFileScope { val testClassModel = TestClassModel(classUnderTest, cgTestSets) context.collectedImports.addAll(importModules) - val testClassFile = PythonCgTestClassConstructor(context).construct(testClassModel) - CodeGeneratorResult(renderClassFile(testClassFile), testClassFile.testsGenerationReport) + val cgTestClassConstructor = PythonCgTestClassConstructor(context) + + val testClassFile = cgTestClassConstructor.construct(testClassModel) + CodeGeneratorResult(renderClassFile(testClassFile), cgTestClassConstructor.testsGenerationReport) } } } \ No newline at end of file diff --git a/utbot-python/src/main/kotlin/org/utbot/python/framework/codegen/model/constructor/tree/PythonCgTestClassConstructor.kt b/utbot-python/src/main/kotlin/org/utbot/python/framework/codegen/model/constructor/tree/PythonCgTestClassConstructor.kt index 07eab68d30..75f8403532 100644 --- a/utbot-python/src/main/kotlin/org/utbot/python/framework/codegen/model/constructor/tree/PythonCgTestClassConstructor.kt +++ b/utbot-python/src/main/kotlin/org/utbot/python/framework/codegen/model/constructor/tree/PythonCgTestClassConstructor.kt @@ -3,18 +3,17 @@ package org.utbot.python.framework.codegen.model.constructor.tree import org.utbot.framework.codegen.model.constructor.TestClassModel import org.utbot.framework.codegen.model.constructor.context.CgContext import org.utbot.framework.codegen.model.constructor.tree.CgTestClassConstructor -import org.utbot.framework.codegen.model.tree.CgTestClassFile -import org.utbot.framework.codegen.model.tree.buildTestClassFile +import org.utbot.framework.codegen.model.tree.CgClassFile +import org.utbot.framework.codegen.model.tree.buildClassFile internal class PythonCgTestClassConstructor(context: CgContext) : CgTestClassConstructor(context) { - override fun construct(testClassModel: TestClassModel): CgTestClassFile { - return buildTestClassFile { + override fun construct(testClassModel: TestClassModel): CgClassFile { + return buildClassFile { this.declaredClass = withTestClassScope { with(currentTestClassContext) { testClassSuperclass = testFramework.testSuperClass } constructTestClass(testClassModel) } imports.addAll(context.collectedImports) - testsGenerationReport = this@PythonCgTestClassConstructor.testsGenerationReport } } }