io.specmatic.core.GenerativeTestsEnabled.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of specmatic-core Show documentation
Show all versions of specmatic-core Show documentation
Turn your contracts into executable specifications. Contract Driven Development - Collaboratively Design & Independently Deploy MicroServices & MicroFrontends.
package io.specmatic.core
import io.specmatic.core.pattern.*
import io.specmatic.core.pattern.isOptional
import io.specmatic.core.pattern.withoutOptionality
import io.specmatic.core.value.Value
data class GenerativeTestsEnabled(private val positiveOnly: Boolean) : GenerationStrategies {
override fun generatedPatternsForGenerativeTests(
resolver: Resolver,
pattern: Pattern,
key: String
): Sequence> {
// TODO generate value outside
return resolver.withCyclePrevention(pattern, isOptional(key)) { cyclePreventedResolver ->
pattern.newBasedOn(Row(), cyclePreventedResolver)
} ?: emptySequence()
}
override fun generateHttpRequestBodies(
resolver: Resolver,
body: Pattern,
row: Row,
requestBodyAsIs: Pattern,
value: Value
): Sequence> {
// TODO generate value outside
val requestsFromFlattenedRow: Sequence> =
resolver.withCyclePrevention(body) { cyclePreventedResolver ->
body.newBasedOn(row.noteRequestBody(), cyclePreventedResolver)
}
var matchFound = false
val iterator = requestsFromFlattenedRow.iterator()
return sequence {
while(iterator.hasNext()) {
val next = iterator.next()
next.withDefault(false) {
if(it.encompasses(requestBodyAsIs, resolver, resolver, emptySet()) is Result.Success)
matchFound = true
}
yield(next)
}
if(!matchFound)
yield(HasValue(requestBodyAsIs))
}
}
override fun generateHttpRequestBodies(
resolver: Resolver,
body: Pattern,
row: Row
): Sequence> {
// TODO generate value outside
val vanilla: Sequence> = resolver.withCyclePrevention(body) { cyclePreventedResolver ->
body.newBasedOn(Row(), cyclePreventedResolver)
}
val fromExamples: Sequence> = resolver.withCyclePrevention(body) { cyclePreventedResolver ->
body.newBasedOn(row, cyclePreventedResolver)
}
val remainingVanilla: Sequence> = vanilla.filterNot { vanillaTypeR ->
fromExamples.any { typeFromExamplesR ->
vanillaTypeR.withDefault(false, typeFromExamplesR) { vanillaType, typeFromExamples ->
vanillaType.encompasses(
typeFromExamples,
resolver,
resolver
).isSuccess()
}
}
}
return fromExamples.plus(remainingVanilla)
}
override fun resolveRow(row: Row): Row {
return Row()
}
override fun generateKeySubLists(key: String, subList: List): Sequence> {
return if(isOptional(key)) {
sequenceOf(subList, subList + key)
} else
sequenceOf(subList + key)
}
override fun positiveTestScenarios(feature: Feature, suggestions: List): Sequence>> {
return feature.positiveTestScenarios(suggestions)
}
override fun negativeTestScenarios(feature: Feature): Sequence>> {
return if(positiveOnly)
emptySequence()
else
feature.negativeTestScenarios()
}
override fun fillInTheMissingMapPatterns(
newQueryParamsList: Sequence