Skip to content

Commit

Permalink
Initial Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
eugeneware committed Feb 13, 2011
0 parents commit 5eab860
Show file tree
Hide file tree
Showing 5 changed files with 210 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
@@ -0,0 +1,2 @@
*.swp
.DS_Store
1 change: 1 addition & 0 deletions bin/cucumis
81 changes: 81 additions & 0 deletions bin/cucumis.js
@@ -0,0 +1,81 @@
#!/usr/bin/env node

var kyuri = require('kyuri');
var path = require('path');
var stepDefs = require(path.join(process.cwd(), 'features/step_definitions/addition'));
var fs = require('fs');

var data = fs.readFileSync(path.join(process.cwd(), 'features/simple.feature'));
var ast = kyuri.parse(data.toString());

var topic = {};

for (var index in ast) {
if (ast[index]) {
var feature = ast[1];
console.log('Feature: ' + feature.name);
console.log(feature.description);

if (feature.scenarios && feature.scenarios.length) {
feature.scenarios.forEach(function(scenario) {

console.log('Scenario' + (scenario.outline ? ' Outline' : '') + ': ' + scenario.name);

if (scenario.breakdown && scenario.breakdown.length) {
var lastStepType = 'GIVEN';

var exampleSets = [{}];

if (scenario.hasExamples) {
var examples = scenario.examples;
for (var exampleVar in examples) {
examples[exampleVar].forEach(function(exampleValue, index) {
if (!exampleSets[index]) {
exampleSets[index] = {};
}

exampleSets[index][exampleVar] = exampleValue;
});
}

}

exampleSets.forEach(function(exampleSet) {
scenario.breakdown.forEach(function(steps) {
for (var i in steps) {
var step = steps[i];

var stepType = step[0];
if (step[0] == 'AND') {
stepType = lastStepType;
}
lastStepType = stepType;

var stepText = step[1];
for (var exampleVar in exampleSet) {
stepText = stepText.replace(new RegExp('<' + exampleVar + '>', 'g'), exampleSet[exampleVar]);
}

var stepLine = step[0] + ' ' + stepText;
console.log(' ' + stepLine);

stepDefs.forEach(function (stepDef) {
var matches;
if (stepDef.operator.toUpperCase() == stepType) {
if (matches = stepDef.pattern.exec(stepText)) {
var stepFn = stepDef.generator(topic);
stepFn.apply(stepFn, matches.slice(1));
}
}
});
}
});

console.log('');
});

}
});
}
}
}
46 changes: 46 additions & 0 deletions features/simple.feature
@@ -0,0 +1,46 @@
Feature: Addition #AND A COMMENT!!!
In order to avoid silly mistakes
As a math idiot
I want to be able to add up numbers

@tag1 @tag2
#AND A COMMENT!!!
Scenario: Add two numbers
"""
Hey I'm a pystring, check me out now
Multi-line too!
"""
Given I have a calculator
And I have entered 50 into the calculator
And I have entered 70 into the calculator
When I press add
Then the result should be 120 on the screen

Scenario: Add three numbers
Given I have a calculator
And I have entered 1 into the calculator
And I have entered 2 into the calculator
And I have entered 3 into the calculator

When I press add
Then the result should be 5 on the screen

When I press add
Then the result should be 6 on the screen

Scenario: Return a single number
Given I have a calculator
And I have entered 42 into the calculator
Then the result should be 42 on the screen

Scenario Outline: Add some numbers
Given I have a calculator
And I have entered <num1> into the calculator
And I have entered <num2> into the calculator
When I press add
Then the result should be <result> on the screen

Examples:
| num1 | num2 | result |
| 1 | 2 | 3 |
| 2 | 3 | 5 |
80 changes: 80 additions & 0 deletions features/step_definitions/addition.js
@@ -0,0 +1,80 @@
/*
* Addition.feature
* Step definitions for Feature: 'Addition'
*
* Auto-generated using Kyuri: http://github.com/nodejitsu/kyuri
*/

var kyuri = require('kyuri'),
Steps = require('kyuri').Steps;

require('should');

var Calculator = function() {
this._stack = [];
};

Calculator.prototype = {
enter: function (value) {
this._stack.push(value);
},

get stack() {
return this._stack;
},

add: function() {
this._stack.push(this._stack.pop() + this._stack.pop());
},

result: function() {
return this._stack[this._stack.length - 1];
},
};

var calc;

//
// Step definitions for Scenario: Add two numbers
//

Steps.Given(/^I have a calculator$/, function(topic) {
return function() {

calc = new Calculator();

return topic;
};
});

Steps.Given(/^I have entered (\d+) into the calculator$/, function (topic) {
return function (value) {
// Always use or extend the same topic since you don't
// know how nested or not nested you are at this point
topic = topic || {};

calc.enter(parseInt(value));

return topic;
};
});

Steps.When(/^I press add$/, function (topic) {
return function () {
// Always use or extend the same topic since you don't
// know how nested or not nested you are at this point
topic = topic || {};

calc.add();

return topic;
};
});

Steps.Then(/^the result should be (\d+) on the screen$/, function (topic) {
return function (value) {
calc.result().should.eql(parseInt(value));
};
});

Steps.export(module);

0 comments on commit 5eab860

Please sign in to comment.