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

Add docstring option in indentation rule #43

Merged
merged 1 commit into from
May 12, 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
57 changes: 51 additions & 6 deletions src/rules/indentation.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,43 @@ const defaultConfig = {
'then': 2,
'and': 2,
'but': 2,
'DocString': 4,
};

const availableConfigs = Object.assign({}, defaultConfig, {
// The values here are unused by the config parsing logic.
'feature tag': -1,
'scenario tag': -1,
'DocString line': -1,
});

const parseDocString = (lines) => (docString) => {
if (!docString) {
return;
}
const {line} = docString.location;
const docStringLines = docString.content.split(/\r\n|\r|\n/);
const rawDocStringLines = lines.slice(line, line + docStringLines.length);
return Object.assign({}, docString, {
lines: rawDocStringLines.map((rawLine, index) => {
const column = rawLine.indexOf(docStringLines[index]) + 1;
return {
content: docStringLines[index],
location: {
line: line + index + 1,
column,
},
};
}),
});
};

const mergeConfiguration = (configuration) => {
const mergedConfiguration = Object.assign({}, defaultConfig, configuration);
return Object.assign({
'feature tag': mergedConfiguration['Feature'],
'scenario tag': mergedConfiguration['Scenario'],
'DocString line': mergedConfiguration['DocString'],
}, mergedConfiguration);
};

Expand Down Expand Up @@ -77,13 +101,30 @@ const findKey = (obj, predicate) => {
return Object.keys(obj).find((key) => predicate(obj[key]));
};

const testStep = (feature, configuration, testNode) => (step) => {
const testDocString = (parseDocString, test) => (step) => {
const docString = parseDocString(step.argument);
const getLines = (docString) => (docString || {lines: []}).lines;
const docStringErrors = test('DocString')(docString);
if (docStringErrors.length > 0) {
return docStringErrors;
}
return getLines(docString).map(test('DocString line'));
};

const getStepType = (feature, configuration) => (step) => {
const keyword = step.keyword;
let stepType = findKey(languageMapping[feature.language], (values) => {
const stepType = findKey(languageMapping[feature.language], (values) => {
return values instanceof Array && values.indexOf(keyword) !== -1;
});
stepType = stepType in configuration ? stepType : 'Step';
return testNode(stepType)(step);
return stepType in configuration ? stepType : 'Step';
};

const testStep = (getStepType, testDocstring, testNode) => (step) => {
const stepType = getStepType(step);
return applyOver([
testNode(stepType),
testDocstring,
])(step);
};

const testFeature = (testStep, test) => {
Expand Down Expand Up @@ -120,14 +161,18 @@ const testFeature = (testStep, test) => {
]);
};

const run = (feature, unused, configuration) => {
const run = (feature, {lines}, configuration) => {
if (Object.keys(feature).length === 0) {
return [];
}
const test = checkNodeIndentation(mergeConfiguration(configuration));

return testFeature(
testStep(feature, configuration, test),
testStep(
getStepType(feature, configuration),
testDocString(parseDocString(lines), test),
test
),
test
)(feature);
};
Expand Down
17 changes: 17 additions & 0 deletions test/rules/indentation/CorrectIndentationDocStrings.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
Feature: My team is never consistent with anything

Scenario: Quotes are indented 6 spaces
When I use this docstring:
"""
This is
a docstring
"""
Then the rule should pass

Scenario: Content of Docstring is indented at least 6 spaces
When I use this docstring:
"""
This is
a docstring
"""
Then the rule should pass
17 changes: 17 additions & 0 deletions test/rules/indentation/WrongIndentationDocStrings.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
Feature: My team is never consistent with anything

Scenario: Quotes are indented 6 spaces
When I use this docstring:
"""
This is
a docstring
"""
Then the rule should pass

Scenario: Content of Docstring is indente at least 6 spaces
When I use this docstring:
"""
This is
a docstring
"""
Then the rule should pass
31 changes: 27 additions & 4 deletions test/rules/indentation/indentation.js
Original file line number Diff line number Diff line change
Expand Up @@ -285,8 +285,31 @@ describe('Indentation rule', () => {
}]);
});

/* TODO: add tests for partial configurations and fallbacks
* (eg rule for Step is used for Given, Then etc is rule for Given, Then, etc
* has not been specified)
*/
context('DocString', () => {
it('Correct indentation', function() {
runTest('indentation/CorrectIndentationDocStrings.feature', {
DocString: 4,
}, []);
});

it('Wrong indentation', function() {
runTest('indentation/WrongIndentationDocStrings.feature', {
DocString: 4,
}, [{
messageElements: {element: 'DocString', expected: 4, actual: 2},
rule: ruleName,
location: {
line: 5,
column: 3,
},
}, {
messageElements: {element: 'DocString line', expected: 4, actual: 2},
rule: ruleName,
location: {
line: 14,
column: 3,
},
}]);
});
});
});