All Downloads are FREE. Search and download functionalities are using the official Maven repository.

run.qontract.core.pattern.BooleanPattern.kt Maven / Gradle / Ivy

Go to download

A Contract Testing Tool that leverages Gherkin to describe APIs in a human readable and machine enforceable manner

There is a newer version: 0.23.1
Show newest version
package run.qontract.core.pattern

import run.qontract.core.Resolver
import run.qontract.core.Result
import run.qontract.core.mismatchResult
import run.qontract.core.resultReport
import run.qontract.core.value.BooleanValue
import run.qontract.core.value.JSONArrayValue
import run.qontract.core.value.Value
import java.util.*

object BooleanPattern : Pattern, ScalarType {
    override fun matches(sampleData: Value?, resolver: Resolver): Result =
        when(sampleData) {
            is BooleanValue -> Result.Success()
            else -> mismatchResult("boolean", sampleData)
        }

    override fun generate(resolver: Resolver): Value =
        when(Random().nextInt(2)) {
            0 -> BooleanValue(false)
            else -> BooleanValue(true)
        }

    override fun newBasedOn(row: Row, resolver: Resolver): List = listOf(this)
    override fun parse(value: String, resolver: Resolver): Value = when {
        value !in (listOf("true", "false")) -> throw ContractException(resultReport(mismatchResult(BooleanPattern, value)))
        else -> BooleanValue(value.toBoolean())
    }
    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?
        get() = null

    override val typeName: String = "boolean"
    override val pattern: Any = "(boolean)"
    override fun toString(): String = pattern.toString()
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy