1
1
package org.utbot.framework.codegen.model.constructor.tree
2
2
3
- import org.utbot.common.appendHtmlLine
3
+ import org.utbot.framework.codegen.Junit4
4
+ import org.utbot.framework.codegen.Junit5
4
5
import org.utbot.framework.codegen.ParametrizedTestSource
6
+ import org.utbot.framework.codegen.TestNg
5
7
import org.utbot.framework.codegen.model.constructor.CgMethodTestSet
8
+ import org.utbot.framework.codegen.model.constructor.TestClassModel
6
9
import org.utbot.framework.codegen.model.constructor.builtin.TestClassUtilMethodProvider
7
10
import org.utbot.framework.codegen.model.constructor.context.CgContext
8
11
import 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
10
19
import 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
12
22
import org.utbot.framework.codegen.model.tree.CgExecutableUnderTestCluster
23
+ import org.utbot.framework.codegen.model.tree.CgMethod
13
24
import org.utbot.framework.codegen.model.tree.CgParameterDeclaration
14
25
import org.utbot.framework.codegen.model.tree.CgRegion
15
26
import org.utbot.framework.codegen.model.tree.CgSimpleRegion
@@ -18,33 +29,32 @@ import org.utbot.framework.codegen.model.tree.CgTestClass
18
29
import org.utbot.framework.codegen.model.tree.CgTestClassFile
19
30
import org.utbot.framework.codegen.model.tree.CgTestMethod
20
31
import org.utbot.framework.codegen.model.tree.CgTestMethodCluster
21
- import org.utbot.framework.codegen.model.tree.CgTestMethodType.*
22
32
import org.utbot.framework.codegen.model.tree.CgTripleSlashMultilineComment
33
+ import org.utbot.framework.codegen.model.tree.CgUtilEntity
23
34
import org.utbot.framework.codegen.model.tree.CgUtilMethod
24
35
import org.utbot.framework.codegen.model.tree.buildTestClass
25
36
import org.utbot.framework.codegen.model.tree.buildTestClassBody
26
37
import org.utbot.framework.codegen.model.tree.buildTestClassFile
27
38
import org.utbot.framework.codegen.model.visitor.importUtilMethodDependencies
39
+ import org.utbot.framework.plugin.api.ClassId
28
40
import org.utbot.framework.plugin.api.ExecutableId
29
41
import 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
35
42
import org.utbot.framework.plugin.api.UtExecutionSuccess
43
+ import org.utbot.framework.plugin.api.UtMethodTestSet
36
44
import org.utbot.framework.plugin.api.util.description
37
45
import org.utbot.framework.plugin.api.util.humanReadableName
38
- import org.utbot.framework.plugin.api.util.kClass
39
- import kotlin.reflect.KClass
40
46
41
47
internal class CgTestClassConstructor (val context : CgContext ) :
42
48
CgContextOwner by context,
43
- CgStatementConstructor by CgComponents .getStatementConstructorBy(context) {
49
+ CgStatementConstructor by getStatementConstructorBy(context) {
50
+
51
+ init {
52
+ clearContextRelatedStorage()
53
+ }
44
54
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)
48
58
49
59
private val testsGenerationReport: TestsGenerationReport = TestsGenerationReport ()
50
60
@@ -279,106 +289,45 @@ internal class CgTestClassConstructor(val context: CgContext) :
279
289
*/
280
290
private val CgMethodTestSet .allErrors: Map <String , Int >
281
291
get() = errors + codeGenerationErrors.getOrDefault(this , mapOf ())
282
- }
283
292
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()
321
307
}
322
308
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 ()
352
314
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 ()
355
317
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) }
357
321
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) }
360
326
}
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()
373
327
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) }
383
331
}
384
332
}
333
+
0 commit comments