Skip to content

Commit

Permalink
Lessen thisInstance creation in parameterized test generation (UnitTe…
Browse files Browse the repository at this point in the history
…stBot#1065)

* Lessen thisInstance creation in parameterized test generation

* Avoid incorrect configuration

Co-authored-by: Egor Kulikov <egor.k.kulikov@gmail.com>
  • Loading branch information
2 people authored and AbdullinAM committed Oct 17, 2022
1 parent 5fb1e16 commit ad150ba
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1337,7 +1337,9 @@ internal class CgMethodConstructor(val context: CgContext) : CgContextOwner by c
substituteStaticFields(statics, isParametrized = true)

// build this instance
thisInstance = genericExecution.stateBefore.thisInstance?.let { currentMethodParameters[CgParameterKind.ThisInstance] }
thisInstance = genericExecution.stateBefore.thisInstance?.let {
variableConstructor.getOrCreateVariable(it)
}

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

return mutableListOf<CgParameterDeclaration>().apply {
// this instance
val thisInstanceModel = genericExecution.stateBefore.thisInstance
if (thisInstanceModel != null) {
val type = wrapTypeIfRequired(thisInstanceModel.classId)
val thisInstance = CgParameterDeclaration(
parameter = declareParameter(
type = type,
name = nameGenerator.variableName(type)
),
isReferenceType = true
)
this += thisInstance
currentMethodParameters[CgParameterKind.ThisInstance] = thisInstance.parameter
}
// arguments
for (index in genericExecution.stateBefore.parameters.indices) {
val argumentName = paramNames[executableUnderTest]?.get(index)
Expand Down Expand Up @@ -1524,9 +1512,6 @@ internal class CgMethodConstructor(val context: CgContext) : CgContextOwner by c

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

for ((paramIndex, paramModel) in execution.stateBefore.parameters.withIndex()) {
val argumentName = paramNames[testSet.executableId]?.get(paramIndex)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -745,13 +745,11 @@ data class CgParameterDeclaration(

/**
* Test method parameter can be one of the following types:
* - this instance for method under test (MUT)
* - argument of MUT with a certain index
* - result expected from MUT with the given arguments
* - exception expected from MUT with the given arguments
*/
sealed class CgParameterKind {
object ThisInstance : CgParameterKind()
data class Argument(val index: Int) : CgParameterKind()
data class Statics(val model: UtModel) : CgParameterKind()
object ExpectedResult : CgParameterKind()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ data class TestFrameworkConfiguration(
// junit4 doesn't support parametrized tests
if (testFramework == Junit4 && parametrizedTestSource == ParametrizedTestSource.PARAMETRIZE) return true

//if we do not use mocks at all, we do not use static mocking too
if (mockStrategy == NO_MOCKS && staticsMocking == MockitoStaticMocking) return true

// if we want to generate mocks for every class but CUT, we must have specified staticsMocking
if (mockStrategy == OTHER_CLASSES && staticsMocking == NoStaticMocking) return true

Expand Down

0 comments on commit ad150ba

Please sign in to comment.