spekframework / spek

A specification framework for Kotlin
Other
2.23k stars 179 forks source link

Gherkin rule dsl missing #992

Open sebastien-delaherche opened 2 years ago

sebastien-delaherche commented 2 years ago

Hi guys,

First thank you for this framwork.

I think we miss some kind of Group between Feature and Scenario on Gherkin implementation. In fact there is actually such definition on Gherkin specification: https://cucumber.io/docs/gherkin/reference/#rule

I would like to add it in the framework if it don't bother you?

I think it is just a matter of extension, BUT when I try to do it in my codebase, green arrow button on Android Studio has been replaced by "Do Nothing" text.

Can you help me on this issue?

Edit: Seems to be a duplicate of https://github.com/spekframework/spek/issues/873

@Synonym(SynonymType.GROUP, prefix = "Rule: ")
@Descriptions(Description(DescriptionLocation.VALUE_PARAMETER, 0))
fun FeatureBody.Rule(description: String, body: RuleBody.() -> Unit) =
    delegate.group("Rule: $description",
        defaultCachingMode = CachingMode.EACH_GROUP,
        preserveExecutionOrder = true,
        failFast = true) {
        body(RuleBody(this))
    }

@SpekDsl
class RuleBody(val delegate: GroupBody) : LifecycleAware by delegate {
    var defaultTimeout: Long
        get() = delegate.defaultTimeout
        set(value) {
            delegate.defaultTimeout = value
        }

    @Synonym(SynonymType.GROUP, prefix = "Scenario: ")
    @Descriptions(Description(DescriptionLocation.VALUE_PARAMETER, 0))
    fun Scenario(description: String, body: ScenarioBody.() -> Unit) {
        delegate.group("Scenario: $description",
            defaultCachingMode = CachingMode.SCOPE,
            preserveExecutionOrder = true,
            failFast = true) {
            body(ScenarioBody(this))
        }
    }

    @Synonym(SynonymType.GROUP, prefix = "Rule: ")
    @Descriptions(Description(DescriptionLocation.VALUE_PARAMETER, 0))
    fun Rule(description: String, body: RuleBody.() -> Unit) =
        delegate.group("Rule: $description",
            defaultCachingMode = CachingMode.EACH_GROUP,
            preserveExecutionOrder = true,
            failFast = true) {
            body(RuleBody(this))
        }

    fun beforeEachScenario(fixture: Fixture) = delegate.beforeEachGroup(fixture)
    fun afterEachScenario(fixture: Fixture) = delegate.afterEachGroup(fixture)
    fun beforeFeature(fixture: Fixture) = delegate.beforeGroup(fixture)
    fun afterFeature(fixture: Fixture) = delegate.afterGroup(fixture)

    @Deprecated("Use beforeEachScenario instead", ReplaceWith("beforeEachScenario(fixture)"))
    override fun beforeEachGroup(fixture: Fixture) = beforeEachScenario(fixture)

    @Deprecated("Use afterEachScenario instead", ReplaceWith("afterEachScenario(fixture)"))
    override fun afterEachGroup(fixture: Fixture) = afterEachScenario(fixture)

    @Deprecated("Use beforeFeature instead", ReplaceWith("beforeFeature(fixture)"))
    override fun beforeGroup(fixture: Fixture) = beforeFeature(fixture)

    @Deprecated("Use afterFeature instead", ReplaceWith("afterFeature(fixture)"))
    override fun afterGroup(fixture: Fixture) = afterFeature(fixture)
}