blob: 368df01dba1bae5ce54a543823beda0c9c5904cb [file] [log] [blame]
Leo Repp58b9f112021-11-22 11:57:47 +01001var test = require('tape');
2var hasSymbols = require('has-symbols')();
3var utilInspect = require('../util.inspect');
4var repeat = require('string.prototype.repeat');
5
6var inspect = require('..');
7
8test('inspect', function (t) {
9 t.plan(3);
10 var obj = [{ inspect: function xyzInspect() { return '!XYZ¡'; } }, []];
11 t.equal(inspect(obj), '[ !XYZ¡, [] ]');
12 t.equal(inspect(obj, { customInspect: true }), '[ !XYZ¡, [] ]');
13 t.equal(inspect(obj, { customInspect: false }), '[ { inspect: [Function: xyzInspect] }, [] ]');
14});
15
16test('inspect custom symbol', { skip: !hasSymbols || !utilInspect || !utilInspect.custom }, function (t) {
17 t.plan(3);
18
19 var obj = { inspect: function stringInspect() { return 'string'; } };
20 obj[utilInspect.custom] = function custom() { return 'symbol'; };
21
22 t.equal(inspect([obj, []]), '[ ' + (utilInspect.custom ? 'symbol' : 'string') + ', [] ]');
23 t.equal(inspect([obj, []], { customInspect: true }), '[ ' + (utilInspect.custom ? 'symbol' : 'string') + ', [] ]');
24 t.equal(
25 inspect([obj, []], { customInspect: false }),
26 '[ { inspect: [Function: stringInspect]' + (utilInspect.custom ? ', [' + inspect(utilInspect.custom) + ']: [Function: custom]' : '') + ' }, [] ]'
27 );
28});
29
30test('symbols', { skip: !hasSymbols }, function (t) {
31 t.plan(2);
32
33 var obj = { a: 1 };
34 obj[Symbol('test')] = 2;
35 obj[Symbol.iterator] = 3;
36 Object.defineProperty(obj, Symbol('non-enum'), {
37 enumerable: false,
38 value: 4
39 });
40
41 t.equal(inspect(obj), '{ a: 1, [Symbol(test)]: 2, [Symbol(Symbol.iterator)]: 3 }', 'object with symbols');
42 t.equal(inspect([obj, []]), '[ { a: 1, [Symbol(test)]: 2, [Symbol(Symbol.iterator)]: 3 }, [] ]', 'object with symbols');
43});
44
45test('maxStringLength', function (t) {
46 t.equal(
47 inspect([repeat('a', 1e8)], { maxStringLength: 10 }),
48 '[ \'aaaaaaaaaa\'... 99999990 more characters ]',
49 'maxStringLength option limits output'
50 );
51
52 t.end();
53});