How to use Arb.Companion.string method of io.kotest.property.arbitrary.StringShrinkerWithMin class

Best Kotest code snippet using io.kotest.property.arbitrary.StringShrinkerWithMin.Arb.Companion.string

strings.kt

Source:strings.kt Github

copy

Full Screen

1package io.kotest.property.arbitrary2import io.kotest.property.Arb3import io.kotest.property.Shrinker4import io.kotest.property.arbitrary.strings.StringClassifier5import kotlin.random.nextInt6/**7 * Returns an [Arb] where each random value is a String of length between minSize and maxSize.8 * By default, the arb uses a [ascii] codepoint generator, but this can be substituted9 * with any codepoint generator. There are many available, such as [katakana] and so on.10 *11 * The edge case values are a string of min length, using the first12 * edge case codepoint provided by the codepoints arb. If the min length is 0 and maxSize > 0, then13 * the edge cases will include a string of length 1 as well.14 */15fun Arb.Companion.string(16 minSize: Int = 0,17 maxSize: Int = 100,18 codepoints: Arb<Codepoint> = Codepoint.ascii()19): Arb<String> {20 return ArbitraryBuilder.create { rs ->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.40 *41 * The edge case values are a string of the first value in the range, using the first edge case42 * codepoint provided by the codepoints arb.43 */44fun Arb.Companion.string(range: IntRange, codepoints: Arb<Codepoint> = Codepoint.ascii()): Arb<String> =45 Arb.string(range.first, range.last, codepoints)46/**47 * Returns an [Arb] where each random value is a String of length [size].48 * By default the arb uses a [ascii] codepoint generator, but this can be substituted49 * with any codepoint generator. There are many available, such as [katakana] and so on.50 *51 * There are no edge case values associated with this arb.52 */53fun Arb.Companion.string(size: Int, codepoints: Arb<Codepoint> = Codepoint.ascii()): Arb<String> =54 Arb.string(size, size, codepoints)55@Deprecated("This Shrinker does not take into account string lengths. Use StringShrinkerWithMin. This was deprecated in 4.5.")56object StringShrinker : Shrinker<String> {57 override fun shrink(value: String): List<String> {58 return when {59 value == "" -> emptyList()60 value == "a" -> listOf("")61 value.length == 1 -> listOf("", "a")62 else -> {63 val firstHalf = value.take(value.length / 2 + value.length % 2)64 val secondHalf = value.takeLast(value.length / 2)65 val secondHalfAs = firstHalf.padEnd(value.length, 'a')66 val firstHalfAs = secondHalf.padStart(value.length, 'a')67 val dropFirstChar = value.drop(1)68 val dropLastChar = value.dropLast(1)69 listOf(70 firstHalf,71 firstHalfAs,72 secondHalf,73 secondHalfAs,74 dropFirstChar,75 dropLastChar76 )77 }78 }79 }80}81/**82 * Shrinks a string. Shrunk variants will be shorter and simplified.83 *84 * Shorter strings will be at least [minLength] in length.85 *86 * Simplified strings will have characters replaced by a character (selected by [simplestCharSelector])87 * of each pre-shrunk value. By default, this is the first character of the pre-shrunk string.88 *89 * When [simplestCharSelector] returns null, no simpler variants will be created.90 */91class StringShrinkerWithMin(92 private val minLength: Int = 0,93 private val simplestCharSelector: (preShrinkValue: String) -> Char? = CharSequence::firstOrNull94) : Shrinker<String> {95// @Deprecated("a static 'simplestChar' means invalid shrinks can be generated - use the alternative constructor instead, which allows for a dynamic 'simplestChar'")96// constructor (97// minLength: Int = 0,98// simplestChar: Char,99// ) : this(minLength, { simplestChar })100 override fun shrink(value: String): List<String> {101 val simplestChar: Char? = simplestCharSelector(value)102 val isShortest = value.length == minLength103 val isSimplest = value.all { it == simplestChar }104 return buildList {105 if (!isShortest) {106 addAll(shorterVariants(value))107 }108 if (!isSimplest && simplestChar != null) {109 addAll(simplerVariants(value, simplestChar))110 }111 }.mapNotNull {112 // ensure the variants are at least minLength long113 when {114 simplestChar != null -> it.padEnd(minLength, simplestChar)115 it.length >= minLength -> it116 else -> null // this string is too short, so filter it out117 }118 }.distinct()119 }120 private fun simplerVariants(value: String, simplestChar: Char): List<String> =121 listOfNotNull(122 // replace the first and last chars that aren't simplestChar with simplestChar123 replace(value, simplestChar, value.indexOfFirst { it != simplestChar }),124 replace(value, simplestChar, value.indexOfLast { it != simplestChar }),125 )126 private fun shorterVariants(value: String) =127 listOf(128 value.take(value.length / 2 + value.length % 2),129 value.takeLast(value.length / 2),130 value.drop(1),131 value.dropLast(1),132 )133 private fun replace(value: String, newChar: Char, index: Int) =134 if (index == -1) null else value.replaceRange(index..index, newChar.toString())135}...

Full Screen

Full Screen

Arb.Companion.string

Using AI Code Generation

copy

Full Screen

1import io.kotest.core.spec.style.FunSpec2import io.kotest.matchers.shouldBe3import io.kotest.property.Arb4import io.kotest.property.arbitrary.string5import io.kotest.property.checkAll6class StringShrinkerWithMinUnitTest : FunSpec({7 test("should shrink the input") {8 checkAll(Arb.Companion.string(0, 4)) {9 }10 }11})

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Kotest automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Most used method in StringShrinkerWithMin

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful