Skip to content

Commit ad150ba

Browse files
sofurihafeEgorkaKulikov
authored andcommitted
Lessen thisInstance creation in parameterized test generation (UnitTestBot#1065)
* Lessen thisInstance creation in parameterized test generation * Avoid incorrect configuration Co-authored-by: Egor Kulikov <egor.k.kulikov@gmail.com>
1 parent 5fb1e16 commit ad150ba

File tree

3 files changed

+6
-20
lines changed

3 files changed

+6
-20
lines changed

utbot-framework/src/main/kotlin/org/utbot/framework/codegen/model/constructor/tree/CgMethodConstructor.kt

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1337,7 +1337,9 @@ internal class CgMethodConstructor(val context: CgContext) : CgContextOwner by c
13371337
substituteStaticFields(statics, isParametrized = true)
13381338

13391339
// build this instance
1340-
thisInstance = genericExecution.stateBefore.thisInstance?.let { currentMethodParameters[CgParameterKind.ThisInstance] }
1340+
thisInstance = genericExecution.stateBefore.thisInstance?.let {
1341+
variableConstructor.getOrCreateVariable(it)
1342+
}
13411343

13421344
// build arguments for method under test and parameterized test
13431345
for (index in genericExecution.stateBefore.parameters.indices) {
@@ -1399,20 +1401,6 @@ internal class CgMethodConstructor(val context: CgContext) : CgContextOwner by c
13991401
val executableUnderTestParameters = testSet.executableId.executable.parameters
14001402

14011403
return mutableListOf<CgParameterDeclaration>().apply {
1402-
// this instance
1403-
val thisInstanceModel = genericExecution.stateBefore.thisInstance
1404-
if (thisInstanceModel != null) {
1405-
val type = wrapTypeIfRequired(thisInstanceModel.classId)
1406-
val thisInstance = CgParameterDeclaration(
1407-
parameter = declareParameter(
1408-
type = type,
1409-
name = nameGenerator.variableName(type)
1410-
),
1411-
isReferenceType = true
1412-
)
1413-
this += thisInstance
1414-
currentMethodParameters[CgParameterKind.ThisInstance] = thisInstance.parameter
1415-
}
14161404
// arguments
14171405
for (index in genericExecution.stateBefore.parameters.indices) {
14181406
val argumentName = paramNames[executableUnderTest]?.get(index)
@@ -1524,9 +1512,6 @@ internal class CgMethodConstructor(val context: CgContext) : CgContextOwner by c
15241512

15251513
private fun createExecutionArguments(testSet: CgMethodTestSet, execution: UtExecution): List<CgExpression> {
15261514
val arguments = mutableListOf<CgExpression>()
1527-
execution.stateBefore.thisInstance?.let {
1528-
arguments += variableConstructor.getOrCreateVariable(it)
1529-
}
15301515

15311516
for ((paramIndex, paramModel) in execution.stateBefore.parameters.withIndex()) {
15321517
val argumentName = paramNames[testSet.executableId]?.get(paramIndex)

utbot-framework/src/main/kotlin/org/utbot/framework/codegen/model/tree/CgElement.kt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -745,13 +745,11 @@ data class CgParameterDeclaration(
745745

746746
/**
747747
* Test method parameter can be one of the following types:
748-
* - this instance for method under test (MUT)
749748
* - argument of MUT with a certain index
750749
* - result expected from MUT with the given arguments
751750
* - exception expected from MUT with the given arguments
752751
*/
753752
sealed class CgParameterKind {
754-
object ThisInstance : CgParameterKind()
755753
data class Argument(val index: Int) : CgParameterKind()
756754
data class Statics(val model: UtModel) : CgParameterKind()
757755
object ExpectedResult : CgParameterKind()

utbot-framework/src/main/kotlin/org/utbot/tests/infrastructure/CheckersUtil.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ data class TestFrameworkConfiguration(
5252
// junit4 doesn't support parametrized tests
5353
if (testFramework == Junit4 && parametrizedTestSource == ParametrizedTestSource.PARAMETRIZE) return true
5454

55+
//if we do not use mocks at all, we do not use static mocking too
56+
if (mockStrategy == NO_MOCKS && staticsMocking == MockitoStaticMocking) return true
57+
5558
// if we want to generate mocks for every class but CUT, we must have specified staticsMocking
5659
if (mockStrategy == OTHER_CLASSES && staticsMocking == NoStaticMocking) return true
5760

0 commit comments

Comments
 (0)