inspect.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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. var inspect = require('..');
  6. test('inspect', function (t) {
  7. t.plan(3);
  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. });
  13. test('inspect custom symbol', { skip: !hasSymbols || !utilInspect || !utilInspect.custom }, function (t) {
  14. t.plan(3);
  15. var obj = { inspect: function stringInspect() { return 'string'; } };
  16. obj[utilInspect.custom] = function custom() { return 'symbol'; };
  17. t.equal(inspect([obj, []]), '[ ' + (utilInspect.custom ? 'symbol' : 'string') + ', [] ]');
  18. t.equal(inspect([obj, []], { customInspect: true }), '[ ' + (utilInspect.custom ? 'symbol' : 'string') + ', [] ]');
  19. t.equal(
  20. inspect([obj, []], { customInspect: false }),
  21. '[ { inspect: [Function: stringInspect]' + (utilInspect.custom ? ', [' + inspect(utilInspect.custom) + ']: [Function: custom]' : '') + ' }, [] ]'
  22. );
  23. });
  24. test('symbols', { skip: !hasSymbols }, function (t) {
  25. t.plan(2);
  26. var obj = { a: 1 };
  27. obj[Symbol('test')] = 2;
  28. obj[Symbol.iterator] = 3;
  29. Object.defineProperty(obj, Symbol('non-enum'), {
  30. enumerable: false,
  31. value: 4
  32. });
  33. t.equal(inspect(obj), '{ a: 1, [Symbol(test)]: 2, [Symbol(Symbol.iterator)]: 3 }', 'object with symbols');
  34. t.equal(inspect([obj, []]), '[ { a: 1, [Symbol(test)]: 2, [Symbol(Symbol.iterator)]: 3 }, [] ]', 'object with symbols');
  35. });
  36. test('maxStringLength', function (t) {
  37. t.equal(
  38. inspect([repeat('a', 1e8)], { maxStringLength: 10 }),
  39. '[ \'aaaaaaaaaa\'... 99999990 more characters ]',
  40. 'maxStringLength option limits output'
  41. );
  42. t.end();
  43. });