Skip to content

Instantly share code, notes, and snippets.

@fredriv
Created August 4, 2009 11:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fredriv/161159 to your computer and use it in GitHub Desktop.
Save fredriv/161159 to your computer and use it in GitHub Desktop.
import reflect.Manifest
import Manifest._
object CucumberScalaDsl extends Application {
def genValue(theType: String): Any =
theType match {
case "int" => 5
case "long" => 123456789L
case "java.lang.String" => "belly"
}
// Uses experimental Manifest type, provided by the compiler
def Given[T](name: String)(proc: T)(implicit m: scala.reflect.Manifest[T]) {
val func1 = """scala.Function1\[([^ ,]+).+\]""".r
val func2 = """scala.Function2\[([^ ,]+), ([^ ,]+).+\]""".r
m.toString match {
case func1(t1) => proc.asInstanceOf[Any => _](genValue(t1))
case func2(t1, t2) => proc.asInstanceOf[(Any, Any) => _](genValue(t1), genValue(t2))
case _ => println("No test defined for type " + m.toString)
}
}
Given("I have (\\d+) cukes in my (.*)") ( (cukes: Int, where: String) =>
println("You have " + cukes + " cukes in your " + where) )
Given("I have (\\d+) cukes in my (.*)") ( (cukes: Long, where: String) =>
println("You have " + cukes + " cukes in your " + where) )
Given("my name is (.*)") ( (name: String) =>
println("Your name is " + name) )
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment