New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

two-step

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

two-step - npm Package Compare versions

Comparing version 0.0.3 to 0.0.4

test/valSync.js

12

index.js

@@ -83,4 +83,4 @@ function ParamList(step, name) {

},
syncVal: function(val, valName) {
this.val(valName)(null, val);
syncVal: function(val, name) {
this.val(name)(null, val);
}

@@ -90,3 +90,4 @@ };

syncVal: function(val, name) {
this.val(name)(null, val);
var callback = this.val(name);
process.nextTick(function() { callback(null, val); });
},

@@ -98,3 +99,5 @@ listen: function(emitter, name) {

emitter.on('data', function (chunk) { chunks.push(chunk); });
emitter.on('error', function(err) { params.error(err, errInfo(params.name, paramIdx, name)); });
emitter.on('error', function(err) {
params.error(err, errInfo(params.name, paramIdx, name));
});
emitter.on('end', function() { params.done(paramIdx, chunks); });

@@ -113,3 +116,2 @@ }

if (typeof func === 'function') {
curIdx = Number.MAX_VALUE;
func.apply(this, args);

@@ -116,0 +118,0 @@ return;

{ "name": "two-step"
, "version": "0.0.3"
, "version": "0.0.4"
, "description": "TwoStep is the spiritual successor of Step with better error handling and finer flow control"

@@ -17,3 +17,2 @@ , "keywords": [ "flow control", "step", "async" ]

, "engines": { "node": ">= 0.3.1" }
, "devDependencies": { "vows": "" }
}

@@ -7,3 +7,6 @@ var assert = require("assert");

stepObj.data.callSeq.push(stepObj._params.name);
stepObj.data[stepObj._params.name] = { when: Date.now(), args: Array.prototype.slice.call(args) };
stepObj.data[stepObj._params.name] = {
when: Date.now(),
args: Array.prototype.slice.call(args)
};
},

@@ -23,3 +26,5 @@ coverage: function(names) {

names,
"Functions were not called in order:\n\t\texpected: " + names + "\n\t\tactual: " + data.callSeq
"Functions were not called in order:" +
"\n\t\texpected: " + names +
"\n\t\tactual: " + data.callSeq
);

@@ -26,0 +31,0 @@ };

@@ -1,2 +0,1 @@

var vows = require("vows");
var assert = require("assert");

@@ -7,71 +6,75 @@ var TwoStep = require("../");

