inspect.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. var test = require('tape');
  2. var hasSymbols = require('has-symbols/shams')();
  3. var utilInspect = require('../util.inspect');
  4. var repeat = require('string.prototype.repeat');
  5. var inspect = require('..');
  6. test('inspect', function (t) {
  7. t.plan(5);
  8. var obj = [{ inspect: function xyzInspect() { return '!XYZ¡'; } }, []];
  9. var stringResult = '[ !XYZ¡, [] ]';
  10. var falseResult = '[ { inspect: [Function: xyzInspect] }, [] ]';
  11. t.equal(inspect(obj), stringResult);
  12. t.equal(inspect(obj, { customInspect: true }), stringResult);
  13. t.equal(inspect(obj, { customInspect: 'symbol' }), falseResult);
  14. t.equal(inspect(obj, { customInspect: false }), falseResult);
  15. t['throws'](
  16. function () { inspect(obj, { customInspect: 'not a boolean or "symbol"' }); },
  17. TypeError,
  18. '`customInspect` must be a boolean or the string "symbol"'
  19. );
  20. });
  21. test('inspect custom symbol', { skip: !hasSymbols || !utilInspect || !utilInspect.custom }, function (t) {
  22. t.plan(4);
  23. var obj = { inspect: function stringInspect() { return 'string'; } };
  24. obj[utilInspect.custom] = function custom() { return 'symbol'; };
  25. var symbolResult = '[ symbol, [] ]';
  26. var stringResult = '[ string, [] ]';
  27. var falseResult = '[ { inspect: [Function: stringInspect]' + (utilInspect.custom ? ', [' + inspect(utilInspect.custom) + ']: [Function: custom]' : '') + ' }, [] ]';
  28. var symbolStringFallback = utilInspect.custom ? symbolResult : stringResult;
  29. var symbolFalseFallback = utilInspect.custom ? symbolResult : falseResult;
  30. t.equal(inspect([obj, []]), symbolStringFallback);
  31. t.equal(inspect([obj, []], { customInspect: true }), symbolStringFallback);
  32. t.equal(inspect([obj, []], { customInspect: 'symbol' }), symbolFalseFallback);
  33. t.equal(inspect([obj, []], { customInspect: false }), falseResult);
  34. });
  35. test('symbols', { skip: !hasSymbols }, function (t) {
  36. t.plan(2);
  37. var obj = { a: 1 };
  38. obj[Symbol('test')] = 2;
  39. obj[Symbol.iterator] = 3;
  40. Object.defineProperty(obj, Symbol('non-enum'), {
  41. enumerable: false,
  42. value: 4
  43. });
  44. if (typeof Symbol.iterator === 'symbol') {
  45. t.equal(inspect(obj), '{ a: 1, [Symbol(test)]: 2, [Symbol(Symbol.iterator)]: 3 }', 'object with symbols');
  46. t.equal(inspect([obj, []]), '[ { a: 1, [Symbol(test)]: 2, [Symbol(Symbol.iterator)]: 3 }, [] ]', 'object with symbols in array');
  47. } else {
  48. // symbol sham key ordering is unreliable
  49. t.match(
  50. inspect(obj),
  51. /^(?:{ a: 1, \[Symbol\(test\)\]: 2, \[Symbol\(Symbol.iterator\)\]: 3 }|{ a: 1, \[Symbol\(Symbol.iterator\)\]: 3, \[Symbol\(test\)\]: 2 })$/,
  52. 'object with symbols (nondeterministic symbol sham key ordering)'
  53. );
  54. t.match(
  55. inspect([obj, []]),
  56. /^\[ (?:{ a: 1, \[Symbol\(test\)\]: 2, \[Symbol\(Symbol.iterator\)\]: 3 }|{ a: 1, \[Symbol\(Symbol.iterator\)\]: 3, \[Symbol\(test\)\]: 2 }), \[\] \]$/,
  57. 'object with symbols in array (nondeterministic symbol sham key ordering)'
  58. );
  59. }
  60. });
  61. test('maxStringLength', function (t) {
  62. t['throws'](
  63. function () { inspect('', { maxStringLength: -1 }); },
  64. TypeError,
  65. 'maxStringLength must be >= 0, or Infinity, not negative'
  66. );
  67. var str = repeat('a', 1e8);
  68. t.equal(
  69. inspect([str], { maxStringLength: 10 }),
  70. '[ \'aaaaaaaaaa\'... 99999990 more characters ]',
  71. 'maxStringLength option limits output'
  72. );
  73. t.equal(
  74. inspect(['f'], { maxStringLength: null }),
  75. '[ \'\'... 1 more character ]',
  76. 'maxStringLength option accepts `null`'
  77. );
  78. t.equal(
  79. inspect([str], { maxStringLength: Infinity }),
  80. '[ \'' + str + '\' ]',
  81. 'maxStringLength option accepts ∞'
  82. );
  83. t.end();
  84. });
  85. test('inspect options', { skip: !utilInspect.custom }, function (t) {
  86. var obj = {};
  87. obj[utilInspect.custom] = function () {
  88. return JSON.stringify(arguments);
  89. };
  90. t.equal(
  91. inspect(obj),
  92. utilInspect(obj, { depth: 5 }),
  93. 'custom symbols will use node\'s inspect'
  94. );
  95. t.equal(
  96. inspect(obj, { depth: 2 }),
  97. utilInspect(obj, { depth: 2 }),
  98. 'a reduced depth will be passed to node\'s inspect'
  99. );
  100. t.equal(
  101. inspect({ d1: obj }, { depth: 3 }),
  102. '{ d1: ' + utilInspect(obj, { depth: 2 }) + ' }',
  103. 'deep objects will receive a reduced depth'
  104. );
  105. t.equal(
  106. inspect({ d1: obj }, { depth: 1 }),
  107. '{ d1: [Object] }',
  108. 'unlike nodejs inspect, customInspect will not be used once the depth is exceeded.'
  109. );
  110. t.end();
  111. });
  112. test('inspect URL', { skip: typeof URL === 'undefined' }, function (t) {
  113. t.match(
  114. inspect(new URL('https://nodejs.org')),
  115. /nodejs\.org/, // Different environments stringify it differently
  116. 'url can be inspected'
  117. );
  118. t.end();
  119. });