How to use shrink method of io.kotest.property.arbitrary.StringShrinkerWithMin class

Best Kotest code snippet using io.kotest.property.arbitrary.StringShrinkerWithMin.shrink

StringShrinkerWithMinTest.kt

Source:StringShrinkerWithMinTest.kt Github

copy

Full Screen

1package com.sksamuel.kotest.property.shrinking2import io.kotest.assertions.shouldFail3import io.kotest.assertions.withClue4import io.kotest.core.spec.style.DescribeSpec5import io.kotest.extensions.system.captureStandardOut6import io.kotest.inspectors.forAll7import io.kotest.inspectors.forAtLeastOne8import io.kotest.matchers.collections.shouldBeIn9import io.kotest.matchers.collections.shouldContain10import io.kotest.matchers.comparables.shouldBeLessThan11import io.kotest.matchers.shouldBe12import io.kotest.matchers.string.shouldContain13import io.kotest.matchers.string.shouldContainOnlyDigits14import io.kotest.matchers.string.shouldHaveLength15import io.kotest.matchers.string.shouldMatch16import io.kotest.matchers.string.shouldNotContain17import io.kotest.property.Arb18import io.kotest.property.PropTestConfig19import io.kotest.property.PropertyTesting20import 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 >= 2222 a.shouldHaveLength(1)223 }224 }225 }226 stdout.shouldContain(227 """228Attempting to shrink arg "a b c d e f g h i j k"229Shrink #1: "a b c d e f" fail230Shrink #2: "a b c ffff" fail231Shrink #3: "f b c ffff" fail232Shrink #4: "ffb c ffff" fail233Shrink #5: "fff c ffff" fail234Shrink #6: "ffffc ffff" fail235Shrink #7: "fffff ffff" fail236Shrink #8: "ffffffffff" fail237Shrink result (after 8 shrinks) => "ffffffffff"238 """.trim()239 )240 PropertyTesting.shouldPrintShrinkSteps = prt241 }242 }243})...

Full Screen

Full Screen

ExpressionTest.kt

Source:ExpressionTest.kt Github

copy

Full Screen

...140 is Function -> "Function"141 is Identifier -> "Identifier"142 }143 },144 shrinker = ExpressionShrinker(boundNames)145 ) {146 if (maxDepth <= 1) {147 identifierArb(boundNames).bind()148 }149 else {150 when (val case = it.random.nextInt(3)) {151 0 -> identifierArb(boundNames).bind()152 1 -> functionArb(boundNames, maxDepth).bind()153 2 -> applicationArb(boundNames, maxDepth).bind()154 else -> throw IllegalStateException("Invalid case $case, should have been one of {0, 1, 2} (seed ${it.seed})")155 }156 }157 }158}159fun identifierArb(boundNames: List<String>): Arb<Identifier> {160 return arbitrary(IdentifierShrinker()) {161 if (boundNames.isNotEmpty() && Arb.boolean().bind()) {162 val name = Arb.element(boundNames).bind()163 Identifier(name)164 }165 else {166 val name = parameterNameArb(boundNames).bind()167 Identifier(name)168 }169 }170}171val functionArb = functionArb(emptyList(), 10)172fun functionArb(boundNames: List<String>, maxDepth: Int): Arb<Function> {173 return arbitrary(174 shrinker = FunctionShrinker(boundNames)175 ) {176 val identifier = identifierArb(boundNames).bind()177 val body = expressionArb(boundNames + identifier.name, maxDepth - 1).bind()178 Function(identifier.name, body)179 }180}181fun applicationArb(boundNames: List<String>, maxDepth: Int): Arb<Application> {182 return arbitrary {183 Application(184 expressionArb(boundNames, maxDepth - 1).bind(),185 expressionArb(boundNames, maxDepth - 1).bind(),186 )187 }188}189fun parameterNameArb(excludedNames: List<String>): Arb<String> {190 return arbitrary {191 it.random.azstring(it.random.nextInt(1, 10))192 }.filterNot {193 it in excludedNames194 }195}196class ExpressionShrinker(private val boundNames: List<String>) : Shrinker<Expression> {197 override fun shrink(value: Expression): List<Expression> {198 return when (value) {199 is Application -> shrinkApplication(value)200 is Function -> shrinkFunction(value)201 is Identifier -> IdentifierShrinker().shrink(value)202 }203 }204 private fun shrinkApplication(application: Application): List<Expression> {205 val shrunkApplication = ApplicationShrinker(boundNames).shrink(application)206 return shrunkApplication + application.argument + application.function207 }208 private fun shrinkFunction(function: Function): List<Expression> {209 val shrunkFunction = FunctionShrinker(boundNames).shrink(function)210 return shrunkFunction211 }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> {257 override fun shrink(value: Application): List<Application> {258 val shrunkFunction = ExpressionShrinker(boundNames)259 .shrink(value.function)260 .map { Application(it, value.argument) }261 val shrunkArgument = ExpressionShrinker(boundNames)262 .shrink(value.function)263 .map { Application(value.function, it) }264 return shrunkFunction + shrunkArgument265 }266}267private fun String.words(): Set<String> {268 return split(" ")269 .filter(String::isNotEmpty)270 .toSet()271}...

Full Screen

Full Screen

strings.kt

Source:strings.kt Github

copy

Full Screen

...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)...

Full Screen

