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

org.jetbrains.spek.engine.Scope.kt Maven / Gradle / Ivy

package org.jetbrains.spek.engine

import org.jetbrains.spek.api.dsl.Pending
import org.jetbrains.spek.api.dsl.TestBody
import org.jetbrains.spek.api.lifecycle.ActionScope
import org.jetbrains.spek.api.lifecycle.GroupScope
import org.jetbrains.spek.api.lifecycle.TestScope
import org.jetbrains.spek.engine.lifecycle.LifecycleManager
import org.junit.platform.engine.TestDescriptor.Type.CONTAINER
import org.junit.platform.engine.TestDescriptor.Type.TEST
import org.junit.platform.engine.TestSource
import org.junit.platform.engine.UniqueId
import org.junit.platform.engine.support.descriptor.AbstractTestDescriptor
import org.junit.platform.engine.support.hierarchical.Node

/**
 * @author Ranie Jade Ramiso
 */
sealed class Scope(uniqueId: UniqueId, val pending: Pending, val source: TestSource?,
                   val lifecycleManager: LifecycleManager)
    : AbstractTestDescriptor(uniqueId, uniqueId.segments.last().value, source), Node,
      org.jetbrains.spek.api.lifecycle.Scope {

    override val parent: GroupScope? by lazy {
        return@lazy if (getParent().isPresent) {
            getParent().get() as GroupScope
        } else {
            null
        }
    }

    class Action(uniqueId: UniqueId, pending: Pending,
                 source: TestSource?,
                 lifecycleManager: LifecycleManager,
                 private val body: Action.(Node.DynamicTestExecutor) -> Unit)
        : Scope(uniqueId, pending, source, lifecycleManager), ActionScope {
        override fun getType() = CONTAINER

        override fun before(context: SpekExecutionContext): SpekExecutionContext {
            lifecycleManager.beforeExecuteAction(this)
            return context
        }

        override fun execute(context: SpekExecutionContext, dynamicTestExecutor: Node.DynamicTestExecutor): SpekExecutionContext {
            val collector = ThrowableCollector()

            if (collector.isEmpty()) {
                collector.executeSafely { body.invoke(this, dynamicTestExecutor) }
            }

            collector.assertEmpty()
            return context
        }

        override fun after(context: SpekExecutionContext) {
            lifecycleManager.afterExecuteAction(this)
        }

        override fun mayRegisterTests(): Boolean {
            return true
        }
    }

    open class Group(uniqueId: UniqueId, pending: Pending,
                     source: TestSource?,
                     lifecycleManager: LifecycleManager)
        : Scope(uniqueId, pending, source, lifecycleManager), GroupScope {
        override fun getType() = CONTAINER

        override fun before(context: SpekExecutionContext): SpekExecutionContext {
            lifecycleManager.beforeExecuteGroup(this@Group)
            return context
        }

        override fun after(context: SpekExecutionContext) {
            lifecycleManager.afterExecuteGroup(this@Group)
        }
    }

    class Test(uniqueId: UniqueId, pending: Pending, source: TestSource?, lifecycleManager: LifecycleManager, val body: TestBody.() -> Unit)
        : Scope(uniqueId, pending, source, lifecycleManager), TestScope {
        override val parent: GroupScope by lazy {
            getParent().get() as GroupScope
        }

        override fun getType() = TEST

        override fun before(context: SpekExecutionContext): SpekExecutionContext {
            lifecycleManager.beforeExecuteTest(this)
            return context
        }

        override fun after(context: SpekExecutionContext) {
            lifecycleManager.afterExecuteTest(this)
        }

        override fun execute(context: SpekExecutionContext, dynamicTestExecutor: Node.DynamicTestExecutor?): SpekExecutionContext {
            val collector = ThrowableCollector()
            if (collector.isEmpty()) {

                if (collector.isEmpty()) {
                    collector.executeSafely { body.invoke(object: TestBody {}) }
                }
            }

            collector.assertEmpty()

            return context
        }
    }

    override fun shouldBeSkipped(context: SpekExecutionContext): Node.SkipResult {
        return when(pending) {
            is Pending.Yes -> Node.SkipResult.skip(pending.reason)
            else -> Node.SkipResult.doNotSkip()
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy