inspect.js 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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(4);
  8. var obj = [{ inspect: function xyzInspect() { return '!XYZ¡'; } }, []];
  9. t.equal(inspect(obj), '[ !XYZ¡, [] ]');
  10. t.equal(inspect(obj, { customInspect: true }), '[ !XYZ¡, [] ]');
  11. t.equal(inspect(obj, { customInspect: false }), '[ { inspect: [Function: xyzInspect] }, [] ]');
  12. t['throws'](
  13. function () { inspect(obj, { customInspect: 'not a boolean' }); },
  14. TypeError,
  15. '`customInspect` must be a boolean'
  16. );
  17. });
  18. test('inspect custom symbol', { skip: !hasSymbols || !utilInspect || !utilInspect.custom }, function (t) {
  19. t.plan(3);
  20. var obj = { inspect: function stringInspect() { return 'string'; } };
  21. obj[utilInspect.custom] = function custom() { return 'symbol'; };
  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. test('symbols', { skip: !hasSymbols }, function (t) {
  30. t.plan(2);
  31. var obj = { a: 1 };
  32. obj[Symbol('test')] = 2;
  33. obj[Symbol.iterator] = 3;
  34. Object.defineProperty(obj, Symbol('non-enum'), {
  35. enumerable: false,
  36. value: 4
  37. });
  38. if (typeof Symbol.iterator === 'symbol') {
  39. t.equal(inspect(obj), '{ a: 1, [Symbol(test)]: 2, [Symbol(Symbol.iterator)]: 3 }', 'object with symbols');
  40. t.equal(inspect([obj, []]), '[ { a: 1, [Symbol(test)]: 2, [Symbol(Symbol.iterator)]: 3 }, [] ]', 'object with symbols in array');
  41. } else {
  42. // symbol sham key ordering is unreliable
  43. t.match(
  44. inspect(obj),
  45. /^(?:{ a: 1, \[Symbol\(test\)\]: 2, \[Symbol\(Symbol.iterator\)\]: 3 }|{ a: 1, \[Symbol\(Symbol.iterator\)\]: 3, \[Symbol\(test\)\]: 2 })$/,
  46. 'object with symbols (nondeterministic symbol sham key ordering)'
  47. );
  48. t.match(
  49. inspect([obj, []]),
  50. /^\[ (?:{ a: 1, \[Symbol\(test\)\]: 2, \[Symbol\(Symbol.iterator\)\]: 3 }|{ a: 1, \[Symbol\(Symbol.iterator\)\]: 3, \[Symbol\(test\)\]: 2 }), \[\] \]$/,
  51. 'object with symbols in array (nondeterministic symbol sham key ordering)'
  52. );
  53. }
  54. });
  55. test('maxStringLength', function (t) {
  56. t['throws'](
  57. function () { inspect('', { maxStringLength: -1 }); },
  58. TypeError,
  59. 'maxStringLength must be >= 0, or Infinity, not negative'
  60. );
  61. var str = repeat('a', 1e8);
  62. t.equal(
  63. inspect([str], { maxStringLength: 10 }),
  64. '[ \'aaaaaaaaaa\'... 99999990 more characters ]',
  65. 'maxStringLength option limits output'
  66. );
  67. t.equal(
  68. inspect(['f'], { maxStringLength: null }),
  69. '[ \'\'... 1 more character ]',
  70. 'maxStringLength option accepts `null`'
  71. );
  72. t.equal(
  73. inspect([str], { maxStringLength: Infinity }),
  74. '[ \'' + str + '\' ]',
  75. 'maxStringLength option accepts ∞'
  76. );
  77. t.end();
  78. });