Permalink
Browse files

Added shareSideEffects()

  • Loading branch information...
luontola committed May 17, 2011
1 parent 20eef82 commit c31f8508969098b05f7bedac0a9fe9a1b6fe833a
View
@@ -248,6 +248,7 @@ Version History
**1.x.x (2011-xx-xx)**
- Fixed the order of tests in JUnit results
- Added `shareSideEffects()`
**1.1.0 (2011-05-13)**
@@ -9,6 +9,10 @@ import net.orfjackal.specsy.core.ContextDealer
trait Spec {
private val context = ContextDealer.take()
def shareSideEffects() {
context.shareSideEffects()
}
def defer(body: => Unit) {
context.defer(body)
}
@@ -77,6 +77,10 @@ class Context(targetPath: Path, notifier: SuiteNotifier) {
current = current.parent
}
def shareSideEffects() {
current.shareSideEffects()
}
def defer(body: => Unit) {
current.addDefer(body)
}
@@ -4,14 +4,13 @@
package net.orfjackal.specsy.core
class SpecDeclaration(
val name: String,
val parent: SpecDeclaration,
val path: Path,
targetPath: Path
) {
class SpecDeclaration(val name: String,
val parent: SpecDeclaration,
val path: Path,
targetPath: Path) {
private var nextChild = path.firstChild
private var children = List[SpecDeclaration]()
private var _shareSideEffects = false
private var _deferred = List[() => Unit]()
def addChild(childName: String): SpecDeclaration = {
@@ -26,15 +25,23 @@ class SpecDeclaration(
path
}
def shareSideEffects() {
_shareSideEffects = true
}
private def isShareSideEffects: Boolean = {
_shareSideEffects || parent != null && parent.isShareSideEffects
}
def addDefer(body: => Unit) {
_deferred = (body _) :: _deferred
}
def deferred = _deferred
def shouldExecute: Boolean = isOnTargetPath || (isUnseen && isFirstChild)
def shouldExecute: Boolean = isOnTargetPath || (isUnseen && isFirstChild) || isShareSideEffects
def shouldPostpone: Boolean = isUnseen && !isFirstChild
def shouldPostpone: Boolean = isUnseen && !isFirstChild && !isShareSideEffects
private def isOnTargetPath = path.isOn(targetPath)
@@ -0,0 +1,85 @@
// Copyright © 2010-2011, Esko Luontola <www.orfjackal.net>
// This software is released under the Apache License 2.0.
// The license text is at http://www.apache.org/licenses/LICENSE-2.0
package net.orfjackal.specsy
import org.junit.Test
import org.junit.Assert._
import org.hamcrest.CoreMatchers._
import collection.mutable.Buffer
import net.orfjackal.specsy.core._
import net.orfjackal.specsy.runner._
class ShareSideEffectsTest {
val spy = Buffer[String]()
private def runSpec(spec: Context => Unit): Any = {
val runner = new SuiteRunner
val unusedCapturer = new OutputCapturer(null, null)
val monitor = new SuiteMonitor(runner, unusedCapturer)
runner.submitTestRun(new SpecRun(spec, monitor))
runner.await()
}
@Test
def children_are_affected() {
runSpec(c => {
c.bootstrap("root", {
c.shareSideEffects()
spy.append("root")
c.specify("child A", {
spy.append("A")
})
c.specify("child A", {
spy.append("B")
})
})
})
assertThat(spy, is(Buffer("root", "A", "B")))
}
@Test
def nested_children_are_affected() {
runSpec(c => {
c.bootstrap("root", {
c.shareSideEffects()
spy.append("root")
c.specify("child A", {
spy.append("A")
c.specify("child AA", {
spy.append("AA")
})
c.specify("child AB", {
spy.append("AB")
})
})
})
})
assertThat(spy, is(Buffer("root", "A", "AA", "AB")))
}
@Test
def siblings_are_not_affected() {
runSpec(c => {
c.bootstrap("root", {
spy.append("root")
c.specify("child A", {
c.shareSideEffects()
spy.append("A")
})
c.specify("child A", {
spy.append("B")
})
})
})
assertThat(spy, is(Buffer("root", "A", "root", "B")))
}
}

0 comments on commit c31f850

Please sign in to comment.