Skip to content

Commit

Permalink
Add support for tags on features (close #7)
Browse files Browse the repository at this point in the history
  • Loading branch information
jbpros committed Feb 3, 2012
1 parent c5dcf07 commit 01cfa8c
Show file tree
Hide file tree
Showing 10 changed files with 99 additions and 34 deletions.
2 changes: 1 addition & 1 deletion features/cucumber-tck
Submodule cucumber-tck updated from 5d3b3d to 7b5b64
15 changes: 5 additions & 10 deletions features/step_definitions/cucumber_js_mappings.rb
Original file line number Diff line number Diff line change
Expand Up @@ -134,22 +134,17 @@ def write_passing_hook hook_type
EOF
end

def write_scenario
provide_cycle_logging_facilities
append_step_definition("a step", "this.logCycleEvent('step');\ncallback();")
scenario_with_steps("A scenario", "Given a step")
end
def write_scenario options = {}
tags = options[:with_tags] || []

def write_passing_scenario_with_tags(tags)
tags = [tags] unless tags.respond_to? :any?
@next_step_count ||= 0
step_name = nth_step_name @next_step_count += 1
tags_definition = tags.any? ? "\n #{tags.join(' ')}" : ""
provide_cycle_logging_facilities
append_step_definition(step_name, "this.logCycleEvent('#{step_name}');\ncallback();")
append_to_feature <<-EOF
#{tags.join(' ')}
Scenario: scenario tagged with #{tags.join(', ')}
#{tags_definition}
Scenario: scenario #{"tagged with " + tags.join(', ') if tags.any?}
Given #{step_name}
EOF
end
Expand Down
14 changes: 12 additions & 2 deletions features/step_definitions/cucumber_steps.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@ var cucumberSteps = function() {
callback();
});

this.Given(/^a scenario without any tags$/, function(callback) {
this.addPassingScenarioWithoutTags();
callback();
});

Given(/^a scenario tagged with "([^"]*)"$/, function(tag, callback) {
this.addPassingScenarioWithTags([tag]);
callback();
Expand All @@ -87,11 +92,16 @@ var cucumberSteps = function() {
callback();
});

this.Given(/^a scenario tagged with "([^"]*)", "([^"]*)" and "([^"]*)"$/, function(tag1, tag2, tag3, callback) {
Given(/^a scenario tagged with "([^"]*)", "([^"]*)" and "([^"]*)"$/, function(tag1, tag2, tag3, callback) {
this.addPassingScenarioWithTags([tag1, tag2, tag3]);
callback();
});

Given(/^a feature tagged with "([^"]*)"$/, function(tag, callback) {
this.createEmptyFeature({tags: [tag]});
callback();
});

When(/^Cucumber executes the scenario$/, function(callback) {
this.runFeature({}, callback);
});
Expand Down Expand Up @@ -225,7 +235,7 @@ callback();\
callback();
});

Then(/^only the first scenario is executed$/, function(callback) {
Then(/^(?:only the first|the) scenario is executed$/, function(callback) {
this.assertExecutedNumberedScenarios(1);
callback();
});
Expand Down
11 changes: 10 additions & 1 deletion features/step_definitions/cucumber_world.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,17 @@ proto.addPassingScenarioWithTags = function addPassingScenarioWithTags(tags) {
});\n";
};

