Skip to content

Commit 8911095

Browse files
committed
Disable test generation for synthetic methods of data classes
1 parent 58609af commit 8911095

File tree

4 files changed

+34
-9
lines changed

4 files changed

+34
-9
lines changed

utbot-cli/src/main/kotlin/org/utbot/cli/GenerateTestsCommand.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ class GenerateTestsCommand :
5050
help = "Specifies source code file for a generated test"
5151
)
5252
.required()
53-
.check("Must exist and ends with *.java suffix") {
54-
it.endsWith(".java") && Files.exists(Paths.get(it))
53+
.check("Must exist and end with .java or .kt suffix") {
54+
(it.endsWith(".java") || it.endsWith(".kt")) && Files.exists(Paths.get(it))
5555
}
5656

5757
private val projectRoot by option(

utbot-framework-api/src/main/kotlin/org/utbot/framework/plugin/api/util/IdUtil.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,9 @@ val ClassId.isIterableOrMap: Boolean
363363
val ClassId.isEnum: Boolean
364364
get() = jClass.isEnum
365365

366+
val ClassId.isData: Boolean
367+
get() = kClass.isData
368+
366369
fun ClassId.findFieldByIdOrNull(fieldId: FieldId): Field? {
367370
if (isNotSubtypeOf(fieldId.declaringClass)) {
368371
return null
Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
package org.utbot.framework.util
22

33
import org.utbot.framework.plugin.api.ExecutableId
4-
import org.utbot.framework.plugin.api.util.humanReadableName
4+
import org.utbot.framework.plugin.api.util.isData
55
import org.utbot.framework.plugin.api.util.isEnum
66

7-
fun isKnownSyntheticMethod(method: ExecutableId): Boolean =
7+
fun isKnownSyntheticMethod(method: ExecutableId): Boolean {
88
if (method.classId.isEnum)
9-
method.humanReadableName.substringBefore('(') in KnownSyntheticMethodNames.enumSyntheticMethodNames
10-
else
11-
false
9+
return method.name in KnownSyntheticMethodNames.enumSyntheticMethodNames
10+
11+
if (method.classId.isData)
12+
return KnownSyntheticMethodNames.dataClassSyntheticMethodNames.any { it.matches(method.name) }
13+
14+
return false
15+
}
1216

1317
/**
1418
* Contains names of methods that are always autogenerated and thus it is unlikely that
@@ -17,4 +21,14 @@ fun isKnownSyntheticMethod(method: ExecutableId): Boolean =
1721
private object KnownSyntheticMethodNames {
1822
/** List with names of enum methods that are autogenerated */
1923
val enumSyntheticMethodNames = listOf("values", "valueOf")
24+
25+
/** List with names of data classes methods that are autogenerated in Kotlin */
26+
val dataClassSyntheticMethodNames = listOf(
27+
"equals",
28+
"hashCode",
29+
"toString",
30+
"copy",
31+
"copy\\\$default",
32+
"component[1-9][0-9]*"
33+
).map { it.toRegex() }
2034
}

utbot-intellij/src/main/kotlin/org/utbot/intellij/plugin/util/PsiClassHelper.kt

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import com.intellij.testIntegration.TestIntegrationUtils
99
import org.jetbrains.kotlin.asJava.elements.KtLightMethod
1010
import org.jetbrains.kotlin.asJava.elements.isGetter
1111
import org.jetbrains.kotlin.asJava.elements.isSetter
12+
import org.jetbrains.kotlin.psi.KtClass
1213
import org.utbot.common.filterWhen
1314
import org.utbot.framework.UtSettings
1415

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

25-
private fun Iterable<MemberInfo>.filterTestableMethods(): List<MemberInfo> = this
26-
.filterWhen(UtSettings.skipTestGenerationForSyntheticMethods) { it.member !is SyntheticElement }
26+
// By now, we think that method in Kotlin is synthetic iff navigation to its declaration leads to its declaring class
27+
// rather than the method itself (because synthetic methods don't have bodies that we can navigate to)
28+
private val PsiMember.isSyntheticKotlinMethod: Boolean
29+
get() = this is KtLightMethod && navigationElement is KtClass
30+
31+
fun Iterable<MemberInfo>.filterTestableMethods(): List<MemberInfo> = this
32+
.filterWhen(UtSettings.skipTestGenerationForSyntheticMethods) {
33+
it.member !is SyntheticElement && !it.member.isSyntheticKotlinMethod
34+
}
2735
.filterNot { it.member.isAbstract }
2836
.filterNot { it.member.isKotlinGetterOrSetter }
2937

0 commit comments

Comments
 (0)