aboutsummaryrefslogtreecommitdiff
path: root/node_modules/events/tests/common.js
blob: 66f70a39076c9cb2227adb7b84caf8d45971d5e5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
var assert = require('assert');

var mustCallChecks = [];

function runCallChecks() {
  var failed_count = 0;
  for (var i=0 ; i< mustCallChecks.length; ++i) {
    var context = mustCallChecks[i];
    if (context.actual === context.expected) {
      continue;
    }

    failed_count++;
    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'));
  }

  assert(failed_count === 0);
}

after(runCallChecks);

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>'
  };

  mustCallChecks.push(context);

  return function() {
    context.actual++;
    return fn.apply(this, arguments);
  };
};