You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

index.js 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. 'use strict';
  2. /* eslint no-magic-numbers: 1 */
  3. var test = require('tape');
  4. var isCallable = require('../');
  5. var hasSymbols = typeof Symbol === 'function' && typeof Symbol('foo') === 'symbol';
  6. var genFn = require('make-generator-function');
  7. var arrowFn = require('make-arrow-function')();
  8. var weirdlyCommentedArrowFn;
  9. var asyncFn;
  10. var asyncArrowFn;
  11. try {
  12. /* eslint-disable no-new-func */
  13. weirdlyCommentedArrowFn = Function('return cl/*/**/=>/**/ass - 1;')();
  14. asyncFn = Function('return async function foo() {};')();
  15. asyncArrowFn = Function('return async () => {};')();
  16. /* eslint-enable no-new-func */
  17. } catch (e) { /**/ }
  18. var forEach = require('foreach');
  19. var noop = function () {};
  20. var classFake = function classFake() { }; // eslint-disable-line func-name-matching
  21. var returnClass = function () { return ' class '; };
  22. var return3 = function () { return 3; };
  23. /* for coverage */
  24. noop();
  25. classFake();
  26. returnClass();
  27. return3();
  28. /* end for coverage */
  29. var invokeFunction = function invokeFunctionString(str) {
  30. var result;
  31. try {
  32. /* eslint-disable no-new-func */
  33. var fn = Function(str);
  34. /* eslint-enable no-new-func */
  35. result = fn();
  36. } catch (e) {}
  37. return result;
  38. };
  39. var classConstructor = invokeFunction('"use strict"; return class Foo {}');
  40. var commentedClass = invokeFunction('"use strict"; return class/*kkk*/\n//blah\n Bar\n//blah\n {}');
  41. var commentedClassOneLine = invokeFunction('"use strict"; return class/**/A{}');
  42. var classAnonymous = invokeFunction('"use strict"; return class{}');
  43. var classAnonymousCommentedOneLine = invokeFunction('"use strict"; return class/*/*/{}');
  44. test('not callables', function (t) {
  45. t.test('non-number/string primitives', function (st) {
  46. st.notOk(isCallable(), 'undefined is not callable');
  47. st.notOk(isCallable(null), 'null is not callable');
  48. st.notOk(isCallable(false), 'false is not callable');
  49. st.notOk(isCallable(true), 'true is not callable');
  50. st.end();
  51. });
  52. t.notOk(isCallable([]), 'array is not callable');
  53. t.notOk(isCallable({}), 'object is not callable');
  54. t.notOk(isCallable(/a/g), 'regex literal is not callable');
  55. t.notOk(isCallable(new RegExp('a', 'g')), 'regex object is not callable');
  56. t.notOk(isCallable(new Date()), 'new Date() is not callable');
  57. t.test('numbers', function (st) {
  58. st.notOk(isCallable(42), 'number is not callable');
  59. st.notOk(isCallable(Object(42)), 'number object is not callable');
  60. st.notOk(isCallable(NaN), 'NaN is not callable');
  61. st.notOk(isCallable(Infinity), 'Infinity is not callable');
  62. st.end();
  63. });
  64. t.test('strings', function (st) {
  65. st.notOk(isCallable('foo'), 'string primitive is not callable');
  66. st.notOk(isCallable(Object('foo')), 'string object is not callable');
  67. st.end();
  68. });
  69. t.test('non-function with function in its [[Prototype]] chain', function (st) {
  70. var Foo = function Bar() {};
  71. Foo.prototype = noop;
  72. st.equal(true, isCallable(Foo), 'sanity check: Foo is callable');
  73. st.equal(false, isCallable(new Foo()), 'instance of Foo is not callable');
  74. st.end();
  75. });
  76. t.end();
  77. });
  78. test('@@toStringTag', { skip: !hasSymbols || !Symbol.toStringTag }, function (t) {
  79. var fakeFunction = {
  80. toString: function () { return String(return3); },
  81. valueOf: return3
  82. };
  83. fakeFunction[Symbol.toStringTag] = 'Function';
  84. t.equal(String(fakeFunction), String(return3));
  85. t.equal(Number(fakeFunction), return3());
  86. t.notOk(isCallable(fakeFunction), 'fake Function with @@toStringTag "Function" is not callable');
  87. t.end();
  88. });
  89. var typedArrayNames = [
  90. 'Int8Array',
  91. 'Uint8Array',
  92. 'Uint8ClampedArray',
  93. 'Int16Array',
  94. 'Uint16Array',
  95. 'Int32Array',
  96. 'Uint32Array',
  97. 'Float32Array',
  98. 'Float64Array'
  99. ];
  100. test('Functions', function (t) {
  101. t.ok(isCallable(noop), 'function is callable');
  102. t.ok(isCallable(classFake), 'function with name containing "class" is callable');
  103. t.ok(isCallable(returnClass), 'function with string " class " is callable');
  104. t.ok(isCallable(isCallable), 'isCallable is callable');
  105. t.end();
  106. });
  107. test('Typed Arrays', function (st) {
  108. forEach(typedArrayNames, function (typedArray) {
  109. /* istanbul ignore if : covered in node 0.6 */
  110. if (typeof global[typedArray] === 'undefined') {
  111. st.comment('# SKIP typed array "' + typedArray + '" not supported');
  112. } else {
  113. st.ok(isCallable(global[typedArray]), typedArray + ' is callable');
  114. }
  115. });
  116. st.end();
  117. });
  118. test('Generators', { skip: !genFn }, function (t) {
  119. t.ok(isCallable(genFn), 'generator function is callable');
  120. t.end();
  121. });
  122. test('Arrow functions', { skip: !arrowFn }, function (t) {
  123. t.ok(isCallable(arrowFn), 'arrow function is callable');
  124. t.ok(isCallable(weirdlyCommentedArrowFn), 'weirdly commented arrow functions are callable');
  125. t.end();
  126. });
  127. test('"Class" constructors', { skip: !classConstructor || !commentedClass || !commentedClassOneLine || !classAnonymous }, function (t) {
  128. t.notOk(isCallable(classConstructor), 'class constructors are not callable');
  129. t.notOk(isCallable(commentedClass), 'class constructors with comments in the signature are not callable');
  130. t.notOk(isCallable(commentedClassOneLine), 'one-line class constructors with comments in the signature are not callable');
  131. t.notOk(isCallable(classAnonymous), 'anonymous class constructors are not callable');
  132. t.notOk(isCallable(classAnonymousCommentedOneLine), 'anonymous one-line class constructors with comments in the signature are not callable');
  133. t.end();
  134. });
  135. test('`async function`s', { skip: !asyncFn }, function (t) {
  136. t.ok(isCallable(asyncFn), '`async function`s are callable');
  137. t.ok(isCallable(asyncArrowFn), '`async` arrow functions are callable');
  138. t.end();
  139. });