run.qontract.core.pattern.EmptyStringPattern.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.*
object EmptyStringPattern : Pattern {
override fun matches(sampleData: Value?, resolver: Resolver): Result {
return when (sampleData) {
EmptyString -> Result.Success()
else -> mismatchResult("empty string", sampleData)
}
}
override fun generate(resolver: Resolver): Value = StringValue("")
override fun newBasedOn(row: Row, resolver: Resolver): List = listOf(this)
override fun parse(value: String, resolver: Resolver): Value {
return when {
value.isEmpty() -> EmptyString
else -> throw ContractException("""No data was expected, but got "$value" instead""")
}
}
override fun encompasses(otherPattern: Pattern, thisResolver: Resolver, otherResolver: Resolver, typeStack: TypeStack): Result {
if(otherPattern is EmptyStringPattern) return Result.Success()
return Result.Failure("No data was expected, but got \"${otherPattern.typeName}\" instead")
}
override fun listOf(valueList: List, resolver: Resolver): Value {
return JSONArrayValue(valueList)
}
override val typeAlias: String?
get() = null
override val typeName: String = "nothing"
override val pattern: Any = ""
override fun toString(): String = "(nothing)"
}