-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #219 from robstoll/feature/ifNotEmpty
introduce ifNotEmpty, drop own Atrium verb
- Loading branch information
Showing
19 changed files
with
101 additions
and
55 deletions.
There are no files selected for viewing
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,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 |
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
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
2 changes: 1 addition & 1 deletion
2
src/commonTest/kotlin/ch/tutteli/kbox/DynamicTreeTraversalSpec.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
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
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
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
2 changes: 1 addition & 1 deletion
2
src/commonTest/kotlin/ch/tutteli/kbox/ForElementAndForEachSpec.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
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,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) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
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
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
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
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
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
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
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
This file was deleted.
Oops, something went wrong.
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,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
20
src/commonTest/kotlin/ch/tutteli/kbox/atrium/atriumVerbs.kt
This file was deleted.
Oops, something went wrong.