diff --git a/utbot-framework/src/main/kotlin/org/utbot/engine/Traverser.kt b/utbot-framework/src/main/kotlin/org/utbot/engine/Traverser.kt index 75e319a943..33d9ca61ce 100644 --- a/utbot-framework/src/main/kotlin/org/utbot/engine/Traverser.kt +++ b/utbot-framework/src/main/kotlin/org/utbot/engine/Traverser.kt @@ -1397,7 +1397,7 @@ class Traverser( // from Spring bean definitions, for example), we can just create a symbolic object // with hard constraint on the mentioned type. val replacedClassId = when (typeReplacer.typeReplacementMode) { - KnownImplementor -> typeReplacer.replaceTypeIfNeeded(type) + KnownImplementor -> typeReplacer.replaceTypeIfNeeded(type.id) AnyImplementor, NoImplementors -> null } diff --git a/utbot-framework/src/main/kotlin/org/utbot/engine/UtBotSymbolicEngine.kt b/utbot-framework/src/main/kotlin/org/utbot/engine/UtBotSymbolicEngine.kt index 6ceb5ca7ba..014afdcb5d 100644 --- a/utbot-framework/src/main/kotlin/org/utbot/engine/UtBotSymbolicEngine.kt +++ b/utbot-framework/src/main/kotlin/org/utbot/engine/UtBotSymbolicEngine.kt @@ -423,7 +423,6 @@ class UtBotSymbolicEngine( * Run fuzzing flow. * * @param until is used by fuzzer to cancel all tasks if the current time is over this value - * @param transform provides model values for a method */ fun fuzzing(until: Long = Long.MAX_VALUE) = flow { val isFuzzable = methodUnderTest.parameters.all { classId -> @@ -440,7 +439,12 @@ class UtBotSymbolicEngine( var testEmittedByFuzzer = 0 val fuzzingContext = try { - concreteExecutionContext.tryCreateFuzzingContext(concreteExecutor, classUnderTest, defaultIdGenerator) + concreteExecutionContext.tryCreateFuzzingContext( + concreteExecutor, + classUnderTest, + mockStrategy, + defaultIdGenerator, + ) } catch (e: Exception) { emit(UtError(e.message ?: "Failed to create ValueProvider", e)) return@flow diff --git a/utbot-framework/src/main/kotlin/org/utbot/framework/context/ConcreteExecutionContext.kt b/utbot-framework/src/main/kotlin/org/utbot/framework/context/ConcreteExecutionContext.kt index 04ca919530..9631af3713 100644 --- a/utbot-framework/src/main/kotlin/org/utbot/framework/context/ConcreteExecutionContext.kt +++ b/utbot-framework/src/main/kotlin/org/utbot/framework/context/ConcreteExecutionContext.kt @@ -1,5 +1,6 @@ package org.utbot.framework.context +import org.utbot.engine.MockStrategy import org.utbot.framework.plugin.api.ClassId import org.utbot.framework.plugin.api.ConcreteContextLoadingResult import org.utbot.framework.plugin.api.UtExecution @@ -23,6 +24,7 @@ interface ConcreteExecutionContext { fun tryCreateFuzzingContext( concreteExecutor: ConcreteExecutor, classUnderTest: ClassId, + mockStrategy: MockStrategy, idGenerator: IdentityPreservingIdGenerator, ): JavaFuzzingContext } \ No newline at end of file diff --git a/utbot-framework/src/main/kotlin/org/utbot/framework/context/JavaFuzzingContext.kt b/utbot-framework/src/main/kotlin/org/utbot/framework/context/JavaFuzzingContext.kt index 64219d260e..a1eda3ab9a 100644 --- a/utbot-framework/src/main/kotlin/org/utbot/framework/context/JavaFuzzingContext.kt +++ b/utbot-framework/src/main/kotlin/org/utbot/framework/context/JavaFuzzingContext.kt @@ -1,5 +1,6 @@ package org.utbot.framework.context +import org.utbot.engine.MockStrategy import org.utbot.framework.plugin.api.ClassId import org.utbot.framework.plugin.api.EnvironmentModels import org.utbot.framework.plugin.api.ExecutableId @@ -11,6 +12,7 @@ import org.utbot.instrumentation.instrumentation.execution.UtConcreteExecutionRe interface JavaFuzzingContext { val classUnderTest: ClassId + val mockStrategy: MockStrategy val idGenerator: IdentityPreservingIdGenerator val valueProvider: JavaValueProvider diff --git a/utbot-framework/src/main/kotlin/org/utbot/framework/context/TypeReplacer.kt b/utbot-framework/src/main/kotlin/org/utbot/framework/context/TypeReplacer.kt index 52c3ee8604..25aec4d025 100644 --- a/utbot-framework/src/main/kotlin/org/utbot/framework/context/TypeReplacer.kt +++ b/utbot-framework/src/main/kotlin/org/utbot/framework/context/TypeReplacer.kt @@ -2,7 +2,6 @@ package org.utbot.framework.context import org.utbot.framework.plugin.api.ClassId import org.utbot.framework.plugin.api.TypeReplacementMode -import soot.RefType interface TypeReplacer { /** @@ -14,5 +13,5 @@ interface TypeReplacer { * Finds a type to replace the original abstract type * if it is guided with some additional information. */ - fun replaceTypeIfNeeded(type: RefType): ClassId? + fun replaceTypeIfNeeded(classId: ClassId): ClassId? } \ No newline at end of file diff --git a/utbot-framework/src/main/kotlin/org/utbot/framework/context/custom/MockingJavaFuzzingContext.kt b/utbot-framework/src/main/kotlin/org/utbot/framework/context/custom/MockingJavaFuzzingContext.kt index cb50b7a861..6c14cdba66 100644 --- a/utbot-framework/src/main/kotlin/org/utbot/framework/context/custom/MockingJavaFuzzingContext.kt +++ b/utbot-framework/src/main/kotlin/org/utbot/framework/context/custom/MockingJavaFuzzingContext.kt @@ -2,38 +2,48 @@ package org.utbot.framework.context.custom import org.utbot.framework.context.JavaFuzzingContext import org.utbot.fuzzing.JavaValueProvider +import org.utbot.fuzzing.providers.AnyDepthNullValueProvider import org.utbot.fuzzing.providers.MapValueProvider import org.utbot.fuzzing.spring.unit.MockValueProvider import org.utbot.fuzzing.providers.NullValueProvider import org.utbot.fuzzing.providers.ObjectValueProvider import org.utbot.fuzzing.providers.StringValueProvider +import org.utbot.fuzzing.providers.anyObjectValueProvider +import org.utbot.fuzzing.spring.decorators.filterTypes import org.utbot.instrumentation.instrumentation.execution.UtConcreteExecutionResult /** - * Makes fuzzer mock all types that don't have *specific* [JavaValueProvider], - * like [MapValueProvider] or [StringValueProvider]. + * Allows fuzzer to use mocks in accordance with [JavaFuzzingContext.mockStrategy]. * - * NOTE: the caller is responsible for providing some *specific* [JavaValueProvider] - * that can create values for class under test (otherwise it will be mocked), - * [ObjectValueProvider] and [NullValueProvider] do not count as *specific*. + * NOTE: + * - fuzzer won't mock types, that have *specific* value providers (e.g. [MapValueProvider] and [StringValueProvider]) + * - [ObjectValueProvider] and [NullValueProvider] do not count as *specific* value providers */ -fun JavaFuzzingContext.mockAllTypesWithoutSpecificValueProvider() = +fun JavaFuzzingContext.allowMocks() = MockingJavaFuzzingContext(delegateContext = this) class MockingJavaFuzzingContext( - val delegateContext: JavaFuzzingContext + val delegateContext: JavaFuzzingContext, ) : JavaFuzzingContext by delegateContext { private val mockValueProvider = MockValueProvider(delegateContext.idGenerator) override val valueProvider: JavaValueProvider = - // NOTE: we first remove `NullValueProvider` from `delegateContext.valueProvider` and then - // add it back as a part of our `withFallback` so it has the same priority as - // `mockValueProvider`, otherwise mocks will never be used where `null` can be used. + // NOTE: we first remove `NullValueProvider` and `ObjectValueProvider` from `delegateContext.valueProvider` + // and then add them back as a part of our `withFallback` so they have the same priority as + // `mockValueProvider`, otherwise mocks will never be used where `null` or new object can be used. delegateContext.valueProvider .except { it is NullValueProvider } .except { it is ObjectValueProvider } .withFallback( mockValueProvider + .filterTypes { type -> + mockStrategy.eligibleToMock( + classToMock = type.classId, + classUnderTest = classUnderTest + ) + } + .with(anyObjectValueProvider(idGenerator)) + .withFallback(mockValueProvider.with(AnyDepthNullValueProvider)) .with(NullValueProvider) ) diff --git a/utbot-framework/src/main/kotlin/org/utbot/framework/context/simple/SimpleConcreteExecutionContext.kt b/utbot-framework/src/main/kotlin/org/utbot/framework/context/simple/SimpleConcreteExecutionContext.kt index 503b6fee93..68df4e0bf8 100644 --- a/utbot-framework/src/main/kotlin/org/utbot/framework/context/simple/SimpleConcreteExecutionContext.kt +++ b/utbot-framework/src/main/kotlin/org/utbot/framework/context/simple/SimpleConcreteExecutionContext.kt @@ -1,5 +1,6 @@ package org.utbot.framework.context.simple +import org.utbot.engine.MockStrategy import org.utbot.framework.context.ConcreteExecutionContext import org.utbot.framework.context.JavaFuzzingContext import org.utbot.framework.plugin.api.ClassId @@ -28,6 +29,7 @@ class SimpleConcreteExecutionContext(fullClassPath: String) : ConcreteExecutionC override fun tryCreateFuzzingContext( concreteExecutor: ConcreteExecutor, classUnderTest: ClassId, + mockStrategy: MockStrategy, idGenerator: IdentityPreservingIdGenerator - ): JavaFuzzingContext = SimpleJavaFuzzingContext(classUnderTest, idGenerator) + ): JavaFuzzingContext = SimpleJavaFuzzingContext(classUnderTest, mockStrategy, idGenerator) } \ No newline at end of file diff --git a/utbot-framework/src/main/kotlin/org/utbot/framework/context/simple/SimpleJavaFuzzingContext.kt b/utbot-framework/src/main/kotlin/org/utbot/framework/context/simple/SimpleJavaFuzzingContext.kt index 4c972f309c..2ec8f7f0c1 100644 --- a/utbot-framework/src/main/kotlin/org/utbot/framework/context/simple/SimpleJavaFuzzingContext.kt +++ b/utbot-framework/src/main/kotlin/org/utbot/framework/context/simple/SimpleJavaFuzzingContext.kt @@ -1,5 +1,6 @@ package org.utbot.framework.context.simple +import org.utbot.engine.MockStrategy import org.utbot.framework.context.JavaFuzzingContext import org.utbot.framework.plugin.api.ClassId import org.utbot.framework.plugin.api.EnvironmentModels @@ -14,6 +15,7 @@ import org.utbot.instrumentation.instrumentation.execution.UtConcreteExecutionRe class SimpleJavaFuzzingContext( override val classUnderTest: ClassId, + override val mockStrategy: MockStrategy, override val idGenerator: IdentityPreservingIdGenerator, ) : JavaFuzzingContext { override val valueProvider: JavaValueProvider = diff --git a/utbot-framework/src/main/kotlin/org/utbot/framework/context/simple/SimpleTypeReplacer.kt b/utbot-framework/src/main/kotlin/org/utbot/framework/context/simple/SimpleTypeReplacer.kt index 300aea86e5..3df1f5930e 100644 --- a/utbot-framework/src/main/kotlin/org/utbot/framework/context/simple/SimpleTypeReplacer.kt +++ b/utbot-framework/src/main/kotlin/org/utbot/framework/context/simple/SimpleTypeReplacer.kt @@ -3,10 +3,9 @@ package org.utbot.framework.context.simple import org.utbot.framework.context.TypeReplacer import org.utbot.framework.plugin.api.ClassId import org.utbot.framework.plugin.api.TypeReplacementMode -import soot.RefType class SimpleTypeReplacer : TypeReplacer { override val typeReplacementMode: TypeReplacementMode = TypeReplacementMode.AnyImplementor - override fun replaceTypeIfNeeded(type: RefType): ClassId? = null + override fun replaceTypeIfNeeded(classId: ClassId): ClassId? = null } \ No newline at end of file diff --git a/utbot-framework/src/main/kotlin/org/utbot/framework/context/spring/SpringApplicationContextImpl.kt b/utbot-framework/src/main/kotlin/org/utbot/framework/context/spring/SpringApplicationContextImpl.kt index f17bf1086a..9194f8f168 100644 --- a/utbot-framework/src/main/kotlin/org/utbot/framework/context/spring/SpringApplicationContextImpl.kt +++ b/utbot-framework/src/main/kotlin/org/utbot/framework/context/spring/SpringApplicationContextImpl.kt @@ -11,9 +11,9 @@ import org.utbot.framework.context.ConcreteExecutionContext import org.utbot.framework.context.NonNullSpeculator import org.utbot.framework.context.TypeReplacer import org.utbot.framework.context.custom.CoverageFilteringConcreteExecutionContext -import org.utbot.framework.context.custom.mockAllTypesWithoutSpecificValueProvider +import org.utbot.framework.context.custom.allowMocks import org.utbot.framework.context.utils.transformJavaFuzzingContext -import org.utbot.framework.context.utils.withValueProvider +import org.utbot.framework.context.utils.transformValueProvider import org.utbot.framework.plugin.api.BeanDefinitionData import org.utbot.framework.plugin.api.ClassId import org.utbot.framework.plugin.api.ConcreteContextLoadingResult @@ -24,7 +24,9 @@ import org.utbot.framework.plugin.api.util.allSuperTypes import org.utbot.framework.plugin.api.util.id import org.utbot.framework.plugin.api.util.jClass import org.utbot.framework.plugin.api.util.utContext +import org.utbot.fuzzing.spring.decorators.replaceTypes import org.utbot.fuzzing.spring.unit.InjectMockValueProvider +import org.utbot.fuzzing.toFuzzerType class SpringApplicationContextImpl( private val delegateContext: ApplicationContext, @@ -64,13 +66,20 @@ class SpringApplicationContextImpl( return when (springTestType) { SpringTestType.UNIT_TEST -> delegateConcreteExecutionContext.transformJavaFuzzingContext { fuzzingContext -> fuzzingContext - .withValueProvider( + .allowMocks() + .transformValueProvider { origValueProvider -> InjectMockValueProvider( idGenerator = fuzzingContext.idGenerator, classToUseCompositeModelFor = fuzzingContext.classUnderTest ) - ) - .mockAllTypesWithoutSpecificValueProvider() + .withFallback(origValueProvider) + .replaceTypes { description, type -> + typeReplacer.replaceTypeIfNeeded(type.classId)?.let { replacement -> + // TODO infer generic type + toFuzzerType(replacement.jClass, description.typeCache) + } ?: type + } + } } SpringTestType.INTEGRATION_TEST -> SpringIntegrationTestConcreteExecutionContext( delegateConcreteExecutionContext, diff --git a/utbot-framework/src/main/kotlin/org/utbot/framework/context/spring/SpringIntegrationTestConcreteExecutionContext.kt b/utbot-framework/src/main/kotlin/org/utbot/framework/context/spring/SpringIntegrationTestConcreteExecutionContext.kt index fdfd8ba2bd..836e677011 100644 --- a/utbot-framework/src/main/kotlin/org/utbot/framework/context/spring/SpringIntegrationTestConcreteExecutionContext.kt +++ b/utbot-framework/src/main/kotlin/org/utbot/framework/context/spring/SpringIntegrationTestConcreteExecutionContext.kt @@ -1,6 +1,7 @@ package org.utbot.framework.context.spring import mu.KotlinLogging +import org.utbot.engine.MockStrategy import org.utbot.framework.context.ConcreteExecutionContext import org.utbot.framework.context.JavaFuzzingContext import org.utbot.framework.plugin.api.ClassId @@ -55,6 +56,7 @@ class SpringIntegrationTestConcreteExecutionContext( override fun tryCreateFuzzingContext( concreteExecutor: ConcreteExecutor, classUnderTest: ClassId, + mockStrategy: MockStrategy, idGenerator: IdentityPreservingIdGenerator ): JavaFuzzingContext { if (springApplicationContext.getBeansAssignableTo(classUnderTest).isEmpty()) @@ -68,7 +70,12 @@ class SpringIntegrationTestConcreteExecutionContext( logger.info { "Detected relevant repositories for class $classUnderTest: $relevantRepositories" } return SpringIntegrationTestJavaFuzzingContext( - delegateContext = delegateContext.tryCreateFuzzingContext(concreteExecutor, classUnderTest, idGenerator), + delegateContext = delegateContext.tryCreateFuzzingContext( + concreteExecutor, + classUnderTest, + mockStrategy, + idGenerator, + ), relevantRepositories = relevantRepositories, springApplicationContext = springApplicationContext, ) diff --git a/utbot-framework/src/main/kotlin/org/utbot/framework/context/spring/SpringTypeReplacer.kt b/utbot-framework/src/main/kotlin/org/utbot/framework/context/spring/SpringTypeReplacer.kt index 8fca71c751..b8a9e7de77 100644 --- a/utbot-framework/src/main/kotlin/org/utbot/framework/context/spring/SpringTypeReplacer.kt +++ b/utbot-framework/src/main/kotlin/org/utbot/framework/context/spring/SpringTypeReplacer.kt @@ -3,10 +3,8 @@ package org.utbot.framework.context.spring import org.utbot.framework.context.TypeReplacer import org.utbot.framework.plugin.api.ClassId import org.utbot.framework.plugin.api.TypeReplacementMode -import org.utbot.framework.plugin.api.id -import org.utbot.framework.plugin.api.isAbstractType +import org.utbot.framework.plugin.api.util.isAbstract import org.utbot.framework.plugin.api.util.isSubtypeOf -import soot.RefType class SpringTypeReplacer( private val delegateTypeReplacer: TypeReplacer, @@ -19,7 +17,7 @@ class SpringTypeReplacer( else TypeReplacementMode.NoImplementors - override fun replaceTypeIfNeeded(type: RefType): ClassId? = - if (type.isAbstractType) springApplicationContext.injectedTypes.singleOrNull { it.isSubtypeOf(type.id) } - else delegateTypeReplacer.replaceTypeIfNeeded(type) + override fun replaceTypeIfNeeded(classId: ClassId): ClassId? = + if (classId.isAbstract) springApplicationContext.injectedTypes.singleOrNull { it.isSubtypeOf(classId) } + else delegateTypeReplacer.replaceTypeIfNeeded(classId) } \ No newline at end of file diff --git a/utbot-framework/src/main/kotlin/org/utbot/framework/context/utils/ConcreteExecutionContextUtils.kt b/utbot-framework/src/main/kotlin/org/utbot/framework/context/utils/ConcreteExecutionContextUtils.kt index 7ae25f10a9..0c9faad18a 100644 --- a/utbot-framework/src/main/kotlin/org/utbot/framework/context/utils/ConcreteExecutionContextUtils.kt +++ b/utbot-framework/src/main/kotlin/org/utbot/framework/context/utils/ConcreteExecutionContextUtils.kt @@ -1,5 +1,6 @@ package org.utbot.framework.context.utils +import org.utbot.engine.MockStrategy import org.utbot.framework.context.ConcreteExecutionContext import org.utbot.framework.context.JavaFuzzingContext import org.utbot.framework.plugin.api.ClassId @@ -15,9 +16,15 @@ fun ConcreteExecutionContext.transformJavaFuzzingContext( override fun tryCreateFuzzingContext( concreteExecutor: ConcreteExecutor, classUnderTest: ClassId, + mockStrategy: MockStrategy, idGenerator: IdentityPreservingIdGenerator ): JavaFuzzingContext = transformer( - this@transformJavaFuzzingContext.tryCreateFuzzingContext(concreteExecutor, classUnderTest, idGenerator) + this@transformJavaFuzzingContext.tryCreateFuzzingContext( + concreteExecutor, + classUnderTest, + mockStrategy, + idGenerator, + ) ) } diff --git a/utbot-java-fuzzing/src/main/kotlin/org/utbot/fuzzing/JavaLanguage.kt b/utbot-java-fuzzing/src/main/kotlin/org/utbot/fuzzing/JavaLanguage.kt index 6f3e600788..f5278473c5 100644 --- a/utbot-java-fuzzing/src/main/kotlin/org/utbot/fuzzing/JavaLanguage.kt +++ b/utbot-java-fuzzing/src/main/kotlin/org/utbot/fuzzing/JavaLanguage.kt @@ -171,7 +171,7 @@ internal fun FuzzedType.traverseHierarchy(typeCache: MutableMap): FuzzedType { +fun toFuzzerType(type: Type, cache: MutableMap): FuzzedType { return toFuzzerType( type = type, classId = { t -> toClassId(t, cache) }, diff --git a/utbot-java-fuzzing/src/main/kotlin/org/utbot/fuzzing/providers/ModifyingWithMethodsProviderWrapper.kt b/utbot-java-fuzzing/src/main/kotlin/org/utbot/fuzzing/providers/ModifyingWithMethodsProviderWrapper.kt index fa3ab0b4cc..c011e14926 100644 --- a/utbot-java-fuzzing/src/main/kotlin/org/utbot/fuzzing/providers/ModifyingWithMethodsProviderWrapper.kt +++ b/utbot-java-fuzzing/src/main/kotlin/org/utbot/fuzzing/providers/ModifyingWithMethodsProviderWrapper.kt @@ -9,8 +9,8 @@ import org.utbot.fuzzer.FuzzedValue import org.utbot.fuzzing.FuzzedDescription import org.utbot.fuzzing.JavaValueProvider import org.utbot.fuzzing.Routine -import org.utbot.fuzzing.Scope import org.utbot.fuzzing.Seed +import org.utbot.fuzzing.spring.decorators.ValueProviderDecorator /** * Value provider that is a buddy for another provider @@ -22,8 +22,11 @@ import org.utbot.fuzzing.Seed */ class ModifyingWithMethodsProviderWrapper( private val classUnderTest: ClassId, - private val delegate: JavaValueProvider -) : JavaValueProvider by delegate { + delegate: JavaValueProvider +) : ValueProviderDecorator(delegate) { + + override fun wrap(provider: JavaValueProvider): JavaValueProvider = + ModifyingWithMethodsProviderWrapper(classUnderTest, provider) override fun generate(description: FuzzedDescription, type: FuzzedType): Sequence> = delegate @@ -50,9 +53,4 @@ class ModifyingWithMethodsProviderWrapper( ) } else seed } - - override fun enrich(description: FuzzedDescription, type: FuzzedType, scope: Scope) = - delegate.enrich(description, type, scope) - - override fun accept(type: FuzzedType): Boolean = delegate.accept(type) } \ No newline at end of file diff --git a/utbot-java-fuzzing/src/main/kotlin/org/utbot/fuzzing/spring/PropertyPreservingValueProvider.kt b/utbot-java-fuzzing/src/main/kotlin/org/utbot/fuzzing/spring/PropertyPreservingValueProvider.kt index 986d6d2ead..27cf6e2d60 100644 --- a/utbot-java-fuzzing/src/main/kotlin/org/utbot/fuzzing/spring/PropertyPreservingValueProvider.kt +++ b/utbot-java-fuzzing/src/main/kotlin/org/utbot/fuzzing/spring/PropertyPreservingValueProvider.kt @@ -6,8 +6,8 @@ import org.utbot.fuzzer.FuzzedValue import org.utbot.fuzzing.FuzzedDescription import org.utbot.fuzzing.JavaValueProvider import org.utbot.fuzzing.Routine -import org.utbot.fuzzing.Scope import org.utbot.fuzzing.Seed +import org.utbot.fuzzing.spring.decorators.ValueProviderDecorator /** * @see preserveProperties @@ -25,14 +25,14 @@ interface PreservableFuzzedTypeProperty : FuzzedTypeProperty fun JavaValueProvider.preserveProperties() : JavaValueProvider = PropertyPreservingValueProvider(this) -class PropertyPreservingValueProvider(private val delegateProvider: JavaValueProvider) : JavaValueProvider { - override fun enrich(description: FuzzedDescription, type: FuzzedType, scope: Scope) = - delegateProvider.enrich(description, type, scope) - - override fun accept(type: FuzzedType): Boolean = delegateProvider.accept(type) +class PropertyPreservingValueProvider( + delegate: JavaValueProvider +) : ValueProviderDecorator(delegate) { + override fun wrap(provider: JavaValueProvider): JavaValueProvider = + provider.preserveProperties() override fun generate(description: FuzzedDescription, type: FuzzedType): Sequence> { - val delegateSeeds = delegateProvider.generate(description, type) + val delegateSeeds = delegate.generate(description, type) val preservedProperties = type.properties.entries .filter { it.property is PreservableFuzzedTypeProperty } @@ -67,10 +67,4 @@ class PropertyPreservingValueProvider(private val delegateProvider: JavaValuePro } } } - - override fun map(transform: (JavaValueProvider) -> JavaValueProvider): JavaValueProvider = - delegateProvider.map(transform).preserveProperties() - - override fun except(filter: (JavaValueProvider) -> Boolean): JavaValueProvider = - delegateProvider.except(filter).preserveProperties() } \ No newline at end of file diff --git a/utbot-java-fuzzing/src/main/kotlin/org/utbot/fuzzing/spring/decorators/FilteredValueProvider.kt b/utbot-java-fuzzing/src/main/kotlin/org/utbot/fuzzing/spring/decorators/FilteredValueProvider.kt new file mode 100644 index 0000000000..faf2178f03 --- /dev/null +++ b/utbot-java-fuzzing/src/main/kotlin/org/utbot/fuzzing/spring/decorators/FilteredValueProvider.kt @@ -0,0 +1,18 @@ +package org.utbot.fuzzing.spring.decorators + +import org.utbot.fuzzing.Description +import org.utbot.fuzzing.ValueProvider + +fun > ValueProvider.filterTypes(predicate: (T) -> Boolean) = + FilteredValueProvider(delegate = this, predicate) + +class FilteredValueProvider>( + delegate: ValueProvider, + private val predicate: (T) -> Boolean +) : ValueProviderDecorator(delegate) { + override fun wrap(provider: ValueProvider): ValueProvider = + provider.filterTypes(predicate) + + override fun accept(type: T): Boolean = + predicate(type) && super.accept(type) +} \ No newline at end of file diff --git a/utbot-java-fuzzing/src/main/kotlin/org/utbot/fuzzing/spring/decorators/TypeReplacingValueProvider.kt b/utbot-java-fuzzing/src/main/kotlin/org/utbot/fuzzing/spring/decorators/TypeReplacingValueProvider.kt new file mode 100644 index 0000000000..2df9984d00 --- /dev/null +++ b/utbot-java-fuzzing/src/main/kotlin/org/utbot/fuzzing/spring/decorators/TypeReplacingValueProvider.kt @@ -0,0 +1,28 @@ +package org.utbot.fuzzing.spring.decorators + +import org.utbot.fuzzing.Description +import org.utbot.fuzzing.Scope +import org.utbot.fuzzing.Seed +import org.utbot.fuzzing.ValueProvider + +fun > ValueProvider.replaceTypes(typeReplacer: (D, T) -> T) = + TypeReplacingValueProvider(delegate = this, typeReplacer) + +class TypeReplacingValueProvider>( + delegate: ValueProvider, + private val typeReplacer: (D, T) -> T +) : ValueProviderDecorator(delegate) { + override fun wrap(provider: ValueProvider): ValueProvider = + provider.replaceTypes(typeReplacer) + + override fun enrich(description: D, type: T, scope: Scope) = + super.enrich(description, typeReplacer(description, type), scope) + + override fun accept(type: T): Boolean = true + + override fun generate(description: D, type: T): Sequence> = + if (super.accept(typeReplacer(description, type))) + super.generate(description, typeReplacer(description, type)) + else + emptySequence() +} diff --git a/utbot-java-fuzzing/src/main/kotlin/org/utbot/fuzzing/spring/decorators/ValueProviderDecorator.kt b/utbot-java-fuzzing/src/main/kotlin/org/utbot/fuzzing/spring/decorators/ValueProviderDecorator.kt new file mode 100644 index 0000000000..030d3bcd81 --- /dev/null +++ b/utbot-java-fuzzing/src/main/kotlin/org/utbot/fuzzing/spring/decorators/ValueProviderDecorator.kt @@ -0,0 +1,29 @@ +package org.utbot.fuzzing.spring.decorators + +import org.utbot.fuzzing.Description +import org.utbot.fuzzing.Scope +import org.utbot.fuzzing.Seed +import org.utbot.fuzzing.ValueProvider + +abstract class ValueProviderDecorator>( + protected val delegate: ValueProvider +) : ValueProvider { + protected abstract fun wrap(provider: ValueProvider): ValueProvider + + override fun enrich(description: D, type: T, scope: Scope) = + delegate.enrich(description, type, scope) + + override fun accept(type: T): Boolean = + delegate.accept(type) + + override fun generate(description: D, type: T): Sequence> = + delegate.generate(description, type) + + override fun except(filter: (ValueProvider) -> Boolean): ValueProvider { + val res = wrap(delegate.except(filter)) + return if (filter(res)) ValueProvider.of(emptyList()) else res + } + + override fun map(transform: (ValueProvider) -> ValueProvider): ValueProvider = + transform(wrap(delegate.map(transform))) +} \ No newline at end of file