Skip to content

Commit

Permalink
Merge pull request blinkbox#1 from timblinkbox/master
Browse files Browse the repository at this point in the history
Integration with Angular Scenario Runner.
  • Loading branch information
chrismilleruk committed Aug 2, 2012
2 parents 9aecd4a + d4e3ddf commit 529e157
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 14 deletions.
18 changes: 14 additions & 4 deletions lib/cucumber/ast/feature.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,20 @@ var Feature = function(keyword, name, description, line) {
return tags;
},

acceptVisitor: function acceptVisitor(visitor, callback) {
self.instructVisitorToVisitBackground(visitor, function() {
self.instructVisitorToVisitScenarios(visitor, callback);
});
acceptVisitor: function acceptVisitor(visitor, callback) {
// Create an angular describe block
describeStart(self.getKeyword() + ': ' + self.getName(),function(endDescribeCallback) {
// Call each cucumber background and all scenarios
self.instructVisitorToVisitBackground(visitor, function() {
self.instructVisitorToVisitScenarios(visitor, function() {
// When background and scenarios have completed, close the describe block
endDescribeCallback();
// Tell cucumber we've finished with the feature - we'll let angular deal with any errors.
callback();
});
});
},
self.getDescription());
},

instructVisitorToVisitBackground: function instructVisitorToVisitBackground(visitor, callback) {
Expand Down
19 changes: 15 additions & 4 deletions lib/cucumber/ast/scenario.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,21 @@ var Scenario = function(keyword, name, description, line) {
return tags;
},

acceptVisitor: function acceptVisitor(visitor, callback) {
self.instructVisitorToVisitBackgroundSteps(visitor, function() {
self.instructVisitorToVisitScenarioSteps(visitor, callback);
});
acceptVisitor: function acceptVisitor(visitor, callback) {
// Create an angular describe block for the current scenario
describeStart(self.getKeyword() + ': ' + self.getName(), function(endDescribeCallback) {
// Visit each of the background steps
self.instructVisitorToVisitBackgroundSteps(visitor, function() {
// Visit each of the scenario steps
self.instructVisitorToVisitScenarioSteps(visitor, function() {
// Close the describe block
endDescribeCallback();
// Tell cucumber we've finished - we'll let angular deal with any errors in the step.
callback();
});
});
},
self.getDescription());
},

instructVisitorToVisitBackgroundSteps: function instructVisitorToVisitBackgroundSteps(visitor, callback) {
Expand Down
6 changes: 6 additions & 0 deletions lib/cucumber/runtime/ast_tree_walker.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,12 @@ var AstTreeWalker = function(features, supportCodeLibrary, listeners) {

processStep: function processStep(step, callback) {
if (self.isStepUndefined(step)) {
// Tell angular that we couldn't find the step definition and give some example code
var snippetBuilder = Cucumber.SupportCode.StepDefinitionSnippetBuilder(step);
var snippet = snippetBuilder.buildSnippet();
it(step.getKeyword() + ': ' + step.getName(), function () { throw ('Error: Step definition could not be found.\n\nExpected a step definition similar to the following:\n\n' + snippet); });

// Continue with the cucumber code
self.witnessUndefinedStep();
self.skipUndefinedStep(step, callback);
} else if (self.isSkippingSteps()) {
Expand Down
1 change: 1 addition & 0 deletions lib/cucumber/support_code/library.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ var Library = function(supportCodeDefinition) {
Given : self.defineStep,
When : self.defineStep,
Then : self.defineStep,
And : self.defineStep,
defineStep : self.defineStep,
World : worldConstructor
};
Expand Down
19 changes: 16 additions & 3 deletions lib/cucumber/support_code/step_definition.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,20 @@ var StepDefinition = function(pattern, code) {
callback(failedStepResult);
};

var parameters = self.buildInvocationParameters(step, codeCallback);
// Previous version of the code had codeCallback as a parameter, but we'll exclude this and call it ourselves.
var parameters = self.buildInvocationParameters(step, null);
var handleException = self.buildExceptionHandlerToCodeCallback(codeCallback);
Cucumber.Util.Exception.registerUncaughtExceptionHandler(handleException);

try {
code.apply(world, parameters);
// Create an angular callback. Angular just executes the function, so we wrap it here so we can include any parameters.
var angularCallback = function() { code.apply(this, parameters); };

// Create an Angular scenario runner 'it' block
it(step.getKeyword() + ': ' + step.getName(), angularCallback);

// Make the callback immediately as angular will actually run the tests, so we just tell Cucumber everything worked fine and let it continue
codeCallback();
} catch (exception) {
handleException(exception);
}
Expand All @@ -64,7 +72,12 @@ var StepDefinition = function(pattern, code) {
var attachmentContents = step.getAttachmentContents();
parameters.push(attachmentContents);
}
parameters.push(callback);

// If a callback exists, then include it, won't be required for angular integration.
if (!!callback) {
parameters.push(callback);
}

return parameters;
},

Expand Down
6 changes: 3 additions & 3 deletions lib/cucumber/support_code/step_definition_snippet_builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,10 @@ var StepDefinitionSnippetBuilder = function(step) {
StepDefinitionSnippetBuilder.STEP_DEFINITION_START = 'this.';
StepDefinitionSnippetBuilder.STEP_DEFINITION_INNER1 = '(';
StepDefinitionSnippetBuilder.STEP_DEFINITION_INNER2 = ', function(';
StepDefinitionSnippetBuilder.STEP_DEFINITION_END = ") {\n // express the regexp above with the code you wish you had\n callback.pending();\n});\n";
StepDefinitionSnippetBuilder.STEP_DEFINITION_END = ") {\n // express the regexp above with the equivalent angular DSL code\n throw ('Error: Step not implemented');\n});\n";
StepDefinitionSnippetBuilder.STEP_DEFINITION_DOC_STRING = 'string';
StepDefinitionSnippetBuilder.STEP_DEFINITION_DATA_TABLE = 'table';
StepDefinitionSnippetBuilder.STEP_DEFINITION_CALLBACK = 'callback';
StepDefinitionSnippetBuilder.STEP_DEFINITION_CALLBACK = '';
StepDefinitionSnippetBuilder.PATTERN_START = '/^';
StepDefinitionSnippetBuilder.PATTERN_END = '$/';
StepDefinitionSnippetBuilder.CONTEXT_STEP_DEFINITION_FUNCTION_NAME = 'Given';
Expand All @@ -101,4 +101,4 @@ StepDefinitionSnippetBuilder.NUMBER_MATCHING_GROUP = '(\\d+)';
StepDefinitionSnippetBuilder.QUOTED_STRING_PATTERN = /"[^"]*"/gi;
StepDefinitionSnippetBuilder.QUOTED_STRING_MATCHING_GROUP = '"([^"]*)"';
StepDefinitionSnippetBuilder.FUNCTION_PARAMETER_SEPARATOR = ', ';
module.exports = StepDefinitionSnippetBuilder;
module.exports = StepDefinitionSnippetBuilder;

0 comments on commit 529e157

Please sign in to comment.