Skip to content

Commit

Permalink
Don't throw MultipleFailuresError if failure list is empty
Browse files Browse the repository at this point in the history
In a nested context this was causing failures to be dropped since it was
mapping to an empty list.

Fixes #314
  • Loading branch information
evant committed Aug 23, 2020
1 parent 18a6bed commit 1a8ca7c
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 10 deletions.
12 changes: 7 additions & 5 deletions assertk/src/commonMain/kotlin/assertk/assertions/iterable.kt
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,13 @@ fun Assert<Iterable<*>>.containsOnly(vararg elements: Any?) = given { actual ->
* }
* ```
*/
fun <E> Assert<Iterable<E>>.each(f: (Assert<E>) -> Unit) = given { actual ->
all {
actual.forEachIndexed { index, item ->
f(assertThat(item, name = appendName(show(index, "[]"))))
}
fun <E> Assert<Iterable<E>>.each(f: (Assert<E>) -> Unit) {
all { doEach(f) }
}

internal fun <E> Assert<Iterable<E>>.doEach(f: (Assert<E>) -> Unit) = given { actual ->
actual.forEachIndexed { index, item ->
f(assertThat(item, name = appendName(show(index, "[]"))))
}
}

Expand Down
16 changes: 11 additions & 5 deletions assertk/src/commonMain/kotlin/assertk/failure.kt
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,17 @@ internal class SoftFailure(
}

private fun compositeErrorMessage(errors: List<Throwable>): Throwable {
return if (errors.size == 1) {
errors.first()
} else {
MultipleFailuresError(message, errors).apply {
errors.forEach(this::addSuppressed)
return when {
errors.isEmpty() -> {
AssertionFailedError(message)
}
errors.size == 1 -> {
errors.first()
}
else -> {
MultipleFailuresError(message, errors).apply {
errors.forEach(this::addSuppressed)
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,17 @@ class IterableTest {
)
}

@Test fun exactly_too_few_inside_all_fails() {
val error = assertFails {
assertThat(listOf(5, 4, 3) as Iterable<Int>).all {
exactly(2) { it.isGreaterThan(2) }
}
}
assertEquals(
"""expected to pass exactly 2 times""".trimMargin(), error.message
)
}

@Test fun exactly_times_passed_passes() {
assertThat(listOf(0, 1, 2) as Iterable<Int>).exactly(2) { it.isGreaterThan(0) }
}
Expand Down

0 comments on commit 1a8ca7c

Please sign in to comment.