values.js 6.9 KB

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