Skip to content

Commit b9fa7ee

Browse files
authored
Merge pull request #67 from cucumber/optional-capture-groups
Add tests to highlight mapping of optional capture groups
2 parents 904d38a + 7abd788 commit b9fa7ee

File tree

3 files changed

+50
-0
lines changed

3 files changed

+50
-0
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Feature: Optional capture groups are supported
2+
3+
Scenario: present, using Java's Optional
4+
Given I have the name: Jack
5+
6+
Scenario: absent, using Java's Optional
7+
Given I don't have the name:
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package tests.misc
2+
3+
import java.util.Optional
4+
5+
import io.cucumber.scala.{EN, ScalaDsl}
6+
7+
class OptionalCaptureGroupsSteps extends ScalaDsl with EN {
8+
9+
// Scala 2.13 only
10+
// import scala.jdk.OptionConverters._
11+
12+
import OptionalCaptureGroupsSteps._
13+
14+
Given("""^I have the name:\s?(.+)?$""") { (name: Optional[String]) =>
15+
val option = name.toScala
16+
assert(option.isDefined)
17+
assert(option.getOrElse("Nope") == "Jack")
18+
}
19+
20+
Given("""^I don't have the name:\s?(.+)?$""") { (name: Optional[String]) =>
21+
val option = name.toScala
22+
assert(option.isEmpty)
23+
}
24+
25+
}
26+
27+
object OptionalCaptureGroupsSteps {
28+
29+
implicit class RichOptional[A](private val o: java.util.Optional[A]) extends AnyVal {
30+
31+
def toScala: Option[A] = if (o.isPresent) Some(o.get) else None
32+
33+
}
34+
35+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package tests.misc
2+
3+
import io.cucumber.junit.{Cucumber, CucumberOptions}
4+
import org.junit.runner.RunWith
5+
6+
@RunWith(classOf[Cucumber])
7+
@CucumberOptions(strict = true)
8+
class RunMiscTest

0 commit comments

Comments
 (0)