Skip to content

Commit

Permalink
Added background calls, beforeBackground event
Browse files Browse the repository at this point in the history
Rearranged outline parsing (now produces more friendly hash structures) and implemented outline execution in cucumber runner
  • Loading branch information
Paul Covell committed Dec 5, 2011
1 parent 00650eb commit 1fe714c
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 39 deletions.
25 changes: 18 additions & 7 deletions bin/kyuri
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
6 changes: 4 additions & 2 deletions examples/complex.feature
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
8 changes: 7 additions & 1 deletion examples/support/env.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
});

13 changes: 7 additions & 6 deletions lib/kyuri/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
87 changes: 64 additions & 23 deletions lib/kyuri/runners/cucumber.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -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();
}
});
}
};

/**
Expand Down

0 comments on commit 1fe714c

Please sign in to comment.