Permalink
Browse files

doing some clean up in the test suite

  • Loading branch information...
1 parent e060724 commit ea38aeca368bdc9b199b5b382ccf4aa094f9b1a3 @jmar777 committed Nov 24, 2013
Showing with 117 additions and 143 deletions.
  1. +3 −4 test/fork.js
  2. +70 −0 test/general.js
  3. +7 −58 test/promises.js
  4. +17 −73 test/resume.js
  5. +20 −8 test/snafus.js
View
7 test/fork.js
@@ -1,8 +1,8 @@
var assert = require('assert'),
suspend = require('../');
-describe('suspend\'s fork API', function() {
- it('should allow multiple async operations in parallel', function(done) {
+describe('suspend.fork()', function() {
+ it('should support concurrent operations', function(done) {
suspend(function* () {
asyncDouble(7, suspend.fork());
asyncTriple(7, suspend.fork());
@@ -12,7 +12,7 @@ describe('suspend\'s fork API', function() {
})();
});
- it('should preserve result order based on calls to fork()', function(done) {
+ it('should order results based on calls to fork()', function(done) {
suspend(function* () {
slowAsyncDouble(3, suspend.fork());
asyncDouble(4, suspend.fork());
@@ -52,7 +52,6 @@ describe('suspend\'s fork API', function() {
done();
})();
});
-
});
// async functions used for test cases
View
70 test/general.js
@@ -0,0 +1,70 @@
+var assert = require('assert'),
+ suspend = require('../'),
+ Q = require('q');
+
+describe('suspend(fn*)', function() {
+ it('should support parameterized initialization', function(done) {
+ suspend(function*(foo) {
+ assert.strictEqual('bar', foo);
+ done();
+ })('bar');
+ });
+
+ it('should preserve wrapper context in generator body', function(done) {
+ suspend(function*() {
+ assert.strictEqual('bar', this.foo);
+ done();
+ }).apply({ foo: 'bar' });
+ });
+
+ it('should support running generators in parallel', function(done) {
+ var doneCount = 0;
+
+ suspend(function* (num) {
+ var doubled = yield asyncDouble(num),
+ tripled = yield asyncTriple(doubled),
+ squared = yield asyncSquare(tripled);
+ assert.strictEqual(324, squared);
+ ++doneCount === 2 && done();
+ })(3);
+
+ suspend(function* (num) {
+ var squared = yield asyncSquare(num),
+ tripled = yield asyncTriple(squared),
+ doubled = yield asyncDouble(tripled);
+ assert.strictEqual(54, doubled);
+ ++doneCount === 2 && done();
+ })(3);
+ });
+
+ it('should support nesting', function(done) {
+ suspend(function* (num) {
+ suspend(function* (num2) {
+ assert.strictEqual(18, yield asyncTriple(num2));
+ done();
+ })(yield asyncDouble(num));
+ })(3);
+ });
+});
+
+// async functions used for test cases
+function asyncDouble(x) {
+ var deferred = Q.defer();
+ setTimeout(function() { deferred.resolve(x * 2); }, 20);
+ return deferred.promise;
+}
+function asyncTriple(x) {
+ var deferred = Q.defer();
+ setTimeout(function() { deferred.resolve(x * 3); }, 20);
+ return deferred.promise;
+}
+function asyncSquare(x) {
+ var deferred = Q.defer();
+ setTimeout(function() { deferred.resolve(x * x); }, 20);
+ return deferred.promise;
+}
+function asyncError() {
+ var deferred = Q.defer();
+ setTimeout(function() { deferred.reject(new Error('fail')); }, 20);
+ return deferred.promise;
+}
View
65 test/promises.js
@@ -2,76 +2,25 @@ var assert = require('assert'),
suspend = require('../'),
Q = require('q');
-describe('suspend\'s promise API', function() {
-
- describe('with default options', function() {
- it('should resolve node-style callbacks', function(done) {
+describe('suspend(fn*)', function() {
+ describe('with promises', function() {
+ it('should resolve correctly', function(done) {
suspend(function*() {
- var doubled = yield asyncDouble(42);
- assert.strictEqual(doubled, 84);
+ assert.strictEqual(84, yield asyncDouble(42));
done();
})();
});
- it('should allow arguments to be passed on initialization', function(done) {
- suspend(function*(foo) {
- assert.strictEqual(foo, 'bar');
- done();
- })('bar');
- });
-
- it('should preserve initializer context for the generator body', function(done) {
- suspend(function*() {
- assert.strictEqual(this.foo, 'bar');
- done();
- }).apply({ foo: 'bar' });
- });
-
- it('should work with multiple generators in parallel', function(done) {
- var doneCount = 0;
-
- suspend(function* (num) {
- var doubled = yield asyncDouble(num);
- var tripled = yield asyncTriple(doubled);
- var squared = yield asyncSquare(tripled);
- assert.strictEqual(squared, 324);
- ++doneCount === 2 && done();
- })(3);
-
- suspend(function* (num) {
- var squared = yield asyncSquare(num);
- var tripled = yield asyncTriple(squared);
- var doubled = yield asyncDouble(tripled);
- assert.strictEqual(doubled, 54);
- ++doneCount === 2 && done();
- })(3);
- });
-
- it('should work when nested', function(done) {
- var doneCount = 0;
-
- suspend(function* (num) {
- var doubled = yield asyncDouble(num);
-
- suspend(function* () {
- var tripled = yield asyncTriple(doubled);
- assert.strictEqual(tripled, 18);
- done();
- })();
- })(3);
- });
-
- it('should throw errors returned from async functions', function(done) {
+ it('should throw errors', function(done) {
suspend(function* () {
try {
yield asyncError();
} catch (err) {
- assert.strictEqual(err.message, 'fail');
+ assert.strictEqual('fail', err.message);
done();
}
- }, { throw: true })();
+ })();
});
-
});
});
View
90 test/resume.js
@@ -1,104 +1,48 @@
var assert = require('assert'),
- suspend = require('../');
+ suspend = require('../'),
+ resume = suspend.resume,
+ resumeRaw = suspend.resumeRaw;
-describe('suspend\'s resume API', function() {
-
- describe('with default options', function() {
- it('should resolve node-style callbacks', function(done) {
+describe('suspend(fn*)', function() {
+ describe('with suspend.resume()', function() {
+ it('should resolve correctly', function(done) {
suspend(function* () {
- var doubled = yield asyncDouble(42, suspend.resume());
- assert.strictEqual(doubled, 84);
+ assert.strictEqual(84, yield asyncDouble(42, resume()));
done();
})();
});
- it('should allow arguments to be passed on initialization', function(done) {
- suspend(function* (foo) {
- assert.strictEqual(foo, 'bar');
- done();
- })('bar');
- });
-
- it('should preserve initializer context for the generator body', function(done) {
- suspend(function* () {
- assert.strictEqual(this.foo, 'bar');
- done();
- }).apply({ foo: 'bar' });
- });
-
- it('should work with multiple generators in parallel', function(done) {
- var doneCount = 0;
-
- suspend(function* (num) {
- var doubled = yield asyncDouble(num, suspend.resume());
- var tripled = yield asyncTriple(doubled, suspend.resume());
- var squared = yield asyncSquare(tripled, suspend.resume());
- assert.strictEqual(squared, 324);
- ++doneCount === 2 && done();
- })(3);
-
- suspend(function* (num) {
- var squared = yield asyncSquare(num, suspend.resume());
- var tripled = yield asyncTriple(squared, suspend.resume());
- var doubled = yield asyncDouble(tripled, suspend.resume());
- assert.strictEqual(doubled, 54);
- ++doneCount === 2 && done();
- })(3);
- });
-
- it('should work when nested', function(done) {
- var doneCount = 0;
-
- suspend(function* (num) {
- var doubled = yield asyncDouble(num, suspend.resume());
- assert.strictEqual(doubled, 6);
-
- yield suspend(function* (next) {
- var tripled = yield asyncTriple(doubled, suspend.resume());
- assert.strictEqual(tripled, 18);
- next();
- })(suspend.resume());
-
- var tripled = yield asyncTriple(doubled, suspend.resume());
- assert.strictEqual(tripled, 18);
- done();
- })(3);
- });
-
- it('should throw errors returned from async functions', function(done) {
+ it('should throw errors', function(done) {
suspend(function* () {
try {
- yield asyncError(suspend.resume());
+ yield asyncError(resume());
} catch (err) {
assert.strictEqual(err.message, 'fail');
done();
}
- }, { throw: true })();
+ })();
});
-
});
describe('with suspend.resumeRaw()', function() {
- it('should provide results as an array', function(done) {
+ it('should resolve to an array', function(done) {
suspend(function* () {
- var res = yield asyncDouble(42, suspend.resumeRaw());
- assert(Array.isArray(res));
+ assert(Array.isArray(yield asyncDouble(42, resumeRaw())));
done();
})();
});
- it('should return errors as first item in array', function(done) {
+ it('should resolve correctly', function(done) {
suspend(function* () {
- var res = yield asyncError(suspend.resumeRaw());
- assert.strictEqual(res[0].message, 'fail');
+ assert.deepEqual([null, 14], yield asyncDouble(7, resumeRaw()));
done();
})();
});
- it('should return non-error results starting at index 1', function(done) {
+ it('should return errors as first item in array', function(done) {
suspend(function* () {
- var res = yield asyncDouble(42, suspend.resumeRaw());
- assert.strictEqual(res[1], 84);
+ var res = yield asyncError(resumeRaw());
+ assert.strictEqual('fail', res[0].message);
done();
})();
});
View
28 test/snafus.js
@@ -1,8 +1,9 @@
var assert = require('assert'),
- suspend = require('../');
+ suspend = require('../'),
+ resume = suspend.resume;
-describe('suspend', function() {
- it('should throw a descriptive error on multiple calls to resume()', function(done) {
+describe('suspend(fn*)', function() {
+ it('should be descriptive on multiple calls to resume()', function(done) {
suspend(function* () {
// temporarily hijack uncaught errors
var mochaListener = process.listeners('uncaughtException')[0];
@@ -20,11 +21,22 @@ describe('suspend', function() {
}
process.on('uncaughtException', newListener);
- yield evilCallback(suspend.resume());
+ yield evilCallback(resume());
})();
});
- it('should throw a descriptive error when resume() is called outside of the generator body', function(done) {
+ it('should throw an error when resume factory is called without a yield', function(done) {
+ try {
+ resume();
+ } catch (err) {
+ if (!/called from the generator body/.test(err.message)) {
+ return done(new Error('Expected a descriptive error message'));
+ }
+ done();
+ }
+ });
+
+ it('should be descriptive when resume factory is called asynchronously', function(done) {
suspend(function* () {
// temporarily hijack uncaught errors
var mochaListener = process.listeners('uncaughtException')[0];
@@ -43,22 +55,22 @@ describe('suspend', function() {
process.on('uncaughtException', newListener);
yield asyncDouble(2, function() {
- suspend.resume()();
+ resume();
});
})();
});
it('should handle synchronous results', function(done) {
suspend(function* () {
- var doubled = yield syncDouble(42, suspend.resume());
+ var doubled = yield syncDouble(42, resume());
assert.strictEqual(doubled, 84);
done();
})();
});
it('should handle multiple runs', function(done) {
var test = suspend(function* (next) {
- var doubled = yield syncDouble(42, suspend.resume());
+ var doubled = yield syncDouble(42, resume());
assert.strictEqual(doubled, 84);
next();
});

0 comments on commit ea38aec

Please sign in to comment.