From c3e84ab632aca4c3e1f7787ac255bdc86a32b368 Mon Sep 17 00:00:00 2001 From: szikszail Date: Sun, 28 Jan 2018 21:04:16 +0100 Subject: [PATCH] #6 ScenarioNumbering --- CHANGELOG.md | 1 + README.md | 1 + lib/builtIn/ScenarioNumbering.js | 43 ++++++++++++++++++++ lib/builtIn/ScenarioNumbering.md | 11 +++++ lib/builtIn/ScenarioOutlineNumbering.js | 3 +- lib/index.js | 1 + test/builtIn/ScenarioNumbering.spec.js | 29 +++++++++++++ test/data/input/scenarioNumbering.feature | 21 ++++++++++ test/data/output/scenarioNumbering.1.feature | 21 ++++++++++ test/data/output/scenarioNumbering.2.feature | 21 ++++++++++ 10 files changed, 151 insertions(+), 1 deletion(-) create mode 100644 lib/builtIn/ScenarioNumbering.js create mode 100644 lib/builtIn/ScenarioNumbering.md create mode 100644 test/builtIn/ScenarioNumbering.spec.js create mode 100644 test/data/input/scenarioNumbering.feature create mode 100644 test/data/output/scenarioNumbering.1.feature create mode 100644 test/data/output/scenarioNumbering.2.feature diff --git a/CHANGELOG.md b/CHANGELOG.md index 485f0c8..bfccb54 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/README.md b/README.md index fc5ddcf..1af632f 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/lib/builtIn/ScenarioNumbering.js b/lib/builtIn/ScenarioNumbering.js new file mode 100644 index 0000000..4ce5001 --- /dev/null +++ b/lib/builtIn/ScenarioNumbering.js @@ -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; \ No newline at end of file diff --git a/lib/builtIn/ScenarioNumbering.md b/lib/builtIn/ScenarioNumbering.md new file mode 100644 index 0000000..cd0eb09 --- /dev/null +++ b/lib/builtIn/ScenarioNumbering.md @@ -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: | `${i}. ${name}` | \ No newline at end of file diff --git a/lib/builtIn/ScenarioOutlineNumbering.js b/lib/builtIn/ScenarioOutlineNumbering.js index a078bd7..4d49e70 100644 --- a/lib/builtIn/ScenarioOutlineNumbering.js +++ b/lib/builtIn/ScenarioOutlineNumbering.js @@ -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 */ diff --git a/lib/index.js b/lib/index.js index beaaafc..3730782 100644 --- a/lib/index.js +++ b/lib/index.js @@ -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; \ No newline at end of file diff --git a/test/builtIn/ScenarioNumbering.spec.js b/test/builtIn/ScenarioNumbering.spec.js new file mode 100644 index 0000000..f070158 --- /dev/null +++ b/test/builtIn/ScenarioNumbering.spec.js @@ -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); + }); +}); \ No newline at end of file diff --git a/test/data/input/scenarioNumbering.feature b/test/data/input/scenarioNumbering.feature new file mode 100644 index 0000000..fe09b9d --- /dev/null +++ b/test/data/input/scenarioNumbering.feature @@ -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 \ No newline at end of file diff --git a/test/data/output/scenarioNumbering.1.feature b/test/data/output/scenarioNumbering.1.feature new file mode 100644 index 0000000..ee9056f --- /dev/null +++ b/test/data/output/scenarioNumbering.1.feature @@ -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 \ No newline at end of file diff --git a/test/data/output/scenarioNumbering.2.feature b/test/data/output/scenarioNumbering.2.feature new file mode 100644 index 0000000..22570ec --- /dev/null +++ b/test/data/output/scenarioNumbering.2.feature @@ -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 \ No newline at end of file