Skip to content

Commit ecefd52

Browse files
author
Ivan Volkov
committed
Rewrote filters for synthetic elements with filterWhen
1 parent fc590f7 commit ecefd52

File tree

5 files changed

+27
-10
lines changed

5 files changed

+27
-10
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ 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
2425
import org.utbot.framework.UtSettings
2526
import org.utbot.framework.util.isSynthetic
2627

@@ -93,9 +94,8 @@ class GenerateTestsCommand :
9394
logger.debug { "Classpath to be used: ${newline()} $classPath ${newline()}" }
9495

9596
val classUnderTest: KClass<*> = loadClassBySpecifiedFqn(targetClassFqn)
96-
val targetMethods = classUnderTest.targetMethods().filter {
97-
UtSettings.generateForSyntheticMethods || isSynthetic(it)
98-
}
97+
val targetMethods = classUnderTest.targetMethods()
98+
.filterWhen(!UtSettings.generateForSyntheticMethods) { !isSynthetic(it) }
9999
initializeEngine(workingDirectory)
100100

101101
if (targetMethods.isEmpty()) {
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package org.utbot.common
2+
3+
// TODO: add docs
4+
inline fun <T> Iterable<T>.filterWhen(condition: Boolean, predicate: (T) -> Boolean): List<T> =
5+
if (condition)
6+
this.filter(predicate)
7+
else
8+
this.toList()
9+
10+
// TODO: add docs
11+
fun <T> Sequence<T>.filterWhen(condition: Boolean, predicate: (T) -> Boolean): Sequence<T> =
12+
if (condition)
13+
this.filter(predicate)
14+
else
15+
this

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ import javax.swing.JList
118118
import javax.swing.JPanel
119119
import kotlin.streams.toList
120120
import org.jetbrains.concurrency.thenRun
121+
import org.utbot.common.filterWhen
121122
import org.utbot.intellij.plugin.ui.utils.allLibraries
122123

123124
private const val RECENTS_KEY = "org.utbot.recents"
@@ -347,7 +348,7 @@ class GenerateTestsDialogWindow(val model: GenerateTestsModel) : DialogWrapper(m
347348
val items: List<MemberInfo>
348349
if (srcClasses.size == 1) {
349350
items = TestIntegrationUtils.extractClassMethods(srcClasses.single(), false)
350-
.filter { UtSettings.generateForSyntheticMethods || it.member !is SyntheticElement }
351+
.filterWhen(!UtSettings.generateForSyntheticMethods) { it.member !is SyntheticElement }
351352
updateMethodsTable(items)
352353
} else {
353354
items = srcClasses.map { MemberInfo(it) }

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ import java.nio.file.Paths
4141
import java.util.concurrent.TimeUnit
4242
import mu.KotlinLogging
4343
import org.jetbrains.kotlin.idea.util.module
44+
import org.utbot.common.filterWhen
4445
import org.utbot.engine.util.mockListeners.ForceMockListener
4546
import org.utbot.engine.util.mockListeners.ForceStaticMockListener
4647
import org.utbot.intellij.plugin.error.showErrorDialogLater
@@ -129,9 +130,10 @@ object UtTestsDialogProcessor {
129130
val methods = ReadAction.nonBlocking<List<UtMethod<*>>> {
130131
val clazz = classLoader.loadClass(srcClass.qualifiedName).kotlin
131132
val srcMethods = model.selectedMethods?.toList() ?:
132-
TestIntegrationUtils.extractClassMethods(srcClass, false).filter {
133-
UtSettings.generateForSyntheticMethods || it.member !is SyntheticElement
134-
}
133+
TestIntegrationUtils.extractClassMethods(srcClass, false)
134+
.filterWhen(!UtSettings.generateForSyntheticMethods) {
135+
it.member !is SyntheticElement
136+
}
135137
findMethodsInClassMatchingSelected(clazz, srcMethods)
136138
}.executeSynchronously()
137139

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ import kotlinx.coroutines.withTimeoutOrNull
6666
import kotlinx.coroutines.yield
6767
import mu.KotlinLogging
6868
import org.apache.commons.io.FileUtils
69+
import org.utbot.common.filterWhen
6970
import org.utbot.framework.util.isSynthetic
7071

7172
internal const val junitVersion = 4
@@ -472,9 +473,7 @@ private fun prepareClass(kotlinClass: KClass<*>, methodNameFilter: String?): Lis
472473
.filter { methodNameFilter?.equals(it.callable.name) ?: true }
473474
.filterNot {
474475
it.isConstructor && (it.clazz.isAbstract || it.clazz.java.isEnum)
475-
}.filter {
476-
UtSettings.generateForSyntheticMethods || !isSynthetic(it)
477-
}
476+
}.filterWhen(!UtSettings.generateForSyntheticMethods) { !isSynthetic(it) }
478477
.toList()
479478

480479
return if (kotlinClass.nestedClasses.isEmpty()) {

0 commit comments

Comments
 (0)