Skip to content

Commit

Permalink
Stop polluting the global namespace with Given(), When() and Then() (c…
Browse files Browse the repository at this point in the history
…lose #2)
  • Loading branch information
jbpros committed Sep 6, 2011
1 parent 9e4d6ae commit 91ab361
Show file tree
Hide file tree
Showing 10 changed files with 104 additions and 127 deletions.
6 changes: 3 additions & 3 deletions features/cli.feature
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Feature: Command line interface
Given a file named "features/step_definitions/cucumber_steps.js" with:
"""
var cucumberSteps = function() {
When(/^a step is passing$/, function(callback) { callback(); });
this.When(/^a step is passing$/, function(callback) { callback(); });
};
module.exports = cucumberSteps;
"""
Expand Down Expand Up @@ -54,7 +54,7 @@ Feature: Command line interface
Given a file named "step_definitions/cucumber_steps.js" with:
"""
var cucumberSteps = function() {
When(/^a step is passing$/, function(callback) { callback(); });
this.When(/^a step is passing$/, function(callback) { callback(); });
};
module.exports = cucumberSteps;
"""
Expand All @@ -78,7 +78,7 @@ Feature: Command line interface
Given a file named "step_definitions/cucumber_steps.js" with:
"""
var cucumberSteps = function() {
When(/^a step is passing$/, function(callback) { callback(); });
this.When(/^a step is passing$/, function(callback) { callback(); });
};
module.exports = cucumberSteps;
"""
Expand Down
1 change: 1 addition & 0 deletions features/step_definitions/calculator_steps.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
var calculatorSteps = function(Calculator) {
var Given = When = Then = this.defineStep;
var calc;

function isNumberWithinRangeOfValue(number, range, value) {
Expand Down
6 changes: 3 additions & 3 deletions features/step_definitions/cli_steps.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ var cliSteps = function cliSteps() {
}
};

Given(/^a file named "(.*)" with:$/, function(filePath, fileContent, callback) {
this.Given(/^a file named "(.*)" with:$/, function(filePath, fileContent, callback) {
cleanseIfNeeded();
var absoluteFilePath = tmpPath(filePath);
var filePathParts = absoluteFilePath.split('/');
Expand All @@ -37,7 +37,7 @@ var cliSteps = function cliSteps() {
});
});

When(/^I run `cucumber.js(| .+)`$/, function(args, callback) {
this.When(/^I run `cucumber.js(| .+)`$/, function(args, callback) {
var initialCwd = process.cwd();
process.chdir(tmpDir);
var command = baseDir + "/bin/cucumber.js" + args;
Expand All @@ -52,7 +52,7 @@ var cliSteps = function cliSteps() {
});
});

Then(/^it should pass with exactly:$/, function(expectedOutput, callback) {
this.Then(/^it should pass with exactly:$/, function(expectedOutput, callback) {
var actualOutput = lastRun['stdout'];
if (actualOutput != expectedOutput)
throw new Error("Expected output to match the following:\n'" + expectedOutput + "'\nGot:\n'" + actualOutput + "'.");
Expand Down
4 changes: 2 additions & 2 deletions features/step_definitions/cucumber_js_mappings.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def write_mappings_for_calculator
append_support_code <<-EOF
var RpnCalculator = require('../support/rpn_calculator');
var calculatorSteps = require('./calculator_steps');
calculatorSteps(RpnCalculator);
calculatorSteps.call(this, RpnCalculator);
EOF
end

Expand Down Expand Up @@ -94,7 +94,7 @@ def failed_output
def append_step_definition(step_name, code)
indented_code = indent_code(code).rstrip
append_support_code <<-EOF
Given(/#{step_name}/, function(callback) {
this.defineStep(/#{step_name}/, function(callback) {
fs.writeFileSync("#{step_file(step_name)}", "");
#{indented_code}
});
Expand Down
7 changes: 5 additions & 2 deletions features/step_definitions/cucumber_steps.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
var cucumberSteps = function() {
var Given = When = Then = this.defineStep;

var shouldPrepare = true;
var featureSource;
var stepDefinitions;
Expand Down Expand Up @@ -66,7 +68,7 @@ var cucumberSteps = function() {

When(/^Cucumber runs the scenario with steps for a calculator$/, function(callback) {
RpnCalculator = require('../support/rpn_calculator');
supportCode = function() { require('./calculator_steps')(RpnCalculator) };
supportCode = function() { require('./calculator_steps').call(this, RpnCalculator) };
runFeatureWithSupportCodeSource(supportCode, callback);
});

Expand Down Expand Up @@ -127,7 +129,8 @@ var cucumberSteps = function() {

function runFeature(callback) {
var supportCode;
var supportCodeSource = "supportCode = function() {\n" + stepDefinitions + "};\n";
var supportCodeSource = "supportCode = function() {\n var Given = When = Then = this.defineStep;\n" +
stepDefinitions + "};\n";
eval(supportCodeSource);
runFeatureWithSupportCodeSource(supportCode, callback);
}
Expand Down
11 changes: 7 additions & 4 deletions features/step_definitions/legacy/cucumber_steps.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
var Cucumber = require('../../../lib/cucumber');

var stepDefinitions = function() {
var Given = When = Then = this.defineStep;

var GIVEN_KEYWORD = "Given";
var WHEN_KEYWORD = "When";
var THEN_KEYWORD = "Then";
Expand Down Expand Up @@ -210,18 +212,19 @@ var stepDefinitions = function() {
var _stepName = RegExp(name);
var stepDefinition;
if (keyword == THEN_KEYWORD) {
stepDefinition = function() { Then(_stepName, content); };
stepDefinition = function() { this.Then(_stepName, content); };
} else if (keyword == WHEN_KEYWORD) {
stepDefinition = function() { When(_stepName, content); };
stepDefinition = function() { this.When(_stepName, content); };
} else {
stepDefinition = function() { Given(_stepName, content); };
stepDefinition = function() { this.Given(_stepName, content); };
}
_stepDefs.push(stepDefinition);
};

function _getSupportCode() {
var supportCodeHelper = this;
_stepDefs.forEach(function(defineStep) {
defineStep();
defineStep.call(supportCodeHelper);
});
};

Expand Down
8 changes: 5 additions & 3 deletions lib/cucumber/cli/support_code_loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ var SupportCodeLoader = function(supportCodeFilePaths) {
var primeSupportCodeInitializer = self.getPrimeSupportCodeInitializer();
var secondarySupportCodeInitializer = self.getSecondarySupportCodeInitializer();
var initializer = function() {
primeSupportCodeInitializer();
secondarySupportCodeInitializer();
var supportCodeHelper = this;
primeSupportCodeInitializer.call(supportCodeHelper);
secondarySupportCodeInitializer.call(supportCodeHelper);
};
return initializer;
},
Expand Down Expand Up @@ -50,9 +51,10 @@ var SupportCodeLoader = function(supportCodeFilePaths) {

buildSupportCodeInitializerFromPaths: function buildSupportCodeInitializerFromPaths(paths) {
var wrapper = function(){
var supportCodeHelper = this;
paths.forEach(function(path) {
var initializer = require(path);
initializer();
initializer.call(supportCodeHelper);
});
};
return wrapper;
Expand Down
41 changes: 9 additions & 32 deletions lib/cucumber/support_code/library.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,42 +20,19 @@ var Library = function(supportCodeDefinition) {
return (stepDefinition != undefined);
},

defineGivenStep: function defineGivenStep(name, code) {
defineStep(name, code);
},

defineWhenStep: function defineWhenStep(name, code) {
defineStep(name, code);
},

defineThenStep: function defineThenStep(name, code) {
defineStep(name, code);
defineStep: function defineStep(name, code) {
var stepDefinition = Cucumber.SupportCode.StepDefinition(name, code);
stepDefinitions.add(stepDefinition);
}
};

withStepDefinitionHelpersDo(function() {
supportCodeDefinition();
});

function withStepDefinitionHelpersDo(callback) {
var originals = {
Given: (typeof(Given) != 'undefined' ? Given : undefined),
When: (typeof(When) != 'undefined' ? When : undefined),
Then: (typeof(Then) != 'undefined' ? Then : undefined)
};
Given = self.defineGivenStep;
When = self.defineWhenStep;
Then = self.defineThenStep;
callback();
Given = originals['Given'];
When = originals['When'];
Then = originals['Then'];
};

function defineStep(name, code) {
var stepDefinition = Cucumber.SupportCode.StepDefinition(name, code);
stepDefinitions.add(stepDefinition);
var supportCodeHelper = {
Given : self.defineStep,
When : self.defineStep,
Then : self.defineStep,
defineStep : self.defineStep
};
supportCodeDefinition.call(supportCodeHelper);

return self;
};
Expand Down
33 changes: 26 additions & 7 deletions spec/cucumber/cli/support_code_loader_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,21 +65,32 @@ describe("Cucumber.Cli.SupportCodeLoader", function() {
});

describe("returned function", function() {
var initializerFunction;
var initializerFunction, supportCodeHelper;

beforeEach(function() {
initializerFunction = supportCodeLoader.getSupportCodeInitializer();
supportCodeHelper = createSpy("support code helper");
});

it("calls the prime support code", function() {
initializerFunction();
initializerFunction.call(supportCodeHelper);
expect(primeSupportCodeInitializer).toHaveBeenCalled();
});

it("calls the prime support code with the support code helper as 'this'", function() {
initializerFunction.call(supportCodeHelper);
expect(primeSupportCodeInitializer.mostRecentCall.object).toBe(supportCodeHelper);
});

it("calls the secondary support code", function() {
initializerFunction();
initializerFunction.call(supportCodeHelper);
expect(secondarySupportCodeInitializer).toHaveBeenCalled();
});

it("calls the secondary support code with the support code helper as 'this'", function() {
initializerFunction.call(supportCodeHelper);
expect(secondarySupportCodeInitializer.mostRecentCall.object).toBe(supportCodeHelper);
});
});
});

Expand Down Expand Up @@ -172,30 +183,38 @@ describe("Cucumber.Cli.SupportCodeLoader", function() {
});

describe("returned wrapper function", function() {
var initializers, returnedFunction;
var initializers, returnedWrapperFunction, supportCodeHelper;

beforeEach(function() {
initializers = [];
paths.forEach(function(path) {
var initializer = spyOnModule(path);
initializers.push(initializer);
});
returnedFunction = supportCodeLoader.buildSupportCodeInitializerFromPaths(paths);
returnedWrapperFunction = supportCodeLoader.buildSupportCodeInitializerFromPaths(paths);
supportCodeHelper = createSpy("support code helper");
});

it("requires each initializer", function() {
returnedFunction();
returnedWrapperFunction.call(supportCodeHelper);
initializers.forEach(function(initializer) {
expect(initializer).toHaveBeenRequired();
});
});

it("calls each initializer function", function() {
returnedFunction();
returnedWrapperFunction.call(supportCodeHelper);
initializers.forEach(function(initializer) {
expect(initializer).toHaveBeenCalled();
});
});

it("calls each initializer function with the support code helper as 'this'", function() {
returnedWrapperFunction.call(supportCodeHelper);
initializers.forEach(function(initializer) {
expect(initializer.mostRecentCall.object).toBe(supportCodeHelper);
});
});
});
});
});
Loading

0 comments on commit 91ab361

Please sign in to comment.