Skip to content

Commit

Permalink
Add support for repeated tag options (#142)
Browse files Browse the repository at this point in the history
  • Loading branch information
Fryuni authored Jan 11, 2024
1 parent 25e63c8 commit 872de16
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 6 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -379,3 +379,5 @@ This library provides 3 Context Types to interact with CucumberJS' World object.
or the `--world-parameters` CLI option.
- `CucumberLog`, which exposes the `log` method of the `World` object.
- `CucumberAttachments`, which exposes the `attach` method of the `World` object.
- `ScenarioInfo`, which exposes information about the running scenario and allows
changing the behavior of steps and hooks based on tags easier.
49 changes: 49 additions & 0 deletions cucumber-tsflow-specs/features/tag-parameters.feature
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ Feature: Tag parameters
assert.strictEqual(this.scenario.getOptionTag(name), undefined);
}
@then("the multi option tag {string} is set to {}")
public checkMultiOption(name: string, value: string) {
assert.deepStrictEqual(this.scenario.getMultiOptionTag(name), JSON.parse(value));
}
@then("the attribute tag {string} is set to {}")
@then("the attribute tag {string} is set to:")
public checkAttributes(name: string, values: string) {
Expand Down Expand Up @@ -145,6 +150,50 @@ Feature: Tag parameters
When I run cucumber-js
Then it passes

Scenario: Checking for multi options on the feature
Given a file named "features/a.feature" with:
"""feature
@foo(bar)
@foo(baz)
Feature: Feature
Scenario: One
Then the multi option tag "foo" is set to ["bar", "baz"]
Scenario: Two
Then the multi option tag "foo" is set to ["bar", "baz"]
"""
When I run cucumber-js
Then it passes

Scenario: Checking for multi options on the scenario
Given a file named "features/a.feature" with:
"""feature
Feature: Feature
@foo(bar)
@foo(baz)
Scenario: One
Then the multi option tag "foo" is set to ["bar", "baz"]
@foo(qux)
@foo(zzz)
Scenario: Two
Then the multi option tag "foo" is set to ["qux", "zzz"]
"""
When I run cucumber-js
Then it passes

Scenario: Checking for multi options on the scenario combining with multi options on the feature
Given a file named "features/a.feature" with:
"""feature
@foo(bar)
Feature: Feature
Scenario: One
Then the multi option tag "foo" is set to ["bar"]
@foo(baz)
Scenario: Two
Then the multi option tag "foo" is set to ["bar", "baz"]
"""
When I run cucumber-js
Then it passes

Scenario: Checking for an attribute tag on the feature
Given a file named "features/a.feature" with:
"""feature
Expand Down
24 changes: 19 additions & 5 deletions cucumber-tsflow/src/scenario-info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { TagName } from "./types";
export class ScenarioInfo {
private _attributeTags?: Map<string, unknown>;

private _optionTags?: Map<string, string>;
private _optionTags?: Map<string, string[]>;

private _flagTags?: Set<string>;

Expand Down Expand Up @@ -39,17 +39,23 @@ export class ScenarioInfo {
return result;
}

private static parseOptionTags(tags: TagName[]): Map<string, string> {
private static parseOptionTags(tags: TagName[]): Map<string, string[]> {
const RGX = /^@?(?<option>[\w-]+)\((?<value>.+?)\)$/s;

const result = new Map<string, string>();
const result = new Map<string, string[]>();

for (const tag of tags) {
const match = tag.match(RGX)?.groups;

if (match !== undefined) {
const { option, value } = match;
result.set(option, value);

const list = result.get(option)
if (list === undefined) {
result.set(option, [value]);
} else {
list.push(value);
}
}
}

Expand Down Expand Up @@ -89,7 +95,15 @@ export class ScenarioInfo {
this._optionTags = ScenarioInfo.parseOptionTags(this.tags);
}

return this._optionTags.get(name);
return this._optionTags.get(name)?.at(-1);
}

public getMultiOptionTag(name: string): string[] | undefined {
if (this._optionTags === undefined) {
this._optionTags = ScenarioInfo.parseOptionTags(this.tags);
}

return this._optionTags.get(name) ?? [];
}

public getFlag(name: string): boolean {
Expand Down
2 changes: 1 addition & 1 deletion version.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"$schema": "https://raw.githubusercontent.com/dotnet/Nerdbank.GitVersioning/master/src/NerdBank.GitVersioning/version.schema.json",
"version": "4.3",
"version": "4.4",
"publicReleaseRefSpec": ["^refs/heads/master$", "^refs/heads/release/"]
}

0 comments on commit 872de16

Please sign in to comment.