-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add unique-examples rule * add unique-examples rule
- Loading branch information
Showing
3 changed files
with
112 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { switchOrSeveritySchema } from '../schemas' | ||
import Schema from '../schema' | ||
import Rule from '../rule' | ||
import { RawSchema, AcceptedSchema } from '../types' | ||
import Document from '../document' | ||
|
||
export default class UniqueExamples implements Rule { | ||
public readonly name: string = 'unique-examples' | ||
|
||
public readonly acceptedSchema: AcceptedSchema = switchOrSeveritySchema | ||
|
||
public readonly schema: Schema | ||
|
||
public constructor(rawSchema: RawSchema) { | ||
this.schema = new Schema(rawSchema) | ||
} | ||
|
||
public async run(document: Document): Promise<void> { | ||
document.feature.children.forEach((child) => { | ||
if (!child.scenario) { | ||
return | ||
} | ||
|
||
if (child.scenario.keyword !== 'Scenario Outline') { | ||
return | ||
} | ||
|
||
if (!child.scenario.examples.length) { | ||
return | ||
} | ||
|
||
const names = [] | ||
child.scenario.examples.map((e) => { | ||
if (!names.includes(e.name)) { | ||
names.push(e.name) | ||
return | ||
} | ||
|
||
document.addError(this.name, 'Examples should contain a unique name if there are more than one.', e.location) | ||
}) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
Feature: Unique Examples | ||
|
||
Scenario: Invalid Examples | ||
Given the following feature file | ||
""" | ||
Feature: Invalid | ||
Scenario Outline: | ||
Given I do <THING> | ||
Then I should have done <THING> | ||
Examples: | ||
| THING | | ||
| Swim | | ||
Examples: | ||
| THING | | ||
| Eat | | ||
""" | ||
When Gherklin is ran with the following configuration | ||
| rules | | ||
| {"unique-examples": "error"} | | ||
Then there is 1 file with errors | ||
And the errors are | ||
| location | severity | rule | message | | ||
| {"line": 8, "column": 3} | error | unique-examples | Examples should contain a unique name if there are more than one. | | ||
|
||
Scenario: Valid Examples | ||
Given the following feature file | ||
""" | ||
Feature: Valid | ||
Scenario Outline: | ||
Given I do <THING> | ||
Then I should have done <THING> | ||
Examples: Recreational | ||
| THING | | ||
| Swim | | ||
Examples: Mandatory | ||
| THING | | ||
| Eat | | ||
""" | ||
When Gherklin is ran with the following configuration | ||
| rules | | ||
| {"unique-examples": "error"} | | ||
Then there are 0 files with errors |