Step definition erroring when regex contains an optional capture group
mpkorstanje opened this issue · 6 comments
From cucumber/cucumber-jvm#948
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 inThen 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
userparameter.
Actually this also apply with optional capture group (not necessarily within non capture group).
Example:
Given("""^I have the name:\s?(.+)?$""") { (name: String) =>
// Do something
}(This one might not make much sense but I didn't have a better example in mind)
I think a first step would be to provide a better exception message when this happens.
The proper fix to this issue would be to be able to declare parameters as Options which is for now not supported out of the box.
Can you map java.util.Optional to scalas Option? That should be relatively easy to add to cucumber-expressions.
I guess having the ability to have Java's Optional supported out of the box as parameters would be enough indeed.
@mpkorstanje Do you mean I should open an issue on cucumber-expressions? I'll be happy to have a look and work on it of course :)
I'd start of with the PR straight away. Saves us writing an issue 😄
We keep Cucumber expressions in the mono repo.
https://github.com/cucumber/cucumber/tree/master/cucumber-expressions
You'd have to add a case for Optional here:
That way when if an Optional<String> or Optional<Integer> is used as a type hint the parameter is transformed to either a wrapped value or an empty.
The first version of JVM after 6.0.0-RC2 JVM will support optionals
Starting Cucumber Scala 6.0.0, it's now possible to have such step definitions:
import java.util.Optional
import io.cucumber.scala.{EN, ScalaDsl}
// Scala 2.13 only, use compat lib or your own converter for 2.12 or 2.11
import scala.jdk.OptionConverters._
class OptionalCaptureGroupsSteps extends ScalaDsl with EN {
Given("""^I have the name:\s?(.+)?$""") { (name: Optional[String]) =>
val option: Option[String] = name.toScala
// Do something
}
}