Skip to content

Lessen thisInstance creation in parameterized test generation #1065

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Oct 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1367,7 +1367,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 @@ -1429,20 +1431,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 @@ -1554,9 +1542,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