@@ -9,6 +9,9 @@ import org.utbot.common.packageName
9
9
import org.utbot.examples.TestFrameworkConfiguration
10
10
import org.utbot.framework.codegen.ExecutionStatus.SUCCESS
11
11
import org.utbot.framework.codegen.model.CodeGenerator
12
+ import org.utbot.framework.codegen.model.CodeGeneratorResult
13
+ import org.utbot.framework.codegen.model.UtilClassKind
14
+ import org.utbot.framework.codegen.model.UtilClassKind.Companion.UT_UTILS_CLASS_NAME
12
15
import org.utbot.framework.plugin.api.CodegenLanguage
13
16
import org.utbot.framework.plugin.api.ExecutableId
14
17
import org.utbot.framework.plugin.api.MockFramework
@@ -18,6 +21,8 @@ import org.utbot.framework.plugin.api.util.UtContext
18
21
import org.utbot.framework.plugin.api.util.description
19
22
import org.utbot.framework.plugin.api.util.id
20
23
import org.utbot.framework.plugin.api.util.withUtContext
24
+ import java.io.File
25
+ import java.nio.file.Path
21
26
import kotlin.reflect.KClass
22
27
23
28
private val logger = KotlinLogging .logger {}
@@ -74,7 +79,8 @@ class TestCodeGeneratorPipeline(private val testFrameworkConfiguration: TestFram
74
79
val codegenLanguage = testFrameworkConfiguration.codegenLanguage
75
80
val parametrizedTestSource = testFrameworkConfiguration.parametrizedTestSource
76
81
77
- val testClass = callToCodeGenerator(testSets, classUnderTest)
82
+ val codeGenerationResult = callToCodeGenerator(testSets, classUnderTest)
83
+ val testClass = codeGenerationResult.generatedCode
78
84
79
85
// actual number of the tests in the generated testClass
80
86
val generatedMethodsCount = testClass
@@ -148,17 +154,33 @@ class TestCodeGeneratorPipeline(private val testFrameworkConfiguration: TestFram
148
154
149
155
val testClassName = classPipeline.retrieveTestClassName(" BrokenGeneratedTest" )
150
156
val generatedTestFile = writeTest(testClass, testClassName, buildDirectory, codegenLanguage)
157
+ val generatedUtilClassFile = codeGenerationResult.utilClassKind?.writeUtilClassToFile(buildDirectory, codegenLanguage)
151
158
152
159
logger.error(" Broken test has been written to the file: [$generatedTestFile ]" )
160
+ if (generatedUtilClassFile != null ) {
161
+ logger.error(" Util class for the broken test has been written to the file: [$generatedUtilClassFile ]" )
162
+ }
153
163
logger.error(" Failed configuration: $testFrameworkConfiguration " )
154
164
155
165
throw it
156
166
}
157
167
158
- classPipeline.stageContext = copy(data = testClass , stages = stages + information.completeStage())
168
+ classPipeline.stageContext = copy(data = codeGenerationResult , stages = stages + information.completeStage())
159
169
}
160
170
}
161
171
172
+ private fun UtilClassKind.writeUtilClassToFile (buildDirectory : Path , language : CodegenLanguage ): File {
173
+ val utilClassFile = File (buildDirectory.toFile(), " $UT_UTILS_CLASS_NAME${language.extension} " )
174
+ val utilClassText = getUtilClassText(language)
175
+ return writeFile(utilClassText, utilClassFile)
176
+ }
177
+
178
+ private data class GeneratedTestClassInfo (
179
+ val testClassName : String ,
180
+ val generatedTestFile : File ,
181
+ val generatedUtilClassFile : File ?
182
+ )
183
+
162
184
@Suppress(" UNCHECKED_CAST" )
163
185
private fun processCompilationStages (classesPipelines : List <ClassPipeline >) {
164
186
val information = StageExecutionInformation (Compilation )
@@ -168,24 +190,34 @@ class TestCodeGeneratorPipeline(private val testFrameworkConfiguration: TestFram
168
190
val codegenLanguage = testFrameworkConfiguration.codegenLanguage
169
191
170
192
val testClassesNamesToTestGeneratedTests = classesPipelines.map { classPipeline ->
171
- val testClass = classPipeline.stageContext.data as String
193
+ val codeGeneratorResult = classPipeline.stageContext.data as CodeGeneratorResult // String
194
+ val testClass = codeGeneratorResult.generatedCode
195
+
172
196
val testClassName = classPipeline.retrieveTestClassName(" GeneratedTest" )
173
197
val generatedTestFile = writeTest(testClass, testClassName, buildDirectory, codegenLanguage)
198
+ val generatedUtilClassFile = codeGeneratorResult.utilClassKind?.writeUtilClassToFile(buildDirectory, codegenLanguage)
174
199
175
200
logger.info(" Test has been written to the file: [$generatedTestFile ]" )
201
+ if (generatedUtilClassFile != null ) {
202
+ logger.info(" Util class for the test has been written to the file: [$generatedUtilClassFile ]" )
203
+ }
176
204
177
- testClassName to generatedTestFile
205
+ GeneratedTestClassInfo ( testClassName, generatedTestFile, generatedUtilClassFile)
178
206
}
179
207
208
+ val sourceFiles = mutableListOf<String >().apply {
209
+ this + = testClassesNamesToTestGeneratedTests.map { it.generatedTestFile.absolutePath }
210
+ this + = testClassesNamesToTestGeneratedTests.mapNotNull { it.generatedUtilClassFile?.absolutePath }
211
+ }
180
212
compileTests(
181
213
" $buildDirectory " ,
182
- testClassesNamesToTestGeneratedTests.map { it.second.absolutePath } ,
214
+ sourceFiles ,
183
215
codegenLanguage
184
216
)
185
217
186
- testClassesNamesToTestGeneratedTests.zip(classesPipelines) { testClassNameToTest , classPipeline ->
218
+ testClassesNamesToTestGeneratedTests.zip(classesPipelines) { generatedTestClassInfo , classPipeline ->
187
219
classPipeline.stageContext = classPipeline.stageContext.copy(
188
- data = CompilationResult (" $buildDirectory " , testClassNameToTest.first ),
220
+ data = CompilationResult (" $buildDirectory " , generatedTestClassInfo.testClassName ),
189
221
stages = classPipeline.stageContext.stages + information.completeStage()
190
222
)
191
223
}
@@ -223,7 +255,7 @@ class TestCodeGeneratorPipeline(private val testFrameworkConfiguration: TestFram
223
255
private fun callToCodeGenerator (
224
256
testSets : List <UtMethodTestSet >,
225
257
classUnderTest : KClass <* >
226
- ): String {
258
+ ): CodeGeneratorResult {
227
259
val params = mutableMapOf<ExecutableId , List <String >>()
228
260
229
261
val codeGenerator = with (testFrameworkConfiguration) {
@@ -243,7 +275,7 @@ class TestCodeGeneratorPipeline(private val testFrameworkConfiguration: TestFram
243
275
}
244
276
val testClassCustomName = " ${classUnderTest.java.simpleName} GeneratedTest"
245
277
246
- return codeGenerator.generateAsString (testSets, testClassCustomName)
278
+ return codeGenerator.generateAsStringWithTestReport (testSets, testClassCustomName)
247
279
}
248
280
249
281
private fun checkPipelinesResults (classesPipelines : List <ClassPipeline >) {
0 commit comments