Other class is not mocked as required #747#1033
Conversation
7a7eeb8 to
35408f2
Compare
|
|
||
| } | ||
|
|
||
| internal fun MethodId.toFuzzerMockable(block: suspend SequenceScope<Pair<MethodId, List<UtModel>>>.() -> Unit): FuzzerMockableMethodId { |
There was a problem hiding this comment.
Could you make a data class for these pairs?
There was a problem hiding this comment.
"Pair" is used as convenient and well-known construction like "key to value". Because this method is used by dsl I'd prefer to keep it this way.
Also, I have some doubt about using data classes for such small pairs. Maybe, typealias is more proper way in these cases?
| constructorId.classId, | ||
| g.name, | ||
| g.returnType.id, | ||
| emptyList() |
There was a problem hiding this comment.
Please, use a named argument here
| setterAndGetter?.first, | ||
| setterAndGetter?.second, |
There was a problem hiding this comment.
And here (a data class). From a browser, it's not obvious which one is what.
| values[index].add(model) | ||
| val mock = replaceToMock(model.model, description.shouldMock) | ||
| values[index].add(FuzzedValue(mock, model.createdBy).apply { | ||
| summary = model.summary |
There was a problem hiding this comment.
Why is it not a part of the constructor?
| fun replaceToMock(assembleModel: UtModel, shouldMock: (ClassId) -> Boolean): UtModel { | ||
| if (assembleModel !is UtAssembleModel) return assembleModel | ||
| if (shouldMock(assembleModel.classId)) { | ||
| return UtCompositeModel(assembleModel.id, assembleModel.classId, true).apply { | ||
| assembleModel.modificationsChain.forEach { | ||
| if (it is UtDirectSetFieldModel) { | ||
| fields[it.fieldId] = replaceToMock(it.fieldModel, shouldMock) | ||
| } | ||
| if (it is UtExecutableCallModel && it.executable is FuzzerMockableMethodId) { | ||
| (it.executable as FuzzerMockableMethodId).mock().forEach { (executionId, models) -> | ||
| mocks[executionId] = models.map { p -> replaceToMock(p, shouldMock) } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } else { | ||
| val models = assembleModel.modificationsChain.map { call -> | ||
| var mockedStatementModel: UtStatementModel? = null | ||
| if (call is UtDirectSetFieldModel) { | ||
| val mock = replaceToMock(call.fieldModel, shouldMock) | ||
| if (mock != call.fieldModel) { | ||
| mockedStatementModel = UtDirectSetFieldModel(call.instance, call.fieldId, mock) | ||
| } | ||
| } else if (call is UtExecutableCallModel) { | ||
| val params = call.params.map { m -> replaceToMock(m, shouldMock) } | ||
| if (params != call.params) { | ||
| mockedStatementModel = UtExecutableCallModel(call.instance, call.executable, params) | ||
| } | ||
| } |
There was a problem hiding this comment.
As for me, it is hard to read because of missing spaces. My suggestion is something like:
fun replaceToMock(
assembleModel: UtModel,
shouldMock: (ClassId) -> Boolean
): UtModel = with(assembleModel) {
if (this !is UtAssembleModel) return this
if (shouldMock(classId)) {
UtCompositeModel(id, classId, isMock = true).apply {
modificationsChain.forEach {
if (it is UtDirectSetFieldModel) {
fields[it.fieldId] = replaceToMock(it.fieldModel, shouldMock)
}
if (it is UtExecutableCallModel && it.executable is FuzzerMockableMethodId) {
(it.executable as FuzzerMockableMethodId).mock().forEach { (executionId, models) ->
mocks[executionId] = models.map { p -> replaceToMock(p, shouldMock) }
}
}
}
}
} else {
val models = modificationsChain.map { call ->
var mockedStatementModel: UtStatementModel? = null
if (call is UtDirectSetFieldModel) {
val mock = replaceToMock(call.fieldModel, shouldMock)
if (mock != call.fieldModel) {
mockedStatementModel = UtDirectSetFieldModel(call.instance, call.fieldId, mock)
}
} else if (call is UtExecutableCallModel) {
val params = call.params.map { m -> replaceToMock(m, shouldMock) }
if (params != call.params) {
mockedStatementModel = UtExecutableCallModel(call.instance, call.executable, params)
}
}
mockedStatementModel ?: call
}
with(assembleModel) {
UtAssembleModel(id, classId, modelName, instantiationCall, origin) { models }
}
}
}a899cec to
23dc52a
Compare
Description
Adds mocks for values that are passed into a method by fuzzer. Note, that fuzzer cannot mock any internal values if they are met in the code block.
Fixes #747
Type of Change
How Has This Been Tested?
Automated Testing
org.utbot.framework.plugin.api.MockOfObjectModelProviderTest
Manual Scenario
Reproduce examples from the issue.
Checklist: