scala - Step definition erroring when regex contains a capture group within non capture group
Closed this issue · 8 comments
I have a step definition as
Given( """^I am logged in(?: as (.+))?$""") { (user: String) =>
// do something
}
And my feature file step is
Given I am logged in
Then I am getting an error
scala.MatchError: List(null) (of class scala.collection.immutable.$colon$colon)
at scala.PartialFunction$$anon$1.apply(PartialFunction.scala:253)
at scala.PartialFunction$$anon$1.apply(PartialFunction.scala:251)
at cucumber.api.scala.ScalaDsl$StepBody$$anonfun$apply$2.applyOrElse(ScalaDsl.scala:95)
at cucumber.api.scala.ScalaDsl$StepBody$$anonfun$apply$2.applyOrElse(ScalaDsl.scala:95)
at scala.runtime.AbstractPartialFunction.apply(AbstractPartialFunction.scala:36)
at cucumber.runtime.scala.ScalaStepDefinition.execute(ScalaStepDefinition.scala:71)
at cucumber.runtime.StepDefinitionMatch.runStep(StepDefinitionMatch.java:37)
at cucumber.runtime.Runtime.runStep(Runtime.java:298)
at cucumber.runtime.model.StepContainer.runStep(StepContainer.java:44)
at cucumber.runtime.model.StepContainer.runSteps(StepContainer.java:39)
at cucumber.runtime.model.CucumberScenario.run(CucumberScenario.java:48)
at cucumber.runtime.model.CucumberFeature.run(CucumberFeature.java:154)
at cucumber.runtime.Runtime.run(Runtime.java:120)
at cucumber.runtime.Runtime.run(Runtime.java:108)
at cucumber.api.cli.Main.run(Main.java:36)
at cucumber.api.cli.Main.main(Main.java:18)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
at ✽.Given I am logged in(/Users/anshulbajpai/projects/payments-frontend/features/spike.feature:319)
Ideally, I'd expect the control to go into the step definition with null value for user parameter.
Nulls are never acceptable in scala, unless you're interfacing with java code. Perhaps a better semantic would be an Option[String] for the type of User. I certainly won't support putting null in there.
I think you are right. I didn't think of using Option as parameter type. Thanks.
It didn't work straight away, I had to write a transformer for it.
@XStreamConverter(classOf[OptionalStringConverter])
case class OptionalString(input : Option[String])
class OptionalStringConverter extends Transformer[OptionalString]{
override def transform(s: String) = OptionalString(Option(s))
}
Given( """^I am logged in(?: as user (.+))?$""") { (user: OptionalString ) =>
user.input match {
case Some(u) => println(s"Logged in as $u")
case None => println(s"Logged in as anonymous user")
}
}
Would have been better if this transformation can be taken care by scala api
@anshulbajpai I agree, the API should do that. Care to try your hand at a pull request? ;)
Sure..I'll take it up.
Hi @anshulbajpai , did you ever get this working? Otherwise someone else might be able to take a look at it. Please let us know!
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.