Skip to content

Commit

Permalink
Merge pull request #219 from robstoll/feature/ifNotEmpty
Browse files Browse the repository at this point in the history
introduce ifNotEmpty, drop own Atrium verb
  • Loading branch information
robstoll authored May 23, 2024
2 parents 740a921 + 87c6d6e commit 1f4b94a
Show file tree
Hide file tree
Showing 19 changed files with 101 additions and 55 deletions.
25 changes: 25 additions & 0 deletions src/commonMain/kotlin/ch/tutteli/kbox/ifNotEmpty.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package ch.tutteli.kbox

/**
* Checks if this collection is empty and if not applies the given [transformation].
*
* @param transformation the function which transforms this collection into [SuperTypeOfT] (at least an
* [Iterable])
*
* @param T The type of the receiver -- a [Collection] or one of its subtypes
* @param SuperTypeOfT The result type can also be a super type of [T] as long as it doesn't go
* beyond [Iterable]. In theory, we would not need to establish this constraint but in all cases we came across
* so far we never wanted to end up with [Any]. E.g. if [T] = [List] then we don't want that the
* [transformation] returns an [Array] as otherwise we have a [List] in case it is empty and an [Array] otherwise and
* the least upper bound of those two types is [Any], i.e. we would end up with [SuperTypeOfT] = [Any].
*
* @return `this` if the given collection is empty, otherwise the result of applying this collection to the given
* [transformation].
*
* @since 1.2.0
*/
@Suppress("BOUNDS_NOT_ALLOWED_IF_BOUNDED_BY_TYPE_PARAMETER")
inline fun <T, SuperTypeOfT> T.ifNotEmpty(
transformation: (T) -> SuperTypeOfT
): SuperTypeOfT where T : Collection<*>, SuperTypeOfT : Iterable<*>, T : SuperTypeOfT =
if (this.isNotEmpty()) transformation(this) else this
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package ch.tutteli.kbox

import ch.tutteli.atrium.api.fluent.en_GB.toBeEmpty
import ch.tutteli.atrium.api.fluent.en_GB.toEqual
import ch.tutteli.kbox.atrium.expect
import ch.tutteli.atrium.api.verbs.expect
import org.spekframework.spek2.Spek
import org.spekframework.spek2.style.specification.describe
import kotlin.reflect.KFunction4
Expand Down
2 changes: 1 addition & 1 deletion src/commonTest/kotlin/ch/tutteli/kbox/BlankToNullSpec.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package ch.tutteli.kbox

import ch.tutteli.atrium.api.fluent.en_GB.toEqual
import ch.tutteli.kbox.atrium.expect
import ch.tutteli.atrium.api.verbs.expect
import org.spekframework.spek2.Spek
import org.spekframework.spek2.style.specification.describe

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package ch.tutteli.kbox

import ch.tutteli.atrium.api.fluent.en_GB.*
import ch.tutteli.kbox.atrium.expect
import ch.tutteli.atrium.api.verbs.expect
import org.spekframework.spek2.Spek
import org.spekframework.spek2.style.specification.describe

Expand Down
2 changes: 1 addition & 1 deletion src/commonTest/kotlin/ch/tutteli/kbox/FailIfTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package ch.tutteli.kbox
import ch.tutteli.atrium.api.fluent.en_GB.messageToContain
import ch.tutteli.atrium.api.fluent.en_GB.notToThrow
import ch.tutteli.atrium.api.fluent.en_GB.toThrow
import ch.tutteli.kbox.atrium.expect
import ch.tutteli.atrium.api.verbs.expect
import kotlin.test.Test

