inspect.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435
  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(inspect([obj, []], { customInspect: false }), '[ { inspect: [Function: stringInspect] }, [] ]');
  20. });
  21. test('maxStringLength', function (t) {
  22. t.equal(
  23. inspect([repeat('a', 1e8)], { maxStringLength: 10 }),
  24. '[ \'aaaaaaaaaa\'... 99999990 more characters ]',
  25. 'maxStringLength option limits output'
  26. );
  27. t.end();
  28. });