diff --git a/core/src/main/kotlin/io/specmatic/core/pattern/StringPattern.kt b/core/src/main/kotlin/io/specmatic/core/pattern/StringPattern.kt index ba51c002f..d1e27a556 100644 --- a/core/src/main/kotlin/io/specmatic/core/pattern/StringPattern.kt +++ b/core/src/main/kotlin/io/specmatic/core/pattern/StringPattern.kt @@ -68,11 +68,10 @@ data class StringPattern ( } //Tells us the minimum length to be used for random string - private val randomStringLength: Int = + private val patternMinLength: Int = when { - minLength != null && 5 < minLength -> minLength - maxLength != null && 5 > maxLength -> maxLength - else -> 5 + minLength != null && minLength > 0 -> minLength + else -> 1 } override fun generate(resolver: Resolver): Value { @@ -82,12 +81,12 @@ data class StringPattern ( if (maxLength != null) return StringValue( Generex(regex.removePrefix("^").removeSuffix("$")).random( - randomStringLength, + patternMinLength, maxLength ) ) - return StringValue(Generex(regex.removePrefix("^").removeSuffix("$")).random(randomStringLength)) + return StringValue(Generex(regex.removePrefix("^").removeSuffix("$")).random(patternMinLength)) } val defaultExampleMatchResult = matches(defaultExample, resolver) @@ -104,7 +103,7 @@ data class StringPattern ( return defaultExample } - return StringValue(randomString(randomStringLength)) + return StringValue(randomString(patternMinLength)) } override fun newBasedOn(row: Row, resolver: Resolver): Sequence> { diff --git a/core/src/test/kotlin/io/specmatic/core/pattern/StringPatternTest.kt b/core/src/test/kotlin/io/specmatic/core/pattern/StringPatternTest.kt index 286580106..4799ccd31 100644 --- a/core/src/test/kotlin/io/specmatic/core/pattern/StringPatternTest.kt +++ b/core/src/test/kotlin/io/specmatic/core/pattern/StringPatternTest.kt @@ -37,7 +37,7 @@ internal class StringPatternTest { @Test fun `should generate 5 character long random string when min and max length are not specified`() { - assertThat(StringPattern().generate(Resolver()).toStringLiteral().length).isEqualTo(5) + assertThat(StringPattern().generate(Resolver()).toStringLiteral().length).isEqualTo(1) } @Test @@ -93,15 +93,15 @@ internal class StringPatternTest { fun `generate string value of appropriate length matching minLength and maxLength parameters`(min: Int?, max: Int?, length: Int) { val result = StringPattern(minLength = min, maxLength = max).generate(Resolver()) as StringValue val generatedLength = result.string.length - val randomStringDefaultLength = 5; + val minPatternLength = min ?: 1; // If max is provided, ensure the generated length is within the range of min and max if (max != null) { // Ensure that the generated string length is between min (or 0 if min is null) and max - assertThat(generatedLength).isGreaterThanOrEqualTo(min ?: 0) + assertThat(generatedLength).isGreaterThanOrEqualTo(minPatternLength) assertThat(generatedLength).isLessThanOrEqualTo(max) } else { // If max is not provided, ensure the generated length is at least the min (or randomStringDefaultLength if min is null) - assertThat(generatedLength).isGreaterThanOrEqualTo(min ?: randomStringDefaultLength) + assertThat(generatedLength).isGreaterThanOrEqualTo(minPatternLength) } }