You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

values.js 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. var inspect = require('../');
  2. var test = require('tape');
  3. test('values', function (t) {
  4. t.plan(1);
  5. var obj = [{}, [], { 'a-b': 5 }];
  6. t.equal(inspect(obj), '[ {}, [], { \'a-b\': 5 } ]');
  7. });
  8. test('arrays with properties', function (t) {
  9. t.plan(1);
  10. var arr = [3];
  11. arr.foo = 'bar';
  12. var obj = [1, 2, arr];
  13. obj.baz = 'quux';
  14. obj.index = -1;
  15. t.equal(inspect(obj), '[ 1, 2, [ 3, foo: \'bar\' ], baz: \'quux\', index: -1 ]');
  16. });
  17. test('has', function (t) {
  18. t.plan(1);
  19. var has = Object.prototype.hasOwnProperty;
  20. delete Object.prototype.hasOwnProperty;
  21. t.equal(inspect({ a: 1, b: 2 }), '{ a: 1, b: 2 }');
  22. Object.prototype.hasOwnProperty = has; // eslint-disable-line no-extend-native
  23. });
  24. test('indexOf seen', function (t) {
  25. t.plan(1);
  26. var xs = [1, 2, 3, {}];
  27. xs.push(xs);
  28. var seen = [];
  29. seen.indexOf = undefined;
  30. t.equal(
  31. inspect(xs, {}, 0, seen),
  32. '[ 1, 2, 3, {}, [Circular] ]'
  33. );
  34. });
  35. test('seen seen', function (t) {
  36. t.plan(1);
  37. var xs = [1, 2, 3];
  38. var seen = [xs];
  39. seen.indexOf = undefined;
  40. t.equal(
  41. inspect(xs, {}, 0, seen),
  42. '[Circular]'
  43. );
  44. });
  45. test('seen seen seen', function (t) {
  46. t.plan(1);
  47. var xs = [1, 2, 3];
  48. var seen = [5, xs];
  49. seen.indexOf = undefined;
  50. t.equal(
  51. inspect(xs, {}, 0, seen),
  52. '[Circular]'
  53. );
  54. });
  55. test('symbols', { skip: typeof Symbol !== 'function' }, function (t) {
  56. var sym = Symbol('foo');
  57. t.equal(inspect(sym), 'Symbol(foo)', 'Symbol("foo") should be "Symbol(foo)"');
  58. t.equal(inspect(Object(sym)), 'Object(Symbol(foo))', 'Object(Symbol("foo")) should be "Object(Symbol(foo))"');
  59. t.end();
  60. });
  61. test('Map', { skip: typeof Map !== 'function' }, function (t) {
  62. var map = new Map();
  63. map.set({ a: 1 }, ['b']);
  64. map.set(3, NaN);
  65. var expectedString = 'Map (2) {' + inspect({ a: 1 }) + ' => ' + inspect(['b']) + ', 3 => NaN}';
  66. t.equal(inspect(map), expectedString, 'new Map([[{ a: 1 }, ["b"]], [3, NaN]]) should show size and contents');
  67. t.equal(inspect(new Map()), 'Map (0) {}', 'empty Map should show as empty');
  68. var nestedMap = new Map();
  69. nestedMap.set(nestedMap, map);
  70. t.equal(inspect(nestedMap), 'Map (1) {[Circular] => ' + expectedString + '}', 'Map containing a Map should work');
  71. t.end();
  72. });
  73. test('WeakMap', { skip: typeof WeakMap !== 'function' }, function (t) {
  74. var map = new WeakMap();
  75. map.set({ a: 1 }, ['b']);
  76. var expectedString = 'WeakMap { ? }';
  77. t.equal(inspect(map), expectedString, 'new WeakMap([[{ a: 1 }, ["b"]]]) should not show size or contents');
  78. t.equal(inspect(new WeakMap()), 'WeakMap { ? }', 'empty WeakMap should not show as empty');
  79. t.end();
  80. });
  81. test('Set', { skip: typeof Set !== 'function' }, function (t) {
  82. var set = new Set();
  83. set.add({ a: 1 });
  84. set.add(['b']);
  85. var expectedString = 'Set (2) {' + inspect({ a: 1 }) + ', ' + inspect(['b']) + '}';
  86. t.equal(inspect(set), expectedString, 'new Set([{ a: 1 }, ["b"]]) should show size and contents');
  87. t.equal(inspect(new Set()), 'Set (0) {}', 'empty Set should show as empty');
  88. var nestedSet = new Set();
  89. nestedSet.add(set);
  90. nestedSet.add(nestedSet);
  91. t.equal(inspect(nestedSet), 'Set (2) {' + expectedString + ', [Circular]}', 'Set containing a Set should work');
  92. t.end();
  93. });
  94. test('WeakSet', { skip: typeof WeakSet !== 'function' }, function (t) {
  95. var map = new WeakSet();
  96. map.add({ a: 1 });
  97. var expectedString = 'WeakSet { ? }';
  98. t.equal(inspect(map), expectedString, 'new WeakSet([{ a: 1 }]) should not show size or contents');
  99. t.equal(inspect(new WeakSet()), 'WeakSet { ? }', 'empty WeakSet should not show as empty');
  100. t.end();
  101. });
  102. test('Strings', function (t) {
  103. var str = 'abc';
  104. t.equal(inspect(str), "'" + str + "'", 'primitive string shows as such');
  105. t.equal(inspect(str, { quoteStyle: 'single' }), "'" + str + "'", 'primitive string shows as such, single quoted');
  106. t.equal(inspect(str, { quoteStyle: 'double' }), '"' + str + '"', 'primitive string shows as such, double quoted');
  107. t.equal(inspect(Object(str)), 'Object(' + inspect(str) + ')', 'String object shows as such');
  108. t.equal(inspect(Object(str), { quoteStyle: 'single' }), 'Object(' + inspect(str, { quoteStyle: 'single' }) + ')', 'String object shows as such, single quoted');
  109. t.equal(inspect(Object(str), { quoteStyle: 'double' }), 'Object(' + inspect(str, { quoteStyle: 'double' }) + ')', 'String object shows as such, double quoted');
  110. t.end();
  111. });
  112. test('Numbers', function (t) {
  113. var num = 42;
  114. t.equal(inspect(num), String(num), 'primitive number shows as such');
  115. t.equal(inspect(Object(num)), 'Object(' + inspect(num) + ')', 'Number object shows as such');
  116. t.end();
  117. });
  118. test('Booleans', function (t) {
  119. t.equal(inspect(true), String(true), 'primitive true shows as such');
  120. t.equal(inspect(Object(true)), 'Object(' + inspect(true) + ')', 'Boolean object true shows as such');
  121. t.equal(inspect(false), String(false), 'primitive false shows as such');
  122. t.equal(inspect(Object(false)), 'Object(' + inspect(false) + ')', 'Boolean false object shows as such');
  123. t.end();
  124. });