Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Minimize exceptions in signature
  • Loading branch information
sofurihafe committed Jul 6, 2022
commit 389b5c203cfeea46e3daf10ed322fb8afefcf937
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ data class UtExecutionSuccess(val model: UtModel) : UtExecutionResult() {

sealed class UtExecutionFailure : UtExecutionResult() {
abstract val exception: Throwable
val isCheckedException get() = !(exception is RuntimeException || exception is Error)
}

data class UtOverflowFailure(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package org.utbot.framework.plugin.api.util

import org.utbot.framework.plugin.api.ClassId

val Throwable.description
get() = message?.replace('\n', '\t') ?: "<Throwable with empty message>"

val Throwable.isCheckedException
get() = !(this is RuntimeException|| this is Error)
Comment thread
EgorkaKulikov marked this conversation as resolved.
Outdated

val ClassId.isCheckedException
get() = !(RuntimeException::class.java.isAssignableFrom(this.jClass) || Error::class.java.isAssignableFrom(this.jClass))
Comment thread
EgorkaKulikov marked this conversation as resolved.
Outdated
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ import org.utbot.framework.plugin.api.util.id
import org.utbot.framework.plugin.api.util.jClass
import org.utbot.framework.plugin.api.util.signature
import org.utbot.framework.plugin.api.util.utContext
import org.utbot.framework.util.description
import org.utbot.framework.plugin.api.util.description
import org.utbot.framework.util.executableId
import org.utbot.fuzzer.FuzzedMethodDescription
import org.utbot.fuzzer.ModelProvider
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ import org.utbot.framework.plugin.api.UtMethod
import org.utbot.framework.plugin.api.UtModel
import org.utbot.framework.plugin.api.UtReferenceModel
import org.utbot.framework.plugin.api.UtTestCase
import org.utbot.framework.plugin.api.util.executableId
import java.util.IdentityHashMap
import kotlinx.collections.immutable.PersistentList
import kotlinx.collections.immutable.PersistentMap
Expand All @@ -55,6 +54,11 @@ import kotlinx.collections.immutable.persistentListOf
import kotlinx.collections.immutable.persistentMapOf
import kotlinx.collections.immutable.persistentSetOf
import org.utbot.framework.codegen.model.constructor.builtin.streamsDeepEqualsMethodId
import org.utbot.framework.plugin.api.util.isSubtypeOf
import org.utbot.framework.plugin.api.util.isNotSubtypeOf
import org.utbot.framework.plugin.api.util.isCheckedException
import org.utbot.framework.plugin.api.util.id
import org.utbot.framework.plugin.api.util.executableId

/**
* Interface for all code generation context aware entities
Expand Down Expand Up @@ -232,7 +236,25 @@ internal interface CgContextOwner {
currentExecutable = method.callable.executableId
}

fun addException(exception: ClassId) {
fun addExceptionIfNeeded(exception: ClassId) {
Comment thread
EgorkaKulikov marked this conversation as resolved.
when (exception) {
is BuiltinClassId -> {}
Comment thread
EgorkaKulikov marked this conversation as resolved.
Outdated
else -> {
if (exception isNotSubtypeOf Throwable::class.id) {
error("Class $exception which is not a Throwable was passed")
}
Comment thread
sofurihafe marked this conversation as resolved.
Outdated

val isUnchecked = !exception.isCheckedException
val alreadyAdded =
collectedExceptions.any { existingException -> exception isSubtypeOf existingException }

if (isUnchecked || alreadyAdded) return

collectedExceptions
.removeIf { existingException -> existingException isSubtypeOf exception }
}
}

if (collectedExceptions.add(exception)) {
importIfNeeded(exception)
}
Expand Down Expand Up @@ -416,9 +438,9 @@ internal data class CgContext(
val simpleName = testClassCustomName ?: "${createTestClassName(classUnderTest.name)}Test"
val name = "$packagePrefix$simpleName"
BuiltinClassId(
name = name,
canonicalName = name,
simpleName = simpleName
name = name,
canonicalName = name,
simpleName = simpleName
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,15 +127,12 @@ internal class CgCallableAccessManagerImpl(val context: CgContext) : CgCallableA
//Builtin methods does not have jClass, so [methodId.method] will crash on it,
//so we need to collect required exceptions manually from source codes
if (methodId is BuiltinMethodId) {
methodId.findExceptionTypes().forEach { addException(it) }
methodId.findExceptionTypes().forEach { addExceptionIfNeeded(it) }
return
}
//If [InvocationTargetException] is thrown manually in test, we need
// to add "throws Throwable" and other exceptions are not required so on.

if (methodId == getTargetException) {
collectedExceptions.clear()
addException(Throwable::class.id)
return
addExceptionIfNeeded(Throwable::class.id)
Comment thread
Damtev marked this conversation as resolved.
}

val methodIsUnderTestAndThrowsExplicitly = methodId == currentExecutable
Expand All @@ -148,13 +145,13 @@ internal class CgCallableAccessManagerImpl(val context: CgContext) : CgCallableA
return
}

methodId.method.exceptionTypes.forEach { addException(it.id) }
methodId.method.exceptionTypes.forEach { addExceptionIfNeeded(it.id) }
}

private fun newConstructorCall(constructorId: ConstructorId) {
importIfNeeded(constructorId.classId)
for (exception in constructorId.exceptions) {
addException(exception)
addExceptionIfNeeded(exception)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import org.utbot.framework.codegen.model.visitor.importUtilMethodDependencies
import org.utbot.framework.plugin.api.MethodId
import org.utbot.framework.plugin.api.UtMethod
import org.utbot.framework.plugin.api.UtTestCase
import org.utbot.framework.util.description
import org.utbot.framework.plugin.api.util.description
import kotlin.reflect.KClass

internal class CgTestClassConstructor(val context: CgContext) :
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import org.utbot.framework.plugin.api.UtMethod
import org.utbot.framework.plugin.api.UtTestCase
import org.utbot.framework.plugin.api.util.UtContext
import org.utbot.framework.plugin.api.util.withUtContext
import org.utbot.framework.util.description
import org.utbot.framework.plugin.api.util.description
import kotlin.reflect.KClass
import mu.KotlinLogging
import org.junit.jupiter.api.Assertions.assertTrue
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import org.utbot.framework.plugin.api.UtImplicitlyThrownException
import org.utbot.framework.plugin.api.UtOverflowFailure
import org.utbot.framework.plugin.api.UtTestCase
import org.utbot.framework.plugin.api.UtTimeoutException
import org.utbot.framework.plugin.api.util.isCheckedException
import org.utbot.summary.UtSummarySettings.MIN_NUMBER_OF_EXECUTIONS_FOR_CLUSTERING
import org.utbot.summary.clustering.MatrixUniqueness
import org.utbot.summary.clustering.SplitSteps
Expand Down Expand Up @@ -146,8 +147,8 @@ enum class ClusterKind {

private fun UtExecutionResult.clusterKind() = when (this) {
is UtExecutionSuccess -> ClusterKind.SUCCESSFUL_EXECUTIONS
is UtImplicitlyThrownException -> if (this.isCheckedException) ClusterKind.CHECKED_EXCEPTIONS else ClusterKind.ERROR_SUITE
is UtExplicitlyThrownException -> if (this.isCheckedException) ClusterKind.CHECKED_EXCEPTIONS else ClusterKind.EXPLICITLY_THROWN_UNCHECKED_EXCEPTIONS
is UtImplicitlyThrownException -> if (this.exception.isCheckedException) ClusterKind.CHECKED_EXCEPTIONS else ClusterKind.ERROR_SUITE
is UtExplicitlyThrownException -> if (this.exception.isCheckedException) ClusterKind.CHECKED_EXCEPTIONS else ClusterKind.EXPLICITLY_THROWN_UNCHECKED_EXCEPTIONS
is UtOverflowFailure -> ClusterKind.OVERFLOWS
is UtTimeoutException -> ClusterKind.TIMEOUTS
is UtConcreteExecutionFailure -> ClusterKind.CRASH_SUITE
Expand Down