Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Arb.list failing within edge cases in case of null values #3982

Merged
merged 3 commits into from
May 9, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,17 @@ fun <A> Arb.Companion.list(gen: Gen<A>, range: IntRange = 0..100): Arb<List<A>>
edgecaseFn = { rs ->
val emptyList = emptyList<A>()
val singleList: List<A>? = when (gen) {
is Arb -> (gen.edgecase(rs) ?: gen.next(rs))?.let { listOf(it) }
is Exhaustive -> gen.values.firstOrNull()?.let { listOf(it) }
is Arb -> listOf(if (gen.edgecases().isNotEmpty()) gen.edgecases().random(rs.random) else gen.next(rs))
is Exhaustive -> gen.values.let { if (it.isEmpty()) null else listOf(it.first()) }
}
val repeatedList: List<A>? = when {
range.last < 2 -> null // too small for repeats
gen is Arb -> (gen.edgecase(rs) ?: gen.next(rs))?.let { a -> List(max(2, range.first)) { a } }
gen is Exhaustive -> gen.values.firstOrNull()?.let { a -> List(max(2, range.first)) { a } }
gen is Arb -> (if (gen.edgecases().isNotEmpty()) gen.edgecases().random(rs.random) else gen.next(rs))
.let { a -> List(max(2, range.first)) { a } }

gen is Exhaustive -> if (gen.values.isEmpty()) null else gen.values.first()
.let { a -> List(max(2, range.first)) { a } }

else -> null
}
listOfNotNull(emptyList, singleList, repeatedList).filter { it.size in range }.distinct().random(rs.random)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import io.kotest.matchers.shouldBe
import io.kotest.property.Arb
import io.kotest.property.Exhaustive
import io.kotest.property.RandomSource
import io.kotest.property.arbitrary.constant
import io.kotest.property.arbitrary.double
import io.kotest.property.arbitrary.edgecases
import io.kotest.property.arbitrary.int
Expand Down Expand Up @@ -91,6 +92,11 @@ class CollectionsTest : DescribeSpec({
it.shouldHaveAtMostSize(500)
}
}

it("not fail on edge cases for non-empty lists with null values") {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as the success here is "it doesn't throw an exception", should we say so: shouldNotThrowAny ...
also do we need to add any other checks for the output?

Arb.list(Arb.constant(null), 1..100).edgecases().forAll {}
Arb.list(Exhaustive.constant(null), 1..100).edgecases().forAll {}
}
}

describe("Arb.set should") {
Expand Down