Skip to content

Commit

Permalink
Only use InjectMockValueProvider for thisInstance
Browse files Browse the repository at this point in the history
  • Loading branch information
IlyaMuravjov committed Aug 29, 2023
1 parent 047a3f8 commit 5f114ce
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ class SpringApplicationContextImpl(
.transformValueProvider { origValueProvider ->
InjectMockValueProvider(
idGenerator = fuzzingContext.idGenerator,
classToUseCompositeModelFor = fuzzingContext.classUnderTest
classUnderTest = fuzzingContext.classUnderTest
)
.withFallback(origValueProvider)
.replaceTypes { description, type ->
Expand Down
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

0 comments on commit 5f114ce

Please sign in to comment.