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,33 +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
30- import 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
3542import org.utbot.framework.plugin.api.UtExecutionSuccess
43+ import org.utbot.framework.plugin.api.UtMethodTestSet
3644import org.utbot.framework.plugin.api.util.description
3745import org.utbot.framework.plugin.api.util.humanReadableName
38- import org.utbot.framework.plugin.api.util.kClass
39- import kotlin.reflect.KClass
4046
4147internal class CgTestClassConstructor (val context : CgContext ) :
4248 CgContextOwner by context,
43- CgStatementConstructor by CgComponents .getStatementConstructorBy(context) {
49+ CgStatementConstructor by getStatementConstructorBy(context) {
50+
51+ init {
52+ clearContextRelatedStorage()
53+ }
4454
45- private val methodConstructor = CgComponents . getMethodConstructorBy(context)
46- private val nameGenerator = CgComponents . getNameGeneratorBy(context)
47- private val testFrameworkManager = CgComponents . getTestFrameworkManagerBy(context)
55+ private val methodConstructor = getMethodConstructorBy(context)
56+ private val nameGenerator = getNameGeneratorBy(context)
57+ private val testFrameworkManager = getTestFrameworkManagerBy(context)
4858
4959 private val testsGenerationReport: TestsGenerationReport = TestsGenerationReport ()
5060
@@ -279,106 +289,45 @@ internal class CgTestClassConstructor(val context: CgContext) :
279289 */
280290 private val CgMethodTestSet .allErrors: Map <String , Int >
281291 get() = errors + codeGenerationErrors.getOrDefault(this , mapOf ())
282- }
283292
284- typealias MethodGeneratedTests = MutableMap <ExecutableId , MutableSet <CgTestMethod >>
285- typealias ErrorsCount = Map <String , Int >
286-
287- data class TestsGenerationReport (
288- val executables : MutableSet <ExecutableId > = mutableSetOf(),
289- var successfulExecutions : MethodGeneratedTests = mutableMapOf(),
290- var timeoutExecutions : MethodGeneratedTests = mutableMapOf(),
291- var failedExecutions : MethodGeneratedTests = mutableMapOf(),
292- var crashExecutions : MethodGeneratedTests = mutableMapOf(),
293- var errors : MutableMap <ExecutableId , ErrorsCount > = mutableMapOf()
294- ) {
295- val classUnderTest: KClass <* >
296- get() = executables.firstOrNull()?.classId?.kClass
297- ? : error(" No executables found in test report" )
298-
299- val initialWarnings: MutableList < () -> String > = mutableListOf ()
300- val hasWarnings: Boolean
301- get() = initialWarnings.isNotEmpty()
302-
303- val detailedStatistics: String
304- get() = buildString {
305- appendHtmlLine(" Class: ${classUnderTest.qualifiedName} " )
306- val testMethodsStatistic = executables.map { it.countTestMethods() }
307- val errors = executables.map { it.countErrors() }
308- val overallErrors = errors.sum()
309-
310- appendHtmlLine(" Successful test methods: ${testMethodsStatistic.sumBy { it.successful }} " )
311- appendHtmlLine(
312- " Failing because of unexpected exception test methods: ${testMethodsStatistic.sumBy { it.failing }} "
313- )
314- appendHtmlLine(
315- " Failing because of exceeding timeout test methods: ${testMethodsStatistic.sumBy { it.timeout }} "
316- )
317- appendHtmlLine(
318- " Failing because of possible JVM crash test methods: ${testMethodsStatistic.sumBy { it.crashes }} "
319- )
320- 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()
321307 }
322308
323- fun addMethodErrors (testSet : CgMethodTestSet , errors : Map <String , Int >) {
324- this .errors[testSet.executableId] = errors
325- }
326-
327- fun addTestsByType (testSet : CgMethodTestSet , testMethods : List <CgTestMethod >) {
328- with (testSet.executableId) {
329- executables + = this
330-
331- testMethods.forEach {
332- when (it.type) {
333- SUCCESSFUL -> updateExecutions(it, successfulExecutions)
334- FAILING -> updateExecutions(it, failedExecutions)
335- TIMEOUT -> updateExecutions(it, timeoutExecutions)
336- CRASH -> updateExecutions(it, crashExecutions)
337- PARAMETRIZED -> {
338- // Parametrized tests are not supported in the tests report yet
339- // TODO JIRA:1507
340- }
341- }
342- }
343- }
344- }
345-
346- fun toString (isShort : Boolean ): String = buildString {
347- appendHtmlLine(" Target: ${classUnderTest.qualifiedName} " )
348- if (initialWarnings.isNotEmpty()) {
349- initialWarnings.forEach { appendHtmlLine(it()) }
350- appendHtmlLine()
351- }
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 ()
352314
353- val testMethodsStatistic = executables.map { it.countTestMethods() }
354- val overallTestMethods = testMethodsStatistic.sumBy { it.count }
315+ private val variableConstructors : MutableMap < CgContext , CgVariableConstructor > = mutableMapOf ()
316+ private val methodConstructors : MutableMap < CgContext , CgMethodConstructor > = mutableMapOf ()
355317
356- 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) }
357321
358- if (! isShort) {
359- 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) }
360326 }
361- }
362-
363- override fun toString (): String = toString(false )
364-
365- private fun ExecutableId.countTestMethods (): TestMethodStatistic = TestMethodStatistic (
366- testMethodsNumber(successfulExecutions),
367- testMethodsNumber(failedExecutions),
368- testMethodsNumber(timeoutExecutions),
369- testMethodsNumber(crashExecutions)
370- )
371-
372- private fun ExecutableId.countErrors (): Int = errors.getOrDefault(this , emptyMap()).values.sum()
373327
374- private fun ExecutableId.testMethodsNumber (executables : MethodGeneratedTests ): Int =
375- executables.getOrDefault(this , emptySet()).size
376-
377- private fun ExecutableId.updateExecutions (it : CgTestMethod , executions : MethodGeneratedTests ) {
378- executions.getOrPut(this ) { mutableSetOf () } + = it
379- }
380-
381- private data class TestMethodStatistic (val successful : Int , val failing : Int , val timeout : Int , val crashes : Int ) {
382- 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) }
383331 }
384332}
333+
0 commit comments