im.yagni.driveby.specs2.NakedDriveByExample.scala Maven / Gradle / Ivy
The newest version!
package im.yagni.driveby.specs2
import im.yagni.driveby.tracking.TrackingIds
import im.yagni.driveby.Example
import org.specs2.specification.AroundOutside
import org.specs2.execute.{FailureException, Result}
import org.specs2.mutable.SpecificationFeatures
trait NakedDriveByExample[T] extends AroundOutside[T] with SpecificationFeatures {
//TODO: get example name working again
//TODO: make example private
val example = Example("xxx", TrackingIds.nextExampleId)
def beforeExample: List[Function1[Example, Unit]] = Nil
def afterExample: List[Function1[Example, Unit]] = Nil
def onFailure: List[Function2[Example, String, Unit]] = Nil
private def doBeforeExample() { beforeExample.reverse.foreach(_(example)) }
private def doAfterExample() { afterExample.foreach(_(example)) }
private def doOnFailure(message: String) { onFailure.foreach(_(example, message)) }
def around[T <% Result](testBody: => T) = {
try {
doBeforeExample()
//TODO: should pass the example through to the body somehow
val r = testBody
if (!r.isSuccess && !r.isSkipped) doOnFailure(r.message)
r
}
catch {
case e: FailureException => doOnFailure(e.getMessage); throw e
case e: Exception => doOnFailure(e.getMessage); false aka e.getMessage must_== true
}
finally {
doAfterExample()
}
}
}