Best Kotest code snippet using io.kotest.property.arbitrary.filter.edgecase
dates.kt
Source:dates.kt
...131 minLocalDateTime: LocalDateTime = LocalDateTime.of(1970, 1, 1, 0, 0),132 maxLocalDateTime: LocalDateTime = LocalDateTime.of(2030, 12, 31, 23, 59)133): Arb<LocalDateTime> {134 return arbitrary(135 edgecaseFn = {136 generateSequence {137 val date = localDate(minLocalDateTime.toLocalDate(), maxLocalDateTime.toLocalDate()).edgecase(it)138 val time = localTime().edgecase(it)139 if (date == null || time == null) null else date.atTime(time)140 }.find { !it.isBefore(minLocalDateTime) && !it.isAfter(maxLocalDateTime) }141 },142 sampleFn = {143 generateSequence {144 val date = localDate(minLocalDateTime.toLocalDate(), maxLocalDateTime.toLocalDate()).single(it)145 val time = localTime().single(it)146 date.atTime(time)147 }.first { !it.isBefore(minLocalDateTime) && !it.isAfter(maxLocalDateTime) }148 }149 )150}151/**152 * Arberates a stream of random YearMonth...
collections.kt
Source:collections.kt
...77fun <A> Arb.Companion.list(gen: Gen<A>, range: IntRange = 0..100): Arb<List<A>> {78 check(!range.isEmpty())79 check(range.first >= 0)80 return arbitrary(81 edgecaseFn = { rs ->82 val emptyList = emptyList<A>()83 val singleList: List<A>? = when (gen) {84 is Arb -> (gen.edgecase(rs) ?: gen.next(rs))?.let { listOf(it) }85 is Exhaustive -> gen.values.firstOrNull()?.let { listOf(it) }86 }87 val repeatedList: List<A>? = when {88 range.last < 2 -> null // too small for repeats89 gen is Arb -> (gen.edgecase(rs) ?: gen.next(rs))?.let { a -> List(max(2, range.first)) { a } }90 gen is Exhaustive -> gen.values.firstOrNull()?.let { a -> List(max(2, range.first)) { a } }91 else -> null92 }93 listOfNotNull(emptyList, singleList, repeatedList).filter { it.size in range }.distinct().random(rs.random)94 },95 shrinker = ListShrinker(range),96 sampleFn = { rs ->97 val targetSize = rs.random.nextInt(range)98 gen.generate(rs).take(targetSize).toList().map { it.value }99 }100 )101}102/**103 * Returns an [Arb] whose of values are a list of values generated by the current arb....
strings.kt
Source:strings.kt
...21 val size = rs.random.nextInt(minSize..maxSize)22 codepoints.take(size, rs).joinToString("") { it.asString() }23 }.withEdgecaseFn { rs ->24 if (minSize == maxSize) null else {25 val lowCodePoint = codepoints.edgecase(rs)26 val min = lowCodePoint?.let { cp -> List(minSize) { cp.asString() }.joinToString("") }27 val minPlus1 = lowCodePoint?.let { cp -> List(minSize + 1) { cp.asString() }.joinToString("") }28 val edgeCases = listOfNotNull(min, minPlus1)29 .filter { it.length in minSize..maxSize }30 if (edgeCases.isEmpty()) null else edgeCases.random(rs.random)31 }32 }.withShrinker(StringShrinkerWithMin(minSize))33 .withClassifier(StringClassifier(minSize, maxSize))34 .build()35}36/**37 * Returns an [Arb] where each random value is a String which has a length in the given range.38 * By default the arb uses a [ascii] codepoint generator, but this can be substituted39 * with any codepoint generator. There are many available, such as [katakana] and so on....
FilterTest.kt
Source:FilterTest.kt
...25 }26 test("should filter edge cases") {27 val arb = Arb.int(1..10).withEdgecases(1, 2, 3).filter { it % 2 == 0 }28 val edgeCases = arb29 .generate(RandomSource.seeded(1234L), EdgeConfig(edgecasesGenerationProbability = 1.0))30 .take(5)31 .map { it.value }32 .toList()33 edgeCases shouldContainExactly listOf(2, 2, 2, 2, 2)34 }35 test("should be stack safe") {36 val arb = object : Arb<Int>() {37 override fun edgecase(rs: RandomSource): Int? = null38 override fun sample(rs: RandomSource): Sample<Int> = Sample(rs.random.nextInt())39 }40 shouldNotThrow<StackOverflowError> {41 arb.filter { it % 2 == 0 }.take(1000000).toList()42 }43 }44 test("should apply filter to shrinks") {45 val arbEvenInts = Arb.int(-100..100).filter { it % 2 == 0 }46 val oddNumbers = (-100..100).filter { it % 2 != 0 }47 arbEvenInts.samples().take(100).forAll { sample ->48 sample.shrinks.value() shouldNotBeIn oddNumbers49 sample.shrinks.children.value.forAll {50 it.value() shouldNotBeIn oddNumbers51 }...
BigDecimalTest.kt
Source:BigDecimalTest.kt
...8import io.kotest.matchers.shouldBe9import io.kotest.property.Arb10import io.kotest.property.arbitrary.bigDecimal11import io.kotest.property.arbitrary.bigDecimalDefaultEdgecases12import io.kotest.property.arbitrary.edgecases13import io.kotest.property.arbitrary.take14import java.math.BigDecimal15import java.math.RoundingMode16import java.util.concurrent.TimeUnit17class BigDecimalTest : FunSpec({18 test("Arb.bigDecimal(min, max) should generate bigDecimal between given range") {19 val min = BigDecimal.valueOf(123)20 val max = BigDecimal.valueOf(555)21 Arb.bigDecimal(min, max).take(100).forAll {22 (it >= min && it <= max) shouldBe true23 }24 }25 test("Arb.bigDecimal(scale, rounding) should generate bigDecimal of given scale") {26 Arb.bigDecimal(4, RoundingMode.CEILING).take(100).forAll {27 it.scale() shouldBe 428 }29 }30 test("Arb.bigDecimal(min, max) for large value should complete with in few seconds") {31 shouldCompleteWithin(5, TimeUnit.SECONDS) {32 Arb.bigDecimal(BigDecimal.valueOf(-100_000.00), BigDecimal.valueOf(100_000.00)).take(100).forEach { _ ->33 }34 }35 }36 test("bigDecimalDefaultEdgecases should contain zeros with differing precision") {37 bigDecimalDefaultEdgecases.shouldContain(BigDecimal("0.00"))38 bigDecimalDefaultEdgecases.shouldContain(BigDecimal("0"))39 }40 test("Arb.bigDecimal(min, max) should always contain min as edgecase but not max") {41 val min = BigDecimal.valueOf(123)42 val max = BigDecimal.valueOf(555)43 val actualEdgecases = Arb.bigDecimal(min = min, max = max).edgecases()44 actualEdgecases.shouldContain(min)45 actualEdgecases.shouldNotContain(max)46 }47 test("Arb.bigDecimal(min, max) should only include default edgecases that are in range [min, max)") {48 val min = BigDecimal.valueOf(0)49 val max = BigDecimal.valueOf(5)50 val expectedEdgecases = bigDecimalDefaultEdgecases51 .filter { min <= it && it < max }52 Arb.bigDecimal(min = min, max = max).edgecases().shouldContainAll(expectedEdgecases)53 }54})...
filter.kt
Source:filter.kt
...11 * the predicate.12 */13fun <A> Arb<A>.filter(predicate: (A) -> Boolean): Arb<A> = trampoline { sampleA ->14 object : Arb<A>() {15 override fun edgecase(rs: RandomSource): A? =16 sequenceOf(sampleA.value)17 .plus(generateSequence { this@filter.edgecase(rs) })18 .take(PropertyTesting.maxFilterAttempts)19 .filter(predicate)20 .firstOrNull()21 override fun sample(rs: RandomSource): Sample<A> {22 val sample = sequenceOf(sampleA).plus(this@filter.samples(rs)).filter { predicate(it.value) }.first()23 return Sample(sample.value, sample.shrinks.filter(predicate) ?: RTree({ sample.value }))24 }25 }26}27/**28 * @return a new [Arb] by filtering this arbs output by the negated function [f]29 */30fun <A> Arb<A>.filterNot(f: (A) -> Boolean): Arb<A> = filter { !f(it) }31/**...
distinct.kt
Source:distinct.kt
...5import io.kotest.property.Sample6@DelicateKotest7fun <A> Arb<A>.distinct(attempts: Int = 100) = object : Arb<A>() {8 private val seen = mutableSetOf<A>()9 override fun edgecase(rs: RandomSource): A? = this@distinct.edgecase(rs)10 override fun sample(rs: RandomSource): Sample<A> {11 var iterations = 012 return generateSequence {13 if (iterations++ < attempts) this@distinct.sample(rs) else null14 }.filter { seen.add(it.value) }.first()15 }16}
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!!