update
This commit is contained in:
+36
-49
@@ -5,7 +5,11 @@
|
||||
|
||||
var test = require('tape');
|
||||
var isCallable = require('../');
|
||||
var hasSymbols = typeof Symbol === 'function' && typeof Symbol('foo') === 'symbol';
|
||||
var hasToStringTag = require('has-tostringtag/shams')();
|
||||
var v = require('es-value-fixtures');
|
||||
var forEach = require('for-each');
|
||||
var inspect = require('object-inspect');
|
||||
var typedArrayNames = require('available-typed-arrays')();
|
||||
var generators = require('make-generator-function')();
|
||||
var arrows = require('make-arrow-function').list();
|
||||
var asyncs = require('make-async-function').list();
|
||||
@@ -15,7 +19,6 @@ try {
|
||||
weirdlyCommentedArrowFn = Function('return cl/*/**/=>/**/ass - 1;')();
|
||||
/* eslint-enable no-new-func */
|
||||
} catch (e) { /**/ }
|
||||
var forEach = require('foreach');
|
||||
|
||||
var noop = function () {};
|
||||
var classFake = function classFake() { }; // eslint-disable-line func-name-matching
|
||||
@@ -63,46 +66,32 @@ var classAnonymous = invokeFunction('"use strict"; return class{}');
|
||||
var classAnonymousCommentedOneLine = invokeFunction('"use strict"; return class/*/*/{}');
|
||||
|
||||
test('not callables', function (t) {
|
||||
t.test('non-number/string primitives', function (st) {
|
||||
st.notOk(isCallable(), 'undefined is not callable');
|
||||
st.notOk(isCallable(null), 'null is not callable');
|
||||
st.notOk(isCallable(false), 'false is not callable');
|
||||
st.notOk(isCallable(true), 'true is not callable');
|
||||
st.end();
|
||||
});
|
||||
t.notOk(isCallable(), 'implicit undefined is not callable');
|
||||
|
||||
t.notOk(isCallable([]), 'array is not callable');
|
||||
t.notOk(isCallable({}), 'object is not callable');
|
||||
t.notOk(isCallable(/a/g), 'regex literal is not callable');
|
||||
t.notOk(isCallable(new RegExp('a', 'g')), 'regex object is not callable');
|
||||
t.notOk(isCallable(new Date()), 'new Date() is not callable');
|
||||
|
||||
t.test('numbers', function (st) {
|
||||
st.notOk(isCallable(42), 'number is not callable');
|
||||
st.notOk(isCallable(Object(42)), 'number object is not callable');
|
||||
st.notOk(isCallable(NaN), 'NaN is not callable');
|
||||
st.notOk(isCallable(Infinity), 'Infinity is not callable');
|
||||
st.end();
|
||||
});
|
||||
|
||||
t.test('strings', function (st) {
|
||||
st.notOk(isCallable('foo'), 'string primitive is not callable');
|
||||
st.notOk(isCallable(Object('foo')), 'string object is not callable');
|
||||
st.end();
|
||||
forEach(v.nonFunctions.concat([
|
||||
Object(42),
|
||||
Object('foo'),
|
||||
NaN,
|
||||
[],
|
||||
/a/g,
|
||||
new RegExp('a', 'g'),
|
||||
new Date()
|
||||
]), function (nonFunction) {
|
||||
t.equal(isCallable(nonFunction), false, inspect(nonFunction) + ' is not callable');
|
||||
});
|
||||
|
||||
t.test('non-function with function in its [[Prototype]] chain', function (st) {
|
||||
var Foo = function Bar() {};
|
||||
Foo.prototype = noop;
|
||||
st.equal(true, isCallable(Foo), 'sanity check: Foo is callable');
|
||||
st.equal(false, isCallable(new Foo()), 'instance of Foo is not callable');
|
||||
st.equal(isCallable(Foo), true, 'sanity check: Foo is callable');
|
||||
st.equal(isCallable(new Foo()), false, 'instance of Foo is not callable');
|
||||
st.end();
|
||||
});
|
||||
|
||||
t.end();
|
||||
});
|
||||
|
||||
test('@@toStringTag', { skip: !hasSymbols || !Symbol.toStringTag }, function (t) {
|
||||
test('@@toStringTag', { skip: !hasToStringTag }, function (t) {
|
||||
var fakeFunction = {
|
||||
toString: function () { return String(return3); },
|
||||
valueOf: return3
|
||||
@@ -114,18 +103,6 @@ test('@@toStringTag', { skip: !hasSymbols || !Symbol.toStringTag }, function (t)
|
||||
t.end();
|
||||
});
|
||||
|
||||
var typedArrayNames = [
|
||||
'Int8Array',
|
||||
'Uint8Array',
|
||||
'Uint8ClampedArray',
|
||||
'Int16Array',
|
||||
'Uint16Array',
|
||||
'Int32Array',
|
||||
'Uint32Array',
|
||||
'Float32Array',
|
||||
'Float64Array'
|
||||
];
|
||||
|
||||
test('Functions', function (t) {
|
||||
t.ok(isCallable(noop), 'function is callable');
|
||||
t.ok(isCallable(classFake), 'function with name containing "class" is callable');
|
||||
@@ -134,14 +111,9 @@ test('Functions', function (t) {
|
||||
t.end();
|
||||
});
|
||||
|
||||
test('Typed Arrays', function (st) {
|
||||
test('Typed Arrays', { skip: typedArrayNames.length === 0 }, function (st) {
|
||||
forEach(typedArrayNames, function (typedArray) {
|
||||
/* istanbul ignore if : covered in node 0.6 */
|
||||
if (typeof global[typedArray] === 'undefined') {
|
||||
st.comment('# SKIP typed array "' + typedArray + '" not supported');
|
||||
} else {
|
||||
st.ok(isCallable(global[typedArray]), typedArray + ' is callable');
|
||||
}
|
||||
st.ok(isCallable(global[typedArray]), typedArray + ' is callable');
|
||||
});
|
||||
st.end();
|
||||
});
|
||||
@@ -181,3 +153,18 @@ test('proxies of functions', { skip: !proxy }, function (t) {
|
||||
t.ok(isCallable(proxy), 'proxies of functions are callable');
|
||||
t.end();
|
||||
});
|
||||
|
||||
test('throwing functions', function (t) {
|
||||
t.plan(1);
|
||||
|
||||
var thrower = function (a) { return a.b; };
|
||||
t.ok(isCallable(thrower), 'a function that throws is callable');
|
||||
});
|
||||
|
||||
/* globals document: false */
|
||||
test('document.all', { skip: typeof document !== 'object' }, function (t) {
|
||||
t.notOk(isCallable(document), 'document is not callable');
|
||||
t.ok(isCallable(document.all), 'document.all is callable');
|
||||
|
||||
t.end();
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user