class FailIfTest {
Expand Down
2 changes: 1 addition & 1 deletion src/commonTest/kotlin/ch/tutteli/kbox/ForEachInSpec.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package ch.tutteli.kbox

import ch.tutteli.atrium.api.fluent.en_GB.toEqual
import ch.tutteli.kbox.atrium.expect
import ch.tutteli.atrium.api.verbs.expect
import org.spekframework.spek2.Spek
import org.spekframework.spek2.style.specification.describe

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package ch.tutteli.kbox

import ch.tutteli.atrium.api.fluent.en_GB.toContainExactly
import ch.tutteli.atrium.api.fluent.en_GB.toBeEmpty
import ch.tutteli.kbox.atrium.expect
import ch.tutteli.atrium.api.verbs.expect
import org.spekframework.spek2.Spek
import org.spekframework.spek2.style.specification.describe
import kotlin.reflect.KFunction2
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package ch.tutteli.kbox

import ch.tutteli.atrium.api.fluent.en_GB.toEqual
import ch.tutteli.kbox.atrium.expect
import ch.tutteli.atrium.api.verbs.expect
import org.spekframework.spek2.Spek
import org.spekframework.spek2.style.specification.describe
import kotlin.reflect.KFunction3
Expand Down
42 changes: 42 additions & 0 deletions src/commonTest/kotlin/ch/tutteli/kbox/IfNotEmptyTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package ch.tutteli.kbox

import ch.tutteli.atrium.api.fluent.en_GB.*
import ch.tutteli.atrium.api.verbs.expect
import ch.tutteli.atrium.api.verbs.expectGrouped
import kotlin.test.Test

class IfNotEmptyTest {

@Test
fun it_returns_the_collection_itself_if_empty() {
expectGrouped {
listOf<Collection<Int>>(
emptyList(),
emptySet(),
).forEach { collection ->
group("check it works with ${collection::class.simpleName}") {
expect(collection.ifNotEmpty { nonEmptyCollection ->
nonEmptyCollection.map { it + 1 }
}).toBeTheInstance(collection)
}
}
}
}

@Test
fun it_returns_the_result_of_the_transformation_if_not_empty() {
expectGrouped {
listOf(
listOf(1),
setOf(1),
).forEach { collection ->
group("check it works with ${collection::class.simpleName}") {
expect(collection.ifNotEmpty { nonEmptyCollection ->
nonEmptyCollection.map { it + 1 }
}).toContainExactly(2)
}
}
}
}
}

2 changes: 1 addition & 1 deletion src/commonTest/kotlin/ch/tutteli/kbox/IfWithinBoundSpec.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package ch.tutteli.kbox

import ch.tutteli.atrium.api.fluent.en_GB.toEqual
import ch.tutteli.kbox.atrium.expect
import ch.tutteli.atrium.api.verbs.expect
import org.spekframework.spek2.Spek
import org.spekframework.spek2.style.specification.describe

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package ch.tutteli.kbox

import ch.tutteli.atrium.api.fluent.en_GB.toEqual
import ch.tutteli.kbox.atrium.expect
import ch.tutteli.atrium.api.verbs.expect
import org.spekframework.spek2.Spek
import org.spekframework.spek2.style.specification.describe

Expand Down
2 changes: 1 addition & 1 deletion src/commonTest/kotlin/ch/tutteli/kbox/JoinToStringSpec.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package ch.tutteli.kbox

import ch.tutteli.atrium.api.fluent.en_GB.toBeEmpty
import ch.tutteli.atrium.api.fluent.en_GB.toEqual
import ch.tutteli.kbox.atrium.expect
import ch.tutteli.atrium.api.verbs.expect
import org.spekframework.spek2.Spek
import org.spekframework.spek2.style.specification.describe
import kotlin.reflect.KFunction3
Expand Down
2 changes: 1 addition & 1 deletion src/commonTest/kotlin/ch/tutteli/kbox/MapParentsSpec.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package ch.tutteli.kbox

import ch.tutteli.atrium.api.fluent.en_GB.*
import ch.tutteli.kbox.atrium.expect
import ch.tutteli.atrium.api.verbs.expect
import org.spekframework.spek2.Spek
import org.spekframework.spek2.style.specification.Suite
import org.spekframework.spek2.style.specification.describe
Expand Down
2 changes: 1 addition & 1 deletion src/commonTest/kotlin/ch/tutteli/kbox/MapRemainingSpec.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package ch.tutteli.kbox

import ch.tutteli.atrium.api.fluent.en_GB.toContainExactly
import ch.tutteli.atrium.api.fluent.en_GB.toBeEmpty
import ch.tutteli.kbox.atrium.expect
import ch.tutteli.atrium.api.verbs.expect
import org.spekframework.spek2.Spek
import org.spekframework.spek2.style.specification.describe

Expand Down
2 changes: 1 addition & 1 deletion src/commonTest/kotlin/ch/tutteli/kbox/MapWithIndexSpec.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package ch.tutteli.kbox

import ch.tutteli.atrium.api.fluent.en_GB.toContainExactly
import ch.tutteli.atrium.api.fluent.en_GB.toBeEmpty
import ch.tutteli.kbox.atrium.expect
import ch.tutteli.atrium.api.verbs.expect
import org.spekframework.spek2.Spek
import org.spekframework.spek2.style.specification.describe

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package ch.tutteli.kbox
import ch.tutteli.atrium.api.fluent.en_GB.toBeTheInstance
import ch.tutteli.atrium.api.fluent.en_GB.toEqual
import ch.tutteli.atrium.api.fluent.en_GB.toThrow
import ch.tutteli.kbox.atrium.expect
import ch.tutteli.atrium.api.verbs.expect
import org.spekframework.spek2.Spek
import org.spekframework.spek2.lifecycle.CachingMode
import org.spekframework.spek2.style.specification.describe
Expand Down
21 changes: 0 additions & 21 deletions src/commonTest/kotlin/ch/tutteli/kbox/TakeIfSpec.kt

This file was deleted.

20 changes: 20 additions & 0 deletions src/commonTest/kotlin/ch/tutteli/kbox/TakeIfTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package ch.tutteli.kbox

import ch.tutteli.atrium.api.fluent.en_GB.toEqual
import ch.tutteli.atrium.api.verbs.expect
import kotlin.test.Test

class TakeIfTest {

@Test
fun `it_returns_null_if_predicate_doesnt_hold`() {
expect(
takeIf<Int>(false) { throw IllegalArgumentException("bla") }
).toEqual(null)
}

@Test
fun it_returns_lambda_result_if_predicate_holds() {
expect(takeIf(true) { 1 }).toEqual(1)
}
}
20 changes: 0 additions & 20 deletions src/commonTest/kotlin/ch/tutteli/kbox/atrium/atriumVerbs.kt

This file was deleted.

0 comments on commit 1f4b94a

Please sign in to comment.