proto.createEmptyFeature = function createEmptyFeature() {
proto.addPassingScenarioWithoutTags = function addPassingScenarioWithoutTags() {
this.addPassingScenarioWithTags([]);
};

proto.createEmptyFeature = function createEmptyFeature(options) {
options = options || {};
tags = options['tags'] || [];

if (!this.emptyFeatureReady) {
if (tags.length > 0)
this.featureSource += tags.join(' ') + "\n";
this.featureSource += "Feature: A feature\n\n";
this.emptyFeatureReady = true;
}
Expand Down
11 changes: 9 additions & 2 deletions lib/cucumber/ast/assembler.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,15 @@ var Assembler = function(features, filter) {
return revealedTags;
},

applyCurrentFeatureTagsToElement: function applyCurrentFeatureTagsToElement(element) {
var currentFeature = self.getCurrentFeature();
var featureTags = currentFeature.getTags();
element.addTags(featureTags);
},

applyStashedTagsToElement: function applyStashedTagsToElement(element) {
var revealedTags = self.revealTags();
element.setTags(revealedTags);
element.addTags(revealedTags);
},

insertBackground: function insertBackground(background) {
Expand All @@ -67,6 +73,7 @@ var Assembler = function(features, filter) {
},

insertScenario: function insertScenario(scenario) {
self.applyCurrentFeatureTagsToElement(scenario);
self.applyStashedTagsToElement(scenario);
self.setCurrentScenarioOrBackground(scenario);
if (filter.isScenarioEnrolled(scenario)) {
Expand All @@ -88,4 +95,4 @@ var Assembler = function(features, filter) {
return self;
};

module.exports = Assembler;
module.exports = Assembler;
4 changes: 2 additions & 2 deletions lib/cucumber/ast/feature.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ var Feature = function(keyword, name, description, line) {
return scenarios.getLast();
},

setTags: function setTags(newTags) {
tags = newTags;
addTags: function setTags(newTags) {
tags = tags.concat(newTags);
},

getTags: function getTags() {
Expand Down
4 changes: 2 additions & 2 deletions lib/cucumber/ast/scenario.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ var Scenario = function(keyword, name, description, line) {
return steps.getLast();
},

setTags: function setTags(newTags) {
tags = newTags;
addTags: function setTags(newTags) {
tags = tags.concat(newTags);
},

getTags: function getTags() {
Expand Down
42 changes: 36 additions & 6 deletions spec/cucumber/ast/assembler_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,37 @@ describe("Cucumber.Ast.Assembler", function() {
});
});

describe("applyCurrentFeatureTagsToElement()", function() {
var feature, featureTags, element;

beforeEach(function() {
element = createSpyWithStubs("AST element", {addTags: null});
featureTags = createSpy("feature tags");
feature = createSpyWithStubs("current feature", {getTags: featureTags});
spyOn(assembler, 'getCurrentFeature').andReturn(feature);
});

it("gets the current feature", function() {
assembler.applyCurrentFeatureTagsToElement(element);
expect(assembler.getCurrentFeature).toHaveBeenCalled();
});

it("gets the feature tags", function() {
assembler.applyCurrentFeatureTagsToElement(element);
expect(feature.getTags).toHaveBeenCalled();
});

it("adds the feature tags to the element", function() {
assembler.applyCurrentFeatureTagsToElement(element);
expect(element.addTags).toHaveBeenCalledWith(featureTags);
});
});

describe("applyStashedTagsToElement()", function() {
var element, revealedTags;

beforeEach(function() {
element = createSpyWithStubs("any AST element accepting tags", {setTags: null});
element = createSpyWithStubs("any AST element accepting tags", {addTags: null});
revealedTags = createSpy("revealed tags");
spyOn(assembler, 'revealTags').andReturn(revealedTags);
});
Expand All @@ -113,9 +139,9 @@ describe("Cucumber.Ast.Assembler", function() {
expect(assembler.revealTags).toHaveBeenCalled();
});

it("sets the tags to the element", function() {
it("adds the tags to the element", function() {
assembler.applyStashedTagsToElement(element);
expect(element.setTags).toHaveBeenCalledWith(revealedTags);
expect(element.addTags).toHaveBeenCalledWith(revealedTags);
});
});

Expand Down Expand Up @@ -217,12 +243,18 @@ describe("Cucumber.Ast.Assembler", function() {
beforeEach(function() {
scenario = createSpy("scenario");
currentFeature = createSpyWithStubs("current feature", {addScenario: null});
spyOn(assembler, 'applyStashedTagsToElement');
spyOnStub(filter, 'isScenarioEnrolled');
spyOn(assembler, 'applyStashedTagsToElement');
spyOn(assembler, 'applyCurrentFeatureTagsToElement');
spyOn(assembler, 'getCurrentFeature').andReturn(currentFeature);
spyOn(assembler, 'setCurrentScenarioOrBackground');
});

it("applies the current feature tags to the scenario", function() {
assembler.insertScenario(scenario);
expect(assembler.applyCurrentFeatureTagsToElement).toHaveBeenCalledWith(scenario);
});

it("applies the stashed tags to the scenario", function() {
assembler.insertScenario(scenario);
expect(assembler.applyStashedTagsToElement).toHaveBeenCalledWith(scenario);
Expand All @@ -239,7 +271,6 @@ describe("Cucumber.Ast.Assembler", function() {
});

describe("when the scenario is enrolled", function() {

beforeEach(function() {
filter.isScenarioEnrolled.andReturn(true);
});
Expand All @@ -256,7 +287,6 @@ describe("Cucumber.Ast.Assembler", function() {
});

describe("when the scenario is not enrolled", function() {

beforeEach(function() {
filter.isScenarioEnrolled.andReturn(false);
});
Expand Down
15 changes: 11 additions & 4 deletions spec/cucumber/ast/feature_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,18 @@ describe("Cucumber.Ast.Feature", function() {
});
});

describe("getTags() [setTags()]", function() {
describe("getTags() [addTags()]", function() {
it("returns an empty set when no tags were added", function() {
expect(feature.getTags()).toEqual([]);
});

it("returns the tags", function() {
var tags = createSpy("tags");
feature.setTags(tags);
expect(feature.getTags()).toBe(tags);
var tag1 = createSpy("tag 1");
var tag2 = createSpy("tag 2");
var tag3 = createSpy("tag 3");
feature.addTags([tag1, tag2]);
feature.addTags([tag3]);
expect(feature.getTags()).toEqual([tag1, tag2, tag3]);
});
});

Expand Down
15 changes: 11 additions & 4 deletions spec/cucumber/ast/scenario_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,18 @@ describe("Cucumber.Ast.Scenario", function() {
});
});

describe("getTags() [setTags()]", function() {
describe("getTags() [addTags()]", function() {
it("returns an empty set when no tags were added", function() {
expect(scenario.getTags()).toEqual([]);
});

it("returns the tags", function() {
var tags = createSpy("tags");
scenario.setTags(tags);
expect(scenario.getTags()).toBe(tags);
var tag1 = createSpy("tag 1");
var tag2 = createSpy("tag 2");
var tag3 = createSpy("tag 3");
scenario.addTags([tag1, tag2]);
scenario.addTags([tag3]);
expect(scenario.getTags()).toEqual([tag1, tag2, tag3]);
});
});

Expand Down

0 comments on commit 01cfa8c

Please sign in to comment.