Cycle 2.5 introduced a new step that will retrieve a specific item from a delimited list and assign to a variable.
The step is , I assign <NUMBER> <st|nd|rd|th> item from "<DELIMITER>" delimited list "<LIST>" to variable "<VARIABLE_NAME>"
Usage:
The step requires a numbered index into the list, a delimiter and the list itself. These values can be provided directly to the step or assigned as variables and used in the step.
Example:
If you are looking to get the 3rd day of the week with the days separated by commas the step would look like:
Given I assign 3 rd item from "," delimited list "Monday,Tuesday,Wednesday,Thursday,Friday,Saturday,Sunday" to variable "day"
Then I echo $day
The expected result is Wednesday.
To employ variable replacement using the same example:
Given I assign 3 to variable "item"
And I assign "," to variable "delimiter"
And I assign "Monday,Tuesday,Wednesday,Thursday,Friday,Saturday,Sunday" to variable "list"
When I assign $item rd item from $delimiter delimited list $list to variable "day"
Then I echo $day
The expected result is again Wednesday.
Notes:
- The delimiter is not restricted to comma. Most non-operational related characters can be specified as a delimiter to fit your application.
- Using a unsupported delimiter will produce an error similar to:
- Delimiter is actually a regular expression so if you were to get an error similar to above try putting brackets [] around your delimeter. Ex. [+].
- The ordinal <st|nd|rd|th> is required but is used mainly for readability. If using variables you may not know in advance which ordinal "reads" the best but as long as one is specified in the step then the step will function properly.
- If the delimiter specified in the step is different than the delimiter in the list, the whole list will be considered index 1.
- Example
- I assign 1 st item from "-" delimited list "red,blue,green" to variable "color"
- Another way to think about this is any string, including empty, satisfies an index of 1 regardless of delimiter.
- Attempting an index out of bounds will produce this error in Cycle