source: imaps-frontend/node_modules/functions-have-names/test/index.js

main
Last change on this file was d565449, checked in by stefan toskovski <stefantoska84@…>, 4 weeks ago

Update repo after prototype presentation

  • Property mode set to 100644
File size: 1.9 KB
Line 
1'use strict';
2
3var test = require('tape');
4
5var hasNames = require('../');
6
7test('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
17var oDP = Object.defineProperty;
18if (oDP) {
19 try {
20 oDP({}, 'a', { value: 1 });
21 } catch (e) {
22 oDP = null;
23 }
24}
25
26test('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
52test('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});
Note: See TracBrowser for help on using the repository browser.