Skip to content

Commit 2f9f48e

Browse files
committed
Disable test generation for synthetic methods of data classes
1 parent 5bd3235 commit 2f9f48e

File tree

3 files changed

+31
-7
lines changed
  • utbot-framework/src/main/kotlin/org/utbot/framework/util
  • utbot-framework-api/src/main/kotlin/org/utbot/framework/plugin/api/util
  • utbot-intellij/src/main/kotlin/org/utbot/intellij/plugin/util

3 files changed

+31
-7
lines changed

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: 18 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,13 @@ 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+
"component[1-9][0-9]*"
32+
).map { it.toRegex() }
2033
}

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)