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

com.bybutter.sisyphus.test.descriptor.SisyphusTestCaseDescriptor.kt Maven / Gradle / Ivy

There is a newer version: 2.1.22
Show newest version
package com.bybutter.sisyphus.test.descriptor

import com.bybutter.sisyphus.test.SisyphusTestEngineContext
import com.bybutter.sisyphus.test.SisyphusTestRootContext
import com.bybutter.sisyphus.test.TestCase
import com.bybutter.sisyphus.test.extension.AfterTestCase
import com.bybutter.sisyphus.test.extension.BeforeTestCase
import com.bybutter.sisyphus.test.extensions
import org.junit.platform.engine.TestDescriptor
import org.junit.platform.engine.UniqueId
import org.junit.platform.engine.support.descriptor.EngineDescriptor
import org.junit.platform.engine.support.hierarchical.Node
import org.opentest4j.AssertionFailedError

class SisyphusTestCaseDescriptor(id: UniqueId, val case: TestCase) :
    EngineDescriptor(id, case.name),
    Node {

    override fun shouldBeSkipped(context: SisyphusTestEngineContext): Node.SkipResult {
        return if (case.steps.isEmpty() && case.asserts.isEmpty()) Node.SkipResult.skip("No test steps found.") else Node.SkipResult.doNotSkip()
    }

    override fun prepare(context: SisyphusTestEngineContext): SisyphusTestEngineContext {
        return (context as SisyphusTestRootContext).forCase()
    }

    override fun before(context: SisyphusTestEngineContext): SisyphusTestEngineContext {
        context.extensions { beforeTestCase(context, this@SisyphusTestCaseDescriptor) }
        return context
    }

    override fun around(context: SisyphusTestEngineContext, invocation: Node.Invocation) {
        invocation.invoke(context)
        val engine = context.cel().fork(context.results())
        for (assert in case.asserts) {
            val result = engine.eval(assert)
            if (result != true) {
                throw AssertionFailedError(
                    "Assertion '$assert' failed in test '$displayName'.",
                    true,
                    result
                )
            }
        }
    }

    override fun after(context: SisyphusTestEngineContext) {
        context.extensions { afterTestCase(context, this@SisyphusTestCaseDescriptor) }
    }

    override fun getType(): TestDescriptor.Type {
        return TestDescriptor.Type.CONTAINER_AND_TEST
    }

    companion object {
        const val SEGMENT_TYPE = "cases"
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy