Skip to content

Commit 529e157

Browse files
author
chrismilleruk
committed
Merge pull request blinkbox#1 from timblinkbox/master
Integration with Angular Scenario Runner.
2 parents 9aecd4a + d4e3ddf commit 529e157

File tree

6 files changed

+55
-14
lines changed

6 files changed

+55
-14
lines changed

lib/cucumber/ast/feature.js

+14-4
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,20 @@ var Feature = function(keyword, name, description, line) {
5252
return tags;
5353
},
5454

55-
acceptVisitor: function acceptVisitor(visitor, callback) {
56-
self.instructVisitorToVisitBackground(visitor, function() {
57-
self.instructVisitorToVisitScenarios(visitor, callback);
58-
});
55+
acceptVisitor: function acceptVisitor(visitor, callback) {
56+
// Create an angular describe block
57+
describeStart(self.getKeyword() + ': ' + self.getName(),function(endDescribeCallback) {
58+
// Call each cucumber background and all scenarios
59+
self.instructVisitorToVisitBackground(visitor, function() {
60+
self.instructVisitorToVisitScenarios(visitor, function() {
61+
// When background and scenarios have completed, close the describe block
62+
endDescribeCallback();
63+
// Tell cucumber we've finished with the feature - we'll let angular deal with any errors.
64+
callback();
65+
});
66+
});
67+
},
68+
self.getDescription());
5969
},
6070

6171
instructVisitorToVisitBackground: function instructVisitorToVisitBackground(visitor, callback) {

lib/cucumber/ast/scenario.js

+15-4
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,21 @@ var Scenario = function(keyword, name, description, line) {
4848
return tags;
4949
},
5050

51-
acceptVisitor: function acceptVisitor(visitor, callback) {
52-
self.instructVisitorToVisitBackgroundSteps(visitor, function() {
53-
self.instructVisitorToVisitScenarioSteps(visitor, callback);
54-
});
51+
acceptVisitor: function acceptVisitor(visitor, callback) {
52+
// Create an angular describe block for the current scenario
53+
describeStart(self.getKeyword() + ': ' + self.getName(), function(endDescribeCallback) {
54+
// Visit each of the background steps
55+
self.instructVisitorToVisitBackgroundSteps(visitor, function() {
56+
// Visit each of the scenario steps
57+
self.instructVisitorToVisitScenarioSteps(visitor, function() {
58+
// Close the describe block
59+
endDescribeCallback();
60+
// Tell cucumber we've finished - we'll let angular deal with any errors in the step.
61+
callback();
62+
});
63+
});
64+
},
65+
self.getDescription());
5566
},
5667

5768
instructVisitorToVisitBackgroundSteps: function instructVisitorToVisitBackgroundSteps(visitor, callback) {

lib/cucumber/runtime/ast_tree_walker.js

+6
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,12 @@ var AstTreeWalker = function(features, supportCodeLibrary, listeners) {
157157

158158
processStep: function processStep(step, callback) {
159159
if (self.isStepUndefined(step)) {
160+
// Tell angular that we couldn't find the step definition and give some example code
161+
var snippetBuilder = Cucumber.SupportCode.StepDefinitionSnippetBuilder(step);
162+
var snippet = snippetBuilder.buildSnippet();
163+
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); });
164+
165+
// Continue with the cucumber code
160166
self.witnessUndefinedStep();
161167
self.skipUndefinedStep(step, callback);
162168
} else if (self.isSkippingSteps()) {

lib/cucumber/support_code/library.js

+1
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ var Library = function(supportCodeDefinition) {
6666
Given : self.defineStep,
6767
When : self.defineStep,
6868
Then : self.defineStep,
69+
And : self.defineStep,
6970
defineStep : self.defineStep,
7071
World : worldConstructor
7172
};

lib/cucumber/support_code/step_definition.js

+16-3
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,20 @@ var StepDefinition = function(pattern, code) {
4444
callback(failedStepResult);
4545
};
4646

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

5152
try {
52-
code.apply(world, parameters);
53+
// Create an angular callback. Angular just executes the function, so we wrap it here so we can include any parameters.
54+
var angularCallback = function() { code.apply(this, parameters); };
55+
56+
// Create an Angular scenario runner 'it' block
57+
it(step.getKeyword() + ': ' + step.getName(), angularCallback);
58+
59+
// Make the callback immediately as angular will actually run the tests, so we just tell Cucumber everything worked fine and let it continue
60+
codeCallback();
5361
} catch (exception) {
5462
handleException(exception);
5563
}
@@ -64,7 +72,12 @@ var StepDefinition = function(pattern, code) {
6472
var attachmentContents = step.getAttachmentContents();
6573
parameters.push(attachmentContents);
6674
}
67-
parameters.push(callback);
75+
76+
// If a callback exists, then include it, won't be required for angular integration.
77+
if (!!callback) {
78+
parameters.push(callback);
79+
}
80+
6881
return parameters;
6982
},
7083

lib/cucumber/support_code/step_definition_snippet_builder.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,10 @@ var StepDefinitionSnippetBuilder = function(step) {
8787
StepDefinitionSnippetBuilder.STEP_DEFINITION_START = 'this.';
8888
StepDefinitionSnippetBuilder.STEP_DEFINITION_INNER1 = '(';
8989
StepDefinitionSnippetBuilder.STEP_DEFINITION_INNER2 = ', function(';
90-
StepDefinitionSnippetBuilder.STEP_DEFINITION_END = ") {\n // express the regexp above with the code you wish you had\n callback.pending();\n});\n";
90+
StepDefinitionSnippetBuilder.STEP_DEFINITION_END = ") {\n // express the regexp above with the equivalent angular DSL code\n throw ('Error: Step not implemented');\n});\n";
9191
StepDefinitionSnippetBuilder.STEP_DEFINITION_DOC_STRING = 'string';
9292
StepDefinitionSnippetBuilder.STEP_DEFINITION_DATA_TABLE = 'table';
93-
StepDefinitionSnippetBuilder.STEP_DEFINITION_CALLBACK = 'callback';
93+
StepDefinitionSnippetBuilder.STEP_DEFINITION_CALLBACK = '';
9494
StepDefinitionSnippetBuilder.PATTERN_START = '/^';
9595
StepDefinitionSnippetBuilder.PATTERN_END = '$/';
9696
StepDefinitionSnippetBuilder.CONTEXT_STEP_DEFINITION_FUNCTION_NAME = 'Given';
@@ -101,4 +101,4 @@ StepDefinitionSnippetBuilder.NUMBER_MATCHING_GROUP = '(\\d+)';
101101
StepDefinitionSnippetBuilder.QUOTED_STRING_PATTERN = /"[^"]*"/gi;
102102
StepDefinitionSnippetBuilder.QUOTED_STRING_MATCHING_GROUP = '"([^"]*)"';
103103
StepDefinitionSnippetBuilder.FUNCTION_PARAMETER_SEPARATOR = ', ';
104-
module.exports = StepDefinitionSnippetBuilder;
104+
module.exports = StepDefinitionSnippetBuilder;

0 commit comments

Comments
 (0)