Best Kotest code snippet using io.kotest.property.arbitrary.StringShrinkerWithMin
StringShrinkerWithMinTest.kt
Source:StringShrinkerWithMinTest.kt
...20import io.kotest.property.RTree21import io.kotest.property.ShrinkingMode22import io.kotest.property.arbitrary.ArbitraryBuilder23import io.kotest.property.arbitrary.Codepoint24import io.kotest.property.arbitrary.StringShrinkerWithMin25import io.kotest.property.arbitrary.arbitrary26import io.kotest.property.arbitrary.ascii27import io.kotest.property.arbitrary.az28import io.kotest.property.arbitrary.char29import io.kotest.property.arbitrary.of30import io.kotest.property.arbitrary.string31import io.kotest.property.checkAll32import io.kotest.property.internal.doShrinking33import io.kotest.property.rtree34class StringShrinkerWithMinTest : DescribeSpec({35 beforeSpec {36 PropertyTesting.shouldPrintShrinkSteps = false37 }38 afterSpec {39 PropertyTesting.shouldPrintShrinkSteps = true40 }41 describe("StringShrinkerWithMin") {42 it("should include bisected input") {43 checkAll { a: String ->44 if (a.length > 1) {45 val candidates = StringShrinkerWithMin().shrink(a)46 candidates.forAtLeastOne {47 it.shouldHaveLength(a.length / 2 + a.length % 2)48 }49 candidates.forAtLeastOne {50 it.shouldHaveLength(a.length / 2)51 }52 }53 }54 }55 it("should use first char to replace the second char") {56 StringShrinkerWithMin(4).shrink("atttt") shouldContain "aattt"57 }58 it("should replace last char with simplest") {59 StringShrinkerWithMin(4).shrink("atttt") shouldContain "attta"60 }61 it("should drop first char") {62 StringShrinkerWithMin(4).shrink("abcde") shouldContain "bcde"63 }64 it("should drop last char") {65 StringShrinkerWithMin(4).shrink("abcde") shouldContain "abcd"66 }67 it("should include first half variant") {68 StringShrinkerWithMin(1).shrink("abcdef") shouldContain "abc"69 StringShrinkerWithMin(1).shrink("abcde") shouldContain "abc"70 }71 it("should include second half variant") {72 StringShrinkerWithMin(1).shrink("abcdef") shouldContain "def"73 StringShrinkerWithMin(1).shrink("abcd") shouldContain "cd"74 }75 it("should shrink to expected value") {76 val prt = PropertyTesting.shouldPrintShrinkSteps77 PropertyTesting.shouldPrintShrinkSteps = false78 checkAll<String> { a ->79 val shrinks = StringShrinkerWithMin().rtree(a)80 val shrunk = doShrinking(shrinks, ShrinkingMode.Unbounded) {81 it.shouldNotContain("#")82 }83 if (a.contains("#")) {84 shrunk.shrink shouldBe "#"85 } else {86 shrunk.shrink shouldBe a87 }88 }89 PropertyTesting.shouldPrintShrinkSteps = prt90 }91 it("should prefer padded values") {92 val prt = PropertyTesting.shouldPrintShrinkSteps93 PropertyTesting.shouldPrintShrinkSteps = false94 val a = "97asd!@#ASD'''234)*safmasd"95 val shrinks = StringShrinkerWithMin().rtree(a)96 doShrinking(shrinks, ShrinkingMode.Unbounded) {97 it.length.shouldBeLessThan(3)98 }.shrink shouldBe "777"99 doShrinking(shrinks, ShrinkingMode.Unbounded) {100 it.length.shouldBeLessThan(8)101 }.shrink shouldBe "!!!!!!!!"102 PropertyTesting.shouldPrintShrinkSteps = prt103 }104 it("should respect min value") {105 val prt = PropertyTesting.shouldPrintShrinkSteps106 PropertyTesting.shouldPrintShrinkSteps = true107 val stdout = captureStandardOut {108 shouldFail {109 checkAll(PropTestConfig(seed = 123125), Arb.string(4, 8)) { a ->110 // will cause the value to fail and shrinks be used, but nothing should be shrunk111 // past the min value of 4, even though we fail on anything >= 2112 a.shouldHaveLength(1)113 }114 }115 }116 stdout.shouldContain(117 """118Attempting to shrink arg "`a,ONF/b"119Shrink #1: "`a,O" fail120Shrink #2: "``,O" fail121Shrink #3: "```O" fail122Shrink #4: "````" fail123Shrink result (after 4 shrinks) => "````"124 """.trim()125 )126 PropertyTesting.shouldPrintShrinkSteps = prt127 }128 it("should generate samples that only contain numbers, given a numeric-codepoints Arb") {129 val numericChars = '0'..'9'130 val arbNumericCodepoints = Arb.of(numericChars.map { Codepoint(it.code) })131 val arbNumericString = Arb.string(1..10, arbNumericCodepoints)132 checkAll(arbNumericString) { numericString ->133 StringShrinkerWithMin()134 .shrink(numericString)135 .forAll { it.shouldContainOnlyDigits() }136 }137 }138 it("should only generate codepoint-compatible samples, when an Arb.string() is created") {139 val arbCharacters: Set<Char> = """ `!"£$%^&*()_+=-[]{}:@~;'#<>?,./ """.trim().toSet()140 val arbNumericCodepoints = Arb.of(arbCharacters.map { Codepoint(it.code) })141 val stringArb = Arb.string(minSize = 4, maxSize = 10, codepoints = arbNumericCodepoints)142 val arbSamples = arbitrary { rs -> stringArb.sample(rs) }143 checkAll(arbSamples) { sample ->144 withClue("all samples should only contain chars used to create the Arb.string(): $arbCharacters") {145 sample.value.toList().forAll { it.shouldBeIn(arbCharacters) }146 sample.shrinks.children.value.forAll { child: RTree<String> ->147 child.value().toList().forAll { it.shouldBeIn(arbCharacters) }148 }149 }150 }151 }152 it("should generate simpler variants using simplestCharSelector") {153 val azStringArb = Arb.string(minSize = 10, maxSize = 10, Codepoint.az())154 val digitsArb = Arb.char('0'..'9')155 checkAll(azStringArb, digitsArb) { preShrinkString, digitChar ->156 val stringShrinker = StringShrinkerWithMin(minLength = 10) { digitChar }157 stringShrinker.shrink(preShrinkString).forAll { shrunkString ->158 shrunkString shouldHaveLength 10159 shrunkString shouldMatch Regex("[a-zA-Z$digitChar]{10}")160 }161 }162 }163 it("should not generate simpler variants when simplestCharSelector returns null") {164 val stringArb = Arb.string(minSize = 1, maxSize = 10, Codepoint.ascii())165 val stringShrinker = StringShrinkerWithMin(minLength = 1) { null }166 checkAll(stringArb) { preShrinkString ->167 stringShrinker.shrink(preShrinkString).forAll { shrunkString ->168 shrunkString shouldHaveLength shrunkString.length169 }170 }171 }172 it("should generate simpler variants using custom simplestCharSelector") {173 fun String.middleChar(): Char? = getOrNull(length / 2)174 val stringShrinker = StringShrinkerWithMin(minLength = 10) { it.middleChar() }175 val stringArbWithShrinker = ArbitraryBuilder.create {176 // '#' is the middle char - expect the '_' chars to be replaced by '#'177 "_____#_____"178 }.withShrinker(stringShrinker)179 .build()180 val prt = PropertyTesting.shouldPrintShrinkSteps181 PropertyTesting.shouldPrintShrinkSteps = true182 val stdout = captureStandardOut {183 shouldFail {184 checkAll(PropTestConfig(seed = 123125), stringArbWithShrinker) { a ->185 // will cause the value to fail and shrinks be used, but nothing should be shrunk186 // past the min value of 4, even though we fail on anything >= 2187 a.shouldHaveLength(1)188 }189 }190 }191 stdout.shouldContain(192 """193Attempting to shrink arg "_____#_____"194Shrink #1: "_____#####" fail195Shrink #2: "#____#####" fail196Shrink #3: "##___#####" fail197Shrink #4: "###__#####" fail198Shrink #5: "####_#####" fail199Shrink #6: "##########" fail200Shrink result (after 6 shrinks) => "##########"201 """.trim()202 )203 PropertyTesting.shouldPrintShrinkSteps = prt204 }205 it("should generate simpler variants using dynamic simplestCharSelector") {206 // dynamically select the simplest char - it should be the char after the last space207 val stringShrinker = StringShrinkerWithMin(minLength = 10) {208 val lastSpaceCharIndex = it.indexOfLast { c -> c == ' ' }209 it.getOrNull(lastSpaceCharIndex + 1)210 }211 val stringArbWithShrinker = ArbitraryBuilder.create {212 "a b c d e f g h i j k"213 }.withShrinker(stringShrinker)214 .build()215 val prt = PropertyTesting.shouldPrintShrinkSteps216 PropertyTesting.shouldPrintShrinkSteps = true217 val stdout = captureStandardOut {218 shouldFail {219 checkAll(PropTestConfig(seed = 123125), stringArbWithShrinker) { a ->220 // will cause the value to fail and shrinks be used, but nothing should be shrunk221 // past the min value of 4, even though we fail on anything >= 2...
ExpressionTest.kt
Source:ExpressionTest.kt
...212}213class IdentifierShrinker :214 Shrinker<Identifier> {215 override fun shrink(value: Identifier): List<Identifier> {216 return StringShrinkerWithMin(1)217 .shrink(value.name)218 .map {219 Identifier(it)220 }221 }222}223class FunctionShrinker(private val boundNames: List<String>) : Shrinker<Function> {224 override fun shrink(value: Function): List<Function> {225 return shrinkParameterName(value) + shrinkBody(value) + removeIntermediate(value) +226 Function(227 value.parameterName,228 Identifier(value.parameterName)229 )230 }231 private fun removeIntermediate(function: Function): List<Function> {232 val (parameterName, body) = function233 if (body !is Function) {234 return emptyList()235 }236 if (parameterName in body.freeVariables) {237 return emptyList()238 }239 return listOf(body)240 }241 private fun shrinkParameterName(function: Function): List<Function> {242 return StringShrinkerWithMin(1)243 .shrink(function.parameterName)244 .map { newParameterName ->245 Function(246 newParameterName,247 function.body.substitute(function.parameterName, Identifier(newParameterName)),248 )249 }250 }251 private fun shrinkBody(function: Function): List<Function> {252 return ExpressionShrinker(boundNames + function.parameterName).shrink(function.body)253 .map { Function(function.parameterName, it) }254 }255}256class ApplicationShrinker(private val boundNames: List<String>) : Shrinker<Application> {...
strings.kt
Source:strings.kt
...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) {...
StringShrinkerWithMin
Using AI Code Generation
1 import io.kotest.property.arbitrary.StringShrinkerWithMin2 import io.kotest.property.arbitrary.StringShrinkerWithMin3 import io.kotest.property.arbitrary.StringShrinkerWithMin4 import io.kotest.property.arbitrary.StringShrinkerWithMin5 import io.kotest.property.arbitrary.StringShrinkerWithMin6 import io.kotest.property.arbitrary.StringShrinkerWithMin7 import io.kotest.property.arbitrary.StringShrinkerWithMin8 import io.kotest.property.arbitrary.StringShrinkerWithMin9 import io.kotest.property.arbitrary.StringShrinkerWithMin10 import io.kotest.property.arbitrary.StringShrinkerWithMin11 import io.kotest.property.arbitrary.StringShrinkerWithMin12 import io.kotest.property.arbitrary.StringShrinkerWithMin13 import io.kotest.property.arbitrary.StringShrinkerWithMin14 import io.k
StringShrinkerWithMin
Using AI Code Generation
1val shrinker = StringShrinkerWithMin(5, 100)2val config = PropertyTestingConfig(shrinker = shrinker)3val shrinker = StringShrinkerWithMin(5, 100)4val config = PropertyTestingConfig(shrinker = shrinker)5val shrinker = StringShrinkerWithMin(5, 100)6val config = PropertyTestingConfig(shrinker = shrinker)7val shrinker = StringShrinkerWithMin(5, 100)8val config = PropertyTestingConfig(shrinker = shrinker)9val shrinker = StringShrinkerWithMin(5, 100)10val config = PropertyTestingConfig(shrinker = shrinker)11val shrinker = StringShrinkerWithMin(5, 100)12val config = PropertyTestingConfig(shrinker = shrinker)13val shrinker = StringShrinkerWithMin(5, 100)14val config = PropertyTestingConfig(shrinker = shrinker)15val shrinker = StringShrinkerWithMin(5, 100)16val config = PropertyTestingConfig(shrinker = shrinker)17val shrinker = StringShrinkerWithMin(5, 100)18val config = PropertyTestingConfig(shrinker = shrinker)19val shrinker = StringShrinkerWithMin(5, 100)20val config = PropertyTestingConfig(shrinker = shrinker)
StringShrinkerWithMin
Using AI Code Generation
1 forAll(arb, config) { s ->2 }3}4fun main() {5 test()6}
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!!