Skip to content

Commit

Permalink
Decouple pattern from regexp in step definition (#48)
Browse files Browse the repository at this point in the history
  • Loading branch information
jbpros committed Apr 16, 2012
1 parent 2a3c5c8 commit 91d68d9
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 21 deletions.
12 changes: 9 additions & 3 deletions lib/cucumber/support_code/step_definition.js
@@ -1,10 +1,15 @@
var UNKNOWN_STEP_FAILURE_MESSAGE = "Step failure";

var StepDefinition = function(regexp, code) {
var StepDefinition = function(pattern, code) {
var Cucumber = require('../../cucumber');

var self = {
getPatternRegexp: function getPatternRegexp() {
return pattern;
},

matchesStepName: function matchesStepName(stepName) {
var regexp = self.getPatternRegexp();
return regexp.test(stepName);
},

Expand Down Expand Up @@ -36,8 +41,9 @@ var StepDefinition = function(regexp, code) {
},

buildInvocationParameters: function buildInvocationParameters(step, callback) {
var stepName = step.getName();
var parameters = regexp.exec(stepName);
var stepName = step.getName();
var patternRegexp = self.getPatternRegexp();
var parameters = patternRegexp.exec(stepName);
parameters.shift();
if (step.hasAttachment()) {
var attachmentContents = step.getAttachmentContents();
Expand Down
51 changes: 33 additions & 18 deletions spec/cucumber/support_code/step_definition_spec.js
Expand Up @@ -2,34 +2,42 @@ require('../../support/spec_helper');

describe("Cucumber.SupportCode.StepDefinition", function() {
var Cucumber = requireLib('cucumber');
var stepDefinition, stepRegexp, stepDefinitionCode;
var stepDefinition, pattern, stepDefinitionCode;

beforeEach(function() {
stepRegexp = createSpyWithStubs("Step regexp", {test:null});
pattern = createSpyWithStubs("pattern", {test:null});
stepDefinitionCode = createSpy("step definition code");
stepDefinition = Cucumber.SupportCode.StepDefinition(stepRegexp, stepDefinitionCode);
stepDefinition = Cucumber.SupportCode.StepDefinition(pattern, stepDefinitionCode);
});

describe("getPatternRegexp()", function() {
it("returns the pattern itself", function() {
expect(stepDefinition.getPatternRegexp()).toBe(pattern);
});
});

describe("matchesStepName()", function() {
var stepName;
var patternRegexp, stepName;

beforeEach(function() {
stepName = createSpy("Step name to match");
stepName = createSpy("step name");
matchResult = createSpy("step match result (boolean)");
patternRegexp = createSpyWithStubs("pattern regexp", {test: matchResult});
spyOn(stepDefinition, 'getPatternRegexp').andReturn(patternRegexp);
});

it("tests the string against the step name", function() {
it("gets the pattern regexp", function(){
stepDefinition.matchesStepName(stepName);
expect(stepRegexp.test).toHaveBeenCalledWith(stepName);
expect(stepDefinition.getPatternRegexp).toHaveBeenCalled();
});

it("returns true when the step name matches the step definition regexp", function() {
stepRegexp.test.andReturn(true);
expect(stepDefinition.matchesStepName(stepName)).toBeTruthy();
it("tests the string against the step name", function() {
stepDefinition.matchesStepName(stepName);
expect(patternRegexp.test).toHaveBeenCalledWith(stepName);
});

it("returns false when the step name does not match the step definition regexp", function() {
stepRegexp.test.andReturn(false);
expect(stepDefinition.matchesStepName(stepName)).toBeFalsy();
it("returns the match result", function() {
expect(stepDefinition.matchesStepName(stepName)).toBe(matchResult);
});
});

Expand Down Expand Up @@ -160,26 +168,33 @@ describe("Cucumber.SupportCode.StepDefinition", function() {
});

describe("buildInvocationParameters()", function() {
var step, stepName, stepAttachment, stepAttachmentContents;
var patternRegexp, step, stepName, stepAttachment, stepAttachmentContents;
var matches, callback;

beforeEach(function() {
stepName = createSpy("step name to match");
matches = createSpyWithStubs("matches", {shift: null, push: null});
patternRegexp = createSpyWithStubs("pattern regexp", {test: matches});
stepAttachmentContents = createSpy("step attachment contents");
step = createSpyWithStubs("step", {hasAttachment: null, getName: stepName, getAttachmentContents: stepAttachmentContents});
matches = createSpyWithStubs("matches", {shift: null, push: null});
callback = createSpy("callback");
spyOnStub(stepRegexp, 'exec').andReturn(matches);
spyOn(stepDefinition, 'getPatternRegexp').andReturn(patternRegexp);
spyOnStub(patternRegexp, 'exec').andReturn(matches);
});

it("gets the step name", function() {
stepDefinition.buildInvocationParameters(step, callback);
expect(step.getName).toHaveBeenCalled();
});

it("executes the step regexp against the step name", function() {
it("gets the pattern regexp", function() {
stepDefinition.buildInvocationParameters(step, callback);
expect(stepDefinition.getPatternRegexp).toHaveBeenCalled();
});

it("executes the pattern regexp against the step name", function() {
stepDefinition.buildInvocationParameters(step, callback);
expect(stepRegexp.exec).toHaveBeenCalledWith(stepName);
expect(patternRegexp.exec).toHaveBeenCalledWith(stepName);
});

it("removes the whole matched string of the regexp result array (to only keep matching groups)", function() {
Expand Down

0 comments on commit 91d68d9

Please sign in to comment.