Skip to content

Commit

Permalink
fix mocking tests errors
Browse files Browse the repository at this point in the history
  • Loading branch information
AbdullinAM committed Jun 4, 2024
1 parent 266891a commit e5959b3
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,86 @@ package org.vorpal.research.kex.mocking

import org.vorpal.research.kex.ExecutionContext
import org.vorpal.research.kex.config.kexConfig
import org.vorpal.research.kex.descriptor.ArrayDescriptor
import org.vorpal.research.kex.descriptor.Descriptor
import org.vorpal.research.kex.descriptor.DescriptorContext
import org.vorpal.research.kex.descriptor.FieldContainingDescriptor
import org.vorpal.research.kex.descriptor.MockDescriptor
import org.vorpal.research.kex.descriptor.any
import org.vorpal.research.kex.descriptor.descriptor
import org.vorpal.research.kex.descriptor.transform
import org.vorpal.research.kex.ktype.KexArray
import org.vorpal.research.kex.ktype.KexClass
import org.vorpal.research.kex.ktype.KexReference
import org.vorpal.research.kex.ktype.KexType
import org.vorpal.research.kex.parameters.FinalParameters
import org.vorpal.research.kex.parameters.map
import org.vorpal.research.kex.state.predicate.CallPredicate
import org.vorpal.research.kex.state.term.CallTerm
import org.vorpal.research.kex.state.term.Term
import org.vorpal.research.kex.trace.symbolic.SymbolicState
import org.vorpal.research.kex.util.containsMockitoMock
import org.vorpal.research.kex.util.isMockingEnabled
import org.vorpal.research.kex.util.removeMockitoMockSuffix
import org.vorpal.research.kfg.ir.Class
import org.vorpal.research.kfg.ir.Method
import org.vorpal.research.kfg.type.ClassType
import org.vorpal.research.kfg.type.TypeFactory
import org.vorpal.research.kthelper.logging.log
import org.vorpal.research.kthelper.logging.warn

fun FinalParameters<Descriptor>.filterMocks(
): FinalParameters<Descriptor> {
val visited = mutableSetOf<Descriptor>()
return this.map { it.filterMockedTypes(visited) ?: descriptor { default(it.type) } }
}

internal val KexType.isMockitoMock: Boolean
get() = this.toString().containsMockitoMock

internal fun KexType.removeMockitoMocks(): KexType = when (this) {
is KexClass -> KexClass(this.name.removeMockitoMockSuffix())
is KexArray -> KexArray(this.element.removeMockitoMocks())
is KexReference -> KexReference(this.reference.removeMockitoMocks())
else -> this
}

internal fun Descriptor.filterMockedTypes(
visited: MutableSet<Descriptor>,
): Descriptor? = when (this) {
in visited -> this
is FieldContainingDescriptor<*> -> when {
this.type.isMockitoMock -> null
else -> {
visited += this
for ((field, value) in this.fields) {
val mapped = value.filterMockedTypes(visited)
if (mapped == null) {
fields.remove(field)
} else {
fields[field] = mapped
}
}
this
}
}

is ArrayDescriptor -> {
visited += this
for ((index, element) in this.elements) {
val mapped = element.filterMockedTypes(visited)
if (mapped == null) {
elements.remove(index)
} else {
this[index] = mapped
}
}
this
}

else -> this
}

fun Descriptor.isRequireMocks(
mockMaker: MockMaker,
expectedClass: Map<Descriptor, Class>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,12 @@ class FinalParameters<T> private constructor(
}
}

inline fun <T : F, U, reified F> FinalParameters<T>.map(transform: (F) -> U): FinalParameters<U> = FinalParameters(
if (instance is F) transform(instance) else null,
args.map(transform),
if (returnValueUnsafe is F) transform(returnValueUnsafe) else null,
)

val <T> FinalParameters<T>?.isSuccessOrFalse: Boolean get() = this?.isSuccess ?: false
val <T> FinalParameters<T>?.isExceptionOrFalse: Boolean get() = this?.isException ?: false
val <T> FinalParameters<T>?.hasReturnValueOrFalse: Boolean get() = this?.hasReturnValue ?: false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import org.vorpal.research.kex.compile.CompilationException
import org.vorpal.research.kex.compile.CompilerHelper
import org.vorpal.research.kex.config.kexConfig
import org.vorpal.research.kex.descriptor.Descriptor
import org.vorpal.research.kex.mocking.filterMocks
import org.vorpal.research.kex.parameters.Parameters
import org.vorpal.research.kex.parameters.asDescriptors
import org.vorpal.research.kex.parameters.extractFinalParameters
Expand Down Expand Up @@ -163,6 +164,7 @@ class InstructionConcolicChecker(
)

val finalInfoDescriptors = extractFinalParameters(result, method)
?.filterMocks()
testWithAssertionsGenerator.generate(parameters, finalInfoDescriptors)
testWithAssertions = testWithAssertionsGenerator.emit()
compilerHelper.compileFile(testWithAssertions)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ public static void testCharHashSetIterator(HashSet<Character> chars) {
if (chars.contains(null)) {
throw new IllegalStateException();
}
if (chars.size() > 1) {
throw new IllegalStateException();
}
// if (chars.size() > 1) {
// throw new IllegalStateException();
// }
Iterator<Character> it = chars.iterator();
if (it.hasNext()) {
if (it.next().charValue() == 'v') {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
public class MockSetTests {
public void testMockSetEasy(HashSet<ToMock> set) {
Iterator<ToMock> iterator = set.iterator();
if (!iterator.hasNext()) return;
if (iterator.next().foo() == 42) {
AssertIntrinsics.kexAssert(true);
} else {
Expand All @@ -20,6 +21,7 @@ public void testMockSetEasy(HashSet<ToMock> set) {

public void testMockSetMedium(HashSet<ToMock> set) {
Iterator<ToMock> iterator = set.iterator();
if (!iterator.hasNext()) return;
HashSet<Integer> values = new HashSet<>();
values.add(iterator.next().foo());
if (values.contains(35)) {
Expand All @@ -29,22 +31,24 @@ public void testMockSetMedium(HashSet<ToMock> set) {
}
}

/* // Uncomment if tests above pass
public void testNoMockSetHard(HashSet<ToMock> set) {
Iterator<ToMock> iterator = set.iterator();
HashSet<Integer> values = new HashSet<>();
values.add(iterator.next().foo());
values.add(iterator.next().foo());
if (values.contains(35)) {
if (values.contains(22)) {
AssertIntrinsics.kexAssert(true);
} else {
AssertIntrinsics.kexAssert(true);
}
} else {
AssertIntrinsics.kexAssert(true);
}
}
*/
// public void testNoMockSetHard(HashSet<ToMock> set) {
// Iterator<ToMock> iterator = set.iterator();
// if (!iterator.hasNext()) return;
//
// HashSet<Integer> values = new HashSet<>();
// values.add(iterator.next().foo());
//
// if (!iterator.hasNext()) return;
// values.add(iterator.next().foo());
// if (values.contains(35)) {
// if (values.contains(22)) {
// AssertIntrinsics.kexAssert(true);
// } else {
// AssertIntrinsics.kexAssert(true);
//
// }
// } else {
// AssertIntrinsics.kexAssert(true);
// }
// }
}

0 comments on commit e5959b3

Please sign in to comment.