io.specmatic.core.pattern.DeferredPattern.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.pattern
import io.specmatic.core.Dictionary
import io.specmatic.core.Resolver
import io.specmatic.core.Result
import io.specmatic.core.Substitution
import io.specmatic.core.pattern.config.NegativePatternConfiguration
import io.specmatic.core.value.EmptyString
import io.specmatic.core.value.Value
data class DeferredPattern(override val pattern: String, val key: String? = null) : Pattern {
override fun fillInTheBlanks(value: Value, dictionary: Dictionary, resolver: Resolver): ReturnValue {
return resolvePattern(resolver).fillInTheBlanks(value, dictionary, resolver)
}
override fun equals(other: Any?): Boolean = when(other) {
is DeferredPattern -> other.pattern == pattern
else -> false
}
override fun hashCode(): Int = pattern.hashCode()
override fun matches(sampleData: Value?, resolver: Resolver) =
resolver.matchesPattern(key, resolver.getPattern(pattern), sampleData ?: EmptyString)
override fun generate(resolver: Resolver): Value {
val resolvedPattern = resolvePattern(resolver)
return resolver.withCyclePrevention(resolvedPattern) { cyclePreventedResolver ->
when (key) {
null -> resolvedPattern.generate(cyclePreventedResolver)
else -> cyclePreventedResolver.generate(key, resolvedPattern)
}
}
}
override fun newBasedOn(resolver: Resolver): Sequence {
val resolvedPattern = resolvePattern(resolver)
return resolver.withCyclePrevention(resolvedPattern) { cyclePreventedResolver ->
resolvedPattern.newBasedOn(cyclePreventedResolver)
}
}
override fun newBasedOn(row: Row, resolver: Resolver): Sequence> {
val resolvedPattern = resolvePattern(resolver)
return resolver.withCyclePrevention(resolvedPattern) { cyclePreventedResolver ->
resolvedPattern.newBasedOn(row, cyclePreventedResolver)
}
}
override fun negativeBasedOn(row: Row, resolver: Resolver, config: NegativePatternConfiguration): Sequence> {
return resolver.getPattern(pattern).negativeBasedOn(row, resolver)
}
override fun encompasses(otherPattern: Pattern, thisResolver: Resolver, otherResolver: Resolver, typeStack: TypeStack): Result {
return thisResolver.getPattern(pattern).encompasses(resolvedHop(otherPattern, otherResolver), thisResolver, otherResolver, typeStack)
}
override fun listOf(valueList: List, resolver: Resolver): Value {
return resolver.getPattern(pattern).listOf(valueList, resolver)
}
override fun resolveSubstitutions(
substitution: Substitution,
value: Value,
resolver: Resolver,
key: String?
): ReturnValue {
return resolvePattern(resolver).resolveSubstitutions(substitution, value, resolver, key)
}
override fun getTemplateTypes(key: String, value: Value, resolver: Resolver): ReturnValue