Skip to content

Commit

Permalink
ToString support for abstract collection using makeSymbolic #391
Browse files Browse the repository at this point in the history
  • Loading branch information
CaelmBleidd committed Jul 4, 2022
1 parent c2673df commit 3b21112
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package org.utbot.engine.overrides.collections;

import org.utbot.api.annotation.UtClassMock;

import static org.utbot.api.mock.UtMock.makeSymbolic;

@UtClassMock(target = java.util.AbstractCollection.class, internalUsage = true)
public abstract class AbstractCollection<E> implements java.util.Collection<E> {
@Override
public String toString() {
return makeSymbolic();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
import org.utbot.engine.overrides.stream.UtStream;

import static org.utbot.api.mock.UtMock.assume;
import static org.utbot.api.mock.UtMock.assumeOrExecuteConcretely;
import static org.utbot.engine.ResolverKt.MAX_LIST_SIZE;
import static org.utbot.engine.overrides.UtOverrideMock.alreadyVisited;
import static org.utbot.api.mock.UtMock.assumeOrExecuteConcretely;
import static org.utbot.engine.overrides.UtOverrideMock.executeConcretely;
import static org.utbot.engine.overrides.UtOverrideMock.parameter;
import static org.utbot.engine.overrides.UtOverrideMock.visit;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
import org.jetbrains.annotations.Nullable;

import static org.utbot.api.mock.UtMock.assume;
import static org.utbot.api.mock.UtMock.makeSymbolic;
import static org.utbot.engine.overrides.UtOverrideMock.alreadyVisited;
import static org.utbot.engine.overrides.UtOverrideMock.doesntThrow;
import static org.utbot.engine.overrides.UtOverrideMock.executeConcretely;
import static org.utbot.engine.overrides.UtOverrideMock.parameter;
import static org.utbot.engine.overrides.UtOverrideMock.visit;

Expand Down Expand Up @@ -556,8 +556,7 @@ public final boolean remove(Object o) {
// TODO rewrite it JIRA:1604
@Override
public String toString() {
executeConcretely();
return super.toString();
return makeSymbolic();
}

public final class Entry implements Map.Entry<K, V> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package org.utbot.engine.overrides.stream;

import org.utbot.api.annotation.UtClassMock;
import org.utbot.engine.overrides.collections.UtArrayList;

import java.util.List;
import java.util.stream.Stream;

@UtClassMock(target = java.util.Arrays.class, internalUsage = true)
Expand All @@ -16,5 +18,12 @@ public static <T> Stream<T> stream(T[] array, int startInclusive, int endExclusi
return new UtStream<>(array, startInclusive, endExclusive);
}

@SuppressWarnings({"unused", "varargs"})
@SafeVarargs
public static <T> List<T> asList(T... a) {
// TODO immutable collection https://github.com/UnitTestBot/UTBotJava/issues/398
return new UtArrayList<>(a);
}

// TODO primitive arrays https://github.com/UnitTestBot/UTBotJava/issues/146
}
Original file line number Diff line number Diff line change
Expand Up @@ -83,16 +83,23 @@ abstract class BaseOverriddenWrapper(protected val overriddenClassName: String)
return listOf(GraphResult(method.jimpleBody().graph()))
}

val overriddenMethod = overriddenClass.findMethodOrNull(method.subSignature)

if (overriddenMethod == null) {
// We need to find either an override from the class (for example, implementation
// of the method from the wrapper) or a method from its ancestors.
// Note that the method from the ancestor might have substitutions as well.
// I.e., it is `toString` method for `UtArrayList` that is defined in
// `AbstractCollection` and has its own overridden implementation.
val overriddenMethod = overriddenClass
.findMethodOrNull(method.subSignature)
?.let { typeRegistry.findSubstitutionOrNull(it) ?: it }

return if (overriddenMethod == null) {
// No overridden method has been found, switch to concrete execution
pathLogger.warn("Method ${overriddenClass.name}::${method.subSignature} not found, executing concretely")
return emptyList()
emptyList()
} else {
val jimpleBody = overriddenMethod.jimpleBody()
val graphResult = GraphResult(jimpleBody.graph())
return listOf(graphResult)
listOf(graphResult)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2536,7 +2536,8 @@ class UtBotSymbolicEngine(
if (methodSignature != makeSymbolicMethod.signature && methodSignature != nonNullableMakeSymbolic.signature) return null

val method = environment.method
val isInternalMock = method.hasInternalMockAnnotation || method.declaringClass.allMethodsAreInternalMocks
val declaringClass = method.declaringClass
val isInternalMock = method.hasInternalMockAnnotation || declaringClass.allMethodsAreInternalMocks || declaringClass.isOverridden
val parameters = resolveParameters(invokeExpr.args, invokeExpr.method.parameterTypes)
val mockMethodResult = mockStaticMethod(invokeExpr.method, parameters)?.single()
?: error("Unsuccessful mock attempt of the `makeSymbolic` method call: $invokeExpr")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ import org.utbot.engine.overrides.collections.UtOptional
import org.utbot.engine.overrides.collections.UtOptionalDouble
import org.utbot.engine.overrides.collections.UtOptionalInt
import org.utbot.engine.overrides.collections.UtOptionalLong
import org.utbot.engine.overrides.collections.AbstractCollection
import org.utbot.engine.overrides.stream.Arrays
import org.utbot.engine.overrides.stream.Stream
import org.utbot.engine.overrides.stream.UtStream
import java.io.File
Expand Down Expand Up @@ -91,6 +93,7 @@ private fun addBasicClasses(vararg classes: KClass<*>) {
}

private val classesToLoad = arrayOf(
AbstractCollection::class,
UtMock::class,
UtOverrideMock::class,
UtLogicMock::class,
Expand Down Expand Up @@ -133,6 +136,7 @@ private val classesToLoad = arrayOf(
UtStringBuilder::class,
UtStringBuffer::class,
Stream::class,
Arrays::class,
Collection::class,
List::class,
UtStream::class,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -589,4 +589,14 @@ internal class StringExamplesTest : AbstractTestCaseGeneratorTest(
)
}
}

@Test
fun testListToString() {
check(
StringExamples::listToString,
eq(1),
{ r -> r == "[a, b, c]"},
coverage = DoNotCalculate
)
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.utbot.examples.strings;

import java.util.Arrays;

import static java.lang.Boolean.valueOf;

class IntPair {
Expand Down Expand Up @@ -411,4 +413,8 @@ public String equalsIgnoreCase(String s) {
return "failure";
}
}

public String listToString() {
return Arrays.asList("a", "b", "c").toString();
}
}

0 comments on commit 3b21112

Please sign in to comment.