Skip to content

Commit

Permalink
Min Length Fix Tested
Browse files Browse the repository at this point in the history
  • Loading branch information
Samy authored and Samy committed Nov 16, 2024
1 parent 278d865 commit f0661ca
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 11 deletions.
13 changes: 6 additions & 7 deletions core/src/main/kotlin/io/specmatic/core/pattern/StringPattern.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)

Expand All @@ -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<ReturnValue<Pattern>> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
}

Expand Down

0 comments on commit f0661ca

Please sign in to comment.