1 | var inspect = require('../');
|
---|
2 | var test = require('tape');
|
---|
3 | var arrow = require('make-arrow-function')();
|
---|
4 | var functionsHaveConfigurableNames = require('functions-have-names').functionsHaveConfigurableNames();
|
---|
5 |
|
---|
6 | test('function', function (t) {
|
---|
7 | t.plan(1);
|
---|
8 | var obj = [1, 2, function f(n) { return n; }, 4];
|
---|
9 | t.equal(inspect(obj), '[ 1, 2, [Function: f], 4 ]');
|
---|
10 | });
|
---|
11 |
|
---|
12 | test('function name', function (t) {
|
---|
13 | t.plan(1);
|
---|
14 | var f = (function () {
|
---|
15 | return function () {};
|
---|
16 | }());
|
---|
17 | f.toString = function toStr() { return 'function xxx () {}'; };
|
---|
18 | var obj = [1, 2, f, 4];
|
---|
19 | t.equal(inspect(obj), '[ 1, 2, [Function (anonymous)] { toString: [Function: toStr] }, 4 ]');
|
---|
20 | });
|
---|
21 |
|
---|
22 | test('anon function', function (t) {
|
---|
23 | var f = (function () {
|
---|
24 | return function () {};
|
---|
25 | }());
|
---|
26 | var obj = [1, 2, f, 4];
|
---|
27 | t.equal(inspect(obj), '[ 1, 2, [Function (anonymous)], 4 ]');
|
---|
28 |
|
---|
29 | t.end();
|
---|
30 | });
|
---|
31 |
|
---|
32 | test('arrow function', { skip: !arrow }, function (t) {
|
---|
33 | t.equal(inspect(arrow), '[Function (anonymous)]');
|
---|
34 |
|
---|
35 | t.end();
|
---|
36 | });
|
---|
37 |
|
---|
38 | test('truly nameless function', { skip: !arrow || !functionsHaveConfigurableNames }, function (t) {
|
---|
39 | function f() {}
|
---|
40 | Object.defineProperty(f, 'name', { value: false });
|
---|
41 | t.equal(f.name, false);
|
---|
42 | t.equal(
|
---|
43 | inspect(f),
|
---|
44 | '[Function: f]',
|
---|
45 | 'named function with falsy `.name` does not hide its original name'
|
---|
46 | );
|
---|
47 |
|
---|
48 | function g() {}
|
---|
49 | Object.defineProperty(g, 'name', { value: true });
|
---|
50 | t.equal(g.name, true);
|
---|
51 | t.equal(
|
---|
52 | inspect(g),
|
---|
53 | '[Function: true]',
|
---|
54 | 'named function with truthy `.name` hides its original name'
|
---|
55 | );
|
---|
56 |
|
---|
57 | var anon = function () {}; // eslint-disable-line func-style
|
---|
58 | Object.defineProperty(anon, 'name', { value: null });
|
---|
59 | t.equal(anon.name, null);
|
---|
60 | t.equal(
|
---|
61 | inspect(anon),
|
---|
62 | '[Function (anonymous)]',
|
---|
63 | 'anon function with falsy `.name` does not hide its anonymity'
|
---|
64 | );
|
---|
65 |
|
---|
66 | var anon2 = function () {}; // eslint-disable-line func-style
|
---|
67 | Object.defineProperty(anon2, 'name', { value: 1 });
|
---|
68 | t.equal(anon2.name, 1);
|
---|
69 | t.equal(
|
---|
70 | inspect(anon2),
|
---|
71 | '[Function: 1]',
|
---|
72 | 'anon function with truthy `.name` hides its anonymity'
|
---|
73 | );
|
---|
74 |
|
---|
75 | t.end();
|
---|
76 | });
|
---|