Skip to content
Prev Previous commit
Next Next commit
An attempt to unsat branched with unexpected mocking
  • Loading branch information
EgorkaKulikov committed Mar 9, 2023
commit dbc3312dd5733a6be13ed81633a8e9597da1dae9
33 changes: 29 additions & 4 deletions utbot-framework/src/main/kotlin/org/utbot/engine/Mocks.kt
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,23 @@ data class UtStaticMethodMockInfo(
val methodId: MethodId
) : UtMockInfo(methodId.classId, addr)

/**
* A wrapper for [ObjectValue] to store additional onfo.
*/
sealed class MockedObjectInfo(val value: ObjectValue?)

object NoMock: MockedObjectInfo(value = null)
class ExpectedMock(value: ObjectValue): MockedObjectInfo(value)

/**
* Represents a mock that occurs when it is not recommended
* E.g. mock strategy recommends do not mock
*/
class UnexpectedMock(value: ObjectValue): MockedObjectInfo(value)

fun ObjectValue?.construct(mockDesired: Boolean): MockedObjectInfo =
this?.let { if (mockDesired) ExpectedMock(it) else UnexpectedMock(it) } ?: NoMock

/**
* Service to mock things. Knows mock strategy, class under test and class hierarchy.
*/
Expand All @@ -138,22 +155,28 @@ class Mocker(
chosenClassesToMockAlways: Set<ClassId>,
internal val mockListenerController: MockListenerController? = null,
) {
private val mocksDesired: Boolean = strategy != MockStrategy.NO_MOCKS

/**
* Creates mocked instance of the [type] using mock info if it should be mocked by the mocker,
* otherwise returns null.
*
* @see shouldMock
*/
fun mock(type: RefType, mockInfo: UtMockInfo): ObjectValue? =
if (shouldMock(type, mockInfo)) createMockObject(type, mockInfo) else null
fun mock(type: RefType, mockInfo: UtMockInfo): MockedObjectInfo {
val objectValue = if (shouldMock(type, mockInfo)) createMockObject(type, mockInfo) else null
return objectValue.construct(mocksDesired)
}

/**
* Creates mocked instance of the [type] using mock info. Unlike to [mock], it does not
* check anything and always returns the constructed mock.
*/
fun forceMock(type: RefType, mockInfo: UtMockInfo): ObjectValue {
fun forceMock(type: RefType, mockInfo: UtMockInfo): MockedObjectInfo {
mockListenerController?.onShouldMock(strategy, mockInfo)
return createMockObject(type, mockInfo)

val objectValue = createMockObject(type, mockInfo)
return objectValue.construct(mocksDesired)
}

/**
Expand All @@ -180,6 +203,8 @@ class Mocker(
}
}

val areMocksAllowed: Boolean = strategy == MockStrategy.NO_MOCKS

private fun checkIfShouldMock(
type: RefType,
mockInfo: UtMockInfo
Expand Down
19 changes: 16 additions & 3 deletions utbot-framework/src/main/kotlin/org/utbot/engine/Traverser.kt
Original file line number Diff line number Diff line change
Expand Up @@ -1383,7 +1383,13 @@ class Traverser(

queuedSymbolicStateUpdates += MemoryUpdate(addrToMockInfo = persistentHashMapOf(addr to mockInfo))

val mockedObject = mocker.mock(type, mockInfo)
val mockedObjectInfo = mocker.mock(type, mockInfo)

val mockedObject = mockedObjectInfo.value
if (mockedObjectInfo is UnexpectedMock) {
queuedSymbolicStateUpdates += UtFalse.asHardConstraint()
}


if (mockedObject != null) {
queuedSymbolicStateUpdates += MemoryUpdate(mockInfos = persistentListOf(MockInfoEnriched(mockInfo)))
Expand Down Expand Up @@ -1470,7 +1476,12 @@ class Traverser(
}

val mockInfo = mockInfoGenerator.generate(addr)
val mockedObject = mocker.forceMock(type, mockInfoGenerator.generate(addr))
val mockedObjectInfo = mocker.forceMock(type, mockInfoGenerator.generate(addr))

val mockedObject = mockedObjectInfo.value ?: error("Mocked value cannot be null after force mock")
if (mockedObjectInfo is UnexpectedMock) {
queuedSymbolicStateUpdates += UtFalse.asHardConstraint()
}

queuedSymbolicStateUpdates += MemoryUpdate(mockInfos = persistentListOf(MockInfoEnriched(mockInfo)))

Expand Down Expand Up @@ -2684,7 +2695,9 @@ class Traverser(
} else {
it.copyWithClassId(classId = implementationClass.id)
}
mocker.mock(implementationClass, updatedMockInfo)

val mockedObjectInfo = mocker.mock(implementationClass, updatedMockInfo)
mockedObjectInfo.value
}

if (mockedObject == null) {
Expand Down