Skip to content

Commit

Permalink
Avoid using anyObjectValueProvider that is removed in #2583
Browse files Browse the repository at this point in the history
  • Loading branch information
IlyaMuravjov committed Sep 19, 2023
1 parent 9ea8815 commit f64a5b7
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 21 deletions.
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
package org.utbot.framework.context.custom

import org.utbot.framework.context.JavaFuzzingContext
import org.utbot.framework.plugin.api.UtNullModel
import org.utbot.framework.plugin.api.ExecutableId
import org.utbot.fuzzer.FuzzedType
import org.utbot.fuzzing.JavaValueProvider
import org.utbot.fuzzing.Seed
import org.utbot.fuzzing.providers.AnyDepthNullValueProvider
import org.utbot.fuzzing.providers.MapValueProvider
import org.utbot.fuzzing.providers.AnyObjectValueProvider
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.filterSeeds
import org.utbot.fuzzing.spring.decorators.filterTypes
import org.utbot.instrumentation.instrumentation.execution.UtConcreteExecutionResult

/**
* Makes fuzzer to use mocks in accordance with [mockPredicate].
*
* 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
* - fuzzer may still resort to mocks despite [mockPredicate] if it can't create other non-null values or at runtime
* - fuzzer won't mock types, that have *specific* value providers
* (i.e. ones that do not implement [AnyObjectValueProvider])
* - fuzzer may still resort to mocks despite [mockPredicate] and *specific*
* value providers if it can't create other non-null values or at runtime
*/
fun JavaFuzzingContext.useMocks(mockPredicate: (FuzzedType) -> Boolean) =
MockingJavaFuzzingContext(delegateContext = this, mockPredicate)
Expand All @@ -32,17 +32,20 @@ class MockingJavaFuzzingContext(
private val mockValueProvider = MockValueProvider(delegateContext.idGenerator)

override val valueProvider: JavaValueProvider =
// 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 }
// NOTE: we first remove `AnyObjectValueProvider` and `NullValueProvider` 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.
.except { it is AnyObjectValueProvider }
.withFallback(
mockValueProvider.filterTypes(mockPredicate)
.with(anyObjectValueProvider(idGenerator).filterTypes { !mockPredicate(it) })
.with(
delegateContext.valueProvider
.filterTypes { !mockPredicate(it) }
.filterSeeds { (it as? Seed.Simple)?.value?.model !is UtNullModel }
)
.withFallback(mockValueProvider.with(AnyDepthNullValueProvider))
.with(NullValueProvider)
)

override fun handleFuzzedConcreteExecutionResult(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,16 @@ fun anyObjectValueProvider(idGenerator: IdentityPreservingIdGenerator<Int>) =
ovp.withFallback(AbstractsObjectValueProvider(idGenerator))
}

/**
* Marker interface that shows that this [JavaValueProvider] can potentially provide values of
* arbitrary types, unlike type-specific value providers that were designed to provide values of
* few specific popular types (e.g. `List`, `String`, etc.).
*/
interface AnyObjectValueProvider : JavaValueProvider

class ObjectValueProvider(
val idGenerator: IdGenerator<Int>,
) : JavaValueProvider {
) : AnyObjectValueProvider {

override fun accept(type: FuzzedType) = !isIgnored(type.classId)

Expand Down Expand Up @@ -140,7 +147,7 @@ class ObjectValueProvider(
}

@Suppress("unused")
object NullValueProvider : JavaValueProvider {
object NullValueProvider : AnyObjectValueProvider {

override fun enrich(description: FuzzedDescription, type: FuzzedType, scope: Scope) {
// any value in static function is ok to fuzz
Expand Down Expand Up @@ -170,7 +177,7 @@ object NullValueProvider : JavaValueProvider {
*
* Intended to be used as a last fallback.
*/
object AnyDepthNullValueProvider : JavaValueProvider {
object AnyDepthNullValueProvider : AnyObjectValueProvider {

override fun accept(type: FuzzedType) = type.classId.isRefType

Expand All @@ -185,7 +192,7 @@ object AnyDepthNullValueProvider : JavaValueProvider {
*/
class AbstractsObjectValueProvider(
val idGenerator: IdGenerator<Int>,
) : JavaValueProvider {
) : AnyObjectValueProvider {

override fun accept(type: FuzzedType) = type.classId.isRefType && !isKnownTypes(type.classId)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package org.utbot.fuzzing.spring.decorators

import org.utbot.fuzzing.Description
import org.utbot.fuzzing.Seed
import org.utbot.fuzzing.ValueProvider

fun <T, R, D : Description<T>> ValueProvider<T, R, D>.filterSeeds(predicate: (Seed<T, R>) -> Boolean) =
SeedFilteringValueProvider(delegate = this, predicate)

class SeedFilteringValueProvider<T, R, D : Description<T>>(
delegate: ValueProvider<T, R, D>,
private val predicate: (Seed<T, R>) -> Boolean
) : ValueProviderDecorator<T, R, D>(delegate) {
override fun wrap(provider: ValueProvider<T, R, D>): ValueProvider<T, R, D> =
provider.filterSeeds(predicate)

override fun generate(description: D, type: T): Sequence<Seed<T, R>> =
delegate.generate(description, type).filter(predicate)
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import org.utbot.fuzzing.Description
import org.utbot.fuzzing.ValueProvider

fun <T, R, D : Description<T>> ValueProvider<T, R, D>.filterTypes(predicate: (T) -> Boolean) =
FilteredValueProvider(delegate = this, predicate)
TypeFilteringValueProvider(delegate = this, predicate)

class FilteredValueProvider<T, R, D : Description<T>>(
class TypeFilteringValueProvider<T, R, D : Description<T>>(
delegate: ValueProvider<T, R, D>,
private val predicate: (T) -> Boolean
) : ValueProviderDecorator<T, R, D>(delegate) {
Expand Down

0 comments on commit f64a5b7

Please sign in to comment.