run.qontract.core.pattern.PatternInStringPattern.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of qontract-core Show documentation
Show all versions of qontract-core Show documentation
A Contract Testing Tool that leverages Gherkin to describe APIs in a human readable and machine enforceable manner
package run.qontract.core.pattern
import run.qontract.core.Resolver
import run.qontract.core.Result
import run.qontract.core.mismatchResult
import run.qontract.core.value.StringValue
import run.qontract.core.value.Value
data class PatternInStringPattern(override val pattern: Pattern = StringPattern, override val typeAlias: String? = null): Pattern {
override fun matches(sampleData: Value?, resolver: Resolver): Result {
if(sampleData !is StringValue)
return mismatchResult(pattern, sampleData)
val value = try {
pattern.parse(sampleData.string, resolver)
} catch(e: Throwable) {
return Result.Failure("Could not parse ${sampleData.displayableValue()} to ${pattern.typeName}")
}
return pattern.matches(value, resolver)
}
override fun generate(resolver: Resolver): Value = StringValue(pattern.generate(resolver).toStringValue())
override fun newBasedOn(row: Row, resolver: Resolver): List =
pattern.newBasedOn(row, resolver).map { PatternInStringPattern(it) }
override fun parse(value: String, resolver: Resolver): Value = StringValue(pattern.parse(value, resolver).toStringValue())
override fun patternSet(resolver: Resolver): List =
pattern.patternSet(resolver).map { PatternInStringPattern(it) }
override fun encompasses(otherPattern: Pattern, thisResolver: Resolver, otherResolver: Resolver, typeStack: TypeStack): Result =
when (otherPattern) {
is ExactValuePattern -> otherPattern.fitsWithin(patternSet(thisResolver), otherResolver, thisResolver, typeStack)
is PatternInStringPattern -> pattern.encompasses(otherPattern.pattern, otherResolver, thisResolver, typeStack)
else -> Result.Failure("Expected type in string type, got ${otherPattern.typeName}")
}
override fun listOf(valueList: List, resolver: Resolver): Value {
return pattern.listOf(valueList, resolver)
}
override val typeName: String = "${pattern.typeName} in string"
}