Skip to content

Instantly share code, notes, and snippets.

@davetron5000
Created August 11, 2009 12:44
Show Gist options
  • Save davetron5000/165792 to your computer and use it in GitHub Desktop.
Save davetron5000/165792 to your computer and use it in GitHub Desktop.
import java.util.regex._
object Cucumber {
var givens:List[(String,(String) => Boolean)] = Nil
var whens:List[(String,(String) => Boolean)] = Nil
var thens:List[(String,(String) => Boolean)] = Nil
var testsRun = 0
var testsFailed = 0
def given(regexp:String, given:(String) => Boolean) = {
givens = (regexp,given) :: givens
testsRun = 0
testsFailed = 0
}
def when(regexp:String, when:(String) => Boolean) = {
whens = (regexp,when) :: whens
}
def then(regexp:String, test:(String) => Boolean) = {
thens = (regexp,test) :: thens
}
def given(s:String) = {
for (g <- givens) {
g match {
case (regexp,func) if (s.matches(regexp)) => callFuncIfMatch(regexp,s,func)
case _ => ;
}
}
}
def when(s:String) = {
for (g <- whens) {
g match {
case (regexp,func) if s.matches(regexp) => callFuncIfMatch(regexp,s,func)
case _ => ;
}
}
}
def then(s:String) = {
for (g <- thens) {
g match {
case (regexp,func) if s.matches(regexp) =>
testsRun += 1
if (!callFuncIfMatch(regexp,s,func)) {
testsFailed += 1
}
case _ => ;
}
}
}
def callFuncIfMatch(reg:String,s:String,func:Function1[String,Boolean]):Boolean = {
val p = Pattern.compile(reg)
val m = p.matcher(s)
if (m.matches)
func(m.group(1).toString)
else
false
}
implicit def stringToInt(s:String):Int = Integer.parseInt(s)
def =?[T](expected:T,got:T):Boolean = =?(expected,got,"Expected " + expected.toString + "\nbut got " + got.toString)
def =?[T](expected:T,got:T,message: => String):Boolean = {
if (expected != got) {
println(message)
false
}
else {
true
}
}
def scenario(desc:String)(test: => Unit) = {
(0 to desc.length).foreach( (_) => print("_") ); println
println(desc)
(0 to desc.length).foreach( (_) => print("-") ); println
println
test
println(testsRun + " tests run; " + testsFailed + " failures")
(0 to desc.length).foreach( (_) => print("=") ); println
}
}
object Test extends Application {
import Cucumber._
var cukesIAte = 0
var weight:Int = 0
var feeling = "indifferent"
given("""I ate (\d+) cucumbers for lunch""",
(numCukes) => {
cukesIAte = numCukes
true
}
)
when ("""I weigh (\w+)""",
(weighee) => {
if (weighee == "myself") {
weight = (cukesIAte * 20) + 100
feeling = if (weight > 180) "bad" else "ok"
true
}
else {
false
}
}
)
then ("""I should weigh (\d+) pounds""",
(expectedWeight) => {
=?[Int](expectedWeight,weight)
}
)
then ("""I should feel (\w+)""",
(expectedFeeling) => {
=?(expectedFeeling,feeling)
}
)
scenario("Eating 5 cucumbers makes me fat and sick")
{
given("I ate 5 cucumbers for lunch")
when("I weigh myself")
then("I should weigh 200 pounds")
then("I should feel bad")
}
scenario("Eating 1 cucumber doesn't make me sick")
{
given("I ate 1 cucumbers for lunch")
when("I weigh myself")
then("I should feel ok")
}
scenario("Eating 7 cucumbers makes me fat and sick")
{
given("I ate 7 cucumbers for lunch")
when("I weigh myself")
then("I should weigh 200 pounds")
then("I should feel bad")
}
}
_________________________________________
Eating 5 cucumbers makes me fat and sick
-----------------------------------------
2 tests run; 0 failures
=========================================
_______________________________________
Eating 1 cucumber doesn't make me sick
---------------------------------------
3 tests run; 0 failures
=======================================
_________________________________________
Eating 7 cucumbers makes me fat and sick
-----------------------------------------
Expected 200
but got 240
5 tests run; 1 failures
=========================================
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment