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,32 +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
42
+ import org.utbot.framework.plugin.api.UtExecutionSuccess
30
43
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
44
import org.utbot.framework.plugin.api.util.description
36
45
import org.utbot.framework.plugin.api.util.humanReadableName
37
- import org.utbot.framework.plugin.api.util.kClass
38
- import kotlin.reflect.KClass
39
46
40
47
internal class CgTestClassConstructor (val context : CgContext ) :
41
48
CgContextOwner by context,
42
- CgStatementConstructor by CgComponents .getStatementConstructorBy(context) {
49
+ CgStatementConstructor by getStatementConstructorBy(context) {
50
+
51
+ init {
52
+ clearContextRelatedStorage()
53
+ }
43
54
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)
47
58
48
59
private val testsGenerationReport: TestsGenerationReport = TestsGenerationReport ()
49
60
@@ -124,7 +135,10 @@ internal class CgTestClassConstructor(val context: CgContext) :
124
135
return null
125
136
}
126
137
127
- allExecutions = testSet.executions
138
+ successfulExecutionsModels = testSet
139
+ .executions
140
+ .filter { it.result is UtExecutionSuccess }
141
+ .map { (it.result as UtExecutionSuccess ).model }
128
142
129
143
val (methodUnderTest, _, _, clustersInfo) = testSet
130
144
val regions = mutableListOf<CgRegion <CgMethod >>()
@@ -275,106 +289,45 @@ internal class CgTestClassConstructor(val context: CgContext) :
275
289
*/
276
290
private val CgMethodTestSet .allErrors: Map <String , Int >
277
291
get() = errors + codeGenerationErrors.getOrDefault(this , mapOf ())
278
- }
279
292
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()
317
307
}
318
308
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 ()
322
314
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 ()
351
317
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) }
353
321
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) }
356
326
}
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()
369
327
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) }
379
331
}
380
332
}
333
+
0 commit comments