Skip to content

Commit

Permalink
fix scenario description
Browse files Browse the repository at this point in the history
closes #647
  • Loading branch information
charlierudolph committed Sep 30, 2016
1 parent 5960f3e commit 93705c5
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 2 deletions.
12 changes: 12 additions & 0 deletions features/json_formatter.feature
Original file line number Diff line number Diff line change
Expand Up @@ -1647,3 +1647,15 @@ Feature: JSON Formatter
}
]
"""

Scenario: output JSON for a feature with one scenario with a description
Given a file named "features/a.feature" with:
"""
Feature: some feature
Scenario: some scenario
Some description
Given an undefined step
"""
When I run cucumber.js with `-f json`
Then the json output's first scenario has the description "Some description"
5 changes: 5 additions & 0 deletions features/step_definitions/json_output_steps.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,11 @@ var jsonOutputSteps = function jsonOutputSteps() {
assert.equal(scenario.name, name);
});

this.Then(/^the json output's first scenario has the description "([^"]*)"$/, function (description) {
var features = JSON.parse(this.lastRun.stdout);
assert.equal(features[0].elements[0].description.trim(), description);
});

};

module.exports = jsonOutputSteps;
9 changes: 9 additions & 0 deletions lib/cucumber/ast/feature.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,15 @@ function Feature(data, scenarios) {
}
},

getScenarioDescriptionByLines: function getScenarioDescriptionByLines(lines) {
var element = _.find(data.children, function(node) {
return _.includes(lines, node.location.line);
});
if (element) {
return element.description;
}
},

getScenarioKeyword: function() {
return Gherkin.DIALECTS[self.getLanguage()].scenario;
},
Expand Down
2 changes: 1 addition & 1 deletion lib/cucumber/ast/scenario.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ function Scenario(data) {
},

getDescription: function getDescription() {
return data.description;
return self.getFeature().getScenarioDescriptionByLines(self.getLines());
},

getFeature: function getFeature() {
Expand Down
79 changes: 79 additions & 0 deletions spec/cucumber/ast/feature_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,85 @@ describe("Cucumber.Ast.Feature", function () {
});
});

describe('getScenarioDescriptionByLines()', function() {
describe('from a scenario', function() {
describe('with a description', function() {
beforeEach(function() {
var source =
'Feature: Foo\n' +
' Scenario: Bar\n' +
' My scenario description\n' +
'\n' +
' Then b\n';
var gherkinDocument = new Gherkin.Parser().parse(source);
feature = Cucumber.Ast.Feature(gherkinDocument.feature, []);
});

it('returns the keyword', function() {
var description = feature.getScenarioDescriptionByLines([2]);
expect(description.trim()).toEqual('My scenario description');
});
});

describe('without a description', function() {
beforeEach(function() {
var source =
'Feature: Foo\n' +
' Scenario: Bar\n' +
' Then b\n';
var gherkinDocument = new Gherkin.Parser().parse(source);
feature = Cucumber.Ast.Feature(gherkinDocument.feature, []);
});

it('returns the keyword', function() {
expect(feature.getScenarioDescriptionByLines([2])).toEqual(undefined);
});
});
});

describe('from an example in a scenario outline', function() {
describe('with a description', function() {
beforeEach(function() {
var source =
'Feature: Foo\n' +
' Scenario Outline: Bar\n' +
' My scenario outline description\n' +
'\n' +
' When <what>\n' +
'\n' +
' Examples:\n' +
' | what |\n' +
' | b |';
var gherkinDocument = new Gherkin.Parser().parse(source);
feature = Cucumber.Ast.Feature(gherkinDocument.feature, []);
});

it('returns the keyword', function() {
var description = feature.getScenarioDescriptionByLines([2]);
expect(description.trim()).toEqual('My scenario outline description');
});
});

describe('without a description', function() {
beforeEach(function() {
var source =
'Feature: Foo\n' +
' Scenario Outline: Bar\n' +
' When <what>\n' +
' Examples:\n' +
' | what |\n' +
' | b |';
var gherkinDocument = new Gherkin.Parser().parse(source);
feature = Cucumber.Ast.Feature(gherkinDocument.feature, []);
});

it('returns the keyword', function() {
expect(feature.getScenarioDescriptionByLines([2])).toEqual(undefined);
});
});
});
});

describe("getKeyword()", function () {
it("returns the keyword of the feature", function () {
expect(feature.getKeyword()).toEqual('keyword');
Expand Down
8 changes: 7 additions & 1 deletion spec/cucumber/ast/scenario_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ describe("Cucumber.Ast.Scenario", function () {

beforeEach(function () {
var scenarioData = {
description: 'description',
locations: [{path: 'path', line: 1}, {line: 2}],
name: 'name',
steps: [
Expand Down Expand Up @@ -67,6 +66,13 @@ describe("Cucumber.Ast.Scenario", function () {
});

describe("getDescription()", function () {
var feature;

beforeEach(function() {
feature = createSpyWithStubs('feature', {getScenarioDescriptionByLines: 'description'});
scenario.setFeature(feature);
});

it("returns the description of the scenario", function () {
expect(scenario.getDescription()).toEqual('description');
});
Expand Down

0 comments on commit 93705c5

Please sign in to comment.