Skip to content

Commit

Permalink
Pattern Validation - Max Length Issue Fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Samy authored and Samy committed Nov 14, 2024
1 parent 899be3d commit 278d865
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 6 deletions.
16 changes: 12 additions & 4 deletions core/src/main/kotlin/io/specmatic/core/pattern/StringPattern.kt
Original file line number Diff line number Diff line change
Expand Up @@ -67,20 +67,28 @@ data class StringPattern (
return JSONArrayValue(valueList)
}

//Tells us the minimum length to be used for random string
private val randomStringLength: Int =
when {
minLength != null && 5 < minLength -> minLength
maxLength != null && 5 > maxLength -> maxLength
else -> 5
}

override fun generate(resolver: Resolver): Value {
val defaultExample: Value? = resolver.resolveExample(example, this)

if (regex != null) {
if(defaultExample == null)
return StringValue(Generex(regex.removePrefix("^").removeSuffix("$")).random(randomStringLength))
if(defaultExample == null) {
if (maxLength != null)
return StringValue(
Generex(regex.removePrefix("^").removeSuffix("$")).random(
randomStringLength,
maxLength
)
)

return StringValue(Generex(regex.removePrefix("^").removeSuffix("$")).random(randomStringLength))
}
val defaultExampleMatchResult = matches(defaultExample, resolver)

if(defaultExampleMatchResult.isSuccess())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,20 @@ internal class StringPatternTest {
@MethodSource("lengthTestValues")
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

assertThat(result.string.length).isEqualTo(length)
val generatedLength = result.string.length
val randomStringDefaultLength = 5;
// 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).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)
}
}


@Test
fun `string should encompass enum of string`() {
val result: Result = StringPattern().encompasses(
Expand Down Expand Up @@ -240,4 +250,12 @@ internal class StringPatternTest {
fun `string pattern encompasses email`() {
assertThat(StringPattern().encompasses(EmailPattern(), Resolver(), Resolver())).isInstanceOf(Result.Success::class.java)
}

@Test
fun `should fail to generate string when maxLength is less than minLength`() {
val exception = assertThrows<IllegalArgumentException> {
StringPattern(minLength = 6, maxLength = 4)
}
assertThat(exception.message).isEqualTo("maxLength cannot be less than minLength")
}
}

0 comments on commit 278d865

Please sign in to comment.