Description
🤔 What's the problem you're trying to solve?
In some cases it might be helpful to have a way of declaring several instances of one type as an expression, e.g.
Given the customer selects product(s) {product}+
which could match something like:
Given the customer selects product bike
Given the customer selects products bike, car
Given the customer selects products bike, car, skateboard
and for a better flow maybe also with a filler:
Given the customer selects products bike and car
Given the customer selects products bike, car or skateboard
What already works is something like this:
val separators = arrayOf(",", ";", "and", "or")
@ParameterType("^(?:(?:,|;|and|or)?\s?\w+)+$")
fun products(product: String): List<Product> {
return product.split(*separators)
.map { it.trim() }
.map { Product(it) }
}
This has the disadvantage, that the regex (if specific things still should be checked) might get complex, since it also has to include all separators and the possibility of them being there or not. Also, the complete string (incl. separators) is highlighted in the feature files. Hence, having that as a feature of cucumber expressions would be nice, since it might make the used regex easier. And might also help with the highlighting in the feature files, since the separators could be displayed in another color.
✨ What's your proposed solution?
I could imagine two different ways. For both ways the definition of the parameter type might be kind of the same like it is now (define how the transformation is done for one single element), but it could define separators:
@ParameterType(regex = "\\w+", separators=[",", ";", "and", "or"])
fun product(product: String): List<Product> {
return Product(it)
}
Option one: a special notation for marking an expression as "vararg", e.g. {name}+
If the + is provided, the method signature must contain a collection type:
@Given("the customer selects product(s) {product}+")
fun givenCustomerSelectsProduct(product: List<Product>) { \* ... *\ }
Option two: no special expression syntax - instead it depends on the method signature if it is a single value or a collection of values is provided.
So both would be possible:
@Given("the customer selects product(s) {product}")
fun givenCustomerSelectsProduct(product: Product) { \* ... *\ }
and
@Given("the customer selects product(s) {product}")
fun givenCustomerSelectsProduct(product: List<Product>) { \* ... *\ }