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

net.serenitybdd.screenplay.ensure.PerformableExpectations.kt Maven / Gradle / Ivy

package net.serenitybdd.screenplay.ensure

import net.serenitybdd.screenplay.Actor
import net.serenitybdd.screenplay.Performable
import net.thucydides.core.annotations.Step

open class PerformableExpectation(private val actual: A?,
                                   private val expectation: Expectation,
                                   private val expected: E?,
                                   private val isNegated: Boolean = false,
                                   private val expectedDescription: String = "a value") : Performable {

    private val description = expectation.describe(expected, isNegated, expectedDescription);

    @Step("{0} should see #description")
    override fun  performAs(actor: T) {
        BlackBox.reset()
        val result = expectation.apply(actual, expected!!, actor)

        if (isAFailure(result, isNegated)) {
            throw AssertionError(expectation.compareActualWithExpected(actual, expected, isNegated, expectedDescription))
        }
    }

    /**
     * Internal use only - DO NOT USE
     */
    protected constructor() : this(null,
            Expectation(
                    "placeholder",
                    "placeholder",
                    fun(_: Actor?, _: A?, _: E): Boolean = true
            ),
            null) {
    }
}

open class BiPerformableExpectation(private val actual: A?,
                                     private val expectation: DoubleValueExpectation,
                                     private val startRange: E?,
                                     private val endRange: E?,
                                     private val isNegated: Boolean = false,
                                     private val expectedDescription: String) : Performable {

    private val description = expectation.describeRange(startRange, endRange, isNegated, expectedDescription);

    @Step("{0} should see #description")
    override fun  performAs(actor: T) {
        BlackBox.reset()
        val result = expectation.apply(actual, startRange, endRange, actor)

        if (isAFailure(result, isNegated)) {
            throw AssertionError(expectation.compareActualWithExpected(actual, startRange, endRange, isNegated, expectedDescription))
        }
    }

    /**
     * Internal use only - DO NOT USE
     */
    protected constructor() : this(null,
            DoubleValueExpectation(
                    "placeholder",
                    fun(_: Actor?, _: A?, _: E, _: E): Boolean = true
            ),
            null,
            null,
            false,
            "") {
    }
}

open class PerformablePredicate(private val actual: A?,
                              private val expectation: PredicateExpectation,
                              private val isNegated: Boolean = false,
                              private val expectedDescription: String) : Performable {

    private val description = expectation.describe(isNegated, expectedDescription);

    @Step("{0} should see #description")
    override fun  performAs(actor: T) {
        BlackBox.reset()
        val result = expectation.apply(actual, actor)

        if (isAFailure(result, isNegated)) {
            throw AssertionError(expectation.compareActualWithExpected(actual, isNegated, expectedDescription))
        }
    }

    /**
     * Internal use only - DO NOT USE
     */
    protected constructor() : this(null,
            PredicateExpectation(
                    "placeholder","placeholder",
                    fun(_: Actor?, _: A?): Boolean = true
            ),
            false,
            "placeholder") {
    }
}

private fun isAFailure(result: Boolean, isNegated: Boolean) = (!isNegated && !result || isNegated && result)




© 2015 - 2025 Weber Informatics LLC | Privacy Policy