Skip to content
Browse files

Added background calls, beforeBackground event

Rearranged outline parsing (now produces more friendly hash structures) and implemented outline execution in cucumber runner
  • Loading branch information...
1 parent 00650eb commit 1fe714c05cbf2a46d03f1428ec727eee8f371f28 @pacovell pacovell committed Oct 4, 2011
Showing with 100 additions and 39 deletions.
  1. +18 −7 bin/kyuri
  2. +4 −2 examples/complex.feature
  3. +7 −1 examples/support/env.js
  4. +7 −6 lib/kyuri/parser.js
  5. +64 −23 lib/kyuri/runners/cucumber.js
View
25 bin/kyuri
@@ -69,20 +69,31 @@ kyuri.runner = kyuri.runners.cucumber;
var features = [];
var steps = [];
+var directories = [];
+
argv.forEach(function (file) {
if (file.match(featureFileExt)) {
// Load envDirs first and without steps
- envDirs.forEach(function (dir) {
- _loadJavascripts(path.join(path.dirname(file), dir));
- });
- // Load steps
- stepsDirs.forEach(function (dir) {
- _loadJavascripts(path.join(path.dirname(file), dir), steps);
- });
+ if (directories.indexOf(path.dirname(file)) === (-1)) {
+ directories.push(path.dirname(file));
+ }
features.push(kyuri.parse(fs.readFileSync(file).toString()));
}
});
+directories.forEach(function (top) {
+ // Load environments first
+ envDirs.forEach(function (dir) {
+ _loadJavascripts(path.join(top, dir));
+ });
+
+ // Load steps
+ stepsDirs.forEach(function (dir) {
+ _loadJavascripts(path.join(top, dir), steps);
+ });
+});
+
+
var complete = false;
kyuri.runners.cucumber.run(features, steps, function (err) {
View
6 examples/complex.feature
@@ -2,7 +2,10 @@ Feature: Complex Addition
In order to avoid silly mistakes
As a math idiot
I want to be told the sum of two numbers
-
+
+ Background:
+ Given I have a calculator
+
Scenario: Add two numbers
Given I have entered 50 into the calculator
And I have entered 70 into the calculator
@@ -20,7 +23,6 @@ Feature: Complex Addition
| number1 | number2 | number3 |
| 10 | 20 | 150 |
| 20 | 40 | 180 |
- | 40 | 60 | 220 |
Scenario: Add two numbers
Given I have entered 50 into the calculator
View
8 examples/support/env.js
@@ -12,7 +12,13 @@ Runner.on('beforeTest', function (done) {
done();
});
+Runner.on('beforeBackground', function (done) {
+ console.log('beforeBackground event');
+ done();
+});
+
Runner.on('afterTest', function (done) {
console.log('afterTest event');
done();
-});
+});
+
View
13 lib/kyuri/parser.js
@@ -321,19 +321,20 @@ var _states = {
next: 'exampleRows',
last: ['INDENT', 'TERMINATOR'],
build: function (ast, token) {
- var scenario = getLastScenario(ast);
+ var scenario = getLastScenario(ast),
+ example;
+
if(!scenario.hasExamples) {
- for (var i = 0; i < token[1].length; i++) {
- scenario.examples[token[1][i]] = [];
- }
-
+ scenario.examples = [];
scenario.exampleVariables = token[1];
scenario.hasExamples = true;
}
else {
+ example = {};
for (var i = 0; i < token[1].length; i++) {
- scenario.examples[scenario.exampleVariables[i]].push(token[1][i]);
+ example[scenario.exampleVariables[i]] = token[1][i];
}
+ scenario.examples.push(example);
}
return scenario;
}
View
87 lib/kyuri/runners/cucumber.js
@@ -22,23 +22,61 @@ util.inherits(Cucumber, EventEmitter);
Cucumber.prototype.run = function (features, steps, callback) {
var self = this;
- self._emitAndWait('beforeTest', function () {
+ function runFeatures (features, next) {
self._invokeSerial(features, function (feature, featureCb) {
- var feature = feature[Object.keys(feature).shift()];
- self._invokeSerial(feature.scenarios, function (scenario, scenarioCb) {
- if (scenario.outline) {
- scenarioCb();
- } else {
- self._invokeSerial(scenario.breakdown, function(step, stepCb) {
- var step = step[Object.keys(step).shift()],
- text = step.join(' ');
-
- self._executeStepDefinition(steps, step[1], stepCb);
- }, scenarioCb);
- }
- }, featureCb);
- },
- function (err) {
+ var feature = feature[Object.keys(feature).shift()];
+ runScenarios(feature, featureCb);
+ }, next);
+ }
+
+ function runBackground (feature, next) {
+ if (feature.background) {
+ self._emitAndWait('beforeBackground', function() {
+ self._invokeSerial(feature.background.breakdown, runStep, next);
+ });
+ } else {
+ next();
+ }
+ }
+
+ function runScenarios (feature, next) {
+ self._invokeSerial(feature.scenarios, function (scenario, scenarioCb) {
+ if (scenario.outline) {
+ self._invokeSerial(scenario.examples, function (example, exampleCb) {
+ var steps = []
+
+ // Create customized steps by replacing the template steps with
+ // the example variables
+ scenario.breakdown.forEach(function (step) {
+ var exampleStep = {};
+ Object.keys(step).forEach(function (i) {
+ exampleStep[i] = step[i].slice(0); // copy the array
+ scenario.exampleVariables.forEach(function (variable) {
+ exampleStep[i][1] = exampleStep[i][1].replace('\<' + variable + '\>', example[variable]);
+ });
+ });
+ steps.push(exampleStep);
+ });
+
+ runBackground(feature, function (err) {
+ self._invokeSerial(steps, runStep, exampleCb);
+ });
+ }, scenarioCb);
+ } else {
+ runBackground(feature, function (err) {
+ self._invokeSerial(scenario.breakdown, runStep, scenarioCb);
+ });
+ }
+ }, next);
+ }
+
+ function runStep (step, next) {
+ var step = step[Object.keys(step).shift()];
+ self._executeStepDefinition(steps, step[1], next);
+ }
+
+ self._emitAndWait('beforeTest', function () {
+ runFeatures(features, function (err) {
self._emitAndWait('afterTest', function() {
if (self.missingSteps.length > 0) {
console.log('Missing Steps');
@@ -79,7 +117,6 @@ Cucumber.prototype._executeStepDefinition = function (steps, step, callback) {
matches = matches.slice(1);
matches.unshift(stepContext);
fn.apply(this, matches);
- return true;
} else {
stepContext.pending();
}
@@ -110,12 +147,16 @@ Cucumber.prototype._invokeSerial = function (ar, fn, callback) {
Cucumber.prototype._emitAndWait = function (event, callback) {
var count = this.listeners(event).length;
- this.emit(event, function () {
- count -= 1;
- if (count === 0) {
- callback();
- }
- });
+ if (count === 0) {
+ callback();
+ } else {
+ this.emit(event, function () {
+ count -= 1;
+ if (count === 0) {
+ callback();
+ }
+ });
+ }
};
/**

0 comments on commit 1fe714c

Please sign in to comment.
Something went wrong with that request. Please try again.