Skip to content

Commit 93705c5

Browse files
author
charlierudolph
committed
fix scenario description
closes #647
1 parent 5960f3e commit 93705c5

File tree

6 files changed

+113
-2
lines changed

6 files changed

+113
-2
lines changed

features/json_formatter.feature

+12
Original file line numberDiff line numberDiff line change
@@ -1647,3 +1647,15 @@ Feature: JSON Formatter
16471647
}
16481648
]
16491649
"""
1650+
1651+
Scenario: output JSON for a feature with one scenario with a description
1652+
Given a file named "features/a.feature" with:
1653+
"""
1654+
Feature: some feature
1655+
Scenario: some scenario
1656+
Some description
1657+
1658+
Given an undefined step
1659+
"""
1660+
When I run cucumber.js with `-f json`
1661+
Then the json output's first scenario has the description "Some description"

features/step_definitions/json_output_steps.js

+5
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,11 @@ var jsonOutputSteps = function jsonOutputSteps() {
185185
assert.equal(scenario.name, name);
186186
});
187187

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

190195
module.exports = jsonOutputSteps;

lib/cucumber/ast/feature.js

+9
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,15 @@ function Feature(data, scenarios) {
2121
}
2222
},
2323

24+
getScenarioDescriptionByLines: function getScenarioDescriptionByLines(lines) {
25+
var element = _.find(data.children, function(node) {
26+
return _.includes(lines, node.location.line);
27+
});
28+
if (element) {
29+
return element.description;
30+
}
31+
},
32+
2433
getScenarioKeyword: function() {
2534
return Gherkin.DIALECTS[self.getLanguage()].scenario;
2635
},

lib/cucumber/ast/scenario.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ function Scenario(data) {
1313
},
1414

1515
getDescription: function getDescription() {
16-
return data.description;
16+
return self.getFeature().getScenarioDescriptionByLines(self.getLines());
1717
},
1818

1919
getFeature: function getFeature() {

spec/cucumber/ast/feature_spec.js

+79
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,85 @@ describe("Cucumber.Ast.Feature", function () {
8888
});
8989
});
9090

91+
describe('getScenarioDescriptionByLines()', function() {
92+
describe('from a scenario', function() {
93+
describe('with a description', function() {
94+
beforeEach(function() {
95+
var source =
96+
'Feature: Foo\n' +
97+
' Scenario: Bar\n' +
98+
' My scenario description\n' +
99+
'\n' +
100+
' Then b\n';
101+
var gherkinDocument = new Gherkin.Parser().parse(source);
102+
feature = Cucumber.Ast.Feature(gherkinDocument.feature, []);
103+
});
104+
105+
it('returns the keyword', function() {
106+
var description = feature.getScenarioDescriptionByLines([2]);
107+
expect(description.trim()).toEqual('My scenario description');
108+
});
109+
});
110+
111+
describe('without a description', function() {
112+
beforeEach(function() {
113+
var source =
114+
'Feature: Foo\n' +
115+
' Scenario: Bar\n' +
116+
' Then b\n';
117+
var gherkinDocument = new Gherkin.Parser().parse(source);
118+
feature = Cucumber.Ast.Feature(gherkinDocument.feature, []);
119+
});
120+
121+
it('returns the keyword', function() {
122+
expect(feature.getScenarioDescriptionByLines([2])).toEqual(undefined);
123+
});
124+
});
125+
});
126+
127+
describe('from an example in a scenario outline', function() {
128+
describe('with a description', function() {
129+
beforeEach(function() {
130+
var source =
131+
'Feature: Foo\n' +
132+
' Scenario Outline: Bar\n' +
133+
' My scenario outline description\n' +
134+
'\n' +
135+
' When <what>\n' +
136+
'\n' +
137+
' Examples:\n' +
138+
' | what |\n' +
139+
' | b |';
140+
var gherkinDocument = new Gherkin.Parser().parse(source);
141+
feature = Cucumber.Ast.Feature(gherkinDocument.feature, []);
142+
});
143+
144+
it('returns the keyword', function() {
145+
var description = feature.getScenarioDescriptionByLines([2]);
146+
expect(description.trim()).toEqual('My scenario outline description');
147+
});
148+
});
149+
150+
describe('without a description', function() {
151+
beforeEach(function() {
152+
var source =
153+
'Feature: Foo\n' +
154+
' Scenario Outline: Bar\n' +
155+
' When <what>\n' +
156+
' Examples:\n' +
157+
' | what |\n' +
158+
' | b |';
159+
var gherkinDocument = new Gherkin.Parser().parse(source);
160+
feature = Cucumber.Ast.Feature(gherkinDocument.feature, []);
161+
});
162+
163+
it('returns the keyword', function() {
164+
expect(feature.getScenarioDescriptionByLines([2])).toEqual(undefined);
165+
});
166+
});
167+
});
168+
});
169+
91170
describe("getKeyword()", function () {
92171
it("returns the keyword of the feature", function () {
93172
expect(feature.getKeyword()).toEqual('keyword');

spec/cucumber/ast/scenario_spec.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ describe("Cucumber.Ast.Scenario", function () {
66

77
beforeEach(function () {
88
var scenarioData = {
9-
description: 'description',
109
locations: [{path: 'path', line: 1}, {line: 2}],
1110
name: 'name',
1211
steps: [
@@ -67,6 +66,13 @@ describe("Cucumber.Ast.Scenario", function () {
6766
});
6867

6968
describe("getDescription()", function () {
69+
var feature;
70+
71+
beforeEach(function() {
72+
feature = createSpyWithStubs('feature', {getScenarioDescriptionByLines: 'description'});
73+
scenario.setFeature(feature);
74+
});
75+
7076
it("returns the description of the scenario", function () {
7177
expect(scenario.getDescription()).toEqual('description');
7278
});

0 commit comments

Comments
 (0)