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

in.specmatic.core.pattern.HasFailure.kt Maven / Gradle / Ivy

Go to download

Turn your contracts into executable specifications. Contract Driven Development - Collaboratively Design & Independently Deploy MicroServices & MicroFrontends. Deprecation Notice for group ID "in.specmatic" ****************************************************************************************************** Updates for "specmatic-core" will no longer be available under the deprecated group ID "in.specmatic". Please update your dependencies to use the new group ID "io.specmatic". ******************************************************************************************************

The newest version!
package `in`.specmatic.core.pattern

import `in`.specmatic.core.Result

data class HasFailure(val failure: Result.Failure, val message: String = "") : ReturnValue, ReturnFailure {
    override fun  withDefault(default: U, fn: (T) -> U): U {
        return default
    }

    override fun  ifValue(fn: (T) -> U): ReturnValue {
        return HasFailure(failure)
    }

    override fun update(fn: (T) -> T): ReturnValue {
        return this
    }

    override fun  assimilate(valueResult: ReturnValue, fn: (T, U) -> T): ReturnValue {
        return cast()
    }

    override fun  combine(valueResult: ReturnValue, fn: (T, U) -> V): ReturnValue {
        return cast()
    }

    override fun  cast(): ReturnValue {
        return HasFailure(failure, message)
    }

    override val value: T
        get() = throw ContractException(failure.toFailureReport())

    override fun  ifHasValue(fn: (HasValue) -> ReturnValue): ReturnValue {
        return cast()
    }

    fun toFailure(): Result.Failure {
        return Result.Failure(message, failure)
    }

    override fun addDetails(message: String, breadCrumb: String): ReturnValue {
        return HasFailure(Result.Failure(message, this.toFailure(), breadCrumb))
    }

    override fun  realise(hasValue: (T, String?) -> U, orFailure: (HasFailure) -> U, orException: (HasException) -> U): U {
        return orFailure(this)
    }
}