Skip to content

Disabled tests for autogenerated functions #457 #505

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
merged 1 commit into from
Jul 18, 2022
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
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ import java.nio.file.Paths
import java.time.temporal.ChronoUnit
import kotlin.reflect.KClass
import mu.KotlinLogging
import org.utbot.common.filterWhen
import org.utbot.framework.UtSettings
import org.utbot.framework.util.isKnownSyntheticMethod


private val logger = KotlinLogging.logger {}
Expand Down Expand Up @@ -92,6 +95,7 @@ class GenerateTestsCommand :

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

if (targetMethods.isEmpty()) {
Expand Down
21 changes: 21 additions & 0 deletions utbot-core/src/main/kotlin/org/utbot/common/FilterWhen.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package org.utbot.common

/**
* If [condition] is true, returns a list containing only elements matching [predicate].
* Otherwise, returns list with all elements of collection
*/
inline fun <T> Iterable<T>.filterWhen(condition: Boolean, predicate: (T) -> Boolean): List<T> =
if (condition)
this.filter(predicate)
else
this.toList()

/**
* If [condition] is true, returns a sequence containing only elements matching [predicate].
* Otherwise, leaves sequence unchanged
*/
fun <T> Sequence<T>.filterWhen(condition: Boolean, predicate: (T) -> Boolean): Sequence<T> =
if (condition)
this.filter(predicate)
else
this
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,11 @@ object UtSettings {
*/
var singleSelector by getBooleanProperty(true)

/**
* Flag that indicates whether tests for synthetic methods (values, valueOf in enums) should be generated, or not
*/
var skipTestGenerationForSyntheticMethods by getBooleanProperty(true)

override fun toString(): String =
properties
.entries
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package org.utbot.framework.util

import org.utbot.engine.displayName
import org.utbot.framework.plugin.api.UtMethod

fun isKnownSyntheticMethod(method: UtMethod<*>): Boolean =
if (method.clazz.java.isEnum)
method.displayName.substringBefore('(') in KnownSyntheticMethodNames.enumSyntheticMethodNames
else
false

/**
* Contains names of methods that are always autogenerated and thus it is unlikely that
* one would want to generate tests for them.
*/
private object KnownSyntheticMethodNames {
/** List with names of enum methods that are autogenerated */
val enumSyntheticMethodNames = listOf("values", "valueOf")
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import com.intellij.openapi.project.Project
import com.intellij.openapi.roots.OrderEnumerator
import com.intellij.openapi.util.text.StringUtil
import com.intellij.psi.PsiClass
import com.intellij.psi.SyntheticElement
import com.intellij.refactoring.util.classMembers.MemberInfo
import com.intellij.testIntegration.TestIntegrationUtils
import com.intellij.util.concurrency.AppExecutorUtil
Expand Down Expand Up @@ -45,6 +46,7 @@ import java.net.URLClassLoader
import java.nio.file.Path
import java.nio.file.Paths
import java.util.concurrent.TimeUnit
import org.utbot.common.filterWhen
import org.utbot.engine.util.mockListeners.ForceStaticMockListener
import kotlin.reflect.KClass
import kotlin.reflect.full.functions
Expand Down Expand Up @@ -134,6 +136,9 @@ object UtTestsDialogProcessor {
val clazz = classLoader.loadClass(srcClass.qualifiedName).kotlin
val srcMethods = model.selectedMethods?.toList() ?:
TestIntegrationUtils.extractClassMethods(srcClass, false)
.filterWhen(UtSettings.skipTestGenerationForSyntheticMethods) {
it.member !is SyntheticElement
}
findMethodsInClassMatchingSelected(clazz, srcMethods)
}.executeSynchronously()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import com.intellij.openapi.wm.ToolWindowManager
import com.intellij.psi.PsiClass
import com.intellij.psi.PsiManager
import com.intellij.psi.PsiMethod
import com.intellij.psi.SyntheticElement
import com.intellij.refactoring.PackageWrapper
import com.intellij.refactoring.ui.MemberSelectionTable
import com.intellij.refactoring.ui.PackageNameReferenceEditorCombo
Expand Down Expand Up @@ -125,6 +126,7 @@ import javax.swing.JComponent
import javax.swing.JList
import javax.swing.JPanel
import kotlin.streams.toList
import org.utbot.common.filterWhen

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

Expand Down Expand Up @@ -358,6 +360,7 @@ class GenerateTestsDialogWindow(val model: GenerateTestsModel) : DialogWrapper(m
val items: List<MemberInfo>
if (srcClasses.size == 1) {
items = TestIntegrationUtils.extractClassMethods(srcClasses.single(), false)
.filterWhen(UtSettings.skipTestGenerationForSyntheticMethods) { it.member !is SyntheticElement }
updateMethodsTable(items)
} else {
items = srcClasses.map { MemberInfo(it) }
Expand Down
10 changes: 6 additions & 4 deletions utbot-junit-contest/src/main/kotlin/org/utbot/contest/Contest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ import kotlin.reflect.jvm.isAccessible
import kotlin.reflect.jvm.javaConstructor
import kotlin.reflect.jvm.javaGetter
import kotlin.reflect.jvm.javaMethod
import org.utbot.common.filterWhen
import org.utbot.framework.util.isKnownSyntheticMethod

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

val classFilteredMethods = methodsToGenerate
val classFilteredMethods = methodsToGenerate.asSequence()
.map { UtMethod(it.first, kotlinClass) }
.filter { methodNameFilter?.equals(it.callable.name) ?: true }
.filterNot {
it.isConstructor && (it.clazz.isAbstract || it.clazz.java.isEnum)
}
.filterNot { it.isConstructor && (it.clazz.isAbstract || it.clazz.java.isEnum) }
.filterWhen(UtSettings.skipTestGenerationForSyntheticMethods) { !isKnownSyntheticMethod(it) }
.toList()

return if (kotlinClass.nestedClasses.isEmpty()) {
classFilteredMethods
Expand Down