Skip to content

Commit

Permalink
Merge pull request #67 from cucumber/optional-capture-groups
Browse files Browse the repository at this point in the history
Add tests to highlight mapping of optional capture groups
  • Loading branch information
gaeljw authored Jun 7, 2020
2 parents 904d38a + 7abd788 commit b9fa7ee
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Feature: Optional capture groups are supported

Scenario: present, using Java's Optional
Given I have the name: Jack

Scenario: absent, using Java's Optional
Given I don't have the name:
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package tests.misc

import java.util.Optional

import io.cucumber.scala.{EN, ScalaDsl}

class OptionalCaptureGroupsSteps extends ScalaDsl with EN {

// Scala 2.13 only
// import scala.jdk.OptionConverters._

import OptionalCaptureGroupsSteps._

Given("""^I have the name:\s?(.+)?$""") { (name: Optional[String]) =>
val option = name.toScala
assert(option.isDefined)
assert(option.getOrElse("Nope") == "Jack")
}

Given("""^I don't have the name:\s?(.+)?$""") { (name: Optional[String]) =>
val option = name.toScala
assert(option.isEmpty)
}

}

object OptionalCaptureGroupsSteps {

implicit class RichOptional[A](private val o: java.util.Optional[A]) extends AnyVal {

def toScala: Option[A] = if (o.isPresent) Some(o.get) else None

}

}
8 changes: 8 additions & 0 deletions scala/sources/src/test/scala/tests/misc/RunMiscTest.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package tests.misc

import io.cucumber.junit.{Cucumber, CucumberOptions}
import org.junit.runner.RunWith

@RunWith(classOf[Cucumber])
@CucumberOptions(strict = true)
class RunMiscTest

0 comments on commit b9fa7ee

Please sign in to comment.