Skip to content

Instantly share code, notes, and snippets.

@stoyle
Created August 4, 2009 06:54
Show Gist options
  • Save stoyle/161064 to your computer and use it in GitHub Desktop.
Save stoyle/161064 to your computer and use it in GitHub Desktop.
import reflect.Manifest
object CucumberScalaDsl extends Application {
/* Revision. Does not work because Manifest.classType uses erased types.
Hence you will get a ClassCastException when using a double in stead of an integer for example.
Thanks fredriv for spotting this!
type stringFunction = Function1[String, Unit]
type intStringFunction = Function2[Int, String, Unit]
// Uses experimental Manifest type, provided by the compiler
def Given[T](name: String)(proc: T)(implicit m: scala.reflect.Manifest[T]) {
if(m >:> Manifest.classType(classOf[intStringFunction])) proc.asInstanceOf[intStringFunction](5, "belly")
else if (m >:> Manifest.classType(classOf[stringFunction])) proc.asInstanceOf[stringFunction]("belly")
}
*/
// Uses experimental Manifest type, provided by the compiler. However checking with Strings aint pretty...
def Given[T](name: String)(proc: T)(implicit m: scala.reflect.Manifest[T]) {
m.toString match {
case "scala.Function2[int, java.lang.String, void]" => proc.asInstanceOf[Function2[Int, String, Unit]](5, "belly")
case "scala.Function1[java.lang.String, void]" => proc.asInstanceOf[Function1[String, Unit]]("belly")
case _ =>
}
}
Given("I have (\\d+) cukes in my (.*)") { (cukes: Int, 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