Best Kotest code snippet using io.kotest.property.arbitrary.edgecases.Arb.removeEdgecases
maps.kt
Source:maps.kt
1package io.kotest.property.arbitrary2import io.kotest.property.Arb3import io.kotest.property.Shrinker4/**5 * Returns an [Arb] where each generated value is a map, with the entries of the map6 * drawn from the given pair generating arb. The size of each7 * generated map is a random value between the specified min and max bounds.8 *9 * There are no edge cases.10 *11 * This arbitrary uses a [Shrinker] which will reduce the size of a failing map by12 * removing elements from the failed case until it is empty.13 *14 * @see MapShrinker15 *16 * @param arb the arbitrary to populate the map entries17 * @param minSize the desired minimum size of the generated map18 * @param maxSize the desired maximum size of the generated map19 * @param slippage when generating keys, we may have repeats if the underlying gen is random.20 * The slippage factor determines how many times we continue after retrieving a duplicate key.21 * The total acceptable number of misses is the slippage factor multiplied by the target set size.22 * If this value is not specified, then the default slippage value of 10 will be used.23 */24fun <K, V> Arb.Companion.map(25 arb: Arb<Pair<K, V>>,26 minSize: Int = 1,27 maxSize: Int = 100,28 slippage: Int = 1029): Arb<Map<K, V>> = arbitrary(MapShrinker(minSize)) { random ->30 val targetSize = random.random.nextInt(minSize, maxSize)31 val maxMisses = targetSize * slippage32 val map = mutableMapOf<K, V>()33 var iterations = 034 while (iterations < maxMisses && map.size < targetSize) {35 val initialSize = map.size36 val (key, value) = arb.single(random)37 map[key] = value38 if (map.size == initialSize) iterations++39 }40 require(map.size >= minSize) {41 "the minimum size requirement of $minSize could not be satisfied after $iterations consecutive samples"42 }43 map44}45/**46 * Returns an [Arb] where each generated value is a map, with the entries of the map47 * drawn by combining values from the key gen and value gen. The size of each48 * generated map is a random value between the specified min and max bounds.49 *50 * There are no edge cases.51 *52 * This arbitrary uses a [Shrinker] which will reduce the size of a failing map by53 * removing elements until they map is empty.54 *55 * @see MapShrinker56 *57 * @param keyArb the arbitrary to populate the keys58 * @param valueArb the arbitrary to populate the values59 * @param minSize the desired minimum size of the generated map60 * @param maxSize the desired maximum size of the generated map61 * @param slippage when generating keys, we may have repeats if the underlying gen is random.62 * The slippage factor determines how many times we continue after retrieving a duplicate key.63 * The total acceptable number of misses is the slippage factor multiplied by the target set size.64 * If this value is not specified, then the default slippage value of 10 will be used.65 */66fun <K, V> Arb.Companion.map(67 keyArb: Arb<K>,68 valueArb: Arb<V>,69 minSize: Int = 1,70 maxSize: Int = 100,71 slippage: Int = 1072): Arb<Map<K, V>> {73 require(minSize >= 0) { "minSize must be positive" }74 require(maxSize >= 0) { "maxSize must be positive" }75 return arbitrary(MapShrinker(minSize)) { random ->76 val targetSize = random.random.nextInt(minSize, maxSize)77 val maxMisses = targetSize * slippage78 val map = mutableMapOf<K, V>()79 var iterations = 080 while (iterations < maxMisses && map.size < targetSize) {81 val initialSize = map.size82 map[keyArb.single(random)] = valueArb.single(random)83 if (map.size == initialSize) iterations++84 }85 require(map.size >= minSize) {86 "the minimum size requirement of $minSize could not be satisfied after $iterations consecutive samples"87 }88 map89 }90}91class MapShrinker<K, V>(private val minSize: Int) : Shrinker<Map<K, V>> {92 override fun shrink(value: Map<K, V>): List<Map<K, V>> {93 val shrinks = when (value.size) {94 0 -> emptyList()95 1 -> listOf(emptyMap())96 else -> listOf(97 value.toList().take(value.size / 2).toMap(),98 value.toList().drop(1).toMap()99 )100 }101 return shrinks.filter { it.size >= minSize }102 }103}104/**105 * Returns an [Arb] that produces Pairs of K,V using the supplied arbs for K and V.106 * Edgecases will be derived from [k] and [v].107 */108fun <K, V> Arb.Companion.pair(k: Arb<K>, v: Arb<V>): Arb<Pair<K, V>> {109 val arbPairWithoutKeyEdges: Arb<Pair<K, V>> = Arb.bind(k.removeEdgecases(), v, ::Pair)110 val arbPairWithoutValueEdges: Arb<Pair<K, V>> = Arb.bind(k, v.removeEdgecases(), ::Pair)111 val arbPair: Arb<Pair<K, V>> = Arb.bind(k, v, ::Pair)112 return Arb.choice(arbPair, arbPairWithoutKeyEdges, arbPairWithoutValueEdges)113}...
edgecases.kt
Source:edgecases.kt
1package io.kotest.property.arbitrary2import io.kotest.property.Arb3import io.kotest.property.EdgeConfig4import io.kotest.property.RandomSource5import io.kotest.property.Sample6/**7 * Randomly chooses an [Arb] and then generates an edge case from that [Arb].8 * If the chosen arb has no edge cases, then another arb will be chosen.9 * If all [Arb]s have no edge cases, then returns null.10 */11tailrec fun <A> List<Arb<A>>.edgecase(rs: RandomSource): A? {12 if (this.isEmpty()) return null13 val shuffled = this.shuffled(rs.random)14 return when (val edge = shuffled.first().edgecase(rs)) {15 null -> this.drop(1).edgecase(rs)16 else -> edge17 }18}19/**20 * Collects the edge cases from this arb.21 * Will stop after the given number of iterations.22 * This function is mainly used for testing.23 */24fun <A> Arb<A>.edgecases(iterations: Int = 100, rs: RandomSource = RandomSource.default()): Set<A> =25 generate(rs, EdgeConfig(edgecasesGenerationProbability = 1.0))26 .take(iterations)27 .map { it.value }28 .toSet()29/**30 * Returns a new [Arb] with the supplied edge cases replacing any existing edge cases.31 */32fun <A> Arb<A>.withEdgecases(edgecases: List<A>): Arb<A> = arbitrary(edgecases) { this@withEdgecases.next(it) }33/**34 * Returns a new [Arb] with the supplied edge cases replacing any existing edge cases.35 */36fun <A> Arb<A>.withEdgecases(vararg edgecases: A): Arb<A> = this.withEdgecases(edgecases.toList())37fun <A> Arb<A>.removeEdgecases(): Arb<A> = this.withEdgecases(emptyList())38/**39 * Returns a new [Arb] with the edge cases from this arb transformed by the given function [f].40 */41fun <A> Arb<A>.modifyEdgecases(f: (A) -> A?): Arb<A> = object : Arb<A>() {42 override fun edgecase(rs: RandomSource): A? = this@modifyEdgecases.edgecase(rs)?.let(f)43 override fun sample(rs: RandomSource): Sample<A> = this@modifyEdgecases.sample(rs)44}...
Arb.removeEdgecases
Using AI Code Generation
1val arb = Arb.int(0..100).removeEdgecases { it == 0 }2val arb = Arb.int(0..100).removeEdgecases { it == 0 }3val arb = Arb.int(0..100).removeEdgecases { it == 0 }4val arb = Arb.int(0..100).removeEdgecases { it == 0 }5val arb = Arb.int(0..100).removeEdgecases { it == 0 }6val arb = Arb.int(0..100).removeEdgecases { it == 0 }7val arb = Arb.int(0..100).removeEdgecases { it == 0 }8val arb = Arb.int(0..100).removeEdgecases { it == 0 }9val arb = Arb.int(0..100).removeEdgecases { it == 0 }10val arb = Arb.int(0..100).removeEdgecases { it == 0 }11val arb = Arb.int(0..100).removeEdgecases { it == 0 }12val arb = Arb.int(0..100).removeEdgecases { it == 0 }13val arb = Arb.int(0..100).removeEdgecases { it == 0
Arb.removeEdgecases
Using AI Code Generation
1Arb.removeEdgecases ( Arb . string (), listOf ( "foo" , "bar" ))2Arb . edgecases ( Arb . string (), listOf ( "foo" , "bar" ))3Arb . edgecases ( Arb . string (), listOf ( "foo" , "bar" ))4Arb . edgecases ( Arb . string (), listOf ( "foo" , "bar" ))5Arb . edgecases ( Arb . string (), listOf ( "foo" , "bar" ))6Arb . edgecases ( Arb . string (), listOf ( "foo" , "bar" ))7Arb . edgecases ( Arb . string (), listOf ( "foo" , "bar" ))8Arb . edgecases ( Arb . string (), listOf ( "foo" , "bar" ))9Arb . edgecases ( Arb . string (), listOf ( "foo" , "bar" ))10Arb . edgecases ( Arb . string (), listOf ( "foo" , "bar" ))11Arb . edgecases ( Arb . string (), listOf ( "foo" , "bar" ))12Arb . edgecases ( Arb . string (), listOf ( "foo" , "bar" ))13Arb . edgecases ( Arb . string (), listOf ( "foo" , "bar
Arb.removeEdgecases
Using AI Code Generation
1Arb.int(0..100).removeEdgecases(0, 1)2Arb.int(0..100).edgecases(0, 1)3Arb.int(0..100).edgecases(0, 1)4Arb.int(0..100).edgecases(0, 1)5Arb.int(0..100).edgecases(0, 1)6Arb.int(0..100).edgecases(0, 1)7Arb.int(0..100).edgecases(0, 1)
Arb.removeEdgecases
Using AI Code Generation
1val arb = Arb . int () . removeEdgecases ( listOf ( 1 , 2 , 3 , 4 , 5 ))2val arb = Arb . int () . removeEdgecases ( listOf ( 1 , 2 , 3 , 4 , 5 ))3val arb = Arb . int () . removeEdgecases ( listOf ( 1 , 2 , 3 , 4 , 5 ))4val arb = Arb . int () . removeEdgecases ( listOf ( 1 , 2 , 3 , 4 , 5 ))5val arb = Arb . int () . removeEdgecases ( listOf ( 1 , 2 , 3 , 4 , 5 ))6val arb = Arb . int () . removeEdgecases ( listOf ( 1 , 2 , 3 , 4 , 5 ))7val arb = Arb . int () . removeEdgecases ( listOf ( 1 , 2 , 3 , 4 , 5 ))8val arb = Arb . int () . removeEdgecases ( listOf ( 1 , 2 , 3 , 4 , 5 ))9val arb = Arb . int () . removeEdgecases ( listOf ( 1 , 2 , 3 , 4 ,
Arb.removeEdgecases
Using AI Code Generation
1val arb: Arb<String> = Arb.string(10..20, CharRange('a', 'z'))2val arbWithEdgeCases: Arb<String> = arb.removeEdgeCases(setOf("abc", "xyz", "123"))3val arb: Arb<String> = Arb.string(10..20, CharRange('a', 'z'))4val arbWithEdgeCases: Arb<String> = arb.removeEdgeCases(setOf("abc", "xyz", "123"))5Kotest also provides a set of edge cases for some of the built-in arbitraries. For example, the following code shows how to use the edge cases for the Arb.string() method:6val arb: Arb<String> = Arb.string(10..20, CharRange('a', 'z'))7val arbWithEdgeCases: Arb<String> = arb.removeEdgeCases(Arb.stringEdgeCases())8The following code shows how to use the edge cases for the Arb.int() method:9val arb: Arb<Int> = Arb.int(10..20)10val arbWithEdgeCases: Arb<Int> = arb.removeEdgeCases(Arb.intEdgeCases())11The following code shows how to use the edge cases for the Arb.long() method:12val arb: Arb<Long> = Arb.long(10..20)13val arbWithEdgeCases: Arb<Long> = arb.removeEdgeCases(Arb.longEdgeCases())14The following code shows how to use the edge cases for the Arb.double() method:15val arb: Arb<Double> = Arb.double(10.0..20.0)16val arbWithEdgeCases: Arb<Double> = arb.removeEdgeCases(Arb.doubleEdgeCases())17The following code shows how to use the edge cases for the Arb.float() method:18val arb: Arb<Float> = Arb.float(10.0f..20.0f)19val arbWithEdgeCases: Arb<Float> = arb.removeEdgeCases(Arb.floatEdgeCases())20The following code shows how to use the edge cases for the Arb.char() method:21val arb: Arb<Char> = Arb.char(CharRange('a', 'z'))22val arbWithEdgeCases: Arb<Char> = arb.removeEdgeCases(Arb.charEdgeCases())23The following code shows how to use the edge cases for the Arb.byte() method:24val arb: Arb<Byte> = Arb.byte()
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!!