Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

index.js 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. 'use strict';
  2. /* eslint no-magic-numbers: 1 */
  3. var test = require('tape');
  4. var isCallable = require('../');
  5. var hasToStringTag = require('has-tostringtag/shams')();
  6. var v = require('es-value-fixtures');
  7. var forEach = require('for-each');
  8. var inspect = require('object-inspect');
  9. var typedArrayNames = require('available-typed-arrays')();
  10. var generators = require('make-generator-function')();
  11. var arrows = require('make-arrow-function').list();
  12. var asyncs = require('make-async-function').list();
  13. var weirdlyCommentedArrowFn;
  14. try {
  15. /* eslint-disable no-new-func */
  16. weirdlyCommentedArrowFn = Function('return cl/*/**/=>/**/ass - 1;')();
  17. /* eslint-enable no-new-func */
  18. } catch (e) { /**/ }
  19. var isIE68 = !(0 in [undefined]);
  20. var isFirefox = typeof window !== 'undefined' && ('netscape' in window) && (/ rv:/).test(navigator.userAgent);
  21. var fnToStringCoerces;
  22. try {
  23. Function.prototype.toString.call(v.uncoercibleFnObject);
  24. fnToStringCoerces = true;
  25. } catch (e) {
  26. fnToStringCoerces = false;
  27. }
  28. var noop = function () {};
  29. var classFake = function classFake() { }; // eslint-disable-line func-name-matching
  30. var returnClass = function () { return ' class '; };
  31. var return3 = function () { return 3; };
  32. /* for coverage */
  33. noop();
  34. classFake();
  35. returnClass();
  36. return3();
  37. /* end for coverage */
  38. var proxy;
  39. if (typeof Proxy === 'function') {
  40. try {
  41. proxy = new Proxy(function () {}, {});
  42. // for coverage
  43. proxy();
  44. String(proxy);
  45. } catch (_) {
  46. // Older engines throw a `TypeError` when `Function.prototype.toString` is called on a Proxy object.
  47. proxy = null;
  48. }
  49. }
  50. var invokeFunction = function invokeFunctionString(str) {
  51. var result;
  52. try {
  53. /* eslint-disable no-new-func */
  54. var fn = Function(str);
  55. /* eslint-enable no-new-func */
  56. result = fn();
  57. } catch (e) {}
  58. return result;
  59. };
  60. var classConstructor = invokeFunction('"use strict"; return class Foo {}');
  61. var hasDetectableClasses = classConstructor && Function.prototype.toString.call(classConstructor) === 'class Foo {}';
  62. var commentedClass = invokeFunction('"use strict"; return class/*kkk*/\n//blah\n Bar\n//blah\n {}');
  63. var commentedClassOneLine = invokeFunction('"use strict"; return class/**/A{}');
  64. var classAnonymous = invokeFunction('"use strict"; return class{}');
  65. var classAnonymousCommentedOneLine = invokeFunction('"use strict"; return class/*/*/{}');
  66. test('not callables', function (t) {
  67. t.notOk(isCallable(), 'implicit undefined is not callable');
  68. forEach(v.nonFunctions.concat([
  69. Object(42),
  70. Object('foo'),
  71. NaN,
  72. [],
  73. /a/g,
  74. new RegExp('a', 'g'),
  75. new Date()
  76. ]), function (nonFunction) {
  77. if (fnToStringCoerces && nonFunction === v.coercibleFnObject) {
  78. t.comment('FF 3.6 has a Function toString that coerces its receiver, so this test is skipped');
  79. return;
  80. }
  81. if (nonFunction != null) { // eslint-disable-line eqeqeq
  82. if (isFirefox) {
  83. // Firefox 3 throws some kind of *object* here instead of a proper error
  84. t['throws'](
  85. function () { Function.prototype.toString.call(nonFunction); },
  86. inspect(nonFunction) + ' can not be used with Function toString'
  87. );
  88. } else {
  89. t['throws'](
  90. function () { Function.prototype.toString.call(nonFunction); },
  91. TypeError,
  92. inspect(nonFunction) + ' can not be used with Function toString'
  93. );
  94. }
  95. }
  96. t.equal(isCallable(nonFunction), false, inspect(nonFunction) + ' is not callable');
  97. });
  98. t.test('non-function with function in its [[Prototype]] chain', function (st) {
  99. var Foo = function Bar() {};
  100. Foo.prototype = noop;
  101. st.equal(isCallable(Foo), true, 'sanity check: Foo is callable');
  102. st.equal(isCallable(new Foo()), false, 'instance of Foo is not callable');
  103. st.end();
  104. });
  105. t.end();
  106. });
  107. test('@@toStringTag', { skip: !hasToStringTag }, function (t) {
  108. var fakeFunction = {
  109. toString: function () { return String(return3); },
  110. valueOf: return3
  111. };
  112. fakeFunction[Symbol.toStringTag] = 'Function';
  113. t.equal(String(fakeFunction), String(return3));
  114. t.equal(Number(fakeFunction), return3());
  115. t.notOk(isCallable(fakeFunction), 'fake Function with @@toStringTag "Function" is not callable');
  116. t.end();
  117. });
  118. test('Functions', function (t) {
  119. t.ok(isCallable(noop), 'function is callable');
  120. t.ok(isCallable(classFake), 'function with name containing "class" is callable');
  121. t.ok(isCallable(returnClass), 'function with string " class " is callable');
  122. t.ok(isCallable(isCallable), 'isCallable is callable');
  123. t.end();
  124. });
  125. test('Typed Arrays', { skip: typedArrayNames.length === 0 }, function (st) {
  126. forEach(typedArrayNames, function (typedArray) {
  127. st.ok(isCallable(global[typedArray]), typedArray + ' is callable');
  128. });
  129. st.end();
  130. });
  131. test('Generators', { skip: generators.length === 0 }, function (t) {
  132. forEach(generators, function (genFn) {
  133. t.ok(isCallable(genFn), 'generator function ' + genFn + ' is callable');
  134. });
  135. t.end();
  136. });
  137. test('Arrow functions', { skip: arrows.length === 0 }, function (t) {
  138. forEach(arrows, function (arrowFn) {
  139. t.ok(isCallable(arrowFn), 'arrow function ' + arrowFn + ' is callable');
  140. });
  141. t.ok(isCallable(weirdlyCommentedArrowFn), 'weirdly commented arrow functions are callable');
  142. t.end();
  143. });
  144. test('"Class" constructors', {
  145. skip: !classConstructor || !commentedClass || !commentedClassOneLine || !classAnonymous, todo: !hasDetectableClasses
  146. }, function (t) {
  147. if (!hasDetectableClasses) {
  148. t.comment('WARNING: This engine does not support detectable classes');
  149. }
  150. t.notOk(isCallable(classConstructor), 'class constructors are not callable');
  151. t.notOk(isCallable(commentedClass), 'class constructors with comments in the signature are not callable');
  152. t.notOk(isCallable(commentedClassOneLine), 'one-line class constructors with comments in the signature are not callable');
  153. t.notOk(isCallable(classAnonymous), 'anonymous class constructors are not callable');
  154. t.notOk(isCallable(classAnonymousCommentedOneLine), 'anonymous one-line class constructors with comments in the signature are not callable');
  155. t.end();
  156. });
  157. test('`async function`s', { skip: asyncs.length === 0 }, function (t) {
  158. forEach(asyncs, function (asyncFn) {
  159. t.ok(isCallable(asyncFn), '`async function` ' + asyncFn + ' is callable');
  160. });
  161. t.end();
  162. });
  163. test('proxies of functions', { skip: !proxy }, function (t) {
  164. t.equal(isCallable(proxy), true, 'proxies of functions are callable');
  165. t.end();
  166. });
  167. test('throwing functions', function (t) {
  168. t.plan(1);
  169. var thrower = function (a) { return a.b; };
  170. t.ok(isCallable(thrower), 'a function that throws is callable');
  171. });
  172. test('DOM', function (t) {
  173. /* eslint-env browser */
  174. t.test('document.all', { skip: typeof document !== 'object' }, function (st) {
  175. st.notOk(isCallable(document), 'document is not callable');
  176. var all = document.all;
  177. var isFF3 = !isIE68 && Object.prototype.toString(all) === Object.prototype.toString.call(document.all); // this test is true in IE 6-8 also
  178. var expected = false;
  179. if (!isFF3) {
  180. try {
  181. expected = document.all('') == null; // eslint-disable-line eqeqeq
  182. } catch (e) { /**/ }
  183. }
  184. st.equal(isCallable(document.all), expected, 'document.all is ' + (isFF3 ? 'not ' : '') + 'callable');
  185. st.end();
  186. });
  187. forEach([
  188. 'HTMLElement',
  189. 'HTMLAnchorElement'
  190. ], function (name) {
  191. var constructor = global[name];
  192. t.test(name, { skip: !constructor }, function (st) {
  193. st.match(typeof constructor, /^(?:function|object)$/, name + ' is a function or object');
  194. var callable = isCallable(constructor);
  195. st.equal(typeof callable, 'boolean');
  196. if (callable) {
  197. st.doesNotThrow(
  198. function () { Function.prototype.toString.call(constructor); },
  199. 'anything this library claims is callable should be accepted by Function toString'
  200. );
  201. } else {
  202. st['throws'](
  203. function () { Function.prototype.toString.call(constructor); },
  204. TypeError,
  205. 'anything this library claims is not callable should not be accepted by Function toString'
  206. );
  207. }
  208. st.end();
  209. });
  210. });
  211. t.end();
  212. });