Skip to content

Add disjunction with null condition to isMock constraint #1521

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,14 @@ internal class CommonMocksExampleTest: UtValueTestCaseChecker(testClass = Common
coverage = DoNotCalculate
)
}

@Test
fun testMocksForNullOfDifferentTypes() {
check(
CommonMocksExample::mocksForNullOfDifferentTypes,
eq(1),
mockStrategy = MockStrategyApi.OTHER_PACKAGES,
coverage = DoNotCalculate
)
}
}
25 changes: 18 additions & 7 deletions utbot-framework/src/main/kotlin/org/utbot/engine/Traverser.kt
Original file line number Diff line number Diff line change
Expand Up @@ -1249,6 +1249,8 @@ class Traverser(
): ObjectValue {
touchAddress(addr)

val nullEqualityConstraint = mkEq(addr, nullObjectAddr)

if (mockInfoGenerator != null) {
val mockInfo = mockInfoGenerator.generate(addr)

Expand All @@ -1260,8 +1262,11 @@ class Traverser(
queuedSymbolicStateUpdates += MemoryUpdate(mockInfos = persistentListOf(MockInfoEnriched(mockInfo)))

// add typeConstraint for mocked object. It's a declared type of the object.
queuedSymbolicStateUpdates += typeRegistry.typeConstraint(addr, mockedObject.typeStorage).all().asHardConstraint()
queuedSymbolicStateUpdates += mkEq(typeRegistry.isMock(mockedObject.addr), UtTrue).asHardConstraint()
val typeConstraint = typeRegistry.typeConstraint(addr, mockedObject.typeStorage).all()
val isMockConstraint = mkEq(typeRegistry.isMock(mockedObject.addr), UtTrue)

queuedSymbolicStateUpdates += typeConstraint.asHardConstraint()
queuedSymbolicStateUpdates += mkOr(isMockConstraint, nullEqualityConstraint).asHardConstraint()

return mockedObject
}
Expand All @@ -1286,8 +1291,10 @@ class Traverser(
typeStoragePossiblyWithOverriddenTypes
}

val typeHardConstraint = typeRegistry.typeConstraint(addr, typeStorage).all().asHardConstraint()

wrapper(type, addr)?.let {
queuedSymbolicStateUpdates += typeRegistry.typeConstraint(addr, typeStorage).all().asHardConstraint()
queuedSymbolicStateUpdates += typeHardConstraint
return it
}

Expand All @@ -1303,8 +1310,11 @@ class Traverser(
queuedSymbolicStateUpdates += MemoryUpdate(mockInfos = persistentListOf(MockInfoEnriched(mockInfo)))

// add typeConstraint for mocked object. It's a declared type of the object.
queuedSymbolicStateUpdates += typeRegistry.typeConstraint(addr, mockedObject.typeStorage).all().asHardConstraint()
queuedSymbolicStateUpdates += mkEq(typeRegistry.isMock(mockedObject.addr), UtTrue).asHardConstraint()
val typeConstraint = typeRegistry.typeConstraint(addr, mockedObject.typeStorage).all()
val isMockConstraint = mkEq(typeRegistry.isMock(mockedObject.addr), UtTrue)

queuedSymbolicStateUpdates += typeConstraint.asHardConstraint()
queuedSymbolicStateUpdates += mkOr(isMockConstraint, nullEqualityConstraint).asHardConstraint()

return mockedObject
}
Expand All @@ -1313,9 +1323,10 @@ class Traverser(
// We should create an object with typeStorage of all possible real types and concrete implementation
// Otherwise we'd have either a wrong type in the resolver, or missing method like 'preconditionCheck'.
val concreteImplementation = wrapperToClass[type]?.first()?.let { wrapper(it, addr) }?.concrete
val isMockConstraint = mkEq(typeRegistry.isMock(addr), UtFalse)

queuedSymbolicStateUpdates += typeRegistry.typeConstraint(addr, typeStorage).all().asHardConstraint()
queuedSymbolicStateUpdates += mkEq(typeRegistry.isMock(addr), UtFalse).asHardConstraint()
queuedSymbolicStateUpdates += typeHardConstraint
queuedSymbolicStateUpdates += mkOr(isMockConstraint, nullEqualityConstraint).asHardConstraint()

return ObjectValue(typeStorage, addr, concreteImplementation)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.utbot.examples.mock;

import org.utbot.api.mock.UtMock;
import org.utbot.examples.mock.others.Random;
import org.utbot.examples.mock.service.impl.ExampleClass;
import org.utbot.examples.objects.ObjectWithFinalStatic;
import org.utbot.examples.objects.RecursiveTypeClass;
Expand Down Expand Up @@ -35,4 +37,11 @@ public int clinitMockExample() {
return -ObjectWithFinalStatic.keyValue;
}
}

public int mocksForNullOfDifferentTypes(Integer intValue, Random random) {
UtMock.assume(intValue == null);
UtMock.assume(random == null);

return 0;
}
}