Skip to content

Commit

Permalink
Add chainable filter methods
Browse files Browse the repository at this point in the history
  • Loading branch information
janseeger committed Feb 23, 2024
1 parent 8da45a0 commit f9f6352
Show file tree
Hide file tree
Showing 2 changed files with 131 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package de.sipgate.dachlatten.primitives.filter

suspend fun <T> Iterable<T>.filter(predicate: Predicate<T>) = filter { item -> predicate(item) }

interface Predicate<T> {
suspend operator fun invoke(test: T): Boolean
}

infix fun <T> Predicate<T>.and(other: Predicate<T>) = object : Predicate<T> {
override suspend fun invoke(test: T): Boolean {
return this@and(test) && other(test)
}
}

infix fun <T> Predicate<T>.or(other: Predicate<T>) = object : Predicate<T> {
override suspend fun invoke(test: T): Boolean {
return this@or(test) || other(test)
}
}

fun <T> not(other: Predicate<T>) = object : Predicate<T> {
override suspend fun invoke(test: T): Boolean = !other(test)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package de.sipgate.dachlatten.primitives.filter

import kotlinx.coroutines.test.runTest
import org.junit.jupiter.api.Assertions.assertFalse
import org.junit.jupiter.api.Assertions.assertTrue
import org.junit.jupiter.api.Test

class PredicateTest {
private val truePredicate = object : Predicate<Any> {
override suspend fun invoke(test: Any): Boolean = true
}

private val falsePredicate = object : Predicate<Any> {
override suspend fun invoke(test: Any): Boolean = false
}

@Test
fun truePredicateReturnsTrueResult() = runTest {
val result = truePredicate(Unit)
assertTrue(result)
}

@Test
fun falsePredicateReturnsFalseResult() = runTest {
val result = falsePredicate(Unit)
assertFalse(result)
}

@Test
fun trueAndTruePredicatesCombineToTrueResult() = runTest {
val combined = truePredicate and truePredicate
val result = combined(Unit)

assertTrue(result)
}

@Test
fun trueAndFalsePredicatesCombineToFalseResult() = runTest {
val combined = truePredicate and falsePredicate
val result = combined(Unit)

assertFalse(result)
}

@Test
fun falseAndTruePredicatesCombineToFalseResult() = runTest {
val combined = falsePredicate and truePredicate
val result = combined(Unit)

assertFalse(result)
}

@Test
fun falseAndfalsePredicatesCombineToFalseResult() = runTest {
val combined = falsePredicate and falsePredicate
val result = combined(Unit)

assertFalse(result)
}

@Test
fun trueOrTruePredicatesCombineToTrueResult() = runTest {
val combined = truePredicate or truePredicate
val result = combined(Unit)

assertTrue(result)
}

@Test
fun trueOrFalsePredicatesCombineToTrueResult() = runTest {
val combined = truePredicate or falsePredicate
val result = combined(Unit)

assertTrue(result)
}

@Test
fun falseOrTruePredicatesCombineToTrueResult() = runTest {
val combined = falsePredicate or truePredicate
val result = combined(Unit)

assertTrue(result)
}

@Test
fun falseOrfalsePredicatesCombineToFalseResult() = runTest {
val combined = falsePredicate or falsePredicate
val result = combined(Unit)

assertFalse(result)
}

@Test
fun notOnTrueResultsInFalse() = runTest {
val not = not(truePredicate)
val result = not(Unit)

assertFalse(result)
}

@Test
fun notOnFalseResultsInTrue() = runTest {
val not = not(falsePredicate)
val result = not(Unit)

assertTrue(result)
}
}

0 comments on commit f9f6352

Please sign in to comment.