From 729f92748f9ad21b54dfb2a4d3a7aadae0233054 Mon Sep 17 00:00:00 2001 From: Julien Biezemans Date: Tue, 9 Oct 2012 09:49:43 +0200 Subject: [PATCH] Add ^ prefix and $ suffix to string-based step definition regexps (#77) --- lib/cucumber/support_code/step_definition.js | 6 ++++++ spec/cucumber/support_code/step_definition_spec.js | 4 ++-- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/lib/cucumber/support_code/step_definition.js b/lib/cucumber/support_code/step_definition.js index 92a276373..0e61b0e9e 100644 --- a/lib/cucumber/support_code/step_definition.js +++ b/lib/cucumber/support_code/step_definition.js @@ -8,6 +8,10 @@ var StepDefinition = function (pattern, code) { var regexpString = pattern .replace(StepDefinition.QUOTED_DOLLAR_PARAMETER_REGEXP, StepDefinition.QUOTED_DOLLAR_PARAMETER_SUBSTITUTION) .replace(StepDefinition.DOLLAR_PARAMETER_REGEXP, StepDefinition.DOLLAR_PARAMETER_SUBSTITUTION); + regexpString = + StepDefinition.STRING_PATTERN_REGEXP_PREFIX + + regexpString + + StepDefinition.STRING_PATTERN_REGEXP_SUFFIX; regexp = RegExp(regexpString); } else @@ -88,6 +92,8 @@ StepDefinition.DOLLAR_PARAMETER_REGEXP = /\$[a-zA-Z_-]+/; StepDefinition.DOLLAR_PARAMETER_SUBSTITUTION = '(.*)'; StepDefinition.QUOTED_DOLLAR_PARAMETER_REGEXP = /"\$[a-zA-Z_-]+"/; StepDefinition.QUOTED_DOLLAR_PARAMETER_SUBSTITUTION = '"([^"]*)"'; +StepDefinition.STRING_PATTERN_REGEXP_PREFIX = '^'; +StepDefinition.STRING_PATTERN_REGEXP_SUFFIX = '$'; StepDefinition.UNKNOWN_STEP_FAILURE_MESSAGE = "Step failure"; module.exports = StepDefinition; diff --git a/spec/cucumber/support_code/step_definition_spec.js b/spec/cucumber/support_code/step_definition_spec.js index 4823e7b4e..76ca79cd6 100644 --- a/spec/cucumber/support_code/step_definition_spec.js +++ b/spec/cucumber/support_code/step_definition_spec.js @@ -27,7 +27,7 @@ describe("Cucumber.SupportCode.StepDefinition", function () { beforeEach(function () { regexp = createSpy("regexp"); - regexpString = createSpy("regexp string"); + regexpString = "regexp string"; quotedDollarParameterSubstitutedPattern = createSpyWithStubs("quoted dollar param substituted pattern", {replace: regexpString}); spyOnStub(pattern, 'replace').andReturn(quotedDollarParameterSubstitutedPattern); global.RegExp.andReturn(regexp); @@ -45,7 +45,7 @@ describe("Cucumber.SupportCode.StepDefinition", function () { it("instantiates a new RegExp", function () { stepDefinition.getPatternRegexp(); - expect(global.RegExp).toHaveBeenCalledWith(regexpString); + expect(global.RegExp).toHaveBeenCalledWith("^" + regexpString + "$"); }); it("returns the new RegExp", function () {