Skip to content

Commit

Permalink
#6 ScenarioNumbering
Browse files Browse the repository at this point in the history
  • Loading branch information
szikszail committed Jan 28, 2018
1 parent d3e3f05 commit c3e84ab
Show file tree
Hide file tree
Showing 10 changed files with 151 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
### Added

- Added predefined precompiler: removeDuplicates ([#10](https://github.com/judit-nahaj/gherkin-precompiler/issues/10))
- Added predefined precompiler: scenarioNumbering ([#6](https://github.com/judit-nahaj/gherkin-precompiler/issues/6))

### Fixed

Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ compiler.save('./features/dist/login.feature', ast, {
* [Macro](lib/builtIn/Macro.md) - Enables the user to create and execute macros.
* [RemoveDuplicates](lib/builtIn/RemoveDuplicates.md) - Removes duplicated tags or example data table rows.
* [Replacer](lib/builtIn/Replacer.md) - Replaces keywords in the feature files.
* [ScenarioNumbering](lib/builtIn/ScenarioNumbering.md) - Adds an index to all scenario and scenario outline's name.
* [ScenarioOutlineNumbering](lib/builtIn/ScenarioOutlineNumbering.md) - Makes all scenario, generated from scenario outlines unique.

## CLI
Expand Down
43 changes: 43 additions & 0 deletions lib/builtIn/ScenarioNumbering.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
'use strict';

const DefaultConfig = require('../DefaultConfig');
const {Background} = require('gherkin-assembler').AST;

/**
* @typedef {Object} ScenarioNumberConfiguration
* @property {string} format
*/
const DEFAULT_CONFIG = {
format: '${i}. ${name}'
};

/**
* The ScenarioNumbering precompiler is responsible
* to add an index to all scenario and scenario outlines
* name.
* @class
* @extends DefaultConfig
*/
class ScenarioNumbering extends DefaultConfig {
/**
* @constructor
* @param {ScenarioNumberingConfig|Object} config
*/
constructor(config) {
super();
this.config = Object.assign({}, DEFAULT_CONFIG, config || {});
}

onFeature(feature) {
let i = 0;
feature.elements.forEach(element => {
if (!(element instanceof Background)) {
element.name = this.config.format
.replace(/\$\{i\}/g, ++i)
.replace(/\$\{name\}/g, element.name);
}
});
}
}

module.exports = ScenarioNumbering;
11 changes: 11 additions & 0 deletions lib/builtIn/ScenarioNumbering.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# ScenarioNumbering for Gherkin precompiler

The ScenarioNumbering precompiler is responsible to add an index to all scenario and scenario outlines.

## Configuration

ScenariNumbering accepts the following configuration:

| Option | Type | Description | Default |
|:------:|:----:|:------------|:--------|
| `format` | `String` | The format, how index should be added to the name of the scenario/scenairo outline. Possible tokens: <ul><li>`${name}` the original name</li><li>`${i}` the index</li></ul> | `${i}. ${name}` |
3 changes: 2 additions & 1 deletion lib/builtIn/ScenarioOutlineNumbering.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ const NUMBERING_COLUMN = 'num';

/**
* The ScenarioOutlineNumbering precompiler is responsible
* to make all scenarios name, which generated from scenario outlines.
* to make all scenarios name unique,
* which generated from scenario outlines.
* @class
* @extends DefaultConfig
*/
Expand Down
1 change: 1 addition & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ API.builtIn.ForLoop = require('./builtIn/ForLoop');
API.builtIn.Macro = require('./builtIn/Macro');
API.builtIn.RemoveDuplicates = require('./builtIn/RemoveDuplicates');
API.builtIn.Replacer = require('./builtIn/Replacer');
API.builtIn.ScenarioNumbering = require('./builtIn/ScenarioNumbering');
API.builtIn.ScenarioOutlineNumbering = require('./builtIn/ScenarioOutlineNumbering');

module.exports = API;
29 changes: 29 additions & 0 deletions test/builtIn/ScenarioNumbering.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
'use strict';
const path = require('path');
const ScenarioNumbering = require(path.resolve('lib/builtIn/ScenarioNumbering.js'));
const expect = require('chai').expect;
const API = require(path.resolve('lib'));

describe('builtIn.ScenarioNumbering', () => {
it('should be available through API', () => {
expect(API.builtIn.ScenarioNumbering).to.equal(ScenarioNumbering);
});

it('should process feature files with default config', () => {
const baseAst = API.load('test/data/input/scenarioNumbering.feature');
const expectedAst = API.load('test/data/output/scenarioNumbering.1.feature');
const resultAst = API.process(baseAst, new ScenarioNumbering());

expect(resultAst).to.eql(expectedAst);
});

it('should process feature files with custom config', () => {
const baseAst = API.load('test/data/input/scenarioNumbering.feature');
const expectedAst = API.load('test/data/output/scenarioNumbering.2.feature');
const resultAst = API.process(baseAst, new ScenarioNumbering({
format: '${i} - ${name}'
}));

expect(resultAst).to.eql(expectedAst);
});
});
21 changes: 21 additions & 0 deletions test/data/input/scenarioNumbering.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
Feature: ScenarioNumbering

Background:
Given step
When step
Then step

Scenario: First Scenario
Given step
When step
Then step

Scenario Outline: Second Scenario
Given step
When step
Then step

Scenario: Third Scenario
Given step
When step
Then step
21 changes: 21 additions & 0 deletions test/data/output/scenarioNumbering.1.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
Feature: ScenarioNumbering

Background:
Given step
When step
Then step

Scenario: 1. First Scenario
Given step
When step
Then step

Scenario Outline: 2. Second Scenario
Given step
When step
Then step

Scenario: 3. Third Scenario
Given step
When step
Then step
21 changes: 21 additions & 0 deletions test/data/output/scenarioNumbering.2.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
Feature: ScenarioNumbering

Background:
Given step
When step
Then step

Scenario: 1 - First Scenario
Given step
When step
Then step

Scenario Outline: 2 - Second Scenario
Given step
When step
Then step

Scenario: 3 - Third Scenario
Given step
When step
Then step

0 comments on commit c3e84ab

Please sign in to comment.