Skip to content

Commit d8abb8a

Browse files
committed
[Tests] increase coverage
1 parent 0bb5fc5 commit d8abb8a

File tree

4 files changed

+80
-3
lines changed

4 files changed

+80
-3
lines changed

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
"core-js": "^2.6.12",
1010
"eslint": "^7.21.0",
1111
"for-each": "^0.3.3",
12+
"functions-have-names": "^1.2.2",
13+
"make-arrow-function": "^1.2.0",
1214
"nyc": "^10.3.2",
1315
"safe-publish-latest": "^1.1.4",
1416
"string.prototype.repeat": "^1.0.0",

test/deep.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ var inspect = require('../');
22
var test = require('tape');
33

44
test('deep', function (t) {
5-
t.plan(3);
5+
t.plan(4);
66
var obj = [[[[[[500]]]]]];
77
t.equal(inspect(obj), '[ [ [ [ [ [Array] ] ] ] ] ]');
88
t.equal(inspect(obj, { depth: 4 }), '[ [ [ [ [Array] ] ] ] ]');
99
t.equal(inspect(obj, { depth: 2 }), '[ [ [Array] ] ]');
10+
11+
t.equal(inspect([[[{ a: 1 }]]], { depth: 3 }), '[ [ [ [Object] ] ] ]');
1012
});

test/fn.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
var inspect = require('../');
22
var test = require('tape');
3+
var arrow = require('make-arrow-function')();
4+
var functionsHaveConfigurableNames = require('functions-have-names').functionsHaveConfigurableNames();
35

46
test('function', function (t) {
57
t.plan(1);
@@ -26,3 +28,49 @@ test('anon function', function (t) {
2628

2729
t.end();
2830
});
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+
});

test/inspect.js

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,16 @@ var repeat = require('string.prototype.repeat');
66
var inspect = require('..');
77

88
test('inspect', function (t) {
9-
t.plan(3);
9+
t.plan(4);
1010
var obj = [{ inspect: function xyzInspect() { return '!XYZ¡'; } }, []];
1111
t.equal(inspect(obj), '[ !XYZ¡, [] ]');
1212
t.equal(inspect(obj, { customInspect: true }), '[ !XYZ¡, [] ]');
1313
t.equal(inspect(obj, { customInspect: false }), '[ { inspect: [Function: xyzInspect] }, [] ]');
14+
t['throws'](
15+
function () { inspect(obj, { customInspect: 'not a boolean' }); },
16+
TypeError,
17+
'`customInspect` must be a boolean'
18+
);
1419
});
1520

1621
test('inspect custom symbol', { skip: !hasSymbols || !utilInspect || !utilInspect.custom }, function (t) {
@@ -43,11 +48,31 @@ test('symbols', { skip: !hasSymbols }, function (t) {
4348
});
4449

4550
test('maxStringLength', function (t) {
51+
t['throws'](
52+
function () { inspect('', { maxStringLength: -1 }); },
53+
TypeError,
54+
'maxStringLength must be >= 0, or Infinity, not negative'
55+
);
56+
57+
var str = repeat('a', 1e8);
58+
4659
t.equal(
47-
inspect([repeat('a', 1e8)], { maxStringLength: 10 }),
60+
inspect([str], { maxStringLength: 10 }),
4861
'[ \'aaaaaaaaaa\'... 99999990 more characters ]',
4962
'maxStringLength option limits output'
5063
);
5164

65+
t.equal(
66+
inspect(['f'], { maxStringLength: null }),
67+
'[ \'\'... 1 more character ]',
68+
'maxStringLength option accepts `null`'
69+
);
70+
71+
t.equal(
72+
inspect([str], { maxStringLength: Infinity }),
73+
'[ \'' + str + '\' ]',
74+
'maxStringLength option accepts ∞'
75+
);
76+
5277
t.end();
5378
});

0 commit comments

Comments
 (0)