-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
131 additions
and
0 deletions.
There are no files selected for viewing
23 changes: 23 additions & 0 deletions
23
dachlatten-primitives/src/main/kotlin/de/sipgate/dachlatten/primitives/filter/Filter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
108 changes: 108 additions & 0 deletions
108
...atten-primitives/src/test/kotlin/de/sipgate/dachlatten/primitives/filter/PredicateTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |