Browse Source

test: add common.mustCall function

Verifies that the callback gets invoked <n> times during the lifetime of the
test script.

This is a back-port of commit d0e6c3f from the master branch.
v0.8.16-release
Ben Noordhuis 13 years ago
parent
commit
65d824b488
  1. 43
      test/common.js

43
test/common.js

@ -139,8 +139,43 @@ process.on('exit', function() {
});
// This function allows one two run an HTTP test agaist both HTTPS and
// normal HTTP modules. This ensures they fit the same API.
exports.httpTest = function httpTest(cb) {
};
var mustCallChecks = [];
function runCallChecks() {
var failed = mustCallChecks.filter(function(context) {
return context.actual !== context.expected;
});
failed.forEach(function(context) {
console.log('Mismatched %s function calls. Expected %d, actual %d.',
context.name,
context.expected,
context.actual);
console.log(context.stack.split('\n').slice(2).join('\n'));
});
if (failed.length) process.exit(1);
}
exports.mustCall = function(fn, expected) {
if (typeof expected !== 'number') expected = 1;
var context = {
expected: expected,
actual: 0,
stack: (new Error).stack,
name: fn.name || '<anonymous>'
};
// add the exit listener only once to avoid listener leak warnings
if (mustCallChecks.length === 0) process.on('exit', runCallChecks);
mustCallChecks.push(context);
return function() {
context.actual++;
return fn.apply(this, arguments);
};
};

Loading…
Cancel
Save