Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Passing Scenario reference in Before and After hooks #431

Merged
merged 1 commit into from
Nov 28, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions scala/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@
</dependency>
</dependencies>

<pluginRepositories>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this needed? When I work with Cucumber-JVM and IDEA I just open the top-level pom.xml as a project and IDEA does the rest.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea, it is not actually. I was working with Gradle for a long time and was using idea plugin(as IDEA integration with gradle projects is not that great) for it and completly forgot that the IDEA can open Maven projects from the top-level pom. Apologies.

<pluginRepository>
<id>maven-idea-plugin-repo</id>
<url>http://maven-idea-plugin.googlecode.com/svn/maven-repo</url>
</pluginRepository>
</pluginRepositories>
<build>
<plugins>
<plugin>
Expand Down Expand Up @@ -124,6 +130,11 @@ file.write(template.toString(), "UTF-8")
</execution>
</executions>
</plugin>
<plugin>
<groupId>com.googlecode</groupId>
<artifactId>maven-idea-plugin</artifactId>
<version>1.6</version>
</plugin>
</plugins>
</build>
</project>
18 changes: 9 additions & 9 deletions scala/src/main/scala/cucumber/api/scala/ScalaDsl.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package cucumber.api.scala

import _root_.cucumber.runtime._
import _root_.cucumber.api.Scenario
import _root_.cucumber.runtime.scala.Transform
import _root_.cucumber.runtime.scala.ScalaHookDefinition
import _root_.cucumber.runtime.scala.ScalaStepDefinition
Expand All @@ -14,28 +14,28 @@ trait ScalaDsl { self =>
private [cucumber] val beforeHooks = new ArrayBuffer[HookDefinition]
private [cucumber] val afterHooks = new ArrayBuffer[HookDefinition]

def Before(f: => Unit){
def Before(f: Scenario => Unit){
Before()(f)
}

def Before(tags: String*)(f: => Unit) {
def Before(tags: String*)(f: Scenario => Unit) {
Before(Int.MaxValue, tags :_*)(f)
}

def Before(order:Int, tags:String*)(f: => Unit){
beforeHooks += new ScalaHookDefinition(f _, order, tags)
def Before(order:Int, tags:String*)(f: Scenario => Unit){
beforeHooks += new ScalaHookDefinition(f, order, tags)
}

def After(f: => Unit){
def After(f: Scenario => Unit){
After()(f)
}

def After(tags: String*)(f: => Unit) {
def After(tags: String*)(f: Scenario => Unit) {
After(Int.MaxValue, tags:_*)(f)
}

def After(order:Int, tags: String*)(f: => Unit){
afterHooks += new ScalaHookDefinition(f _, order, tags)
def After(order:Int, tags: String*)(f: Scenario => Unit){
afterHooks += new ScalaHookDefinition(f, order, tags)
}

final class Step(name: String) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ import _root_.cucumber.api.Scenario
import _root_.cucumber.runtime.HookDefinition
import collection.JavaConverters._

class ScalaHookDefinition(f:() => Unit, order:Int, tags:Seq[String]) extends HookDefinition {
class ScalaHookDefinition(f:Scenario => Unit, order:Int, tags:Seq[String]) extends HookDefinition {
val tagExpression = new TagExpression(tags.asJava)

def getLocation(detail: Boolean) = "TODO: Implement getLocation in similar fashion to ScalaStepDefinition"

def execute(scenario: Scenario) { f() }
def execute(scenario: Scenario) { f(scenario) }

def matches(tags: Collection[Tag]) = tagExpression.eval(tags)

Expand Down
65 changes: 30 additions & 35 deletions scala/src/test/scala/cucumber/api/scala/ScalaDslTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,36 +7,43 @@ import _root_.gherkin.formatter.model.Tag
import collection.JavaConverters._

import _root_.cucumber.runtime.scala.Transform
import cucumber.api.Scenario

class ScalaDslTest {

object StubScenario extends Scenario{
def getStatus = ""

def isFailed = false

def embed(p1: Array[Byte], p2: String) {}

def write(p1: String) {}
}

@Test
def emptyBefore {

var called = false
var actualScenario : Scenario = null

object Befores extends ScalaDsl with EN {
Before {
called = true
}
Before { actualScenario = _ }
}

assertEquals(1, Befores.beforeHooks.size)
val hook = Befores.beforeHooks.head
assertTrue(hook.matches(List[Tag]().asJava))
hook.execute(null)
assertTrue(called)
hook.execute(StubScenario)
assertEquals(Int.MaxValue, hook.getOrder)
assertEquals(StubScenario, actualScenario)
}

@Test
def taggedBefore {
var called = false
var actualScenario : Scenario = null

object Befores extends ScalaDsl with EN {
Before("@foo,@bar", "@zap"){
called = true
}
Before("@foo,@bar", "@zap"){ actualScenario = _ }
}

assertEquals(1, Befores.beforeHooks.size)
Expand All @@ -46,20 +53,16 @@ class ScalaDslTest {
assertTrue(hook.matches(List(new Tag("@bar", 0), new Tag("@zap", 0)).asJava))
assertFalse(hook.matches(List(new Tag("@bar", 1)).asJava))

hook.execute(null)
assertTrue(called)
hook.execute(StubScenario)
assertEquals(StubScenario, actualScenario)
assertEquals(Int.MaxValue, hook.getOrder)
}

@Test
def orderedBefore {

var called = false

object Befores extends ScalaDsl with EN {
Before(10){
called = true
}
Before(10){ scenario : Scenario => }
}

val hook = Befores.beforeHooks(0)
Expand All @@ -69,12 +72,8 @@ class ScalaDslTest {
@Test
def taggedOrderedBefore {

var called = false

object Befores extends ScalaDsl with EN {
Before(10, "@foo,@bar", "@zap"){
called = true
}
Before(10, "@foo,@bar", "@zap"){ scenario : Scenario => }
}

val hook = Befores.beforeHooks(0)
Expand All @@ -84,29 +83,25 @@ class ScalaDslTest {
@Test
def emptyAfter {

var called = false
var actualScenario : Scenario = null

object Afters extends ScalaDsl with EN {
After {
called = true
}
After { actualScenario = _ }
}

assertEquals(1, Afters.afterHooks.size)
val hook = Afters.afterHooks.head
assertTrue(hook.matches(List[Tag]().asJava))
hook.execute(null)
assertTrue(called)
hook.execute(StubScenario)
assertEquals(StubScenario, actualScenario)
}

@Test
def taggedAfter {
var called = false
var actualScenario : Scenario = null

object Afters extends ScalaDsl with EN {
After("@foo,@bar", "@zap"){
called = true
}
After("@foo,@bar", "@zap"){ actualScenario = _ }
}

assertEquals(1, Afters.afterHooks.size)
Expand All @@ -116,8 +111,8 @@ class ScalaDslTest {
assertTrue(hook.matches(List(new Tag("@bar", 0), new Tag("@zap", 0)).asJava))
assertFalse(hook.matches(List(new Tag("@bar", 1)).asJava))

hook.execute(null)
assertTrue(called)
hook.execute(StubScenario)
assertEquals(StubScenario, actualScenario)
}

@Test
Expand All @@ -132,7 +127,7 @@ class ScalaDslTest {

assertEquals(1, Dummy.stepDefinitions.size)
val step = Dummy.stepDefinitions.head
assertEquals("ScalaDslTest.scala:128", step.getLocation(true)) // be careful with formatting or this test will break
assertEquals("ScalaDslTest.scala:123", step.getLocation(true)) // be careful with formatting or this test will break
assertEquals("x", step.getPattern)
step.execute(new I18n("en"), Array())
assertTrue(called)
Expand Down