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

run.smt.ktest.api.internal.SpecExecutor.kt Maven / Gradle / Ivy

package run.smt.ktest.api.internal

import run.smt.ktest.api.*
import run.smt.ktest.api.lifecycle.Lifecycle

enum class InitializationMode {
    EAGER, LAZY
}

class SpecExecutor(
    specClass: Class,
    initializationMode: InitializationMode,
    runnerDescription: R
) {
    private val spec = instantiateSpec(specClass)

    val rootSuite by lazy { with(spec) { Internals.currentSuite.also { initialize(it) } } }
    private val notifier by lazy { Lifecycle.createNotifierFor(spec) }

    init {
        with(spec) {
            Internals.runnerDescription = runnerDescription
        }

        if (initializationMode == InitializationMode.EAGER) {
            rootSuite.name // initializing
        }
    }

    val executables: List by lazy {
        rootSuite.allChildCases.map { ExecutableCase(it, notifier) }
    }

    fun startup() {
        // noop
    }

    fun finalize() {
        with(spec) {
            Internals.closeResources {
                notifier.emitCaseFailure(
                    it,
                    Case(rootSuite, "close resources", rootSuite.inheritedMetaData) {}
                )
            }
        }
    }

    private fun initialize(suite: Suite) {
        val initializationException = suite.initialize()
        if (initializationException != null) {
            // that's a hack! :)
            suite.addCase(Case(suite, "initialization", noMetaData()) {
                throw initializationException
            })
        }
        suite.childSuites.forEach(this::initialize)
    }

    private fun instantiateSpec(specClass: Class): BaseSpec {
        return specClass.kotlin.objectInstance ?: specClass.newInstance()
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy