values.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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. var forEach = require('for-each');
  8. var semver = require('semver');
  9. test('values', function (t) {
  10. t.plan(1);
  11. var obj = [{}, [], { 'a-b': 5 }];
  12. t.equal(inspect(obj), '[ {}, [], { \'a-b\': 5 } ]');
  13. });
  14. test('arrays with properties', function (t) {
  15. t.plan(1);
  16. var arr = [3];
  17. arr.foo = 'bar';
  18. var obj = [1, 2, arr];
  19. obj.baz = 'quux';
  20. obj.index = -1;
  21. t.equal(inspect(obj), '[ 1, 2, [ 3, foo: \'bar\' ], baz: \'quux\', index: -1 ]');
  22. });
  23. test('has', function (t) {
  24. t.plan(1);
  25. t.teardown(mockProperty(Object.prototype, 'hasOwnProperty', { 'delete': true }));
  26. t.equal(inspect({ a: 1, b: 2 }), '{ a: 1, b: 2 }');
  27. });
  28. test('indexOf seen', function (t) {
  29. t.plan(1);
  30. var xs = [1, 2, 3, {}];
  31. xs.push(xs);
  32. var seen = [];
  33. seen.indexOf = undefined;
  34. t.equal(
  35. inspect(xs, {}, 0, seen),
  36. '[ 1, 2, 3, {}, [Circular] ]'
  37. );
  38. });
  39. test('seen seen', function (t) {
  40. t.plan(1);
  41. var xs = [1, 2, 3];
  42. var seen = [xs];
  43. seen.indexOf = undefined;
  44. t.equal(
  45. inspect(xs, {}, 0, seen),
  46. '[Circular]'
  47. );
  48. });
  49. test('seen seen seen', function (t) {
  50. t.plan(1);
  51. var xs = [1, 2, 3];
  52. var seen = [5, xs];
  53. seen.indexOf = undefined;
  54. t.equal(
  55. inspect(xs, {}, 0, seen),
  56. '[Circular]'
  57. );
  58. });
  59. test('symbols', { skip: !hasSymbols }, function (t) {
  60. var sym = Symbol('foo');
  61. t.equal(inspect(sym), 'Symbol(foo)', 'Symbol("foo") should be "Symbol(foo)"');
  62. if (typeof sym === 'symbol') {
  63. // Symbol shams are incapable of differentiating boxed from unboxed symbols
  64. t.equal(inspect(Object(sym)), 'Object(Symbol(foo))', 'Object(Symbol("foo")) should be "Object(Symbol(foo))"');
  65. }
  66. t.test('toStringTag', { skip: !hasToStringTag }, function (st) {
  67. st.plan(1);
  68. var faker = {};
  69. faker[Symbol.toStringTag] = 'Symbol';
  70. st.equal(
  71. inspect(faker),
  72. '{ [Symbol(Symbol.toStringTag)]: \'Symbol\' }',
  73. 'object lying about being a Symbol inspects as an object'
  74. );
  75. });
  76. t.end();
  77. });
  78. test('Map', { skip: typeof Map !== 'function' }, function (t) {
  79. var map = new Map();
  80. map.set({ a: 1 }, ['b']);
  81. map.set(3, NaN);
  82. var expectedString = 'Map (2) {' + inspect({ a: 1 }) + ' => ' + inspect(['b']) + ', 3 => NaN}';
  83. t.equal(inspect(map), expectedString, 'new Map([[{ a: 1 }, ["b"]], [3, NaN]]) should show size and contents');
  84. t.equal(inspect(new Map()), 'Map (0) {}', 'empty Map should show as empty');
  85. var nestedMap = new Map();
  86. nestedMap.set(nestedMap, map);
  87. t.equal(inspect(nestedMap), 'Map (1) {[Circular] => ' + expectedString + '}', 'Map containing a Map should work');
  88. t.end();
  89. });
  90. test('WeakMap', { skip: typeof WeakMap !== 'function' }, function (t) {
  91. var map = new WeakMap();
  92. map.set({ a: 1 }, ['b']);
  93. var expectedString = 'WeakMap { ? }';
  94. t.equal(inspect(map), expectedString, 'new WeakMap([[{ a: 1 }, ["b"]]]) should not show size or contents');
  95. t.equal(inspect(new WeakMap()), 'WeakMap { ? }', 'empty WeakMap should not show as empty');
  96. t.end();
  97. });
  98. test('Set', { skip: typeof Set !== 'function' }, function (t) {
  99. var set = new Set();
  100. set.add({ a: 1 });
  101. set.add(['b']);
  102. var expectedString = 'Set (2) {' + inspect({ a: 1 }) + ', ' + inspect(['b']) + '}';
  103. t.equal(inspect(set), expectedString, 'new Set([{ a: 1 }, ["b"]]) should show size and contents');
  104. t.equal(inspect(new Set()), 'Set (0) {}', 'empty Set should show as empty');
  105. var nestedSet = new Set();
  106. nestedSet.add(set);
  107. nestedSet.add(nestedSet);
  108. t.equal(inspect(nestedSet), 'Set (2) {' + expectedString + ', [Circular]}', 'Set containing a Set should work');
  109. t.end();
  110. });
  111. test('WeakSet', { skip: typeof WeakSet !== 'function' }, function (t) {
  112. var map = new WeakSet();
  113. map.add({ a: 1 });
  114. var expectedString = 'WeakSet { ? }';
  115. t.equal(inspect(map), expectedString, 'new WeakSet([{ a: 1 }]) should not show size or contents');
  116. t.equal(inspect(new WeakSet()), 'WeakSet { ? }', 'empty WeakSet should not show as empty');
  117. t.end();
  118. });
  119. test('WeakRef', { skip: typeof WeakRef !== 'function' }, function (t) {
  120. var ref = new WeakRef({ a: 1 });
  121. var expectedString = 'WeakRef { ? }';
  122. t.equal(inspect(ref), expectedString, 'new WeakRef({ a: 1 }) should not show contents');
  123. t.end();
  124. });
  125. test('FinalizationRegistry', { skip: typeof FinalizationRegistry !== 'function' }, function (t) {
  126. var registry = new FinalizationRegistry(function () {});
  127. var expectedString = 'FinalizationRegistry [FinalizationRegistry] {}';
  128. t.equal(inspect(registry), expectedString, 'new FinalizationRegistry(function () {}) should work normallys');
  129. t.end();
  130. });
  131. test('Strings', function (t) {
  132. var str = 'abc';
  133. t.equal(inspect(str), "'" + str + "'", 'primitive string shows as such');
  134. t.equal(inspect(str, { quoteStyle: 'single' }), "'" + str + "'", 'primitive string shows as such, single quoted');
  135. t.equal(inspect(str, { quoteStyle: 'double' }), '"' + str + '"', 'primitive string shows as such, double quoted');
  136. t.equal(inspect(Object(str)), 'Object(' + inspect(str) + ')', 'String object shows as such');
  137. t.equal(inspect(Object(str), { quoteStyle: 'single' }), 'Object(' + inspect(str, { quoteStyle: 'single' }) + ')', 'String object shows as such, single quoted');
  138. t.equal(inspect(Object(str), { quoteStyle: 'double' }), 'Object(' + inspect(str, { quoteStyle: 'double' }) + ')', 'String object shows as such, double quoted');
  139. t.end();
  140. });
  141. test('Numbers', function (t) {
  142. var num = 42;
  143. t.equal(inspect(num), String(num), 'primitive number shows as such');
  144. t.equal(inspect(Object(num)), 'Object(' + inspect(num) + ')', 'Number object shows as such');
  145. t.end();
  146. });
  147. test('Booleans', function (t) {
  148. t.equal(inspect(true), String(true), 'primitive true shows as such');
  149. t.equal(inspect(Object(true)), 'Object(' + inspect(true) + ')', 'Boolean object true shows as such');
  150. t.equal(inspect(false), String(false), 'primitive false shows as such');
  151. t.equal(inspect(Object(false)), 'Object(' + inspect(false) + ')', 'Boolean false object shows as such');
  152. t.end();
  153. });
  154. test('Date', function (t) {
  155. var now = new Date();
  156. t.equal(inspect(now), String(now), 'Date shows properly');
  157. t.equal(inspect(new Date(NaN)), 'Invalid Date', 'Invalid Date shows properly');
  158. t.end();
  159. });
  160. test('RegExps', function (t) {
  161. t.equal(inspect(/a/g), '/a/g', 'regex shows properly');
  162. t.equal(inspect(new RegExp('abc', 'i')), '/abc/i', 'new RegExp shows properly');
  163. var match = 'abc abc'.match(/[ab]+/);
  164. delete match.groups; // for node < 10
  165. t.equal(inspect(match), '[ \'ab\', index: 0, input: \'abc abc\' ]', 'RegExp match object shows properly');
  166. t.end();
  167. });
  168. test('Proxies', { skip: typeof Proxy !== 'function' || !hasToStringTag }, function (t) {
  169. var target = { proxy: true };
  170. var fake = new Proxy(target, { has: function () { return false; } });
  171. // needed to work around a weird difference in node v6.0 - v6.4 where non-present properties are not logged
  172. var isNode60 = semver.satisfies(process.version, '6.0 - 6.4');
  173. forEach([
  174. 'Boolean',
  175. 'Number',
  176. 'String',
  177. 'Symbol',
  178. 'Date'
  179. ], function (tag) {
  180. target[Symbol.toStringTag] = tag;
  181. t.equal(
  182. inspect(fake),
  183. '{ ' + (isNode60 ? '' : 'proxy: true, ') + '[Symbol(Symbol.toStringTag)]: \'' + tag + '\' }',
  184. 'Proxy for + ' + tag + ' shows as the target, which has no slots'
  185. );
  186. });
  187. t.end();
  188. });
  189. test('fakers', { skip: !hasToStringTag }, function (t) {
  190. var target = { proxy: false };
  191. forEach([
  192. 'Boolean',
  193. 'Number',
  194. 'String',
  195. 'Symbol',
  196. 'Date'
  197. ], function (tag) {
  198. target[Symbol.toStringTag] = tag;
  199. t.equal(
  200. inspect(target),
  201. '{ proxy: false, [Symbol(Symbol.toStringTag)]: \'' + tag + '\' }',
  202. 'Object pretending to be ' + tag + ' does not trick us'
  203. );
  204. });
  205. t.end();
  206. });