common.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 assert = require('assert');
  22. var noop = function() {};
  23. var mustCallChecks = [];
  24. function runCallChecks(exitCode) {
  25. if (exitCode !== 0) return;
  26. var failed = filter(mustCallChecks, function(context) {
  27. if ('minimum' in context) {
  28. context.messageSegment = 'at least ' + context.minimum;
  29. return context.actual < context.minimum;
  30. } else {
  31. context.messageSegment = 'exactly ' + context.exact;
  32. return context.actual !== context.exact;
  33. }
  34. });
  35. for (var i = 0; i < failed.length; i++) {
  36. var context = failed[i];
  37. console.log('Mismatched %s function calls. Expected %s, actual %d.',
  38. context.name,
  39. context.messageSegment,
  40. context.actual);
  41. // IE8 has no .stack
  42. if (context.stack) console.log(context.stack.split('\n').slice(2).join('\n'));
  43. }
  44. assert.strictEqual(failed.length, 0);
  45. }
  46. exports.mustCall = function(fn, exact) {
  47. return _mustCallInner(fn, exact, 'exact');
  48. };
  49. function _mustCallInner(fn, criteria, field) {
  50. if (typeof criteria == 'undefined') criteria = 1;
  51. if (typeof fn === 'number') {
  52. criteria = fn;
  53. fn = noop;
  54. } else if (fn === undefined) {
  55. fn = noop;
  56. }
  57. if (typeof criteria !== 'number')
  58. throw new TypeError('Invalid ' + field + ' value: ' + criteria);
  59. var context = {
  60. actual: 0,
  61. stack: (new Error()).stack,
  62. name: fn.name || '<anonymous>'
  63. };
  64. context[field] = criteria;
  65. // add the exit listener only once to avoid listener leak warnings
  66. if (mustCallChecks.length === 0) after(function() { runCallChecks(0); });
  67. mustCallChecks.push(context);
  68. return function() {
  69. context.actual++;
  70. return fn.apply(this, arguments);
  71. };
  72. }
  73. exports.mustNotCall = function(msg) {
  74. return function mustNotCall() {
  75. assert.fail(msg || 'function should not have been called');
  76. };
  77. };
  78. function filter(arr, fn) {
  79. if (arr.filter) return arr.filter(fn);
  80. var filtered = [];
  81. for (var i = 0; i < arr.length; i++) {
  82. if (fn(arr[i], i, arr)) filtered.push(arr[i]);
  83. }
  84. return filtered
  85. }