Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

adding Steppy functionality + tests

  • Loading branch information...
commit 5ad7a5eda5f635ba140c06dc9ac9c4455239751b 1 parent f7e5256
Alexy Maslennikov authored
View
1  .gitignore
@@ -0,0 +1 @@
+node_modules/*
View
94 lib/twoStep.js
@@ -1,11 +1,23 @@
module.exports = TwoStep;
+module.exports.Steppy = Steppy;
var slice = Array.prototype.slice;
-function Group(callback) {
+/**
+ * Group represents one step in Step's execution flow. Makes a decision when
+ * the execution should jump to the next step.
+ *
+ * @param {function(err, args)} callback
+ * Callback to be called on successful execution end
+
+ * @param {function(err, args)} [errorCallback]
+ * If defined, will be called in case of error (default value: callback arg)
+ */
+function Group(callback, errorCallback) {
this.args = [null];
this.left = 0;
this.callback = callback;
+ this.errorCallback = errorCallback || callback;
this.isDone = false;
}
@@ -25,8 +37,7 @@ Group.prototype.error = function error(err) {
return;
}
this.isDone = true;
- var callback = this.callback;
- callback(err);
+ this.errorCallback(err);
};
// Simple utility for passing a sync value to the next step.
@@ -74,8 +85,24 @@ Group.prototype.makeGroup = function makeGroup() {
});
};
-// Expose just for fun and extensibility
-TwoStep.Group = Group;
+/**
+ * TwoStep section
+ */
+
+// Execute steps immediately
+function TwoStep() {
+ exec(slice.call(arguments), []);
+}
+
+// Create a composite function with steps built-in
+TwoStep.fn = function () {
+ var steps = slice.call(arguments);
+ return function () {
+ var args = slice.call(arguments);
+ var callback = args.pop();
+ exec(steps, args, callback);
+ };
+};
// Stepper function
function exec(steps, args, callback) {
@@ -100,20 +127,8 @@ function exec(steps, args, callback) {
next.apply(null, args);
}
-// Execute steps immedietly
-function TwoStep() {
- exec(slice.call(arguments), []);
-}
-
-// Create a composite function with steps built-in
-TwoStep.fn = function () {
- var steps = slice.call(arguments);
- return function () {
- var args = slice.call(arguments);
- var callback = args.pop();
- exec(steps, args, callback);
- };
-};
+// Expose just for fun and extensibility
+TwoStep.Group = Group;
//Simple step function, just return callack
TwoStep.simple = function(callback) {
@@ -129,3 +144,44 @@ TwoStep.throwIfError = function(callback) {
return callback.apply(this, arguments);
};
};
+
+
+/**
+ * Steppy section
+ */
+
+// Execute steps immediately
+function Steppy() {
+ execSteppy(slice.call(arguments), []);
+}
+
+// Stepper function
+function execSteppy(steps, args, callback) {
+ if (steps.length < 2) {
+ throw new Error('Actually, you don\'t want to run less than two steps');
+ }
+ var pos = 0;
+ var errorHandler = steps[steps.length - 1];
+
+ function next() {
+ var step = steps[pos++];
+ if (!step) {
+ callback && callback.apply(null, arguments);
+ return;
+ }
+ var group = new Group(next, errorHandler);
+ try {
+ step.apply(group, arguments);
+ } catch (e) {
+ group.error(e);
+ return;
+ }
+ if (group.left === 0) {
+ group.done();
+ }
+ };
+ next.apply(null, args);
+}
+
+// Expose just for fun and extensibility
+TwoStep.Group = Group;
View
29 spec/errorThrow_steppy.spec.js
@@ -0,0 +1,29 @@
+'use strict';
+
+
+var Steppy = require('../lib/twoStep').Steppy,
+ fs = require('fs'),
+ selfText = fs.readFileSync(__filename, 'utf8'),
+ error = new Error('Steppy error');
+
+describe('simple callback usage', function() {
+ it('should throw error and catch it in last callback', function(done) {
+ Steppy(
+ function() {
+ throw error;
+ },
+ function(err, text) {
+ //this step will never be executed
+ expect(1).toEqual(0);
+ },
+ function(err) {
+ //this step will never be executed
+ expect(1).toEqual(0);
+ },
+ function(err) {
+ expect(err).toEqual(error);
+ done();
+ }
+ );
+ });
+});
View
45 spec/simpleCallback_steppy.spec.js
@@ -0,0 +1,45 @@
+'use strict';
+
+
+var Steppy = require('../lib/twoStep').Steppy,
+ fs = require('fs'),
+ selfText = fs.readFileSync(__filename, 'utf8');
+
+describe('simple callback usage', function() {
+ it('calling steppy with less than two steps', function() {
+ try {
+ Steppy();
+ throw new Error('should not be here');
+ } catch (e) {
+ expect(e).toBeTruthy();
+ }
+
+ try {
+ Steppy(
+ function() {
+ fs.readFile(__filename, 'utf8', this.slot());
+ }
+ );
+ throw new Error('should not be here');
+ } catch (e) {
+ expect(e).toBeTruthy();
+ }
+ }),
+
+ it('should check the pass,using uppercased text and wraps', function(done) {
+ Steppy(
+ function() {
+ fs.readFile(__filename, 'utf8', this.slot());
+ },
+ function(err, text) {
+ expect(text).toEqual(selfText);
+ this.pass(text.toUpperCase());
+ },
+ function(err, uppercasedText) {
+ expect(uppercasedText).toEqual(selfText.toUpperCase());
+ done(err);
+ },
+ done //in case of error
+ );
+ });
+});
Please sign in to comment.
Something went wrong with that request. Please try again.