Skip to content

Avoid set fields not present in UTest (for UtCompositeModel) #2698

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
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 @@ -1029,7 +1029,7 @@ open class ClassId @JvmOverloads constructor(
// so we create a specific name for them
isAnonymous -> "Anonymous${supertypeOfAnonymousClass.prettifiedName}"
// in other cases where canonical name is still null, we use ClassId.name instead
else -> jClass.canonicalName ?: name // Explicit jClass reference to get null instead of exception
else -> runCatching { canonicalName }.getOrElse { name }
}
return baseName
.substringAfterLast(".")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@ import org.utbot.framework.plugin.api.ClassId
import org.utbot.framework.plugin.api.Coverage
import org.utbot.framework.plugin.api.EnvironmentModels
import org.utbot.framework.plugin.api.ExecutableId
import org.utbot.framework.plugin.api.FieldId
import org.utbot.framework.plugin.api.Instruction
import org.utbot.framework.plugin.api.UtArrayModel
import org.utbot.framework.plugin.api.UtAssembleModel
import org.utbot.framework.plugin.api.UtCompositeModel
import org.utbot.framework.plugin.api.UtExecutableCallModel
import org.utbot.framework.plugin.api.UtExecution
import org.utbot.framework.plugin.api.UtExecutionFailure
Expand Down Expand Up @@ -170,7 +172,25 @@ class JcToUtExecutionConverter(
utilMethodProvider.setFieldMethodId == (it as? UtStatementCallModel)?.statement
}
) {
model.origin ?: model
UtCompositeModel(
id = model.id,
classId = model.classId,
isMock = false,
fields = model.modificationsChain.associateTo(mutableMapOf()) {
// `setFieldMethodId` call example for reference:
// setField(outputStream, "java.io.ByteArrayOutputStream", "buf", buf);

val params = (it as UtStatementCallModel).params
val fieldId = FieldId(
declaringClass = ClassId((params[1] as UtPrimitiveModel).value as String),
name = ((params[2] as UtPrimitiveModel).value as String)
)
// We prefer `model.origin?.fields?.get(fieldId)` over `params[3]`, because
// - `model.origin?.fields?.get(fieldId)` is created from concrete execution initial state
// - `params[3]` is created from jcMachine output, which could be a bit off
fieldId to (model.origin?.fields?.get(fieldId) ?: params[3])
}
)
} else {
model
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ class UTestInstToUtModelConverter(
executable = utilMethodProvider.getFieldValueMethodId,
params = listOf(
instanceModel,
UtPrimitiveModel(uTestExpr.field.type),
UtPrimitiveModel(uTestExpr.field.enclosingClass.classId.name),
UtPrimitiveModel(uTestExpr.field.name),
),
)
Expand All @@ -258,7 +258,7 @@ class UTestInstToUtModelConverter(
instance = null,
executable = utilMethodProvider.getStaticFieldValueMethodId,
params = listOf(
UtPrimitiveModel(uTestExpr.field.type),
UtPrimitiveModel(uTestExpr.field.enclosingClass.classId.name),
UtPrimitiveModel(uTestExpr.field.name),
),
)
Expand Down