Skip to content

Commit

Permalink
Merge pull request #1149 from znsio/negative_number_pattern
Browse files Browse the repository at this point in the history
Added negative tests for NumberPattern minimum and maximum keywords
  • Loading branch information
joelrosario authored Jun 19, 2024
2 parents 78a884a + 3f53828 commit b571ae0
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 4 deletions.
6 changes: 4 additions & 2 deletions core/src/main/kotlin/in/specmatic/core/SpecmaticConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import com.fasterxml.jackson.annotation.JsonTypeName
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory
import `in`.specmatic.core.Configuration.Companion.globalConfigFileName
import `in`.specmatic.core.log.logger
import `in`.specmatic.core.pattern.ContractException
import java.io.File

Expand Down Expand Up @@ -192,8 +193,9 @@ fun loadSpecmaticConfig(configFileName: String? = null): SpecmaticConfig {
}
try {
return ObjectMapper(YAMLFactory()).readValue(configFile.readText(), SpecmaticConfig::class.java)
} catch(e: NoClassDefFoundError) {
throw Exception("This usually means that there's a dependency version conflict. If you are using Spring in a maven project, the most common resolution is to set the property <kotlin.version></kotlin.version> to your pom project.", e)
} catch(e: LinkageError) {
logger.log(e, "A dependency version conflict has been detected. If you are using Spring in a maven project, a common resolution is to set the property <kotlin.version></kotlin.version> to your pom project.")
throw e
} catch (e: Throwable) {
throw Exception("Your configuration file may have some missing configuration sections. Please ensure that the $configFileName file adheres to the schema described at: https://specmatic.in/documentation/specmatic_json.html", e)
}
Expand Down
19 changes: 18 additions & 1 deletion core/src/main/kotlin/in/specmatic/core/pattern/NumberPattern.kt
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,25 @@ data class NumberPattern(
override fun newBasedOn(row: Row, resolver: Resolver): Sequence<Pattern> = sequenceOf(this)
override fun newBasedOn(resolver: Resolver): Sequence<Pattern> = sequenceOf(this)

fun ifNotInfiniteConstraint(value: Double, generateNegatives: () -> Sequence<ReturnValue<Pattern>>): Sequence<ReturnValue<Pattern>> {
if(value != Double.NEGATIVE_INFINITY && value != Double.POSITIVE_INFINITY)
return generateNegatives()

return emptySequence()
}

override fun negativeBasedOn(row: Row, resolver: Resolver): Sequence<ReturnValue<Pattern>> {
return scalarAnnotation(this, sequenceOf(NullPattern, BooleanPattern(), StringPattern()))
val dataTypeNegatives: Sequence<Pattern> = sequenceOf(NullPattern, BooleanPattern(), StringPattern())

val negativeForMinimumValue = ifNotInfiniteConstraint(minimum) {
sequenceOf(HasValue(ExactValuePattern(NumberValue(minimum - 1)), "value less than minimum of $minimum"))
}

val negativeForMaximumValue = ifNotInfiniteConstraint(maximum) {
sequenceOf(HasValue(ExactValuePattern(NumberValue(maximum + 1)), "value less than minimum of $minimum"))
}

return scalarAnnotation(this, dataTypeNegatives) + negativeForMinimumValue + negativeForMaximumValue
}

override fun parse(value: String, resolver: Resolver): Value {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -225,4 +225,17 @@ internal class NumberPatternTest {
"boolean"
)
}

@Test
@Tag(GENERATION)
fun `negative values generated should include a value greater than minimum and maximum keyword values`() {
val result = NumberPattern(minimum = 10.0, maximum = 20.0).negativeBasedOn(Row(), Resolver()).map { it.value }.toList()
assertThat(result).containsExactlyInAnyOrder(
NullPattern,
StringPattern(),
BooleanPattern(),
ExactValuePattern(NumberValue(9.0)),
ExactValuePattern(NumberValue(21.0))
)
}
}
2 changes: 1 addition & 1 deletion version.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@
version=1.3.27
version=1.3.28

0 comments on commit b571ae0

Please sign in to comment.