Skip to content

Commit

Permalink
Change Arb.filterIsInstance() signature to use single type parameter (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
kshired committed Mar 20, 2024
1 parent 0315867 commit eff1ab0
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,4 @@ fun <A> Arb<A>.filterNot(f: (A) -> Boolean): Arb<A> = filter { !f(it) }
* a particular subtype.
*/
@Suppress("UNCHECKED_CAST")
inline fun <A, reified B : A> Arb<A>.filterIsInstance(): Arb<B> = filter { it is B }.map { it as B }
inline fun <reified B> Arb<*>.filterIsInstance(): Arb<B> = filter { it is B } as Arb<B>
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@ import io.kotest.core.spec.style.FunSpec
import io.kotest.inspectors.forAll
import io.kotest.matchers.collections.shouldContainExactly
import io.kotest.matchers.collections.shouldNotBeIn
import io.kotest.matchers.should
import io.kotest.matchers.shouldBe
import io.kotest.matchers.types.beInstanceOf
import io.kotest.property.Arb
import io.kotest.property.EdgeConfig
import io.kotest.property.RandomSource
import io.kotest.property.Sample
import io.kotest.property.arbitrary.filter
import io.kotest.property.arbitrary.filterIsInstance
import io.kotest.property.arbitrary.int
import io.kotest.property.arbitrary.map
import io.kotest.property.arbitrary.of
Expand Down Expand Up @@ -68,4 +71,11 @@ class FilterTest : FunSpec({
val result = shouldNotThrowAny { arb.single(RandomSource.seeded(1234L)) }
result shouldBe 0
}

test("Arb.filterIsInstance should only keep instances of the given type") {
val arb: Arb<Any> = Arb.of(1, "2", 3.0, "4", 5)
val filtered = arb.filterIsInstance<String>()
val result = filtered.samples().take(100).map { it.value }
result.forAll { it should beInstanceOf(String::class) }
}
})

0 comments on commit eff1ab0

Please sign in to comment.