1 | 'use strict';
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | var test = require('tape');
|
7 | var isCallable = require('../');
|
8 | var hasToStringTag = require('has-tostringtag/shams')();
|
9 | var v = require('es-value-fixtures');
|
10 | var forEach = require('for-each');
|
11 | var inspect = require('object-inspect');
|
12 | var typedArrayNames = require('available-typed-arrays')();
|
13 | var generators = require('make-generator-function')();
|
14 | var arrows = require('make-arrow-function').list();
|
15 | var asyncs = require('make-async-function').list();
|
16 | var weirdlyCommentedArrowFn;
|
17 | try {
|
18 |
|
19 | weirdlyCommentedArrowFn = Function('return cl/*/**/=>/**/ass - 1;')();
|
20 |
|
21 | } catch (e) { }
|
22 |
|
23 | var noop = function () {};
|
24 | var classFake = function classFake() { };
|
25 | var returnClass = function () { return ' class '; };
|
26 | var return3 = function () { return 3; };
|
27 |
|
28 | noop();
|
29 | classFake();
|
30 | returnClass();
|
31 | return3();
|
32 |
|
33 |
|
34 | var proxy;
|
35 | if (typeof Proxy === 'function') {
|
36 | try {
|
37 | proxy = new Proxy(function () {}, {});
|
38 |
|
39 | proxy();
|
40 | String(proxy);
|
41 | } catch (_) {
|
42 |
|
43 | if (typeof Reflect !== 'object') {
|
44 |
|
45 | proxy = null;
|
46 | }
|
47 | }
|
48 | }
|
49 |
|
50 | var invokeFunction = function invokeFunctionString(str) {
|
51 | var result;
|
52 | try {
|
53 |
|
54 | var fn = Function(str);
|
55 |
|
56 | result = fn();
|
57 | } catch (e) {}
|
58 | return result;
|
59 | };
|
60 |
|
61 | var classConstructor = invokeFunction('"use strict"; return class Foo {}');
|
62 |
|
63 | var commentedClass = invokeFunction('"use strict"; return class/*kkk*/\n//blah\n Bar\n//blah\n {}');
|
64 | var commentedClassOneLine = invokeFunction('"use strict"; return class/**/A{}');
|
65 | var classAnonymous = invokeFunction('"use strict"; return class{}');
|
66 | var classAnonymousCommentedOneLine = invokeFunction('"use strict"; return class/*/*/{}');
|
67 |
|
68 | test('not callables', function (t) {
|
69 | t.notOk(isCallable(), 'implicit undefined is not callable');
|
70 |
|
71 | forEach(v.nonFunctions.concat([
|
72 | Object(42),
|
73 | Object('foo'),
|
74 | NaN,
|
75 | [],
|
76 | /a/g,
|
77 | new RegExp('a', 'g'),
|
78 | new Date()
|
79 | ]), function (nonFunction) {
|
80 | t.equal(isCallable(nonFunction), false, inspect(nonFunction) + ' is not callable');
|
81 | });
|
82 |
|
83 | t.test('non-function with function in its [[Prototype]] chain', function (st) {
|
84 | var Foo = function Bar() {};
|
85 | Foo.prototype = noop;
|
86 | st.equal(isCallable(Foo), true, 'sanity check: Foo is callable');
|
87 | st.equal(isCallable(new Foo()), false, 'instance of Foo is not callable');
|
88 | st.end();
|
89 | });
|
90 |
|
91 | t.end();
|
92 | });
|
93 |
|
94 | test('@@toStringTag', { skip: !hasToStringTag }, function (t) {
|
95 | var fakeFunction = {
|
96 | toString: function () { return String(return3); },
|
97 | valueOf: return3
|
98 | };
|
99 | fakeFunction[Symbol.toStringTag] = 'Function';
|
100 | t.equal(String(fakeFunction), String(return3));
|
101 | t.equal(Number(fakeFunction), return3());
|
102 | t.notOk(isCallable(fakeFunction), 'fake Function with @@toStringTag "Function" is not callable');
|
103 | t.end();
|
104 | });
|
105 |
|
106 | test('Functions', function (t) {
|
107 | t.ok(isCallable(noop), 'function is callable');
|
108 | t.ok(isCallable(classFake), 'function with name containing "class" is callable');
|
109 | t.ok(isCallable(returnClass), 'function with string " class " is callable');
|
110 | t.ok(isCallable(isCallable), 'isCallable is callable');
|
111 | t.end();
|
112 | });
|
113 |
|
114 | test('Typed Arrays', { skip: typedArrayNames.length === 0 }, function (st) {
|
115 | forEach(typedArrayNames, function (typedArray) {
|
116 | st.ok(isCallable(global[typedArray]), typedArray + ' is callable');
|
117 | });
|
118 | st.end();
|
119 | });
|
120 |
|
121 | test('Generators', { skip: generators.length === 0 }, function (t) {
|
122 | forEach(generators, function (genFn) {
|
123 | t.ok(isCallable(genFn), 'generator function ' + genFn + ' is callable');
|
124 | });
|
125 | t.end();
|
126 | });
|
127 |
|
128 | test('Arrow functions', { skip: arrows.length === 0 }, function (t) {
|
129 | forEach(arrows, function (arrowFn) {
|
130 | t.ok(isCallable(arrowFn), 'arrow function ' + arrowFn + ' is callable');
|
131 | });
|
132 | t.ok(isCallable(weirdlyCommentedArrowFn), 'weirdly commented arrow functions are callable');
|
133 | t.end();
|
134 | });
|
135 |
|
136 | test('"Class" constructors', { skip: !classConstructor || !commentedClass || !commentedClassOneLine || !classAnonymous }, function (t) {
|
137 | t.notOk(isCallable(classConstructor), 'class constructors are not callable');
|
138 | t.notOk(isCallable(commentedClass), 'class constructors with comments in the signature are not callable');
|
139 | t.notOk(isCallable(commentedClassOneLine), 'one-line class constructors with comments in the signature are not callable');
|
140 | t.notOk(isCallable(classAnonymous), 'anonymous class constructors are not callable');
|
141 | t.notOk(isCallable(classAnonymousCommentedOneLine), 'anonymous one-line class constructors with comments in the signature are not callable');
|
142 | t.end();
|
143 | });
|
144 |
|
145 | test('`async function`s', { skip: asyncs.length === 0 }, function (t) {
|
146 | forEach(asyncs, function (asyncFn) {
|
147 | t.ok(isCallable(asyncFn), '`async function` ' + asyncFn + ' is callable');
|
148 | });
|
149 | t.end();
|
150 | });
|
151 |
|
152 | test('proxies of functions', { skip: !proxy }, function (t) {
|
153 | t.ok(isCallable(proxy), 'proxies of functions are callable');
|
154 | t.end();
|
155 | });
|
156 |
|
157 | test('throwing functions', function (t) {
|
158 | t.plan(1);
|
159 |
|
160 | var thrower = function (a) { return a.b; };
|
161 | t.ok(isCallable(thrower), 'a function that throws is callable');
|
162 | });
|
163 |
|
164 |
|
165 | test('document.all', { skip: typeof document !== 'object' }, function (t) {
|
166 | t.notOk(isCallable(document), 'document is not callable');
|
167 | t.ok(isCallable(document.all), 'document.all is callable');
|
168 |
|
169 | t.end();
|
170 | });
|