Skip to content

Commit

Permalink
Introduce fixture aliases for Gherkin style (#818)
Browse files Browse the repository at this point in the history
  • Loading branch information
jcornaz authored and raniejade committed Nov 8, 2019
1 parent 526f58d commit 72ef370
Show file tree
Hide file tree
Showing 4 changed files with 170 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package org.spekframework.spek2


import org.spekframework.spek2.dsl.Fixture
import org.spekframework.spek2.dsl.GroupBody
import org.spekframework.spek2.dsl.Skip
import org.spekframework.spek2.dsl.TestBody
import org.spekframework.spek2.lifecycle.CachingMode
import org.spekframework.spek2.lifecycle.MemoizedValue

class FakeGroupBody : GroupBody {
var beforeEachGroup: Fixture? = null
var afterEachGroup: Fixture? = null
var beforeGroup: Fixture? = null
var afterGroup: Fixture? = null
var beforeEachTest: Fixture? = null
var afterEachTest: Fixture? = null

override fun group(description: String, skip: Skip, defaultCachingMode: CachingMode, preserveExecutionOrder: Boolean, failFast: Boolean, body: GroupBody.() -> Unit) =
throw UnsupportedOperationException()

override val defaultCachingMode: CachingMode
get() = throw UnsupportedOperationException()

override fun <T> memoized(mode: CachingMode, factory: () -> T): MemoizedValue<T> =
throw UnsupportedOperationException()

override fun <T> memoized(mode: CachingMode, factory: () -> T, destructor: (T) -> Unit): MemoizedValue<T> =
throw UnsupportedOperationException()

override fun beforeEachTest(fixture: Fixture) {
beforeEachTest = fixture
}

override fun afterEachTest(fixture: Fixture) {
afterEachTest = fixture
}

override fun beforeEachGroup(fixture: Fixture) {
beforeEachGroup = fixture
}

override fun afterEachGroup(fixture: Fixture) {
afterEachGroup = fixture
}

override fun beforeGroup(fixture: Fixture) {
beforeGroup = fixture
}

override fun afterGroup(fixture: Fixture) {
afterGroup = fixture
}

override fun <T> memoized(): MemoizedValue<T> = throw UnsupportedOperationException()

override var defaultTimeout: Long = 0L

override fun test(description: String, skip: Skip, timeout: Long, body: TestBody.() -> Unit) = throw UnsupportedOperationException()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package org.spekframework.spek2

import org.spekframework.spek2.style.gherkin.FeatureBody
import kotlin.test.assertSame

object FeatureBodyAliasesTest : Spek({

val groupBody by memoized { FakeGroupBody() }
val featureBody by memoized { FeatureBody(groupBody) }

test("beforeEachScenario should call beforeEachGroup") {
val fixture = { println("hello") }
featureBody.beforeEachScenario(fixture)
assertSame(fixture, groupBody.beforeEachGroup)
}

test("afterEachScenario should call AfterEachGroup") {
val fixture = { println("hello") }
featureBody.afterEachScenario(fixture)
assertSame(fixture, groupBody.afterEachGroup)
}

test("beforeFeature should call BeforeGroup") {
val fixture = { println("hello") }
featureBody.beforeFeature(fixture)
assertSame(fixture, groupBody.beforeGroup)
}

test("afterFeature should call AfterGroup") {
val fixture = { println("hello") }
featureBody.afterFeature(fixture)
assertSame(fixture, groupBody.afterGroup)
}
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package org.spekframework.spek2


import org.spekframework.spek2.style.gherkin.ScenarioBody
import kotlin.test.assertSame

object ScenarioBodyAliasesTest : Spek({

val groupBody by memoized { FakeGroupBody() }
val scenarioBody by memoized { ScenarioBody(groupBody) }

test("beforeEachScenario should call BeforeEachGroup") {
val fixture = { println("hello") }
scenarioBody.beforeScenario(fixture)
assertSame(fixture, groupBody.beforeGroup)
}

test("afterEachScenario should call AfterEachGroup") {
val fixture = { println("hello") }
scenarioBody.afterScenario(fixture)
assertSame(fixture, groupBody.afterGroup)
}

test("beforeEachStep should call BeforeEachTest") {
val fixture = { println("hello") }
scenarioBody.beforeEachStep(fixture)
assertSame(fixture, groupBody.beforeEachTest)
}

test("afterEachStep should call AfterEachTest") {
val fixture = { println("hello") }
scenarioBody.afterEachStep(fixture)
assertSame(fixture, groupBody.afterEachTest)
}
})
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
package org.spekframework.spek2.style.gherkin

import org.spekframework.spek2.dsl.Fixture
import org.spekframework.spek2.dsl.GroupBody
import org.spekframework.spek2.dsl.LifecycleAware
import org.spekframework.spek2.dsl.TestBody
import org.spekframework.spek2.lifecycle.CachingMode
import org.spekframework.spek2.meta.*
import org.spekframework.spek2.meta.Description
import org.spekframework.spek2.meta.DescriptionLocation
import org.spekframework.spek2.meta.Descriptions
import org.spekframework.spek2.meta.SpekDsl
import org.spekframework.spek2.meta.Synonym
import org.spekframework.spek2.meta.SynonymType

@Synonym(SynonymType.GROUP, prefix = "Feature: ")
@Descriptions(Description(DescriptionLocation.VALUE_PARAMETER, 0))
Expand All @@ -27,6 +33,23 @@ class FeatureBody(val delegate: GroupBody): LifecycleAware by delegate {
body(ScenarioBody(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)
}

@SpekDsl
Expand Down Expand Up @@ -58,4 +81,21 @@ class ScenarioBody(val delegate: GroupBody): LifecycleAware by delegate {
fun And(description: String, timeout: Long = defaultTimeout, body: TestBody.() -> Unit) {
delegate.test("And: $description", timeout = timeout, body = body)
}

fun beforeScenario(fixture: Fixture) = delegate.beforeGroup(fixture)
fun afterScenario(fixture: Fixture) = delegate.afterGroup(fixture)
fun beforeEachStep(fixture: Fixture) = delegate.beforeEachTest(fixture)
fun afterEachStep(fixture: Fixture) = delegate.afterEachTest(fixture)

@Deprecated("Use beforeEachStep instead", ReplaceWith("beforeEachStep(fixture)"))
override fun beforeEachTest(fixture: Fixture) = beforeEachStep(fixture)

@Deprecated("Use afterEachStep instead", ReplaceWith("afterEachStep(fixture)"))
override fun afterEachTest(fixture: Fixture) = afterEachStep(fixture)

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

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

0 comments on commit 72ef370

Please sign in to comment.