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.

158 lines
5.4 KiB

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