Full Screen

shrink

Using AI Code Generation

copy

Full Screen

1val shrinker = StringShrinkerWithMin(5)2shrinker.shrink("12345") shouldBe empty()3shrinker.shrink("123456") shouldBe listOf("12345")4shrinker.shrink("1234567") shouldBe listOf("123456")5shrinker.shrink("12345678") shouldBe listOf("1234567")6shrinker.shrink("123456789") shouldBe listOf("12345678")7shrinker.shrink("1234567890") shouldBe listOf("123456789")8shrinker.shrink("12345678901") shouldBe listOf("1234567890")9shrinker.shrink("123456789012") shouldBe listOf("12345678901")10shrinker.shrink("1234567890123") shouldBe listOf("123456789012")11shrinker.shrink("12345678901234") shouldBe listOf("1234567890123")12shrinker.shrink("123456789012345") shouldBe listOf("12345678901234")13shrinker.shrink("1234567890123456") shouldBe listOf("123456789012345")14val shrinker = StringShrinkerWithMin(5)15shrinker.shrink("12345") shouldBe empty()16shrinker.shrink("123456") shouldBe listOf("12345")17shrinker.shrink("1234567") shouldBe listOf("123456")18shrinker.shrink("12345678") shouldBe listOf("1234567")19shrinker.shrink("123456789") shouldBe listOf("12345678")20shrinker.shrink("1234567890") shouldBe listOf("123456789")21shrinker.shrink("12345678901") shouldBe listOf("1234567890")22shrinker.shrink("123456789012") shouldBe listOf("12345678901")23shrinker.shrink("1234567890123") shouldBe listOf("123456789012")24shrinker.shrink("12345678901234") shouldBe listOf("1234567890123")25shrinker.shrink("123456789012345") shouldBe listOf("12345678901234")26shrinker.shrink("1234567890123456") shouldBe listOf("123456789012345")

Full Screen

Full Screen

shrink

Using AI Code Generation

copy

Full Screen

1val shrinker = StringShrinkerWithMin(10)2shrinker.shrink("abcdefghij").toList() shouldBe listOf("abcdefghi", "abcdefgh", "abcdefg", "abcdef", "abcde", "abcd", "abc", "ab", "a")3val shrinker = StringShrinkerWithMin(10)4shrinker.shrink("abcdefghij").toList() shouldBe listOf("abcdefghi", "abcdefgh", "abcdefg", "abcdef", "abcde", "abcd", "abc", "ab", "a")5val shrinker = StringShrinkerWithMin(10)6shrinker.shrink("abcdefghij").toList() shouldBe listOf("abcdefghi", "abcdefgh", "abcdefg", "abcdef", "abcde", "abcd", "abc", "ab", "a")7val shrinker = StringShrinkerWithMin(10)8shrinker.shrink("abcdefghij").toList() shouldBe listOf("abcdefghi", "abcdefgh", "abcdefg", "abcdef", "abcde", "abcd", "abc", "ab", "a")9val shrinker = StringShrinkerWithMin(10)10shrinker.shrink("abcdefghij").toList() shouldBe listOf("abcdefghi", "abcdefgh", "abcdefg", "abcdef", "abcde", "abcd", "abc", "ab", "a")11val shrinker = StringShrinkerWithMin(10)12shrinker.shrink("abcdefghij").toList() shouldBe listOf("abcdefghi", "abcdefgh", "abcdefg", "abcdef", "abcde", "abcd", "abc", "ab", "a")13val shrinker = StringShrinkerWithMin(10)14shrinker.shrink("abcdefghij").toList() shouldBe listOf("abcdefghi",

Full Screen

Full Screen

shrink

Using AI Code Generation

copy

Full Screen

1val shrinker = StringShrinkerWithMin(10)2shrinker.shrink("1234567890")3val shrinker = StringShrinkerWithMin(10)4shrinker.shrink("1234567890")5val shrinker = StringShrinkerWithMin(10)6shrinker.shrink("1234567890")7val shrinker = StringShrinkerWithMin(10)8shrinker.shrink("1234567890")9val shrinker = StringShrinkerWithMin(10)10shrinker.shrink("1234567890")11val shrinker = StringShrinkerWithMin(10)12shrinker.shrink("1234567890")13val shrinker = StringShrinkerWithMin(10)14shrinker.shrink("1234567890")15val shrinker = StringShrinkerWithMin(10)16shrinker.shrink("1234567890")17val shrinker = StringShrinkerWithMin(10)18shrinker.shrink("1234567890")19val shrinker = StringShrinkerWithMin(10)20shrinker.shrink("1234567890")21val shrinker = StringShrinkerWithMin(10)22shrinker.shrink("1234567890")

Full Screen

Full Screen

shrink

Using AI Code Generation

copy

Full Screen

1 val arb = StringShrinkerWithMin(minLength = 5, maxShrinks = 5).shrinker(arb)2 forAll(arb, 100) { s ->3 }4 val arb = StringShrinkerWithMin(minLength = 5, maxShrinks = 5)5 forAll(arb, 100) { s ->6 }7 val arb = StringShrinkerWithMin(minLength = 5, maxShrinks = 5)8 forAll(arb, 100) { s ->9 }10 val arb = StringShrinkerWithMin(minLength = 5, maxShrinks = 5)11 forAll(arb, 100) { s ->12 }

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