values.js 6.9 KB

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