Skip to content

Commit 0eaf9ac

Browse files
authored
Avoid set fields not present in UTest (for UtCompositeModel) (#2698)
1 parent 0a3db6a commit 0eaf9ac

File tree

3 files changed

+24
-4
lines changed

3 files changed

+24
-4
lines changed

utbot-framework-api/src/main/kotlin/org/utbot/framework/plugin/api/Api.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1029,7 +1029,7 @@ open class ClassId @JvmOverloads constructor(
10291029
// so we create a specific name for them
10301030
isAnonymous -> "Anonymous${supertypeOfAnonymousClass.prettifiedName}"
10311031
// in other cases where canonical name is still null, we use ClassId.name instead
1032-
else -> jClass.canonicalName ?: name // Explicit jClass reference to get null instead of exception
1032+
else -> runCatching { canonicalName }.getOrElse { name }
10331033
}
10341034
return baseName
10351035
.substringAfterLast(".")

utbot-junit-contest/src/main/kotlin/org/utbot/contest/usvm/converter/JcToUtExecutionConverter.kt

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,11 @@ import org.utbot.framework.plugin.api.ClassId
2424
import org.utbot.framework.plugin.api.Coverage
2525
import org.utbot.framework.plugin.api.EnvironmentModels
2626
import org.utbot.framework.plugin.api.ExecutableId
27+
import org.utbot.framework.plugin.api.FieldId
2728
import org.utbot.framework.plugin.api.Instruction
2829
import org.utbot.framework.plugin.api.UtArrayModel
2930
import org.utbot.framework.plugin.api.UtAssembleModel
31+
import org.utbot.framework.plugin.api.UtCompositeModel
3032
import org.utbot.framework.plugin.api.UtExecutableCallModel
3133
import org.utbot.framework.plugin.api.UtExecution
3234
import org.utbot.framework.plugin.api.UtExecutionFailure
@@ -170,7 +172,25 @@ class JcToUtExecutionConverter(
170172
utilMethodProvider.setFieldMethodId == (it as? UtStatementCallModel)?.statement
171173
}
172174
) {
173-
model.origin ?: model
175+
UtCompositeModel(
176+
id = model.id,
177+
classId = model.classId,
178+
isMock = false,
179+
fields = model.modificationsChain.associateTo(mutableMapOf()) {
180+
// `setFieldMethodId` call example for reference:
181+
// setField(outputStream, "java.io.ByteArrayOutputStream", "buf", buf);
182+
183+
val params = (it as UtStatementCallModel).params
184+
val fieldId = FieldId(
185+
declaringClass = ClassId((params[1] as UtPrimitiveModel).value as String),
186+
name = ((params[2] as UtPrimitiveModel).value as String)
187+
)
188+
// We prefer `model.origin?.fields?.get(fieldId)` over `params[3]`, because
189+
// - `model.origin?.fields?.get(fieldId)` is created from concrete execution initial state
190+
// - `params[3]` is created from jcMachine output, which could be a bit off
191+
fieldId to (model.origin?.fields?.get(fieldId) ?: params[3])
192+
}
193+
)
174194
} else {
175195
model
176196
}

utbot-junit-contest/src/main/kotlin/org/utbot/contest/usvm/converter/UTestInstToUtModelConverter.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ class UTestInstToUtModelConverter(
240240
executable = utilMethodProvider.getFieldValueMethodId,
241241
params = listOf(
242242
instanceModel,
243-
UtPrimitiveModel(uTestExpr.field.type),
243+
UtPrimitiveModel(uTestExpr.field.enclosingClass.classId.name),
244244
UtPrimitiveModel(uTestExpr.field.name),
245245
),
246246
)
@@ -258,7 +258,7 @@ class UTestInstToUtModelConverter(
258258
instance = null,
259259
executable = utilMethodProvider.getStaticFieldValueMethodId,
260260
params = listOf(
261-
UtPrimitiveModel(uTestExpr.field.type),
261+
UtPrimitiveModel(uTestExpr.field.enclosingClass.classId.name),
262262
UtPrimitiveModel(uTestExpr.field.name),
263263
),
264264
)

0 commit comments

Comments
 (0)