common.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. // Copyright Joyent, Inc. and other Node contributors.
  2. //
  3. // Permission is hereby granted, free of charge, to any person obtaining a
  4. // copy of this software and associated documentation files (the
  5. // "Software"), to deal in the Software without restriction, including
  6. // without limitation the rights to use, copy, modify, merge, publish,
  7. // distribute, sublicense, and/or sell copies of the Software, and to permit
  8. // persons to whom the Software is furnished to do so, subject to the
  9. // following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included
  12. // in all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  15. // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  16. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
  17. // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  18. // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  19. // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  20. // USE OR OTHER DEALINGS IN THE SOFTWARE.
  21. var test = require('tape');
  22. var assert = require('assert');
  23. var noop = function() {};
  24. var mustCallChecks = [];
  25. function runCallChecks(exitCode) {
  26. if (exitCode !== 0) return;
  27. var failed = filter(mustCallChecks, function(context) {
  28. if ('minimum' in context) {
  29. context.messageSegment = 'at least ' + context.minimum;
  30. return context.actual < context.minimum;
  31. } else {
  32. context.messageSegment = 'exactly ' + context.exact;
  33. return context.actual !== context.exact;
  34. }
  35. });
  36. for (var i = 0; i < failed.length; i++) {
  37. var context = failed[i];
  38. console.log('Mismatched %s function calls. Expected %s, actual %d.',
  39. context.name,
  40. context.messageSegment,
  41. context.actual);
  42. // IE8 has no .stack
  43. if (context.stack) console.log(context.stack.split('\n').slice(2).join('\n'));
  44. }
  45. assert.strictEqual(failed.length, 0);
  46. }
  47. exports.mustCall = function(fn, exact) {
  48. return _mustCallInner(fn, exact, 'exact');
  49. };
  50. function _mustCallInner(fn, criteria, field) {
  51. if (typeof criteria == 'undefined') criteria = 1;
  52. if (typeof fn === 'number') {
  53. criteria = fn;
  54. fn = noop;
  55. } else if (fn === undefined) {
  56. fn = noop;
  57. }
  58. if (typeof criteria !== 'number')
  59. throw new TypeError('Invalid ' + field + ' value: ' + criteria);
  60. var context = {
  61. actual: 0,
  62. stack: (new Error()).stack,
  63. name: fn.name || '<anonymous>'
  64. };
  65. context[field] = criteria;
  66. // add the exit listener only once to avoid listener leak warnings
  67. if (mustCallChecks.length === 0) test.onFinish(function() { runCallChecks(0); });
  68. mustCallChecks.push(context);
  69. return function() {
  70. context.actual++;
  71. return fn.apply(this, arguments);
  72. };
  73. }
  74. exports.mustNotCall = function(msg) {
  75. return function mustNotCall() {
  76. assert.fail(msg || 'function should not have been called');
  77. };
  78. };
  79. function filter(arr, fn) {
  80. if (arr.filter) return arr.filter(fn);
  81. var filtered = [];
  82. for (var i = 0; i < arr.length; i++) {
  83. if (fn(arr[i], i, arr)) filtered.push(arr[i]);
  84. }
  85. return filtered
  86. }