Created
September 11, 2009 06:51
-
-
Save abailly/185126 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Scenario[T <: ScenarioContext[T]](val title: String)(implicit val context : T) { | |
import system._ | |
private var s: Scenario[T] = null | |
def apply(synopsis: => Unit) = { | |
new Scenario(title)(context) { | |
override def run : Unit = synopsis | |
} | |
} | |
def run : Unit = fail("Empty scenario") | |
} | |
object Scenario { | |
def apply[T <: ScenarioContext[T]](title: String)(implicit c : T) : Scenario[T] = new Scenario(title) | |
implicit def stringToScenario[T <: ScenarioContext[T]](title: String)(implicit c : T) : Scenario[T] = Scenario(title) | |
} | |
trait ScenarioContext[T <: ScenarioContext[T]] { | |
self : T => | |
var givens : List[Given] = Nil | |
var whens : List[When] = Nil | |
var thens : List[Then] = Nil | |
val groups : Array[String] = Array() | |
trait Step { | |
val title : String | |
var step : T => Unit = null | |
lazy val pat : Pattern = Pattern.compile(title) | |
def -:(s : String) : boolean = { | |
val m = pat.matcher(s) | |
if(m.matches) { | |
for(i <- Array.range(0, m.groupCount)) | |
groups(i) = m.group(i) | |
true | |
} | |
false | |
} | |
def apply(step : T => Unit) = { | |
this.step = step | |
} | |
def run(context : T) = { | |
step(context) | |
println(this) | |
} | |
} | |
case class Given(val title : String) extends Step { | |
givens = this :: givens | |
override def toString : String = { | |
"Given :" + title | |
} | |
} | |
case class When(val title : String) extends Step{ | |
whens = this :: whens | |
override def toString : String = { | |
"When :" + title | |
} | |
} | |
case class Then(val title : String) extends Step { | |
thens = this :: thens | |
override def toString : String = { | |
"Then :" + title | |
} | |
} | |
def given(assumption: String) = { | |
for(g <- this.givens; if g.title == assumption) | |
if(assumption -: g) | |
g.run(this) | |
} | |
def when(action : String) = { | |
for(g <- this.whens; if g.title == action) | |
if(action -: g) | |
g.run(this) | |
} | |
def then(assertion : String) = { | |
for(g <- this.thens) { | |
if(assertion -: g) | |
g.run(this) | |
} | |
} | |
def and(followup : String) = { | |
for(g <- this.thens) { | |
if(followup -: g) | |
g.run(this) | |
} | |
} | |
def Scenario(title : String) : Scenario[T] = new Scenario(title)(this) | |
} | |
trait MonitorSteps extends ScenarioContext[MonitorSteps] { | |
import system._ | |
Given("I am starting from scratch") { m : MonitorSteps => | |
start | |
} | |
When("I visit the home page") { m : MonitorSteps => | |
home | |
} | |
Then("I should see (\\w+) with \"([^\"])\"") { m : MonitorSteps => | |
findXpath("//" + groups(1)).getText == "MX Metrics " + groups(2) | |
} | |
Then("I should not see (\\w+) with \"([^\"])\"") { m : MonitorSteps => | |
try { | |
findXpath("//" + groups(1)).getText == "MX Metrics " + groups(2) | |
} catch { | |
case _ => | |
} | |
fail("Found matching title") | |
} | |
} | |
/** | |
* | |
* @author [email protected] | |
* @version $Rev$ | |
*/ | |
class MonitorTest extends MonitorSteps { | |
@Test | |
def canConnectToBasePage = { | |
Scenario("user connects to base page and sees title with 'Monitor'") { | |
given("I am starting from scratch") | |
when("I visit the home page") | |
then("I should see title with \"Monitor\"") | |
and("I should not see title with \"Toto\"") | |
}.run | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment