Skip to content

Commit

Permalink
fix(core): subtraction should apply first filter and negate others
Browse files Browse the repository at this point in the history
  • Loading branch information
Malinskiy committed Aug 25, 2023
1 parent aa2922a commit 2d7b736
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,16 @@ class CompositionFilter(


private fun filterWithSubtractOperation(tests: List<Test>): List<Test> {
return filters.fold(tests.toSet()) { acc, f ->
acc.subtract(f.filter(tests))
return when (filters.size) {
0, 1 -> tests

Check warning on line 44 in core/src/main/kotlin/com/malinskiy/marathon/execution/filter/CompositionFilter.kt

View check run for this annotation

Codecov / codecov/patch

core/src/main/kotlin/com/malinskiy/marathon/execution/filter/CompositionFilter.kt#L44

Added line #L44 was not covered by 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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand All @@ -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)
}
}

Expand Down

0 comments on commit 2d7b736

Please sign in to comment.