Permalink
Browse files

beefing up and reorganizing the test suite

  • Loading branch information...
1 parent def54e8 commit 688aca5bd5bc7cd28b54c03599823c624bc7814b @jmar777 committed Dec 5, 2013
Showing with 461 additions and 359 deletions.
  1. +0 −50 test/callbacks.js
  2. +0 −63 test/fork.js
  3. +0 −78 test/general.js
  4. +0 −37 test/promises.js
  5. +0 −58 test/resume.js
  6. +0 −73 test/snafus.js
  7. +30 −0 test/suspend.async.js
  8. +33 −0 test/suspend.fn.js
  9. +122 −0 test/suspend.fork.js
  10. +197 −0 test/suspend.resume.js
  11. +8 −0 test/suspend.run.js
  12. +71 −0 test/yielding-promises.js
View
@@ -1,50 +0,0 @@
-var assert = require('assert'),
- suspend = require('../'),
- Q = require('q');
-
-describe('suspend(fn*)', function() {
- it('should pass returned values to callbacks', function(done) {
- var test = suspend(function* (num) {
- return yield asyncDouble(num);
- });
-
- test(3, function(err, doubled) {
- assert.strictEqual(6, doubled);
- done();
- });
- });
-
- it('should pass uncaught synchronous errors to callbacks', function(done) {
- var test = suspend(function* () {
- throw new Error('fail');
- });
-
- test(function(err) {
- assert.strictEqual('fail', err.message);
- done();
- });
- });
-
- it('should pass uncaught asynchronous errors to callbacks', function(done) {
- var test = suspend(function* () {
- yield asyncError();
- });
-
- test(function(err) {
- assert.strictEqual('fail', err.message);
- done();
- });
- });
-});
-
-// async functions used for test cases
-function asyncDouble(x) {
- var deferred = Q.defer();
- setTimeout(function() { deferred.resolve(x * 2); }, 20);
- return deferred.promise;
-}
-function asyncError() {
- var deferred = Q.defer();
- setTimeout(function() { deferred.reject(new Error('fail')); }, 20);
- return deferred.promise;
-}
View
@@ -1,63 +0,0 @@
-var assert = require('assert'),
- suspend = require('../');
-
-describe('suspend.fork()', function() {
- it('should support concurrent operations', function(done) {
- suspend(function* () {
- asyncDouble(7, suspend.fork());
- asyncDouble(8, suspend.fork());
- asyncDouble(9, suspend.fork());
- assert.deepEqual([14, 16, 18], yield suspend.join());
- done();
- })();
- });
-
- it('should order results based on calls to fork()', function(done) {
- suspend(function* () {
- slowAsyncDouble(3, suspend.fork());
- asyncDouble(4, suspend.fork());
- assert.deepEqual([6, 8], yield suspend.join());
- done();
- })();
- });
-
- it('should support join() even with no forks()\'s', function(done) {
- suspend(function* () {
- var res = yield suspend.join();
- assert(Array.isArray(res));
- assert.strictEqual(0, res.length);
- done();
- })();
- });
-
- it('should reset properly after a join()', function(done) {
- suspend(function* () {
- asyncDouble(3, suspend.fork());
- asyncDouble(4, suspend.fork());
- assert.deepEqual([6, 8], yield suspend.join());
- asyncDouble(4, suspend.fork());
- asyncDouble(3, suspend.fork());
- assert.deepEqual([8, 6], yield suspend.join());
- done();
- })();
- });
-
- it('should play nice with native control structures', function(done) {
- suspend(function* () {
- for (var i = 0; i < 10; i++) {
- asyncDouble(i, suspend.fork());
- }
- assert.deepEqual([0, 2, 4, 6, 8, 10, 12, 14, 16, 18],
- yield suspend.join());
- done();
- })();
- });
-});
-
-// async functions used for test cases
-function asyncDouble(x, cb) {
- setTimeout(function() { cb(null, x * 2); }, 20);
-}
-function slowAsyncDouble(x, cb) {
- setTimeout(function() { cb(null, x * 2); }, 60);
-}
View
@@ -1,78 +0,0 @@
-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);
- })('bar', done);
- });
-
- it('should preserve wrapper context in generator body', function(done) {
- suspend(function*() {
- assert.strictEqual('bar', this.foo);
- }).apply({ foo: 'bar' }, [done]);
- });
-
- it('should support running generators in parallel', function(done) {
- var doneCount = 0,
- maybeDone = function() { ++doneCount === 2 && done() };
-
- suspend(function* (num) {
- var doubled = yield asyncDouble(num),
- quadrupled = yield asyncDouble(doubled),
- octupled = yield asyncDouble(quadrupled);
- assert.strictEqual(24, octupled);
- })(3, maybeDone);
-
- suspend(function* (num) {
- var doubled = yield asyncDouble(num),
- quadrupled = yield asyncDouble(doubled),
- octupled = yield asyncDouble(quadrupled);
- assert.strictEqual(24, octupled);
- })(3, maybeDone);
- });
-
- it('should support nesting', function(done) {
- suspend(function* (num) {
- suspend(function* (num2) {
- assert.strictEqual(20, yield asyncDouble(num2));
- })(yield asyncDouble(num), done);
- })(5, assert.ifError);
- });
-
- it('should support continuing execution after a handled error', function(done) {
- suspend(function* (num) {
- var doubled = yield asyncDouble(num);
- try {
- yield asyncError();
- } catch (err) {
- // ignore
- }
- assert.strictEqual(28, yield asyncDouble(doubled));
- })(7, done);
- });
-
- it('should handle multiple runs', function(done) {
- var test = suspend(function* () {
- assert.strictEqual(84, yield asyncDouble(42));
- });
-
- test(function() {
- test(done);
- });
- });
-});
-
-// async functions used for test cases
-function asyncDouble(x) {
- var deferred = Q.defer();
- setTimeout(function() { deferred.resolve(x * 2); }, 20);
- return deferred.promise;
-}
-function asyncError() {
- var deferred = Q.defer();
- setTimeout(function() { deferred.reject(new Error('fail')); }, 20);
- return deferred.promise;
-}
View
@@ -1,37 +0,0 @@
-var assert = require('assert'),
- suspend = require('../'),
- Q = require('q');
-
-describe('suspend(fn*)', function() {
- describe('with promises', function() {
- it('should resolve correctly', function(done) {
- suspend(function*() {
- assert.strictEqual(84, yield asyncDouble(42));
- done();
- })();
- });
-
- it('should throw errors', function(done) {
- suspend(function* () {
- try {
- yield asyncError();
- } catch (err) {
- assert.strictEqual('fail', err.message);
- done();
- }
- })();
- });
- });
-});
-
-// async functions used for test cases
-function asyncDouble(x) {
- var deferred = Q.defer();
- setTimeout(function() { deferred.resolve(x * 2); }, 20);
- return deferred.promise;
-}
-function asyncError() {
- var deferred = Q.defer();
- setTimeout(function() { deferred.reject(new Error('fail')); }, 20);
- return deferred.promise;
-}
View
@@ -1,58 +0,0 @@
-var assert = require('assert'),
- suspend = require('../'),
- resume = suspend.resume,
- resumeRaw = suspend.resumeRaw;
-
-describe('suspend(fn*)', function() {
- describe('with suspend.resume()', function() {
- it('should resolve correctly', function(done) {
- suspend(function* () {
- assert.strictEqual(84, yield asyncDouble(42, resume()));
- done();
- })();
- });
-
- it('should throw errors', function(done) {
- suspend(function* () {
- try {
- yield asyncError(resume());
- } catch (err) {
- assert.strictEqual(err.message, 'fail');
- done();
- }
- })();
- });
- });
-
- describe('with suspend.resumeRaw()', function() {
- it('should resolve to an array', function(done) {
- suspend(function* () {
- assert(Array.isArray(yield asyncDouble(42, resumeRaw())));
- done();
- })();
- });
-
- it('should resolve correctly', function(done) {
- suspend(function* () {
- assert.deepEqual([null, 14], yield asyncDouble(7, resumeRaw()));
- done();
- })();
- });
-
- it('should return errors as first item in array', function(done) {
- suspend(function* () {
- var res = yield asyncError(resumeRaw());
- assert.strictEqual('fail', res[0].message);
- done();
- })();
- });
- });
-});
-
-// async functions used for test cases
-function asyncDouble(x, cb) {
- setTimeout(function() { cb(null, x * 2); }, 20);
-}
-function asyncError(cb) {
- setTimeout(function() { cb(new Error('fail')); }, 20);
-}
View
@@ -1,73 +0,0 @@
-var assert = require('assert'),
- suspend = require('../'),
- resume = suspend.resume;
-
-describe('suspend(fn*)', function() {
- it('should be descriptive on multiple calls to resume()', function(done) {
- suspend(function* () {
- captureNextUncaughtException(function(err) {
- if (!/same resumer multiple times/.test(err.message)) {
- return done(new Error('Expected a descriptive error message'));
- }
- done();
- });
- yield evilCallback(resume());
- })();
- });
-
- 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* () {
- captureNextUncaughtException(function(err) {
- if (!/called from the generator body/.test(err.message)) {
- return done(new Error('Expected a descriptive error message'));
- }
- done();
- });
- yield asyncDouble(2, function() {
- resume();
- });
- })();
- });
-
- it('should handle synchronous results', function(done) {
- suspend(function* () {
- var doubled = yield syncDouble(42, resume());
- assert.strictEqual(doubled, 84);
- done();
- })();
- });
-});
-
-// functions used for test cases
-function asyncDouble(x, cb) {
- setTimeout(function() { cb(null, x * 2); }, 20);
-}
-function evilCallback(cb) {
- setTimeout(cb, 20);
- setTimeout(cb, 20);
-}
-function syncDouble(x, cb) {
- cb(null, x * 2);
-}
-function captureNextUncaughtException(cb) {
- var mochaListener = process.listeners('uncaughtException')[0];
- process.removeListener('uncaughtException', mochaListener);
- var newListener = function(err) {
- // restore mocha's listener
- process.removeListener('uncaughtException', newListener);
- process.on('uncaughtException', mochaListener);
- cb(err);
- }
- process.on('uncaughtException', newListener);
-}
Oops, something went wrong.

0 comments on commit 688aca5

Please sign in to comment.