Skip to content

Commit

Permalink
Added documentation and finalized precompiler
Browse files Browse the repository at this point in the history
  • Loading branch information
szikszail committed Jan 6, 2018
1 parent 9e1ae50 commit dec83e1
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 1 deletion.
55 changes: 54 additions & 1 deletion lib/builtIn/RemoveDuplicates.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,30 +20,58 @@ const removeDuplicates = array => Array.from(new Set(array));

/**
* Precompiler which removes duplicate tags and/or data table rows.
*
* @class
* @extends DefaultConfig
*/
class RemoveDuplicates extends DefaultConfig {
/**
* @constructor
* @param {RemoveDuplicatesConfiguration} config
*/
constructor(config) {
super();
/** @member {RemoveDuplicatesConfiguration} */
this.config = Object.assign({}, DEFAULT_CONFIG, config || {});

/** @member {Function} */
this.logTag = this._getLogger(this.config.processTags);
/** @member {Function} */
this.logRow = this._getLogger(this.config.processRows);
}

/**
* Creates an appropriate logger, based on configuration.
* @private
* @param {boolean} enabled
* @returns {Function}
*/
_getLogger(enabled) {
return this.config.verbose ? (...args) => console[enabled ? 'log' : 'warn'].apply(console, args) : () => null;
}

/**
* Checkes whether the given object has a tag with given name.
* @private
* @param {Scenario|ScenarioOutline|Examples|Feature} element
* @param {string} tagName
* @returns {boolean}
*/
_hasTag(element, tagName) {
if (!element.tags || !element.tags.length) {
return false;
}
return element.tags.some(tag => tag.name === tagName);
}

/**
* Removes duplicated tags from the given object.
* It removes:
* - tags which exists on parent too
* - duplicate tags
* @private
* @param {Scenario|ScenarioOutline|Examples|Feature} element
* @param {Feature} [parent]
*/
_filterTags(element, parent) {
if (element.tags && element.tags.length) {
const ownTags = element.tags.filter(tag => {
Expand All @@ -68,6 +96,13 @@ class RemoveDuplicates extends DefaultConfig {
}
}

/**
* Removes duplicate rows from the body of the examples.
* @private
* @param {Examples} examples
* @param {ScenarioOutline} scenario
* @param {Feature} parent
*/
_filterRows(examples, scenario, parent) {
if (examples.body && examples.body.length) {
const rowSet = new ObjectSet();
Expand All @@ -85,10 +120,28 @@ class RemoveDuplicates extends DefaultConfig {
}
}

/**
* Event handler for feature event.
* @param {Feature} feature
*/
onFeature(feature) {
this._filterTags(feature);
}

/**
* Event handler for scenario event.
* @param {Scenario} scenario
* @param {Feature} parent
*/
onScenario(scenario, parent) {
this._filterTags(scenario, parent);
}

/**
* Event handler for scenarioOutline event.
* @param {ScenarioOutline} scenarioOutline
* @param {Feature} parent
*/
onScenarioOutline(scenarioOutline, parent) {
this._filterTags(scenarioOutline, parent);
if (scenarioOutline.examples) {
Expand Down
25 changes: 25 additions & 0 deletions lib/builtIn/RemoveDuplicates.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# RemoveDuplicates for Gherkin precompiler

The RemoveDuplicates precompiler is responsible to have only reasonable amount of tags and/or rows in each feature file.

It can proceed the following actions:
1. Removes tags from Scenario/ScenarioOutline which exists on Feature too.
1. Removes duplicate tags from Scenario/ScearioOutline/Feature/Examples.
1. Removes duplicate rows from Examples.

## Configuration

RemoveDuplicateRows accepts the following configuration:

| Option | Type | Description | Default |
|:------:|:----:|:------------|:--------|
| `processTags` | `Boolean` | It indicates whether the 1st and 2nd option should be applied. | `true` |
| `processRows` | `Boolean` | It indicates whether the 3rd option should be applied. | `false` |
| `verbose` | `Boolean` | It indicates whether any warning or information message should be displayed. | `true` |

## Verbose mode

In case of any duplicate item the precompiler displays a message, unless verbose mode is turned off.

- If processing of an item type is turned **on**, it displayes an **INFO** message if it finds duplicate item.
- If processing of an item type if turned **off**, it displayes a **WARNING** message if it finds duplicate item, to indicate that processing would be suggested.
8 changes: 8 additions & 0 deletions test/builtIn/RemoveDuplicates.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,14 @@ describe('builtIn.RemoveDuplicates', () => {
});

describe('handlers', () => {
it('should process tags in case of Feature', () => {
const compiler = new RemoveDuplicates();
sinon.spy(compiler, '_filterTags');

compiler.onFeature('Feature');
expect(compiler._filterTags.calledWith('Feature')).to.be.true;
});

it('should process tags in case of Scenarios', () => {
const compiler = new RemoveDuplicates();
sinon.spy(compiler, '_filterTags');
Expand Down

0 comments on commit dec83e1

Please sign in to comment.