Skip to content

Commit

Permalink
Add extractCallbackFromArguments()
Browse files Browse the repository at this point in the history
  • Loading branch information
jbpros committed Jun 7, 2011
1 parent 95f5a07 commit 55fe390
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 18 deletions.
7 changes: 6 additions & 1 deletion lib/cucumber.js
Original file line number Diff line number Diff line change
Expand Up @@ -339,13 +339,18 @@ Cucumber.Ast.TreeWalker = function(features, supportCodeLibrary, listeners) {
return userFunction;
},

extractCallbackFromArguments: function extractCallbackFromArguments(argumentsObject) {
var nonPayloadArguments = self.extractNonMessagePayloadArgumentsFromArguments(argumentsObject);
var callback = nonPayloadArguments.pop();
return callback;
},

extractNonMessagePayloadArgumentsFromArguments: function extractNonMessagePayloadArgumentsFromArguments(argumentsObject) {
var argumentsArray = Cucumber.Util.Arguments(argumentsObject);
var nonPayloadArguments = argumentsArray.slice(-Cucumber.Ast.TreeWalker.NON_PAYLOAD_TRAILING_PARAMETERS_COUNT);
return nonPayloadArguments;
},

extractCallbackFromArguments: TODO("extractCallbackFromArguments()"),
wrapUserFunctionAndAfterMessageBroadcast: TODO("wrapUserFunctionAndAfterMessageBroadcast()"),
broadcastBeforeMessage: TODO("broadcastBeforeMessage()"),

Expand Down
62 changes: 45 additions & 17 deletions spec/cucumber/ast/tree_walker_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,35 @@ describe("Cucumber.Ast.TreeWalker", function() {
});
});

describe("extractNonMessagePayloadArgumentsFromArguments()", function() {
var argumentsObject, argumentsArray, nonPayloadArguments;

beforeEach(function() {
nonPayloadArguments = createSpy("Non payload arguments");
argumentsObject = createSpy("Arguments object");
argumentsArray = createSpyWithStubs("Cucumber util arguments array",
{slice: nonPayloadArguments});
spyOn(Cucumber.Util, 'Arguments').andReturn(argumentsArray);
});

it("transforms the arguments object into an array", function() {
treeWalker.extractNonMessagePayloadArgumentsFromArguments(argumentsObject);
expect(Cucumber.Util.Arguments).toHaveBeenCalledWith(argumentsObject);
});

it("slices the payload parameters off", function() {
treeWalker.extractNonMessagePayloadArgumentsFromArguments(argumentsObject);
expect(argumentsArray.slice).toHaveBeenCalledWith(
-Cucumber.Ast.TreeWalker.NON_PAYLOAD_TRAILING_PARAMETERS_COUNT
);
});

it("returns the array slice", function() {
var returned = treeWalker.extractNonMessagePayloadArgumentsFromArguments(argumentsObject);
expect(returned).toBe(nonPayloadArguments);
});
});

describe("extractUserFunctionFromArguments()", function() {
var argumentsObject, userFunction, nonPayloadArguments;

Expand Down Expand Up @@ -300,35 +329,34 @@ describe("Cucumber.Ast.TreeWalker", function() {
});
});

describe("extractNonMessagePayloadArgumentsFromArguments()", function() {
var argumentsObject, argumentsArray, nonPayloadArguments;
describe("extractCallbackFromArguments()", function() {
var argumentsObject, callback, nonPayloadArguments;

beforeEach(function() {
nonPayloadArguments = createSpy("Non payload arguments");
callback = createSpy("User function");
argumentsObject = createSpy("Arguments object");
argumentsArray = createSpyWithStubs("Cucumber util arguments array",
{slice: nonPayloadArguments});
spyOn(Cucumber.Util, 'Arguments').andReturn(argumentsArray);
nonPayloadArguments = createSpyWithStubs("Non payload arguments", {pop: callback});
spyOn(treeWalker, 'extractNonMessagePayloadArgumentsFromArguments').
andReturn(nonPayloadArguments);
});

it("transforms the arguments object into an array", function() {
treeWalker.extractNonMessagePayloadArgumentsFromArguments(argumentsObject);
expect(Cucumber.Util.Arguments).toHaveBeenCalledWith(argumentsObject);
it("extracts the non-payload arguments from the arguments object", function() {
treeWalker.extractCallbackFromArguments(argumentsObject);
expect(treeWalker.extractNonMessagePayloadArgumentsFromArguments).toHaveBeenCalledWith(argumentsObject);
});

it("slices the payload parameters off", function() {
treeWalker.extractNonMessagePayloadArgumentsFromArguments(argumentsObject);
expect(argumentsArray.slice).toHaveBeenCalledWith(
-Cucumber.Ast.TreeWalker.NON_PAYLOAD_TRAILING_PARAMETERS_COUNT
);
it("pops the last argument out of the non-payload arguments", function() {
treeWalker.extractCallbackFromArguments(argumentsObject);
expect(nonPayloadArguments.pop).toHaveBeenCalled();
});

it("returns the array slice", function() {
var returned = treeWalker.extractNonMessagePayloadArgumentsFromArguments(argumentsObject);
expect(returned).toBe(nonPayloadArguments);
it("returns the unshifted argument", function() {
var returned = treeWalker.extractCallbackFromArguments(argumentsObject);
expect(returned).toBe(callback);
});
});


/*
describe("broadcastUserFunction()", function() {
var message, userFunction, callback;
Expand Down

0 comments on commit 55fe390

Please sign in to comment.