Skip to content

Commit

Permalink
Fix matching groups in step definition snippets (close #42)
Browse files Browse the repository at this point in the history
This was due to a bug in underscore.string library's `count()` function. We rolled our own.
  • Loading branch information
jbpros committed Feb 4, 2012
1 parent 7a84265 commit 4492696
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 18 deletions.
12 changes: 10 additions & 2 deletions features/step_definition_snippets.feature
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ Feature: step definition snippets
Given a scenario with:
"""
Given I am a happy veggie \o/
When I type -[]{}()*+?.,\^$|#/
When I type -[]{}()*+?.\^$|#/
"""
When Cucumber executes the scenario
Then a "Given" step definition snippet for /^I am a happy veggie \\o\/$/ is suggested
Then a "When" step definition snippet for /^I type \-\[\]\{\}\(\)\*\+\?\.\,\\\^\$\|\#\/$/ is suggested
Then a "When" step definition snippet for /^I type \-\[\]\{\}\(\)\*\+\?\.\\\^\$\|\#\/$/ is suggested

Scenario: step matching groups
Given a scenario with:
Expand All @@ -17,3 +17,11 @@ Feature: step definition snippets
"""
When Cucumber executes the scenario
Then a "Given" step definition snippet for /^I have (\d+) "([^"]*)" cucumbers$/ with 2 parameters is suggested
Scenario: multiple matching groups
Given a scenario with:
"""
Given I have some "hekiri", "wild" and "regular" cucumbers
"""
When Cucumber executes the scenario
Then a "Given" step definition snippet for /^I have some "([^"]*)", "([^"]*)" and "([^"]*)" cucumbers$/ with 3 parameters is suggested
9 changes: 4 additions & 5 deletions lib/cucumber/support_code/step_definition_snippet_builder.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
var _ = require('underscore');
var _s = require('underscore.string');

var StepDefinitionSnippetBuilder = function(step) {
var Cucumber = require('../../cucumber');
Expand Down Expand Up @@ -67,9 +66,9 @@ var StepDefinitionSnippetBuilder = function(step) {
countStepDefinitionPatternMatchingGroups: function countStepDefinitionPatternMatchingGroups() {
var stepDefinitionPattern = self.buildStepDefinitionPattern();
var numberMatchingGroupCount =
_s.count(stepDefinitionPattern, StepDefinitionSnippetBuilder.NUMBER_MATCHING_GROUP);
Cucumber.Util.String.count(stepDefinitionPattern, StepDefinitionSnippetBuilder.NUMBER_MATCHING_GROUP);
var quotedStringMatchingGroupCount =
_s.count(stepDefinitionPattern, StepDefinitionSnippetBuilder.QUOTED_STRING_MATCHING_GROUP);
Cucumber.Util.String.count(stepDefinitionPattern, StepDefinitionSnippetBuilder.QUOTED_STRING_MATCHING_GROUP);
var count = numberMatchingGroupCount + quotedStringMatchingGroupCount;
return count;
},
Expand Down Expand Up @@ -97,9 +96,9 @@ StepDefinitionSnippetBuilder.PATTERN_END = '$/';
StepDefinitionSnippetBuilder.CONTEXT_STEP_DEFINITION_FUNCTION_NAME = 'Given';
StepDefinitionSnippetBuilder.EVENT_STEP_DEFINITION_FUNCTION_NAME = 'When';
StepDefinitionSnippetBuilder.OUTCOME_STEP_DEFINITION_FUNCTION_NAME = 'Then';
StepDefinitionSnippetBuilder.NUMBER_PATTERN = /\d+/i;
StepDefinitionSnippetBuilder.NUMBER_PATTERN = /\d+/gi;
StepDefinitionSnippetBuilder.NUMBER_MATCHING_GROUP = '(\\d+)';
StepDefinitionSnippetBuilder.QUOTED_STRING_PATTERN = /"[^"]*"/i;
StepDefinitionSnippetBuilder.QUOTED_STRING_PATTERN = /"[^"]*"/gi;
StepDefinitionSnippetBuilder.QUOTED_STRING_MATCHING_GROUP = '"([^"]*)"';
StepDefinitionSnippetBuilder.FUNCTION_PARAMETER_SEPARATOR = ', ';
module.exports = StepDefinitionSnippetBuilder;
1 change: 1 addition & 0 deletions lib/cucumber/util.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
var Util = {};
Util.Arguments = require('./util/arguments');
Util.RegExp = require('./util/reg_exp');
Util.String = require('./util/string');
module.exports = Util;
2 changes: 1 addition & 1 deletion lib/cucumber/util/reg_exp.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ var RegExp = {
}
};

RegExp.ESCAPE_PATTERN = /[-[\]{}()*+?.,\\^$|#\n\/]/g;
RegExp.ESCAPE_PATTERN = /[-[\]{}()*+?.\\^$|#\n\/]/g;
RegExp.ESCAPE_REPLACEMENT = "\\$&";
module.exports = RegExp;
7 changes: 7 additions & 0 deletions lib/cucumber/util/string.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
var String = {
count: function count(hayStack, needle) {
var splitHayStack = hayStack.split(needle);
return splitHayStack.length - 1;
}
};
module.exports = String;
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
, "browserify" : "1.8.1"
, "nopt" : "1.0.10"
, "underscore" : "1.2.2"
, "underscore.string" : "2.0.0"
, "rimraf" : "1.0.8"
, "mkdirp" : "0.2.1"
, "cucumber-html": "0.2.0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,14 +153,14 @@ describe("Cucumber.SupportCode.StepDefinitionSnippetBuilder", function() {
it("replaces numbers with matching groups", function() {
snippetBuilder.parameterizeStepName(stepName);
expect(stepName.replace).toHaveBeenCalled();
expect(stepName.replace).toHaveBeenCalledWithRegExpAsNthParameter(/\d+/i, 1);
expect(stepName.replace).toHaveBeenCalledWithRegExpAsNthParameter(/\d+/gi, 1);
expect(stepName.replace).toHaveBeenCalledWithValueAsNthParameter('(\\d+)', 2);
});

it("replaces quoted strings with matching groups", function() {
snippetBuilder.parameterizeStepName(stepName);
expect(parameterizedNumbersStepName.replace).toHaveBeenCalled();
expect(parameterizedNumbersStepName.replace).toHaveBeenCalledWithRegExpAsNthParameter(/"[^"]*"/i, 1);
expect(parameterizedNumbersStepName.replace).toHaveBeenCalledWithRegExpAsNthParameter(/"[^"]*"/gi, 1);
expect(parameterizedNumbersStepName.replace).toHaveBeenCalledWithValueAsNthParameter('"([^"]*)"', 2);
});

Expand Down Expand Up @@ -254,7 +254,7 @@ describe("Cucumber.SupportCode.StepDefinitionSnippetBuilder", function() {
count = numberCount + stringCount
stepDefinitionPattern = createSpy("step definition pattern");
spyOn(snippetBuilder, 'buildStepDefinitionPattern').andReturn(stepDefinitionPattern);
spyOn(_s, 'count').andReturnSeveral([numberCount, stringCount]);
spyOn(Cucumber.Util.String, 'count').andReturnSeveral([numberCount, stringCount]);
});

it("builds the step definition pattern", function() {
Expand All @@ -264,12 +264,12 @@ describe("Cucumber.SupportCode.StepDefinitionSnippetBuilder", function() {

it("counts the number matching groups inside the pattern", function() {
snippetBuilder.countStepDefinitionPatternMatchingGroups();
expect(_s.count).toHaveBeenCalledWith(stepDefinitionPattern, '(\\d+)');
expect(Cucumber.Util.String.count).toHaveBeenCalledWith(stepDefinitionPattern, '(\\d+)');
});

it("counts the quoted string matching groups inside the pattern", function() {
snippetBuilder.countStepDefinitionPatternMatchingGroups();
expect(_s.count).toHaveBeenCalledWith(stepDefinitionPattern, '"([^"]*)"');
expect(Cucumber.Util.String.count).toHaveBeenCalledWith(stepDefinitionPattern, '"([^"]*)"');
});

it("returns the sum of both counts", function() {
Expand Down
4 changes: 0 additions & 4 deletions spec/cucumber/util/reg_exp_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,6 @@ describe("Cucumber.Util.RegExp", function() {
expect(escapeString(".")).toBe("\\.");
});

it("escapes commas", function() {
expect(escapeString(",")).toBe("\\,");
});

it("escapes backslashes", function() {
expect(escapeString("\\")).toBe("\\\\");
});
Expand Down
24 changes: 24 additions & 0 deletions spec/cucumber/util/string_spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
require('../../support/spec_helper');

describe("Cucumber.Util.String", function() {
var Cucumber = requireLib('cucumber');

describe("count()", function() {
var hayStack, needle;

it("returns 0 when the needle is not found", function() {
var count = Cucumber.Util.String.count("cucumber", "a");
expect(count).toBe(0);
});

it("returns 1 when the needle was found once", function() {
var count = Cucumber.Util.String.count("cucumber", "b");
expect(count).toBe(1);
});

it("returns 2 when the needle was found twice", function() {
var count = Cucumber.Util.String.count("cucumber", "c");
expect(count).toBe(2);
});
});
});

0 comments on commit 4492696

Please sign in to comment.