| 1 |
'use strict'; |
| 2 |
|
| 3 |
var test = require('tape'); |
| 4 |
|
| 5 |
var hasNames = require('../'); |
| 6 |
|
| 7 |
test('named functions', function (t) { |
| 8 |
function f() {} // eslint-disable-line func-style |
| 9 |
var g = function h() {}; |
| 10 |
|
| 11 |
t.equal(typeof hasNames, 'function', 'is a function'); |
| 12 |
t.equal(hasNames(), f.name === 'f' && g.name === 'h', 'functions have names or not as expected'); |
| 13 |
|
| 14 |
t.end(); |
| 15 |
}); |
| 16 |
|
| 17 |
var oDP = Object.defineProperty; |
| 18 |
if (oDP) { |
| 19 |
try { |
| 20 |
oDP({}, 'a', { value: 1 }); |
| 21 |
} catch (e) { |
| 22 |
oDP = null; |
| 23 |
} |
| 24 |
} |
| 25 |
|
| 26 |
test('functionsHaveConfigurableNames', function (t) { |
| 27 |
t.equal(typeof hasNames.functionsHaveConfigurableNames, 'function', 'is a function'); |
| 28 |
|
| 29 |
if (hasNames()) { |
| 30 |
var fn = function f() {}; |
| 31 |
if (oDP) { |
| 32 |
try { |
| 33 |
oDP(fn, 'name', { configurable: true, value: 'foo' }); |
| 34 |
} catch (e) {} |
| 35 |
if (fn.name === 'f') { |
| 36 |
t.equal(hasNames.functionsHaveConfigurableNames(), false, 'function names are not configurable'); |
| 37 |
} else if (fn.name === 'foo') { |
| 38 |
t.equal(hasNames.functionsHaveConfigurableNames(), true, 'function names are not configurable'); |
| 39 |
} else { |
| 40 |
t.fail('functions have names, but something surprising has happened. Please report this!'); |
| 41 |
} |
| 42 |
} else { |
| 43 |
t.equal(hasNames.functionsHaveConfigurableNames(), false, 'function names are not configurable'); |
| 44 |
} |
| 45 |
} else { |
| 46 |
t.equal(hasNames.functionsHaveConfigurableNames(), false, 'functions do not have names'); |
| 47 |
} |
| 48 |
|
| 49 |
t.end(); |
| 50 |
}); |
| 51 |
|
| 52 |
test('boundFunctionsHaveNames', function (t) { |
| 53 |
t.equal(typeof hasNames.boundFunctionsHaveNames, 'function', 'is a function'); |
| 54 |
|
| 55 |
var fn = function f() {}; |
| 56 |
if (typeof fn.bind !== 'function') { |
| 57 |
t.equal(hasNames.boundFunctionsHaveNames(), false, 'bound functions do not have names, because .bind does not exist'); |
| 58 |
} else if (hasNames()) { |
| 59 |
t.equal(hasNames.boundFunctionsHaveNames(), fn.bind().name !== '', 'bound functions have names'); |
| 60 |
} else { |
| 61 |
t.equal(hasNames.boundFunctionsHaveNames(), false, 'bound functions do not have names, because none do'); |
| 62 |
} |
| 63 |
|
| 64 |
t.end(); |
| 65 |
}); |