Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

individual test files for suspend.*() methods

  • Loading branch information...
commit 9d99fcd2beadb44f45d37e7d0f5abd70e6dfc7bf 1 parent a7b9a77
@jmar777 authored
View
105 test/suspend.async.js
@@ -0,0 +1,105 @@
+var assert = require('assert'),
+ suspend = require('../'),
+ async = suspend.async,
+ resume = suspend.resume;
+
+describe('suspend.async(fn*)', function() {
+ it('should return a function', function(done) {
+ var ret = async(function* () {});
+ assert.strictEqual(typeof ret, 'function');
+ done();
+ });
+
+ it('should not run immediately', function(done) {
+ async(function*() {
+ throw new Error('you ran me');
+ });
+ setTimeout(done, 1);
+ });
+
+ it('should throw if GeneratorFunction argument is missing', function(done) {
+ assert.throws(async, /must be a GeneratorFunction/);
+ done();
+ });
+
+ it('should throw if GeneratorFunction argument is wrong type', function(done) {
+ assert.throws(async.bind(null, 'foo'), /must be a GeneratorFunction/);
+ done();
+ });
+});
+
+describe('suspend.async(fn*)()', function() {
+ it('should invoke callback when done', function(done) {
+ async(function*() {})(done);
+ });
+
+ it('should let callback be optional when no arguments are provided', function(done) {
+ assert.doesNotThrow(async(function*() {}));
+ done();
+ });
+
+ it('should throw if callback argument is wrong type', function(done) {
+ assert.throws(
+ async(function*() {}).bind(null, 'foo'),
+ /must be a callback/
+ );
+ done();
+ });
+
+ it('should preserve `this` binding', function(done) {
+ async(function*() {
+ assert.strictEqual('bar', this.foo);
+ done();
+ }).call({ foo: 'bar' });
+ });
+
+ it('should support input parameters', function(done) {
+ async(function*(foo) {
+ assert.strictEqual('bar', foo);
+ })('bar', done);
+ });
+
+ it('should pass synchronously returned values to callback', function(done) {
+ async(function*() {
+ return 3;
+ })(function(err, val) {
+ assert.strictEqual(val, 3);
+ done();
+ });
+ });
+
+ it('should pass asynchronously resolved values to callback', function(done) {
+ async(function*() {
+ return yield asyncDouble(3, resume());
+ })(function(err, val) {
+ assert.strictEqual(val, 6);
+ done();
+ });
+ });
+
+ it('should pass synchronously thrown errors to callback', function(done) {
+ async(function*() {
+ throw new Error('oops');
+ })(function(err) {
+ assert(err.message === 'oops');
+ done();
+ });
+ });
+
+ it('should pass unhandled asynchronous errors to callback', function(done) {
+ async(function*() {
+ yield asyncError(resume());
+ })(function(err) {
+ assert(err.message === 'oops');
+ done();
+ });
+ });
+});
+
+// functions used for test cases
+function asyncDouble(num, cb) {
+ setTimeout(cb.bind(null, null, num * 2), 20);
+}
+function asyncError(cb) {
+ setTimeout(cb.bind(null, new Error('oops')), 20);
+}
View
53 test/suspend.fn.js
@@ -0,0 +1,53 @@
+var assert = require('assert'),
+ suspend = require('../'),
+ fn = suspend.fn,
+ resume = suspend.resume;
+
+describe('suspend.fn(fn*)', function() {
+ it('should return a function', function(done) {
+ var ret = fn(function* () {});
+ assert.strictEqual(typeof ret, 'function');
+ done();
+ });
+
+ it('should not run immediately', function(done) {
+ fn(function*() {
+ throw new Error('you ran me');
+ });
+ setTimeout(done, 1);
+ });
+
+ it('should throw if GeneratorFunction argument is missing', function(done) {
+ assert.throws(fn, /must be a GeneratorFunction/);
+ done();
+ });
+
+ it('should throw if GeneratorFunction argument is wrong type', function(done) {
+ assert.throws(fn.bind(null, 'foo'), /must be a GeneratorFunction/);
+ done();
+ });
+});
+
+describe('suspend.fn(fn*)()', function() {
+ it('should preserve `this` binding', function(done) {
+ fn(function*() {
+ assert.strictEqual('bar', this.foo);
+ done();
+ }).call({ foo: 'bar' });
+ });
+
+ it('should support input parameters', function(done) {
+ fn(function*(foo) {
+ assert.strictEqual('bar', foo);
+ done();
+ })('bar');
+ });
+});
+
+// functions used for test cases
+function asyncDouble(num, cb) {
+ setTimeout(cb.bind(null, null, num * 2), 20);
+}
+function asyncError(cb) {
+ setTimeout(cb.bind(null, new Error('oops')), 20);
+}
View
9 test/suspend.js
@@ -0,0 +1,9 @@
+var assert = require('assert'),
+ suspend = require('../');
+
+describe('suspend(fn*)', function() {
+ it('should be an alias for suspend.async', function(done) {
+ assert.strictEqual(suspend, suspend.async);
+ done();
+ });
+});
View
87 test/suspend.run.js
@@ -0,0 +1,87 @@
+var assert = require('assert'),
+ suspend = require('../'),
+ run = suspend.run,
+ resume = suspend.resume;
+
+describe('suspend.run(fn*)', function() {
+ it('should run immediately', function(done) {
+ run(function*() {
+ done();
+ });
+ });
+
+ it('should preserve `this` binding', function(done) {
+ run.call({ foo: 'bar' }, function*() {
+ assert.strictEqual('bar', this.foo);
+ done();
+ });
+ });
+
+ it('should throw if GeneratorFunction argument is missing', function(done) {
+ assert.throws(run, /must be a GeneratorFunction/);
+ done();
+ });
+
+ it('should throw if GeneratorFunction argument is wrong type', function(done) {
+ assert.throws(run.bind(null, 'foo'), /must be a GeneratorFunction/);
+ done();
+ });
+});
+
+describe('suspend.run(fn*, cb)', function() {
+ it('should invoke callback when done', function(done) {
+ run(function*() {}, done);
+ });
+
+ it('should throw if callback argument is wrong type', function(done) {
+ assert.throws(
+ run.bind(null, function*() {}, 'foo'),
+ /must be a callback/
+ );
+ done();
+ });
+
+ it('should pass synchronously returned values to callback', function(done) {
+ run(function*() {
+ return 3;
+ }, function(err, val) {
+ assert.strictEqual(val, 3);
+ done();
+ });
+ });
+
+ it('should pass asynchronously resolved values to callback', function(done) {
+ run(function*() {
+ return yield asyncDouble(3, resume());
+ }, function(err, val) {
+ assert.strictEqual(val, 6);
+ done();
+ });
+ });
+
+ it('should pass synchronously thrown errors to callback', function(done) {
+ run(function*() {
+ throw new Error('oops');
+ }, function(err) {
+ assert(err.message === 'oops');
+ done();
+ });
+ });
+
+ it('should pass unhandled asynchronous errors to callback', function(done) {
+ run(function*() {
+ yield asyncError(resume());
+ }, function(err) {
+ assert(err.message === 'oops');
+ done();
+ });
+ });
+});
+
+// functions used for test cases
+function asyncDouble(num, cb) {
+ setTimeout(cb.bind(null, null, num * 2), 20);
+}
+function asyncError(cb) {
+ setTimeout(cb.bind(null, new Error('oops')), 20);
+}
Please sign in to comment.
Something went wrong with that request. Please try again.