| Leo Repp | 58b9f11 | 2021-11-22 11:57:47 +0100 | [diff] [blame^] | 1 | 'use strict'; |
| 2 | |
| 3 | /* globals Proxy */ |
| 4 | /* eslint no-magic-numbers: 1 */ |
| 5 | |
| 6 | var test = require('tape'); |
| 7 | var isCallable = require('../'); |
| 8 | var hasSymbols = typeof Symbol === 'function' && typeof Symbol('foo') === 'symbol'; |
| 9 | var generators = require('make-generator-function')(); |
| 10 | var arrows = require('make-arrow-function').list(); |
| 11 | var asyncs = require('make-async-function').list(); |
| 12 | var weirdlyCommentedArrowFn; |
| 13 | try { |
| 14 | /* eslint-disable no-new-func */ |
| 15 | weirdlyCommentedArrowFn = Function('return cl/*/**/=>/**/ass - 1;')(); |
| 16 | /* eslint-enable no-new-func */ |
| 17 | } catch (e) { /**/ } |
| 18 | var forEach = require('foreach'); |
| 19 | |
| 20 | var noop = function () {}; |
| 21 | var classFake = function classFake() { }; // eslint-disable-line func-name-matching |
| 22 | var returnClass = function () { return ' class '; }; |
| 23 | var return3 = function () { return 3; }; |
| 24 | /* for coverage */ |
| 25 | noop(); |
| 26 | classFake(); |
| 27 | returnClass(); |
| 28 | return3(); |
| 29 | /* end for coverage */ |
| 30 | |
| 31 | var proxy; |
| 32 | if (typeof Proxy === 'function') { |
| 33 | try { |
| 34 | proxy = new Proxy(function () {}, {}); |
| 35 | // for coverage |
| 36 | proxy(); |
| 37 | String(proxy); |
| 38 | } catch (_) { |
| 39 | // If `Reflect` is supported, then `Function.prototype.toString` isn't used for callability detection. |
| 40 | if (typeof Reflect !== 'object') { |
| 41 | // Older engines throw a `TypeError` when `Function.prototype.toString` is called on a Proxy object. |
| 42 | proxy = null; |
| 43 | } |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | var invokeFunction = function invokeFunctionString(str) { |
| 48 | var result; |
| 49 | try { |
| 50 | /* eslint-disable no-new-func */ |
| 51 | var fn = Function(str); |
| 52 | /* eslint-enable no-new-func */ |
| 53 | result = fn(); |
| 54 | } catch (e) {} |
| 55 | return result; |
| 56 | }; |
| 57 | |
| 58 | var classConstructor = invokeFunction('"use strict"; return class Foo {}'); |
| 59 | |
| 60 | var commentedClass = invokeFunction('"use strict"; return class/*kkk*/\n//blah\n Bar\n//blah\n {}'); |
| 61 | var commentedClassOneLine = invokeFunction('"use strict"; return class/**/A{}'); |
| 62 | var classAnonymous = invokeFunction('"use strict"; return class{}'); |
| 63 | var classAnonymousCommentedOneLine = invokeFunction('"use strict"; return class/*/*/{}'); |
| 64 | |
| 65 | test('not callables', function (t) { |
| 66 | t.test('non-number/string primitives', function (st) { |
| 67 | st.notOk(isCallable(), 'undefined is not callable'); |
| 68 | st.notOk(isCallable(null), 'null is not callable'); |
| 69 | st.notOk(isCallable(false), 'false is not callable'); |
| 70 | st.notOk(isCallable(true), 'true is not callable'); |
| 71 | st.end(); |
| 72 | }); |
| 73 | |
| 74 | t.notOk(isCallable([]), 'array is not callable'); |
| 75 | t.notOk(isCallable({}), 'object is not callable'); |
| 76 | t.notOk(isCallable(/a/g), 'regex literal is not callable'); |
| 77 | t.notOk(isCallable(new RegExp('a', 'g')), 'regex object is not callable'); |
| 78 | t.notOk(isCallable(new Date()), 'new Date() is not callable'); |
| 79 | |
| 80 | t.test('numbers', function (st) { |
| 81 | st.notOk(isCallable(42), 'number is not callable'); |
| 82 | st.notOk(isCallable(Object(42)), 'number object is not callable'); |
| 83 | st.notOk(isCallable(NaN), 'NaN is not callable'); |
| 84 | st.notOk(isCallable(Infinity), 'Infinity is not callable'); |
| 85 | st.end(); |
| 86 | }); |
| 87 | |
| 88 | t.test('strings', function (st) { |
| 89 | st.notOk(isCallable('foo'), 'string primitive is not callable'); |
| 90 | st.notOk(isCallable(Object('foo')), 'string object is not callable'); |
| 91 | st.end(); |
| 92 | }); |
| 93 | |
| 94 | t.test('non-function with function in its [[Prototype]] chain', function (st) { |
| 95 | var Foo = function Bar() {}; |
| 96 | Foo.prototype = noop; |
| 97 | st.equal(true, isCallable(Foo), 'sanity check: Foo is callable'); |
| 98 | st.equal(false, isCallable(new Foo()), 'instance of Foo is not callable'); |
| 99 | st.end(); |
| 100 | }); |
| 101 | |
| 102 | t.end(); |
| 103 | }); |
| 104 | |
| 105 | test('@@toStringTag', { skip: !hasSymbols || !Symbol.toStringTag }, function (t) { |
| 106 | var fakeFunction = { |
| 107 | toString: function () { return String(return3); }, |
| 108 | valueOf: return3 |
| 109 | }; |
| 110 | fakeFunction[Symbol.toStringTag] = 'Function'; |
| 111 | t.equal(String(fakeFunction), String(return3)); |
| 112 | t.equal(Number(fakeFunction), return3()); |
| 113 | t.notOk(isCallable(fakeFunction), 'fake Function with @@toStringTag "Function" is not callable'); |
| 114 | t.end(); |
| 115 | }); |
| 116 | |
| 117 | var typedArrayNames = [ |
| 118 | 'Int8Array', |
| 119 | 'Uint8Array', |
| 120 | 'Uint8ClampedArray', |
| 121 | 'Int16Array', |
| 122 | 'Uint16Array', |
| 123 | 'Int32Array', |
| 124 | 'Uint32Array', |
| 125 | 'Float32Array', |
| 126 | 'Float64Array' |
| 127 | ]; |
| 128 | |
| 129 | test('Functions', function (t) { |
| 130 | t.ok(isCallable(noop), 'function is callable'); |
| 131 | t.ok(isCallable(classFake), 'function with name containing "class" is callable'); |
| 132 | t.ok(isCallable(returnClass), 'function with string " class " is callable'); |
| 133 | t.ok(isCallable(isCallable), 'isCallable is callable'); |
| 134 | t.end(); |
| 135 | }); |
| 136 | |
| 137 | test('Typed Arrays', function (st) { |
| 138 | forEach(typedArrayNames, function (typedArray) { |
| 139 | /* istanbul ignore if : covered in node 0.6 */ |
| 140 | if (typeof global[typedArray] === 'undefined') { |
| 141 | st.comment('# SKIP typed array "' + typedArray + '" not supported'); |
| 142 | } else { |
| 143 | st.ok(isCallable(global[typedArray]), typedArray + ' is callable'); |
| 144 | } |
| 145 | }); |
| 146 | st.end(); |
| 147 | }); |
| 148 | |
| 149 | test('Generators', { skip: generators.length === 0 }, function (t) { |
| 150 | forEach(generators, function (genFn) { |
| 151 | t.ok(isCallable(genFn), 'generator function ' + genFn + ' is callable'); |
| 152 | }); |
| 153 | t.end(); |
| 154 | }); |
| 155 | |
| 156 | test('Arrow functions', { skip: arrows.length === 0 }, function (t) { |
| 157 | forEach(arrows, function (arrowFn) { |
| 158 | t.ok(isCallable(arrowFn), 'arrow function ' + arrowFn + ' is callable'); |
| 159 | }); |
| 160 | t.ok(isCallable(weirdlyCommentedArrowFn), 'weirdly commented arrow functions are callable'); |
| 161 | t.end(); |
| 162 | }); |
| 163 | |
| 164 | test('"Class" constructors', { skip: !classConstructor || !commentedClass || !commentedClassOneLine || !classAnonymous }, function (t) { |
| 165 | t.notOk(isCallable(classConstructor), 'class constructors are not callable'); |
| 166 | t.notOk(isCallable(commentedClass), 'class constructors with comments in the signature are not callable'); |
| 167 | t.notOk(isCallable(commentedClassOneLine), 'one-line class constructors with comments in the signature are not callable'); |
| 168 | t.notOk(isCallable(classAnonymous), 'anonymous class constructors are not callable'); |
| 169 | t.notOk(isCallable(classAnonymousCommentedOneLine), 'anonymous one-line class constructors with comments in the signature are not callable'); |
| 170 | t.end(); |
| 171 | }); |
| 172 | |
| 173 | test('`async function`s', { skip: asyncs.length === 0 }, function (t) { |
| 174 | forEach(asyncs, function (asyncFn) { |
| 175 | t.ok(isCallable(asyncFn), '`async function` ' + asyncFn + ' is callable'); |
| 176 | }); |
| 177 | t.end(); |
| 178 | }); |
| 179 | |
| 180 | test('proxies of functions', { skip: !proxy }, function (t) { |
| 181 | t.ok(isCallable(proxy), 'proxies of functions are callable'); |
| 182 | t.end(); |
| 183 | }); |
| 184 | |
| 185 | test('throwing functions', function (t) { |
| 186 | t.plan(1); |
| 187 | |
| 188 | var thrower = function (a) { return a.b; }; |
| 189 | t.ok(isCallable(thrower), 'a function that throws is callable'); |
| 190 | }); |
| 191 | |
| 192 | /* globals document: false */ |
| 193 | test('document.all', { skip: typeof document !== 'object' }, function (t) { |
| 194 | t.notOk(isCallable(document), 'document is not callable'); |
| 195 | t.ok(isCallable(document.all), 'document.all is callable'); |
| 196 | |
| 197 | t.end(); |
| 198 | }); |