@@ -3,19 +3,15 @@ package org.utbot.framework.codegen.model.constructor.tree
3
3
import org.utbot.common.PathUtil
4
4
import org.utbot.framework.assemble.assemble
5
5
import org.utbot.framework.codegen.ForceStaticMocking
6
- import org.utbot.framework.codegen.Junit4
7
- import org.utbot.framework.codegen.Junit5
8
6
import org.utbot.framework.codegen.ParametrizedTestSource
9
7
import org.utbot.framework.codegen.RuntimeExceptionTestsBehaviour.PASS
10
- import org.utbot.framework.codegen.TestNg
11
8
import org.utbot.framework.codegen.model.constructor.CgMethodTestSet
12
9
import org.utbot.framework.codegen.model.constructor.builtin.closeMethodIdOrNull
13
10
import org.utbot.framework.codegen.model.constructor.builtin.forName
14
11
import org.utbot.framework.codegen.model.constructor.builtin.getClass
15
12
import org.utbot.framework.codegen.model.constructor.builtin.getTargetException
16
13
import org.utbot.framework.codegen.model.constructor.builtin.invoke
17
14
import org.utbot.framework.codegen.model.constructor.builtin.newInstance
18
- import org.utbot.framework.codegen.model.constructor.builtin.setArrayElement
19
15
import org.utbot.framework.codegen.model.constructor.context.CgContext
20
16
import org.utbot.framework.codegen.model.constructor.context.CgContextOwner
21
17
import org.utbot.framework.codegen.model.constructor.util.CgComponents
@@ -26,9 +22,9 @@ import org.utbot.framework.codegen.model.constructor.util.classCgClassId
26
22
import org.utbot.framework.codegen.model.constructor.util.needExpectedDeclaration
27
23
import org.utbot.framework.codegen.model.constructor.util.overridesEquals
28
24
import org.utbot.framework.codegen.model.constructor.util.plus
25
+ import org.utbot.framework.codegen.model.constructor.util.setArgumentsArrayElement
29
26
import org.utbot.framework.codegen.model.constructor.util.typeCast
30
27
import org.utbot.framework.codegen.model.tree.CgAllocateArray
31
- import org.utbot.framework.codegen.model.tree.CgAnnotation
32
28
import org.utbot.framework.codegen.model.tree.CgArrayElementAccess
33
29
import org.utbot.framework.codegen.model.tree.CgClassId
34
30
import org.utbot.framework.codegen.model.tree.CgDeclaration
@@ -69,7 +65,6 @@ import org.utbot.framework.codegen.model.tree.buildParameterizedTestDataProvider
69
65
import org.utbot.framework.codegen.model.tree.buildTestMethod
70
66
import org.utbot.framework.codegen.model.tree.convertDocToCg
71
67
import org.utbot.framework.codegen.model.tree.toStatement
72
- import org.utbot.framework.codegen.model.util.at
73
68
import org.utbot.framework.codegen.model.util.canBeSetIn
74
69
import org.utbot.framework.codegen.model.util.equalTo
75
70
import org.utbot.framework.codegen.model.util.get
@@ -116,7 +111,6 @@ import org.utbot.framework.plugin.api.UtVoidModel
116
111
import org.utbot.framework.plugin.api.onFailure
117
112
import org.utbot.framework.plugin.api.onSuccess
118
113
import org.utbot.framework.plugin.api.util.booleanClassId
119
- import org.utbot.framework.plugin.api.util.builtinStaticMethodId
120
114
import org.utbot.framework.plugin.api.util.doubleArrayClassId
121
115
import org.utbot.framework.plugin.api.util.doubleClassId
122
116
import org.utbot.framework.plugin.api.util.doubleWrapperClassId
@@ -137,7 +131,6 @@ import org.utbot.framework.plugin.api.util.isPrimitiveWrapper
137
131
import org.utbot.framework.plugin.api.util.isRefType
138
132
import org.utbot.framework.plugin.api.util.jClass
139
133
import org.utbot.framework.plugin.api.util.kClass
140
- import org.utbot.framework.plugin.api.util.methodId
141
134
import org.utbot.framework.plugin.api.util.objectArrayClassId
142
135
import org.utbot.framework.plugin.api.util.objectClassId
143
136
import org.utbot.framework.plugin.api.util.stringClassId
@@ -1317,7 +1310,7 @@ internal class CgMethodConstructor(val context: CgContext) : CgContextOwner by c
1317
1310
return withDataProviderScope {
1318
1311
dataProviderMethod(dataProviderMethodName) {
1319
1312
val argListLength = testSet.executions.size
1320
- val argListVariable = createArgList(argListLength)
1313
+ val argListVariable = testFrameworkManager. createArgList(argListLength, " argList " )
1321
1314
1322
1315
emptyLine()
1323
1316
@@ -1432,161 +1425,15 @@ internal class CgMethodConstructor(val context: CgContext) : CgContextOwner by c
1432
1425
CgAllocateArray (objectArrayClassId, objectClassId, arguments.size)
1433
1426
}
1434
1427
for ((i, argument) in arguments.withIndex()) {
1435
- setArgumentsArrayElement(argsArray, i, argument)
1436
- }
1437
- when (testFramework) {
1438
- Junit5 -> {
1439
- + argsVariable[addToListMethodId](
1440
- argumentsClassId[argumentsMethodId](argsArray)
1441
- )
1442
- }
1443
- TestNg -> {
1444
- setArgumentsArrayElement(argsVariable, executionIndex, argsArray)
1445
- }
1446
- Junit4 -> error(" Parameterized tests are not supported for JUnit4" )
1447
- }
1448
- }
1449
-
1450
- /* *
1451
- * Sets an element of arguments array in parameterized test,
1452
- * if test framework represents arguments as array.
1453
- */
1454
- private fun setArgumentsArrayElement (array : CgVariable , index : Int , value : CgExpression ) {
1455
- when (array.type) {
1456
- objectClassId -> {
1457
- + java.lang.reflect.Array ::class .id[setArrayElement](array, index, value)
1458
- }
1459
- else -> array.at(index) `= ` value
1428
+ setArgumentsArrayElement(argsArray, i, argument, this )
1460
1429
}
1430
+ testFrameworkManager.passArgumentsToArgsVariable(argsVariable, argsArray, executionIndex)
1461
1431
}
1462
1432
1463
- /* *
1464
- * Creates annotations for data provider method in parameterized tests
1465
- * depending on test framework.
1466
- */
1467
- private fun createDataProviderAnnotations (dataProviderMethodName : String? ): MutableList <CgAnnotation > =
1468
- when (testFramework) {
1469
- Junit5 -> mutableListOf ()
1470
- TestNg -> mutableListOf (
1471
- annotation(
1472
- testFramework.methodSourceAnnotationId,
1473
- listOf (" name" to CgLiteral (stringClassId, dataProviderMethodName))
1474
- ),
1475
- )
1476
- Junit4 -> error(" Parameterized tests are not supported for JUnit4" )
1477
- }
1478
-
1479
- /* *
1480
- * Creates declaration of argList collection in parameterized tests.
1481
- */
1482
- private fun createArgList (length : Int ): CgVariable {
1483
- val argListName = " argList"
1484
- return when (testFramework) {
1485
- Junit5 ->
1486
- newVar(argListClassId, argListName) {
1487
- val constructor = ConstructorId (argListClassId, emptyList())
1488
- constructor .invoke()
1489
- }
1490
- TestNg ->
1491
- newVar(argListClassId, argListName) {
1492
- CgAllocateArray (argListClassId, Array <Any >::class .java.id, length)
1493
- }
1494
- Junit4 -> error(" Parameterized tests are not supported for JUnit4" )
1495
- }
1496
- }
1497
-
1498
- /* *
1499
- * Creates a [ClassId] for arguments collection.
1500
- */
1501
- private val argListClassId: ClassId
1502
- get() = when (testFramework) {
1503
- Junit5 -> {
1504
- val arrayListId = java.util.ArrayList ::class .id
1505
- BuiltinClassId (
1506
- name = arrayListId.name,
1507
- simpleName = arrayListId.simpleName,
1508
- canonicalName = arrayListId.canonicalName,
1509
- packageName = arrayListId.packageName,
1510
- typeParameters = TypeParameters (listOf (argumentsClassId))
1511
- )
1512
- }
1513
- TestNg -> {
1514
- val outerArrayId = Array <Array <Any ?>? > ::class .id
1515
- val innerArrayId = BuiltinClassId (
1516
- name = objectArrayClassId.name,
1517
- simpleName = objectArrayClassId.simpleName,
1518
- canonicalName = objectArrayClassId.canonicalName,
1519
- packageName = objectArrayClassId.packageName,
1520
- elementClassId = objectClassId,
1521
- typeParameters = TypeParameters (listOf (objectClassId))
1522
- )
1523
-
1524
- BuiltinClassId (
1525
- name = outerArrayId.name,
1526
- simpleName = outerArrayId.simpleName,
1527
- canonicalName = outerArrayId.canonicalName,
1528
- packageName = outerArrayId.packageName,
1529
- elementClassId = innerArrayId,
1530
- typeParameters = TypeParameters (listOf (innerArrayId))
1531
- )
1532
- }
1533
- Junit4 -> error(" Parameterized tests are not supported for JUnit4" )
1534
- }
1535
-
1536
-
1537
- /* *
1538
- * A [MethodId] to add an item into [ArrayList].
1539
- */
1540
- private val addToListMethodId: MethodId
1541
- get() = methodId(
1542
- classId = ArrayList ::class .id,
1543
- name = " add" ,
1544
- returnType = booleanClassId,
1545
- arguments = arrayOf(Object ::class .id),
1546
- )
1547
-
1548
- /* *
1549
- * A [ClassId] of class `org.junit.jupiter.params.provider.Arguments`
1550
- */
1551
- private val argumentsClassId: BuiltinClassId
1552
- get() = BuiltinClassId (
1553
- name = " org.junit.jupiter.params.provider.Arguments" ,
1554
- simpleName = " Arguments" ,
1555
- canonicalName = " org.junit.jupiter.params.provider.Arguments" ,
1556
- packageName = " org.junit.jupiter.params.provider"
1557
- )
1558
-
1559
- /* *
1560
- * A [MethodId] to call JUnit Arguments method.
1561
- */
1562
- private val argumentsMethodId: BuiltinMethodId
1563
- get() = builtinStaticMethodId(
1564
- classId = argumentsClassId,
1565
- name = " arguments" ,
1566
- returnType = argumentsClassId,
1567
- // vararg of Objects
1568
- arguments = arrayOf(objectArrayClassId)
1569
- )
1570
-
1571
1433
private fun containsFailureExecution (testSet : CgMethodTestSet ) =
1572
1434
testSet.executions.any { it.result is UtExecutionFailure }
1573
1435
1574
1436
1575
- private fun collectParameterizedTestAnnotations (dataProviderMethodName : String? ): Set <CgAnnotation > =
1576
- when (testFramework) {
1577
- Junit5 -> setOf (
1578
- annotation(testFramework.parameterizedTestAnnotationId),
1579
- annotation(testFramework.methodSourceAnnotationId, " ${outerMostTestClass.canonicalName} #$dataProviderMethodName " ),
1580
- )
1581
- TestNg -> setOf (
1582
- annotation(
1583
- testFramework.parameterizedTestAnnotationId,
1584
- listOf (" dataProvider" to CgLiteral (stringClassId, dataProviderMethodName))
1585
- ),
1586
- )
1587
- Junit4 -> error(" Parameterized tests are not supported for JUnit4" )
1588
- }
1589
-
1590
1437
private fun testMethod (
1591
1438
methodName : String ,
1592
1439
displayName : String? ,
@@ -1596,26 +1443,13 @@ internal class CgMethodConstructor(val context: CgContext) : CgContextOwner by c
1596
1443
body : () -> Unit ,
1597
1444
): CgTestMethod {
1598
1445
collectedMethodAnnotations + = if (parameterized) {
1599
- collectParameterizedTestAnnotations(dataProviderMethodName)
1446
+ testFrameworkManager. collectParameterizedTestAnnotations(dataProviderMethodName)
1600
1447
} else {
1601
1448
setOf (annotation(testFramework.testAnnotationId))
1602
1449
}
1603
1450
1604
- /* Add a short test's description depending on the test framework type:
1605
- DisplayName annotation in case of JUni5, and description argument to Test annotation in case of TestNG.
1606
- */
1607
- if (displayName != null ) {
1608
- when (testFramework) {
1609
- is Junit5 -> {
1610
- displayName.let { testFrameworkManager.addDisplayName(it) }
1611
- }
1612
- is TestNg -> {
1613
- testFrameworkManager.addTestDescription(displayName)
1614
- }
1615
- else -> {
1616
- // nothing
1617
- }
1618
- }
1451
+ displayName?.let {
1452
+ testFrameworkManager.addTestDescription(displayName)
1619
1453
}
1620
1454
1621
1455
val result = currentExecution!! .result
@@ -1676,12 +1510,12 @@ internal class CgMethodConstructor(val context: CgContext) : CgContextOwner by c
1676
1510
private fun dataProviderMethod (dataProviderMethodName : String , body : () -> Unit ): CgParameterizedTestDataProviderMethod {
1677
1511
return buildParameterizedTestDataProviderMethod {
1678
1512
name = dataProviderMethodName
1679
- returnType = argListClassId
1513
+ returnType = testFramework. argListClassId
1680
1514
statements = block(body)
1681
1515
// Exceptions and annotations assignment must run after the statements block is build,
1682
1516
// because we collect info about exceptions and required annotations while building the statements
1683
1517
exceptions + = collectedExceptions
1684
- annotations + = createDataProviderAnnotations(dataProviderMethodName)
1518
+ annotations + = testFrameworkManager. createDataProviderAnnotations(dataProviderMethodName)
1685
1519
}
1686
1520
}
1687
1521
0 commit comments