Skip to content
Browse files

Write unit tests.

1 parent 58eac25 commit e4addcae08a7c280814710de6a14a70608789cf5 @creationix committed
Showing with 148 additions and 85 deletions.
  1. +4 −5 README.markdown
  2. +0 −80 test.js
  3. +26 −0 test/callbackTest.js
  4. +25 −0 test/errorTest.js
  5. +21 −0 test/fnTest.js
  6. +33 −0 test/groupTest.js
  7. +17 −0 test/helper.js
  8. +22 −0 test/parallelTest.js
View
9 README.markdown
@@ -15,13 +15,12 @@ The step library exports a single function I call `Step`. It accepts any number
fs.readFile(__filename, this);
},
function capitalize(err, text) {
- if (err) {
- throw err;
- }
+ if (err) throw err;
return text.toUpperCase();
},
function showIt(err, newText) {
- sys.puts(newText);
+ if (err) throw err;
+ console.log(newText);
}
);
@@ -59,7 +58,7 @@ Also you can use group with a dynamic number of common tasks.
var group = this.group();
results.forEach(function (filename) {
if (/\.js$/.test(filename)) {
- fs.readFile(__dirname + "/" + filename, group());
+ fs.readFile(__dirname + "/" + filename, 'utf8', group());
}
});
},
View
80 test.js
@@ -1,80 +0,0 @@
-var Step = require(__dirname + "/lib/step"),
- fs = require('fs'),
- sys = require('sys');
-
-Step(
- function () {
- var group = this.group();
- [1,2,3,4,5,6].forEach(function (num) {
- fs.readFile(__filename, group());
- });
- },
- function (err, contents) {
- if (err) { throw err; }
- sys.p(contents);
- }
-);
-
-Step(
- function readSelf() {
- fs.readFile(__filename, this);
- },
- function capitalize(err, text) {
- if (err) {
- throw err;
- }
- return text.toUpperCase();
- },
- function showIt(err, newText) {
- sys.puts(newText);
- }
-);
-
-
-Step(
- // Loads two files in parallel
- function loadStuff() {
- fs.readFile(__filename, this.parallel());
- fs.readFile("/etc/passwd", this.parallel());
- },
- // Show the result when done
- function showStuff(err, code, users) {
- if (err) throw err;
- sys.puts(code);
- sys.puts(users);
- }
-)
-
-Step(
- function readDir() {
- fs.readdir(__dirname, this);
- },
- function readFiles(err, results) {
- if (err) throw err;
- // Create a closure to the parallel helper
- var parallel = this.parallel;
- results.forEach(function (filename) {
- if (/\.js$/.test(filename)) {
- fs.readFile(__dirname + "/" + filename, parallel());
- }
- });
- },
- function showAll(err /*, file1, file2, ...*/) {
- if (err) throw err;
- var files = Array.prototype.slice.call(arguments, 1);
- sys.p(files);
- }
-);
-
-var myfn = Step.fn(
- function (name) {
- fs.readFile(name, this);
- },
- function capitalize(err, text) {
- if (err) {
- throw err;
- }
- return text.toUpperCase();
- }
-);
-myfn(__filename, sys.p);
View
26 test/callbackTest.js
@@ -0,0 +1,26 @@
+require('./helper');
+
+var selfText = fs.readFileSync(__filename, 'utf8');
+
+// This example tests passing async results and sync results to the next layer
+
+expect('one');
+expect('two');
+expect('three');
+Step(
+ function readSelf() {
+ fulfill("one");
+ fs.readFile(__filename, 'utf8', this);
+ },
+ function capitalize(err, text) {
+ fulfill("two");
+ if (err) throw err;
+ assert.equal(selfText, text, "Text Loaded");
+ return text.toUpperCase();
+ },
+ function showIt(err, newText) {
+ fulfill("three");
+ if (err) throw err;
+ assert.equal(selfText.toUpperCase(), newText, "Text Uppercased");
+ }
+);
View
25 test/errorTest.js
@@ -0,0 +1,25 @@
+require('./helper');
+
+var exception = new Error('Catch me!');
+
+expect('one');
+expect('two');
+expect('three');
+Step(
+ function () {
+ fulfill('one');
+ var callback = this;
+ setTimeout(function () {
+ callback(exception);
+ });
+ },
+ function (err) {
+ fulfill('two');
+ assert.equal(exception, err, "error should passed through");
+ throw exception;
+ },
+ function (err) {
+ fulfill('three');
+ assert.equal(exception, err, "error should be caught");
+ }
+);
View
21 test/fnTest.js
@@ -0,0 +1,21 @@
+require('./helper');
+
+
+var myfn = Step.fn(
+ function (name) {
+ fs.readFile(name, 'utf8', this);
+ },
+ function capitalize(err, text) {
+ if (err) throw err;
+ return text.toUpperCase();
+ }
+);
+
+var selfText = fs.readFileSync(__filename, 'utf8');
+
+expect('result');
+myfn(__filename, function (err, result) {
+ fulfill('result');
+ if (err) throw err;
+ assert.equal(selfText.toUpperCase(), result, "It should work");
+});
View
33 test/groupTest.js
@@ -0,0 +1,33 @@
+require('./helper');
+
+var dirListing = fs.readdirSync(__dirname),
+ dirResults = dirListing.map(function (filename) {
+ return fs.readFileSync(__dirname + "/" + filename, 'utf8');
+ });
+
+expect('one');
+expect('two');
+expect('three');
+Step(
+ function readDir() {
+ fulfill('one');
+ fs.readdir(__dirname, this);
+ },
+ function readFiles(err, results) {
+ fulfill('two');
+ if (err) throw err;
+ // Create a new group
+ assert.deepEqual(dirListing, results);
+ var group = this.group();
+ results.forEach(function (filename) {
+ if (/\.js$/.test(filename)) {
+ fs.readFile(__dirname + "/" + filename, 'utf8', group());
+ }
+ });
+ },
+ function showAll(err , files) {
+ fulfill('three');
+ if (err) throw err;
+ assert.deepEqual(dirResults, files);
+ }
+);
View
17 test/helper.js
@@ -0,0 +1,17 @@
+global.assert = require('assert');
+global.fs = require('fs');
+global.Step = require('../lib/step');
+
+// A mini expectations module to ensure expected callback fire at all.
+var expectations = {};
+global.expect = function expect(message) {
+ expectations[message] = new Error("Missing expectation: " + message);
+}
+global.fulfill = function fulfill(message) {
+ delete expectations[message];
+}
+process.addListener('exit', function () {
+ Object.keys(expectations).forEach(function (message) {
+ throw expectations[message];
+ });
+});
View
22 test/parallelTest.js
@@ -0,0 +1,22 @@
+require('./helper');
+
+var selfText = fs.readFileSync(__filename, 'utf8'),
+ etcText = fs.readFileSync('/etc/passwd', 'utf8');
+
+expect('one');
+expect('two');
+Step(
+ // Loads two files in parallel
+ function loadStuff() {
+ fulfill('one');
+ fs.readFile(__filename, this.parallel());
+ fs.readFile("/etc/passwd", this.parallel());
+ },
+ // Show the result when done
+ function showStuff(err, code, users) {
+ fulfill('two');
+ if (err) throw err;
+ assert.equal(selfText, code, "Code should come first");
+ assert.equal(etcText, users, "Users should come second");
+ }
+);

0 comments on commit e4addca

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