values.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. 'use strict';
  2. var inspect = require('../');
  3. var test = require('tape');
  4. var hasSymbols = require('has-symbols')();
  5. test('values', function (t) {
  6. t.plan(1);
  7. var obj = [{}, [], { 'a-b': 5 }];
  8. t.equal(inspect(obj), '[ {}, [], { \'a-b\': 5 } ]');
  9. });
  10. test('arrays with properties', function (t) {
  11. t.plan(1);
  12. var arr = [3];
  13. arr.foo = 'bar';
  14. var obj = [1, 2, arr];
  15. obj.baz = 'quux';
  16. obj.index = -1;
  17. t.equal(inspect(obj), '[ 1, 2, [ 3, foo: \'bar\' ], baz: \'quux\', index: -1 ]');
  18. });
  19. test('has', function (t) {
  20. t.plan(1);
  21. var has = Object.prototype.hasOwnProperty;
  22. delete Object.prototype.hasOwnProperty;
  23. t.equal(inspect({ a: 1, b: 2 }), '{ a: 1, b: 2 }');
  24. Object.prototype.hasOwnProperty = has; // eslint-disable-line no-extend-native
  25. });
  26. test('indexOf seen', function (t) {
  27. t.plan(1);
  28. var xs = [1, 2, 3, {}];
  29. xs.push(xs);
  30. var seen = [];
  31. seen.indexOf = undefined;
  32. t.equal(
  33. inspect(xs, {}, 0, seen),
  34. '[ 1, 2, 3, {}, [Circular] ]'
  35. );
  36. });
  37. test('seen seen', function (t) {
  38. t.plan(1);
  39. var xs = [1, 2, 3];
  40. var seen = [xs];
  41. seen.indexOf = undefined;
  42. t.equal(
  43. inspect(xs, {}, 0, seen),
  44. '[Circular]'
  45. );
  46. });
  47. test('seen seen seen', function (t) {
  48. t.plan(1);
  49. var xs = [1, 2, 3];
  50. var seen = [5, xs];
  51. seen.indexOf = undefined;
  52. t.equal(
  53. inspect(xs, {}, 0, seen),
  54. '[Circular]'
  55. );
  56. });
  57. test('symbols', { skip: typeof Symbol !== 'function' }, function (t) {
  58. var sym = Symbol('foo');
  59. t.equal(inspect(sym), 'Symbol(foo)', 'Symbol("foo") should be "Symbol(foo)"');
  60. t.equal(inspect(Object(sym)), 'Object(Symbol(foo))', 'Object(Symbol("foo")) should be "Object(Symbol(foo))"');
  61. t.test('toStringTag', { skip: !hasSymbols || typeof Symbol.toStringTag !== 'symbol' }, function (st) {
  62. st.plan(1);
  63. var faker = {};
  64. faker[Symbol.toStringTag] = 'Symbol';
  65. st.equal(
  66. inspect(faker),
  67. '{ [Symbol(Symbol.toStringTag)]: \'Symbol\' }',
  68. 'object lying about being a Symbol inspects as an object'
  69. );
  70. });
  71. t.end();
  72. });
  73. test('Map', { skip: typeof Map !== 'function' }, function (t) {
  74. var map = new Map();
  75. map.set({ a: 1 }, ['b']);
  76. map.set(3, NaN);
  77. var expectedString = 'Map (2) {' + inspect({ a: 1 }) + ' => ' + inspect(['b']) + ', 3 => NaN}';
  78. t.equal(inspect(map), expectedString, 'new Map([[{ a: 1 }, ["b"]], [3, NaN]]) should show size and contents');
  79. t.equal(inspect(new Map()), 'Map (0) {}', 'empty Map should show as empty');
  80. var nestedMap = new Map();
  81. nestedMap.set(nestedMap, map);
  82. t.equal(inspect(nestedMap), 'Map (1) {[Circular] => ' + expectedString + '}', 'Map containing a Map should work');
  83. t.end();
  84. });
  85. test('WeakMap', { skip: typeof WeakMap !== 'function' }, function (t) {
  86. var map = new WeakMap();
  87. map.set({ a: 1 }, ['b']);
  88. var expectedString = 'WeakMap { ? }';
  89. t.equal(inspect(map), expectedString, 'new WeakMap([[{ a: 1 }, ["b"]]]) should not show size or contents');
  90. t.equal(inspect(new WeakMap()), 'WeakMap { ? }', 'empty WeakMap should not show as empty');
  91. t.end();
  92. });
  93. test('Set', { skip: typeof Set !== 'function' }, function (t) {
  94. var set = new Set();
  95. set.add({ a: 1 });
  96. set.add(['b']);
  97. var expectedString = 'Set (2) {' + inspect({ a: 1 }) + ', ' + inspect(['b']) + '}';
  98. t.equal(inspect(set), expectedString, 'new Set([{ a: 1 }, ["b"]]) should show size and contents');
  99. t.equal(inspect(new Set()), 'Set (0) {}', 'empty Set should show as empty');
  100. var nestedSet = new Set();
  101. nestedSet.add(set);
  102. nestedSet.add(nestedSet);
  103. t.equal(inspect(nestedSet), 'Set (2) {' + expectedString + ', [Circular]}', 'Set containing a Set should work');
  104. t.end();
  105. });
  106. test('WeakSet', { skip: typeof WeakSet !== 'function' }, function (t) {
  107. var map = new WeakSet();
  108. map.add({ a: 1 });
  109. var expectedString = 'WeakSet { ? }';
  110. t.equal(inspect(map), expectedString, 'new WeakSet([{ a: 1 }]) should not show size or contents');
  111. t.equal(inspect(new WeakSet()), 'WeakSet { ? }', 'empty WeakSet should not show as empty');
  112. t.end();
  113. });
  114. test('WeakRef', { skip: typeof WeakRef !== 'function' }, function (t) {
  115. var ref = new WeakRef({ a: 1 });
  116. var expectedString = 'WeakRef { ? }';
  117. t.equal(inspect(ref), expectedString, 'new WeakRef({ a: 1 }) should not show contents');
  118. t.end();
  119. });
  120. test('FinalizationRegistry', { skip: typeof FinalizationRegistry !== 'function' }, function (t) {
  121. var registry = new FinalizationRegistry(function () {});
  122. var expectedString = 'FinalizationRegistry [FinalizationRegistry] {}';
  123. t.equal(inspect(registry), expectedString, 'new FinalizationRegistry(function () {}) should work normallys');
  124. t.end();
  125. });
  126. test('Strings', function (t) {
  127. var str = 'abc';
  128. t.equal(inspect(str), "'" + str + "'", 'primitive string shows as such');
  129. t.equal(inspect(str, { quoteStyle: 'single' }), "'" + str + "'", 'primitive string shows as such, single quoted');
  130. t.equal(inspect(str, { quoteStyle: 'double' }), '"' + str + '"', 'primitive string shows as such, double quoted');
  131. t.equal(inspect(Object(str)), 'Object(' + inspect(str) + ')', 'String object shows as such');
  132. t.equal(inspect(Object(str), { quoteStyle: 'single' }), 'Object(' + inspect(str, { quoteStyle: 'single' }) + ')', 'String object shows as such, single quoted');
  133. t.equal(inspect(Object(str), { quoteStyle: 'double' }), 'Object(' + inspect(str, { quoteStyle: 'double' }) + ')', 'String object shows as such, double quoted');
  134. t.end();
  135. });
  136. test('Numbers', function (t) {
  137. var num = 42;
  138. t.equal(inspect(num), String(num), 'primitive number shows as such');
  139. t.equal(inspect(Object(num)), 'Object(' + inspect(num) + ')', 'Number object shows as such');
  140. t.end();
  141. });
  142. test('Booleans', function (t) {
  143. t.equal(inspect(true), String(true), 'primitive true shows as such');
  144. t.equal(inspect(Object(true)), 'Object(' + inspect(true) + ')', 'Boolean object true shows as such');
  145. t.equal(inspect(false), String(false), 'primitive false shows as such');
  146. t.equal(inspect(Object(false)), 'Object(' + inspect(false) + ')', 'Boolean false object shows as such');
  147. t.end();
  148. });
  149. test('Date', function (t) {
  150. var now = new Date();
  151. t.equal(inspect(now), String(now), 'Date shows properly');
  152. t.equal(inspect(new Date(NaN)), 'Invalid Date', 'Invalid Date shows properly');
  153. t.end();
  154. });
  155. test('RegExps', function (t) {
  156. t.equal(inspect(/a/g), '/a/g', 'regex shows properly');
  157. t.equal(inspect(new RegExp('abc', 'i')), '/abc/i', 'new RegExp shows properly');
  158. var match = 'abc abc'.match(/[ab]+/);
  159. delete match.groups; // for node < 10
  160. t.equal(inspect(match), '[ \'ab\', index: 0, input: \'abc abc\' ]', 'RegExp match object shows properly');
  161. t.end();
  162. });