Skip to content

Improve the readability of integration test generated for Spring controller #2564

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 3 commits into from
Aug 29, 2023
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 @@ -720,34 +720,17 @@ object SpringModelUtils {
methodId: MethodId,
arguments: List<UtModel>,
idGenerator: () -> Int
): List< Pair<UtPrimitiveModel, UtAssembleModel> > {
): List<Pair<UtPrimitiveModel, UtArrayModel>> {
val requestParams = collectArgumentsWithAnnotationModels(methodId, requestParamClassId, arguments)

return requestParams.map { (name, value) ->
Pair(UtPrimitiveModel(name),
UtAssembleModel(
UtArrayModel(
id = idGenerator(),
classId = listClassId,
modelName = "queryParams",
instantiationCall = UtExecutableCallModel(
instance = null,
executable = constructorId(java.util.ArrayList::class.id),
params = emptyList()
),
modificationsChainProvider = {
listOf(
UtExecutableCallModel(
instance = this,
executable = methodId(
classId = listClassId,
name = "add",
returnType = booleanClassId,
arguments = arrayOf(Object::class.id),
),
params = listOf(value)
)
)
}
classId = getArrayClassIdByElementClassId(objectClassId),
length = 1,
constModel = UtNullModel(objectClassId),
stores = mutableMapOf(0 to value),
)
)
}
Expand Down Expand Up @@ -801,7 +784,7 @@ object SpringModelUtils {
private fun createUrlTemplateModel(
requestPath: String,
pathVariablesModel: UtAssembleModel,
requestParamModel: List<Pair<UtPrimitiveModel, UtAssembleModel>>,
requestParamModel: List<Pair<UtPrimitiveModel, UtArrayModel>>,
idGenerator: () -> Int
): UtModel {
val requestPathModel = UtPrimitiveModel(requestPath)
Expand Down Expand Up @@ -851,7 +834,7 @@ object SpringModelUtils {
executable = MethodId(
classId = uriComponentsBuilderClassId,
name = "queryParam",
parameters = listOf(stringClassId, collectionClassId),
parameters = listOf(stringClassId, getArrayClassIdByElementClassId(objectClassId)),
returnType = uriComponentsBuilderClassId
),
params = listOf(name, value),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ import org.utbot.framework.plugin.api.util.booleanClassId
import org.utbot.framework.plugin.api.util.booleanWrapperClassId
import org.utbot.framework.plugin.api.util.classClassId
import org.utbot.framework.plugin.api.util.defaultValueModel
import org.utbot.framework.plugin.api.util.executable
import org.utbot.framework.plugin.api.util.jField
import org.utbot.framework.plugin.api.util.findFieldByIdOrNull
import org.utbot.framework.plugin.api.util.id
Expand Down Expand Up @@ -188,7 +189,7 @@ open class CgVariableConstructor(val context: CgContext) :
}

for ((fieldId, fieldModel) in model.fields) {
val variableForField = getOrCreateVariable(fieldModel)
val variableForField = getOrCreateVariable(fieldModel, name = fieldId.name)
if (!variableForField.hasDefaultValue())
setFieldValue(obj, fieldId, variableForField)
}
Expand Down Expand Up @@ -259,9 +260,12 @@ open class CgVariableConstructor(val context: CgContext) :
for (statementModel in model.modificationsChain) {
when (statementModel) {
is UtDirectSetFieldModel -> {
val instance = declareOrGet(statementModel.instance)
val instance = getOrCreateVariable(statementModel.instance)
// fields here are supposed to be accessible, so we assign them directly without any checks
instance[statementModel.fieldId] `=` declareOrGet(statementModel.fieldModel)
instance[statementModel.fieldId] `=` getOrCreateVariable(
model = statementModel.fieldModel,
name = statementModel.fieldId.name,
)
}
is UtStatementCallModel -> {
val call = createCgExecutableCallFromUtExecutableCall(statementModel)
Expand All @@ -284,23 +288,22 @@ open class CgVariableConstructor(val context: CgContext) :
when (statementModel) {
is UtExecutableCallModel -> {
val executable = statementModel.executable
val paramNames = runCatching {
executable.executable.parameters.map { if (it.isNamePresent) it.name else null }
}.getOrNull()
val params = statementModel.params
val caller = statementModel.instance?.let { getOrCreateVariable(it) }
val args = params.mapIndexed { i, param ->
getOrCreateVariable(param, name = paramNames?.getOrNull(i))
}

when (executable) {
is MethodId -> {
val caller = statementModel.instance?.let { declareOrGet(it) }
val args = params.map { declareOrGet(it) }
caller[executable](*args.toTypedArray())
}

is ConstructorId -> {
val args = params.map { declareOrGet(it) }
executable(*args.toTypedArray())
}
is MethodId -> caller[executable](*args.toTypedArray())
is ConstructorId -> executable(*args.toTypedArray())
}
}
is UtDirectGetFieldModel -> {
val instance = declareOrGet(statementModel.instance)
val instance = getOrCreateVariable(statementModel.instance)
val fieldAccess = statementModel.fieldAccess
utilsClassId[getFieldValue](instance, fieldAccess.fieldId.declaringClass.canonicalName, fieldAccess.fieldId.name)
}
Expand Down Expand Up @@ -508,13 +511,6 @@ open class CgVariableConstructor(val context: CgContext) :
return newVar(Class::class.id, baseName) { init }
}

/**
* Either declares a new variable or gets it from context's cache
* Returns the obtained variable
*/
private fun declareOrGet(model: UtModel): CgValue =
valueByUtModelWrapper[model.wrap()] ?: getOrCreateVariable(model)

private fun basicForLoop(start: Any, until: Any, body: (i: CgExpression) -> Unit) {
forLoop {
val (i, init) = loopInitialization(intClassId, "i", start.resolve())
Expand Down