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

com.justai.jaicf.test.context.TestActionContext.kt Maven / Gradle / Ivy

package com.justai.jaicf.test.context

import com.justai.jaicf.api.BotRequest
import com.justai.jaicf.context.ActionContext
import com.justai.jaicf.context.ActivatorContext
import com.justai.jaicf.context.BotContext
import com.justai.jaicf.context.DefaultActionContext
import com.justai.jaicf.reactions.Reactions

/**
 * A [ActionContext] subclass used during the unit test execution.
 * Every action block of the scenario will be executed in this context during the unit test.
 * This class contains some functions that determines a smart random numbers generation and others helpers.
 */
data class TestActionContext(
    override val context: BotContext,
    override val activator: A,
    override val request: B,
    override val reactions: R,
    private val requestContext: TestRequestContext
) : ActionContext(context, activator, request, reactions) {

    internal fun nextRandomInt() = requestContext.randomNumbers.poll() ?: 0
    override fun random(min: Int, max: Int) = min

    /**
     * Finds a named variable passed from test.
     * @param name a name of the variable to find
     * @return variable value or null if nothing was found
     */
    fun getVar(name: String) = requestContext.variables[name]
}

/**
 * Indicates if scenario is running in test mode
 */
fun DefaultActionContext.isTestMode() = this is TestActionContext

/**
 * Runs this block of code only if the scenario is running in test mode
 * @param block a block of code to be ran in test mode only
 */
fun  ActionContext.runInTest(
    block: TestActionContext.() -> T
) = (this as? TestActionContext)?.block()




© 2015 - 2024 Weber Informatics LLC | Privacy Policy