Skip to content

Commit c31f850

Browse files
committed
Added shareSideEffects()
1 parent 20eef82 commit c31f850

File tree

5 files changed

+109
-8
lines changed

5 files changed

+109
-8
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,7 @@ Version History
248248
**1.x.x (2011-xx-xx)**
249249

250250
- Fixed the order of tests in JUnit results
251+
- Added `shareSideEffects()`
251252

252253
**1.1.0 (2011-05-13)**
253254

src/main/scala/net/orfjackal/specsy/Spec.scala

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ import net.orfjackal.specsy.core.ContextDealer
99
trait Spec {
1010
private val context = ContextDealer.take()
1111

12+
def shareSideEffects() {
13+
context.shareSideEffects()
14+
}
15+
1216
def defer(body: => Unit) {
1317
context.defer(body)
1418
}

src/main/scala/net/orfjackal/specsy/core/Context.scala

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,10 @@ class Context(targetPath: Path, notifier: SuiteNotifier) {
7777
current = current.parent
7878
}
7979

80+
def shareSideEffects() {
81+
current.shareSideEffects()
82+
}
83+
8084
def defer(body: => Unit) {
8185
current.addDefer(body)
8286
}

src/main/scala/net/orfjackal/specsy/core/SpecDeclaration.scala

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,13 @@
44

55
package net.orfjackal.specsy.core
66

7-
class SpecDeclaration(
8-
val name: String,
9-
val parent: SpecDeclaration,
10-
val path: Path,
11-
targetPath: Path
12-
) {
7+
class SpecDeclaration(val name: String,
8+
val parent: SpecDeclaration,
9+
val path: Path,
10+
targetPath: Path) {
1311
private var nextChild = path.firstChild
1412
private var children = List[SpecDeclaration]()
13+
private var _shareSideEffects = false
1514
private var _deferred = List[() => Unit]()
1615

1716
def addChild(childName: String): SpecDeclaration = {
@@ -26,15 +25,23 @@ class SpecDeclaration(
2625
path
2726
}
2827

28+
def shareSideEffects() {
29+
_shareSideEffects = true
30+
}
31+
32+
private def isShareSideEffects: Boolean = {
33+
_shareSideEffects || parent != null && parent.isShareSideEffects
34+
}
35+
2936
def addDefer(body: => Unit) {
3037
_deferred = (body _) :: _deferred
3138
}
3239

3340
def deferred = _deferred
3441

35-
def shouldExecute: Boolean = isOnTargetPath || (isUnseen && isFirstChild)
42+
def shouldExecute: Boolean = isOnTargetPath || (isUnseen && isFirstChild) || isShareSideEffects
3643

37-
def shouldPostpone: Boolean = isUnseen && !isFirstChild
44+
def shouldPostpone: Boolean = isUnseen && !isFirstChild && !isShareSideEffects
3845

3946
private def isOnTargetPath = path.isOn(targetPath)
4047

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
// Copyright © 2010-2011, Esko Luontola <www.orfjackal.net>
2+
// This software is released under the Apache License 2.0.
3+
// The license text is at http://www.apache.org/licenses/LICENSE-2.0
4+
5+
package net.orfjackal.specsy
6+
7+
import org.junit.Test
8+
import org.junit.Assert._
9+
import org.hamcrest.CoreMatchers._
10+
import collection.mutable.Buffer
11+
import net.orfjackal.specsy.core._
12+
import net.orfjackal.specsy.runner._
13+
14+
class ShareSideEffectsTest {
15+
val spy = Buffer[String]()
16+
17+
private def runSpec(spec: Context => Unit): Any = {
18+
val runner = new SuiteRunner
19+
val unusedCapturer = new OutputCapturer(null, null)
20+
val monitor = new SuiteMonitor(runner, unusedCapturer)
21+
runner.submitTestRun(new SpecRun(spec, monitor))
22+
runner.await()
23+
}
24+
25+
@Test
26+
def children_are_affected() {
27+
runSpec(c => {
28+
c.bootstrap("root", {
29+
c.shareSideEffects()
30+
spy.append("root")
31+
32+
c.specify("child A", {
33+
spy.append("A")
34+
})
35+
c.specify("child A", {
36+
spy.append("B")
37+
})
38+
})
39+
})
40+
41+
assertThat(spy, is(Buffer("root", "A", "B")))
42+
}
43+
44+
@Test
45+
def nested_children_are_affected() {
46+
runSpec(c => {
47+
c.bootstrap("root", {
48+
c.shareSideEffects()
49+
spy.append("root")
50+
51+
c.specify("child A", {
52+
spy.append("A")
53+
54+
c.specify("child AA", {
55+
spy.append("AA")
56+
})
57+
c.specify("child AB", {
58+
spy.append("AB")
59+
})
60+
})
61+
})
62+
})
63+
64+
assertThat(spy, is(Buffer("root", "A", "AA", "AB")))
65+
}
66+
67+
@Test
68+
def siblings_are_not_affected() {
69+
runSpec(c => {
70+
c.bootstrap("root", {
71+
spy.append("root")
72+
73+
c.specify("child A", {
74+
c.shareSideEffects()
75+
spy.append("A")
76+
})
77+
c.specify("child A", {
78+
spy.append("B")
79+
})
80+
})
81+
})
82+
83+
assertThat(spy, is(Buffer("root", "A", "root", "B")))
84+
}
85+
}

0 commit comments

Comments
 (0)