Skip to content
Merged
Prev Previous commit
Next Next commit
Only use InjectMockValueProvider for thisInstance
  • Loading branch information
IlyaMuravjov committed Sep 19, 2023
commit badfd3b7a98c9d178a1ab1821a18431791934913
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,36 @@ import org.utbot.fuzzer.fuzzed
import org.utbot.fuzzing.FuzzedDescription
import org.utbot.fuzzing.JavaValueProvider
import org.utbot.fuzzing.Routine
import org.utbot.fuzzing.Scope
import org.utbot.fuzzing.ScopeProperty
import org.utbot.fuzzing.Seed
import org.utbot.fuzzing.toFuzzerType

val INJECT_MOCK_FLAG = ScopeProperty<Unit>(
"INJECT_MOCK_FLAG is present if composite model should be used (i.e. thisInstance is being created)"
)

/**
* Models created by this class can be used with `@InjectMock` annotation, because
* they are [UtCompositeModel]s similar to the ones created by the symbolic engine.
*
* This class only creates models for thisInstance of type [classUnderTest].
*/
class InjectMockValueProvider(
private val idGenerator: IdGenerator<Int>,
private val classToUseCompositeModelFor: ClassId
private val classUnderTest: ClassId
) : JavaValueProvider {
override fun accept(type: FuzzedType): Boolean = type.classId == classToUseCompositeModelFor
override fun enrich(description: FuzzedDescription, type: FuzzedType, scope: Scope) {
// any value except this
if (description.description.isStatic == false && scope.parameterIndex == 0 && scope.recursionDepth == 1) {
scope.putProperty(INJECT_MOCK_FLAG, Unit)
}
}

override fun accept(type: FuzzedType): Boolean = type.classId == classUnderTest

override fun generate(description: FuzzedDescription, type: FuzzedType): Sequence<Seed<FuzzedType, FuzzedValue>> {
if (description.scope?.getProperty(INJECT_MOCK_FLAG) == null) return emptySequence()
val fields = type.classId.allDeclaredFieldIds.filterNot { it.isStatic && it.isFinal }.toList()
return sequenceOf(Seed.Recursive(
construct = Routine.Create(types = fields.map { toFuzzerType(it.jField.genericType, description.typeCache) }) { values ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class SpringApplicationContextImpl(
.transformValueProvider { origValueProvider ->
InjectMockValueProvider(
idGenerator = fuzzingContext.idGenerator,
classToUseCompositeModelFor = fuzzingContext.classUnderTest
classUnderTest = fuzzingContext.classUnderTest
)
.withFallback(origValueProvider)
.replaceTypes { description, type ->
Expand Down