Skip to content

ToString support for abstract collection using makeSymbolic #391 #402

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
Jul 4, 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
@@ -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();
}
}