Best Kotest code snippet using io.kotest.property.exhaustive.ints.Exhaustive.Companion.ints
Gen.kt
Source:Gen.kt
1package io.kotest.property2import io.kotest.property.arbitrary.of3/**4 * A [Gen] is responsible for providing values to be used in property testing. You can think of it as like5 * an input stream for values. Each arg will provide data for a specific type <A>.6 *7 * Gens can be created in two ways: with arbitrary (random) values from instances of [Arb] and8 * exhaustive values over a closed space from instances of [Exhaustive].9 *10 * Arbs generate random values across a given space. The values may be repeated, and some11 * values may never be generated at all. For example generating 1000 random integers between 0 and Int.MAX12 * will clearly not return all possible values, and some values may happen to be generated more than once.13 *14 * Exhaustives generate all values from a given space. This is useful when you want to ensure every15 * value in that space is used. For instance for enum values, it is usually more helpful to ensure each16 * enum is used, rather than picking randomly from the enums values.17 *18 * Both types of gens can be mixed and matched in property tests. For example,19 * you could test a function with 100 random positive integers (arbitrary) alongside every20 * even number from 0 to 200 (exhaustive).21 */22sealed class Gen<out A> {23 /**24 * Returns values from this generator as a lazily generated sequence.25 *26 * If this gen is an [Arb], then each value will either be a sample or an edge case. The bias27 * towards edge cases or samples is given by the value of [EdgeConfig.edgecasesGenerationProbability]28 * inside the [edgeConfig] parameter.29 *30 * If this gen is an [Exhaustive], then the returned values will iterate in turn, repeating31 * once exhausted as required.32 *33 */34 fun generate(35 rs: RandomSource,36 edgeConfig: EdgeConfig = EdgeConfig.default()37 ): Sequence<Sample<A>> =38 when (this) {39 is Arb -> {40 val samples = this.samples(rs).iterator()41 generateSequence {42 val isEdgeCase = rs.random.nextDouble(0.0, 1.0) < edgeConfig.edgecasesGenerationProbability43 if (isEdgeCase) {44 this.edgecase(rs)?.asSample() ?: samples.next()45 } else samples.next()46 }47 }48 is Exhaustive -> {49 check(this.values.isNotEmpty()) { "Exhaustive.values shouldn't be a empty list." }50 generateSequence { this.values.map { Sample(it) } }.flatten()51 }52 }53 /**54 * Returns an optional [Classifier] to label values.55 */56 open val classifier: Classifier<out A>? = null57 /**58 * The minimum iteration count required for this [Gen] to be invoked.59 * Requesting a property test with fewer than this will result in an exception.60 */61 fun minIterations(): Int = when (this) {62 is Exhaustive -> this.values.size63 else -> 164 }65}66/**67 * An [Arb] (short for arbitrary) is a generator of values in two categories: edge cases and samples.68 *69 * Edge cases are values that are a common source of bugs. For example, a function using ints is70 * more likely to fail for common edge cases like zero, minus 1, positive 1, [Int.MAX_VALUE] and [Int.MIN_VALUE]71 * rather than random values like 965489. Therefore, it is useful that we try to include such values72 * rather than relying entirely on random values which are unlikely to generate these.73 *74 * Not all arbitraries will utilize edge cases. For example, if you define an integer generator75 * using a subset of the number space - say from 100 to 200 - then no edge cases would be provided.76 *77 * Samples are chosen randomly from the sample space and are used to give a greater breadth to78 * the test cases. For example, in the case of a function using integers, these random values79 * could be from across the entire integer number line, or could be limited to a subset of ints80 * such as natural numbers or even numbers.81 *82 * In addition to values, arbs can optionally implement a [classify] function which classifies83 * the generated values with labels. These labels can then be used to display information on the84 * types of values generated.85 *86 * In order to use an [Arb] outside a property test, one must invoke the [take] method, passing in87 * the number of iterations required and optionally a [ShrinkingMode].88 */89abstract class Arb<out A> : Gen<A>() {90 /**91 * Returns a single edge case for this arbitrary. If this arb supports multiple edge cases,92 * then one should be chosen randomly each time this function is invoked.93 *94 * Can return null if this arb does not provide edge cases.95 */96 abstract fun edgecase(rs: RandomSource): A?97 /**98 * Returns a single random [Sample] from this [Arb] using the supplied random source.99 */100 abstract fun sample(rs: RandomSource): Sample<A>101 /**102 * Returns a sequence from values generated from this arb.103 * Edgecases will be ignored.104 */105 fun samples(rs: RandomSource = RandomSource.default()): Sequence<Sample<A>> {106 return generateSequence { sample(rs) }107 }108 companion object109}110/**111 * An exhaustive is a type of [Gen] which generates an exhaustive set of values from a defined range.112 *113 * An example of a exhaustive is the sequence of integers from 0 to 100.114 * Another example is all strings of two characters.115 *116 * A progression is useful when you want to generate an exhaustive set of values from a given117 * sample space, rather than random values from that space. For example, if you were testing a118 * function that used an enum, you might prefer to guarantee that every enum value is used, rather119 * than selecting randomly from amongst the enum values (with possible duplicates and gaps).120 *121 * Exhaustives do not shrink their values. There is no need to find a smaller failing case, because122 * the smaller values will themselves naturally be included in the tested values.123 *124 * An exhaustive is less suitable when you have a large sample space you need to select values from.125 */126abstract class Exhaustive<out A> : Gen<A>() {127 /**128 * Returns the values of this [Exhaustive].129 */130 abstract val values: List<A>131 /**132 * Converts this into an [Arb] where the generated values of the returned arb133 * are choosen randomly from the values provided by this exhausive.134 */135 fun toArb(): Arb<A> = Arb.of(values)136 companion object137}138fun interface Classifier<A> {139 fun classify(value: A): String?140}141/**142 * Contains a single generated value from a [Gen] and an [RTree] of lazily evaluated shrinks.143 */144data class Sample<out A>(val value: A, val shrinks: RTree<A> = RTree({ value }))145fun <A> A.asSample(): Sample<A> = Sample(this)146/**147 * Returns a [Sample] with shrinks by using the supplied [Shrinker] against the input value [a].148 */149fun <A> sampleOf(a: A, shrinker: Shrinker<A>) = Sample(a, shrinker.rtree(a))150data class EdgeConfig(151 val edgecasesGenerationProbability: Double = PropertyTesting.defaultEdgecasesGenerationProbability152) {153 companion object;154 init {155 check(edgecasesGenerationProbability in 0.0..1.0) {156 "provided edgecasesProbability $edgecasesGenerationProbability is not between 0.0 and 1.0"157 }158 }159}...
Exhaustive.kt
Source:Exhaustive.kt
1package utils2import io.kotest.property.Arb3import io.kotest.property.Exhaustive4import io.kotest.property.Gen5import io.kotest.property.RandomSource6import io.kotest.property.arbitrary.byte7import io.kotest.property.arbitrary.next8import io.kotest.property.exhaustive.exhaustive9import io.kotest.property.exhaustive.filter10import io.kotest.property.exhaustive.ints11import io.kotest.property.exhaustive.map12fun <N : Number> Exhaustive<N>.toInt() = map { it.toInt() }13fun <N : Number> Exhaustive<N>.toShort() = map { it.toShort() }14fun Exhaustive.Companion.shorts(min: Short = Short.MIN_VALUE, max: Short = Short.MAX_VALUE) =15 Exhaustive.ints(min..max).map { it.toShort() }16fun Exhaustive.Companion.ubytes(min: UByte = UByte.MIN_VALUE, max: UByte = UByte.MAX_VALUE): Exhaustive<UByte> =17 Exhaustive.ints(min.toInt()..max.toInt()).map { it.toUByte() }18fun Exhaustive.Companion.ushorts(min: UShort = UShort.MIN_VALUE, max: UShort = UShort.MAX_VALUE): Exhaustive<UShort> =19 Exhaustive.ints(min.toInt()..max.toInt()).map { it.toUShort() }20fun Exhaustive.Companion.byteArrays(length: IntRange, byte: Gen<Byte> = Arb.byte()): Exhaustive<ByteArray> {21 val generator = byte.generate(RandomSource.Default).iterator()22 return length.map { ByteArray(it) { generator.next().value } }.exhaustive()23}24operator fun <A> Exhaustive<A>.minus(other: Exhaustive<A>) =25 filter { it !in other.values }26inline fun <reified T> Exhaustive.Companion.arrayOf(value: Arb<T>, length: IntRange): Exhaustive<Array<T>> {27 return length.map { Array(it) { value.next() } }.exhaustive()28}...
ints.kt
Source:ints.kt
1package io.kotest.property.exhaustive2import io.kotest.property.Exhaustive3fun Exhaustive.Companion.ints(range: IntRange): Exhaustive<Int> = range.toList().exhaustive()4fun Exhaustive.Companion.longs(range: LongRange): Exhaustive<Long> = range.toList().exhaustive()...
Exhaustive.Companion.ints
Using AI Code Generation
1val intList = io.kotest.property.exhaustive.ints(1, 10).take(10)2val intList = io.kotest.property.exhaustive.ints(1, 10).take(10)3val intList = io.kotest.property.exhaustive.ints(1, 10).take(10)4val intList = io.kotest.property.exhaustive.ints(1, 10).take(10)5val intList = io.kotest.property.exhaustive.ints(1, 10).take(10)6val intList = io.kotest.property.exhaustive.ints(1, 10).take(10)7val intList = io.kotest.property.exhaustive.ints(1, 10).take(10)8val intList = io.kotest.property.exhaustive.ints(1, 10).take(10)9val intList = io.kotest.property.exhaustive.ints(1, 10).take(10)10val intList = io.kotest.property.exhaustive.ints(1, 10).take(10)11val intList = io.kotest.property.exhaustive.ints(1, 10).take(10)
Exhaustive.Companion.ints
Using AI Code Generation
1val ints = Exhaustive.ints(1, 3)2ints.iterator().forEach { println(it) }3val ints = Exhaustive.ints(1, 3)4ints.iterator().forEach { println(it) }5val ints = Exhaustive.ints(1, 3)6ints.iterator().forEach { println(it) }7val ints = Exhaustive.ints(1, 3)8ints.iterator().forEach { println(it) }9val ints = Exhaustive.ints(1, 3)10ints.iterator().forEach { println(it) }11val ints = Exhaustive.ints(1, 3)12ints.iterator().forEach { println(it) }13val ints = Exhaustive.ints(1, 3)14ints.iterator().forEach { println(it) }15val ints = Exhaustive.ints(1, 3)16ints.iterator().forEach { println(it) }17val ints = Exhaustive.ints(1, 3)18ints.iterator().forEach { println(it) }19val ints = Exhaustive.ints(1, 3
Exhaustive.Companion.ints
Using AI Code Generation
1 val ints = Exhaustive.ints(1..10)2 val ints = Exhaustive.ints(1, 2, 3, 4, 5)3 val ints = Exhaustive.ints(1..10, 11..20)4 val ints = Exhaustive.ints(1..10, 11..20, 21..30)5 val ints = Exhaustive.ints(1..10, 11..20, 21..30, 31..40)6 val ints = Exhaustive.ints(1..10, 11..20, 21..30, 31..40, 41..50)7 val ints = Exhaustive.ints(1..10, 11..20, 21..30, 31..40, 41..50, 51..60)8 val ints = Exhaustive.ints(1..10, 11..20, 21..30, 31..40, 41..50, 51..60, 61..70)
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!