Skip to content

Commit

Permalink
Added count() and count(predicate) mappings for `Assertion.Builde…
Browse files Browse the repository at this point in the history
…r<Iterable<*>>`
  • Loading branch information
robfletcher committed Dec 1, 2020
1 parent a609164 commit ca62e12
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
16 changes: 16 additions & 0 deletions strikt-core/src/main/kotlin/strikt/assertions/Iterable.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,22 @@ package strikt.assertions

import strikt.api.Assertion.Builder

/**
* Maps this assertion to an assertion over the count of elements in the subject.
*
* @see Iterable.count
*/
fun Builder<out Iterable<*>>.count(): Builder<Int> =
get(Iterable<*>::count)

/**
* Maps this assertion to an assertion over the count of elements matching [predicate].
*
* @see Iterable.count
*/
fun <T : Iterable<E>, E> Builder<T>.count(description: String, predicate: (E) -> Boolean): Builder<Int> =
get("count matching $description") { count(predicate) }

/**
* Applies [Iterable.map] with [function] to the subject and returns an
* assertion builder wrapping the result.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -884,5 +884,19 @@ internal object IterableAssertions : JUnit5Minutests {
}
}
}

context("count mapping") {
test("maps to an assertion on the iterable's size") {
val subject = listOf("catflap", "rubberplant", "marzipan")
expectThat(subject).count().isEqualTo(subject.size)
}

test("maps to an assertion on the count of elements matching the predicate") {
val subject = listOf("catflap", "rubberplant", "marzipan")
expectThat(subject)
.count("elements containing 't'") { it.contains("t") }
.isEqualTo(2)
}
}
}
}

0 comments on commit ca62e12

Please sign in to comment.