diff --git a/dachlatten-primitives/src/main/kotlin/de/sipgate/dachlatten/primitives/CollectionExt.kt b/dachlatten-primitives/src/main/kotlin/de/sipgate/dachlatten/primitives/CollectionExt.kt new file mode 100644 index 0000000..a12daae --- /dev/null +++ b/dachlatten-primitives/src/main/kotlin/de/sipgate/dachlatten/primitives/CollectionExt.kt @@ -0,0 +1,7 @@ +package de.sipgate.dachlatten.primitives + +import de.sipgate.dachlatten.primitives.predicates.Predicate + +fun Collection.contains(predicate: Predicate): Boolean { + return firstOrNull { predicate(it) } != null +} diff --git a/dachlatten-primitives/src/test/kotlin/de/sipgate/dachlatten/primitives/ComplexContainsTest.kt b/dachlatten-primitives/src/test/kotlin/de/sipgate/dachlatten/primitives/ComplexContainsTest.kt new file mode 100644 index 0000000..f7df796 --- /dev/null +++ b/dachlatten-primitives/src/test/kotlin/de/sipgate/dachlatten/primitives/ComplexContainsTest.kt @@ -0,0 +1,45 @@ +package de.sipgate.dachlatten.primitives + +import de.sipgate.dachlatten.primitives.predicates.Predicate +import de.sipgate.dachlatten.primitives.predicates.not +import kotlinx.coroutines.test.runTest +import kotlin.test.Test +import kotlin.test.assertFalse +import kotlin.test.assertTrue + +class ComplexContainsTest { + + val list = listOf("a1entry", "b2entry", "otherEntry") + + @Test + fun testSimpleContains() = runTest { + val result = list.contains { it == "a1entry" } + assertTrue(result) + } + + @Test + fun testSimpleContainsNegative() = runTest { + val result = list.contains { it == "does-not-exist" } + assertFalse(result) + } + + @Test + fun testComplexContains() = runTest { + val result = list.contains { it.all { it.isLetter() } } + assertTrue(result) + } + + @Test + fun testComplexContainsNegative() = runTest { + val result = list.contains { it.all { it.isWhitespace() } } + assertFalse(result) + } + + @Test + fun testComplexContainsInvert() = runTest { + val complexPredicate: Predicate = { it.all { it.isWhitespace() } } + val invertedPredicate = !complexPredicate + val result = list.contains(invertedPredicate) + assertTrue(result) + } +}