inspect.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. });