run.qontract.core.pattern.NumberPattern.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.JSONArrayValue
import run.qontract.core.value.NumberValue
import run.qontract.core.value.Value
import java.util.*
object NumberPattern : Pattern, ScalarType {
override fun matches(sampleData: Value?, resolver: Resolver): Result {
return when(sampleData is NumberValue) {
true -> Result.Success()
false -> mismatchResult("number", sampleData)
}
}
override fun generate(resolver: Resolver): Value = NumberValue(Random().nextInt(1000))
override fun newBasedOn(row: Row, resolver: Resolver): List = listOf(this)
override fun parse(value: String, resolver: Resolver): Value {
return NumberValue(convertToNumber(value))
}
override fun encompasses(otherPattern: Pattern, thisResolver: Resolver, otherResolver: Resolver, typeStack: TypeStack): Result {
return encompasses(this, otherPattern, thisResolver, otherResolver, typeStack)
}
override fun listOf(valueList: List, resolver: Resolver): Value {
return JSONArrayValue(valueList)
}
override val typeAlias: String? = null
override val typeName: String = "number"
override val pattern: Any = "(number)"
override fun toString(): String = pattern.toString()
}
fun encompasses(thisPattern: Pattern, otherPattern: Pattern, thisResolver: Resolver, otherResolver: Resolver, typeStack: TypeStack): Result =
when {
otherPattern::class == thisPattern::class -> Result.Success()
otherPattern is ExactValuePattern -> otherPattern.fitsWithin(thisPattern.patternSet(thisResolver), otherResolver, thisResolver, typeStack)
else -> mismatchResult(thisPattern, otherPattern)
}