Skip to content

Commit c1ca5cf

Browse files
committed
Passing Scenario reference in Before and After hooks. Added idea plugin in pom to generate idea files
1 parent b1d3f48 commit c1ca5cf

File tree

4 files changed

+52
-46
lines changed

4 files changed

+52
-46
lines changed

scala/pom.xml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,12 @@
5555
</dependency>
5656
</dependencies>
5757

58+
<pluginRepositories>
59+
<pluginRepository>
60+
<id>maven-idea-plugin-repo</id>
61+
<url>http://maven-idea-plugin.googlecode.com/svn/maven-repo</url>
62+
</pluginRepository>
63+
</pluginRepositories>
5864
<build>
5965
<plugins>
6066
<plugin>
@@ -124,6 +130,11 @@ file.write(template.toString(), "UTF-8")
124130
</execution>
125131
</executions>
126132
</plugin>
133+
<plugin>
134+
<groupId>com.googlecode</groupId>
135+
<artifactId>maven-idea-plugin</artifactId>
136+
<version>1.6</version>
137+
</plugin>
127138
</plugins>
128139
</build>
129140
</project>

scala/src/main/scala/cucumber/api/scala/ScalaDsl.scala

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package cucumber.api.scala
22

3-
import _root_.cucumber.runtime._
3+
import _root_.cucumber.api.Scenario
44
import _root_.cucumber.runtime.scala.Transform
55
import _root_.cucumber.runtime.scala.ScalaHookDefinition
66
import _root_.cucumber.runtime.scala.ScalaStepDefinition
@@ -14,28 +14,28 @@ trait ScalaDsl { self =>
1414
private [cucumber] val beforeHooks = new ArrayBuffer[HookDefinition]
1515
private [cucumber] val afterHooks = new ArrayBuffer[HookDefinition]
1616

17-
def Before(f: => Unit){
17+
def Before(f: Scenario => Unit){
1818
Before()(f)
1919
}
2020

21-
def Before(tags: String*)(f: => Unit) {
21+
def Before(tags: String*)(f: Scenario => Unit) {
2222
Before(Int.MaxValue, tags :_*)(f)
2323
}
2424

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

29-
def After(f: => Unit){
29+
def After(f: Scenario => Unit){
3030
After()(f)
3131
}
3232

33-
def After(tags: String*)(f: => Unit) {
33+
def After(tags: String*)(f: Scenario => Unit) {
3434
After(Int.MaxValue, tags:_*)(f)
3535
}
3636

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

4141
final class Step(name: String) {

scala/src/main/scala/cucumber/runtime/scala/ScalaHookDefinition.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ import _root_.cucumber.api.Scenario
77
import _root_.cucumber.runtime.HookDefinition
88
import collection.JavaConverters._
99

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

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

15-
def execute(scenario: Scenario) { f() }
15+
def execute(scenario: Scenario) { f(scenario) }
1616

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

scala/src/test/scala/cucumber/api/scala/ScalaDslTest.scala

Lines changed: 30 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -7,36 +7,43 @@ import _root_.gherkin.formatter.model.Tag
77
import collection.JavaConverters._
88

99
import _root_.cucumber.runtime.scala.Transform
10+
import cucumber.api.Scenario
1011

1112
class ScalaDslTest {
1213

14+
object StubScenario extends Scenario{
15+
def getStatus = ""
16+
17+
def isFailed = false
18+
19+
def embed(p1: Array[Byte], p2: String) {}
20+
21+
def write(p1: String) {}
22+
}
23+
1324
@Test
1425
def emptyBefore {
1526

16-
var called = false
27+
var actualScenario : Scenario = null
1728

1829
object Befores extends ScalaDsl with EN {
19-
Before {
20-
called = true
21-
}
30+
Before { actualScenario = _ }
2231
}
2332

2433
assertEquals(1, Befores.beforeHooks.size)
2534
val hook = Befores.beforeHooks.head
2635
assertTrue(hook.matches(List[Tag]().asJava))
27-
hook.execute(null)
28-
assertTrue(called)
36+
hook.execute(StubScenario)
2937
assertEquals(Int.MaxValue, hook.getOrder)
38+
assertEquals(StubScenario, actualScenario)
3039
}
3140

3241
@Test
3342
def taggedBefore {
34-
var called = false
43+
var actualScenario : Scenario = null
3544

3645
object Befores extends ScalaDsl with EN {
37-
Before("@foo,@bar", "@zap"){
38-
called = true
39-
}
46+
Before("@foo,@bar", "@zap"){ actualScenario = _ }
4047
}
4148

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

49-
hook.execute(null)
50-
assertTrue(called)
56+
hook.execute(StubScenario)
57+
assertEquals(StubScenario, actualScenario)
5158
assertEquals(Int.MaxValue, hook.getOrder)
5259
}
5360

5461
@Test
5562
def orderedBefore {
5663

57-
var called = false
58-
5964
object Befores extends ScalaDsl with EN {
60-
Before(10){
61-
called = true
62-
}
65+
Before(10){ scenario : Scenario => }
6366
}
6467

6568
val hook = Befores.beforeHooks(0)
@@ -69,12 +72,8 @@ class ScalaDslTest {
6972
@Test
7073
def taggedOrderedBefore {
7174

72-
var called = false
73-
7475
object Befores extends ScalaDsl with EN {
75-
Before(10, "@foo,@bar", "@zap"){
76-
called = true
77-
}
76+
Before(10, "@foo,@bar", "@zap"){ scenario : Scenario => }
7877
}
7978

8079
val hook = Befores.beforeHooks(0)
@@ -84,29 +83,25 @@ class ScalaDslTest {
8483
@Test
8584
def emptyAfter {
8685

87-
var called = false
86+
var actualScenario : Scenario = null
8887

8988
object Afters extends ScalaDsl with EN {
90-
After {
91-
called = true
92-
}
89+
After { actualScenario = _ }
9390
}
9491

9592
assertEquals(1, Afters.afterHooks.size)
9693
val hook = Afters.afterHooks.head
9794
assertTrue(hook.matches(List[Tag]().asJava))
98-
hook.execute(null)
99-
assertTrue(called)
95+
hook.execute(StubScenario)
96+
assertEquals(StubScenario, actualScenario)
10097
}
10198

10299
@Test
103100
def taggedAfter {
104-
var called = false
101+
var actualScenario : Scenario = null
105102

106103
object Afters extends ScalaDsl with EN {
107-
After("@foo,@bar", "@zap"){
108-
called = true
109-
}
104+
After("@foo,@bar", "@zap"){ actualScenario = _ }
110105
}
111106

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

119-
hook.execute(null)
120-
assertTrue(called)
114+
hook.execute(StubScenario)
115+
assertEquals(StubScenario, actualScenario)
121116
}
122117

123118
@Test
@@ -132,7 +127,7 @@ class ScalaDslTest {
132127

133128
assertEquals(1, Dummy.stepDefinitions.size)
134129
val step = Dummy.stepDefinitions.head
135-
assertEquals("ScalaDslTest.scala:128", step.getLocation(true)) // be careful with formatting or this test will break
130+
assertEquals("ScalaDslTest.scala:123", step.getLocation(true)) // be careful with formatting or this test will break
136131
assertEquals("x", step.getPattern)
137132
step.execute(new I18n("en"), Array())
138133
assertTrue(called)

0 commit comments

Comments
 (0)