Skip to content

Commit

Permalink
add scenario-action and scenario-verification rules
Browse files Browse the repository at this point in the history
  • Loading branch information
Carl Markham committed Sep 29, 2024
1 parent bbf9be7 commit b32797c
Show file tree
Hide file tree
Showing 5 changed files with 177 additions and 0 deletions.
54 changes: 54 additions & 0 deletions src/rules/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ option.
| [Filename Kebab Case](#filename-kebab-case) | `filename-kebab-case` ||
| [Unique Examples](#unique-examples) | `unique-examples` ||
| [Feature Description](#feature-description) | `feature-description` ||
| [Scenario Action](#scenario-action) | `scenario-action` ||
| [Scenario Verification](#scenario-verification) | `scenario-verification` ||

### Allowed Tags

Expand Down Expand Up @@ -736,3 +738,55 @@ export default {
}
}
```

### Scenario Action

Scenarios should have a "When" to denote an action

**Examples**

Enable the rule

```typescript
export default {
rules: {
'scenario-action': 'on',
}
}
```

Enable the rule and set severity

```typescript
export default {
rules: {
'scenario-action': 'error',
}
}
```

### Scenario Verification

Scenarios should have a "Then" to denote verification of an action

**Examples**

Enable the rule

```typescript
export default {
rules: {
'scenario-verification': 'on',
}
}
```

Enable the rule and set severity

```typescript
export default {
rules: {
'scenario-verification': 'error',
}
}
```
30 changes: 30 additions & 0 deletions src/rules/scenario-action.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { switchOrSeveritySchema } from '../schemas'
import Schema from '../schema'
import Rule from '../rule'
import { RawSchema, AcceptedSchema } from '../types'
import Document from '../document'

export default class ScenarioAction implements Rule {
public readonly name: string = 'scenario-action'

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
}

const whens = child.scenario.steps.filter((s) => s.keyword.trim() === 'When')
if (whens.length === 0) {
document.addError(this.name, 'Scenario should contain a "When" to denote an action.', child.scenario.location)
}
})
}
}
34 changes: 34 additions & 0 deletions src/rules/scenario-verification.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { switchOrSeveritySchema } from '../schemas'
import Schema from '../schema'
import Rule from '../rule'
import { RawSchema, AcceptedSchema } from '../types'
import Document from '../document'

export default class ScenarioVerification implements Rule {
public readonly name: string = 'scenario-verification'

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
}

const whens = child.scenario.steps.filter((s) => s.keyword.trim() === 'Then')
if (whens.length === 0) {
document.addError(
this.name,
'Scenario should contain a "Then" to denote verification of an action.',
child.scenario.location,
)
}
})
}
}
30 changes: 30 additions & 0 deletions tests/acceptance/features/scenario-action.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
Feature: Feature Description

Scenario: Invalid
Given the following feature file
"""
Feature: Invalid
Scenario: Doing something
Given I do something
Then I should have done something
"""
When Gherklin is ran with the following configuration
| rules |
| {"scenario-action": "error"} |
Then there is 1 file with errors
And the errors are
| location | severity | rule | message |
| {"line": 2, "column": 3} | error | scenario-action | Scenario should contain a "When" to denote an action. |

Scenario: Valid
Given the following feature file
"""
Feature: Valid
Scenario: Doing something
When I do something
Then I should have done something
"""
When Gherklin is ran with the following configuration
| rules |
| {"scenario-action": "error"} |
Then there are 0 files with errors
29 changes: 29 additions & 0 deletions tests/acceptance/features/scenario-verification.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
Feature: Feature Description

Scenario: Invalid
Given the following feature file
"""
Feature: Invalid
Scenario: Doing something
When I do something
"""
When Gherklin is ran with the following configuration
| rules |
| {"scenario-verification": "error"} |
Then there is 1 file with errors
And the errors are
| location | severity | rule | message |
| {"line": 2, "column": 3} | error | scenario-verification | Scenario should contain a "Then" to denote verification of an action. |

Scenario: Valid
Given the following feature file
"""
Feature: Valid
Scenario: Doing something
When I do something
Then I should have done something
"""
When Gherklin is ran with the following configuration
| rules |
| {"scenario-verification": "error"} |
Then there are 0 files with errors

0 comments on commit b32797c

Please sign in to comment.