diff options
author | Ben Noordhuis <info@bnoordhuis.nl> | 2012-07-31 17:47:53 +0200 |
---|---|---|
committer | Ben Noordhuis <info@bnoordhuis.nl> | 2012-09-22 03:48:58 +0200 |
commit | d0e6c3f5a644e4f84e80e9081d28570bf7477391 (patch) | |
tree | 64c1264bc9e415b1dba051cbe08ef6fe06e54204 /test/common.js | |
parent | 7bd84de5ed7c5e8c072bcc02fb50037b8f3aac3f (diff) | |
download | android-node-v8-d0e6c3f5a644e4f84e80e9081d28570bf7477391.tar.gz android-node-v8-d0e6c3f5a644e4f84e80e9081d28570bf7477391.tar.bz2 android-node-v8-d0e6c3f5a644e4f84e80e9081d28570bf7477391.zip |
test: add common.mustCall function
Verifies that the callback gets invoked <n> times during the lifetime of the
test script.
Diffstat (limited to 'test/common.js')
-rw-r--r-- | test/common.js | 43 |
1 files changed, 39 insertions, 4 deletions
diff --git a/test/common.js b/test/common.js index 62dd0f4a80..783466a8f6 100644 --- a/test/common.js +++ b/test/common.js @@ -141,8 +141,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); + }; +}; |