Skip to content

Commit

Permalink
Add unique-examples rule (#69)
Browse files Browse the repository at this point in the history
* add unique-examples rule

* add unique-examples rule
  • Loading branch information
cjmarkham authored Sep 29, 2024
1 parent a2ae21d commit 9c4f451
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/rules/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ option.
| [No Inconsistent Quotes](#no-inconsistent-quotes) | `no-inconsistent-quotes` ||
| [Filename Snake Case](#filename-snake-case) | `filename-snake-case` ||
| [Filename Kebab Case](#filename-kebab-case) | `filename-kebab-case` ||
| [Unique Examples](#unique-examples) | `unique-examples` ||

### Allowed Tags

Expand Down Expand Up @@ -682,3 +683,29 @@ export default {
}
}
```

### Unique Examples

Examples should have a unique name if there are more than 1 in a scenario outline.

**Examples**

Enable the rule

```typescript
export default {
rules: {
'unique-examples': 'on',
}
}
```

Enable the rule and set severity

```typescript
export default {
rules: {
'unique-examples': 'error',
}
}
```
43 changes: 43 additions & 0 deletions src/rules/unique-examples.ts
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)
})
})
}
}
42 changes: 42 additions & 0 deletions tests/acceptance/features/unique-examples.feature
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

0 comments on commit 9c4f451

Please sign in to comment.