| Leo Repp | 58b9f11 | 2021-11-22 11:57:47 +0100 | [diff] [blame^] | 1 | var test = require('tape'); |
| 2 | var hasSymbols = require('has-symbols')(); |
| 3 | var utilInspect = require('../util.inspect'); |
| 4 | var repeat = require('string.prototype.repeat'); |
| 5 | |
| 6 | var inspect = require('..'); |
| 7 | |
| 8 | test('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 | |
| 16 | test('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 | |
| 30 | test('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 | |
| 45 | test('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 | }); |