var callbackErr = new Error("Catch me!"), explicitErr = new Error("Catch me too!");
vows.describe("Test error handling").addBatch({
"throwing and catching exceptions": {
topic: function() {
TwoStep(
function valueErr() {
check.save(this, arguments);
var callback = this.val("faulty-param");
setTimeout(function () { callback(callbackErr); }, 0);
},
function explictThrow(err) {
check.save(this, arguments);
throw explicitErr;
},
function catchException(err) {
check.save(this, arguments);
this.syncVal(this.data);
},
this.callback
);
},
"no args to first callback": check.emptyArgs("valueErr"),
"every step called": check.coverage([ "valueErr", "explictThrow", "catchException" ]),
"steps called in order": check.order([ "valueErr", "explictThrow", "catchException" ]),
"catch callback error": function(data) {
var err = data["explictThrow"].args[0];
assert.equal(err, callbackErr, "Callback error wasn't replayed correctly");
assert.ok(err.step, "Step info object wasn't added to error");
assert.equal(err.step.name, "valueErr", "Incorrect step name specified in error obj");
assert.equal(err.step.paramName, "faulty-param", "Incorrect param name specified in error obj");
assert.equal(err.step.paramIdx, 1, "Incorrect param index specified in error obj");
},
"catch random exception": function(data) {
var err = data["catchException"].args[0];
assert.equal(err, explicitErr, "Callback error wasn't replayed correctly");
assert.ok(err.step, "Step info object wasn't added to error");
assert.equal(err.step.name, "explictThrow", "Incorrect step name specified in error obj");
assert.equal(err.step.paramName, undefined, "Incorrect param name specified in error obj");
assert.equal(err.step.paramIdx, undefined, "Incorrect param index specified in error obj");
}
var tests = {
expected: 1,
executed: 0,
finished: function() { tests.executed++; }
};
process.on("exit", function() {
assert.equal(tests.executed, tests.expected);
console.log("All done. Everything passed.");
});
TwoStep(
function valueErr() {
check.save(this, arguments);
var callback = this.val("faulty-param");
setTimeout(function () { callback(callbackErr); }, 0);
},
"throwing in last step": {
topic: function() { return TwoStep; },
"error in last step throws error": function(TwoStep) {
assert.throws(function() {
TwoStep(
function first() { },
function lastErr() { throw "Last Error Thrown"; }
);
}, "Last Error Thrown");
},
"error thrown when jumpTo non-existant location": function(TwoStep) {
assert.throws(function() {
TwoStep(
function first() { this.jumpTo("no-where"); }
);
}, "Error thrown when trying to jumpTo unknow function name.");
assert.throws(function() {
TwoStep(
function first() { this.jumpTo(5); }
);
}, "Error thrown when trying to jumpTo unknow function index.");
assert.throws(function() {
TwoStep(
function first() { this.jumpTo(null); }
);
}, "Error thrown when trying to jumpTo unknow function.");
}
function explictThrow(err) {
check.save(this, arguments);
throw explicitErr;
},
function catchException(err) {
check.save(this, arguments);
this.syncVal(this.data);
},
function done(err, data) {
if(err) { throw err; }
check.emptyArgs("valueErr")(data);
check.coverage([ "valueErr", "explictThrow", "catchException" ])(data);
check.order([ "valueErr", "explictThrow", "catchException" ])(data);
err = data["explictThrow"].args[0];
assert.equal(err, callbackErr, "Callback error wasn't replayed correctly");
assert.ok(err.step, "Step info object wasn't added to error");
assert.equal(err.step.name, "valueErr", "Incorrect step name specified in error obj");
assert.equal(
err.step.paramName, "faulty-param", "Incorrect param name specified in error obj"
);
assert.equal(err.step.paramIdx, 1, "Incorrect param index specified in error obj");
err = data["catchException"].args[0];
assert.equal(err, explicitErr, "Callback error wasn't replayed correctly");
assert.ok(err.step, "Step info object wasn't added to error");
assert.equal(err.step.name, "explictThrow", "Incorrect step name specified in error obj");
assert.equal(err.step.paramName, undefined, "Incorrect param name specified in error obj");
assert.equal(err.step.paramIdx, undefined, "Incorrect param index specified in error obj");
tests.finished();
}
}).export(module);
);
assert.throws(function() {
TwoStep(
function first() { },
function lastErr() { throw "Last Error Thrown"; }
);
}, "Last Error Thrown");
assert.throws(function() {
TwoStep(
function first() { this.jumpTo("no-where"); }
);
}, "Error thrown when trying to jumpTo unknow function name.");
assert.throws(function() {
TwoStep(
function first() { this.jumpTo(5); }
);
}, "Error thrown when trying to jumpTo unknow function index.");
assert.throws(function() {
TwoStep(
function first() { this.jumpTo(null); }
);
}, "Error thrown when trying to jumpTo unknow function.");

@@ -1,2 +0,1 @@

var vows = require("vows");
var assert = require("assert");

@@ -6,106 +5,106 @@ var TwoStep = require("../");

vows.describe("Test `this.val`").addBatch({
"basic test passing function name and one param": {
topic: function() {
TwoStep(
function start(err) {
check.save(this, arguments);
this.jumpTo("last", [ err, "hello" ]);
},
function skip(err) {
check.save(this, arguments);
},
function last(err) {
check.save(this, arguments);
this.syncVal(this.data);
},
this.callback
);
},
"no args to first callback": check.emptyArgs("start"),
"correct callbacks were called": check.coverage([ "start", "last" ]),
"callbacks executed in order": check.order([ "start", "last" ]),
"correct callback were skipped": function(data) {
assert.ok(!data["skip"], "The skipped callback was executed");
},
"check arguments of recieving function": function(data) {
assert.ok(!data["last"].args[0], "The error argument was incorrectly set");
assert.equal(data["last"].args[1], "hello", "The incorrect arguments were sent");
}
var tests = {
expected: 3,
executed: 0,
finished: function() { tests.executed++; }
};
process.on("exit", function() {
assert.equal(tests.executed, tests.expected);
console.log("All done. Everything passed.");
});
TwoStep(
function start(err) {
check.save(this, arguments);
this.jumpTo("last", [ err, "hello" ]);
},
"test that step chain continues once a jumpTo is executed": {
topic: function() {
TwoStep(
function first(err) {
check.save(this, arguments);
this.jumpTo("fourth", [ err, "go to fourth" ]);
},
function second(err) {
check.save(this, arguments);
this.syncVal("foo")
},
function third(err) {
check.save(this, arguments);
this.jumpTo("fifth", [ err, "go to fifth" ]);
},
function fourth(err) {
check.save(this, arguments);
this.jumpTo("second", [ err, "go to second" ]);
},
function fifth(err) {
check.save(this, arguments);
this.syncVal(this.data);
},
this.callback
);
},
"no args to first callback": check.emptyArgs("first"),
"correct callbacks were called": check.coverage([ "first", "second", "third", "fourth", "fifth" ]),
"callbacks executed in order": check.order([ "first", "fourth", "second", "third", "fifth" ]),
"check arguments of recieving function": function(data) {
assert.ok(!data["second"].args[0], "The error argument was incorrectly set");
assert.equal(data["second"].args[1], "go to second", "The incorrect arguments were sent");
assert.equal(data["third"].args[1], "foo", "The incorrect arguments were sent");
assert.equal(data["fourth"].args[1], "go to fourth", "The incorrect arguments were sent");
assert.equal(data["fifth"].args[1], "go to fifth", "The incorrect arguments were sent");
}
function skip(err) {
check.save(this, arguments);
},
"bail out if a function was specified": {
topic: function() {
var callback = this.callback;
function last(err) {
check.save(this, arguments);
this.syncVal(this.data);
},
function date(err, data) {
if(err) { throw err; }
function exit() {
this.data.exited = true;
this.data.exitArgs = Array.prototype.slice.call(arguments);
callback(null, this.data);
}
TwoStep(
function start() {
check.save(this, arguments);
this.jumpTo(exit, ['foo', 'bar', 'baz']);
},
function skip1() {
check.save(this, arguments);
},
function skip2() {
check.save(this, arguments);
check.emptyArgs("start")(data);
check.coverage([ "start", "last" ])(data);
check.order([ "start", "last" ])(data);
assert.ok(!data["skip"], "The skipped callback was executed");
assert.ok(!data["last"].args[0], "The error argument was incorrectly set");
assert.equal(data["last"].args[1], "hello", "The incorrect arguments were sent");
this.syncVal(this.data);
},
callback
);
},
"no args to first callback": check.emptyArgs("start"),
"correct callbacks were called": check.coverage([ "start" ]),
"correct callback were skipped": function(data) {
assert.ok(!data["skip1"], "First skipped callback was executed");
assert.ok(!data["skip2"], "Second skipped callback was executed");
},
"correct callbacks were called": function(data) {
assert.ok(data.exited, "External jumpTo function was executed.");
},
"correct arguments were passed to external function": function(data) {
assert.deepEqual(data.exitArgs, ['foo', 'bar', 'baz']);
}
tests.finished();
}
}).export(module);
);
TwoStep(
function first(err) {
check.save(this, arguments);
this.jumpTo("fourth", [ err, "go to fourth" ]);
},
function second(err) {
check.save(this, arguments);
this.syncVal("foo");
},
function third(err) {
check.save(this, arguments);
this.jumpTo("fifth", [ err, "go to fifth" ]);
},
function fourth(err) {
check.save(this, arguments);
this.jumpTo("second", [ err, "go to second" ]);
},
function fifth(err) {
check.save(this, arguments);
this.syncVal(this.data);
},
function done(err, data) {
if(err) { throw err; }
check.emptyArgs("first")(data);
check.coverage([ "first", "second", "third", "fourth", "fifth" ])(data);
check.order([ "first", "fourth", "second", "third", "fifth" ])(data);
assert.ok(!data["second"].args[0], "The error argument was incorrectly set");
assert.equal(data["second"].args[1], "go to second", "The incorrect arguments were sent");
assert.equal(data["third"].args[1], "foo", "The incorrect arguments were sent");
assert.equal(data["fourth"].args[1], "go to fourth", "The incorrect arguments were sent");
assert.equal(data["fifth"].args[1], "go to fifth", "The incorrect arguments were sent");
tests.finished();
}
);
function done(err, data) {
if(err) { throw err; }
check.emptyArgs("start")(data);
check.coverage([ "start" ])(data);
assert.ok(!data["skip1"], "First skipped callback was executed");
assert.ok(!data["skip2"], "Second skipped callback was executed");
assert.ok(data.exited, "External jumpTo function was executed.");
assert.deepEqual(data.exitArgs, ['foo', 'bar', 'baz']);
tests.finished();
}
function exit() {
this.data.exited = true;
this.data.exitArgs = Array.prototype.slice.call(arguments);
done(null, this.data);
}
TwoStep(
function start() {
check.save(this, arguments);
this.jumpTo(exit, ['foo', 'bar', 'baz']);
},
function skip1() {
check.save(this, arguments);
},
function skip2() {
check.save(this, arguments);
this.syncVal(this.data);
},
done
);

@@ -1,2 +0,1 @@

var vows = require("vows");
var assert = require("assert");

@@ -7,71 +6,81 @@ var TwoStep = require("../");

vows.describe("Test `this.listen`").addBatch({
"basic test": {
topic: function() {
TwoStep(
function calls() {
check.save(this, arguments);
var tests = {
expected: 2,
executed: 0,
finished: function() { tests.executed++; }
};
process.on("exit", function() {
assert.equal(tests.executed, tests.expected);
console.log("All done. Everything passed.");
});
var evt = new EventEmitter();
TwoStep(
function calls() {
check.save(this, arguments);
this.listen(evt);
var evt = new EventEmitter();
setTimeout(function() { evt.emit("data", "one"); }, 0);
setTimeout(function() { evt.emit("data", "two"); }, 1);
setTimeout(function() { evt.emit("data", "three"); }, 2);
setTimeout(function() { evt.emit("end"); }, 3);
},
function results(err, chunks) {
if(err) { throw err; }
this.listen(evt);
check.save(this, arguments);
setTimeout(function() { evt.emit("data", "one"); }, 0);
setTimeout(function() { evt.emit("data", "two"); }, 1);
setTimeout(function() { evt.emit("data", "three"); }, 2);
setTimeout(function() { evt.emit("end"); }, 3);
},
function results(err, chunks) {
if(err) { throw err; }
this.syncVal(this.data);
},
this.callback
);
},
"no args to first callback": check.emptyArgs("calls"),
"every step called": check.coverage([ "calls", "results" ]),
"steps called in order": check.order([ "calls", "results" ]),
"check results": function(data) {
assert.deepEqual(data["results"].args[1], [ "one", "two", "three" ]);
}
check.save(this, arguments);
this.syncVal(this.data);
},
"test error handling": {
topic: function() {
TwoStep(
function calls() {
check.save(this, arguments);
function done(err, data) {
if(err) { throw err; }
var evt = new EventEmitter();
check.emptyArgs("calls")(data);
check.coverage([ "calls", "results" ])(data);
check.order([ "calls", "results" ])(data);
assert.deepEqual(data["results"].args[1], [ "one", "two", "three" ]);
this.listen(evt, "emitter-param");
tests.finished();
}
);
setTimeout(function() { evt.emit("data", "one"); }, 0);
setTimeout(function() { evt.emit("data", "two"); }, 1);
setTimeout(function() { evt.emit("data", "three"); }, 2);
setTimeout(function() { evt.emit("error", { message: "emitted error" }); }, 3);
},
function results(err, chunks) {
check.save(this, arguments);
TwoStep(
function calls() {
check.save(this, arguments);
this.syncVal(this.data);
},
this.callback
);
},
"no args to first callback": check.emptyArgs("calls"),
"every step called": check.coverage([ "calls", "results" ]),
"steps called in order": check.order([ "calls", "results" ]),
"check results": function(data) {
var err = data["results"].args[0];
assert.ok(err, "Error was not detected");
assert.ok(err.step, "Step info object was not added");
assert.equal(err.message, "emitted error");
assert.equal(err.step.name, "calls", "Incorrect step name specified in error obj");
assert.equal(err.step.paramName, "emitter-param", "Incorrect param name specified in error obj");
assert.equal(err.step.paramIdx, 1, "Incorrect param index specified in error obj");
}
var evt = new EventEmitter();
this.listen(evt, "emitter-param");
setTimeout(function() { evt.emit("data", "one"); }, 0);
setTimeout(function() { evt.emit("data", "two"); }, 1);
setTimeout(function() { evt.emit("data", "three"); }, 2);
setTimeout(function() { evt.emit("error", { message: "emitted error" }); }, 3);
},
function results(err, chunks) {
check.save(this, arguments);
this.syncVal(this.data);
},
function done(err, data) {
if(err) { throw err; }
check.emptyArgs("calls")(data);
check.coverage([ "calls", "results" ])(data);
check.order([ "calls", "results" ])(data);
err = data["results"].args[0];
assert.ok(err, "Error was not detected");
assert.ok(err.step, "Step info object was not added");
assert.equal(err.message, "emitted error");
assert.equal(err.step.name, "calls", "Incorrect step name specified in error obj");
assert.equal(
err.step.paramName, "emitter-param", "Incorrect param name specified in error obj"
);
assert.equal(err.step.paramIdx, 1, "Incorrect param index specified in error obj");
tests.finished();
}
}).export(module);
);
var fs = require("fs");
var vows = require("vows");
var assert = require("assert");

@@ -7,126 +6,134 @@ var TwoStep = require("../");

vows.describe("Test `this.val`").addBatch({
"basic test passing one param": {
topic: function() {
TwoStep(
function readSelf() {
check.save(this, arguments);
var tests = {
expected: 4,
executed: 0,
finished: function() { tests.executed++; }
};
process.on("exit", function() {
assert.equal(tests.executed, tests.expected);
console.log("All done. Everything passed.");
});
fs.readFile(__filename, 'utf8', this.val());
},
function capitalize(err, text) {
if(err) { throw err; }
TwoStep(
function readSelf() {
check.save(this, arguments);
check.save(this, arguments);
fs.readFile(__filename, 'utf8', this.val());
},
function capitalize(err, text) {
if(err) { throw err; }
this.syncVal(text.toUpperCase());
},
function showIt(err, newText) {
if(err) { throw err; }
check.save(this, arguments);
check.save(this, arguments);
this.syncVal(text.toUpperCase());
},
function showIt(err, newText) {
if(err) { throw err; }
this.syncVal(this.data);
},
this.callback
);
},
"no args to first callback": check.emptyArgs("readSelf"),
"every step called": check.coverage([ "readSelf", "capitalize", "showIt" ]),
"steps called in order": check.order([ "readSelf", "capitalize", "showIt" ]),
"check file loaded": function(data) {
var testText = fs.readFileSync(__filename, 'utf8');
assert.equal(testText, data["capitalize"].args[1], "Text Loaded");
},
"check text is capitalize": function(data) {
assert.equal(data["capitalize"].args[1].toUpperCase(), data["showIt"].args[1], "Text Uppercased");
}
check.save(this, arguments);
this.syncVal(this.data);
},
"multiple async params test": {
topic: function() {
TwoStep(
function calls() {
check.save(this, arguments);
function done(err, data) {
if(err) { throw err; }
var p1 = this.val(), p2 = this.val();
setTimeout(function() { p1(null, 1); }, 0);
setTimeout(function() { p2(null, 2); }, 0);
},
function results(err, one, two) {
if(err) { throw err; }
check.emptyArgs("readSelf")(data);
check.coverage([ "readSelf", "capitalize", "showIt" ])(data);
check.order([ "readSelf", "capitalize", "showIt" ])(data);
check.save(this, arguments);
var testText = fs.readFileSync(__filename, 'utf8');
assert.equal(testText, data["capitalize"].args[1], "Text Loaded");
assert.equal(
data["capitalize"].args[1].toUpperCase(), data["showIt"].args[1], "Text Uppercased"
);
this.syncVal(this.data);
},
this.callback
);
},
"no args to first callback": check.emptyArgs("calls"),
"every step called": check.coverage([ "calls", "results" ]),
"steps called in order": check.order([ "calls", "results" ]),
"confirm results": function(data) {
assert.equal(data["results"].args[1], 1, "First async argument is incorrect");
assert.equal(data["results"].args[2], 2, "Second async argument is incorrect");
}
tests.finished();
}
);
TwoStep(
function calls() {
check.save(this, arguments);
var p1 = this.val(), p2 = this.val();
setTimeout(function() { p1(null, 1); }, 0);
setTimeout(function() { p2(null, 2); }, 0);
},
"multiple sync params test": {
topic: function() {
TwoStep(
function calls() {
check.save(this, arguments);
function results(err, one, two) {
if(err) { throw err; }
var p1 = this.val(), p2 = this.val();
p1(null, 1);
p2(null, 2);
},
function results(err, one, two) {
if(err) { throw err; }
check.save(this, arguments);
check.save(this, arguments);
this.syncVal(this.data);
},
function done(err, data) {
if(err) { throw err; }
this.syncVal(this.data);
},
this.callback
);
},
"no args to first callback": check.emptyArgs("calls"),
"every step called": check.coverage([ "calls", "results" ]),
"steps called in order": check.order([ "calls", "results" ]),
"confirm results": function(data) {
assert.equal(data["results"].args[1], 1, "First sync argument is incorrect");
assert.equal(data["results"].args[2], 2, "Second sync argument is incorrect");
}
check.emptyArgs("calls")(data);
check.coverage([ "calls", "results" ])(data);
check.order([ "calls", "results" ])(data);
assert.equal(data["results"].args[1], 1, "First async argument is incorrect");
assert.equal(data["results"].args[2], 2, "Second async argument is incorrect");
tests.finished();
}
);
TwoStep(
function calls() {
check.save(this, arguments);
var p1 = this.val(), p2 = this.val();
p1(null, 1);
p2(null, 2);
},
"multiple out of order sync and async params test": {
topic: function() {
TwoStep(
function calls() {
check.save(this, arguments);
function results(err, one, two) {
if(err) { throw err; }
var p1 = this.val();
setTimeout(function() { p1(null, 1); }, 10);
this.syncVal(2);
var p3 = this.val();
setTimeout(function() { p3(null, 3); }, 0);
},
function results(err, one, two, three) {
if(err) { throw err; }
check.save(this, arguments);
check.save(this, arguments);
this.syncVal(this.data);
},
function done(err, data) {
if(err) { throw err; }
this.syncVal(this.data);
},
this.callback
);
},
"no args to first callback": check.emptyArgs("calls"),
"every step called": check.coverage([ "calls", "results" ]),
"steps called in order": check.order([ "calls", "results" ]),
"confirm results": function(data) {
assert.equal(data["results"].args[1], 1, "First async argument is incorrect");
assert.equal(data["results"].args[2], 2, "Second sync argument is incorrect");
assert.equal(data["results"].args[3], 3, "Third async argument is incorrect");
}
check.emptyArgs("calls")(data);
check.coverage([ "calls", "results" ])(data);
check.order([ "calls", "results" ])(data);
assert.equal(data["results"].args[1], 1, "First sync argument is incorrect");
assert.equal(data["results"].args[2], 2, "Second sync argument is incorrect");
tests.finished();
}
}).export(module);
);
TwoStep(
function calls() {
check.save(this, arguments);
var p1 = this.val();
setTimeout(function() { p1(null, 1); }, 10);
this.syncVal(2);
var p3 = this.val();
setTimeout(function() { p3(null, 3); }, 0);
},
function results(err, one, two, three) {
if(err) { throw err; }
check.save(this, arguments);
this.syncVal(this.data);
},
function done(err, data) {
if(err) { throw err; }
check.emptyArgs("calls")(data);
check.coverage([ "calls", "results" ])(data);
check.order([ "calls", "results" ])(data);
assert.equal(data["results"].args[1], 1, "First async argument is incorrect");
assert.equal(data["results"].args[2], 2, "Second sync argument is incorrect");
assert.equal(data["results"].args[3], 3, "Third async argument is incorrect");
tests.finished();
}
);
var fs = require("fs");
var vows = require("vows");
var assert = require("assert");

@@ -7,173 +6,186 @@ var TwoStep = require("../");

vows.describe("Test `this.valArray`").addBatch({
"basic valArray test reading directories": {
topic: function() {
TwoStep(
function readDir() {
check.save(this, arguments);
fs.readdir(__dirname, this.val());
},
function readFiles(err, results) {
if(err) { throw err; }
var tests = {
expected: 5,
executed: 0,
finished: function() { tests.executed++; }
};
process.on("exit", function() {
assert.equal(tests.executed, tests.expected);
console.log("All done. Everything passed.");
});
check.save(this, arguments);
TwoStep(
function readDir() {
check.save(this, arguments);
fs.readdir(__dirname, this.val());
},
function readFiles(err, results) {
if(err) { throw err; }
var files = this.valArray();
results.forEach(function (filename) {
if (/\.js$/.test(filename)) {
fs.readFile(__dirname + "/" + filename, 'utf8', files.val());
}
});
},
function showAll(err, files) {
if(err) { throw err; }
check.save(this, arguments);
check.save(this, arguments);
var files = this.valArray();
results.forEach(function (filename) {
if (/\.js$/.test(filename)) {
fs.readFile(__dirname + "/" + filename, 'utf8', files.val());
}
});
},
function showAll(err, files) {
if(err) { throw err; }
this.syncVal(this.data);
},
this.callback
);
},
"no args to first callback": check.emptyArgs("readDir"),
"every step called": check.coverage([ "readDir", "readFiles", "showAll" ]),
"steps called in order": check.order([ "readDir", "readFiles", "showAll" ]),
"check results": function(data) {
var dirListing = fs.readdirSync(__dirname);
var dirResults = dirListing.map(function (filename) {
return fs.readFileSync(__dirname + "/" + filename, 'utf8');
});
check.save(this, arguments);
assert.deepEqual(data["readFiles"].args[1], dirListing);
assert.deepEqual(data["showAll"].args[1], dirResults);
}
this.syncVal(this.data);
},
"test empty valArray": {
topic: function() {
TwoStep(
function calls() {
check.save(this, arguments);
this.valArray();
},
function results(err, arr) {
if(err) { throw err; }
check.save(this, arguments);
function done(err, data) {
if(err) { throw err; }
this.syncVal(this.data);
},
this.callback
);
},
"no args to first callback": check.emptyArgs("calls"),
"every step called": check.coverage([ "calls", "results" ]),
"steps called in order": check.order([ "calls", "results" ]),
"check results": function(data) {
assert.deepEqual(data["results"].args[1], [], "Empty valArray didn't result in empty array");
}
check.emptyArgs("readDir")(data);
check.coverage([ "readDir", "readFiles", "showAll" ])(data);
check.order([ "readDir", "readFiles", "showAll" ])(data);
var dirListing = fs.readdirSync(__dirname);
var dirResults = dirListing.map(function (filename) {
return fs.readFileSync(__dirname + "/" + filename, 'utf8');
});
assert.deepEqual(data["readFiles"].args[1], dirListing);
assert.deepEqual(data["showAll"].args[1], dirResults);
tests.finished();
}
);
TwoStep(
function calls() {
check.save(this, arguments);
this.valArray();
},
"test sync calls to valArray async callbacks": {
topic: function() {
TwoStep(
function calls() {
check.save(this, arguments);
function results(err, arr) {
if(err) { throw err; }
var group = this.valArray();
var p1 = group.val(), p2 = group.val();
p1(null, 1);
p2(null, 2);
},
function results(err, arr) {
if(err) { throw err; }
check.save(this, arguments);
check.save(this, arguments);
this.syncVal(this.data);
},
this.callback
);
},
"no args to first callback": check.emptyArgs("calls"),
"every step called": check.coverage([ "calls", "results" ]),
"steps called in order": check.order([ "calls", "results" ]),
"check results": function(data) {
assert.deepEqual(data["results"].args[1], [ 1, 2 ], "Results array from sync values is incorrect");
}
this.syncVal(this.data);
},
"test mixed sync and async values": {
topic: function() {
TwoStep(
function calls(err, num) {
check.save(this, arguments);
function done(err, data) {
if(err) { throw err; }
var group = this.valArray();
var p1 = group.val();
setTimeout(function() { p1(null, 1); }, 10);
group.syncVal(2);
var p3 = group.val();
setTimeout(function() { p3(null, 3); }, 0);
},
function results(err, arr) {
if(err) { throw err; }
check.save(this, arguments);
check.emptyArgs("calls")(data);
check.coverage([ "calls", "results" ])(data);
check.order([ "calls", "results" ])(data);
assert.deepEqual(
data["results"].args[1], [], "Empty valArray didn't result in empty array"
);
this.syncVal(this.data);
},
this.callback
);
},
"no args to first callback": check.emptyArgs("calls"),
"every step called": check.coverage([ "calls", "results" ]),
"steps called in order": check.order([ "calls", "results" ]),
"check results": function(data) {
assert.deepEqual(
data["results"].args[1], [ 1, 2, 3 ],
"Results array from mixed sync and async values is incorrect"
);
}
tests.finished();
}
);
TwoStep(
function calls() {
check.save(this, arguments);
var group = this.valArray();
var p1 = group.val(), p2 = group.val();
p1(null, 1);
p2(null, 2);
},
"multi-valArray with mixed sync and async values": {
topic: function() {
TwoStep(
function calls(err, num) {
check.save(this, arguments);
function results(err, arr) {
if(err) { throw err; }
var group1 = this.valArray();
var g1p1 = group1.val();
setTimeout(function() { g1p1(null, 1); }, 10);
group1.syncVal(2);
var g1p3 = group1.val();
setTimeout(function() { g1p3(null, 3); }, 0);
check.save(this, arguments);
var group2 = this.valArray();
var g2p1 = group2.val(), g2p2 = group2.val(), g2p3 = group2.val();
setTimeout(function() { g2p1(null, "a"); }, 0);
setTimeout(function() { g2p2(null, "b"); }, 0);
setTimeout(function() { g2p3(null, "c"); }, 0);
},
function results(err, results1, results2) {
if(err) { throw err; }
check.save(this, arguments);
this.syncVal(this.data);
},
function done(err, data) {
if(err) { throw err; }
this.syncVal(this.data);
},
this.callback
);
},
"no args to first callback": check.emptyArgs("calls"),
"every step called": check.coverage([ "calls", "results" ]),
"steps called in order": check.order([ "calls", "results" ]),
"check results": function(data) {
assert.deepEqual(
data["results"].args[1], [ 1, 2, 3 ],
"Results array with mixed sync and async values is incorrect"
);
assert.deepEqual(
data["results"].args[2], [ "a", "b", "c" ],
"Results array from mixed sync and async values is incorrect"
);
}
check.emptyArgs("calls")(data);
check.coverage([ "calls", "results" ])(data);
check.order([ "calls", "results" ])(data);
assert.deepEqual(
data["results"].args[1], [ 1, 2 ], "Results array from sync values is incorrect"
);
tests.finished();
}
}).export(module);
);
TwoStep(
function calls(err, num) {
check.save(this, arguments);
var group = this.valArray();
var p1 = group.val();
setTimeout(function() { p1(null, 1); }, 10);
group.syncVal(2);
var p3 = group.val();
setTimeout(function() { p3(null, 3); }, 0);
},
function results(err, arr) {
if(err) { throw err; }
check.save(this, arguments);
this.syncVal(this.data);
},
function done(err, data) {
if(err) { throw err; }
check.emptyArgs("calls")(data);
check.coverage([ "calls", "results" ])(data);
check.order([ "calls", "results" ])(data);
assert.deepEqual(
data["results"].args[1], [ 1, 2, 3 ],
"Results array from mixed sync and async values is incorrect"
);
tests.finished();
}
);
TwoStep(
function calls(err, num) {
check.save(this, arguments);
var group1 = this.valArray();
var g1p1 = group1.val();
setTimeout(function() { g1p1(null, 1); }, 10);
group1.syncVal(2);
var g1p3 = group1.val();
setTimeout(function() { g1p3(null, 3); }, 0);
var group2 = this.valArray();
var g2p1 = group2.val(), g2p2 = group2.val(), g2p3 = group2.val();
setTimeout(function() { g2p1(null, "a"); }, 0);
setTimeout(function() { g2p2(null, "b"); }, 0);
setTimeout(function() { g2p3(null, "c"); }, 0);
},
function results(err, results1, results2) {
if(err) { throw err; }
check.save(this, arguments);
this.syncVal(this.data);
},
function done(err, data) {
if(err) { throw err; }
check.emptyArgs("calls")(data);
check.coverage([ "calls", "results" ])(data);
check.order([ "calls", "results" ])(data);
assert.deepEqual(
data["results"].args[1], [ 1, 2, 3 ],
"Results array with mixed sync and async values is incorrect"
);
assert.deepEqual(
data["results"].args[2], [ "a", "b", "c" ],
"Results array from mixed sync and async values is incorrect"
);
tests.finished();
}
);
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc