Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix no-homogenous-tags: report homogenous example tags errors #40

Merged
merged 1 commit into from
May 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 15 additions & 8 deletions src/rules/no-homogenous-tags.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
const rule = 'no-homogenous-tags';
const {
applyOver,
compose,
intoArray,
} = require('../utils/generic');
const {filter, map} = require('../utils/transducers');
const {getScenarios} = require('../utils/selectors');
const {getExamples, getScenarios} = require('../utils/selectors');
const {flatMapScenarios} = require('../utils/gherkin');

const groupTagsByName = (tags) => {
return tags.reduce((set, {name}) => set.add(name), new Set());
Expand All @@ -17,19 +19,19 @@ const collectScenarioTagInfo = (info, {tags}) => {
}, info);
};

const createErrorByFeature = (feature) => ([name, tagsInfo]) => {
const createErrorByFeature = (feature, [parent, children]) => ([name, tagsInfo]) => {
return {
type: 'rule',
message: `${name} tag is declared in each scenario.` +
' It could be defined at feature level.',
message: `${name} tag is declared in each ${children}.` +
` It could be defined at ${parent} level.`,
rule: rule,
location: feature.location,
};
};

const noHomogenousTags = (feature) => {
const createError = createErrorByFeature(feature);
const scenarios = getScenarios(feature);
const noHomogenousTags = (getNodes, labels) => (feature) => {
const createError = createErrorByFeature(feature, labels);
const scenarios = getNodes(feature);
const tagsInfo = scenarios.reduce(collectScenarioTagInfo, new Map());
const {length} = scenarios;
return intoArray(compose(
Expand All @@ -38,8 +40,13 @@ const noHomogenousTags = (feature) => {
))([...tagsInfo]);
};

const run = applyOver([
noHomogenousTags(getScenarios, ['feature', 'scenario']),
flatMapScenarios(noHomogenousTags(getExamples, ['scenario', 'example'])),
]);

module.exports = {
name: rule,
run: noHomogenousTags,
run,
isValidConfig: () => [],
};
20 changes: 20 additions & 0 deletions test/rules/no-homogenous-tags/HomogenousExampleTags.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
Feature: Feature with example homogenous tags

Background:
Given I have a Background

@tag1
Scenario: This is a Scenario
Then this is a then step

@tag3
Scenario Outline: This is a Scenario Outline with the same tags
Then this is a then step <foo>
@tag2
Examples:
| foo |
| bar |
@tag2
Examples:
| foo |
| rab |
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Feature: Feature with homogenous tags
Feature: Feature with scenario homogenous tags

Background:
Given I have a Background
Expand Down
21 changes: 16 additions & 5 deletions test/rules/no-homogenous-tags/no-homogenous-tags.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,40 @@
const ruleName = 'no-homogenous-tags';
const ruleTestBase = require('../rule-test-base');
const rule = require('../../../src/rules/no-homogenous-tags.js');
const runTest = ruleTestBase.createRuleTest(rule, ({tag}) =>
`${tag} tag is declared in each scenario. It could be defined at feature level.`);
const runTest = ruleTestBase.createRuleTest(rule, ({tag, parent, children}) =>
`${tag} tag is declared in each ${children}. It could be defined at ${parent} level.`);

describe('No Homogenous Tags Rule', function() {
it('doesn\'t raise errors when there are no violations', function() {
runTest('no-homogenous-tags/NoViolations.feature', {}, []);
});

it('detects errors for scenarios, and scenario outlines', function() {
runTest('no-homogenous-tags/Violations.feature', {}, [{
runTest('no-homogenous-tags/HomogenousScenarioTags.feature', {}, [{
location: {
line: 1,
column: 1,
},
rule: ruleName,
messageElements: {tag: '@tag1'},
messageElements: {tag: '@tag1', children: 'scenario', parent: 'feature'},
}, {
location: {
line: 1,
column: 1,
},
rule: ruleName,
messageElements: {tag: '@tag2'},
messageElements: {tag: '@tag2', children: 'scenario', parent: 'feature'},
}]);
});

it('detects errors for examples', function() {
runTest('no-homogenous-tags/HomogenousExampleTags.feature', {}, [{
location: {
line: 11,
column: 1,
},
rule: ruleName,
messageElements: {tag: '@tag2', children: 'example', parent: 'scenario'},
}]);
});
});