Skip to content

Commit

Permalink
Make function name consistent with other 'having' transformations
Browse files Browse the repository at this point in the history
  • Loading branch information
jzbrooks committed Dec 14, 2024
1 parent 642aa4c commit fd0c651
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 34 deletions.
15 changes: 2 additions & 13 deletions assertk/src/commonMain/kotlin/assertk/assertions/iterable.kt
Original file line number Diff line number Diff line change
Expand Up @@ -137,30 +137,19 @@ internal fun MutableList<*>.removeFirst(value: Any?) {
* Asserts the collection contains at least one instance of a given type.
*
* ```
* assertThat(listOf<Any>("one", "two", 1)).containsInstanceOf<String>().each {
* assertThat(listOf<Any>("one", "two", 1)).havingInstancesOf<String>().each {
* it.hasLength(3)
* }
* ```
*/
inline fun <reified T> Assert<Iterable<*>>.containsInstanceOf(): Assert<List<T>> {
inline fun <reified T> Assert<Iterable<*>>.havingInstancesOf(): Assert<List<T>> {
return transform("contains subtype of ${T::class}") { actual ->
actual.filterIsInstance<T>().also {
if (it.isEmpty()) expected("to contain at least one instance of ${T::class} but was $actual")
}
}
}

/**
* Asserts the collection does not contain an instance of a given type.
*
* ```
* assertThat(listOf<Any>("one", "two", 1)).doesNotContainInstanceOf<Double>()
* ```
*/
inline fun <reified T> Assert<Iterable<*>>.doesNotContainInstanceOf() = given { actual ->
if (actual.any { it is T }) expected("to not contain instances of ${T::class} but was $actual")
}

/**
* Asserts on each item in the iterable. The given lambda will be run for each item.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package test.assertk.assertions

import assertk.all
import assertk.assertFailure
import assertk.assertThat
import assertk.assertions.any
import assertk.assertions.atLeast
Expand All @@ -10,11 +9,10 @@ import assertk.assertions.contains
import assertk.assertions.containsAtLeast
import assertk.assertions.containsExactly
import assertk.assertions.containsExactlyInAnyOrder
import assertk.assertions.containsInstanceOf
import assertk.assertions.havingInstancesOf
import assertk.assertions.containsNone
import assertk.assertions.containsOnly
import assertk.assertions.doesNotContain
import assertk.assertions.doesNotContainInstanceOf
import assertk.assertions.each
import assertk.assertions.exactly
import assertk.assertions.extracting
Expand Down Expand Up @@ -232,32 +230,19 @@ class IterableTest {
}
//endregion

//region containsInstanceOf
@Test fun containsInstanceOf_element_present_passes() {
assertThat(iterableOf(1, "two")).containsInstanceOf<String>().single().isEqualTo("two")
//region havingInstancesOf
@Test fun havingInstancesOf_element_present_passes() {
assertThat(iterableOf(1, "two")).havingInstancesOf<String>().single().isEqualTo("two")
}

@Test fun containsInstanceOf_element_missing_fails() {
@Test fun havingInstancesOf_element_missing_fails() {
val error = assertFailsWith<AssertionError> {
assertThat(iterableOf(1, "two")).containsInstanceOf<Double>()
assertThat(iterableOf(1, "two")).havingInstancesOf<Double>()
}
assertEquals("expected to contain at least one instance of class kotlin.Double but was [1, two]", error.message)
}
//endregion

//region doesNotContainInstanceOf
@Test fun doesNotContainInstanceOf_element_present_fails() {
val error = assertFailsWith<AssertionError>() {
assertThat(iterableOf(1, "two")).doesNotContainInstanceOf<String>()
}
assertEquals("expected to not contain instances of class kotlin.String but was [1, two]", error.message)
}

@Test fun doesNotContainInstanceOf_element_missing_passes() {
assertThat(iterableOf(1, "two")).doesNotContainInstanceOf<Double>()
}
//endregion

//region each
@Test
fun each_empty_list_passes() {
Expand Down

0 comments on commit fd0c651

Please sign in to comment.