Permalink
Browse files

Fix matching groups in step definition snippets (close #42)

This was due to a bug in underscore.string library's `count()` function. We rolled our own.
  • Loading branch information...
1 parent 7a84265 commit 449269624544bdeb82613523bfdabb678fb8aae4 @jbpros jbpros committed Feb 4, 2012
@@ -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:
@@ -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
@@ -1,5 +1,4 @@
var _ = require('underscore');
-var _s = require('underscore.string');
var StepDefinitionSnippetBuilder = function(step) {
var Cucumber = require('../../cucumber');
@@ -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;
},
@@ -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;
View
@@ -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;
@@ -5,6 +5,6 @@ var RegExp = {
}
};
-RegExp.ESCAPE_PATTERN = /[-[\]{}()*+?.,\\^$|#\n\/]/g;
+RegExp.ESCAPE_PATTERN = /[-[\]{}()*+?.\\^$|#\n\/]/g;
RegExp.ESCAPE_REPLACEMENT = "\\$&";
module.exports = RegExp;
@@ -0,0 +1,7 @@
+var String = {
+ count: function count(hayStack, needle) {
+ var splitHayStack = hayStack.split(needle);
+ return splitHayStack.length - 1;
+ }
+};
+module.exports = String;
View
@@ -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"
@@ -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);
});
@@ -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() {
@@ -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() {
@@ -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("\\\\");
});
@@ -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.