|
1 | 1 | package org.utbot.framework.context.custom
|
2 | 2 |
|
3 | 3 | import org.utbot.framework.context.JavaFuzzingContext
|
| 4 | +import org.utbot.framework.plugin.api.UtNullModel |
4 | 5 | import org.utbot.framework.plugin.api.ExecutableId
|
| 6 | +import org.utbot.fuzzer.FuzzedType |
5 | 7 | import org.utbot.fuzzing.JavaValueProvider
|
6 |
| -import org.utbot.fuzzing.providers.MapValueProvider |
| 8 | +import org.utbot.fuzzing.Seed |
| 9 | +import org.utbot.fuzzing.providers.AnyDepthNullValueProvider |
| 10 | +import org.utbot.fuzzing.providers.AnyObjectValueProvider |
7 | 11 | import org.utbot.fuzzing.spring.unit.MockValueProvider
|
8 |
| -import org.utbot.fuzzing.providers.NullValueProvider |
9 |
| -import org.utbot.fuzzing.providers.ObjectValueProvider |
10 |
| -import org.utbot.fuzzing.providers.StringValueProvider |
| 12 | +import org.utbot.fuzzing.spring.decorators.filterSeeds |
| 13 | +import org.utbot.fuzzing.spring.decorators.filterTypes |
11 | 14 | import org.utbot.instrumentation.instrumentation.execution.UtConcreteExecutionResult
|
12 | 15 |
|
13 | 16 | /**
|
14 |
| - * Makes fuzzer mock all types that don't have *specific* [JavaValueProvider], |
15 |
| - * like [MapValueProvider] or [StringValueProvider]. |
| 17 | + * Makes fuzzer to use mocks in accordance with [mockPredicate]. |
16 | 18 | *
|
17 |
| - * NOTE: the caller is responsible for providing some *specific* [JavaValueProvider] |
18 |
| - * that can create values for class under test (otherwise it will be mocked), |
19 |
| - * [ObjectValueProvider] and [NullValueProvider] do not count as *specific*. |
| 19 | + * NOTE: |
| 20 | + * - fuzzer won't mock types, that have *specific* value providers |
| 21 | + * (i.e. ones that do not implement [AnyObjectValueProvider]) |
| 22 | + * - fuzzer may still resort to mocks despite [mockPredicate] and *specific* |
| 23 | + * value providers if it can't create other non-null values or at runtime |
20 | 24 | */
|
21 |
| -fun JavaFuzzingContext.mockAllTypesWithoutSpecificValueProvider() = |
22 |
| - MockingJavaFuzzingContext(delegateContext = this) |
| 25 | +fun JavaFuzzingContext.useMocks(mockPredicate: (FuzzedType) -> Boolean) = |
| 26 | + MockingJavaFuzzingContext(delegateContext = this, mockPredicate) |
23 | 27 |
|
24 | 28 | class MockingJavaFuzzingContext(
|
25 |
| - val delegateContext: JavaFuzzingContext |
| 29 | + val delegateContext: JavaFuzzingContext, |
| 30 | + val mockPredicate: (FuzzedType) -> Boolean, |
26 | 31 | ) : JavaFuzzingContext by delegateContext {
|
27 | 32 | private val mockValueProvider = MockValueProvider(delegateContext.idGenerator)
|
28 | 33 |
|
29 | 34 | override val valueProvider: JavaValueProvider =
|
30 |
| - // NOTE: we first remove `NullValueProvider` from `delegateContext.valueProvider` and then |
31 |
| - // add it back as a part of our `withFallback` so it has the same priority as |
32 |
| - // `mockValueProvider`, otherwise mocks will never be used where `null` can be used. |
| 35 | + |
33 | 36 | delegateContext.valueProvider
|
34 |
| - .except { it is NullValueProvider } |
35 |
| - .except { it is ObjectValueProvider } |
| 37 | + // NOTE: we first remove `AnyObjectValueProvider` and `NullValueProvider` from `delegateContext.valueProvider` |
| 38 | + // and then add them back as a part of our `withFallback` so they have the same priority as |
| 39 | + // `mockValueProvider`, otherwise mocks will never be used where `null` or new object can be used. |
| 40 | + .except { it is AnyObjectValueProvider } |
36 | 41 | .withFallback(
|
37 |
| - mockValueProvider |
38 |
| - .with(NullValueProvider) |
| 42 | + mockValueProvider.filterTypes(mockPredicate) |
| 43 | + .with( |
| 44 | + delegateContext.valueProvider |
| 45 | + .filterTypes { !mockPredicate(it) } |
| 46 | + .filterSeeds { (it as? Seed.Simple)?.value?.model !is UtNullModel } |
| 47 | + ) |
| 48 | + .withFallback(mockValueProvider.with(AnyDepthNullValueProvider)) |
39 | 49 | )
|
40 | 50 |
|
41 | 51 | override fun handleFuzzedConcreteExecutionResult(
|
|
0 commit comments