Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added support for array arbitraries (#3728)
Co-authored-by: Sam <sam@sksamuel.com> Co-authored-by: Leonardo Colman Lopes <dev@leonardo.colman.com.br>
- Loading branch information
1 parent
d950fdb
commit ba28879
Showing
4 changed files
with
113 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 81 additions & 10 deletions
91
kotest-property/src/jvmTest/kotlin/com/sksamuel/kotest/property/arbitrary/ArrayTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,95 @@ | ||
package com.sksamuel.kotest.property.arbitrary | ||
|
||
import io.kotest.core.spec.style.FunSpec | ||
import io.kotest.core.spec.style.DescribeSpec | ||
import io.kotest.inspectors.forAll | ||
import io.kotest.matchers.collections.shouldContain | ||
import io.kotest.matchers.collections.shouldHaveAtLeastSize | ||
import io.kotest.matchers.collections.shouldHaveAtMostSize | ||
import io.kotest.matchers.collections.shouldNotBeEmpty | ||
import io.kotest.matchers.ints.shouldBeGreaterThanOrEqual | ||
import io.kotest.matchers.ints.shouldBeLessThanOrEqual | ||
import io.kotest.matchers.shouldBe | ||
import io.kotest.property.Arb | ||
import io.kotest.property.arbitrary.* | ||
import io.kotest.property.Exhaustive | ||
import io.kotest.property.PropTestConfig | ||
import io.kotest.property.arbitrary.array | ||
import io.kotest.property.arbitrary.byte | ||
import io.kotest.property.arbitrary.byteArray | ||
import io.kotest.property.arbitrary.constant | ||
import io.kotest.property.arbitrary.double | ||
import io.kotest.property.arbitrary.edgecases | ||
import io.kotest.property.arbitrary.int | ||
import io.kotest.property.arbitrary.positiveInt | ||
import io.kotest.property.arbitrary.take | ||
import io.kotest.property.checkAll | ||
import io.kotest.property.exhaustive.constant | ||
import io.kotest.property.forAll | ||
|
||
class ArrayTest : FunSpec({ | ||
test("ByteArray should generate specified lengths") { | ||
Arb.byteArray(Arb.int(5, 15), Arb.byte()).take(1000).toList().forAll { | ||
it.size.shouldBeGreaterThanOrEqual(5) | ||
it.size.shouldBeLessThanOrEqual(15) | ||
class ArrayTest : DescribeSpec({ | ||
describe("ByteArray should") { | ||
it("generate specified lengths") { | ||
Arb.byteArray(Arb.int(5, 15), Arb.byte()).take(1000).toList().forAll { | ||
it.size.shouldBeGreaterThanOrEqual(5) | ||
it.size.shouldBeLessThanOrEqual(15) | ||
} | ||
} | ||
|
||
it("populate random byte values") { | ||
Arb.byteArray(Arb.constant(1000000), Arb.byte()).take(10).toList().forAll { | ||
it.toSet().size shouldBe 256 | ||
} | ||
} | ||
} | ||
|
||
test("ByteArray should populate random byte values") { | ||
Arb.byteArray(Arb.constant(1000000), Arb.byte()).take(10).toList().forAll { | ||
it.toSet().size shouldBe 256 | ||
describe("Arb.array should") { | ||
it("not include empty edge cases as first sample") { | ||
val numGen = Arb.array(Arb.int(), 1..10) | ||
forAll(1, numGen) { it.isNotEmpty() } | ||
} | ||
|
||
it("return arrays of underlying generators") { | ||
val gen = Arb.array(Exhaustive.constant(1), 2..10) | ||
checkAll(gen) { | ||
it.shouldHaveAtLeastSize(2) | ||
it.shouldHaveAtMostSize(10) | ||
it.toSet() shouldBe setOf(1) | ||
} | ||
} | ||
|
||
it("include repeated elements in edge cases") { | ||
val edgeCase = Arb.positiveInt().edgecases().firstOrNull() | ||
Arb.array(Arb.positiveInt()).edgecases() shouldContain listOf(edgeCase, edgeCase) | ||
Arb.array(Arb.positiveInt(), 4..6).edgecases() shouldContain listOf(edgeCase, edgeCase, edgeCase, edgeCase) | ||
} | ||
|
||
it("include empty array in edge cases") { | ||
Arb.array(Arb.positiveInt()).edgecases() shouldContain emptyArray() | ||
} | ||
|
||
it("respect bounds in edge cases") { | ||
val edges = Arb.array(Arb.positiveInt(), 2..10).edgecases().toSet() | ||
edges.forAll { it.shouldNotBeEmpty() } | ||
} | ||
|
||
it("generate arrays of length up to 100 by default") { | ||
checkAll(10_000, Arb.array(Arb.double())) { | ||
it.shouldHaveAtMostSize(100) | ||
} | ||
|
||
checkAll<Array<Double>>(PropTestConfig(iterations = 10_000)) { | ||
it.shouldHaveAtMostSize(100) | ||
} | ||
|
||
forAll<Array<Double>>(PropTestConfig(iterations = 10_000)) { | ||
it.size <= 100 | ||
} | ||
} | ||
|
||
it("generate arrays in the given range") { | ||
checkAll(1000, Arb.array(Arb.double(), 250..500)) { | ||
it.shouldHaveAtLeastSize(250) | ||
it.shouldHaveAtMostSize(500) | ||
} | ||
} | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters