Skip to content

Commit

Permalink
Tests WIP 17
Browse files Browse the repository at this point in the history
  • Loading branch information
overlookmotel committed Jul 3, 2016
1 parent 8172637 commit 4f8a44e
Show file tree
Hide file tree
Showing 7 changed files with 250 additions and 120 deletions.
14 changes: 4 additions & 10 deletions test/methods/catch.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* cls-bluebird tests
* Tests for .catch() / .error()
* Tests for .catch()
*/

/* global describe, it */
Expand All @@ -21,6 +21,9 @@ runTests('.catch()', function(u) {
});

describe('with 2nd arg', function() {
// NB In bluebird v3 handler is not bound when on 2nd arg.
// `.catch()` calls `.then()` synchronously but with proxy handler.
// No way to test for binding.
u.testSetProtoMethodAsync(function(p, handler) {
return p.catch(Error, handler);
}, {catches: true, noUndefined: true, noBind: (u.bluebirdVersion === 3)});
Expand All @@ -32,12 +35,3 @@ runTests('.caught()', function(u, Promise) { // jshint ignore:line
expect(Promise.prototype.caught).to.equal(Promise.prototype.catch);
});
});

/*
// TODO make this work - .error() only catches operational errors
runTests('.error()', function(u) {
u.testSetProtoMethodAsync(function(p, handler) {
return p.error(handler);
}, {catches: true});
});
*/
4 changes: 2 additions & 2 deletions test/methods/constructor.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ runTests('new Promise()', function(u, Promise) {
return new Promise(handler);
};

u.testSetCallbackSync(testFn);
u.testSetCallbackSync(testFn, {handler: function(resolve) { resolve(); }});

u.testSetCallbackNotBound(testFn);
u.testSetCallbackNotBound(testFn, {handler: function(resolve) { resolve(); }});
});
19 changes: 19 additions & 0 deletions test/methods/error.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* cls-bluebird tests
* Tests for .error()
*/

// Imports
var runTests = require('../support');

// Run tests

// TODO make this work in bluebird v3 - .error() only catches operational errors
runTests('.error()', function(u) {
u = u;
/*
u.testSetProtoMethodAsync(function(p, handler) {
return p.error(handler);
}, {catches: true, noUndefined: true, noBind: (u.bluebirdVersion === 3)});
*/
});
152 changes: 102 additions & 50 deletions test/support/checks.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,45 +13,71 @@ module.exports = {
* `fn` is called immediately, and passed a handler.
* If handler is called synchronously, `done` callback is called without error.
* If handler is called asynchronously, `done` callback is called with an error.
* If handler is not called, `done` callback is called with an error.
*
* @param {Function} fn - Function to run.
* @param {Function} done - Final callback to call with result
* @param {Function} done - Final callback to finish test
* @param {Function} error - Callback to call with errors
* @param {Function} [handler] - Optional handler function
* @returns {undefined}
*/
checkSync: function(fn, done) {
var u = this;
var sync = true;
var handler = function() {
u.toCallback(function() {
if (!sync) throw new Error('Called asynchronously');
}, done);
};

fn(handler);
sync = false;
checkSync: function(fn, done, error, handler) {
this.checkSyncAsync(fn, true, done, error, handler);
},

/**
* Runs a function and checks it calls back a handler asynchronously.
* `fn` is called immediately, and passed a handler.
* If handler is called asynchronously, `done` callback is called without error.
* If handler is called synchronously, `done` callback is called with an error.
* If handler is not called, `done` callback is called with an error.
*
* @param {Function} fn - Function to run.
* @param {Function} done - Final callback to call with result
* @param {Function} done - Final callback to finish test
* @param {Function} error - Callback to call with errors
* @param {Function} [handler] - Optional handler function
* @returns {undefined}
*/
checkAsync: function(fn, done) {
var u = this;
var sync = true;
var handler = function() {
u.toCallback(function() {
if (sync) throw new Error('Called synchronously');
}, done);
checkAsync: function(fn, done, error, handler) {
this.checkSyncAsync(fn, false, done, error, handler);
},

/**
* Runs a function and checks it calls back a handler synchronously or asynchronously.
* `fn` is called immediately, and passed a handler.
* Whether expect sync or async callback is defined by `expectSync` argument.
* If handler is called as expected, `done` callback is called without error.
* If handler is called not as expected, `done` callback is called with an error.
* If handler is not called, `done` callback is called with an error.
*
* If `handler` argument is provided to this function, it's executed as part of the handler.
*
* @param {Function} fn - Function to run.
* @param {boolean} expectSync - true if expect callback to be called sync, false if expect async
* @param {Function} done - Final callback to finish test
* @param {Function} error - Callback to call with errors
* @param {Function} [handler] - Handler function
* @returns {undefined}
*/
checkSyncAsync: function(fn, expectSync, done, error, handler) {
// Create handler
var sync = true,
called = false;

var handlerWrapped = function() {
called = true;
if (sync !== expectSync) error(new Error('Callback called ' + (expectSync ? 'asynchronously' : 'synchronously')));
if (handler) return handler.apply(this, arguments);
};

fn(handler);
// Run test function with handler
var p = fn(handlerWrapped);
sync = false;

// Check handler was called
done(p, null, function() {
if (!called) error(new Error('Callback not called'));
});
},

/**
Expand All @@ -61,18 +87,30 @@ module.exports = {
* If handler is bound, `done` callback is called with an error.
*
* @param {Function} fn - Function to run.
* @param {Function} done - Final callback to call with result
* @param {Function} done - Final callback to finish test
* @param {Function} error - Callback to call with errors
* @param {Function} [handler] - Handler function
* @returns {undefined}
*/
checkNotBound: function(fn, done) {
checkNotBound: function(fn, done, error, handler) {
var u = this;
var handler = function() {
u.toCallback(function() {
u.throwIfBound(handler);
}, done);

// Create handler
var called = false;

var handlerWrapped = function() {
called = true;
error(u.returnErrIfBound(handlerWrapped));
if (handler) return handler.apply(this, arguments);
};

fn(handler);
// Run test function with handler
var p = fn(handlerWrapped);

// Check handler was called
done(p, null, function() {
if (!called) error(new Error('Callback not called'));
});
},

/**
Expand All @@ -88,27 +126,32 @@ module.exports = {
* If any check fails, `done` callback is called with an error.
*
* @param {Function} fn - Function to run.
* @param {Function} done - Final callback to call with result
* @param {Object} context - CLS context should be bound to
* @param {Function} done - Final callback to finish test
* @param {Function} error - Callback to call with errors
* @returns {undefined}
*/
checkBound: function(fn, context, done) {
checkBound: function(fn, context, done, error) {
var u = this;
var err;
var handler = function() {
u.toCallback(function() {
// Throw if was not bound synchronously
if (err) throw err;

// Throw if not bound at time handler called
u.throwIfNotBound(handler, context);
}, done);
// Create handler
var called = false;

var handler = function() {
called = true;
error(u.returnErrIfNotBound(handler, context));
};

// Run function, passing handler
fn(handler);
// Run test function with handler
var p = fn(handler);

// Check if bound synchronously and set `err` to Error object if not
err = u.returnErrIfNotBound(handler, context);
// Check that bound synchronously
error(u.returnErrIfNotBound(handler, context));

// Check handler was called
done(p, null, function() {
if (!called) error(new Error('Callback not called'));
});
},

/**
Expand All @@ -118,19 +161,28 @@ module.exports = {
* `done` callback is called with/without error if is/isn't in right context.
*
* @param {Function} fn - Function to run.
* @param {Function} done - Final callback to call with result
* @param {Function} done - Final callback to finish test
* @param {Function} error - Callback to call with errors
* @returns {undefined}
*/
checkRunContext: function(fn, context, done) {
checkRunContext: function(fn, context, done, error) {
var u = this;
var handler = function() {
u.toCallback(function() {
if (u.ns.active !== context) throw new Error('Function run in wrong context (expected: ' + JSON.stringify(context) + ', got: ' + JSON.stringify(u.ns.active) + ')');
}, done);

// Create handler
var called = false;

var handler = function() {
called = true;
if (u.ns.active !== context) error(new Error('Function run in wrong context (expected: ' + JSON.stringify(context) + ', got: ' + JSON.stringify(u.ns.active) + ')'));
};

// Run function, passing handler
fn(handler);
// Run test function with handler
var p = fn(handler);

// Check handler was called
done(p, null, function() {
if (!called) error(new Error('Callback not called'));
});
},

/**
Expand Down
27 changes: 14 additions & 13 deletions test/support/testSets/binding.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
* Mixin to Utils prototype.
*/

/* global it */

// Exports

module.exports = {
Expand All @@ -21,7 +19,6 @@ module.exports = {
* @param {Function} fn - Test function
* @param {Object} [options] - Options object
* @param {boolean} [options.catches] - true if method catches rejected promises e.g. `promise.catch()`
* @param {string} [options.name] - Name of test ('binds callback' if not provided)
* @returns {undefined}
*/
testSetProtoCallbackBound: function(fn, options) {
Expand All @@ -30,12 +27,12 @@ module.exports = {

var makePromise = options.catches ? u.rejectSyncMethod() : u.resolveSyncMethod();

it(options.name || 'binds callback', function(done) {
u.it('binds callback', function(done, error) {
var p = makePromise();
u.runInContext(function(context) {
u.checkBound(function(handler) {
fn(p, handler);
}, context, done);
return fn(p, handler);
}, context, done, error);
});
});
},
Expand All @@ -46,14 +43,18 @@ module.exports = {
* e.g. `Promise.try(handler)`
*
* @param {Function} fn - Test function
* @param {Object} [options] - Options object
* @param {Function} [options.handler] - Optional handler function
* @returns {undefined}
*/
testSetCallbackNotBound: function(fn) {
testSetCallbackNotBound: function(fn, options) {
var u = this;
it('does not bind callback', function(done) {
options = options || {};

u.it('does not bind callback', function(done, error) {
u.checkNotBound(function(handler) {
fn(handler);
}, done);
return fn(handler);
}, done, error, options.handler);
});
},

Expand All @@ -77,12 +78,12 @@ module.exports = {

var makePromise = options.catches ? u.rejectSyncMethod() : u.resolveSyncMethod();

u.itMultiple(options.name || 'callback runs in context', function(done) {
u.itMultiple(options.name || 'callback runs in context', function(done, error) {
var p = makePromise();
u.runInContext(function(context) {
u.checkRunContext(function(handler) {
fn(p, handler);
}, context, done);
return fn(p, handler);
}, context, done, error);
});
});
}
Expand Down
Loading

0 comments on commit 4f8a44e

Please sign in to comment.