PageRenderTime 43ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/scala/sources/src/main/scala/cucumber/runtime/scala/ScalaStepDefinition.scala

http://github.com/cucumber/cucumber-jvm
Scala | 85 lines | 27 code | 11 blank | 47 comment | 1 complexity | 0359599df6da2d2945885e8866dd6287 MD5 | raw file
  1. package cucumber.runtime.scala
  2. import _root_.java.lang.reflect.Type
  3. import _root_.gherkin.formatter.model.Step
  4. import _root_.gherkin.I18n
  5. import _root_.java.util.regex.Pattern
  6. import _root_.cucumber.runtime.StepDefinition
  7. import _root_.cucumber.runtime.JdkPatternArgumentMatcher
  8. import _root_.cucumber.runtime.ParameterInfo
  9. import collection.JavaConversions._
  10. import cucumber.api.Transform
  11. /**
  12. * Implementation of step definition for scala.
  13. *
  14. * @param frame Representation of a stack frame containing information about the context in which a
  15. * step was defined. Allows retrospective queries about the definition of a step.
  16. *
  17. * @param name The name of the step definition class, e.g. cucumber.runtime.scala.test.CukesStepDefinitions
  18. *
  19. * @param pattern The regex matcher that defines the cucumber step, e.g. /I eat (.*) cukes$/
  20. * @param parameterInfos
  21. *
  22. * @param f Function body of a step definition. This is what actually runs the code within the step def.
  23. */
  24. class ScalaStepDefinition(frame:StackTraceElement,
  25. name:String,
  26. pattern:String,
  27. parameterInfos:Array[Type],
  28. f:List[Any] => Any) extends StepDefinition {
  29. /**
  30. * Compiled pattern matcher for the cucumber step regex.
  31. */
  32. private val argumentMatcher = new JdkPatternArgumentMatcher(Pattern.compile(pattern))
  33. /**
  34. * Returns a list of arguments. Return null if the step definition
  35. * doesn't match at all. Return an empty List if it matches with 0 arguments
  36. * and bigger sizes if it matches several.
  37. */
  38. def matchedArguments(step: Step) = argumentMatcher.argumentsFrom(step.getName)
  39. /**
  40. * The source line where the step definition is defined.
  41. * Example: foo/bar/Zap.brainfuck:42
  42. *
  43. * @param detail true if extra detailed location information should be included.
  44. */
  45. def getLocation(detail: Boolean) = frame.getFileName + ":" + frame.getLineNumber
  46. /**
  47. * How many declared parameters this stepdefinition has. Returns null if unknown.
  48. */
  49. def getParameterCount() = parameterInfos.size
  50. /**
  51. * The parameter type at index n. A hint about the raw parameter type is passed to make
  52. * it easier for the implementation to make a guess based on runtime information.
  53. * As Scala is a statically typed language, the javaType parameter is ignored
  54. */
  55. def getParameterType(index: Int, javaType: Type) = {
  56. new ParameterInfo(parameterInfos(index), null, null, null)
  57. }
  58. /**
  59. * Invokes the step definition. The method should raise a Throwable
  60. * if the invocation fails, which will cause the step to fail.
  61. */
  62. def execute(i18n: I18n, args: Array[AnyRef]) { f(args.toList) }
  63. /**
  64. * Return true if this matches the location. This is used to filter
  65. * stack traces.
  66. */
  67. def isDefinedAt(stackTraceElement: StackTraceElement) = stackTraceElement == frame
  68. /**
  69. * @return the pattern associated with this instance. Used for error reporting only.
  70. */
  71. def getPattern = pattern
  72. def isScenarioScoped = false
  73. }