11package org.utbot.framework.codegen.model.constructor.tree
22
3- import org.utbot.common.appendHtmlLine
3+ import org.utbot.framework.codegen.Junit4
4+ import org.utbot.framework.codegen.Junit5
45import org.utbot.framework.codegen.ParametrizedTestSource
6+ import org.utbot.framework.codegen.TestNg
57import org.utbot.framework.codegen.model.constructor.CgMethodTestSet
8+ import org.utbot.framework.codegen.model.constructor.TestClassModel
69import org.utbot.framework.codegen.model.constructor.builtin.TestClassUtilMethodProvider
710import org.utbot.framework.codegen.model.constructor.context.CgContext
811import org.utbot.framework.codegen.model.constructor.context.CgContextOwner
9- import org.utbot.framework.codegen.model.constructor.util.CgComponents
12+ import org.utbot.framework.codegen.model.constructor.name.CgNameGenerator
13+ import org.utbot.framework.codegen.model.constructor.name.CgNameGeneratorImpl
14+ import org.utbot.framework.codegen.model.constructor.tree.CgTestClassConstructor.CgComponents.clearContextRelatedStorage
15+ import org.utbot.framework.codegen.model.constructor.tree.CgTestClassConstructor.CgComponents.getMethodConstructorBy
16+ import org.utbot.framework.codegen.model.constructor.tree.CgTestClassConstructor.CgComponents.getNameGeneratorBy
17+ import org.utbot.framework.codegen.model.constructor.tree.CgTestClassConstructor.CgComponents.getStatementConstructorBy
18+ import org.utbot.framework.codegen.model.constructor.tree.CgTestClassConstructor.CgComponents.getTestFrameworkManagerBy
1019import org.utbot.framework.codegen.model.constructor.util.CgStatementConstructor
11- import org.utbot.framework.codegen.model.tree.CgMethod
20+ import org.utbot.framework.codegen.model.constructor.util.CgStatementConstructorImpl
21+ import org.utbot.framework.codegen.model.tree.CgAuxiliaryClass
1222import org.utbot.framework.codegen.model.tree.CgExecutableUnderTestCluster
23+ import org.utbot.framework.codegen.model.tree.CgMethod
1324import org.utbot.framework.codegen.model.tree.CgParameterDeclaration
1425import org.utbot.framework.codegen.model.tree.CgRegion
1526import org.utbot.framework.codegen.model.tree.CgSimpleRegion
@@ -18,32 +29,32 @@ import org.utbot.framework.codegen.model.tree.CgTestClass
1829import org.utbot.framework.codegen.model.tree.CgTestClassFile
1930import org.utbot.framework.codegen.model.tree.CgTestMethod
2031import org.utbot.framework.codegen.model.tree.CgTestMethodCluster
21- import org.utbot.framework.codegen.model.tree.CgTestMethodType.*
2232import org.utbot.framework.codegen.model.tree.CgTripleSlashMultilineComment
33+ import org.utbot.framework.codegen.model.tree.CgUtilEntity
2334import org.utbot.framework.codegen.model.tree.CgUtilMethod
2435import org.utbot.framework.codegen.model.tree.buildTestClass
2536import org.utbot.framework.codegen.model.tree.buildTestClassBody
2637import org.utbot.framework.codegen.model.tree.buildTestClassFile
2738import org.utbot.framework.codegen.model.visitor.importUtilMethodDependencies
39+ import org.utbot.framework.plugin.api.ClassId
2840import org.utbot.framework.plugin.api.ExecutableId
2941import org.utbot.framework.plugin.api.MethodId
42+ import org.utbot.framework.plugin.api.UtExecutionSuccess
3043import org.utbot.framework.plugin.api.UtMethodTestSet
31- import org.utbot.framework.codegen.model.constructor.TestClassModel
32- import org.utbot.framework.codegen.model.tree.CgAuxiliaryClass
33- import org.utbot.framework.codegen.model.tree.CgUtilEntity
34- import org.utbot.framework.plugin.api.ClassId
3544import org.utbot.framework.plugin.api.util.description
3645import org.utbot.framework.plugin.api.util.humanReadableName
37- import org.utbot.framework.plugin.api.util.kClass
38- import kotlin.reflect.KClass
3946
4047internal class CgTestClassConstructor (val context : CgContext ) :
4148 CgContextOwner by context,
42- CgStatementConstructor by CgComponents .getStatementConstructorBy(context) {
49+ CgStatementConstructor by getStatementConstructorBy(context) {
50+
51+ init {
52+ clearContextRelatedStorage()
53+ }
4354
44- private val methodConstructor = CgComponents . getMethodConstructorBy(context)
45- private val nameGenerator = CgComponents . getNameGeneratorBy(context)
46- private val testFrameworkManager = CgComponents . getTestFrameworkManagerBy(context)
55+ private val methodConstructor = getMethodConstructorBy(context)
56+ private val nameGenerator = getNameGeneratorBy(context)
57+ private val testFrameworkManager = getTestFrameworkManagerBy(context)
4758
4859 private val testsGenerationReport: TestsGenerationReport = TestsGenerationReport ()
4960
@@ -124,7 +135,10 @@ internal class CgTestClassConstructor(val context: CgContext) :
124135 return null
125136 }
126137
127- allExecutions = testSet.executions
138+ successfulExecutionsModels = testSet
139+ .executions
140+ .filter { it.result is UtExecutionSuccess }
141+ .map { (it.result as UtExecutionSuccess ).model }
128142
129143 val (methodUnderTest, _, _, clustersInfo) = testSet
130144 val regions = mutableListOf<CgRegion <CgMethod >>()
@@ -275,106 +289,45 @@ internal class CgTestClassConstructor(val context: CgContext) :
275289 */
276290 private val CgMethodTestSet .allErrors: Map <String , Int >
277291 get() = errors + codeGenerationErrors.getOrDefault(this , mapOf ())
278- }
279292
280- typealias MethodGeneratedTests = MutableMap <ExecutableId , MutableSet <CgTestMethod >>
281- typealias ErrorsCount = Map <String , Int >
282-
283- data class TestsGenerationReport (
284- val executables : MutableSet <ExecutableId > = mutableSetOf(),
285- var successfulExecutions : MethodGeneratedTests = mutableMapOf(),
286- var timeoutExecutions : MethodGeneratedTests = mutableMapOf(),
287- var failedExecutions : MethodGeneratedTests = mutableMapOf(),
288- var crashExecutions : MethodGeneratedTests = mutableMapOf(),
289- var errors : MutableMap <ExecutableId , ErrorsCount > = mutableMapOf()
290- ) {
291- val classUnderTest: KClass <* >
292- get() = executables.firstOrNull()?.classId?.kClass
293- ? : error(" No executables found in test report" )
294-
295- val initialWarnings: MutableList < () -> String > = mutableListOf ()
296- val hasWarnings: Boolean
297- get() = initialWarnings.isNotEmpty()
298-
299- val detailedStatistics: String
300- get() = buildString {
301- appendHtmlLine(" Class: ${classUnderTest.qualifiedName} " )
302- val testMethodsStatistic = executables.map { it.countTestMethods() }
303- val errors = executables.map { it.countErrors() }
304- val overallErrors = errors.sum()
305-
306- appendHtmlLine(" Successful test methods: ${testMethodsStatistic.sumBy { it.successful }} " )
307- appendHtmlLine(
308- " Failing because of unexpected exception test methods: ${testMethodsStatistic.sumBy { it.failing }} "
309- )
310- appendHtmlLine(
311- " Failing because of exceeding timeout test methods: ${testMethodsStatistic.sumBy { it.timeout }} "
312- )
313- appendHtmlLine(
314- " Failing because of possible JVM crash test methods: ${testMethodsStatistic.sumBy { it.crashes }} "
315- )
316- appendHtmlLine(" Not generated because of internal errors test methods: $overallErrors " )
293+ internal object CgComponents {
294+ /* *
295+ * Clears all stored data for current [CgContext].
296+ * As far as context is created per class under test,
297+ * no related data is required after it's processing.
298+ */
299+ fun clearContextRelatedStorage () {
300+ nameGenerators.clear()
301+ statementConstructors.clear()
302+ callableAccessManagers.clear()
303+ testFrameworkManagers.clear()
304+ mockFrameworkManagers.clear()
305+ variableConstructors.clear()
306+ methodConstructors.clear()
317307 }
318308
319- fun addMethodErrors (testSet : CgMethodTestSet , errors : Map <String , Int >) {
320- this .errors[testSet.executableId] = errors
321- }
309+ private val nameGenerators: MutableMap <CgContext , CgNameGenerator > = mutableMapOf ()
310+ private val statementConstructors: MutableMap <CgContext , CgStatementConstructor > = mutableMapOf ()
311+ private val callableAccessManagers: MutableMap <CgContext , CgCallableAccessManager > = mutableMapOf ()
312+ private val testFrameworkManagers: MutableMap <CgContext , TestFrameworkManager > = mutableMapOf ()
313+ private val mockFrameworkManagers: MutableMap <CgContext , MockFrameworkManager > = mutableMapOf ()
322314
323- fun addTestsByType (testSet : CgMethodTestSet , testMethods : List <CgTestMethod >) {
324- with (testSet.executableId) {
325- executables + = this
326-
327- testMethods.forEach {
328- when (it.type) {
329- SUCCESSFUL -> updateExecutions(it, successfulExecutions)
330- FAILING -> updateExecutions(it, failedExecutions)
331- TIMEOUT -> updateExecutions(it, timeoutExecutions)
332- CRASH -> updateExecutions(it, crashExecutions)
333- PARAMETRIZED -> {
334- // Parametrized tests are not supported in the tests report yet
335- // TODO JIRA:1507
336- }
337- }
338- }
339- }
340- }
341-
342- fun toString (isShort : Boolean ): String = buildString {
343- appendHtmlLine(" Target: ${classUnderTest.qualifiedName} " )
344- if (initialWarnings.isNotEmpty()) {
345- initialWarnings.forEach { appendHtmlLine(it()) }
346- appendHtmlLine()
347- }
348-
349- val testMethodsStatistic = executables.map { it.countTestMethods() }
350- val overallTestMethods = testMethodsStatistic.sumBy { it.count }
315+ private val variableConstructors: MutableMap <CgContext , CgVariableConstructor > = mutableMapOf ()
316+ private val methodConstructors: MutableMap <CgContext , CgMethodConstructor > = mutableMapOf ()
351317
352- appendHtmlLine(" Overall test methods: $overallTestMethods " )
318+ fun getNameGeneratorBy (context : CgContext ) = nameGenerators.getOrPut(context) { CgNameGeneratorImpl (context) }
319+ fun getCallableAccessManagerBy (context : CgContext ) = callableAccessManagers.getOrPut(context) { CgCallableAccessManagerImpl (context) }
320+ fun getStatementConstructorBy (context : CgContext ) = statementConstructors.getOrPut(context) { CgStatementConstructorImpl (context) }
353321
354- if (! isShort) {
355- appendHtmlLine(detailedStatistics)
322+ fun getTestFrameworkManagerBy (context : CgContext ) = when (context.testFramework) {
323+ is Junit4 -> testFrameworkManagers.getOrPut(context) { Junit4Manager (context) }
324+ is Junit5 -> testFrameworkManagers.getOrPut(context) { Junit5Manager (context) }
325+ is TestNg -> testFrameworkManagers.getOrPut(context) { TestNgManager (context) }
356326 }
357- }
358-
359- override fun toString (): String = toString(false )
360-
361- private fun ExecutableId.countTestMethods (): TestMethodStatistic = TestMethodStatistic (
362- testMethodsNumber(successfulExecutions),
363- testMethodsNumber(failedExecutions),
364- testMethodsNumber(timeoutExecutions),
365- testMethodsNumber(crashExecutions)
366- )
367-
368- private fun ExecutableId.countErrors (): Int = errors.getOrDefault(this , emptyMap()).values.sum()
369327
370- private fun ExecutableId.testMethodsNumber (executables : MethodGeneratedTests ): Int =
371- executables.getOrDefault(this , emptySet()).size
372-
373- private fun ExecutableId.updateExecutions (it : CgTestMethod , executions : MethodGeneratedTests ) {
374- executions.getOrPut(this ) { mutableSetOf () } + = it
375- }
376-
377- private data class TestMethodStatistic (val successful : Int , val failing : Int , val timeout : Int , val crashes : Int ) {
378- val count: Int = successful + failing + timeout + crashes
328+ fun getMockFrameworkManagerBy (context : CgContext ) = mockFrameworkManagers.getOrPut(context) { MockFrameworkManager (context) }
329+ fun getVariableConstructorBy (context : CgContext ) = variableConstructors.getOrPut(context) { CgVariableConstructor (context) }
330+ fun getMethodConstructorBy (context : CgContext ) = methodConstructors.getOrPut(context) { CgMethodConstructor (context) }
379331 }
380332}
333+
0 commit comments