test.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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() === 'symbol';
  6. var genFn = require('make-generator-function');
  7. var arrowFn = require('make-arrow-function')();
  8. var forEach = require('foreach');
  9. var invokeFunction = function invokeFunction(str) {
  10. var result;
  11. try {
  12. /* eslint-disable no-new-func */
  13. var fn = Function(str);
  14. /* eslint-enable no-new-func */
  15. result = fn();
  16. } catch (e) { /**/ }
  17. return result;
  18. };
  19. var classConstructor = invokeFunction('"use strict"; return class Foo {}');
  20. var commentedClass = invokeFunction('"use strict"; return class/*kkk*/\n\/\/blah\n Bar\n\/\/blah\n {}');
  21. test('not callables', function (t) {
  22. t.test('non-number/string primitives', function (st) {
  23. st.notOk(isCallable(), 'undefined is not callable');
  24. st.notOk(isCallable(null), 'null is not callable');
  25. st.notOk(isCallable(false), 'false is not callable');
  26. st.notOk(isCallable(true), 'true is not callable');
  27. st.end();
  28. });
  29. t.notOk(isCallable([]), 'array is not callable');
  30. t.notOk(isCallable({}), 'object is not callable');
  31. t.notOk(isCallable(/a/g), 'regex literal is not callable');
  32. t.notOk(isCallable(new RegExp('a', 'g')), 'regex object is not callable');
  33. t.notOk(isCallable(new Date()), 'new Date() is not callable');
  34. t.test('numbers', function (st) {
  35. st.notOk(isCallable(42), 'number is not callable');
  36. st.notOk(isCallable(Object(42)), 'number object is not callable');
  37. st.notOk(isCallable(NaN), 'NaN is not callable');
  38. st.notOk(isCallable(Infinity), 'Infinity is not callable');
  39. st.end();
  40. });
  41. t.test('strings', function (st) {
  42. st.notOk(isCallable('foo'), 'string primitive is not callable');
  43. st.notOk(isCallable(Object('foo')), 'string object is not callable');
  44. st.end();
  45. });
  46. t.test('non-function with function in its [[Prototype]] chain', function (st) {
  47. var Foo = function Bar() {};
  48. Foo.prototype = function () {};
  49. st.equal(true, isCallable(Foo), 'sanity check: Foo is callable');
  50. st.equal(false, isCallable(new Foo()), 'instance of Foo is not callable');
  51. st.end();
  52. });
  53. t.end();
  54. });
  55. test('@@toStringTag', { skip: !hasSymbols || !Symbol.toStringTag }, function (t) {
  56. var fn = function () { return 3; };
  57. var fakeFunction = {
  58. toString: function () { return String(fn); },
  59. valueOf: function () { return fn; }
  60. };
  61. fakeFunction[Symbol.toStringTag] = 'Function';
  62. t.notOk(isCallable(fakeFunction), 'fake Function with @@toStringTag "Function" is not callable');
  63. t.end();
  64. });
  65. var typedArrayNames = [
  66. 'Int8Array',
  67. 'Uint8Array',
  68. 'Uint8ClampedArray',
  69. 'Int16Array',
  70. 'Uint16Array',
  71. 'Int32Array',
  72. 'Uint32Array',
  73. 'Float32Array',
  74. 'Float64Array'
  75. ];
  76. test('Functions', function (t) {
  77. t.ok(isCallable(function () {}), 'function is callable');
  78. t.ok(isCallable(function classFake() { }), 'function with name containing "class" is callable');
  79. t.ok(isCallable(function () { return ' class '; }), 'function with string " class " is callable');
  80. t.ok(isCallable(isCallable), 'isCallable is callable');
  81. t.end();
  82. });
  83. test('Typed Arrays', function (st) {
  84. forEach(typedArrayNames, function (typedArray) {
  85. if (typeof global[typedArray] !== 'undefined') {
  86. st.ok(isCallable(global[typedArray]), typedArray + ' is callable');
  87. }
  88. });
  89. st.end();
  90. });
  91. test('Generators', { skip: !genFn }, function (t) {
  92. t.ok(isCallable(genFn), 'generator function is callable');
  93. t.end();
  94. });
  95. test('Arrow functions', { skip: !arrowFn }, function (t) {
  96. t.ok(isCallable(arrowFn), 'arrow function is callable');
  97. t.end();
  98. });
  99. test('"Class" constructors', { skip: !classConstructor || !commentedClass }, function (t) {
  100. t.notOk(isCallable(classConstructor), 'class constructors are not callable');
  101. t.notOk(isCallable(commentedClass), 'class constructors with comments in the signature are not callable');
  102. t.end();
  103. });