Skip to content

Disable test generation for synthetic methods of data classes #1191 #1192

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
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
11 changes: 7 additions & 4 deletions utbot-cli/src/main/kotlin/org/utbot/cli/GenerateTestsCommand.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ import org.utbot.framework.plugin.api.CodegenLanguage
import org.utbot.framework.plugin.api.UtMethodTestSet
import org.utbot.framework.plugin.api.util.UtContext
import org.utbot.framework.plugin.api.util.isAbstract
import org.utbot.framework.plugin.api.util.isSynthetic
import org.utbot.framework.plugin.api.util.withUtContext
import org.utbot.framework.util.isKnownSyntheticMethod
import org.utbot.framework.util.isKnownImplicitlyDeclaredMethod
import org.utbot.sarif.SarifReport
import org.utbot.sarif.SourceFindingStrategyDefault
import java.nio.file.Files
Expand Down Expand Up @@ -50,8 +51,8 @@ class GenerateTestsCommand :
help = "Specifies source code file for a generated test"
)
.required()
.check("Must exist and ends with *.java suffix") {
it.endsWith(".java") && Files.exists(Paths.get(it))
.check("Must exist and end with .java or .kt suffix") {
(it.endsWith(".java") || it.endsWith(".kt")) && Files.exists(Paths.get(it))
}

private val projectRoot by option(
Expand Down Expand Up @@ -97,7 +98,9 @@ class GenerateTestsCommand :
withUtContext(UtContext(classLoader)) {
val classIdUnderTest = ClassId(targetClassFqn)
val targetMethods = classIdUnderTest.targetMethods()
.filterWhen(UtSettings.skipTestGenerationForSyntheticMethods) { !isKnownSyntheticMethod(it) }
.filterWhen(UtSettings.skipTestGenerationForSyntheticAndImplicitlyDeclaredMethods) {
!it.isSynthetic && !it.isKnownImplicitlyDeclaredMethod
}
.filterNot { it.isAbstract }
val testCaseGenerator = initializeGenerator(workingDirectory)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package org.utbot.framework
import com.jetbrains.rd.util.LogLevel
import mu.KotlinLogging
import org.utbot.common.AbstractSettings
import java.lang.reflect.Executable
private val logger = KotlinLogging.logger {}

/**
Expand Down Expand Up @@ -361,9 +362,9 @@ object UtSettings : AbstractSettings(
var singleSelector by getBooleanProperty(true)

/**
* Flag that indicates whether tests for synthetic methods (values, valueOf in enums) should be generated, or not
* Flag that indicates whether tests for synthetic (see [Executable.isSynthetic]) and implicitly declared methods (like values, valueOf in enums) should be generated, or not
*/
var skipTestGenerationForSyntheticMethods by getBooleanProperty(true)
var skipTestGenerationForSyntheticAndImplicitlyDeclaredMethods by getBooleanProperty(true)

/**
* Flag that indicates whether should we branch on and set static fields from trusted libraries or not.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.utbot.framework.plugin.api.util

import org.utbot.common.withAccessibility
import org.utbot.framework.plugin.api.BuiltinClassId
import org.utbot.framework.plugin.api.BuiltinConstructorId
import org.utbot.framework.plugin.api.BuiltinMethodId
Expand Down Expand Up @@ -394,6 +393,9 @@ val ClassId.isIterableOrMap: Boolean
val ClassId.isEnum: Boolean
get() = jClass.isEnum

val ClassId.isData: Boolean
get() = kClass.isData

fun ClassId.findFieldByIdOrNull(fieldId: FieldId): Field? {
if (isNotSubtypeOf(fieldId.declaringClass)) {
return null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package org.utbot.framework.plugin.api.util
import org.utbot.framework.plugin.api.ClassId
import org.utbot.framework.plugin.api.ExecutableId
import org.utbot.framework.plugin.api.FieldId
import org.utbot.framework.plugin.api.MethodId
import java.lang.reflect.Modifier

class ModifierFactory private constructor(
Expand Down Expand Up @@ -77,6 +78,9 @@ val ExecutableId.isPackagePrivate: Boolean
val ExecutableId.isAbstract: Boolean
get() = Modifier.isAbstract(modifiers)

val ExecutableId.isSynthetic: Boolean
get() = (this is MethodId) && method.isSynthetic

// FieldIds

val FieldId.isStatic: Boolean
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package org.utbot.framework.util

import org.utbot.framework.plugin.api.ExecutableId
import org.utbot.framework.plugin.api.util.isData
import org.utbot.framework.plugin.api.util.isEnum

/**
* Returns whether this method could be implicitly generated by compiler, or not.
*
* Note that here we can only judge this by method name and kind of class (data class, enum, etc).
* There seems to be no (at least, easy) way to check from bytecode if this method was actually overridden by user,
* so this function will return true even if the matching method is not autogenerated but written explicitly by user.
*/
val ExecutableId.isKnownImplicitlyDeclaredMethod: Boolean
get() =
when {
classId.isEnum -> name in KnownImplicitlyDeclaredMethods.enumImplicitlyDeclaredMethodNames
classId.isData -> KnownImplicitlyDeclaredMethods.dataClassImplicitlyDeclaredMethodNameRegexps.any { it.matches(name) }
else -> false
}

/**
* Contains names of methods that are always autogenerated by compiler and thus it is unlikely that
* one would want to generate tests for them.
*/
private object KnownImplicitlyDeclaredMethods {
/** List with names of enum methods that are generated by compiler */
val enumImplicitlyDeclaredMethodNames = listOf("values", "valueOf")

/** List with regexps that match names of methods that are generated by Kotlin compiler for data classes */
val dataClassImplicitlyDeclaredMethodNameRegexps = listOf(
"equals",
"hashCode",
"toString",
"copy",
"component[1-9][0-9]*"
).map { it.toRegex() }
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import com.intellij.testIntegration.TestIntegrationUtils
import org.jetbrains.kotlin.asJava.elements.KtLightMethod
import org.jetbrains.kotlin.asJava.elements.isGetter
import org.jetbrains.kotlin.asJava.elements.isSetter
import org.jetbrains.kotlin.psi.KtClass
import org.utbot.common.filterWhen
import org.utbot.framework.UtSettings

Expand All @@ -22,8 +23,15 @@ private val PsiMember.isKotlinGetterOrSetter: Boolean
return isGetter || isSetter
}

private fun Iterable<MemberInfo>.filterTestableMethods(): List<MemberInfo> = this
.filterWhen(UtSettings.skipTestGenerationForSyntheticMethods) { it.member !is SyntheticElement }
// By now, we think that method in Kotlin is autogenerated iff navigation to its declaration leads to its declaring class
// rather than the method itself (because such methods don't have bodies that we can navigate to)
private val PsiMember.isKotlinAutogeneratedMethod: Boolean
get() = this is KtLightMethod && navigationElement is KtClass

fun Iterable<MemberInfo>.filterTestableMethods(): List<MemberInfo> = this
.filterWhen(UtSettings.skipTestGenerationForSyntheticAndImplicitlyDeclaredMethods) {
it.member !is SyntheticElement && !it.member.isKotlinAutogeneratedMethod
}
.filterNot { it.member.isAbstract }
.filterNot { it.member.isKotlinGetterOrSetter }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import org.utbot.framework.plugin.api.util.jClass
import org.utbot.framework.plugin.api.util.utContext
import org.utbot.framework.plugin.api.util.withUtContext
import org.utbot.framework.plugin.services.JdkInfoService
import org.utbot.framework.util.isKnownSyntheticMethod
import org.utbot.framework.util.isKnownImplicitlyDeclaredMethod
import org.utbot.fuzzer.UtFuzzedExecution
import org.utbot.instrumentation.ConcreteExecutor
import org.utbot.instrumentation.ConcreteExecutorPool
Expand Down Expand Up @@ -60,6 +60,7 @@ import kotlinx.coroutines.newSingleThreadContext
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.withTimeoutOrNull
import kotlinx.coroutines.yield
import org.utbot.framework.plugin.api.util.isSynthetic

internal const val junitVersion = 4
private val logger = KotlinLogging.logger {}
Expand Down Expand Up @@ -402,7 +403,9 @@ private fun prepareClass(javaClazz: Class<*>, methodNameFilter: String?): List<E
val classFilteredMethods = methodsToGenerate
.map { it.executableId }
.filter { methodNameFilter?.equals(it.name) ?: true }
.filterWhen(UtSettings.skipTestGenerationForSyntheticMethods) { !isKnownSyntheticMethod(it) }
.filterWhen(UtSettings.skipTestGenerationForSyntheticAndImplicitlyDeclaredMethods) {
!it.isSynthetic && !it.isKnownImplicitlyDeclaredMethod
}
.toList()


Expand Down