diff --git a/core/src/main/kotlin/com/malinskiy/marathon/execution/filter/CompositionFilter.kt b/core/src/main/kotlin/com/malinskiy/marathon/execution/filter/CompositionFilter.kt index 1986510a0..e566c8186 100644 --- a/core/src/main/kotlin/com/malinskiy/marathon/execution/filter/CompositionFilter.kt +++ b/core/src/main/kotlin/com/malinskiy/marathon/execution/filter/CompositionFilter.kt @@ -40,10 +40,16 @@ class CompositionFilter( private fun filterWithSubtractOperation(tests: List): List { - return filters.fold(tests.toSet()) { acc, f -> - acc.subtract(f.filter(tests)) + return when (filters.size) { + 0, 1 -> tests + else -> { + val left = filters.first().filter(tests) + filters.slice(1..filters.size - 1).fold(left.toSet()) { acc, f -> + acc.subtract(f.filter(left)) - }.toList() + }.toList() + } + } } override fun equals(other: Any?): Boolean { diff --git a/core/src/test/kotlin/com/malinskiy/marathon/execution/filter/CompositionFilterTest.kt b/core/src/test/kotlin/com/malinskiy/marathon/execution/filter/CompositionFilterTest.kt index 5316a90ea..55d603c5e 100644 --- a/core/src/test/kotlin/com/malinskiy/marathon/execution/filter/CompositionFilterTest.kt +++ b/core/src/test/kotlin/com/malinskiy/marathon/execution/filter/CompositionFilterTest.kt @@ -11,6 +11,7 @@ class CompositionFilterTest { private val dogTest = stubTest("FilterAnimalDogTest", "com.example.BestAnimal") private val catTest = stubTest("FilterAnimalCatTest", "") private val horseTest = stubTest("FilterAnimalHorseTest", "") + private val cubeTest = stubTest("FilterCubeTest", "") val tests = listOf( dogTest, catTest, @@ -33,9 +34,9 @@ class CompositionFilterTest { TestFilterConfiguration.CompositionFilterConfiguration.OPERATION.INTERSECTION ).toTestFilter() - private val composition = TestFilterConfiguration.CompositionFilterConfiguration( + private val subtraction = TestFilterConfiguration.CompositionFilterConfiguration( listOf( - TestFilterConfiguration.SimpleClassnameFilterConfiguration(".*Dog.*".toRegex()), + TestFilterConfiguration.SimpleClassnameFilterConfiguration(".*Animal.*".toRegex()), TestFilterConfiguration.AnnotationFilterConfiguration("com.example.BestAnimal".toRegex()) ), TestFilterConfiguration.CompositionFilterConfiguration.OPERATION.SUBTRACT @@ -62,13 +63,13 @@ class CompositionFilterTest { } @Test - fun shouldFilterComposition() { - composition.filter(tests) shouldBeEqualTo listOf(catTest, horseTest) + fun shouldFilterSubtraction() { + subtraction.filter(tests + cubeTest) shouldBeEqualTo listOf(catTest, horseTest) } @Test - fun shouldFilterNotComposition() { - composition.filterNot(tests) shouldBeEqualTo listOf(dogTest) + fun shouldFilterNotSubtraction() { + subtraction.filterNot(tests + cubeTest) shouldBeEqualTo listOf(dogTest, cubeTest) } }