Skip to content

Commit a245bc6

Browse files
author
Ivan Volkov
committed
Disable tests for autogenerated functions
1 parent 80e37a2 commit a245bc6

File tree

7 files changed

+63
-4
lines changed

7 files changed

+63
-4
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ import java.nio.file.Paths
2121
import java.time.temporal.ChronoUnit
2222
import kotlin.reflect.KClass
2323
import mu.KotlinLogging
24+
import org.utbot.common.filterWhen
25+
import org.utbot.framework.UtSettings
26+
import org.utbot.framework.util.isKnownSyntheticMethod
2427

2528

2629
private val logger = KotlinLogging.logger {}
@@ -92,6 +95,7 @@ class GenerateTestsCommand :
9295

9396
val classUnderTest: KClass<*> = loadClassBySpecifiedFqn(targetClassFqn)
9497
val targetMethods = classUnderTest.targetMethods()
98+
.filterWhen(UtSettings.skipTestGenerationForSyntheticMethods) { !isKnownSyntheticMethod(it) }
9599
initializeEngine(workingDirectory)
96100

97101
if (targetMethods.isEmpty()) {
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package org.utbot.common
2+
3+
/**
4+
* If [condition] is true, returns a list containing only elements matching [predicate].
5+
* Otherwise, returns list with all elements of collection
6+
*/
7+
inline fun <T> Iterable<T>.filterWhen(condition: Boolean, predicate: (T) -> Boolean): List<T> =
8+
if (condition)
9+
this.filter(predicate)
10+
else
11+
this.toList()
12+
13+
/**
14+
* If [condition] is true, returns a sequence containing only elements matching [predicate].
15+
* Otherwise, leaves sequence unchanged
16+
*/
17+
fun <T> Sequence<T>.filterWhen(condition: Boolean, predicate: (T) -> Boolean): Sequence<T> =
18+
if (condition)
19+
this.filter(predicate)
20+
else
21+
this

utbot-framework-api/src/main/kotlin/org/utbot/framework/UtSettings.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,11 @@ object UtSettings {
375375
*/
376376
var singleSelector by getBooleanProperty(true)
377377

378+
/**
379+
* Flag that indicates whether tests for synthetic methods (values, valueOf in enums) should be generated, or not
380+
*/
381+
var skipTestGenerationForSyntheticMethods by getBooleanProperty(true)
382+
378383
override fun toString(): String =
379384
properties
380385
.entries
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package org.utbot.framework.util
2+
3+
import org.utbot.engine.displayName
4+
import org.utbot.framework.plugin.api.UtMethod
5+
6+
fun isKnownSyntheticMethod(method: UtMethod<*>): Boolean =
7+
if (method.clazz.java.isEnum)
8+
method.displayName.substringBefore('(') in KnownSyntheticMethodNames.enumSyntheticMethodNames
9+
else
10+
false
11+
12+
/**
13+
* Contains names of methods that are always autogenerated and thus it is unlikely that
14+
* one would want to generate tests for them.
15+
*/
16+
private object KnownSyntheticMethodNames {
17+
/** List with names of enum methods that are autogenerated */
18+
val enumSyntheticMethodNames = listOf("values", "valueOf")
19+
}

utbot-intellij/src/main/kotlin/org/utbot/intellij/plugin/generator/UtTestsDialogProcessor.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import com.intellij.openapi.project.Project
1515
import com.intellij.openapi.roots.OrderEnumerator
1616
import com.intellij.openapi.util.text.StringUtil
1717
import com.intellij.psi.PsiClass
18+
import com.intellij.psi.SyntheticElement
1819
import com.intellij.refactoring.util.classMembers.MemberInfo
1920
import com.intellij.testIntegration.TestIntegrationUtils
2021
import com.intellij.util.concurrency.AppExecutorUtil
@@ -45,6 +46,7 @@ import java.net.URLClassLoader
4546
import java.nio.file.Path
4647
import java.nio.file.Paths
4748
import java.util.concurrent.TimeUnit
49+
import org.utbot.common.filterWhen
4850
import org.utbot.engine.util.mockListeners.ForceStaticMockListener
4951
import kotlin.reflect.KClass
5052
import kotlin.reflect.full.functions
@@ -134,6 +136,9 @@ object UtTestsDialogProcessor {
134136
val clazz = classLoader.loadClass(srcClass.qualifiedName).kotlin
135137
val srcMethods = model.selectedMethods?.toList() ?:
136138
TestIntegrationUtils.extractClassMethods(srcClass, false)
139+
.filterWhen(UtSettings.skipTestGenerationForSyntheticMethods) {
140+
it.member !is SyntheticElement
141+
}
137142
findMethodsInClassMatchingSelected(clazz, srcMethods)
138143
}.executeSynchronously()
139144

utbot-intellij/src/main/kotlin/org/utbot/intellij/plugin/ui/GenerateTestsDialogWindow.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import com.intellij.openapi.wm.ToolWindowManager
3939
import com.intellij.psi.PsiClass
4040
import com.intellij.psi.PsiManager
4141
import com.intellij.psi.PsiMethod
42+
import com.intellij.psi.SyntheticElement
4243
import com.intellij.refactoring.PackageWrapper
4344
import com.intellij.refactoring.ui.MemberSelectionTable
4445
import com.intellij.refactoring.ui.PackageNameReferenceEditorCombo
@@ -125,6 +126,7 @@ import javax.swing.JComponent
125126
import javax.swing.JList
126127
import javax.swing.JPanel
127128
import kotlin.streams.toList
129+
import org.utbot.common.filterWhen
128130

129131
private const val RECENTS_KEY = "org.utbot.recents"
130132

@@ -358,6 +360,7 @@ class GenerateTestsDialogWindow(val model: GenerateTestsModel) : DialogWrapper(m
358360
val items: List<MemberInfo>
359361
if (srcClasses.size == 1) {
360362
items = TestIntegrationUtils.extractClassMethods(srcClasses.single(), false)
363+
.filterWhen(UtSettings.skipTestGenerationForSyntheticMethods) { it.member !is SyntheticElement }
361364
updateMethodsTable(items)
362365
} else {
363366
items = srcClasses.map { MemberInfo(it) }

utbot-junit-contest/src/main/kotlin/org/utbot/contest/Contest.kt

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ import kotlin.reflect.jvm.isAccessible
6161
import kotlin.reflect.jvm.javaConstructor
6262
import kotlin.reflect.jvm.javaGetter
6363
import kotlin.reflect.jvm.javaMethod
64+
import org.utbot.common.filterWhen
65+
import org.utbot.framework.util.isKnownSyntheticMethod
6466

6567
internal const val junitVersion = 4
6668
private val logger = KotlinLogging.logger {}
@@ -442,12 +444,12 @@ private fun prepareClass(kotlinClass: KClass<*>, methodNameFilter: String?): Lis
442444
//join
443445
.union(kotlin2javaCtors)
444446

445-
val classFilteredMethods = methodsToGenerate
447+
val classFilteredMethods = methodsToGenerate.asSequence()
446448
.map { UtMethod(it.first, kotlinClass) }
447449
.filter { methodNameFilter?.equals(it.callable.name) ?: true }
448-
.filterNot {
449-
it.isConstructor && (it.clazz.isAbstract || it.clazz.java.isEnum)
450-
}
450+
.filterNot { it.isConstructor && (it.clazz.isAbstract || it.clazz.java.isEnum) }
451+
.filterWhen(UtSettings.skipTestGenerationForSyntheticMethods) { !isKnownSyntheticMethod(it) }
452+
.toList()
451453

452454
return if (kotlinClass.nestedClasses.isEmpty()) {
453455
classFilteredMethods

0 commit comments

Comments
 (0)